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