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