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