1 /*- 2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD: src/libexec/rtld-elf/amd64/reloc.c,v 1.20 2010/12/25 08:51:20 kib Exp $ 26 */ 27 28 /* 29 * Dynamic linker for ELF. 30 * 31 * John Polstra <jdp@polstra.com>. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/mman.h> 36 #include <sys/tls.h> 37 38 #include <machine/sysarch.h> 39 #include <machine/tls.h> 40 41 #include <dlfcn.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include "debug.h" 52 #include "rtld.h" 53 54 /* 55 * Process the special R_X86_64_COPY relocations in the main program. These 56 * copy data from a shared object into a region in the main program's BSS 57 * segment. 58 * 59 * Returns 0 on success, -1 on failure. 60 */ 61 int 62 do_copy_relocations(Obj_Entry *dstobj) 63 { 64 const Elf_Rela *relalim; 65 const Elf_Rela *rela; 66 67 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */ 68 69 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize); 70 for (rela = dstobj->rela; rela < relalim; rela++) { 71 if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) { 72 void *dstaddr; 73 const Elf_Sym *dstsym; 74 const char *name; 75 unsigned long hash; 76 size_t size; 77 const void *srcaddr; 78 const Elf_Sym *srcsym; 79 Obj_Entry *srcobj; 80 const Ver_Entry *ve; 81 82 dstaddr = (void *) (dstobj->relocbase + rela->r_offset); 83 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info); 84 name = dstobj->strtab + dstsym->st_name; 85 hash = elf_hash(name); 86 size = dstsym->st_size; 87 ve = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info)); 88 89 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 90 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) != NULL) 91 break; 92 93 if (srcobj == NULL) { 94 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 95 " relocation in %s", name, dstobj->path); 96 return -1; 97 } 98 99 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value); 100 memcpy(dstaddr, srcaddr, size); 101 } 102 } 103 104 return 0; 105 } 106 107 /* Initialize the special GOT entries. */ 108 void 109 init_pltgot(Obj_Entry *obj) 110 { 111 if (obj->pltgot != NULL) { 112 obj->pltgot[1] = (Elf_Addr) obj; 113 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 114 } 115 } 116 117 /* Process the non-PLT relocations. */ 118 int 119 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 120 { 121 const Elf_Rela *relalim; 122 const Elf_Rela *rela; 123 SymCache *cache; 124 int r = -1; 125 126 /* 127 * The dynamic loader may be called from a thread, we have 128 * limited amounts of stack available so we cannot use alloca(). 129 */ 130 if (obj != obj_rtld) { 131 cache = calloc(obj->nchains, sizeof(SymCache)); 132 /* No need to check for NULL here */ 133 } else 134 cache = NULL; 135 136 relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize); 137 for (rela = obj->rela; rela < relalim; rela++) { 138 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset); 139 Elf32_Addr *where32 = (Elf32_Addr *)where; 140 141 switch (ELF_R_TYPE(rela->r_info)) { 142 143 case R_X86_64_NONE: 144 break; 145 146 case R_X86_64_64: 147 { 148 const Elf_Sym *def; 149 const Obj_Entry *defobj; 150 151 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 152 false, cache); 153 if (def == NULL) 154 goto done; 155 156 *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend); 157 } 158 break; 159 160 case R_X86_64_PC32: 161 /* 162 * I don't think the dynamic linker should ever see this 163 * type of relocation. But the binutils-2.6 tools sometimes 164 * generate it. 165 */ 166 { 167 const Elf_Sym *def; 168 const Obj_Entry *defobj; 169 170 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 171 false, cache); 172 if (def == NULL) 173 goto done; 174 175 *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase + 176 def->st_value + rela->r_addend - (Elf_Addr) where); 177 } 178 break; 179 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */ 180 181 case R_X86_64_COPY: 182 /* 183 * These are deferred until all other relocations have 184 * been done. All we do here is make sure that the COPY 185 * relocation is not in a shared library. They are allowed 186 * only in executable files. 187 */ 188 if (!obj->mainprog) { 189 _rtld_error("%s: Unexpected R_X86_64_COPY relocation" 190 " in shared library", obj->path); 191 goto done; 192 } 193 break; 194 195 case R_X86_64_GLOB_DAT: 196 { 197 const Elf_Sym *def; 198 const Obj_Entry *defobj; 199 200 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 201 false, cache); 202 if (def == NULL) 203 goto done; 204 205 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 206 } 207 break; 208 209 case R_X86_64_TPOFF64: 210 { 211 const Elf_Sym *def; 212 const Obj_Entry *defobj; 213 214 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 215 false, cache); 216 if (def == NULL) 217 goto done; 218 219 /* 220 * We lazily allocate offsets for static TLS as we 221 * see the first relocation that references the 222 * TLS block. This allows us to support (small 223 * amounts of) static TLS in dynamically loaded 224 * modules. If we run out of space, we generate an 225 * error. 226 */ 227 if (!defobj->tls_done) { 228 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 229 _rtld_error("%s: No space available for static " 230 "Thread Local Storage", obj->path); 231 goto done; 232 } 233 } 234 235 *where = (Elf_Addr) (def->st_value - defobj->tlsoffset + 236 rela->r_addend); 237 } 238 break; 239 240 case R_X86_64_TPOFF32: 241 { 242 const Elf_Sym *def; 243 const Obj_Entry *defobj; 244 245 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 246 false, cache); 247 if (def == NULL) 248 goto done; 249 250 /* 251 * We lazily allocate offsets for static TLS as we 252 * see the first relocation that references the 253 * TLS block. This allows us to support (small 254 * amounts of) static TLS in dynamically loaded 255 * modules. If we run out of space, we generate an 256 * error. 257 */ 258 if (!defobj->tls_done) { 259 if (!allocate_tls_offset((Obj_Entry*) defobj)) { 260 _rtld_error("%s: No space available for static " 261 "Thread Local Storage", obj->path); 262 goto done; 263 } 264 } 265 266 *where32 = (Elf32_Addr) (def->st_value - 267 defobj->tlsoffset + 268 rela->r_addend); 269 } 270 break; 271 272 case R_X86_64_DTPMOD64: 273 { 274 const Elf_Sym *def; 275 const Obj_Entry *defobj; 276 277 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 278 false, cache); 279 if (def == NULL) 280 goto done; 281 282 *where += (Elf_Addr) defobj->tlsindex; 283 } 284 break; 285 286 case R_X86_64_DTPOFF64: 287 { 288 const Elf_Sym *def; 289 const Obj_Entry *defobj; 290 291 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 292 false, cache); 293 if (def == NULL) 294 goto done; 295 296 *where += (Elf_Addr) (def->st_value + rela->r_addend); 297 } 298 break; 299 300 case R_X86_64_DTPOFF32: 301 { 302 const Elf_Sym *def; 303 const Obj_Entry *defobj; 304 305 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, 306 false, cache); 307 if (def == NULL) 308 goto done; 309 310 *where32 += (Elf32_Addr) (def->st_value + rela->r_addend); 311 } 312 break; 313 314 case R_X86_64_RELATIVE: 315 *where = (Elf_Addr)(obj->relocbase + rela->r_addend); 316 break; 317 318 /* missing: R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16, R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8 */ 319 320 default: 321 _rtld_error("%s: Unsupported relocation type %u" 322 " in non-PLT relocations\n", obj->path, 323 (unsigned int)ELF_R_TYPE(rela->r_info)); 324 goto done; 325 } 326 } 327 r = 0; 328 done: 329 if (cache != NULL) 330 free(cache); 331 return(r); 332 } 333 334 /* Process the PLT relocations. */ 335 int 336 reloc_plt(Obj_Entry *obj) 337 { 338 const Elf_Rela *relalim; 339 const Elf_Rela *rela; 340 341 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize); 342 for (rela = obj->pltrela; rela < relalim; rela++) { 343 Elf_Addr *where; 344 345 assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT); 346 347 /* Relocate the GOT slot pointing into the PLT. */ 348 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 349 *where += (Elf_Addr)obj->relocbase; 350 } 351 return 0; 352 } 353 354 /* Relocate the jump slots in an object. */ 355 int 356 reloc_jmpslots(Obj_Entry *obj) 357 { 358 const Elf_Rela *relalim; 359 const Elf_Rela *rela; 360 361 if (obj->jmpslots_done) 362 return 0; 363 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize); 364 for (rela = obj->pltrela; rela < relalim; rela++) { 365 Elf_Addr *where, target; 366 const Elf_Sym *def; 367 const Obj_Entry *defobj; 368 369 assert(ELF_R_TYPE(rela->r_info) == R_X86_64_JMP_SLOT); 370 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 371 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true, NULL); 372 if (def == NULL) 373 return -1; 374 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend); 375 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela); 376 } 377 obj->jmpslots_done = true; 378 return 0; 379 } 380 381 void *__tls_get_addr(tls_index *ti) 382 { 383 struct tls_tcb *tcb; 384 385 tcb = tls_get_tcb(); 386 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset); 387 } 388 389 void * 390 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti) 391 { 392 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset); 393 } 394