rtld_machine.c revision 1.44
1/* $OpenBSD: rtld_machine.c,v 1.44 2019/10/24 22:11:10 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 80int64_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_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 102#define _RF_RS(s) ((s) & 0xff) /* right shift */ 103static const int reloc_target_flags[] = { 104 0, /* NONE */ 105 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32*/ 106 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC32 */ 107 _RF_G| _RF_SZ(32) | _RF_RS(00), /* GOT32 */ 108 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 109 _RF_S| _RF_SZ(32) | _RF_RS(0), /* COPY */ 110 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */ 111 _RF_S| _RF_SZ(32) | _RF_RS(0), /* JUMP_SLOT */ 112 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */ 113 0, /* GOTOFF XXX */ 114 0, /* GOTPC XXX */ 115 0, /* DUMMY 11 */ 116 0, /* DUMMY 12 */ 117 0, /* DUMMY 13 */ 118 0, /* DUMMY 14 */ 119 0, /* DUMMY 15 */ 120 0, /* DUMMY 16 */ 121 0, /* DUMMY 17 */ 122 0, /* DUMMY 18 */ 123 0, /* DUMMY 19 */ 124 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 125 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* PC_16 */ 126 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 127 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* RELOC_PC8 */ 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_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 134#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 135#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 136 137static const long reloc_target_bitmask[] = { 138#define _BM(x) (~(-(1ULL << (x)))) 139 0, /* NONE */ 140 _BM(32), /* RELOC_32*/ 141 _BM(32), /* PC32 */ 142 _BM(32), /* GOT32 */ 143 _BM(32), /* PLT32 */ 144 0, /* COPY */ 145 _BM(32), /* GLOB_DAT */ 146 _BM(32), /* JUMP_SLOT */ 147 _BM(32), /* RELATIVE */ 148 0, /* GOTOFF XXX */ 149 0, /* GOTPC XXX */ 150 0, /* DUMMY 11 */ 151 0, /* DUMMY 12 */ 152 0, /* DUMMY 13 */ 153 0, /* DUMMY 14 */ 154 0, /* DUMMY 15 */ 155 0, /* DUMMY 16 */ 156 0, /* DUMMY 17 */ 157 0, /* DUMMY 18 */ 158 0, /* DUMMY 19 */ 159 _BM(16), /* RELOC_16 */ 160 _BM(8), /* PC_16 */ 161 _BM(8), /* RELOC_8 */ 162 _BM(8), /* RELOC_PC8 */ 163#undef _BM 164}; 165#define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 166 167void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value); 168 169int 170_dl_md_reloc(elf_object_t *object, int rel, int relsz) 171{ 172 long i; 173 long numrel; 174 long relrel; 175 int fails = 0; 176 Elf_Addr loff; 177 Elf_Addr prev_value = 0; 178 const Elf_Sym *prev_sym = NULL; 179 Elf_Rel *rels; 180 181 loff = object->obj_base; 182 numrel = object->Dyn.info[relsz] / sizeof(Elf_Rel); 183 relrel = rel == DT_REL ? object->relcount : 0; 184 rels = (Elf_Rel *)(object->Dyn.info[rel]); 185 if (rels == NULL) 186 return 0; 187 188 if (relrel > numrel) 189 _dl_die("relcount > numrel: %ld > %ld", relrel, numrel); 190 191 /* tight loop for leading RELATIVE relocs */ 192 for (i = 0; i < relrel; i++, rels++) { 193 Elf_Addr *where; 194 195 where = (Elf_Addr *)(rels->r_offset + loff); 196 *where += loff; 197 } 198 for (; i < numrel; i++, rels++) { 199 Elf_Addr *where, value, mask; 200 Elf_Word type; 201 const Elf_Sym *sym; 202 const char *symn; 203 204 type = ELF_R_TYPE(rels->r_info); 205 206 if (type == R_TYPE(NONE)) 207 continue; 208 209 if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL) 210 continue; 211 212 where = (Elf_Addr *)(rels->r_offset + loff); 213 214 if (RELOC_USE_ADDEND(type)) 215 value = *where & RELOC_VALUE_BITMASK(type); 216 else 217 value = 0; 218 219 sym = NULL; 220 symn = NULL; 221 if (RELOC_RESOLVE_SYMBOL(type)) { 222 sym = object->dyn.symtab; 223 sym += ELF_R_SYM(rels->r_info); 224 symn = object->dyn.strtab + sym->st_name; 225 226 if (sym->st_shndx != SHN_UNDEF && 227 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 228 value += loff; 229 } else if (sym == prev_sym) { 230 value += prev_value; 231 } else { 232 struct sym_res sr; 233 234 sr = _dl_find_symbol(symn, 235 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 236 ((type == R_TYPE(JUMP_SLOT))? 237 SYM_PLT:SYM_NOTPLT), sym, object); 238 if (sr.sym == NULL) { 239resolve_failed: 240 if (ELF_ST_BIND(sym->st_info) != 241 STB_WEAK) 242 fails++; 243 continue; 244 } 245 prev_sym = sym; 246 prev_value = (Elf_Addr)(sr.obj->obj_base + 247 sr.sym->st_value); 248 value += prev_value; 249 } 250 } 251 252 if (type == R_TYPE(JUMP_SLOT)) { 253 _dl_reloc_plt((Elf_Word *)where, value); 254 continue; 255 } 256 257 if (type == R_TYPE(COPY)) { 258 void *dstaddr = where; 259 const void *srcaddr; 260 const Elf_Sym *dstsym = sym; 261 struct sym_res sr; 262 263 sr = _dl_find_symbol(symn, 264 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT, 265 dstsym, object); 266 if (sr.sym == NULL) 267 goto resolve_failed; 268 269 srcaddr = (void *)(sr.obj->obj_base + sr.sym->st_value); 270 _dl_bcopy(srcaddr, dstaddr, dstsym->st_size); 271 continue; 272 } 273 274 if (RELOC_PC_RELATIVE(type)) 275 value -= (Elf_Addr)where; 276 if (RELOC_BASE_RELATIVE(type)) 277 value += loff; 278 279 mask = RELOC_VALUE_BITMASK(type); 280 value >>= RELOC_VALUE_RIGHTSHIFT(type); 281 value &= mask; 282 283 *where &= ~mask; 284 *where |= value; 285 } 286 287 return fails; 288} 289 290#if 0 291struct jmpslot { 292 u_short opcode; 293 u_short addr[2]; 294 u_short reloc_index; 295#define JMPSLOT_RELOC_MASK 0xffff 296}; 297#define JUMP 0xe990 /* NOP + JMP opcode */ 298#endif 299 300void 301_dl_reloc_plt(Elf_Addr *where, Elf_Addr value) 302{ 303 *where = value; 304} 305 306/* 307 * Resolve a symbol at run-time. 308 */ 309Elf_Addr 310_dl_bind(elf_object_t *object, int index) 311{ 312 Elf_Rel *rel; 313 const Elf_Sym *sym; 314 const char *symn; 315 struct sym_res sr; 316 uint64_t cookie = pcookie; 317 struct { 318 struct __kbind param; 319 Elf_Addr newval; 320 } buf; 321 322 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 323 324 rel += index/sizeof(Elf_Rel); 325 326 sym = object->dyn.symtab; 327 sym += ELF_R_SYM(rel->r_info); 328 symn = object->dyn.strtab + sym->st_name; 329 330 sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, 331 sym, object); 332 if (sr.sym == NULL) 333 _dl_die("lazy binding failed!"); 334 335 buf.newval = sr.obj->obj_base + sr.sym->st_value; 336 337 if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn)) 338 return buf.newval; 339 340 buf.param.kb_addr = (Elf_Word *)(object->obj_base + rel->r_offset); 341 buf.param.kb_size = sizeof(Elf_Addr); 342 343 /* directly code the syscall, so that it's actually inline here */ 344 { 345 register long syscall_num __asm("eax") = SYS_kbind; 346 347 __asm volatile("lea %3, %%edx; pushl 4(%%edx);" 348 " pushl (%%edx); pushl %2; pushl %1;" 349 " push %%eax; int $0x80; addl $20, %%esp" : 350 "+a" (syscall_num) : "r" (&buf), "i" (sizeof(buf)), 351 "m" (cookie) : "edx", "cc", "memory"); 352 } 353 354 return buf.newval; 355} 356 357int 358_dl_md_reloc_got(elf_object_t *object, int lazy) 359{ 360 extern void _dl_bind_start(void); /* XXX */ 361 int fails = 0; 362 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT]; 363 int i, num; 364 Elf_Rel *rel; 365 366 if (pltgot == NULL) 367 return 0; /* it is possible to have no PLT/GOT relocations */ 368 369 if (object->Dyn.info[DT_PLTREL] != DT_REL) 370 return 0; 371 372 if (object->traced) 373 lazy = 1; 374 375 if (!lazy) { 376 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 377 } else { 378 pltgot[1] = (Elf_Addr)object; 379 pltgot[2] = (Elf_Addr)&_dl_bind_start; 380 381 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 382 num = (object->Dyn.info[DT_PLTRELSZ]); 383 for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) { 384 Elf_Addr *where; 385 where = (Elf_Addr *)(rel->r_offset + object->obj_base); 386 *where += object->obj_base; 387 } 388 } 389 390 return fails; 391} 392