1 /* $NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $ */ 2 3 #include <sys/cdefs.h> 4 #ifndef lint 5 __RCSID("$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $"); 6 #endif /* not lint */ 7 8 #include <sys/types.h> 9 #include <sys/tls.h> 10 11 #include "debug.h" 12 #include "rtld.h" 13 14 void _rtld_bind_start(void); 15 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 16 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 17 static inline int _rtld_relocate_plt_object(const Obj_Entry *, 18 const Elf_Rela *, Elf_Addr *); 19 20 void 21 _rtld_setup_pltgot(const Obj_Entry *obj) 22 { 23 obj->pltgot[1] = (Elf_Addr) obj; 24 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 25 } 26 27 void 28 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 29 { 30 const Elf_Rela *rela = 0, *relalim; 31 Elf_Addr relasz = 0; 32 Elf_Addr *where; 33 34 for (; dynp->d_tag != DT_NULL; dynp++) { 35 switch (dynp->d_tag) { 36 case DT_RELA: 37 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr); 38 break; 39 case DT_RELASZ: 40 relasz = dynp->d_un.d_val; 41 break; 42 } 43 } 44 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz); 45 for (; rela < relalim; rela++) { 46 where = (Elf_Addr *)(relocbase + rela->r_offset); 47 *where = (Elf_Addr)(relocbase + rela->r_addend); 48 } 49 } 50 51 int 52 _rtld_relocate_nonplt_objects(Obj_Entry *obj) 53 { 54 const Elf_Rela *rela; 55 const Elf_Sym *def = NULL; 56 const Obj_Entry *defobj = NULL; 57 unsigned long last_symnum = ULONG_MAX; 58 59 for (rela = obj->rela; rela < obj->relalim; rela++) { 60 Elf_Addr *where; 61 Elf_Addr tmp; 62 unsigned long symnum; 63 64 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 65 66 switch (ELF_R_TYPE(rela->r_info)) { 67 case R_TYPE(GOT32): 68 case R_TYPE(REL32): 69 case R_TYPE(DIR32): 70 case R_TYPE(GLOB_DAT): 71 case R_TYPE(TLS_DTPOFF32): 72 case R_TYPE(TLS_DTPMOD32): 73 case R_TYPE(TLS_TPOFF32): 74 symnum = ELF_R_SYM(rela->r_info); 75 if (last_symnum != symnum) { 76 last_symnum = symnum; 77 def = _rtld_find_symdef(symnum, obj, &defobj, 78 false); 79 if (def == NULL) 80 return -1; 81 } 82 break; 83 default: 84 break; 85 } 86 87 switch (ELF_R_TYPE(rela->r_info)) { 88 case R_TYPE(NONE): 89 break; 90 91 #if 1 /* XXX should not occur */ 92 case R_TYPE(GOT32): 93 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 94 rela->r_addend); 95 if (*where != tmp) 96 *where = tmp; 97 rdbg(("GOT32 %s in %s --> %p in %s", 98 obj->strtab + obj->symtab[symnum].st_name, 99 obj->path, (void *)*where, defobj->path)); 100 break; 101 102 case R_TYPE(REL32): 103 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 104 rela->r_addend) - (Elf_Addr)where; 105 if (*where != tmp) 106 *where = tmp; 107 rdbg(("PC32 %s in %s --> %p in %s", 108 obj->strtab + obj->symtab[symnum].st_name, 109 obj->path, (void *)*where, defobj->path)); 110 break; 111 #endif 112 113 case R_TYPE(DIR32): 114 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 115 rela->r_addend); 116 if (*where != tmp) 117 *where = tmp; 118 rdbg(("32 %s in %s --> %p in %s", 119 obj->strtab + obj->symtab[symnum].st_name, 120 obj->path, (void *)*where, defobj->path)); 121 break; 122 123 case R_TYPE(GLOB_DAT): 124 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) + 125 rela->r_addend; 126 if (*where != tmp) 127 *where = tmp; 128 rdbg(("GLOB_DAT %s in %s --> %p in %s", 129 obj->strtab + obj->symtab[symnum].st_name, 130 obj->path, (void *)*where, defobj->path)); 131 break; 132 133 case R_TYPE(RELATIVE): 134 if (rela->r_addend) 135 *where = (Elf_Addr)obj->relocbase + rela->r_addend; 136 else 137 *where += (Elf_Addr)obj->relocbase; 138 rdbg(("RELATIVE in %s --> %p", obj->path, 139 (void *)*where)); 140 break; 141 142 case R_TYPE(COPY): 143 /* 144 * These are deferred until all other relocations have 145 * been done. All we do here is make sure that the 146 * COPY relocation is not in a shared library. They 147 * are allowed only in executable files. 148 */ 149 if (obj->isdynamic) { 150 _rtld_error( 151 "%s: Unexpected R_COPY relocation in shared library", 152 obj->path); 153 return -1; 154 } 155 rdbg(("COPY (avoid in main)")); 156 break; 157 158 case R_TYPE(TLS_DTPOFF32): 159 *where = (Elf_Addr)(def->st_value); 160 161 rdbg(("TLS_DTPOFF32 %s in %s --> %p", 162 obj->strtab + obj->symtab[symnum].st_name, 163 obj->path, (void *)*where)); 164 165 break; 166 case R_TYPE(TLS_DTPMOD32): 167 *where = (Elf_Addr)(defobj->tlsindex); 168 169 rdbg(("TLS_DTPMOD32 %s in %s --> %p", 170 obj->strtab + obj->symtab[symnum].st_name, 171 obj->path, (void *)*where)); 172 173 break; 174 175 case R_TYPE(TLS_TPOFF32): 176 if (!defobj->tls_static && 177 _rtld_tls_offset_allocate(__UNCONST(defobj))) 178 return -1; 179 180 *where = (Elf_Addr)def->st_value + 181 rela->r_addend + defobj->tlsoffset + 182 sizeof(struct tls_tcb); 183 184 rdbg(("TLS_TPOFF32 %s in %s --> %p", 185 obj->strtab + obj->symtab[symnum].st_name, 186 obj->path, (void *)*where)); 187 break; 188 189 default: 190 rdbg(("sym = %lu, type = %lu, offset = %p, " 191 "addend = %p, contents = %p, symbol = %s", 192 (u_long)ELF_R_SYM(rela->r_info), 193 (u_long)ELF_R_TYPE(rela->r_info), 194 (void *)rela->r_offset, (void *)rela->r_addend, 195 (void *)*where, 196 obj->strtab + obj->symtab[symnum].st_name)); 197 _rtld_error("%s: Unsupported relocation type %ld " 198 "in non-PLT relocations", 199 obj->path, (u_long) ELF_R_TYPE(rela->r_info)); 200 return -1; 201 } 202 } 203 return 0; 204 } 205 206 int 207 _rtld_relocate_plt_lazy(Obj_Entry *obj) 208 { 209 const Elf_Rela *rela; 210 211 if (!obj->relocbase) 212 return 0; 213 214 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) { 215 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 216 217 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 218 219 /* Just relocate the GOT slots pointing into the PLT */ 220 *where += (Elf_Addr)obj->relocbase; 221 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 222 } 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 new_value; 232 int err; 233 234 new_value = 0; /* XXX gcc */ 235 236 _rtld_shared_enter(); 237 err = _rtld_relocate_plt_object(obj, rela, &new_value); 238 if (err) 239 _rtld_die(); 240 _rtld_shared_exit(); 241 242 return (caddr_t)new_value; 243 } 244 245 int 246 _rtld_relocate_plt_objects(const Obj_Entry *obj) 247 { 248 const Elf_Rela *rela = obj->pltrela; 249 250 for (; rela < obj->pltrelalim; rela++) 251 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0) 252 return -1; 253 254 return 0; 255 } 256 257 static inline int 258 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp) 259 { 260 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 261 Elf_Addr new_value; 262 const Elf_Sym *def; 263 const Obj_Entry *defobj; 264 unsigned long info = rela->r_info; 265 266 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT)); 267 268 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL); 269 if (__predict_false(def == NULL)) 270 return -1; 271 if (__predict_false(def == &_rtld_sym_zero)) 272 return 0; 273 274 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 275 if (tp == NULL) 276 return 0; 277 new_value = _rtld_resolve_ifunc(defobj, def); 278 } else { 279 new_value = (Elf_Addr)(defobj->relocbase + def->st_value); 280 } 281 rdbg(("bind now/fixup in %s --> old=%p new=%p", 282 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 283 if (*where != new_value) 284 *where = new_value; 285 286 if (tp) 287 *tp = new_value; 288 289 return 0; 290 } 291