alpha_reloc.c revision 1.14
1/* $NetBSD: alpha_reloc.c,v 1.14 2002/09/08 02:48:28 thorpej Exp $ */ 2 3/* 4 * Copyright (c) 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38/* 39 * Copyright 1996, 1997, 1998, 1999 John D. Polstra. 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63#include <sys/types.h> 64#include <sys/stat.h> 65 66#include "rtld.h" 67#include "debug.h" 68 69#ifdef RTLD_DEBUG_ALPHA 70#define adbg(x) if (dodebug) xprintf x 71#else 72#define adbg(x) /* nothing */ 73#endif 74 75void 76_rtld_setup_pltgot(const Obj_Entry *obj) 77{ 78 uint32_t word0; 79 80 /* 81 * The PLTGOT on the Alpha looks like this: 82 * 83 * PLT HEADER 84 * . 85 * . 32 bytes 86 * . 87 * PLT ENTRY #0 88 * . 89 * . 12 bytes 90 * . 91 * PLT ENTRY #1 92 * . 93 * . 12 bytes 94 * . 95 * etc. 96 * 97 * The old-format entries look like (displacements filled in 98 * by the linker): 99 * 100 * ldah $28, 0($31) # 0x279f0000 101 * lda $28, 0($28) # 0x239c0000 102 * br $31, plt0 # 0xc3e00000 103 * 104 * The new-format entries look like: 105 * 106 * br $28, plt0 # 0xc3800000 107 * # 0x00000000 108 * # 0x00000000 109 * 110 * What we do is fetch the first PLT entry and check to 111 * see the first word of it matches the first word of the 112 * old format. If so, we use a binding routine that can 113 * handle the old format, otherwise we use a binding routine 114 * that handles the new format. 115 * 116 * Note that this is done on a per-object basis, we can mix 117 * and match shared objects build with both the old and new 118 * linker. 119 */ 120 word0 = *(uint32_t *)(((char *) obj->pltgot) + 32); 121 if ((word0 & 0xffff0000) == 0x279f0000) { 122 /* Old PLT entry format. */ 123 adbg(("ALPHA: object %p has old PLT format\n", obj)); 124 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old; 125 obj->pltgot[3] = (Elf_Addr) obj; 126 } else { 127 /* New PLT entry format. */ 128 adbg(("ALPHA: object %p has new PLT format\n", obj)); 129 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 130 obj->pltgot[3] = (Elf_Addr) obj; 131 } 132 133 __asm __volatile("imb"); 134} 135 136int 137_rtld_relocate_nonplt_objects(obj, self, dodebug) 138 const Obj_Entry *obj; 139 bool self; 140 bool dodebug; 141{ 142 const Elf_Rela *rela; 143 144 for (rela = obj->rela; rela < obj->relalim; rela++) { 145 Elf_Addr *where; 146 const Elf_Sym *def; 147 const Obj_Entry *defobj; 148 Elf_Addr tmp; 149 unsigned long symnum; 150 151 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 152 symnum = ELF_R_SYM(rela->r_info); 153 154 switch (ELF_R_TYPE(rela->r_info)) { 155 case R_TYPE(NONE): 156 break; 157 158 case R_TYPE(REFQUAD): 159 def = _rtld_find_symdef(symnum, obj, &defobj, false); 160 if (def == NULL) 161 return -1; 162 163 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) + 164 *where + rela->r_addend; 165 if (*where != tmp) 166 *where = tmp; 167 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s", 168 obj->strtab + obj->symtab[symnum].st_name, 169 obj->path, (void *)*where, defobj->path)); 170 break; 171 172 case R_TYPE(GLOB_DAT): 173 def = _rtld_find_symdef(symnum, obj, &defobj, false); 174 if (def == NULL) 175 return -1; 176 177 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) + 178 rela->r_addend; 179 if (*where != tmp) 180 *where = tmp; 181 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s", 182 obj->strtab + obj->symtab[symnum].st_name, 183 obj->path, (void *)*where, defobj->path)); 184 break; 185 186 case R_TYPE(RELATIVE): 187 { 188 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; 189 extern Elf_Addr _GOT_END_[]; 190 191 /* This is the ...iffy hueristic. */ 192 if (!self || 193 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ || 194 (caddr_t)where >= (caddr_t)_GOT_END_) { 195 *where += (Elf_Addr)obj->relocbase; 196 rdbg(dodebug, ("RELATIVE in %s --> %p", 197 obj->path, (void *)*where)); 198 } else 199 rdbg(dodebug, ("RELATIVE in %s stays at %p", 200 obj->path, (void *)*where)); 201 break; 202 } 203 204 case R_TYPE(COPY): 205 /* 206 * These are deferred until all other relocations have 207 * been done. All we do here is make sure that the 208 * COPY relocation is not in a shared library. They 209 * are allowed only in executable files. 210 */ 211 if (obj->isdynamic) { 212 _rtld_error( 213 "%s: Unexpected R_COPY relocation in shared library", 214 obj->path); 215 return -1; 216 } 217 rdbg(dodebug, ("COPY (avoid in main)")); 218 break; 219 220 default: 221 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, " 222 "addend = %p, contents = %p, symbol = %s", 223 symnum, (u_long)ELF_R_TYPE(rela->r_info), 224 (void *)rela->r_offset, (void *)rela->r_addend, 225 (void *)*where, 226 obj->strtab + obj->symtab[symnum].st_name)); 227 _rtld_error("%s: Unsupported relocation type %ld " 228 "in non-PLT relocations\n", 229 obj->path, (u_long) ELF_R_TYPE(rela->r_info)); 230 return -1; 231 } 232 } 233 return 0; 234} 235 236int 237_rtld_relocate_plt_lazy(obj, dodebug) 238 const Obj_Entry *obj; 239 bool dodebug; 240{ 241 const Elf_Rela *rela; 242 243 if (!obj->isdynamic) 244 return 0; 245 246 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) { 247 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 248 249 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 250 251 /* Just relocate the GOT slots pointing into the PLT */ 252 *where += (Elf_Addr)obj->relocbase; 253 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path, 254 (void *)*where)); 255 } 256 257 return 0; 258} 259 260int 261_rtld_relocate_plt_object(obj, rela, addrp, dodebug) 262 const Obj_Entry *obj; 263 const Elf_Rela *rela; 264 caddr_t *addrp; 265 bool dodebug; 266{ 267 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 268 Elf_Addr new_value; 269 const Elf_Sym *def; 270 const Obj_Entry *defobj; 271 Elf_Addr stubaddr; 272 273 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 274 275 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true); 276 if (def == NULL) 277 return -1; 278 279 new_value = (Elf_Addr)(defobj->relocbase + def->st_value); 280 rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p", 281 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 282 283 if ((stubaddr = *where) != new_value) { 284 int64_t delta, idisp; 285 uint32_t insn[3], *stubptr; 286 int insncnt; 287 Elf_Addr pc; 288 289 /* Point this GOT entry at the target. */ 290 *where = new_value; 291 292 /* 293 * Alpha shared objects may have multiple GOTs, each 294 * of which may point to this entry in the PLT. But, 295 * we only have a reference to the first GOT entry which 296 * points to this PLT entry. In order to avoid having to 297 * re-bind this call every time a non-first GOT entry is 298 * used, we will attempt to patch up the PLT entry to 299 * reference the target, rather than the binder. 300 * 301 * When the PLT stub gets control, PV contains the address 302 * of the PLT entry. Each PLT entry has room for 3 insns. 303 * If the displacement of the target from PV fits in a signed 304 * 32-bit integer, we can simply add it to PV. Otherwise, 305 * we must load the GOT entry itself into PV. 306 * 307 * Note if the shared object uses the old PLT format, then 308 * we cannot patch up the PLT safely, and so we skip it 309 * in that case[*]. 310 * 311 * [*] Actually, if we're not doing lazy-binding, then 312 * we *can* (and do) patch up this PLT entry; the PLTGOT 313 * thunk won't yet point to any binder entry point, and 314 * so this test will fail as it would for the new PLT 315 * entry format. 316 */ 317 if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) { 318 rdbg(dodebug, (" old PLT format")); 319 goto out; 320 } 321 322 delta = new_value - stubaddr; 323 rdbg(dodebug, (" stubaddr=%p, where-stubaddr=%ld, delta=%ld", 324 (void *)stubaddr, (long)where - (long)stubaddr, 325 (long)delta)); 326 insncnt = 0; 327 if ((int32_t)delta == delta) { 328 /* 329 * We can adjust PV with an LDA, LDAH sequence. 330 * 331 * First, build an LDA insn to adjust the low 16 332 * bits. 333 */ 334 insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 | 335 (delta & 0xffff); 336 rdbg(dodebug, (" LDA $27,%d($27)", (int16_t)delta)); 337 /* 338 * Adjust the delta to account for the effects of 339 * the LDA, including sign-extension. 340 */ 341 delta -= (int16_t)delta; 342 if (delta != 0) { 343 /* 344 * Build an LDAH instruction to adjust the 345 * high 16 bits. 346 */ 347 insn[insncnt++] = 0x09 << 26 | 27 << 21 | 348 27 << 16 | ((delta >> 16) & 0xffff); 349 rdbg(dodebug, (" LDAH $27,%d($27)", 350 (int16_t)(delta >> 16))); 351 } 352 } else { 353 int64_t dhigh; 354 355 /* We must load the GOT entry. */ 356 delta = (Elf_Addr)where - stubaddr; 357 358 /* 359 * If the GOT entry is too far away from the PLT 360 * entry, then we can't patch up the PLT entry. 361 * This PLT entry will have to be bound for each 362 * GOT entry except for the first one. This program 363 * will still run, albeit very slowly. It is very 364 * unlikely that this case will ever happen in 365 * practice. 366 */ 367 if ((int32_t)delta != delta) { 368 rdbg(dodebug, 369 (" PLT stub too far from GOT to relocate")); 370 goto out; 371 } 372 dhigh = delta - (int16_t)delta; 373 if (dhigh != 0) { 374 /* 375 * Build an LDAH instruction to adjust the 376 * high 16 bits. 377 */ 378 insn[insncnt++] = 0x09 << 26 | 27 << 21 | 379 27 << 16 | ((dhigh >> 16) & 0xffff); 380 rdbg(dodebug, (" LDAH $27,%d($27)", 381 (int16_t)(dhigh >> 16))); 382 } 383 /* Build an LDQ to load the GOT entry. */ 384 insn[insncnt++] = 0x29 << 26 | 27 << 21 | 385 27 << 16 | (delta & 0xffff); 386 rdbg(dodebug, (" LDQ $27,%d($27)", 387 (int16_t)delta)); 388 } 389 390 /* 391 * Now, build a JMP or BR insn to jump to the target. If 392 * the displacement fits in a sign-extended 21-bit field, 393 * we can use the more efficient BR insn. Otherwise, we 394 * have to jump indirect through PV. 395 */ 396 pc = stubaddr + (4 * (insncnt + 1)); 397 idisp = (int64_t)(new_value - pc) >> 2; 398 if (-0x100000 <= idisp && idisp < 0x100000) { 399 insn[insncnt++] = 0x30 << 26 | 31 << 21 | 400 (idisp & 0x1fffff); 401 rdbg(dodebug, (" BR $31,%p", (void *)target)); 402 } else { 403 insn[insncnt++] = 0x1a << 26 | 31 << 21 | 404 27 << 16 | (idisp & 0x3fff); 405 rdbg(dodebug, (" JMP $31,($27),%d", 406 (int)(idisp & 0x3fff))); 407 } 408 409 /* 410 * Fill in the tail of the PLT entry first, for reentrancy. 411 * Until we have overwritten the first insn (an unconditional 412 * branch), the remaining insns have no effect. 413 */ 414 stubptr = (uint32_t *)stubaddr; 415 while (insncnt > 1) { 416 insncnt--; 417 stubptr[insncnt] = insn[insncnt]; 418 } 419 /* 420 * Commit the tail of the insn sequence to memory 421 * before overwriting the first insn. 422 */ 423 __asm __volatile("wmb" ::: "memory"); 424 stubptr[0] = insn[0]; 425 /* 426 * I-stream will be sync'd when we either return from 427 * the binder (lazy bind case) or when the PLTGOT thunk 428 * is patched up (bind-now case). 429 */ 430 } 431 432 out: 433 *addrp = (caddr_t)new_value; 434 return 0; 435} 436