1 /*- 2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 /* 29 * Dynamic linker for ELF. 30 * 31 * John Polstra <jdp@polstra.com>. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/mman.h> 36 #include <machine/segments.h> 37 #include <machine/sysarch.h> 38 39 #include <dlfcn.h> 40 #include <err.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <stdarg.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 #include "debug.h" 50 #include "rtld.h" 51 #include "rtld_tls.h" 52 53 /* 54 * Process the special R_386_COPY relocations in the main program. These 55 * copy data from a shared object into a region in the main program's BSS 56 * segment. 57 * 58 * Returns 0 on success, -1 on failure. 59 */ 60 int 61 do_copy_relocations(Obj_Entry *dstobj) 62 { 63 const Elf_Rel *rellim; 64 const Elf_Rel *rel; 65 66 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */ 67 68 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize); 69 for (rel = dstobj->rel; rel < rellim; rel++) { 70 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) { 71 void *dstaddr; 72 const Elf_Sym *dstsym; 73 const char *name; 74 size_t size; 75 const void *srcaddr; 76 const Elf_Sym *srcsym; 77 const Obj_Entry *srcobj, *defobj; 78 SymLook req; 79 int res; 80 81 dstaddr = (void *) (dstobj->relocbase + rel->r_offset); 82 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info); 83 name = dstobj->strtab + dstsym->st_name; 84 size = dstsym->st_size; 85 symlook_init(&req, name); 86 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info)); 87 req.flags = SYMLOOK_EARLY; 88 89 for (srcobj = globallist_next(dstobj); srcobj != NULL; 90 srcobj = globallist_next(srcobj)) { 91 res = symlook_obj(&req, srcobj); 92 if (res == 0) { 93 srcsym = req.sym_out; 94 defobj = req.defobj_out; 95 break; 96 } 97 } 98 99 if (srcobj == NULL) { 100 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 101 " relocation in %s", name, dstobj->path); 102 return -1; 103 } 104 105 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value); 106 memcpy(dstaddr, srcaddr, size); 107 } 108 } 109 110 return 0; 111 } 112 113 /* Initialize the special GOT entries. */ 114 void 115 init_pltgot(Obj_Entry *obj) 116 { 117 if (obj->pltgot != NULL) { 118 obj->pltgot[1] = (Elf_Addr) obj; 119 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 120 } 121 } 122 123 /* Process the non-PLT relocations. */ 124 int 125 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags, 126 RtldLockState *lockstate) 127 { 128 const Elf_Rel *rellim; 129 const Elf_Rel *rel; 130 SymCache *cache; 131 const Elf_Sym *def; 132 const Obj_Entry *defobj; 133 Elf_Addr *where, symval, add; 134 int r; 135 136 r = -1; 137 /* 138 * The dynamic loader may be called from a thread, we have 139 * limited amounts of stack available so we cannot use alloca(). 140 */ 141 if (obj != obj_rtld) { 142 cache = calloc(obj->dynsymcount, sizeof(SymCache)); 143 /* No need to check for NULL here */ 144 } else 145 cache = NULL; 146 147 rellim = (const Elf_Rel *)((caddr_t) obj->rel + obj->relsize); 148 for (rel = obj->rel; rel < rellim; rel++) { 149 switch (ELF_R_TYPE(rel->r_info)) { 150 case R_386_32: 151 case R_386_PC32: 152 case R_386_GLOB_DAT: 153 case R_386_TLS_TPOFF: 154 case R_386_TLS_TPOFF32: 155 case R_386_TLS_DTPMOD32: 156 case R_386_TLS_DTPOFF32: 157 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 158 flags, cache, lockstate); 159 if (def == NULL) 160 goto done; 161 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 162 switch (ELF_R_TYPE(rel->r_info)) { 163 case R_386_32: 164 case R_386_PC32: 165 case R_386_GLOB_DAT: 166 if ((flags & SYMLOOK_IFUNC) == 0) { 167 obj->non_plt_gnu_ifunc = true; 168 continue; 169 } 170 symval = (Elf_Addr)rtld_resolve_ifunc( 171 defobj, def); 172 break; 173 case R_386_TLS_TPOFF: 174 case R_386_TLS_TPOFF32: 175 case R_386_TLS_DTPMOD32: 176 case R_386_TLS_DTPOFF32: 177 _rtld_error("%s: IFUNC for TLS reloc", 178 obj->path); 179 goto done; 180 } 181 } else { 182 if ((flags & SYMLOOK_IFUNC) != 0) 183 continue; 184 symval = (Elf_Addr)defobj->relocbase + 185 def->st_value; 186 } 187 break; 188 default: 189 if ((flags & SYMLOOK_IFUNC) != 0) 190 continue; 191 break; 192 } 193 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 194 195 switch (ELF_R_TYPE(rel->r_info)) { 196 case R_386_NONE: 197 break; 198 case R_386_32: 199 *where += symval; 200 break; 201 case R_386_PC32: 202 /* 203 * I don't think the dynamic linker should ever 204 * see this type of relocation. But the 205 * binutils-2.6 tools sometimes generate it. 206 */ 207 *where += symval - (Elf_Addr)where; 208 break; 209 case R_386_COPY: 210 /* 211 * These are deferred until all other 212 * relocations have been done. All we do here 213 * is make sure that the COPY relocation is 214 * not in a shared library. They are allowed 215 * only in executable files. 216 */ 217 if (!obj->mainprog) { 218 _rtld_error("%s: Unexpected R_386_COPY " 219 "relocation in shared library", obj->path); 220 goto done; 221 } 222 break; 223 case R_386_GLOB_DAT: 224 *where = symval; 225 break; 226 case R_386_RELATIVE: 227 *where += (Elf_Addr)obj->relocbase; 228 break; 229 case R_386_TLS_TPOFF: 230 case R_386_TLS_TPOFF32: 231 /* 232 * We lazily allocate offsets for static TLS 233 * as we see the first relocation that 234 * references the TLS block. This allows us to 235 * support (small amounts of) static TLS in 236 * dynamically loaded modules. If we run out 237 * of space, we generate an error. 238 */ 239 if (!defobj->tls_done) { 240 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 241 _rtld_error("%s: No space available " 242 "for static Thread Local Storage", 243 obj->path); 244 goto done; 245 } 246 } 247 add = (Elf_Addr)(def->st_value - defobj->tlsoffset); 248 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF) 249 *where += add; 250 else 251 *where -= add; 252 break; 253 case R_386_TLS_DTPMOD32: 254 *where += (Elf_Addr)defobj->tlsindex; 255 break; 256 case R_386_TLS_DTPOFF32: 257 *where += (Elf_Addr) def->st_value; 258 break; 259 default: 260 _rtld_error("%s: Unsupported relocation type %d" 261 " in non-PLT relocations\n", obj->path, 262 ELF_R_TYPE(rel->r_info)); 263 goto done; 264 } 265 } 266 r = 0; 267 done: 268 free(cache); 269 return (r); 270 } 271 272 /* Process the PLT relocations. */ 273 int 274 reloc_plt(Obj_Entry *obj) 275 { 276 const Elf_Rel *rellim; 277 const Elf_Rel *rel; 278 279 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 280 for (rel = obj->pltrel; rel < rellim; rel++) { 281 Elf_Addr *where/*, val*/; 282 283 switch (ELF_R_TYPE(rel->r_info)) { 284 case R_386_JMP_SLOT: 285 /* Relocate the GOT slot pointing into the PLT. */ 286 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 287 *where += (Elf_Addr)obj->relocbase; 288 break; 289 290 case R_386_IRELATIVE: 291 obj->irelative = true; 292 break; 293 294 default: 295 _rtld_error("Unknown relocation type %x in PLT", 296 ELF_R_TYPE(rel->r_info)); 297 return (-1); 298 } 299 } 300 return 0; 301 } 302 303 /* Relocate the jump slots in an object. */ 304 int 305 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate) 306 { 307 const Elf_Rel *rellim; 308 const Elf_Rel *rel; 309 310 if (obj->jmpslots_done) 311 return 0; 312 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 313 for (rel = obj->pltrel; rel < rellim; rel++) { 314 Elf_Addr *where, target; 315 const Elf_Sym *def; 316 const Obj_Entry *defobj; 317 318 switch (ELF_R_TYPE(rel->r_info)) { 319 case R_386_JMP_SLOT: 320 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 321 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 322 SYMLOOK_IN_PLT | flags, NULL, lockstate); 323 if (def == NULL) 324 return (-1); 325 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 326 obj->gnu_ifunc = true; 327 continue; 328 } 329 target = (Elf_Addr)(defobj->relocbase + def->st_value); 330 reloc_jmpslot(where, target, defobj, obj, rel); 331 break; 332 333 case R_386_IRELATIVE: 334 break; 335 336 default: 337 _rtld_error("Unknown relocation type %x in PLT", 338 ELF_R_TYPE(rel->r_info)); 339 return (-1); 340 } 341 } 342 343 obj->jmpslots_done = true; 344 return 0; 345 } 346 347 /* Fixup the jump slot at "where" to transfer control to "target". */ 348 Elf_Addr 349 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, 350 const struct Struct_Obj_Entry *obj, const struct Struct_Obj_Entry *refobj, 351 const Elf_Rel *rel) 352 { 353 #ifdef dbg 354 dbg("reloc_jmpslot: *%p = %p", where, (void *)target); 355 #endif 356 if (!ld_bind_not) 357 *where = target; 358 return (target); 359 } 360 361 int 362 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate) 363 { 364 const Elf_Rel *rellim; 365 const Elf_Rel *rel; 366 Elf_Addr *where, target; 367 368 if (!obj->irelative) 369 return (0); 370 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 371 for (rel = obj->pltrel; rel < rellim; rel++) { 372 switch (ELF_R_TYPE(rel->r_info)) { 373 case R_386_IRELATIVE: 374 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 375 lock_release(rtld_bind_lock, lockstate); 376 target = call_ifunc_resolver(obj->relocbase + *where); 377 wlock_acquire(rtld_bind_lock, lockstate); 378 *where = target; 379 break; 380 } 381 } 382 obj->irelative = false; 383 return (0); 384 } 385 386 int 387 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate) 388 { 389 const Elf_Rel *rellim; 390 const Elf_Rel *rel; 391 392 if (!obj->gnu_ifunc) 393 return (0); 394 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize); 395 for (rel = obj->pltrel; rel < rellim; rel++) { 396 Elf_Addr *where, target; 397 const Elf_Sym *def; 398 const Obj_Entry *defobj; 399 400 switch (ELF_R_TYPE(rel->r_info)) { 401 case R_386_JMP_SLOT: 402 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 403 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 404 SYMLOOK_IN_PLT | flags, NULL, lockstate); 405 if (def == NULL) 406 return (-1); 407 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC) 408 continue; 409 lock_release(rtld_bind_lock, lockstate); 410 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def); 411 wlock_acquire(rtld_bind_lock, lockstate); 412 reloc_jmpslot(where, target, defobj, obj, rel); 413 break; 414 } 415 } 416 417 obj->gnu_ifunc = false; 418 return (0); 419 } 420 421 uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2; 422 423 static void 424 rtld_cpuid_count(int idx, int cnt, u_int *p) 425 { 426 427 __asm __volatile( 428 " pushl %%ebx\n" 429 " cpuid\n" 430 " movl %%ebx,%1\n" 431 " popl %%ebx\n" 432 : "=a" (p[0]), "=r" (p[1]), "=c" (p[2]), "=d" (p[3]) 433 : "0" (idx), "2" (cnt)); 434 } 435 436 void 437 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) 438 { 439 u_int p[4], cpu_high; 440 int cpuid_supported; 441 442 __asm __volatile( 443 " pushfl\n" 444 " popl %%eax\n" 445 " movl %%eax,%%ecx\n" 446 " xorl $0x200000,%%eax\n" 447 " pushl %%eax\n" 448 " popfl\n" 449 " pushfl\n" 450 " popl %%eax\n" 451 " xorl %%eax,%%ecx\n" 452 " je 1f\n" 453 " movl $1,%0\n" 454 " jmp 2f\n" 455 "1: movl $0,%0\n" 456 "2:\n" 457 : "=r" (cpuid_supported) : : "eax", "ecx"); 458 if (!cpuid_supported) 459 return; 460 461 rtld_cpuid_count(1, 0, p); 462 cpu_feature = p[3]; 463 cpu_feature2 = p[2]; 464 rtld_cpuid_count(0, 0, p); 465 cpu_high = p[0]; 466 if (cpu_high >= 7) { 467 rtld_cpuid_count(7, 0, p); 468 cpu_stdext_feature = p[1]; 469 cpu_stdext_feature2 = p[2]; 470 } 471 } 472 473 void 474 allocate_initial_tls(Obj_Entry *objs) 475 { 476 void* tls; 477 478 /* 479 * Fix the size of the static TLS block by using the maximum 480 * offset allocated so far and adding a bit for dynamic modules to 481 * use. 482 */ 483 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA; 484 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr)); 485 i386_set_gsbase(tls); 486 } 487 488 /* GNU ABI */ 489 __attribute__((__regparm__(1))) 490 void *___tls_get_addr(tls_index *ti) 491 { 492 Elf_Addr** segbase; 493 494 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase)); 495 496 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset); 497 } 498 499 /* Sun ABI */ 500 void *__tls_get_addr(tls_index *ti) 501 { 502 Elf_Addr** segbase; 503 504 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase)); 505 506 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset); 507 } 508