reloc.c revision 216695
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: head/libexec/rtld-elf/powerpc64/reloc.c 216695 2010-12-25 08:51:20Z kib $ 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 46struct funcdesc { 47 Elf_Addr addr; 48 Elf_Addr toc; 49 Elf_Addr env; 50}; 51 52/* 53 * Process the R_PPC_COPY relocations 54 */ 55int 56do_copy_relocations(Obj_Entry *dstobj) 57{ 58 const Elf_Rela *relalim; 59 const Elf_Rela *rela; 60 61 /* 62 * COPY relocs are invalid outside of the main program 63 */ 64 assert(dstobj->mainprog); 65 66 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + 67 dstobj->relasize); 68 for (rela = dstobj->rela; rela < relalim; rela++) { 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 = NULL; 75 const Obj_Entry *srcobj, *defobj; 76 SymLook req; 77 int res; 78 79 if (ELF_R_TYPE(rela->r_info) != R_PPC_COPY) { 80 continue; 81 } 82 83 dstaddr = (void *) (dstobj->relocbase + rela->r_offset); 84 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info); 85 name = dstobj->strtab + dstsym->st_name; 86 size = dstsym->st_size; 87 symlook_init(&req, name); 88 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)); 89 90 for (srcobj = dstobj->next; srcobj != NULL; 91 srcobj = srcobj->next) { 92 res = symlook_obj(&req, srcobj); 93 if (res == 0) { 94 srcsym = req.sym_out; 95 defobj = req.defobj_out; 96 break; 97 } 98 } 99 100 if (srcobj == NULL) { 101 _rtld_error("Undefined symbol \"%s\" " 102 " referenced from COPY" 103 " relocation in %s", name, dstobj->path); 104 return (-1); 105 } 106 107 srcaddr = (const void *) (defobj->relocbase+srcsym->st_value); 108 memcpy(dstaddr, srcaddr, size); 109 dbg("copy_reloc: src=%p,dst=%p,size=%zd\n",srcaddr,dstaddr,size); 110 } 111 112 return (0); 113} 114 115 116/* 117 * Perform early relocation of the run-time linker image 118 */ 119void 120reloc_non_plt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 121{ 122 const Elf_Rela *rela = 0, *relalim; 123 Elf_Addr relasz = 0; 124 Elf_Addr *where; 125 126 /* 127 * Extract the rela/relasz values from the dynamic section 128 */ 129 for (; dynp->d_tag != DT_NULL; dynp++) { 130 switch (dynp->d_tag) { 131 case DT_RELA: 132 rela = (const Elf_Rela *)(relocbase+dynp->d_un.d_ptr); 133 break; 134 case DT_RELASZ: 135 relasz = dynp->d_un.d_val; 136 break; 137 } 138 } 139 140 /* 141 * Relocate these values 142 */ 143 relalim = (const Elf_Rela *)((caddr_t)rela + relasz); 144 for (; rela < relalim; rela++) { 145 where = (Elf_Addr *)(relocbase + rela->r_offset); 146 *where = (Elf_Addr)(relocbase + rela->r_addend); 147 } 148} 149 150 151/* 152 * Relocate a non-PLT object with addend. 153 */ 154static int 155reloc_nonplt_object(Obj_Entry *obj_rtld, Obj_Entry *obj, const Elf_Rela *rela, 156 SymCache *cache, RtldLockState *lockstate) 157{ 158 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 159 const Elf_Sym *def; 160 const Obj_Entry *defobj; 161 Elf_Addr tmp; 162 163 switch (ELF_R_TYPE(rela->r_info)) { 164 165 case R_PPC_NONE: 166 break; 167 168 case R_PPC64_ADDR64: /* doubleword64 S + A */ 169 case R_PPC_GLOB_DAT: 170 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 171 false, cache, lockstate); 172 if (def == NULL) { 173 return (-1); 174 } 175 176 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 177 rela->r_addend); 178 179 /* Don't issue write if unnecessary; avoid COW page fault */ 180 if (*where != tmp) { 181 *where = tmp; 182 } 183 break; 184 185 case R_PPC_RELATIVE: /* doubleword64 B + A */ 186 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend); 187 188 /* As above, don't issue write unnecessarily */ 189 if (*where != tmp) { 190 *where = tmp; 191 } 192 break; 193 194 case R_PPC_COPY: 195 /* 196 * These are deferred until all other relocations 197 * have been done. All we do here is make sure 198 * that the COPY relocation is not in a shared 199 * library. They are allowed only in executable 200 * files. 201 */ 202 if (!obj->mainprog) { 203 _rtld_error("%s: Unexpected R_COPY " 204 " relocation in shared library", 205 obj->path); 206 return (-1); 207 } 208 break; 209 210 case R_PPC_JMP_SLOT: 211 /* 212 * These will be handled by the plt/jmpslot routines 213 */ 214 break; 215 216 case R_PPC64_DTPMOD64: 217 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 218 false, cache, lockstate); 219 220 if (def == NULL) 221 return (-1); 222 223 *where = (Elf_Addr) defobj->tlsindex; 224 225 break; 226 227 case R_PPC64_TPREL64: 228 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 229 false, cache, lockstate); 230 231 if (def == NULL) 232 return (-1); 233 234 /* 235 * We lazily allocate offsets for static TLS as we 236 * see the first relocation that references the 237 * TLS block. This allows us to support (small 238 * amounts of) static TLS in dynamically loaded 239 * modules. If we run out of space, we generate an 240 * error. 241 */ 242 if (!defobj->tls_done) { 243 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 244 _rtld_error("%s: No space available for static " 245 "Thread Local Storage", obj->path); 246 return (-1); 247 } 248 } 249 250 *(Elf_Addr **)where = *where * sizeof(Elf_Addr) 251 + (Elf_Addr *)(def->st_value + rela->r_addend 252 + defobj->tlsoffset - TLS_TP_OFFSET); 253 254 break; 255 256 case R_PPC64_DTPREL64: 257 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 258 false, cache, lockstate); 259 260 if (def == NULL) 261 return (-1); 262 263 *where += (Elf_Addr)(def->st_value + rela->r_addend 264 - TLS_DTV_OFFSET); 265 266 break; 267 268 default: 269 _rtld_error("%s: Unsupported relocation type %ld" 270 " in non-PLT relocations\n", obj->path, 271 ELF_R_TYPE(rela->r_info)); 272 return (-1); 273 } 274 return (0); 275} 276 277 278/* 279 * Process non-PLT relocations 280 */ 281int 282reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, RtldLockState *lockstate) 283{ 284 const Elf_Rela *relalim; 285 const Elf_Rela *rela; 286 SymCache *cache; 287 int bytes = obj->nchains * sizeof(SymCache); 288 int r = -1; 289 290 /* 291 * The dynamic loader may be called from a thread, we have 292 * limited amounts of stack available so we cannot use alloca(). 293 */ 294 if (obj != obj_rtld) { 295 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, 296 -1, 0); 297 if (cache == MAP_FAILED) 298 cache = NULL; 299 } else 300 cache = NULL; 301 302 /* 303 * From the SVR4 PPC ABI: 304 * "The PowerPC family uses only the Elf32_Rela relocation 305 * entries with explicit addends." 306 */ 307 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize); 308 for (rela = obj->rela; rela < relalim; rela++) { 309 if (reloc_nonplt_object(obj_rtld, obj, rela, cache, lockstate) 310 < 0) 311 goto done; 312 } 313 r = 0; 314done: 315 if (cache) { 316 munmap(cache, bytes); 317 } 318 return (r); 319} 320 321 322/* 323 * Initialise a PLT slot to the resolving trampoline 324 */ 325static int 326reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela) 327{ 328 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 329 Elf_Addr *glink; 330 long reloff; 331 332 reloff = rela - obj->pltrela; 333 334 if (obj->priv == NULL) 335 obj->priv = malloc(obj->pltrelasize); 336 glink = obj->priv + reloff*sizeof(Elf_Addr)*2; 337 338 if ((reloff < 0) || (reloff >= 0x8000)) { 339 return (-1); 340 } 341 342 dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%p", (void *)where, reloff, glink); 343 344 memcpy(where, _rtld_bind_start, sizeof(struct funcdesc)); 345 ((struct funcdesc *)(where))->env = (Elf_Addr)glink; 346 *(glink++) = (Elf_Addr)obj; 347 *(glink++) = reloff*sizeof(Elf_Rela); 348 349 return (0); 350} 351 352 353/* 354 * Process the PLT relocations. 355 */ 356int 357reloc_plt(Obj_Entry *obj) 358{ 359 const Elf_Rela *relalim; 360 const Elf_Rela *rela; 361 362 if (obj->pltrelasize != 0) { 363 relalim = (const Elf_Rela *)((char *)obj->pltrela + 364 obj->pltrelasize); 365 for (rela = obj->pltrela; rela < relalim; rela++) { 366 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); 367 368 if (reloc_plt_object(obj, rela) < 0) { 369 return (-1); 370 } 371 } 372 } 373 374 return (0); 375} 376 377 378/* 379 * LD_BIND_NOW was set - force relocation for all jump slots 380 */ 381int 382reloc_jmpslots(Obj_Entry *obj, RtldLockState *lockstate) 383{ 384 const Obj_Entry *defobj; 385 const Elf_Rela *relalim; 386 const Elf_Rela *rela; 387 const Elf_Sym *def; 388 Elf_Addr *where; 389 Elf_Addr target; 390 391 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize); 392 for (rela = obj->pltrela; rela < relalim; rela++) { 393 assert(ELF_R_TYPE(rela->r_info) == R_PPC_JMP_SLOT); 394 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 395 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 396 true, NULL, lockstate); 397 if (def == NULL) { 398 dbg("reloc_jmpslots: sym not found"); 399 return (-1); 400 } 401 402 target = (Elf_Addr)(defobj->relocbase + def->st_value); 403 404#if 0 405 /* PG XXX */ 406 dbg("\"%s\" in \"%s\" --> %p in \"%s\"", 407 defobj->strtab + def->st_name, basename(obj->path), 408 (void *)target, basename(defobj->path)); 409#endif 410 411 if (def == &sym_zero) { 412 /* Zero undefined weak symbols */ 413 bzero(where, sizeof(struct funcdesc)); 414 } else { 415 reloc_jmpslot(where, target, defobj, obj, 416 (const Elf_Rel *) rela); 417 } 418 } 419 420 obj->jmpslots_done = true; 421 422 return (0); 423} 424 425 426/* 427 * Update the value of a PLT jump slot. 428 */ 429Elf_Addr 430reloc_jmpslot(Elf_Addr *wherep, Elf_Addr target, const Obj_Entry *defobj, 431 const Obj_Entry *obj, const Elf_Rel *rel) 432{ 433 dbg(" reloc_jmpslot: where=%p, target=%p (%#lx + %#lx)", 434 (void *)wherep, (void *)target, *(Elf_Addr *)target, 435 (Elf_Addr)defobj->relocbase); 436 437 /* 438 * At the PLT entry pointed at by `wherep', construct 439 * a direct transfer to the now fully resolved function 440 * address. 441 */ 442 443 memcpy(wherep, (void *)target, sizeof(struct funcdesc)); 444 if (((struct funcdesc *)(wherep))->addr < (Elf_Addr)defobj->relocbase) { 445 /* 446 * XXX: It is possible (e.g. LD_BIND_NOW) that the function 447 * descriptor we are copying has not yet been relocated. 448 * If this happens, fix it. 449 */ 450 451 ((struct funcdesc *)(wherep))->addr += 452 (Elf_Addr)defobj->relocbase; 453 ((struct funcdesc *)(wherep))->toc += 454 (Elf_Addr)defobj->relocbase; 455 } 456 457 __asm __volatile("dcbst 0,%0; sync" :: "r"(wherep) : "memory"); 458 459 return (target); 460} 461 462void 463init_pltgot(Obj_Entry *obj) 464{ 465} 466 467void 468allocate_initial_tls(Obj_Entry *list) 469{ 470 register Elf_Addr **tp __asm__("r13"); 471 Elf_Addr **_tp; 472 473 /* 474 * Fix the size of the static TLS block by using the maximum 475 * offset allocated so far and adding a bit for dynamic modules to 476 * use. 477 */ 478 479 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA; 480 481 _tp = (Elf_Addr **) ((char *)allocate_tls(list, NULL, TLS_TCB_SIZE, 16) 482 + TLS_TP_OFFSET + TLS_TCB_SIZE); 483 484 /* 485 * XXX gcc seems to ignore 'tp = _tp;' 486 */ 487 488 __asm __volatile("mr %0,%1" : "=r"(tp) : "r"(_tp)); 489} 490 491void* 492__tls_get_addr(tls_index* ti) 493{ 494 register Elf_Addr **tp __asm__("r13"); 495 char *p; 496 497 p = tls_get_addr_common((Elf_Addr**)((Elf_Addr)tp - TLS_TP_OFFSET 498 - TLS_TCB_SIZE), ti->ti_module, ti->ti_offset); 499 500 return (p + TLS_DTV_OFFSET); 501} 502