1 /* $NetBSD: mdreloc.c,v 1.47 2024/11/30 01:04:05 christos Exp $ */ 2 3 #include <sys/cdefs.h> 4 #ifndef lint 5 __RCSID("$NetBSD: mdreloc.c,v 1.47 2024/11/30 01:04:05 christos Exp $"); 6 #endif /* not lint */ 7 8 #include <sys/types.h> 9 #include <machine/lwp_private.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 18 #define rdbg_symname(obj, rela) \ 19 ((obj)->strtab + (obj)->symtab[ELF_R_SYM((rela)->r_info)].st_name) 20 21 void 22 _rtld_setup_pltgot(const Obj_Entry *obj) 23 { 24 obj->pltgot[1] = (Elf_Addr) obj; 25 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 26 } 27 28 void 29 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 30 { 31 const Elf_Rel *rel = 0, *rellim; 32 Elf_Addr relsz = 0; 33 Elf_Addr *where; 34 35 for (; dynp->d_tag != DT_NULL; dynp++) { 36 switch (dynp->d_tag) { 37 case DT_REL: 38 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr); 39 break; 40 case DT_RELSZ: 41 relsz = dynp->d_un.d_val; 42 break; 43 } 44 } 45 if (rel == 0 || relsz == 0) 46 return; 47 rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz); 48 for (; rel < rellim; rel++) { 49 where = (Elf_Addr *)(relocbase + rel->r_offset); 50 *where += (Elf_Addr)relocbase; 51 } 52 } 53 54 int 55 _rtld_relocate_nonplt_objects(Obj_Entry *obj) 56 { 57 const Elf_Rel *rel; 58 Elf_Addr target = 0; 59 const Elf_Sym *def = NULL; 60 const Obj_Entry *defobj = NULL; 61 unsigned long last_symnum = ULONG_MAX; 62 63 for (rel = obj->rel; rel < obj->rellim; rel++) { 64 Elf_Addr *where; 65 Elf_Addr tmp; 66 unsigned long symnum; 67 68 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 69 70 switch (ELF_R_TYPE(rel->r_info)) { 71 case R_TYPE(PC32): 72 case R_TYPE(GOT32): 73 case R_TYPE(32): 74 case R_TYPE(GLOB_DAT): 75 case R_TYPE(TLS_TPOFF): 76 case R_TYPE(TLS_TPOFF32): 77 case R_TYPE(TLS_DTPMOD32): 78 case R_TYPE(TLS_DTPOFF32): 79 symnum = ELF_R_SYM(rel->r_info); 80 if (symnum != last_symnum) { 81 last_symnum = symnum; 82 def = _rtld_find_symdef(symnum, obj, &defobj, 83 false); 84 if (def == NULL) 85 return -1; 86 } 87 break; 88 default: 89 break; 90 } 91 92 93 switch (ELF_R_TYPE(rel->r_info)) { 94 case R_TYPE(NONE): 95 break; 96 97 #if 1 /* XXX should not occur */ 98 case R_TYPE(PC32): 99 target = (Elf_Addr)(defobj->relocbase + def->st_value); 100 101 *where += target - (Elf_Addr)where; 102 rdbg(("PC32 %s in %s --> %p in %s", 103 rdbg_symname(obj, rel), 104 obj->path, (void *)*where, defobj->path)); 105 break; 106 107 case R_TYPE(GOT32): 108 #endif 109 case R_TYPE(32): 110 case R_TYPE(GLOB_DAT): 111 target = (Elf_Addr)(defobj->relocbase + def->st_value); 112 113 tmp = target + *where; 114 if (*where != tmp) 115 *where = tmp; 116 rdbg(("32/GLOB_DAT %s in %s --> %p in %s", 117 rdbg_symname(obj, rel), 118 obj->path, (void *)*where, defobj->path)); 119 break; 120 121 122 case R_TYPE(IRELATIVE): 123 /* IFUNC relocations are handled in _rtld_call_ifunc */ 124 if (obj->ifunc_remaining_nonplt == 0) { 125 obj->ifunc_remaining_nonplt = 126 obj->rellim - rel; 127 } 128 /* FALL-THROUGH */ 129 130 case R_TYPE(RELATIVE): 131 *where += (Elf_Addr)obj->relocbase; 132 rdbg(("RELATIVE in %s --> %p", obj->path, 133 (void *)*where)); 134 break; 135 136 case R_TYPE(COPY): 137 /* 138 * These are deferred until all other relocations have 139 * been done. All we do here is make sure that the 140 * COPY relocation is not in a shared library. They 141 * are allowed only in executable files. 142 */ 143 if (obj->isdynamic) { 144 _rtld_error( 145 "%s: Unexpected R_COPY relocation in shared library", 146 obj->path); 147 return -1; 148 } 149 rdbg(("COPY (avoid in main)")); 150 break; 151 152 case R_TYPE(TLS_TPOFF): 153 if (!defobj->tls_static && 154 _rtld_tls_offset_allocate(__UNCONST(defobj))) 155 return -1; 156 157 *where += (Elf_Addr)(def->st_value - defobj->tlsoffset); 158 159 rdbg(("TLS_TPOFF %s in %s --> %p", 160 rdbg_symname(obj, rel), 161 obj->path, (void *)*where)); 162 break; 163 164 case R_TYPE(TLS_TPOFF32): 165 if (!defobj->tls_static && 166 _rtld_tls_offset_allocate(__UNCONST(defobj))) 167 return -1; 168 169 *where += (Elf_Addr)(defobj->tlsoffset - def->st_value); 170 rdbg(("TLS_TPOFF32 %s in %s --> %p", 171 rdbg_symname(obj, rel), 172 obj->path, (void *)*where)); 173 break; 174 175 case R_TYPE(TLS_DTPMOD32): 176 *where = (Elf_Addr)(defobj->tlsindex); 177 178 rdbg(("TLS_DTPMOD32 %s in %s --> %p", 179 rdbg_symname(obj, rel), 180 obj->path, (void *)*where)); 181 break; 182 183 case R_TYPE(TLS_DTPOFF32): 184 *where = (Elf_Addr)(def->st_value); 185 186 rdbg(("TLS_DTPOFF32 %s in %s --> %p", 187 rdbg_symname(obj, rel), 188 obj->path, (void *)*where)); 189 190 break; 191 192 default: 193 rdbg(("sym = %lu, type = %lu, offset = %p, " 194 "contents = %p, symbol = %s", 195 (u_long)ELF_R_SYM(rel->r_info), 196 (u_long)ELF_R_TYPE(rel->r_info), 197 (void *)rel->r_offset, (void *)*where, 198 rdbg_symname(obj, rel))); 199 _rtld_error("%s: Unsupported relocation type %ld " 200 "in non-PLT relocations", 201 obj->path, (u_long) ELF_R_TYPE(rel->r_info)); 202 return -1; 203 } 204 } 205 return 0; 206 } 207 208 int 209 _rtld_relocate_plt_lazy(Obj_Entry *obj) 210 { 211 const Elf_Rel *rel; 212 213 for (rel = obj->pltrellim; rel-- > obj->pltrel; ) { 214 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 215 216 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT) || 217 ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)); 218 219 if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) 220 obj->ifunc_remaining = obj->pltrellim - rel; 221 222 /* Just relocate the GOT slots pointing into the PLT */ 223 *where += (Elf_Addr)obj->relocbase; 224 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 225 } 226 227 return 0; 228 } 229 230 static inline int 231 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel, 232 Elf_Addr *tp) 233 { 234 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 235 Elf_Addr target; 236 const Elf_Sym *def; 237 const Obj_Entry *defobj; 238 unsigned long info = rel->r_info; 239 240 if (ELF_R_TYPE(info) == R_TYPE(IRELATIVE)) 241 return 0; 242 243 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT)); 244 245 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL); 246 if (__predict_false(def == NULL)) 247 return -1; 248 if (__predict_false(def == &_rtld_sym_zero)) 249 return 0; 250 251 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 252 if (tp == NULL) 253 return 0; 254 target = _rtld_resolve_ifunc(defobj, def); 255 } else { 256 target = (Elf_Addr)(defobj->relocbase + def->st_value); 257 } 258 259 rdbg(("bind now/fixup in %s --> old=%p new=%p", 260 defobj->strtab + def->st_name, (void *)*where, 261 (void *)target)); 262 if (*where != target) 263 *where = target; 264 if (tp) 265 *tp = target; 266 return 0; 267 } 268 269 caddr_t 270 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 271 { 272 const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel 273 + reloff); 274 Elf_Addr new_value; 275 int err; 276 277 new_value = 0; /* XXX gcc */ 278 279 _rtld_shared_enter(); 280 err = _rtld_relocate_plt_object(obj, rel, &new_value); 281 if (err) 282 _rtld_die(); 283 _rtld_shared_exit(); 284 285 return (caddr_t)new_value; 286 } 287 288 int 289 _rtld_relocate_plt_objects(const Obj_Entry *obj) 290 { 291 const Elf_Rel *rel; 292 int err = 0; 293 294 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) { 295 err = _rtld_relocate_plt_object(obj, rel, NULL); 296 if (err) 297 break; 298 } 299 return err; 300 } 301 302 /* 303 * i386 specific GNU variant of __tls_get_addr using register based 304 * argument passing. 305 */ 306 #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1])) 307 308 __dso_public __attribute__((__regparm__(1))) void * 309 ___tls_get_addr(void *arg_) 310 { 311 size_t *arg = (size_t *)arg_; 312 void **dtv; 313 struct tls_tcb *tcb = __lwp_getprivate_fast(); 314 size_t idx = arg[0], offset = arg[1]; 315 316 dtv = tcb->tcb_dtv; 317 318 if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL)) 319 return (uint8_t *)dtv[idx] + offset; 320 321 return _rtld_tls_get_addr(tcb, idx, offset); 322 } 323