1 /* $OpenBSD: rtld_machine.c,v 1.43 2008/07/16 20:33:42 drahn Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Dale Rahn 5 * Copyright (c) 2001 Niklas Hallqvist 6 * Copyright (c) 2001 Artur Grabowski 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 ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * 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 * Copyright (c) 2000 Eduardo Horvath. 31 * Copyright (c) 1999 The NetBSD Foundation, Inc. 32 * All rights reserved. 33 * 34 * This code is derived from software contributed to The NetBSD Foundation 35 * by Paul Kranenburg. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the NetBSD 48 * Foundation, Inc. and its contributors. 49 * 4. Neither the name of The NetBSD Foundation nor the names of its 50 * contributors may be used to endorse or promote products derived 51 * from this software without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 * POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 #define _DYN_LOADER 67 68 #include <sys/types.h> 69 #include <sys/cdefs.h> 70 #include <sys/mman.h> 71 72 #include <nlist.h> 73 #include <link.h> 74 #include <signal.h> 75 76 #include "syscall.h" 77 #include "archdep.h" 78 #include "resolve.h" 79 80 /* 81 * The following table holds for each relocation type: 82 * - the width in bits of the memory location the relocation 83 * applies to (not currently used) 84 * - the number of bits the relocation value must be shifted to the 85 * right (i.e. discard least significant bits) to fit into 86 * the appropriate field in the instruction word. 87 * - flags indicating whether 88 * * the relocation involves a symbol 89 * * the relocation is relative to the current position 90 * * the relocation is for a GOT entry 91 * * the relocation is relative to the load address 92 * 93 */ 94 #define _RF_S 0x80000000 /* Resolve symbol */ 95 #define _RF_A 0x40000000 /* Use addend */ 96 #define _RF_P 0x20000000 /* Location relative */ 97 #define _RF_G 0x10000000 /* GOT offset */ 98 #define _RF_B 0x08000000 /* Load address relative */ 99 #define _RF_U 0x04000000 /* Unaligned */ 100 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 101 #define _RF_RS(s) ((s) & 0xff) /* right shift */ 102 static int reloc_target_flags[] = { 103 0, /* NONE */ 104 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 105 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 106 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */ 107 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 108 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 109 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 110 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 111 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 112 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */ 113 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */ 114 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */ 115 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */ 116 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 117 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 118 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 119 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 120 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 121 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 122 _RF_S| _RF_SZ(32) | _RF_RS(0), /* COPY */ 123 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */ 124 _RF_S| _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 125 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */ 126 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ 127 128 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 129 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */ 130 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */ 131 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */ 132 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */ 133 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */ 134 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */ 135 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */ 136 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */ 137 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */ 138 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */ 139 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */ 140 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */ 141 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */ 142 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */ 143 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */ 144 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */ 145 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */ 146 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */ 147 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */ 148 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */ 149 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */ 150 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */ 151 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */ 152 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */ 153 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */ 154 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */ 155 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */ 156 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */ 157 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */ 158 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */ 159 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */ 160 }; 161 162 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 163 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 164 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 165 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 166 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 167 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 168 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 169 170 static long reloc_target_bitmask[] = { 171 #define _BM(x) (~(-(1ULL << (x)))) 172 0, /* NONE */ 173 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ 174 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 175 _BM(30), _BM(22), /* WDISP30, WDISP22 */ 176 _BM(22), _BM(22), /* HI22, _22 */ 177 _BM(13), _BM(10), /* RELOC_13, _LO10 */ 178 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 179 _BM(10), _BM(22), /* _PC10, _PC22 */ 180 _BM(30), 0, /* _WPLT30, _COPY */ 181 -1, _BM(32), -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ 182 _BM(32), _BM(32), /* _UA32, PLT32 */ 183 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */ 184 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */ 185 _BM(10), _BM(11), -1, /* _10, _11, _64 */ 186 _BM(10), _BM(22), /* _OLO10, _HH22 */ 187 _BM(10), _BM(22), /* _HM10, _LM22 */ 188 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */ 189 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */ 190 -1, /* GLOB_JMP */ 191 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */ 192 -1, -1, /* DISP64, PLT64 */ 193 _BM(22), _BM(13), /* HIX22, LOX10 */ 194 _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */ 195 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */ 196 #undef _BM 197 }; 198 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 199 200 void _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_RelA *rela); 201 void _dl_install_plt(Elf_Word *pltgot, Elf_Addr proc); 202 203 int 204 _dl_md_reloc(elf_object_t *object, int rel, int relasz) 205 { 206 long i; 207 long numrela; 208 int fails = 0; 209 Elf_Addr loff; 210 Elf_RelA *relas; 211 struct load_list *llist; 212 213 loff = object->obj_base; 214 numrela = object->Dyn.info[relasz] / sizeof(Elf64_Rela); 215 relas = (Elf64_Rela *)(object->Dyn.info[rel]); 216 217 if (relas == NULL) 218 return(0); 219 220 /* 221 * unprotect some segments if we need it. 222 */ 223 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 224 for (llist = object->load_list; llist != NULL; llist = llist->next) { 225 if (!(llist->prot & PROT_WRITE)) 226 _dl_mprotect(llist->start, llist->size, 227 llist->prot|PROT_WRITE); 228 } 229 } 230 231 for (i = 0; i < numrela; i++, relas++) { 232 Elf_Addr *where, value, ooff, mask; 233 Elf_Word type; 234 const Elf_Sym *sym, *this; 235 const char *symn; 236 237 type = ELF_R_TYPE(relas->r_info); 238 239 if (type == R_TYPE(NONE)) 240 continue; 241 242 if (type == R_TYPE(JMP_SLOT) && rel != DT_JMPREL) 243 continue; 244 245 where = (Elf_Addr *)(relas->r_offset + loff); 246 247 if (RELOC_USE_ADDEND(type)) 248 value = relas->r_addend; 249 else 250 value = 0; 251 252 sym = NULL; 253 symn = NULL; 254 if (RELOC_RESOLVE_SYMBOL(type)) { 255 sym = object->dyn.symtab; 256 sym += ELF_R_SYM(relas->r_info); 257 symn = object->dyn.strtab + sym->st_name; 258 259 if (sym->st_shndx != SHN_UNDEF && 260 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 261 value += loff; 262 } else { 263 this = NULL; 264 ooff = _dl_find_symbol_bysym(object, 265 ELF_R_SYM(relas->r_info), &this, 266 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 267 ((type == R_TYPE(JMP_SLOT)) ? 268 SYM_PLT : SYM_NOTPLT), 269 sym, NULL); 270 if (this == NULL) { 271 resolve_failed: 272 if (ELF_ST_BIND(sym->st_info) != 273 STB_WEAK) 274 fails++; 275 continue; 276 } 277 value += (Elf_Addr)(ooff + this->st_value); 278 } 279 } 280 281 if (type == R_TYPE(JMP_SLOT)) { 282 _dl_reloc_plt((Elf_Word *)where, value, relas); 283 continue; 284 } 285 286 if (type == R_TYPE(COPY)) { 287 void *dstaddr = where; 288 const void *srcaddr; 289 const Elf_Sym *dstsym = sym, *srcsym = NULL; 290 size_t size = dstsym->st_size; 291 Elf_Addr soff; 292 293 soff = _dl_find_symbol(symn, &srcsym, 294 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT, 295 dstsym, object, NULL); 296 if (srcsym == NULL) 297 goto resolve_failed; 298 299 srcaddr = (void *)(soff + srcsym->st_value); 300 _dl_bcopy(srcaddr, dstaddr, size); 301 continue; 302 } 303 304 if (RELOC_PC_RELATIVE(type)) 305 value -= (Elf_Addr)where; 306 if (RELOC_BASE_RELATIVE(type)) 307 value += loff; 308 309 mask = RELOC_VALUE_BITMASK(type); 310 value >>= RELOC_VALUE_RIGHTSHIFT(type); 311 value &= mask; 312 313 if (RELOC_UNALIGNED(type)) { 314 /* Handle unaligned relocations. */ 315 Elf_Addr tmp = 0; 316 char *ptr = (char *)where; 317 int i, size = RELOC_TARGET_SIZE(type)/8; 318 319 /* Read it in one byte at a time. */ 320 for (i=0; i<size; i++) 321 tmp = (tmp << 8) | ptr[i]; 322 323 tmp &= ~mask; 324 tmp |= value; 325 326 /* Write it back out. */ 327 for (i=0; i<size; i++) 328 ptr[i] = ((tmp >> (8*i)) & 0xff); 329 } else if (RELOC_TARGET_SIZE(type) > 32) { 330 *where &= ~mask; 331 *where |= value; 332 } else { 333 Elf32_Addr *where32 = (Elf32_Addr *)where; 334 335 *where32 &= ~mask; 336 *where32 |= value; 337 } 338 } 339 340 /* reprotect the unprotected segments */ 341 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 342 for (llist = object->load_list; llist != NULL; llist = llist->next) { 343 if (!(llist->prot & PROT_WRITE)) 344 _dl_mprotect(llist->start, llist->size, 345 llist->prot); 346 } 347 } 348 349 return (fails); 350 } 351 352 /* 353 * Instruction templates: 354 */ 355 #define BAA 0x10400000 /* ba,a %xcc, 0 */ 356 #define SETHI 0x03000000 /* sethi %hi(0), %g1 */ 357 #define JMP 0x81c06000 /* jmpl %g1+%lo(0), %g0 */ 358 #define NOP 0x01000000 /* sethi %hi(0), %g0 */ 359 #define OR 0x82106000 /* or %g1, 0, %g1 */ 360 #define ORG5 0x8a116000 /* or %g5, 0, %g5 */ 361 #define XOR 0x82186000 /* xor %g1, 0, %g1 */ 362 #define MOV71 0x8283a000 /* or %o7, 0, %g1 */ 363 #define MOV17 0x9c806000 /* or %g1, 0, %o7 */ 364 #define CALL 0x40000000 /* call 0 */ 365 #define SLLX 0x83287000 /* sllx %g1, 0, %g1 */ 366 #define SLLXG5 0x8b297000 /* sllx %g5, 0, %g5 */ 367 #define SRAX 0x83387000 /* srax %g1, 0, %g1 */ 368 #define SETHIG5 0x0b000000 /* sethi %hi(0), %g5 */ 369 #define ORG15 0x82804005 /* or %g1, %g5, %g1 */ 370 371 372 /* %hi(v) with variable shift */ 373 #define HIVAL(v, s) (((v) >> (s)) & 0x003fffff) 374 #define LOVAL(v) ((v) & 0x000003ff) 375 376 void 377 _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_RelA *rela) 378 { 379 Elf_Addr offset; 380 381 /* 382 * At the PLT entry pointed at by `where', we now construct 383 * a direct transfer to the now fully resolved function 384 * address. 385 * 386 * A PLT entry is supposed to start by looking like this: 387 * 388 * sethi %hi(. - .PLT0), %g1 389 * ba,a %xcc, .PLT1 390 * nop 391 * nop 392 * nop 393 * nop 394 * nop 395 * nop 396 * 397 * When we replace these entries we start from the second 398 * entry and do it in reverse order so the last thing we 399 * do is replace the branch. That allows us to change this 400 * atomically. 401 * 402 * We now need to find out how far we need to jump. We 403 * have a choice of several different relocation techniques 404 * which are increasingly expensive. 405 */ 406 407 offset = ((Elf_Addr)where) - value; 408 if (rela->r_addend) { 409 Elf_Addr *ptr = (Elf_Addr *)where; 410 /* 411 * This entry is >32768. Just replace the pointer. 412 */ 413 ptr[0] = value; 414 415 } else if (offset <= (1L<<20) && offset >= -(1L<<20)) { 416 /* 417 * We're within 1MB -- we can use a direct branch insn. 418 * 419 * We can generate this pattern: 420 * 421 * sethi %hi(. - .PLT0), %g1 422 * ba,a %xcc, addr 423 * nop 424 * nop 425 * nop 426 * nop 427 * nop 428 * nop 429 * 430 */ 431 where[1] = BAA | ((offset >> 2) &0x3fffff); 432 __asm __volatile("iflush %0+4" : : "r" (where)); 433 } else if (value < (1UL<<32)) { 434 /* 435 * We're within 32-bits of address zero. 436 * 437 * The resulting code in the jump slot is: 438 * 439 * sethi %hi(. - .PLT0), %g1 440 * sethi %hi(addr), %g1 441 * jmp %g1+%lo(addr) 442 * nop 443 * nop 444 * nop 445 * nop 446 * nop 447 * 448 */ 449 where[2] = JMP | LOVAL(value); 450 where[1] = SETHI | HIVAL(value, 10); 451 __asm __volatile("iflush %0+8" : : "r" (where)); 452 __asm __volatile("iflush %0+4" : : "r" (where)); 453 454 } else if (value > -(1UL<<32)) { 455 /* 456 * We're within 32-bits of address -1. 457 * 458 * The resulting code in the jump slot is: 459 * 460 * sethi %hi(. - .PLT0), %g1 461 * sethi %hix(addr), %g1 462 * xor %g1, %lox(addr), %g1 463 * jmp %g1 464 * nop 465 * nop 466 * nop 467 * nop 468 * 469 */ 470 where[3] = JMP; 471 where[2] = XOR | ((~value) & 0x00001fff); 472 where[1] = SETHI | HIVAL(~value, 10); 473 __asm __volatile("iflush %0+12" : : "r" (where)); 474 __asm __volatile("iflush %0+8" : : "r" (where)); 475 __asm __volatile("iflush %0+4" : : "r" (where)); 476 477 } else if (offset <= (1L<<32) && offset >= -((1L<<32) - 4)) { 478 /* 479 * We're within 32-bits -- we can use a direct call insn 480 * 481 * The resulting code in the jump slot is: 482 * 483 * sethi %hi(. - .PLT0), %g1 484 * mov %o7, %g1 485 * call (.+offset) 486 * mov %g1, %o7 487 * nop 488 * nop 489 * nop 490 * nop 491 * 492 */ 493 where[3] = MOV17; 494 where[2] = CALL | ((offset >> 4) & 0x3fffffff); 495 where[1] = MOV71; 496 __asm __volatile("iflush %0+12" : : "r" (where)); 497 __asm __volatile("iflush %0+8" : : "r" (where)); 498 __asm __volatile("iflush %0+4" : : "r" (where)); 499 500 } else if (value < (1L<<42)) { 501 /* 502 * Target 42bits or smaller. 503 * We can generate this pattern: 504 * 505 * The resulting code in the jump slot is: 506 * 507 * sethi %hi(. - .PLT0), %g1 508 * sethi %hi(addr >> 20), %g1 509 * or %g1, %lo(addr >> 10), %g1 510 * sllx %g1, 10, %g1 511 * jmp %g1+%lo(addr) 512 * nop 513 * nop 514 * nop 515 * 516 * this can handle addresses 0 - 0x3fffffffffc 517 */ 518 where[4] = JMP | LOVAL(value); 519 where[3] = SLLX | 10; 520 where[2] = OR | LOVAL(value >> 10); 521 where[1] = SETHI | HIVAL(value, 20); 522 __asm __volatile("iflush %0+16" : : "r" (where)); 523 __asm __volatile("iflush %0+12" : : "r" (where)); 524 __asm __volatile("iflush %0+8" : : "r" (where)); 525 __asm __volatile("iflush %0+4" : : "r" (where)); 526 527 } else if (value > -(1UL<<41)) { 528 /* 529 * Large target >= 0xfffffe0000000000UL 530 * We can generate this pattern: 531 * 532 * The resulting code in the jump slot is: 533 * 534 * sethi %hi(. - .PLT0), %g1 535 * sethi %hi(addr >> 20), %g1 536 * or %g1, %lo(addr >> 10), %g1 537 * sllx %g1, 32, %g1 538 * srax %g1, 22, %g1 539 * jmp %g1+%lo(addr) 540 * nop 541 * nop 542 * nop 543 * 544 */ 545 where[5] = JMP | LOVAL(value); 546 where[4] = SRAX | 22; 547 where[3] = SLLX | 32; 548 where[2] = OR | LOVAL(value >> 10); 549 where[1] = SETHI | HIVAL(value, 20); 550 551 __asm __volatile("iflush %0+16" : : "r" (where)); 552 __asm __volatile("iflush %0+12" : : "r" (where)); 553 __asm __volatile("iflush %0+8" : : "r" (where)); 554 __asm __volatile("iflush %0+4" : : "r" (where)); 555 556 } else { 557 /* 558 * We need to load all 64-bits 559 * 560 * The resulting code in the jump slot is: 561 * 562 * sethi %hi(. - .PLT0), %g1 563 * sethi %hi(addr >> 42), %g5 564 * sethi %hi(addr >> 10), %g1 565 * or %g1, %lo(addr >> 32), %g5 566 * sllx %g5, 32, %g5 567 * or %g1, %g5, %g1 568 * jmp %g1+%lo(addr) 569 * nop 570 * 571 */ 572 where[6] = JMP | LOVAL(value); 573 where[5] = ORG15; 574 where[4] = SLLXG5 | 32; 575 where[3] = ORG5 | LOVAL(value >> 32); 576 where[2] = SETHI | HIVAL(value, 10); 577 where[1] = SETHIG5 | HIVAL(value, 42); 578 __asm __volatile("iflush %0+24" : : "r" (where)); 579 __asm __volatile("iflush %0+20" : : "r" (where)); 580 __asm __volatile("iflush %0+16" : : "r" (where)); 581 __asm __volatile("iflush %0+12" : : "r" (where)); 582 __asm __volatile("iflush %0+8" : : "r" (where)); 583 __asm __volatile("iflush %0+4" : : "r" (where)); 584 } 585 } 586 587 /* 588 * Resolve a symbol at run-time. 589 */ 590 Elf_Addr 591 _dl_bind(elf_object_t *object, int index) 592 { 593 Elf_RelA *rela; 594 Elf_Word *addr; 595 Elf_Addr ooff; 596 const Elf_Sym *sym, *this; 597 const char *symn; 598 sigset_t omask, nmask; 599 600 rela = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]); 601 if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)) { 602 /* 603 * XXXX 604 * 605 * The first four PLT entries are reserved. There 606 * is some disagreement whether they should have 607 * associated relocation entries. Both the SPARC 608 * 32-bit and 64-bit ELF specifications say that 609 * they should have relocation entries, but the 610 * 32-bit SPARC binutils do not generate them, 611 * and now the 64-bit SPARC binutils have stopped 612 * generating them too. 613 * 614 * So, to provide binary compatibility, we will 615 * check the first entry, if it is reserved it 616 * should not be of the type JMP_SLOT. If it 617 * is JMP_SLOT, then the 4 reserved entries were 618 * not generated and our index is 4 entries too far. 619 */ 620 index -= 4; 621 } 622 623 rela += index; 624 625 sym = object->dyn.symtab; 626 sym += ELF64_R_SYM(rela->r_info); 627 symn = object->dyn.strtab + sym->st_name; 628 629 addr = (Elf_Word *)(object->obj_base + rela->r_offset); 630 this = NULL; 631 ooff = _dl_find_symbol(symn, &this, 632 SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, 633 object, NULL); 634 if (this == NULL) { 635 _dl_printf("lazy binding failed!\n"); 636 *((int *)0) = 0; /* XXX */ 637 } 638 639 /* if PLT is protected, allow the write */ 640 if (object->plt_size != 0) { 641 sigfillset(&nmask); 642 _dl_sigprocmask(SIG_BLOCK, &nmask, &omask); 643 _dl_thread_bind_lock(0); 644 _dl_mprotect((void*)object->plt_start, object->plt_size, 645 PROT_READ|PROT_WRITE|PROT_EXEC); 646 } 647 648 _dl_reloc_plt(addr, ooff + this->st_value, rela); 649 650 /* if PLT is (to be protected), change back to RO/X */ 651 if (object->plt_size != 0) { 652 _dl_mprotect((void*)object->plt_start, object->plt_size, 653 PROT_READ|PROT_EXEC); 654 _dl_thread_bind_lock(1); 655 _dl_sigprocmask(SIG_SETMASK, &omask, NULL); 656 } 657 658 return ooff + this->st_value; 659 } 660 661 /* 662 * Install rtld function call into this PLT slot. 663 */ 664 #define SAVE 0x9de3bf50 665 #define SETHI_l0 0x21000000 666 #define SETHI_l1 0x23000000 667 #define OR_l0_l0 0xa0142000 668 #define SLLX_l0_32_l0 0xa12c3020 669 #define OR_l0_l1_l0 0xa0140011 670 #define JMPL_l0_o1 0x93c42000 671 #define MOV_g1_o0 0x90100001 672 673 void 674 _dl_install_plt(Elf_Word *pltgot, Elf_Addr proc) 675 { 676 pltgot[0] = SAVE; 677 pltgot[1] = SETHI_l0 | HIVAL(proc, 42); 678 pltgot[2] = SETHI_l1 | HIVAL(proc, 10); 679 pltgot[3] = OR_l0_l0 | LOVAL((proc) >> 32); 680 pltgot[4] = SLLX_l0_32_l0; 681 pltgot[5] = OR_l0_l1_l0; 682 pltgot[6] = JMPL_l0_o1 | LOVAL(proc); 683 pltgot[7] = MOV_g1_o0; 684 } 685 686 void _dl_bind_start_0(long, long); 687 void _dl_bind_start_1(long, long); 688 689 /* 690 * Relocate the Global Offset Table (GOT). 691 */ 692 int 693 _dl_md_reloc_got(elf_object_t *object, int lazy) 694 { 695 int fails = 0; 696 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT]; 697 Elf_Word *entry = (Elf_Word *)pltgot; 698 Elf_Addr ooff; 699 Elf_Addr plt_addr; 700 const Elf_Sym *this; 701 702 if (object->Dyn.info[DT_PLTREL] != DT_RELA) 703 return (0); 704 705 object->got_addr = NULL; 706 object->got_size = 0; 707 this = NULL; 708 ooff = _dl_find_symbol("__got_start", &this, 709 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, 710 object, NULL); 711 if (this != NULL) 712 object->got_addr = ooff + this->st_value; 713 714 this = NULL; 715 ooff = _dl_find_symbol("__got_end", &this, 716 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, 717 object, NULL); 718 if (this != NULL) 719 object->got_size = ooff + this->st_value - object->got_addr; 720 721 plt_addr = 0; 722 object->plt_size = 0; 723 this = NULL; 724 ooff = _dl_find_symbol("__plt_start", &this, 725 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, 726 object, NULL); 727 if (this != NULL) 728 plt_addr = ooff + this->st_value; 729 730 this = NULL; 731 ooff = _dl_find_symbol("__plt_end", &this, 732 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, 733 object, NULL); 734 if (this != NULL) 735 object->plt_size = ooff + this->st_value - plt_addr; 736 737 if (object->got_addr == NULL) 738 object->got_start = NULL; 739 else { 740 object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz); 741 object->got_size += object->got_addr - object->got_start; 742 object->got_size = ELF_ROUND(object->got_size, _dl_pagesz); 743 } 744 if (plt_addr == NULL) 745 object->plt_start = NULL; 746 else { 747 object->plt_start = ELF_TRUNC(plt_addr, _dl_pagesz); 748 object->plt_size += plt_addr - object->plt_start; 749 object->plt_size = ELF_ROUND(object->plt_size, _dl_pagesz); 750 } 751 752 if (!lazy) { 753 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 754 } else { 755 _dl_install_plt(&entry[0], (Elf_Addr)&_dl_bind_start_0); 756 _dl_install_plt(&entry[8], (Elf_Addr)&_dl_bind_start_1); 757 758 pltgot[8] = (Elf_Addr)object; 759 } 760 if (object->got_size != 0) 761 _dl_mprotect((void*)object->got_start, object->got_size, 762 PROT_READ); 763 if (object->plt_size != 0) 764 _dl_mprotect((void*)object->plt_start, object->plt_size, 765 PROT_READ|PROT_EXEC); 766 767 return (fails); 768 } 769