1 /* $OpenBSD: rtld_machine.c,v 1.26 2016/06/21 15:25:37 deraadt 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/mman.h> 70 #include <sys/syscall.h> 71 #include <sys/unistd.h> 72 73 #include <nlist.h> 74 #include <link.h> 75 76 #include "syscall.h" 77 #include "archdep.h" 78 #include "resolve.h" 79 80 int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden; 81 82 /* 83 * The following table holds for each relocation type: 84 * - the width in bits of the memory location the relocation 85 * applies to 86 * - the number of bits the relocation value must be shifted to the 87 * right (i.e. discard least significant bits) to fit into 88 * the appropriate field in the instruction word. 89 * - flags indicating whether 90 * * the relocation involves a symbol 91 * * the relocation is relative to the current position 92 * * the relocation is for a GOT entry 93 * * the relocation is relative to the load address 94 * 95 */ 96 #define _RF_S 0x80000000 /* Resolve symbol */ 97 #define _RF_A 0x40000000 /* Use addend */ 98 #define _RF_P 0x20000000 /* Location relative */ 99 #define _RF_G 0x10000000 /* GOT offset */ 100 #define _RF_B 0x08000000 /* Load address relative */ 101 #define _RF_E 0x02000000 /* ERROR */ 102 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 103 #define _RF_RS(s) ((s) & 0xff) /* right shift */ 104 static int reloc_target_flags[] = { 105 0, /* 0 NONE */ 106 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 1 _64*/ 107 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* 2 PC32 */ 108 _RF_G|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 3 GOT32 */ 109 _RF_E|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 4 PLT32 */ 110 _RF_S| _RF_SZ(32) | _RF_RS(0), /* 5 COPY */ 111 _RF_S| _RF_SZ(64) | _RF_RS(0), /* 6 GLOB_DAT*/ 112 _RF_S| _RF_SZ(64) | _RF_RS(0), /* 7 JUMP_SLOT*/ 113 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* 8 RELATIVE*/ 114 _RF_E, /* 9 GOTPCREL*/ 115 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 32 */ 116 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 32S */ 117 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* 12 16 */ 118 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* 13 PC16 */ 119 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* 14 8 */ 120 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* 15 PC8 */ 121 _RF_E, /* 16 DTPMOD64*/ 122 _RF_E, /* 17 DTPOFF64*/ 123 _RF_E, /* 18 TPOFF64 */ 124 _RF_E, /* 19 TLSGD */ 125 _RF_E, /* 20 TLSLD */ 126 _RF_E, /* 21 DTPOFF32*/ 127 _RF_E, /* 22 GOTTPOFF*/ 128 _RF_E /* 23 TPOFF32*/ 129 }; 130 131 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 132 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 133 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 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 Elf_Addr reloc_target_bitmask[] = { 140 #define _BM(x) (~(Elf_Addr)0 >> ((8*sizeof(reloc_target_bitmask[0])) - (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 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 DTPMOD64*/ 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 long relrel; 177 int fails = 0; 178 Elf_Addr loff; 179 Elf_Addr prev_value = 0; 180 const Elf_Sym *prev_sym = NULL; 181 Elf_RelA *rels; 182 struct load_list *llist; 183 184 loff = object->obj_base; 185 numrel = object->Dyn.info[relsz] / sizeof(Elf_RelA); 186 relrel = rel == DT_RELA ? object->relacount : 0; 187 rels = (Elf_RelA *)(object->Dyn.info[rel]); 188 if (rels == NULL) 189 return(0); 190 191 if (relrel > numrel) { 192 _dl_printf("relacount > numrel: %ld > %ld\n", relrel, numrel); 193 _dl_exit(20); 194 } 195 196 /* 197 * unprotect some segments if we need it. 198 */ 199 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 200 for (llist = object->load_list; llist != NULL; llist = llist->next) { 201 if (!(llist->prot & PROT_WRITE)) 202 _dl_mprotect(llist->start, llist->size, 203 PROT_READ | PROT_WRITE); 204 } 205 } 206 207 /* tight loop for leading RELATIVE relocs */ 208 for (i = 0; i < relrel; i++, rels++) { 209 Elf_Addr *where; 210 211 #ifdef DEBUG 212 if (ELF_R_TYPE(rels->r_info) != R_TYPE(RELATIVE)) { 213 _dl_printf("RELACOUNT wrong\n"); 214 _dl_exit(20); 215 } 216 #endif 217 where = (Elf_Addr *)(rels->r_offset + loff); 218 *where = rels->r_addend + loff; 219 } 220 for (; i < numrel; i++, rels++) { 221 Elf_Addr *where, value, ooff, mask; 222 Elf_Word type; 223 const Elf_Sym *sym, *this; 224 const char *symn; 225 226 type = ELF_R_TYPE(rels->r_info); 227 228 if (RELOC_ERROR(type)) { 229 _dl_printf("relocation error %d idx %d\n", type, i); 230 _dl_exit(20); 231 } 232 233 if (type == R_TYPE(NONE)) 234 continue; 235 236 if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL) 237 continue; 238 239 where = (Elf_Addr *)(rels->r_offset + loff); 240 241 if (RELOC_USE_ADDEND(type)) 242 value = rels->r_addend; 243 else 244 value = 0; 245 246 sym = NULL; 247 symn = NULL; 248 if (RELOC_RESOLVE_SYMBOL(type)) { 249 sym = object->dyn.symtab; 250 sym += ELF_R_SYM(rels->r_info); 251 symn = object->dyn.strtab + sym->st_name; 252 253 if (sym->st_shndx != SHN_UNDEF && 254 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 255 value += loff; 256 } else if (sym == prev_sym) { 257 value += prev_value; 258 } else { 259 this = NULL; 260 ooff = _dl_find_symbol_bysym(object, 261 ELF_R_SYM(rels->r_info), &this, 262 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 263 ((type == R_TYPE(JUMP_SLOT))? 264 SYM_PLT:SYM_NOTPLT), 265 sym, NULL); 266 if (this == NULL) { 267 resolve_failed: 268 if (ELF_ST_BIND(sym->st_info) != 269 STB_WEAK) 270 fails++; 271 continue; 272 } 273 prev_sym = sym; 274 prev_value = (Elf_Addr)(ooff + this->st_value); 275 value += prev_value; 276 } 277 } 278 279 if (type == R_TYPE(JUMP_SLOT)) { 280 _dl_reloc_plt(where, value); 281 continue; 282 } 283 284 if (type == R_TYPE(COPY)) { 285 void *dstaddr = where; 286 const void *srcaddr; 287 const Elf_Sym *dstsym = sym, *srcsym = NULL; 288 Elf_Addr soff; 289 290 soff = _dl_find_symbol(symn, &srcsym, 291 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT, 292 dstsym, object, NULL); 293 if (srcsym == NULL) 294 goto resolve_failed; 295 296 srcaddr = (void *)(soff + srcsym->st_value); 297 _dl_bcopy(srcaddr, dstaddr, dstsym->st_size); 298 continue; 299 } 300 301 if (RELOC_PC_RELATIVE(type)) 302 value -= (Elf_Addr)where; 303 if (RELOC_BASE_RELATIVE(type)) 304 value += loff; 305 306 mask = RELOC_VALUE_BITMASK(type); 307 value >>= RELOC_VALUE_RIGHTSHIFT(type); 308 value &= mask; 309 310 if (RELOC_TARGET_SIZE(type) > 32) { 311 *where &= ~mask; 312 *where |= value; 313 } else { 314 Elf32_Addr *where32 = (Elf32_Addr *)where; 315 316 *where32 &= ~mask; 317 *where32 |= value; 318 } 319 } 320 321 /* reprotect the unprotected segments */ 322 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 323 for (llist = object->load_list; llist != NULL; llist = llist->next) { 324 if (!(llist->prot & PROT_WRITE)) 325 _dl_mprotect(llist->start, llist->size, 326 llist->prot); 327 } 328 } 329 330 return (fails); 331 } 332 333 void 334 _dl_reloc_plt(Elf_Addr *where, Elf_Addr value) 335 { 336 *where = value; 337 } 338 339 /* 340 * Resolve a symbol at run-time. 341 */ 342 Elf_Addr 343 _dl_bind(elf_object_t *object, int index) 344 { 345 Elf_RelA *rel; 346 const Elf_Sym *sym, *this; 347 const char *symn; 348 const elf_object_t *sobj; 349 Elf_Addr ooff; 350 int64_t cookie = pcookie; 351 struct { 352 struct __kbind param; 353 Elf_Addr newval; 354 } buf; 355 356 rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]) + index; 357 358 sym = object->dyn.symtab; 359 sym += ELF_R_SYM(rel->r_info); 360 symn = object->dyn.strtab + sym->st_name; 361 362 this = NULL; 363 ooff = _dl_find_symbol(symn, &this, 364 SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, object, &sobj); 365 if (this == NULL) { 366 _dl_printf("lazy binding failed!\n"); 367 *(volatile int *)0 = 0; /* XXX */ 368 } 369 370 buf.newval = ooff + this->st_value + rel->r_addend; 371 372 if (__predict_false(sobj->traced) && _dl_trace_plt(sobj, symn)) 373 return (buf.newval); 374 375 buf.param.kb_addr = (Elf_Word *)(object->obj_base + rel->r_offset); 376 buf.param.kb_size = sizeof(Elf_Addr); 377 378 /* directly code the syscall, so that it's actually inline here */ 379 { 380 register long syscall_num __asm("rax") = SYS_kbind; 381 register void *arg1 __asm("rdi") = &buf; 382 register long arg2 __asm("rsi") = sizeof(buf); 383 register long arg3 __asm("rdx") = cookie; 384 385 __asm volatile("syscall" : "+r" (syscall_num), "+r" (arg3) : 386 "r" (arg1), "r" (arg2) : "cc", "rcx", "r11", "memory"); 387 } 388 return (buf.newval); 389 } 390 391 int 392 _dl_md_reloc_got(elf_object_t *object, int lazy) 393 { 394 extern void _dl_bind_start(void); /* XXX */ 395 int fails = 0; 396 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT]; 397 int i, num; 398 Elf_RelA *rel; 399 400 if (pltgot == NULL) 401 return (0); /* it is possible to have no PLT/GOT relocations */ 402 403 if (object->Dyn.info[DT_PLTREL] != DT_RELA) 404 return (0); 405 406 if (object->traced) 407 lazy = 1; 408 409 if (__predict_false(!lazy)) { 410 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 411 } else { 412 pltgot[1] = (Elf_Addr)object; 413 pltgot[2] = (Elf_Addr)&_dl_bind_start; 414 415 rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]); 416 num = (object->Dyn.info[DT_PLTRELSZ]); 417 for (i = 0; i < num/sizeof(Elf_RelA); i++, rel++) { 418 Elf_Addr *where; 419 where = (Elf_Addr *)(rel->r_offset + object->obj_base); 420 *where += object->obj_base; 421 } 422 } 423 424 /* mprotect the GOT */ 425 _dl_protect_segment(object, 0, "__got_start", "__got_end", PROT_READ); 426 427 return (fails); 428 } 429