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 <jdp@polstra.com>. 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 *)((const char *) 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 /* 142 * The dynamic loader may be called from a thread, we have 143 * limited amounts of stack available so we cannot use alloca(). 144 */ 145 if (obj != obj_rtld) { 146 cache = calloc(obj->dynsymcount, sizeof(SymCache)); 147 /* No need to check for NULL here */ 148 } else 149 cache = NULL; 150 151 relalim = (const Elf_Rela *)((const char*)obj->rela + obj->relasize); 152 for (rela = obj->rela; rela < relalim; rela++) { 153 /* 154 * First, resolve symbol for relocations which 155 * reference symbols. 156 */ 157 switch (ELF_R_TYPE(rela->r_info)) { 158 case R_X86_64_64: 159 case R_X86_64_PC32: 160 case R_X86_64_GLOB_DAT: 161 case R_X86_64_TPOFF64: 162 case R_X86_64_TPOFF32: 163 case R_X86_64_DTPMOD64: 164 case R_X86_64_DTPOFF64: 165 case R_X86_64_DTPOFF32: 166 def = find_symdef(ELF_R_SYM(rela->r_info), obj, 167 &defobj, flags, cache, lockstate); 168 if (def == NULL) 169 goto done; 170 /* 171 * If symbol is IFUNC, only perform relocation 172 * when caller allowed it by passing 173 * SYMLOOK_IFUNC flag. Skip the relocations 174 * otherwise. 175 * 176 * Also error out in case IFUNC relocations 177 * are specified for TLS, which cannot be 178 * usefully interpreted. 179 */ 180 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 181 switch (ELF_R_TYPE(rela->r_info)) { 182 case R_X86_64_64: 183 case R_X86_64_PC32: 184 case R_X86_64_GLOB_DAT: 185 if ((flags & SYMLOOK_IFUNC) == 0) { 186 obj->non_plt_gnu_ifunc = true; 187 continue; 188 } 189 symval = (Elf_Addr)rtld_resolve_ifunc( 190 defobj, def); 191 break; 192 case R_X86_64_TPOFF64: 193 case R_X86_64_TPOFF32: 194 case R_X86_64_DTPMOD64: 195 case R_X86_64_DTPOFF64: 196 case R_X86_64_DTPOFF32: 197 _rtld_error("%s: IFUNC for TLS reloc", 198 obj->path); 199 goto done; 200 } 201 } else { 202 if ((flags & SYMLOOK_IFUNC) != 0) 203 continue; 204 symval = (Elf_Addr)defobj->relocbase + 205 def->st_value; 206 } 207 break; 208 default: 209 if ((flags & SYMLOOK_IFUNC) != 0) 210 continue; 211 break; 212 } 213 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 214 where32 = (Elf32_Addr *)where; 215 216 switch (ELF_R_TYPE(rela->r_info)) { 217 case R_X86_64_NONE: 218 break; 219 case R_X86_64_64: 220 *where = symval + rela->r_addend; 221 break; 222 case R_X86_64_PC32: 223 /* 224 * I don't think the dynamic linker should 225 * ever see this type of relocation. But the 226 * binutils-2.6 tools sometimes generate it. 227 */ 228 *where32 = (Elf32_Addr)(unsigned long)(symval + 229 rela->r_addend - (Elf_Addr)where); 230 break; 231 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */ 232 case R_X86_64_COPY: 233 /* 234 * These are deferred until all other relocations have 235 * been done. All we do here is make sure that the COPY 236 * relocation is not in a shared library. They are 237 * allowed only in executable files. 238 */ 239 if (!obj->mainprog) { 240 _rtld_error("%s: Unexpected R_X86_64_COPY " 241 "relocation in shared library", obj->path); 242 goto done; 243 } 244 break; 245 case R_X86_64_GLOB_DAT: 246 *where = symval; 247 break; 248 case R_X86_64_TPOFF64: 249 /* 250 * We lazily allocate offsets for static TLS 251 * as we see the first relocation that 252 * references the TLS block. This allows us to 253 * support (small amounts of) static TLS in 254 * dynamically loaded modules. If we run out 255 * of space, we generate an error. 256 */ 257 if (!defobj->tls_done) { 258 if (!allocate_tls_offset( 259 __DECONST(Obj_Entry *, defobj))) { 260 _rtld_error("%s: No space available " 261 "for static Thread Local Storage", 262 obj->path); 263 goto done; 264 } 265 } 266 *where = (Elf_Addr)(def->st_value - defobj->tlsoffset + 267 rela->r_addend); 268 break; 269 case R_X86_64_TPOFF32: 270 /* 271 * We lazily allocate offsets for static TLS 272 * as we see the first relocation that 273 * references the TLS block. This allows us to 274 * support (small amounts of) static TLS in 275 * dynamically loaded modules. If we run out 276 * of space, we generate an error. 277 */ 278 if (!defobj->tls_done) { 279 if (!allocate_tls_offset( 280 __DECONST(Obj_Entry *, defobj))) { 281 _rtld_error("%s: No space available " 282 "for static Thread Local Storage", 283 obj->path); 284 goto done; 285 } 286 } 287 *where32 = (Elf32_Addr)(def->st_value - 288 defobj->tlsoffset + rela->r_addend); 289 break; 290 case R_X86_64_DTPMOD64: 291 *where += (Elf_Addr)defobj->tlsindex; 292 break; 293 case R_X86_64_DTPOFF64: 294 *where += (Elf_Addr)(def->st_value + rela->r_addend); 295 break; 296 case R_X86_64_DTPOFF32: 297 *where32 += (Elf32_Addr)(def->st_value + 298 rela->r_addend); 299 break; 300 case R_X86_64_RELATIVE: 301 *where = (Elf_Addr)(obj->relocbase + rela->r_addend); 302 break; 303 /* 304 * missing: 305 * R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16, 306 * R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8 307 */ 308 default: 309 _rtld_error("%s: Unsupported relocation type %u" 310 " in non-PLT relocations\n", obj->path, 311 (unsigned int)ELF_R_TYPE(rela->r_info)); 312 goto done; 313 } 314 } 315 r = 0; 316 done: 317 free(cache); 318 return (r); 319 } 320 321 /* Process the PLT relocations. */ 322 int 323 reloc_plt(Obj_Entry *obj) 324 { 325 const Elf_Rela *relalim; 326 const Elf_Rela *rela; 327 328 relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize); 329 for (rela = obj->pltrela; rela < relalim; rela++) { 330 Elf_Addr *where; 331 332 switch(ELF_R_TYPE(rela->r_info)) { 333 case R_X86_64_JMP_SLOT: 334 /* Relocate the GOT slot pointing into the PLT. */ 335 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 336 *where += (Elf_Addr)obj->relocbase; 337 break; 338 339 case R_X86_64_IRELATIVE: 340 obj->irelative = true; 341 break; 342 343 default: 344 _rtld_error("Unknown relocation type %x in PLT", 345 (unsigned int)ELF_R_TYPE(rela->r_info)); 346 return (-1); 347 } 348 } 349 return 0; 350 } 351 352 /* Relocate the jump slots in an object. */ 353 int 354 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate) 355 { 356 const Elf_Rela *relalim; 357 const Elf_Rela *rela; 358 359 if (obj->jmpslots_done) 360 return 0; 361 relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize); 362 for (rela = obj->pltrela; rela < relalim; rela++) { 363 Elf_Addr *where, target; 364 const Elf_Sym *def; 365 const Obj_Entry *defobj; 366 367 switch (ELF_R_TYPE(rela->r_info)) { 368 case R_X86_64_JMP_SLOT: 369 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 370 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 371 SYMLOOK_IN_PLT | flags, NULL, lockstate); 372 if (def == NULL) 373 return (-1); 374 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 375 obj->gnu_ifunc = true; 376 continue; 377 } 378 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend); 379 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela); 380 break; 381 382 case R_X86_64_IRELATIVE: 383 break; 384 385 default: 386 _rtld_error("Unknown relocation type %x in PLT", 387 (unsigned int)ELF_R_TYPE(rela->r_info)); 388 return (-1); 389 } 390 } 391 obj->jmpslots_done = true; 392 return 0; 393 } 394 395 /* Fixup the jump slot at "where" to transfer control to "target". */ 396 Elf_Addr 397 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, 398 const struct Struct_Obj_Entry *obj __unused, 399 const struct Struct_Obj_Entry *refobj __unused, 400 const Elf_Rel *rel __unused) 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 *)((const 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 *)((const 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