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 42 #include "debug.h" 43 #include "rtld.h" 44 45 #define _ppc_ha(x) ((((u_int32_t)(x) & 0x8000) ? \ 46 ((u_int32_t)(x) + 0x10000) : (u_int32_t)(x)) >> 16) 47 #define _ppc_la(x) ((u_int32_t)(x) & 0xffff) 48 49 /* 50 * Process the R_PPC_COPY relocations 51 */ 52 int 53 do_copy_relocations(Obj_Entry *dstobj) 54 { 55 const Elf_Rela *relalim; 56 const Elf_Rela *rela; 57 58 /* 59 * COPY relocs are invalid outside of the main program 60 */ 61 assert(dstobj->mainprog); 62 63 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + 64 dstobj->relasize); 65 for (rela = dstobj->rela; rela < relalim; rela++) { 66 void *dstaddr; 67 const Elf_Sym *dstsym; 68 const char *name; 69 unsigned long hash; 70 size_t size; 71 const void *srcaddr; 72 const Elf_Sym *srcsym = NULL; 73 Obj_Entry *srcobj; 74 75 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) { 76 continue; 77 } 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 hash = elf_hash(name); 83 size = dstsym->st_size; 84 85 for (srcobj = dstobj->next; srcobj != NULL; 86 srcobj = srcobj->next) { 87 if ((srcsym = symlook_obj(name, hash, srcobj, false)) 88 != NULL) { 89 break; 90 } 91 } 92 93 if (srcobj == NULL) { 94 _rtld_error("Undefined symbol \"%s\" " 95 " referenced from COPY" 96 " relocation in %s", name, dstobj->path); 97 return (-1); 98 } 99 100 srcaddr = (const void *) (srcobj->relocbase+srcsym->st_value); 101 memcpy(dstaddr, srcaddr, size); 102 dbg("copy_reloc: src=%p,dst=%p,size=%d\n",srcaddr,dstaddr,size); 103 } 104 105 return (0); 106 } 107 108 109 /* 110 * Perform early relocation of the run-time linker image 111 */ 112 void 113 reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 114 { 115 const Elf_Rela *rela = 0, *relalim; 116 Elf_Addr relasz = 0; 117 Elf_Addr *where; 118 119 /* 120 * Extract the rela/relasz values from the dynamic section 121 */ 122 for (; dynp->d_tag != DT_NULL; dynp++) { 123 switch (dynp->d_tag) { 124 case DT_RELA: 125 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr); 126 break; 127 case DT_RELASZ: 128 relasz = dynp->d_un.d_val; 129 break; 130 } 131 } 132 133 /* 134 * Relocate these values 135 */ 136 relalim = (const Elf_Rela *)((caddr_t)rela + relasz); 137 for (; rela < relalim; rela++) { 138 where = (Elf_Addr *)(relocbase + rela->r_offset); 139 *where = (Elf_Addr)(relocbase + rela->r_addend); 140 } 141 } 142 143 144 /* 145 * Relocate a non-PLT object with addend. 146 */ 147 static int 148 reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela, 149 SymCache *cache) 150 { 151 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 152 const Elf_Sym *def; 153 const Obj_Entry *defobj; 154 Elf_Addr tmp; 155 156 switch (ELF_R_TYPE(rela->r_info)) { 157 158 case R_PPC_NONE: 159 break; 160 161 case R_PPC_ADDR32: /* word32 S + A */ 162 case R_PPC_GLOB_DAT: /* word32 S + A */ 163 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 164 false, cache); 165 if (def == NULL) { 166 return (-1); 167 } 168 169 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 170 rela->r_addend); 171 172 /* Don't issue write if unnecessary; avoid COW page fault */ 173 if (*where != tmp) { 174 *where = tmp; 175 } 176 break; 177 178 case R_PPC_RELATIVE: /* word32 B + A */ 179 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend); 180 181 /* As above, don't issue write unnecessarily */ 182 if (*where != tmp) { 183 *where = tmp; 184 } 185 break; 186 187 case R_PPC_COPY: 188 /* 189 * These are deferred until all other relocations 190 * have been done. All we do here is make sure 191 * that the COPY relocation is not in a shared 192 * library. They are allowed only in executable 193 * files. 194 */ 195 if (!obj->mainprog) { 196 _rtld_error("%s: Unexpected R_COPY " 197 " relocation in shared library", 198 obj->path); 199 return (-1); 200 } 201 break; 202 203 case R_PPC_JMP_SLOT: 204 /* 205 * These will be handled by the plt/jmpslot routines 206 */ 207 break; 208 209 default: 210 _rtld_error("%s: Unsupported relocation type %d" 211 " in non-PLT relocations\n", obj->path, 212 ELF_R_TYPE(rela->r_info)); 213 return (-1); 214 } 215 return (0); 216 } 217 218 219 /* 220 * Process non-PLT relocations 221 */ 222 int 223 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 224 { 225 const Elf_Rela *relalim; 226 const Elf_Rela *rela; 227 SymCache *cache; 228 int bytes = obj->nchains * sizeof(SymCache); 229 int r = -1; 230 231 /* 232 * The dynamic loader may be called from a thread, we have 233 * limited amounts of stack available so we cannot use alloca(). 234 */ 235 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0); 236 if (cache == MAP_FAILED) { 237 cache = NULL; 238 } 239 if (cache != NULL) { 240 memset(cache, 0, obj->nchains * sizeof(SymCache)); 241 } 242 243 /* 244 * From the SVR4 PPC ABI: 245 * "The PowerPC family uses only the Elf32_Rela relocation 246 * entries with explicit addends." 247 */ 248 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize); 249 for (rela = obj->rela; rela < relalim; rela++) { 250 if (reloc_nonplt_object(obj_rtld, obj, rela, cache) < 0) 251 goto done; 252 } 253 r = 0; 254 done: 255 if (cache) { 256 munmap(cache, bytes); 257 } 258 return (r); 259 } 260 261 262 /* 263 * Initialise a PLT slot to the resolving trampoline 264 */ 265 static int 266 reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela) 267 { 268 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset); 269 Elf_Addr *pltresolve; 270 Elf_Addr distance; 271 int reloff; 272 273 reloff = rela - obj->pltrela; 274 275 if ((reloff < 0) || (reloff >= 0x8000)) { 276 return (-1); 277 } 278 279 pltresolve = obj->pltgot + 8; 280 281 distance = (Elf_Addr)pltresolve - (Elf_Addr)(where + 1); 282 283 dbg(" reloc_plt_object: where=%p,pltres=%p,reloff=%x,distance=%x", 284 (void *)where, (void *)pltresolve, reloff, distance); 285 286 /* li r11,reloff */ 287 /* b pltresolve */ 288 where[0] = 0x39600000 | reloff; 289 where[1] = 0x48000000 | (distance & 0x03fffffc); 290 291 /* 292 * The icache will be sync'd in init_pltgot, which is called 293 * after all the slots have been updated 294 */ 295 296 return (0); 297 } 298 299 300 /* 301 * Process the PLT relocations. 302 */ 303 int 304 reloc_plt(Obj_Entry *obj) 305 { 306 const Elf_Rela *relalim; 307 const Elf_Rela *rela; 308 309 if (obj->pltrelasize != 0) { 310 311 relalim = (const Elf_Rela *)((char *)obj->pltrela + 312 obj->pltrelasize); 313 for (rela = obj->pltrela; rela < relalim; rela++) { 314 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); 315 316 if (reloc_plt_object(obj, rela) < 0) { 317 return (-1); 318 } 319 } 320 } 321 322 return (0); 323 } 324 325 326 /* 327 * LD_BIND_NOW was set - force relocation for all jump slots 328 */ 329 int 330 reloc_jmpslots(Obj_Entry *obj) 331 { 332 const Obj_Entry *defobj; 333 const Elf_Rela *relalim; 334 const Elf_Rela *rela; 335 const Elf_Sym *def; 336 Elf_Addr *where; 337 Elf_Addr target; 338 339 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize); 340 for (rela = obj->pltrela; rela < relalim; rela++) { 341 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); 342 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 343 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 344 true, NULL); 345 if (def == NULL) { 346 dbg("reloc_jmpslots: sym not found"); 347 return (-1); 348 } 349 350 target = (Elf_Addr)(defobj->relocbase + def->st_value); 351 352 #if 0 353 /* PG XXX */ 354 dbg("\"%s\" in \"%s\" --> %p in \"%s\"", 355 defobj->strtab + def->st_name, basename(obj->path), 356 (void *)target, basename(defobj->path)); 357 #endif 358 359 reloc_jmpslot(where, target, defobj, obj, 360 (const Elf_Rel *) rela); 361 } 362 363 obj->jmpslots_done = true; 364 365 return (0); 366 } 367 368 369 /* 370 * Update the value of a PLT jump slot. Branch directly to the target if 371 * it is within +/- 32Mb, otherwise go indirectly via the pltcall 372 * trampoline call and jump table. 373 */ 374 Elf_Addr 375 reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj, 376 const Obj_Entry *obj, const Elf_Rel *rel) 377 { 378 Elf_Addr offset; 379 const Elf_Rela *rela = (const Elf_Rela *) rel; 380 381 dbg(" reloc_jmpslot: where=%p, target=%p", 382 (void *)wherep, (void *)target); 383 384 /* 385 * At the PLT entry pointed at by `wherep', construct 386 * a direct transfer to the now fully resolved function 387 * address. 388 */ 389 offset = target - (Elf_Addr)wherep; 390 391 if (abs(offset) < 32*1024*1024) { /* inside 32MB? */ 392 /* b value # branch directly */ 393 *wherep = 0x48000000 | (offset & 0x03fffffc); 394 __syncicache(wherep, 4); 395 } else { 396 Elf_Addr *pltcall, *jmptab; 397 int distance; 398 int N = obj->pltrelasize / sizeof(Elf_Rela); 399 int reloff = rela - obj->pltrela; 400 401 if ((reloff < 0) || (reloff >= 0x8000)) { 402 return (-1); 403 } 404 405 pltcall = obj->pltgot; 406 407 dbg(" reloc_jmpslot: indir, reloff=%d, N=%d\n", 408 reloff, N); 409 410 jmptab = obj->pltgot + 18 + N * 2; 411 jmptab[reloff] = target; 412 413 distance = (Elf_Addr)pltcall - (Elf_Addr)(wherep + 1); 414 415 /* li r11,reloff */ 416 /* b pltcall # use indirect pltcall routine */ 417 wherep[0] = 0x39600000 | reloff; 418 wherep[1] = 0x48000000 | (distance & 0x03fffffc); 419 __syncicache(wherep, 8); 420 } 421 422 return (target); 423 } 424 425 426 /* 427 * Setup the plt glue routines. 428 */ 429 #define PLTCALL_SIZE 20 430 #define PLTRESOLVE_SIZE 24 431 432 void 433 init_pltgot(Obj_Entry *obj) 434 { 435 Elf_Word *pltcall, *pltresolve; 436 Elf_Word *jmptab; 437 int N = obj->pltrelasize / sizeof(Elf_Rela); 438 439 pltcall = obj->pltgot; 440 441 if (pltcall == NULL) { 442 return; 443 } 444 445 /* 446 * From the SVR4 PPC ABI: 447 * 448 * 'The first 18 words (72 bytes) of the PLT are reserved for 449 * use by the dynamic linker. 450 * ... 451 * 'If the executable or shared object requires N procedure 452 * linkage table entries, the link editor shall reserve 3*N 453 * words (12*N bytes) following the 18 reserved words. The 454 * first 2*N of these words are the procedure linkage table 455 * entries themselves. The static linker directs calls to bytes 456 * (72 + (i-1)*8), for i between 1 and N inclusive. The remaining 457 * N words (4*N bytes) are reserved for use by the dynamic linker.' 458 */ 459 460 /* 461 * Copy the absolute-call assembler stub into the first part of 462 * the reserved PLT area. 463 */ 464 memcpy(pltcall, _rtld_powerpc_pltcall, PLTCALL_SIZE); 465 466 /* 467 * Determine the address of the jumptable, which is the dyn-linker 468 * reserved area after the call cells. Write the absolute address 469 * of the jumptable into the absolute-call assembler code so it 470 * can determine this address. 471 */ 472 jmptab = pltcall + 18 + N * 2; 473 pltcall[1] |= _ppc_ha(jmptab); /* addis 11,11,jmptab@ha */ 474 pltcall[2] |= _ppc_la(jmptab); /* lwz 11,jmptab@l(11) */ 475 476 /* 477 * Skip down 32 bytes into the initial reserved area and copy 478 * in the standard resolving assembler call. Into this assembler, 479 * insert the absolute address of the _rtld_bind_start routine 480 * and the address of the relocation object. 481 */ 482 pltresolve = obj->pltgot + 8; 483 484 memcpy(pltresolve, _rtld_powerpc_pltresolve, PLTRESOLVE_SIZE); 485 pltresolve[0] |= _ppc_ha(_rtld_bind_start); 486 pltresolve[1] |= _ppc_la(_rtld_bind_start); 487 pltresolve[3] |= _ppc_ha(obj); 488 pltresolve[4] |= _ppc_la(obj); 489 490 /* 491 * Sync the icache for the byte range represented by the 492 * trampoline routines and call slots. 493 */ 494 __syncicache(pltcall, 72 + N * 8); 495 } 496