rtld_machine.c revision 1.30
1/* $OpenBSD: rtld_machine.c,v 1.30 2015/07/26 03:08:16 guenther Exp $ */ 2 3/* 4 * Copyright (c) 2002 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/mman.h> 70 71#include <nlist.h> 72#include <link.h> 73#include <signal.h> 74 75#include "syscall.h" 76#include "archdep.h" 77#include "resolve.h" 78 79/* 80 * The following table holds for each relocation type: 81 * - the width in bits of the memory location the relocation 82 * applies to (not currently used) 83 * - the number of bits the relocation value must be shifted to the 84 * right (i.e. discard least significant bits) to fit into 85 * the appropriate field in the instruction word. 86 * - flags indicating whether 87 * * the relocation involves a symbol 88 * * the relocation is relative to the current position 89 * * the relocation is for a GOT entry 90 * * the relocation is relative to the load address 91 * 92 */ 93#define _RF_S 0x80000000 /* Resolve symbol */ 94#define _RF_A 0x40000000 /* Use addend */ 95#define _RF_P 0x20000000 /* Location relative */ 96#define _RF_G 0x10000000 /* GOT offset */ 97#define _RF_B 0x08000000 /* Load address relative */ 98#define _RF_U 0x04000000 /* Unaligned */ 99#define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 100#define _RF_RS(s) ((s) & 0xff) /* right shift */ 101static int reloc_target_flags[] = { 102 0, /* NONE */ 103 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32*/ 104 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC32 */ 105 _RF_G| _RF_SZ(32) | _RF_RS(00), /* GOT32 */ 106 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 107 _RF_S| _RF_SZ(32) | _RF_RS(0), /* COPY */ 108 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */ 109 _RF_S| _RF_SZ(32) | _RF_RS(0), /* JUMP_SLOT */ 110 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */ 111 0, /* GOTOFF XXX */ 112 0, /* GOTPC XXX */ 113 0, /* DUMMY 11 */ 114 0, /* DUMMY 12 */ 115 0, /* DUMMY 13 */ 116 0, /* DUMMY 14 */ 117 0, /* DUMMY 15 */ 118 0, /* DUMMY 16 */ 119 0, /* DUMMY 17 */ 120 0, /* DUMMY 18 */ 121 0, /* DUMMY 19 */ 122 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 123 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* PC_16 */ 124 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 125 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* RELOC_PC8 */ 126}; 127 128#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 129#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 130#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 131#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 132#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 133#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 134#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 135 136static long reloc_target_bitmask[] = { 137#define _BM(x) (~(-(1ULL << (x)))) 138 0, /* NONE */ 139 _BM(32), /* RELOC_32*/ 140 _BM(32), /* PC32 */ 141 _BM(32), /* GOT32 */ 142 _BM(32), /* PLT32 */ 143 0, /* COPY */ 144 _BM(32), /* GLOB_DAT */ 145 _BM(32), /* JUMP_SLOT */ 146 _BM(32), /* RELATIVE */ 147 0, /* GOTOFF XXX */ 148 0, /* GOTPC XXX */ 149 0, /* DUMMY 11 */ 150 0, /* DUMMY 12 */ 151 0, /* DUMMY 13 */ 152 0, /* DUMMY 14 */ 153 0, /* DUMMY 15 */ 154 0, /* DUMMY 16 */ 155 0, /* DUMMY 17 */ 156 0, /* DUMMY 18 */ 157 0, /* DUMMY 19 */ 158 _BM(16), /* RELOC_16 */ 159 _BM(8), /* PC_16 */ 160 _BM(8), /* RELOC_8 */ 161 _BM(8), /* RELOC_PC8 */ 162#undef _BM 163}; 164#define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 165 166void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value); 167 168int 169_dl_md_reloc(elf_object_t *object, int rel, int relsz) 170{ 171 long i; 172 long numrel; 173 long relrel; 174 int fails = 0; 175 Elf_Addr loff; 176 Elf_Addr prev_value = 0; 177 const Elf_Sym *prev_sym = NULL; 178 Elf_Rel *rels; 179 struct load_list *llist; 180 181 loff = object->obj_base; 182 numrel = object->Dyn.info[relsz] / sizeof(Elf32_Rel); 183 relrel = rel == DT_REL ? object->relcount : 0; 184 rels = (Elf32_Rel *)(object->Dyn.info[rel]); 185 if (rels == NULL) 186 return(0); 187 188 if (relrel > numrel) { 189 _dl_printf("relcount > numrel: %ld > %ld\n", relrel, numrel); 190 _dl_exit(20); 191 } 192 193 /* 194 * unprotect some segments if we need it. 195 */ 196 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 197 for (llist = object->load_list; llist != NULL; llist = llist->next) { 198 if (!(llist->prot & PROT_WRITE)) 199 _dl_mprotect(llist->start, llist->size, 200 llist->prot|PROT_WRITE); 201 } 202 } 203 204 /* tight loop for leading RELATIVE relocs */ 205 for (i = 0; i < relrel; i++, rels++) { 206 Elf_Addr *where; 207 208#ifdef DEBUG 209 if (ELF_R_TYPE(rels->r_info) != R_TYPE(RELATIVE)) { 210 _dl_printf("RELCOUNT wrong\n"); 211 _dl_exit(20); 212 } 213#endif 214 where = (Elf_Addr *)(rels->r_offset + loff); 215 *where += loff; 216 } 217 for (; i < numrel; i++, rels++) { 218 Elf_Addr *where, value, ooff, mask; 219 Elf_Word type; 220 const Elf_Sym *sym, *this; 221 const char *symn; 222 223 type = ELF_R_TYPE(rels->r_info); 224 225 if (type == R_TYPE(NONE)) 226 continue; 227 228 if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL) 229 continue; 230 231 where = (Elf_Addr *)(rels->r_offset + loff); 232 233 if (RELOC_USE_ADDEND(type)) 234 value = *where & RELOC_VALUE_BITMASK(type); 235 else 236 value = 0; 237 238 sym = NULL; 239 symn = NULL; 240 if (RELOC_RESOLVE_SYMBOL(type)) { 241 sym = object->dyn.symtab; 242 sym += ELF_R_SYM(rels->r_info); 243 symn = object->dyn.strtab + sym->st_name; 244 245 if (sym->st_shndx != SHN_UNDEF && 246 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 247 value += loff; 248 } else if (sym == prev_sym) { 249 value += prev_value; 250 } else { 251 this = NULL; 252 ooff = _dl_find_symbol_bysym(object, 253 ELF_R_SYM(rels->r_info), &this, 254 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 255 ((type == R_TYPE(JUMP_SLOT))? 256 SYM_PLT:SYM_NOTPLT), 257 sym, NULL); 258 if (this == NULL) { 259resolve_failed: 260 if (ELF_ST_BIND(sym->st_info) != 261 STB_WEAK) 262 fails++; 263 continue; 264 } 265 prev_sym = sym; 266 prev_value = (Elf_Addr)(ooff + this->st_value); 267 value += prev_value; 268 } 269 } 270 271 if (type == R_TYPE(JUMP_SLOT)) { 272 _dl_reloc_plt((Elf_Word *)where, value); 273 continue; 274 } 275 276 if (type == R_TYPE(COPY)) { 277 void *dstaddr = where; 278 const void *srcaddr; 279 const Elf_Sym *dstsym = sym, *srcsym = NULL; 280 size_t size = dstsym->st_size; 281 Elf_Addr soff; 282 283 soff = _dl_find_symbol(symn, &srcsym, 284 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT, 285 sym, object, NULL); 286 if (srcsym == NULL) 287 goto resolve_failed; 288 289 srcaddr = (void *)(soff + srcsym->st_value); 290 _dl_bcopy(srcaddr, dstaddr, size); 291 continue; 292 } 293 294 if (RELOC_PC_RELATIVE(type)) 295 value -= (Elf_Addr)where; 296 if (RELOC_BASE_RELATIVE(type)) 297 value += loff; 298 299 mask = RELOC_VALUE_BITMASK(type); 300 value >>= RELOC_VALUE_RIGHTSHIFT(type); 301 value &= mask; 302 303 if (RELOC_UNALIGNED(type)) { 304 /* Handle unaligned relocations. */ 305 Elf_Addr tmp = 0; 306 char *ptr = (char *)where; 307 int i, size = RELOC_TARGET_SIZE(type)/8; 308 309 /* Read it in one byte at a time. */ 310 for (i=0; i<size; i++) 311 tmp = (tmp << 8) | ptr[i]; 312 313 tmp &= ~mask; 314 tmp |= value; 315 316 /* Write it back out. */ 317 for (i=0; i<size; i++) 318 ptr[i] = ((tmp >> (8*i)) & 0xff); 319 } else if (RELOC_TARGET_SIZE(type) > 32) { 320 *where &= ~mask; 321 *where |= value; 322 } else { 323 Elf32_Addr *where32 = (Elf32_Addr *)where; 324 325 *where32 &= ~mask; 326 *where32 |= value; 327 } 328 } 329 330 /* reprotect the unprotected segments */ 331 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 332 for (llist = object->load_list; llist != NULL; llist = llist->next) { 333 if (!(llist->prot & PROT_WRITE)) 334 _dl_mprotect(llist->start, llist->size, 335 llist->prot); 336 } 337 } 338 339 return (fails); 340} 341 342#if 0 343struct jmpslot { 344 u_short opcode; 345 u_short addr[2]; 346 u_short reloc_index; 347#define JMPSLOT_RELOC_MASK 0xffff 348}; 349#define JUMP 0xe990 /* NOP + JMP opcode */ 350#endif 351 352void 353_dl_reloc_plt(Elf_Addr *where, Elf_Addr value) 354{ 355 *where = value; 356} 357 358/* 359 * Resolve a symbol at run-time. 360 */ 361Elf_Addr 362_dl_bind(elf_object_t *object, int index) 363{ 364 Elf_Rel *rel; 365 Elf_Word *addr; 366 const Elf_Sym *sym, *this; 367 const char *symn; 368 const elf_object_t *sobj; 369 Elf_Addr ooff; 370 sigset_t savedmask; 371 372 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 373 374 rel += index/sizeof(Elf_Rel); 375 376 sym = object->dyn.symtab; 377 sym += ELF_R_SYM(rel->r_info); 378 symn = object->dyn.strtab + sym->st_name; 379 380 addr = (Elf_Word *)(object->obj_base + rel->r_offset); 381 this = NULL; 382 ooff = _dl_find_symbol(symn, &this, 383 SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, object, &sobj); 384 if (this == NULL) { 385 _dl_printf("lazy binding failed!\n"); 386 *(volatile int *)0 = 0; /* XXX */ 387 } 388 389 if (sobj->traced && _dl_trace_plt(sobj, symn)) 390 return ooff + this->st_value; 391 392 /* if GOT is protected, allow the write */ 393 if (object->got_size != 0) { 394 _dl_thread_bind_lock(0, &savedmask); 395 _dl_mprotect((void*)object->got_start, object->got_size, 396 PROT_READ|PROT_WRITE); 397 } 398 399 _dl_reloc_plt(addr, ooff + this->st_value); 400 401 /* put the GOT back to RO */ 402 if (object->got_size != 0) { 403 _dl_mprotect((void*)object->got_start, object->got_size, 404 PROT_READ); 405 _dl_thread_bind_lock(1, &savedmask); 406 } 407 408 return((Elf_Addr)ooff + this->st_value); 409} 410 411int 412_dl_md_reloc_got(elf_object_t *object, int lazy) 413{ 414 extern void _dl_bind_start(void); /* XXX */ 415 int fails = 0; 416 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT]; 417 int i, num; 418 Elf_Rel *rel; 419 struct load_list *llist; 420 Elf_Addr ooff; 421 const Elf_Sym *this; 422 423 if (pltgot == NULL) 424 return (0); /* it is possible to have no PLT/GOT relocations */ 425 426 pltgot[1] = (Elf_Addr)object; 427 pltgot[2] = (Elf_Addr)&_dl_bind_start; 428 429 if (object->Dyn.info[DT_PLTREL] != DT_REL) 430 return (0); 431 432 object->got_addr = 0; 433 object->got_size = 0; 434 this = NULL; 435 ooff = _dl_find_symbol("__got_start", &this, 436 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL); 437 if (this != NULL) 438 object->got_addr = ooff + this->st_value; 439 440 this = NULL; 441 ooff = _dl_find_symbol("__got_end", &this, 442 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL); 443 if (this != NULL) 444 object->got_size = ooff + this->st_value - object->got_addr; 445 446 if (object->got_addr == 0) 447 object->got_start = 0; 448 else { 449 object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz); 450 object->got_size += object->got_addr - object->got_start; 451 object->got_size = ELF_ROUND(object->got_size, _dl_pagesz); 452 } 453 454 if (object->traced) 455 lazy = 1; 456 457 if (!lazy) { 458 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 459 } else { 460 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 461 num = (object->Dyn.info[DT_PLTRELSZ]); 462 for (llist = object->load_list; llist != NULL; 463 llist = llist->next) { 464 if (!(llist->prot & PROT_WRITE)) 465 _dl_mprotect(llist->start, llist->size, 466 llist->prot|PROT_WRITE); 467 } 468 for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) { 469 Elf_Addr *where; 470 where = (Elf_Addr *)(rel->r_offset + object->obj_base); 471 *where += object->obj_base; 472 } 473 for (llist = object->load_list; llist != NULL; 474 llist = llist->next) { 475 if (!(llist->prot & PROT_WRITE)) 476 _dl_mprotect(llist->start, llist->size, 477 llist->prot); 478 } 479 480 } 481 /* PLT is already RO on i386, no point in mprotecting it, just GOT */ 482 if (object->got_size != 0) 483 _dl_mprotect((void*)object->got_start, object->got_size, 484 PROT_READ); 485 486 return (fails); 487} 488