1 /* $NetBSD: ppc_reloc.c,v 1.10 2001/09/10 06:09:41 mycroft Exp $ */ 2 3 /*- 4 * Copyright (C) 1998 Tsubai Masanari 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/mman.h> 34 35 #include <errno.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <machine/cpu.h> 41 #include <machine/md_var.h> 42 43 #include "debug.h" 44 #include "rtld.h" 45 46 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \ 47 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16) 48 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff) 49 50 #define min(a,b) (((a) < (b)) ? (a) : (b)) 51 #define max(a,b) (((a) > (b)) ? (a) : (b)) 52 53 #define PLT_EXTENDED_BEGIN (1 << 13) 54 #define JMPTAB_BASE(N) (18 + N*2 + ((N > PLT_EXTENDED_BEGIN) ? \ 55 (N - PLT_EXTENDED_BEGIN)*2 : 0)) 56 57 /* 58 * Process the R_PPC_COPY relocations 59 */ 60 int 61 do_copy_relocations(Obj_Entry *dstobj) 62 { 63 const Elf_Rela *relalim; 64 const Elf_Rela *rela; 65 66 /* 67 * COPY relocs are invalid outside of the main program 68 */ 69 assert(dstobj->mainprog); 70 71 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + 72 dstobj->relasize); 73 for (rela = dstobj->rela; rela < relalim; rela++) { 74 void *dstaddr; 75 const Elf_Sym *dstsym; 76 const char *name; 77 unsigned long hash; 78 size_t size; 79 const void *srcaddr; 80 const Elf_Sym *srcsym = NULL; 81 Obj_Entry *srcobj; 82 const Ver_Entry *ve; 83 84 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) { 85 continue; 86 } 87 88 dstaddr = (void *) (dstobj->relocbase + rela->r_offset); 89 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info); 90 name = dstobj->strtab + dstsym->st_name; 91 hash = elf_hash(name); 92 size = dstsym->st_size; 93 ve = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)); 94 95 for (srcobj = dstobj->next; srcobj != NULL; 96 srcobj = srcobj->next) { 97 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) 98 != NULL) { 99 break; 100 } 101 } 102 103 if (srcobj == NULL) { 104 _rtld_error("Undefined symbol \"%s\" " 105 " referenced from COPY" 106 " relocation in %s", name, dstobj->path); 107 return (-1); 108 } 109 110 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value); 111 memcpy(dstaddr, srcaddr, size); 112 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size); 113 } 114 115 return (0); 116 } 117 118 119 /* 120 * Perform early relocation of the run-time linker image 121 */ 122 void 123 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 124 { 125 const Elf_Rela *rela = 0, *relalim; 126 Elf_Addr relasz = 0; 127 Elf_Addr *where; 128 129 /* 130 * Extract the rela/relasz values from the dynamic section 131 */ 132 for (; dynp->d_tag != DT_NULL; dynp++) { 133 switch (dynp->d_tag) { 134 case DT_RELA: 135 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr); 136 break; 137 case DT_RELASZ: 138 relasz = dynp->d_un.d_val; 139 break; 140 } 141 } 142 143 /* 144 * Relocate these values 145 */ 146 relalim = (const Elf_Rela *)((caddr_t)rela + relasz); 147 for (; rela < relalim; rela++) { 148 where = (Elf_Addr *)(relocbase + rela->r_offset); 149 *where = (Elf_Addr)(relocbase + rela->r_addend); 150 } 151 } 152 153 154 /* 155 * Relocate a non-PLT object with addend. 156 */ 157 static int 158 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela, 159 SymCache *cache) 160 { 161 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 162 const Elf_Sym *def; 163 const Obj_Entry *defobj; 164 Elf_Addr tmp; 165 166 switch (ELF_R_TYPE(rela->r_info)) { 167 168 case R_PPC_NONE: 169 break; 170 171 case R_PPC_ADDR32: /* word32 S + A */ 172 case R_PPC_GLOB_DAT: /* word32 S + A */ 173 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 174 false, cache); 175 if (def == NULL) { 176 return (-1); 177 } 178 179 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 180 rela->r_addend); 181 182 /* Don't issue write if unnecessary; avoid COW page fault */ 183 if (*where != tmp) { 184 *where = tmp; 185 } 186 break; 187 188 case R_PPC_RELATIVE: /* word32 B + A */ 189 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend); 190 191 /* As above, don't issue write unnecessarily */ 192 if (*where != tmp) { 193 *where = tmp; 194 } 195 break; 196 197 case R_PPC_COPY: 198 /* 199 * These are deferred until all other relocations 200 * have been done. All we do here is make sure 201 * that the COPY relocation is not in a shared 202 * library. They are allowed only in executable 203 * files. 204 */ 205 if (!obj->mainprog) { 206 _rtld_error("%s: Unexpected R_COPY " 207 " relocation in shared library", 208 obj->path); 209 return (-1); 210 } 211 break; 212 213 case R_PPC_JMP_SLOT: 214 /* 215 * These will be handled by the plt/jmpslot routines 216 */ 217 break; 218 219 case R_PPC_DTPMOD32: 220 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 221 false, cache); 222 223 if (def == NULL) 224 return (-1); 225 226 *where = (Elf_Addr) defobj->tlsindex; 227 228 break; 229 230 case R_PPC_TPREL32: 231 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 232 false, cache); 233 234 if (def == NULL) 235 return (-1); 236 237 /* 238 * We lazily allocate offsets for static TLS as we 239 * see the first relocation that references the 240 * TLS block. This allows us to support (small 241 * amounts of) static TLS in dynamically loaded 242 * modules. If we run out of space, we generate an 243 * error. 244 */ 245 if (!defobj->tls_done) { 246 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 247 _rtld_error("%s: No space available for static " 248 "Thread Local Storage", obj->path); 249 return (-1); 250 } 251 } 252 253 *(Elf_Addr **)where = *where * sizeof(Elf_Addr) 254 + (Elf_Addr *)(def->st_value + rela->r_addend 255 + defobj->tlsoffset - TLS_TP_OFFSET); 256 257 break; 258 259 case R_PPC_DTPREL32: 260 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 261 false, cache); 262 263 if (def == NULL) 264 return (-1); 265 266 *where += (Elf_Addr)(def->st_value + rela->r_addend 267 - TLS_DTV_OFFSET); 268 269 break; 270 271 default: 272 _rtld_error("%s: Unsupported relocation type %d" 273 " in non-PLT relocations\n", obj->path, 274 ELF_R_TYPE(rela->r_info)); 275 return (-1); 276 } 277 return (0); 278 } 279 280 281 /* 282 * Process non-PLT relocations 283 */ 284 int 285 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 286 { 287 const Elf_Rela *relalim; 288 const Elf_Rela *rela; 289 SymCache *cache; 290 int bytes = obj->nchains * sizeof(SymCache); 291 int r = -1; 292 293 /* 294 * The dynamic loader may be called from a thread, we have 295 * limited amounts of stack available so we cannot use alloca(). 296 */ 297 if (obj != obj_rtld) { 298 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, 299 -1, 0); 300 if (cache == MAP_FAILED) 301 cache = NULL; 302 } else 303 cache = NULL; 304 305 /* 306 * From the SVR4 PPC ABI: 307 * "The PowerPC family uses only the Elf32_Rela relocation 308 * entries with explicit addends." 309 */ 310 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize); 311 for (rela = obj->rela; rela < relalim; rela++) { 312 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0) 313 goto done; 314 } 315 r = 0; 316 done: 317 if (cache) { 318 munmap(cache, bytes); 319 } 320 return (r); 321 } 322 323 /* 324 * Initialise a PLT slot to the resolving trampoline 325 */ 326 static int 327 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela) 328 { 329 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset); 330 Elf_Addr *pltresolve, *pltlongresolve, *jmptab; 331 Elf_Addr distance; 332 int N = obj->pltrelasize / sizeof(Elf_Rela); 333 int reloff; 334 335 reloff = rela - obj->pltrela; 336 337 if (reloff < 0) 338 return (-1); 339 340 pltlongresolve = obj->pltgot + 5; 341 pltresolve = pltlongresolve + 5; 342 343 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1); 344 345 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x", 346 (void *)where, (void *)pltresolve, reloff, distance); 347 348 if (reloff < PLT_EXTENDED_BEGIN) { 349 /* li r11,reloff */ 350 /* b pltresolve */ 351 where[0] = 0x39600000 | reloff; 352 where[1] = 0x48000000 | (distance & 0x03fffffc); 353 } else { 354 jmptab = obj->pltgot + JMPTAB_BASE(N); 355 jmptab[reloff] = (u_int)pltlongresolve; 356 357 /* lis r11,jmptab[reloff]@ha */ 358 /* lwzu r12,jmptab[reloff]@l(r11) */ 359 /* mtctr r12 */ 360 /* bctr */ 361 where[0] = 0x3d600000 | _ppc_ha(&jmptab[reloff]); 362 where[1] = 0x858b0000 | _ppc_la(&jmptab[reloff]); 363 where[2] = 0x7d8903a6; 364 where[3] = 0x4e800420; 365 } 366 367 368 /* 369 * The icache will be sync'd in init_pltgot, which is called 370 * after all the slots have been updated 371 */ 372 373 return (0); 374 } 375 376 377 /* 378 * Process the PLT relocations. 379 */ 380 int 381 reloc_plt(Obj_Entry *obj) 382 { 383 const Elf_Rela *relalim; 384 const Elf_Rela *rela; 385 386 if (obj->pltrelasize != 0) { 387 388 relalim = (const Elf_Rela *)((char *)obj->pltrela + 389 obj->pltrelasize); 390 for (rela = obj->pltrela; rela < relalim; rela++) { 391 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); 392 393 if (reloc_plt_object(obj, rela) < 0) { 394 return (-1); 395 } 396 } 397 } 398 399 return (0); 400 } 401 402 403 /* 404 * LD_BIND_NOW was set - force relocation for all jump slots 405 */ 406 int 407 reloc_jmpslots(Obj_Entry *obj) 408 { 409 const Obj_Entry *defobj; 410 const Elf_Rela *relalim; 411 const Elf_Rela *rela; 412 const Elf_Sym *def; 413 Elf_Addr *where; 414 Elf_Addr target; 415 416 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize); 417 for (rela = obj->pltrela; rela < relalim; rela++) { 418 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); 419 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 420 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 421 true, NULL); 422 if (def == NULL) { 423 dbg("reloc_jmpslots: sym not found"); 424 return (-1); 425 } 426 427 target = (Elf_Addr)(defobj->relocbase + def->st_value); 428 429 #if 0 430 /* PG XXX */ 431 dbg("\"%s\" in \"%s\" --> %p in \"%s\"", 432 defobj->strtab + def->st_name, basename(obj->path), 433 (void *)target, basename(defobj->path)); 434 #endif 435 436 reloc_jmpslot(where, target, defobj, obj, 437 (const Elf_Rel *) rela); 438 } 439 440 obj->jmpslots_done = true; 441 442 return (0); 443 } 444 445 446 /* 447 * Update the value of a PLT jump slot. Branch directly to the target if 448 * it is within +/- 32Mb, otherwise go indirectly via the pltcall 449 * trampoline call and jump table. 450 */ 451 Elf_Addr 452 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj, 453 const Obj_Entry *obj, const Elf_Rel *rel) 454 { 455 Elf_Addr offset; 456 const Elf_Rela *rela = (const Elf_Rela *) rel; 457 458 dbg(" reloc_jmpslot: where=%p, target=%p", 459 (void *)wherep, (void *)target); 460 461 /* 462 * At the PLT entry pointed at by `wherep', construct 463 * a direct transfer to the now fully resolved function 464 * address. 465 */ 466 offset = target - (Elf_Addr)wherep; 467 468 if (abs(offset) < 32*1024*1024) { /* inside 32MB? */ 469 /* b value # branch directly */ 470 *wherep = 0x48000000 | (offset & 0x03fffffc); 471 __syncicache(wherep, 4); 472 } else { 473 Elf_Addr *pltcall, *jmptab; 474 int distance; 475 int N = obj->pltrelasize / sizeof(Elf_Rela); 476 int reloff = rela - obj->pltrela; 477 478 if (reloff < 0) 479 return (-1); 480 481 pltcall = obj->pltgot; 482 483 dbg(" reloc_jmpslot: indir, reloff=%x, N=%x\n", 484 reloff, N); 485 486 jmptab = obj->pltgot + JMPTAB_BASE(N); 487 jmptab[reloff] = target; 488 489 if (reloff < PLT_EXTENDED_BEGIN) { 490 /* for extended PLT entries, we keep the old code */ 491 492 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1); 493 494 /* li r11,reloff */ 495 /* b pltcall # use indirect pltcall routine */ 496 wherep[0] = 0x39600000 | reloff; 497 wherep[1] = 0x48000000 | (distance & 0x03fffffc); 498 __syncicache(wherep, 8); 499 } 500 } 501 502 return (target); 503 } 504 505 506 /* 507 * Setup the plt glue routines. 508 */ 509 #define PLTCALL_SIZE 20 510 #define PLTLONGRESOLVE_SIZE 20 511 #define PLTRESOLVE_SIZE 24 512 513 void 514 init_pltgot(Obj_Entry *obj) 515 { 516 Elf_Word *pltcall, *pltresolve, *pltlongresolve; 517 Elf_Word *jmptab; 518 int N = obj->pltrelasize / sizeof(Elf_Rela); 519 520 pltcall = obj->pltgot; 521 522 if (pltcall == NULL) { 523 return; 524 } 525 526 /* 527 * From the SVR4 PPC ABI: 528 * 529 * 'The first 18 words (72 bytes) of the PLT are reserved for 530 * use by the dynamic linker. 531 * ... 532 * 'If the executable or shared object requires N procedure 533 * linkage table entries, the link editor shall reserve 3*N 534 * words (12*N bytes) following the 18 reserved words. The 535 * first 2*N of these words are the procedure linkage table 536 * entries themselves. The static linker directs calls to bytes 537 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining 538 * N words (4*N bytes) are reserved for use by the dynamic linker.' 539 */ 540 541 /* 542 * Copy the absolute-call assembler stub into the first part of 543 * the reserved PLT area. 544 */ 545 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE); 546 547 /* 548 * Determine the address of the jumptable, which is the dyn-linker 549 * reserved area after the call cells. Write the absolute address 550 * of the jumptable into the absolute-call assembler code so it 551 * can determine this address. 552 */ 553 jmptab = obj->pltgot + JMPTAB_BASE(N); 554 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */ 555 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */ 556 557 /* 558 * Skip down 20 bytes into the initial reserved area and copy 559 * in the standard resolving assembler call. Into this assembler, 560 * insert the absolute address of the _rtld_bind_start routine 561 * and the address of the relocation object. 562 * 563 * We place pltlongresolve first, so it can fix up its arguments 564 * and then fall through to the regular PLT resolver. 565 */ 566 pltlongresolve = obj->pltgot + 5; 567 568 memcpy(pltlongresolve, _rtld_powerpc_pltlongresolve, 569 PLTLONGRESOLVE_SIZE); 570 pltlongresolve[0] |= _ppc_ha(jmptab); /* lis 12,jmptab@ha */ 571 pltlongresolve[1] |= _ppc_la(jmptab); /* addi 12,12,jmptab@l */ 572 573 pltresolve = pltlongresolve + PLTLONGRESOLVE_SIZE/sizeof(uint32_t); 574 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE); 575 pltresolve[0] |= _ppc_ha(_rtld_bind_start); 576 pltresolve[1] |= _ppc_la(_rtld_bind_start); 577 pltresolve[3] |= _ppc_ha(obj); 578 pltresolve[4] |= _ppc_la(obj); 579 580 /* 581 * Sync the icache for the byte range represented by the 582 * trampoline routines and call slots. 583 */ 584 __syncicache(pltcall, 72 + N * 8); 585 } 586 587 void 588 allocate_initial_tls(Obj_Entry *list) 589 { 590 register Elf_Addr **tp __asm__("r2"); 591 Elf_Addr **_tp; 592 593 /* 594 * Fix the size of the static TLS block by using the maximum 595 * offset allocated so far and adding a bit for dynamic modules to 596 * use. 597 */ 598 599 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA; 600 601 _tp = (Elf_Addr **) ((char *) allocate_tls(list, NULL, TLS_TCB_SIZE, 8) 602 + TLS_TP_OFFSET + TLS_TCB_SIZE); 603 604 /* 605 * XXX gcc seems to ignore 'tp = _tp;' 606 */ 607 608 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp)); 609 } 610 611 void* 612 __tls_get_addr(tls_index* ti) 613 { 614 register Elf_Addr **tp __asm__("r2"); 615 char *p; 616 617 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET 618 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset); 619 620 return (p + TLS_DTV_OFFSET); 621 } 622