1 /*- 2 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * This software was developed by the University of Cambridge Computer 10 * Laboratory as part of the CTSRD Project, with support from the UK Higher 11 * Education Innovation Fund (HEIF). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #include <sys/types.h> 37 38 #include <stdlib.h> 39 40 #include "debug.h" 41 #include "rtld.h" 42 #include "rtld_printf.h" 43 44 /* 45 * It is possible for the compiler to emit relocations for unaligned data. 46 * We handle this situation with these inlines. 47 */ 48 #define RELOC_ALIGNED_P(x) \ 49 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) 50 51 uint64_t 52 set_gp(Obj_Entry *obj) 53 { 54 uint64_t old; 55 SymLook req; 56 uint64_t gp; 57 int res; 58 59 __asm __volatile("mv %0, gp" : "=r"(old)); 60 61 symlook_init(&req, "__global_pointer$"); 62 req.ventry = NULL; 63 req.flags = SYMLOOK_EARLY; 64 res = symlook_obj(&req, obj); 65 66 if (res == 0) { 67 gp = req.sym_out->st_value; 68 __asm __volatile("mv gp, %0" :: "r"(gp)); 69 } 70 71 return (old); 72 } 73 74 void 75 init_pltgot(Obj_Entry *obj) 76 { 77 78 if (obj->pltgot != NULL) { 79 obj->pltgot[0] = (Elf_Addr)&_rtld_bind_start; 80 obj->pltgot[1] = (Elf_Addr)obj; 81 } 82 } 83 84 int 85 do_copy_relocations(Obj_Entry *dstobj) 86 { 87 const Obj_Entry *srcobj, *defobj; 88 const Elf_Rela *relalim; 89 const Elf_Rela *rela; 90 const Elf_Sym *srcsym; 91 const Elf_Sym *dstsym; 92 const void *srcaddr; 93 const char *name; 94 void *dstaddr; 95 SymLook req; 96 size_t size; 97 int res; 98 99 /* 100 * COPY relocs are invalid outside of the main program 101 */ 102 assert(dstobj->mainprog); 103 104 relalim = (const Elf_Rela *)((const char *)dstobj->rela + 105 dstobj->relasize); 106 for (rela = dstobj->rela; rela < relalim; rela++) { 107 if (ELF_R_TYPE(rela->r_info) != R_RISCV_COPY) 108 continue; 109 110 dstaddr = (void *)(dstobj->relocbase + rela->r_offset); 111 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info); 112 name = dstobj->strtab + dstsym->st_name; 113 size = dstsym->st_size; 114 115 symlook_init(&req, name); 116 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)); 117 req.flags = SYMLOOK_EARLY; 118 119 for (srcobj = globallist_next(dstobj); srcobj != NULL; 120 srcobj = globallist_next(srcobj)) { 121 res = symlook_obj(&req, srcobj); 122 if (res == 0) { 123 srcsym = req.sym_out; 124 defobj = req.defobj_out; 125 break; 126 } 127 } 128 if (srcobj == NULL) { 129 _rtld_error( 130 "Undefined symbol \"%s\" referenced from COPY relocation in %s", 131 name, dstobj->path); 132 return (-1); 133 } 134 135 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value); 136 memcpy(dstaddr, srcaddr, size); 137 } 138 139 return (0); 140 } 141 142 /* 143 * Process the PLT relocations. 144 */ 145 int 146 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused) 147 { 148 const Elf_Rela *relalim; 149 const Elf_Rela *rela; 150 151 relalim = (const Elf_Rela *)((const char *)obj->pltrela + 152 obj->pltrelasize); 153 for (rela = obj->pltrela; rela < relalim; rela++) { 154 Elf_Addr *where; 155 156 assert(ELF_R_TYPE(rela->r_info) == R_RISCV_JUMP_SLOT); 157 158 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 159 *where += (Elf_Addr)obj->relocbase; 160 } 161 162 return (0); 163 } 164 165 /* 166 * LD_BIND_NOW was set - force relocation for all jump slots 167 */ 168 int 169 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate) 170 { 171 const Obj_Entry *defobj; 172 const Elf_Rela *relalim; 173 const Elf_Rela *rela; 174 const Elf_Sym *def; 175 176 relalim = (const Elf_Rela *)((const char *)obj->pltrela + 177 obj->pltrelasize); 178 for (rela = obj->pltrela; rela < relalim; rela++) { 179 Elf_Addr *where; 180 181 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 182 switch(ELF_R_TYPE(rela->r_info)) { 183 case R_RISCV_JUMP_SLOT: 184 def = find_symdef(ELF_R_SYM(rela->r_info), obj, 185 &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate); 186 if (def == NULL) { 187 dbg("reloc_jmpslots: sym not found"); 188 return (-1); 189 } 190 191 *where = (Elf_Addr)(defobj->relocbase + def->st_value); 192 break; 193 default: 194 _rtld_error("Unknown relocation type %x in jmpslot", 195 (unsigned int)ELF_R_TYPE(rela->r_info)); 196 return (-1); 197 } 198 } 199 200 return (0); 201 } 202 203 int 204 reloc_iresolve(Obj_Entry *obj __unused, 205 struct Struct_RtldLockState *lockstate __unused) 206 { 207 208 /* XXX not implemented */ 209 return (0); 210 } 211 212 int 213 reloc_iresolve_nonplt(Obj_Entry *obj __unused, 214 struct Struct_RtldLockState *lockstate __unused) 215 { 216 217 /* XXX not implemented */ 218 return (0); 219 } 220 221 int 222 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused, 223 struct Struct_RtldLockState *lockstate __unused) 224 { 225 226 /* XXX not implemented */ 227 return (0); 228 } 229 230 Elf_Addr 231 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, 232 const Obj_Entry *defobj __unused, const Obj_Entry *obj __unused, 233 const Elf_Rel *rel) 234 { 235 236 assert(ELF_R_TYPE(rel->r_info) == R_RISCV_JUMP_SLOT); 237 238 if (*where != target && !ld_bind_not) 239 *where = target; 240 return (target); 241 } 242 243 /* 244 * Process non-PLT relocations 245 */ 246 int 247 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags, 248 RtldLockState *lockstate) 249 { 250 const Obj_Entry *defobj; 251 const Elf_Rela *relalim; 252 const Elf_Rela *rela; 253 const Elf_Sym *def; 254 SymCache *cache; 255 Elf_Addr *where; 256 unsigned long symnum; 257 258 if ((flags & SYMLOOK_IFUNC) != 0) 259 /* XXX not implemented */ 260 return (0); 261 262 /* 263 * The dynamic loader may be called from a thread, we have 264 * limited amounts of stack available so we cannot use alloca(). 265 */ 266 if (obj == obj_rtld) 267 cache = NULL; 268 else 269 cache = calloc(obj->dynsymcount, sizeof(SymCache)); 270 /* No need to check for NULL here */ 271 272 relalim = (const Elf_Rela *)((const char *)obj->rela + obj->relasize); 273 for (rela = obj->rela; rela < relalim; rela++) { 274 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 275 symnum = ELF_R_SYM(rela->r_info); 276 277 switch (ELF_R_TYPE(rela->r_info)) { 278 case R_RISCV_JUMP_SLOT: 279 /* This will be handled by the plt/jmpslot routines */ 280 break; 281 case R_RISCV_NONE: 282 break; 283 case R_RISCV_64: 284 def = find_symdef(symnum, obj, &defobj, flags, cache, 285 lockstate); 286 if (def == NULL) 287 return (-1); 288 289 *where = (Elf_Addr)(defobj->relocbase + def->st_value + 290 rela->r_addend); 291 break; 292 case R_RISCV_TLS_DTPMOD64: 293 def = find_symdef(symnum, obj, &defobj, flags, cache, 294 lockstate); 295 if (def == NULL) 296 return -1; 297 298 *where += (Elf_Addr)defobj->tlsindex; 299 break; 300 case R_RISCV_COPY: 301 /* 302 * These are deferred until all other relocations have 303 * been done. All we do here is make sure that the 304 * COPY relocation is not in a shared library. They 305 * are allowed only in executable files. 306 */ 307 if (!obj->mainprog) { 308 _rtld_error("%s: Unexpected R_RISCV_COPY " 309 "relocation in shared library", obj->path); 310 return (-1); 311 } 312 break; 313 case R_RISCV_TLS_DTPREL64: 314 def = find_symdef(symnum, obj, &defobj, flags, cache, 315 lockstate); 316 if (def == NULL) 317 return (-1); 318 /* 319 * We lazily allocate offsets for static TLS as we 320 * see the first relocation that references the 321 * TLS block. This allows us to support (small 322 * amounts of) static TLS in dynamically loaded 323 * modules. If we run out of space, we generate an 324 * error. 325 */ 326 if (!defobj->tls_static) { 327 if (!allocate_tls_offset( 328 __DECONST(Obj_Entry *, defobj))) { 329 _rtld_error( 330 "%s: No space available for static " 331 "Thread Local Storage", obj->path); 332 return (-1); 333 } 334 } 335 336 *where += (Elf_Addr)(def->st_value + rela->r_addend 337 - TLS_DTV_OFFSET); 338 break; 339 case R_RISCV_TLS_TPREL64: 340 def = find_symdef(symnum, obj, &defobj, flags, cache, 341 lockstate); 342 if (def == NULL) 343 return (-1); 344 345 /* 346 * We lazily allocate offsets for static TLS as we 347 * see the first relocation that references the 348 * TLS block. This allows us to support (small 349 * amounts of) static TLS in dynamically loaded 350 * modules. If we run out of space, we generate an 351 * error. 352 */ 353 if (!defobj->tls_static) { 354 if (!allocate_tls_offset( 355 __DECONST(Obj_Entry *, defobj))) { 356 _rtld_error( 357 "%s: No space available for static " 358 "Thread Local Storage", obj->path); 359 return (-1); 360 } 361 } 362 363 *where = (def->st_value + rela->r_addend + 364 defobj->tlsoffset - TLS_TP_OFFSET - TLS_TCB_SIZE); 365 break; 366 case R_RISCV_RELATIVE: 367 *where = (Elf_Addr)(obj->relocbase + rela->r_addend); 368 break; 369 default: 370 rtld_printf("%s: Unhandled relocation %lu\n", 371 obj->path, ELF_R_TYPE(rela->r_info)); 372 return (-1); 373 } 374 } 375 376 return (0); 377 } 378 379 void 380 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused) 381 { 382 383 } 384 385 void 386 allocate_initial_tls(Obj_Entry *objs) 387 { 388 389 /* 390 * Fix the size of the static TLS block by using the maximum 391 * offset allocated so far and adding a bit for dynamic modules to 392 * use. 393 */ 394 tls_static_space = tls_last_offset + tls_last_size + 395 RTLD_STATIC_TLS_EXTRA; 396 397 _tcb_set(allocate_tls(objs, NULL, TLS_TCB_SIZE, TLS_TCB_ALIGN)); 398 } 399 400 void * 401 __tls_get_addr(tls_index* ti) 402 { 403 uintptr_t **dtvp; 404 void *p; 405 406 dtvp = &_tcb_get()->tcb_dtv; 407 p = tls_get_addr_common(dtvp, ti->ti_module, ti->ti_offset); 408 409 return ((char*)p + TLS_DTV_OFFSET); 410 } 411