1 /* $OpenBSD: rtld_machine.c,v 1.16 2011/04/06 11:36:25 miod Exp $ */ 2 3 /* 4 * Copyright (c) 2002,2004 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_E 0x02000000 /* ERROR */ 101 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 102 #define _RF_RS(s) ((s) & 0xff) /* right shift */ 103 static int reloc_target_flags[] = { 104 0, /* 0 NONE */ 105 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 1 _64*/ 106 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* 2 PC32 */ 107 _RF_G|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 3 GOT32 */ 108 _RF_E|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 4 PLT32 */ 109 _RF_S| _RF_SZ(32) | _RF_RS(0), /* 5 COPY */ 110 _RF_S| _RF_SZ(64) | _RF_RS(0), /* 6 GLOB_DAT*/ 111 _RF_S| _RF_SZ(64) | _RF_RS(0), /* 7 JUMP_SLOT*/ 112 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* 8 RELATIVE*/ 113 _RF_E, /* 9 GOTPCREL*/ 114 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 32 */ 115 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 32S */ 116 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* 12 16 */ 117 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* 13 PC16 */ 118 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* 14 8 */ 119 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* 15 PC8 */ 120 _RF_E, /* 16 DPTMOD64*/ 121 _RF_E, /* 17 DTPOFF64*/ 122 _RF_E, /* 18 TPOFF64 */ 123 _RF_E, /* 19 TLSGD */ 124 _RF_E, /* 20 TLSLD */ 125 _RF_E, /* 21 DTPOFF32*/ 126 _RF_E, /* 22 GOTTPOFF*/ 127 _RF_E /* 23 TPOFF32*/ 128 }; 129 130 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 131 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 132 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 133 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 134 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 135 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 136 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 137 #define RELOC_ERROR(t) (reloc_target_flags[t] & _RF_E) 138 139 static long reloc_target_bitmask[] = { 140 #define _BM(x) (x == 64? ~0 : ~(-(1UL << (x)))) 141 0, /* 0 NONE */ 142 _BM(64), /* 1 _64*/ 143 _BM(32), /* 2 PC32 */ 144 _BM(32), /* 3 GOT32 */ 145 _BM(32), /* 4 PLT32 */ 146 _BM(0), /* 5 COPY */ 147 _BM(64), /* 6 GLOB_DAT*/ 148 _BM(64), /* 7 JUMP_SLOT*/ 149 _BM(64), /* 8 RELATIVE*/ 150 _BM(32), /* 9 GOTPCREL*/ 151 _BM(32), /* 10 32 */ 152 _BM(32), /* 11 32S */ 153 _BM(16), /* 12 16 */ 154 _BM(16), /* 13 PC16 */ 155 _BM(8), /* 14 8 */ 156 _BM(8), /* 15 PC8 */ 157 0, /* 16 DPTMOD64*/ 158 0, /* 17 DTPOFF64*/ 159 0, /* 18 TPOFF64 */ 160 0, /* 19 TLSGD */ 161 0, /* 20 TLSLD */ 162 0, /* 21 DTPOFF32*/ 163 0, /* 22 GOTTPOFF*/ 164 0 /* 23 TPOFF32*/ 165 #undef _BM 166 }; 167 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 168 169 void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value); 170 171 int 172 _dl_md_reloc(elf_object_t *object, int rel, int relsz) 173 { 174 long i; 175 long numrel; 176 int fails = 0; 177 Elf_Addr loff; 178 Elf_RelA *rels; 179 struct load_list *llist; 180 181 loff = object->obj_base; 182 numrel = object->Dyn.info[relsz] / sizeof(Elf_RelA); 183 rels = (Elf_RelA *)(object->Dyn.info[rel]); 184 if (rels == NULL) 185 return(0); 186 187 /* 188 * unprotect some segments if we need it. 189 */ 190 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 191 for (llist = object->load_list; llist != NULL; llist = llist->next) { 192 if (!(llist->prot & PROT_WRITE)) 193 _dl_mprotect(llist->start, llist->size, 194 llist->prot|PROT_WRITE); 195 } 196 } 197 198 for (i = 0; i < numrel; i++, rels++) { 199 Elf_Addr *where, value, ooff, mask; 200 Elf_Word type; 201 const Elf_Sym *sym, *this; 202 const char *symn; 203 204 type = ELF_R_TYPE(rels->r_info); 205 206 if (RELOC_ERROR(type)) { 207 _dl_printf("relocation error %d idx %d\n", type, i); 208 _dl_exit(20); 209 } 210 211 if (type == R_TYPE(NONE)) 212 continue; 213 214 if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL) 215 continue; 216 217 where = (Elf_Addr *)(rels->r_offset + loff); 218 219 if (RELOC_USE_ADDEND(type)) 220 value = rels->r_addend; 221 else 222 value = 0; 223 224 sym = NULL; 225 symn = NULL; 226 if (RELOC_RESOLVE_SYMBOL(type)) { 227 sym = object->dyn.symtab; 228 sym += ELF_R_SYM(rels->r_info); 229 symn = object->dyn.strtab + sym->st_name; 230 231 if (sym->st_shndx != SHN_UNDEF && 232 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 233 value += loff; 234 } else { 235 this = NULL; 236 ooff = _dl_find_symbol_bysym(object, 237 ELF_R_SYM(rels->r_info), &this, 238 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 239 ((type == R_TYPE(JUMP_SLOT))? 240 SYM_PLT:SYM_NOTPLT), 241 sym, NULL); 242 if (this == NULL) { 243 resolve_failed: 244 if (ELF_ST_BIND(sym->st_info) != 245 STB_WEAK) 246 fails++; 247 continue; 248 } 249 value += (Elf_Addr)(ooff + this->st_value); 250 } 251 } 252 253 if (type == R_TYPE(JUMP_SLOT)) { 254 _dl_reloc_plt(where, value); 255 continue; 256 } 257 258 if (type == R_TYPE(COPY)) { 259 void *dstaddr = where; 260 const void *srcaddr; 261 const Elf_Sym *dstsym = sym, *srcsym = NULL; 262 Elf_Addr soff; 263 264 soff = _dl_find_symbol(symn, &srcsym, 265 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND| 266 ((type == R_TYPE(JUMP_SLOT)) ? SYM_PLT:SYM_NOTPLT), 267 dstsym, object, NULL); 268 if (srcsym == NULL) 269 goto resolve_failed; 270 271 srcaddr = (void *)(soff + srcsym->st_value); 272 _dl_bcopy(srcaddr, dstaddr, dstsym->st_size); 273 continue; 274 } 275 276 if (RELOC_PC_RELATIVE(type)) 277 value -= (Elf_Addr)where; 278 if (RELOC_BASE_RELATIVE(type)) 279 value += loff; 280 281 mask = RELOC_VALUE_BITMASK(type); 282 value >>= RELOC_VALUE_RIGHTSHIFT(type); 283 value &= mask; 284 285 if (RELOC_UNALIGNED(type)) { 286 /* Handle unaligned relocations. */ 287 Elf_Addr tmp = 0; 288 char *ptr = (char *)where; 289 int i, size = RELOC_TARGET_SIZE(type)/8; 290 291 /* Read it in one byte at a time. */ 292 for (i=0; i<size; i++) 293 tmp = (tmp << 8) | ptr[i]; 294 295 tmp &= ~mask; 296 tmp |= value; 297 298 /* Write it back out. */ 299 for (i=0; i<size; i++) 300 ptr[i] = ((tmp >> (8*i)) & 0xff); 301 } else if (RELOC_TARGET_SIZE(type) > 32) { 302 *where &= ~mask; 303 *where |= value; 304 } else { 305 Elf32_Addr *where32 = (Elf32_Addr *)where; 306 307 *where32 &= ~mask; 308 *where32 |= value; 309 } 310 } 311 312 /* reprotect the unprotected segments */ 313 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 314 for (llist = object->load_list; llist != NULL; llist = llist->next) { 315 if (!(llist->prot & PROT_WRITE)) 316 _dl_mprotect(llist->start, llist->size, 317 llist->prot); 318 } 319 } 320 321 return (fails); 322 } 323 324 void 325 _dl_reloc_plt(Elf_Addr *where, Elf_Addr value) 326 { 327 *where = value; 328 } 329 330 /* 331 * Resolve a symbol at run-time. 332 */ 333 Elf_Addr 334 _dl_bind(elf_object_t *object, int index) 335 { 336 Elf_RelA *rel; 337 Elf_Word *addr; 338 const Elf_Sym *sym, *this; 339 const char *symn; 340 Elf_Addr ooff, newval; 341 sigset_t savedmask; 342 343 rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]); 344 345 rel += index; 346 347 sym = object->dyn.symtab; 348 sym += ELF_R_SYM(rel->r_info); 349 symn = object->dyn.strtab + sym->st_name; 350 351 addr = (Elf_Word *)(object->obj_base + rel->r_offset); 352 this = NULL; 353 ooff = _dl_find_symbol(symn, &this, 354 SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, 355 object, NULL); 356 if (this == NULL) { 357 _dl_printf("lazy binding failed!\n"); 358 *((int *)0) = 0; /* XXX */ 359 } 360 361 newval = ooff + this->st_value + rel->r_addend; 362 363 /* if GOT is protected, allow the write */ 364 if (object->got_size != 0) { 365 _dl_thread_bind_lock(0, &savedmask); 366 _dl_mprotect((void*)object->got_start, object->got_size, 367 PROT_READ|PROT_WRITE); 368 } 369 370 _dl_reloc_plt((Elf_Addr *)addr, newval); 371 372 /* put the GOT back to RO */ 373 if (object->got_size != 0) { 374 _dl_mprotect((void*)object->got_start, object->got_size, 375 PROT_READ); 376 _dl_thread_bind_lock(1, &savedmask); 377 } 378 379 return(newval); 380 } 381 382 int 383 _dl_md_reloc_got(elf_object_t *object, int lazy) 384 { 385 extern void _dl_bind_start(void); /* XXX */ 386 int fails = 0; 387 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT]; 388 int i, num; 389 Elf_RelA *rel; 390 Elf_Addr ooff; 391 const Elf_Sym *this; 392 393 if (pltgot == NULL) 394 return (0); /* it is possible to have no PLT/GOT relocations */ 395 396 pltgot[1] = (Elf_Addr)object; 397 pltgot[2] = (Elf_Addr)&_dl_bind_start; 398 399 if (object->Dyn.info[DT_PLTREL] != DT_RELA) 400 return (0); 401 402 object->got_addr = 0; 403 object->got_size = 0; 404 this = NULL; 405 ooff = _dl_find_symbol("__got_start", &this, 406 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL); 407 if (this != NULL) 408 object->got_addr = ooff + this->st_value; 409 410 this = NULL; 411 ooff = _dl_find_symbol("__got_end", &this, 412 SYM_SEARCH_OBJ|SYM_NOWARNNOTFOUND|SYM_PLT, NULL, object, NULL); 413 if (this != NULL) 414 object->got_size = ooff + this->st_value - object->got_addr; 415 416 if (object->got_addr == 0) 417 object->got_start = 0; 418 else { 419 object->got_start = ELF_TRUNC(object->got_addr, _dl_pagesz); 420 object->got_size += object->got_addr - object->got_start; 421 object->got_size = ELF_ROUND(object->got_size, _dl_pagesz); 422 } 423 424 if (!lazy) { 425 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 426 } else { 427 rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]); 428 num = (object->Dyn.info[DT_PLTRELSZ]); 429 for (i = 0; i < num/sizeof(Elf_RelA); i++, rel++) { 430 Elf_Addr *where; 431 where = (Elf_Addr *)(rel->r_offset + object->obj_base); 432 *where += object->obj_base; 433 } 434 435 } 436 437 /* PLT is already RO on i386, no point in mprotecting it, just GOT */ 438 if (object->got_size != 0) 439 _dl_mprotect((void*)object->got_start, object->got_size, 440 PROT_READ); 441 442 return (fails); 443 } 444