1 /* $OpenBSD: rtld_machine.c,v 1.42 2019/10/05 00:08:50 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 #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 (not currently used) 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_U 0x04000000 /* Unaligned */ 102 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 103 #define _RF_RS(s) ((s) & 0xff) /* right shift */ 104 static const int reloc_target_flags[] = { 105 0, /* NONE */ 106 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32*/ 107 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC32 */ 108 _RF_G| _RF_SZ(32) | _RF_RS(00), /* GOT32 */ 109 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 110 _RF_S| _RF_SZ(32) | _RF_RS(0), /* COPY */ 111 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */ 112 _RF_S| _RF_SZ(32) | _RF_RS(0), /* JUMP_SLOT */ 113 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */ 114 0, /* GOTOFF XXX */ 115 0, /* GOTPC XXX */ 116 0, /* DUMMY 11 */ 117 0, /* DUMMY 12 */ 118 0, /* DUMMY 13 */ 119 0, /* DUMMY 14 */ 120 0, /* DUMMY 15 */ 121 0, /* DUMMY 16 */ 122 0, /* DUMMY 17 */ 123 0, /* DUMMY 18 */ 124 0, /* DUMMY 19 */ 125 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 126 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* PC_16 */ 127 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 128 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* RELOC_PC8 */ 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_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 135 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 136 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 137 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 138 139 static const long reloc_target_bitmask[] = { 140 #define _BM(x) (~(-(1ULL << (x)))) 141 0, /* NONE */ 142 _BM(32), /* RELOC_32*/ 143 _BM(32), /* PC32 */ 144 _BM(32), /* GOT32 */ 145 _BM(32), /* PLT32 */ 146 0, /* COPY */ 147 _BM(32), /* GLOB_DAT */ 148 _BM(32), /* JUMP_SLOT */ 149 _BM(32), /* RELATIVE */ 150 0, /* GOTOFF XXX */ 151 0, /* GOTPC XXX */ 152 0, /* DUMMY 11 */ 153 0, /* DUMMY 12 */ 154 0, /* DUMMY 13 */ 155 0, /* DUMMY 14 */ 156 0, /* DUMMY 15 */ 157 0, /* DUMMY 16 */ 158 0, /* DUMMY 17 */ 159 0, /* DUMMY 18 */ 160 0, /* DUMMY 19 */ 161 _BM(16), /* RELOC_16 */ 162 _BM(8), /* PC_16 */ 163 _BM(8), /* RELOC_8 */ 164 _BM(8), /* RELOC_PC8 */ 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_Rel *rels; 182 183 loff = object->obj_base; 184 numrel = object->Dyn.info[relsz] / sizeof(Elf32_Rel); 185 relrel = rel == DT_REL ? object->relcount : 0; 186 rels = (Elf32_Rel *)(object->Dyn.info[rel]); 187 if (rels == NULL) 188 return(0); 189 190 if (relrel > numrel) 191 _dl_die("relcount > numrel: %ld > %ld", relrel, numrel); 192 193 /* tight loop for leading RELATIVE relocs */ 194 for (i = 0; i < relrel; i++, rels++) { 195 Elf_Addr *where; 196 197 where = (Elf_Addr *)(rels->r_offset + loff); 198 *where += loff; 199 } 200 for (; i < numrel; i++, rels++) { 201 Elf_Addr *where, value, mask; 202 Elf_Word type; 203 const Elf_Sym *sym; 204 const char *symn; 205 206 type = ELF_R_TYPE(rels->r_info); 207 208 if (type == R_TYPE(NONE)) 209 continue; 210 211 if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL) 212 continue; 213 214 where = (Elf_Addr *)(rels->r_offset + loff); 215 216 if (RELOC_USE_ADDEND(type)) 217 value = *where & RELOC_VALUE_BITMASK(type); 218 else 219 value = 0; 220 221 sym = NULL; 222 symn = NULL; 223 if (RELOC_RESOLVE_SYMBOL(type)) { 224 sym = object->dyn.symtab; 225 sym += ELF_R_SYM(rels->r_info); 226 symn = object->dyn.strtab + sym->st_name; 227 228 if (sym->st_shndx != SHN_UNDEF && 229 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 230 value += loff; 231 } else if (sym == prev_sym) { 232 value += prev_value; 233 } else { 234 struct sym_res sr; 235 236 sr = _dl_find_symbol(symn, 237 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 238 ((type == R_TYPE(JUMP_SLOT))? 239 SYM_PLT:SYM_NOTPLT), sym, object); 240 if (sr.sym == NULL) { 241 resolve_failed: 242 if (ELF_ST_BIND(sym->st_info) != 243 STB_WEAK) 244 fails++; 245 continue; 246 } 247 prev_sym = sym; 248 prev_value = (Elf_Addr)(sr.obj->obj_base + 249 sr.sym->st_value); 250 value += prev_value; 251 } 252 } 253 254 if (type == R_TYPE(JUMP_SLOT)) { 255 _dl_reloc_plt((Elf_Word *)where, value); 256 continue; 257 } 258 259 if (type == R_TYPE(COPY)) { 260 void *dstaddr = where; 261 const void *srcaddr; 262 const Elf_Sym *dstsym = sym; 263 struct sym_res sr; 264 265 sr = _dl_find_symbol(symn, 266 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT, 267 dstsym, object); 268 if (sr.sym == NULL) 269 goto resolve_failed; 270 271 srcaddr = (void *)(sr.obj->obj_base + sr.sym->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 return (fails); 313 } 314 315 #if 0 316 struct jmpslot { 317 u_short opcode; 318 u_short addr[2]; 319 u_short reloc_index; 320 #define JMPSLOT_RELOC_MASK 0xffff 321 }; 322 #define JUMP 0xe990 /* NOP + JMP opcode */ 323 #endif 324 325 void 326 _dl_reloc_plt(Elf_Addr *where, Elf_Addr value) 327 { 328 *where = value; 329 } 330 331 /* 332 * Resolve a symbol at run-time. 333 */ 334 Elf_Addr 335 _dl_bind(elf_object_t *object, int index) 336 { 337 Elf_Rel *rel; 338 const Elf_Sym *sym; 339 const char *symn; 340 struct sym_res sr; 341 uint64_t cookie = pcookie; 342 struct { 343 struct __kbind param; 344 Elf_Addr newval; 345 } buf; 346 347 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 348 349 rel += index/sizeof(Elf_Rel); 350 351 sym = object->dyn.symtab; 352 sym += ELF_R_SYM(rel->r_info); 353 symn = object->dyn.strtab + sym->st_name; 354 355 sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, 356 sym, object); 357 if (sr.sym == NULL) 358 _dl_die("lazy binding failed!"); 359 360 buf.newval = sr.obj->obj_base + sr.sym->st_value; 361 362 if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn)) 363 return (buf.newval); 364 365 buf.param.kb_addr = (Elf_Word *)(object->obj_base + rel->r_offset); 366 buf.param.kb_size = sizeof(Elf_Addr); 367 368 /* directly code the syscall, so that it's actually inline here */ 369 { 370 register long syscall_num __asm("eax") = SYS_kbind; 371 372 __asm volatile("lea %3, %%edx; pushl 4(%%edx);" 373 " pushl (%%edx); pushl %2; pushl %1;" 374 " push %%eax; int $0x80; addl $20, %%esp" : 375 "+a" (syscall_num) : "r" (&buf), "i" (sizeof(buf)), 376 "m" (cookie) : "edx", "cc", "memory"); 377 } 378 379 return (buf.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_Rel *rel; 390 391 if (pltgot == NULL) 392 return (0); /* it is possible to have no PLT/GOT relocations */ 393 394 if (object->Dyn.info[DT_PLTREL] != DT_REL) 395 return (0); 396 397 if (object->traced) 398 lazy = 1; 399 400 if (!lazy) { 401 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 402 } else { 403 pltgot[1] = (Elf_Addr)object; 404 pltgot[2] = (Elf_Addr)&_dl_bind_start; 405 406 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 407 num = (object->Dyn.info[DT_PLTRELSZ]); 408 for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) { 409 Elf_Addr *where; 410 where = (Elf_Addr *)(rel->r_offset + object->obj_base); 411 *where += object->obj_base; 412 } 413 } 414 415 return (fails); 416 } 417