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