1 /* $NetBSD: mdreloc.c,v 1.35 2025/04/16 18:01:01 riastradh Exp $ */ 2 3 /* 4 * Copyright 1996 John D. Polstra. 5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by John Polstra. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #ifndef lint 36 __RCSID("$NetBSD: mdreloc.c,v 1.35 2025/04/16 18:01:01 riastradh Exp $"); 37 #endif /* not lint */ 38 39 #include <sys/types.h> 40 41 #include "debug.h" 42 #include "rtld.h" 43 44 void _rtld_bind_start(void); 45 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 46 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 47 static inline int _rtld_relocate_plt_object(const Obj_Entry *, 48 const Elf_Rela *, Elf_Addr *); 49 50 void 51 _rtld_setup_pltgot(const Obj_Entry *obj) 52 { 53 obj->pltgot[1] = (Elf_Addr) obj; 54 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 55 } 56 57 void 58 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 59 { 60 const Elf_Rela *rela = 0, *relalim; 61 Elf_Addr relasz = 0; 62 Elf_Addr *where; 63 64 for (; dynp->d_tag != DT_NULL; dynp++) { 65 switch (dynp->d_tag) { 66 case DT_RELA: 67 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr); 68 break; 69 case DT_RELASZ: 70 relasz = dynp->d_un.d_val; 71 break; 72 } 73 } 74 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz); 75 for (; rela < relalim; rela++) { 76 where = (Elf_Addr *)(relocbase + rela->r_offset); 77 *where = (Elf_Addr)(relocbase + rela->r_addend); 78 } 79 } 80 81 int 82 _rtld_relocate_nonplt_objects(Obj_Entry *obj) 83 { 84 const Elf_Rela *rela; 85 const Elf_Sym *def = NULL; 86 const Obj_Entry *defobj = NULL; 87 unsigned long last_symnum = ULONG_MAX; 88 89 for (rela = obj->rela; rela < obj->relalim; rela++) { 90 Elf_Addr *where; 91 Elf_Addr tmp; 92 unsigned long symnum; 93 94 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 95 96 switch (ELF_R_TYPE(rela->r_info)) { 97 case R_TYPE(32): /* word32 S + A */ 98 case R_TYPE(GLOB_DAT): /* word32 S + A */ 99 symnum = ELF_R_SYM(rela->r_info); 100 if (last_symnum != symnum) { 101 last_symnum = symnum; 102 def = _rtld_find_symdef(symnum, obj, &defobj, 103 false); 104 if (def == NULL) 105 return -1; 106 } 107 break; 108 109 default: 110 break; 111 } 112 113 switch (ELF_R_TYPE(rela->r_info)) { 114 case R_TYPE(NONE): 115 break; 116 117 case R_TYPE(32): /* word32 S + A */ 118 case R_TYPE(GLOB_DAT): /* word32 S + A */ 119 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 120 rela->r_addend); 121 122 if (*where != tmp) 123 *where = tmp; 124 rdbg(("32/GLOB_DAT %s in %s --> %p in %s", 125 obj->strtab + obj->symtab[symnum].st_name, 126 obj->path, (void *)*where, defobj->path)); 127 break; 128 129 case R_TYPE(RELATIVE): /* word32 B + A */ 130 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend); 131 if (*where != tmp) 132 *where = tmp; 133 rdbg(("RELATIVE in %s --> %p", obj->path, 134 (void *)*where)); 135 break; 136 137 case R_TYPE(COPY): 138 /* 139 * These are deferred until all other relocations have 140 * been done. All we do here is make sure that the 141 * COPY relocation is not in a shared library. They 142 * are allowed only in executable files. 143 */ 144 if (obj->isdynamic) { 145 _rtld_error( 146 "%s: Unexpected R_COPY relocation in shared library", 147 obj->path); 148 return -1; 149 } 150 rdbg(("COPY (avoid in main)")); 151 break; 152 153 default: 154 rdbg(("sym = %lu, type = %lu, offset = %p, " 155 "addend = %p, contents = %p", 156 (u_long)ELF_R_SYM(rela->r_info), 157 (u_long)ELF_R_TYPE(rela->r_info), 158 (void *)rela->r_offset, (void *)rela->r_addend, 159 (void *)*where)); 160 _rtld_error("%s: Unsupported relocation type %ld " 161 "in non-PLT relocations", 162 obj->path, (u_long) ELF_R_TYPE(rela->r_info)); 163 return -1; 164 } 165 } 166 return 0; 167 } 168 169 int 170 _rtld_relocate_plt_lazy(Obj_Entry *obj) 171 { 172 const Elf_Rela *rela; 173 174 if (!obj->relocbase) 175 return 0; 176 177 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) { 178 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 179 180 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 181 182 /* Just relocate the GOT slots pointing into the PLT */ 183 *where += (Elf_Addr)obj->relocbase; 184 rdbg(("lazy fixup pltgot %p in %s --> %p", where, obj->path, 185 (void *)*where)); 186 } 187 188 return 0; 189 } 190 191 static inline int 192 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp) 193 { 194 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 195 Elf_Addr new_value; 196 const Elf_Sym *def; 197 const Obj_Entry *defobj; 198 unsigned long info = rela->r_info; 199 200 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT)); 201 202 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL); 203 if (__predict_false(def == NULL)) 204 return -1; 205 if (__predict_false(def == &_rtld_sym_zero)) 206 return 0; 207 208 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 209 if (tp == NULL) 210 return 0; 211 new_value = _rtld_resolve_ifunc(defobj, def); 212 } else { 213 new_value = (Elf_Addr)(defobj->relocbase + def->st_value + 214 rela->r_addend); 215 } 216 rdbg(("bind now/fixup pltgot %p in %s --> old=%p new=%p", where, 217 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 218 if (*where != new_value) 219 *where = new_value; 220 221 if (tp) 222 *tp = new_value - rela->r_addend; 223 224 return 0; 225 } 226 227 caddr_t 228 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 229 { 230 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff); 231 Elf_Addr result; 232 int err; 233 234 result = 0; /* XXX gcc */ 235 236 _rtld_shared_enter(); 237 err = _rtld_relocate_plt_object(obj, rela, &result); 238 if (err) 239 _rtld_die(); 240 _rtld_shared_exit(); 241 242 return (caddr_t)result; 243 } 244 245 int 246 _rtld_relocate_plt_objects(const Obj_Entry *obj) 247 { 248 const Elf_Rela *rela; 249 250 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) 251 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0) 252 return -1; 253 254 return 0; 255 } 256