1 /* $OpenBSD: rtld_machine.c,v 1.25 2017/01/24 07:48:37 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Dale Rahn 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #define _DYN_LOADER 30 31 #include <sys/types.h> 32 #include <sys/mman.h> 33 #include <sys/syscall.h> 34 #include <sys/unistd.h> 35 36 #include <nlist.h> 37 #include <link.h> 38 39 #include "syscall.h" 40 #include "archdep.h" 41 #include "resolve.h" 42 43 int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden; 44 45 void _dl_bind_start(void); /* XXX */ 46 Elf_Addr _dl_bind(elf_object_t *object, int reloff); 47 #define _RF_S 0x80000000 /* Resolve symbol */ 48 #define _RF_A 0x40000000 /* Use addend */ 49 #define _RF_P 0x20000000 /* Location relative */ 50 #define _RF_G 0x10000000 /* GOT offset */ 51 #define _RF_B 0x08000000 /* Load address relative */ 52 #define _RF_U 0x04000000 /* Unaligned */ 53 #define _RF_E 0x02000000 /* ERROR */ 54 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 55 #define _RF_RS(s) ((s) & 0xff) /* right shift */ 56 static int reloc_target_flags[] = { 57 0, /* 0 NONE */ 58 _RF_S|_RF_P|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 1 PC24 */ 59 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 2 ABS32 */ 60 _RF_S|_RF_P|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 3 REL32 */ 61 _RF_S|_RF_P|_RF_A| _RF_E, /* 4 REL13 */ 62 _RF_S|_RF_A| _RF_E, /* 5 ABS16 */ 63 _RF_S|_RF_A| _RF_E, /* 6 ABS12 */ 64 _RF_S|_RF_A| _RF_E, /* 7 T_ABS5 */ 65 _RF_S|_RF_A| _RF_E, /* 8 ABS8 */ 66 _RF_S|_RF_B|_RF_A| _RF_E, /* 9 SBREL32 */ 67 _RF_S|_RF_P|_RF_A| _RF_E, /* 10 T_PC22 */ 68 _RF_S|_RF_P|_RF_A| _RF_E, /* 11 T_PC8 */ 69 _RF_E, /* 12 Reserved */ 70 _RF_S|_RF_A| _RF_E, /* 13 SWI24 */ 71 _RF_S|_RF_A| _RF_E, /* 14 T_SWI8 */ 72 _RF_E, /* 15 OBSL */ 73 _RF_E, /* 16 OBSL */ 74 _RF_E, /* 17 UNUSED */ 75 _RF_E, /* 18 UNUSED */ 76 _RF_E, /* 19 UNUSED */ 77 _RF_S| _RF_SZ(32) | _RF_RS(0), /* 20 COPY */ 78 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 21 GLOB_DAT */ 79 _RF_S| _RF_SZ(32) | _RF_RS(0), /* 22 JUMP_SLOT */ 80 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* 23 RELATIVE */ 81 _RF_E, /* 24 GOTOFF */ 82 _RF_E, /* 25 GOTPC */ 83 _RF_E, /* 26 GOT32 */ 84 _RF_E, /* 27 PLT32 */ 85 _RF_E, /* 28 UNUSED */ 86 _RF_E, /* 29 UNUSED */ 87 _RF_E, /* 30 UNUSED */ 88 _RF_E, /* 31 UNUSED */ 89 _RF_E, /* 32 A_PCR 0 */ 90 _RF_E, /* 33 A_PCR 8 */ 91 _RF_E, /* 34 A_PCR 16 */ 92 _RF_E, /* 35 B_PCR 0 */ 93 _RF_E, /* 36 B_PCR 12 */ 94 _RF_E, /* 37 B_PCR 20 */ 95 _RF_E, /* 38 RELAB32 */ 96 _RF_E, /* 39 ROSGREL32 */ 97 _RF_E, /* 40 V4BX */ 98 _RF_E, /* 41 STKCHK */ 99 _RF_E /* 42 TSTKCHK */ 100 }; 101 102 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 103 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 104 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 105 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 106 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 107 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 108 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 109 static int reloc_target_bitmask[] = { 110 #define _BM(x) (x == 32? ~0 : ~(-(1UL << (x)))) 111 _BM(0), /* 0 NONE */ 112 _BM(24), /* 1 PC24 */ 113 _BM(32), /* 2 ABS32 */ 114 _BM(32), /* 3 REL32 */ 115 _BM(0), /* 4 REL13 */ 116 _BM(0), /* 5 ABS16 */ 117 _BM(0), /* 6 ABS12 */ 118 _BM(0), /* 7 T_ABS5 */ 119 _BM(0), /* 8 ABS8 */ 120 _BM(32), /* 9 SBREL32 */ 121 _BM(0), /* 10 T_PC22 */ 122 _BM(0), /* 11 T_PC8 */ 123 _BM(0), /* 12 Reserved */ 124 _BM(0), /* 13 SWI24 */ 125 _BM(0), /* 14 T_SWI8 */ 126 _BM(0), /* 15 OBSL */ 127 _BM(0), /* 16 OBSL */ 128 _BM(0), /* 17 UNUSED */ 129 _BM(0), /* 18 UNUSED */ 130 _BM(0), /* 19 UNUSED */ 131 _BM(32), /* 20 COPY */ 132 _BM(32), /* 21 GLOB_DAT */ 133 _BM(32), /* 22 JUMP_SLOT */ 134 _BM(32), /* 23 RELATIVE */ 135 _BM(0), /* 24 GOTOFF */ 136 _BM(0), /* 25 GOTPC */ 137 _BM(0), /* 26 GOT32 */ 138 _BM(0), /* 27 PLT32 */ 139 _BM(0), /* 28 UNUSED */ 140 _BM(0), /* 29 UNUSED */ 141 _BM(0), /* 30 UNUSED */ 142 _BM(0), /* 31 UNUSED */ 143 _BM(0), /* 32 A_PCR 0 */ 144 _BM(0), /* 33 A_PCR 8 */ 145 _BM(0), /* 34 A_PCR 16 */ 146 _BM(0), /* 35 B_PCR 0 */ 147 _BM(0), /* 36 B_PCR 12 */ 148 _BM(0), /* 37 B_PCR 20 */ 149 _BM(0), /* 38 RELAB32 */ 150 _BM(0), /* 39 ROSGREL32 */ 151 _BM(0), /* 40 V4BX */ 152 _BM(0), /* 41 STKCHK */ 153 _BM(0) /* 42 TSTKCHK */ 154 #undef _BM 155 }; 156 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 157 158 #define R_TYPE(x) R_ARM_ ## x 159 160 void _dl_reloc_plt(Elf_Word *where, Elf_Addr value, Elf_Rel *rel); 161 162 int 163 _dl_md_reloc(elf_object_t *object, int rel, int relsz) 164 { 165 long i; 166 long numrel; 167 long relrel; 168 int fails = 0; 169 Elf_Addr loff; 170 Elf_Addr prev_value = 0; 171 const Elf_Sym *prev_sym = NULL; 172 Elf_Rel *rels; 173 struct load_list *llist; 174 175 loff = object->obj_base; 176 numrel = object->Dyn.info[relsz] / sizeof(Elf_Rel); 177 relrel = rel == DT_REL ? object->relcount : 0; 178 rels = (Elf_Rel *)(object->Dyn.info[rel]); 179 180 if (rels == NULL) 181 return(0); 182 183 if (relrel > numrel) 184 _dl_die("relcount > numrel: %ld > %ld", relrel, numrel); 185 186 /* 187 * unprotect some segments if we need it. 188 */ 189 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 190 for (llist = object->load_list; 191 llist != NULL; 192 llist = llist->next) { 193 if (!(llist->prot & PROT_WRITE)) 194 _dl_mprotect(llist->start, llist->size, 195 PROT_READ | PROT_WRITE); 196 } 197 } 198 199 /* tight loop for leading RELATIVE relocs */ 200 for (i = 0; i < relrel; i++, rels++) { 201 Elf_Addr *where; 202 203 #ifdef DEBUG 204 if (ELF_R_TYPE(rels->r_info) != R_TYPE(RELATIVE)) 205 _dl_die("RELCOUNT wrong"); 206 #endif 207 where = (Elf_Addr *)(rels->r_offset + loff); 208 *where += loff; 209 } 210 for (; i < numrel; i++, rels++) { 211 Elf_Addr *where, value, ooff, mask; 212 Elf_Word type; 213 const Elf_Sym *sym, *this; 214 const char *symn; 215 216 type = ELF_R_TYPE(rels->r_info); 217 218 if (reloc_target_flags[type] & _RF_E) 219 _dl_die("bad relocation %ld %d", i, type); 220 if (type == R_TYPE(NONE)) 221 continue; 222 223 if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL) 224 continue; 225 226 where = (Elf_Addr *)(rels->r_offset + loff); 227 228 if (RELOC_USE_ADDEND(type)) 229 #ifdef LDSO_ARCH_IS_RELA_ 230 value = rels->r_addend; 231 #else 232 value = *where & RELOC_VALUE_BITMASK(type); 233 #endif 234 else 235 value = 0; 236 237 sym = NULL; 238 symn = NULL; 239 if (RELOC_RESOLVE_SYMBOL(type)) { 240 sym = object->dyn.symtab; 241 sym += ELF_R_SYM(rels->r_info); 242 symn = object->dyn.strtab + sym->st_name; 243 244 if (sym->st_shndx != SHN_UNDEF && 245 ELF_ST_BIND(sym->st_info) == STB_LOCAL) { 246 value += loff; 247 } else if (sym == prev_sym) { 248 value += prev_value; 249 } else { 250 this = NULL; 251 ooff = _dl_find_symbol_bysym(object, 252 ELF_R_SYM(rels->r_info), &this, 253 SYM_SEARCH_ALL|SYM_WARNNOTFOUND| 254 ((type == R_TYPE(JUMP_SLOT)) ? 255 SYM_PLT : SYM_NOTPLT), 256 sym, NULL); 257 if (this == NULL) { 258 resolve_failed: 259 if (ELF_ST_BIND(sym->st_info) != 260 STB_WEAK) 261 fails++; 262 continue; 263 } 264 prev_sym = sym; 265 prev_value = (Elf_Addr)(ooff + this->st_value); 266 value += prev_value; 267 } 268 } 269 270 if (type == R_TYPE(JUMP_SLOT)) { 271 /* 272 _dl_reloc_plt((Elf_Word *)where, value, rels); 273 */ 274 *where = value; 275 continue; 276 } 277 278 if (type == R_TYPE(COPY)) { 279 void *dstaddr = where; 280 const void *srcaddr; 281 const Elf_Sym *dstsym = sym, *srcsym = NULL; 282 Elf_Addr soff; 283 284 soff = _dl_find_symbol(symn, &srcsym, 285 SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT, 286 dstsym, object, NULL); 287 if (srcsym == NULL) 288 goto resolve_failed; 289 290 srcaddr = (void *)(soff + srcsym->st_value); 291 _dl_bcopy(srcaddr, dstaddr, dstsym->st_size); 292 continue; 293 } 294 295 if (RELOC_PC_RELATIVE(type)) 296 value -= (Elf_Addr)where; 297 if (RELOC_BASE_RELATIVE(type)) 298 value += loff; 299 300 mask = RELOC_VALUE_BITMASK(type); 301 value >>= RELOC_VALUE_RIGHTSHIFT(type); 302 value &= mask; 303 304 if (RELOC_UNALIGNED(type)) { 305 /* Handle unaligned relocations. */ 306 Elf_Addr tmp = 0; 307 char *ptr = (char *)where; 308 int i, size = RELOC_TARGET_SIZE(type)/8; 309 310 /* Read it in one byte at a time. */ 311 for (i=0; i<size; i++) 312 tmp = (tmp << 8) | ptr[i]; 313 314 tmp &= ~mask; 315 tmp |= value; 316 317 /* Write it back out. */ 318 for (i=0; i<size; i++) 319 ptr[i] = ((tmp >> (8*i)) & 0xff); 320 } else { 321 *where &= ~mask; 322 *where |= value; 323 } 324 } 325 326 /* reprotect the unprotected segments */ 327 if ((object->dyn.textrel == 1) && (rel == DT_REL || rel == DT_RELA)) { 328 for (llist = object->load_list; 329 llist != NULL; 330 llist = llist->next) { 331 if (!(llist->prot & PROT_WRITE)) 332 _dl_mprotect(llist->start, llist->size, 333 llist->prot); 334 } 335 } 336 337 return (fails); 338 } 339 340 /* 341 * Relocate the Global Offset Table (GOT). 342 * This is done by calling _dl_md_reloc on DT_JMPREL for DL_BIND_NOW, 343 * otherwise the lazy binding plt initialization is performed. 344 */ 345 int 346 _dl_md_reloc_got(elf_object_t *object, int lazy) 347 { 348 int fails = 0; 349 Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT]; 350 int i, num; 351 Elf_Rel *rel; 352 353 if (object->Dyn.info[DT_PLTREL] != DT_REL) 354 return (0); 355 356 if (object->traced) 357 lazy = 1; 358 359 if (!lazy) { 360 fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ); 361 } else { 362 rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]); 363 num = (object->Dyn.info[DT_PLTRELSZ]); 364 365 for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) { 366 Elf_Addr *where; 367 where = (Elf_Addr *)(rel->r_offset + object->obj_base); 368 *where += object->obj_base; 369 } 370 371 pltgot[1] = (Elf_Addr)object; 372 pltgot[2] = (Elf_Addr)_dl_bind_start; 373 } 374 375 /* mprotect the GOT */ 376 _dl_protect_segment(object, 0, "__got_start", "__got_end", PROT_READ); 377 378 return (fails); 379 } 380 381 Elf_Addr 382 _dl_bind(elf_object_t *object, int relidx) 383 { 384 Elf_Rel *rel; 385 const Elf_Sym *sym, *this; 386 const char *symn; 387 const elf_object_t *sobj; 388 Elf_Addr ooff; 389 int64_t cookie = pcookie; 390 struct { 391 struct __kbind param; 392 Elf_Word newval; 393 } buf; 394 395 rel = ((Elf_Rel *)object->Dyn.info[DT_JMPREL]) + (relidx); 396 397 sym = object->dyn.symtab; 398 sym += ELF_R_SYM(rel->r_info); 399 symn = object->dyn.strtab + sym->st_name; 400 401 this = NULL; 402 ooff = _dl_find_symbol(symn, &this, 403 SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, sym, object, &sobj); 404 if (this == NULL) 405 _dl_die("lazy binding failed!"); 406 407 buf.newval = ooff + this->st_value; 408 409 if (__predict_false(sobj->traced) && _dl_trace_plt(sobj, symn)) 410 return (buf.newval); 411 412 buf.param.kb_addr = (Elf_Addr *)(object->obj_base + rel->r_offset); 413 buf.param.kb_size = sizeof(Elf_Word); 414 415 /* directly code the syscall, so that it's actually inline here */ 416 { 417 register long syscall_num __asm("r12") = SYS_kbind; 418 register void *arg1 __asm("r0") = &buf; 419 register long arg2 __asm("r1") = sizeof(buf); 420 register long arg3 __asm("r2") = 0xffffffff & cookie; 421 register long arg4 __asm("r3") = 0xffffffff & (cookie >> 32); 422 423 __asm volatile("swi 0" : "+r" (arg1), "+r" (arg2) 424 : "r" (syscall_num), "r" (arg3), "r" (arg4) 425 : "cc", "memory"); 426 } 427 428 return (buf.newval); 429 } 430