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