2 * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 * Dynamic linker for ELF.
31 * John Polstra <jdp@polstra.com>.
34 #include <sys/param.h>
38 #include <machine/sysarch.h>
39 #include <machine/tls.h>
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
59 * Returns 0 on success, -1 on failure.
62 do_copy_relocations(Obj_Entry *dstobj)
64 const Elf_Rela *relalim;
67 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
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) {
73 const Elf_Sym *dstsym;
77 const Elf_Sym *srcsym;
78 const Obj_Entry *srcobj, *defobj;
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 size = dstsym->st_size;
86 symlook_init(&req, name);
87 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
88 req.flags = SYMLOOK_EARLY;
90 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
91 res = symlook_obj(&req, srcobj);
94 defobj = req.defobj_out;
100 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
101 " relocation in %s", name, dstobj->path);
105 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
106 memcpy(dstaddr, srcaddr, size);
113 /* Initialize the special GOT entries. */
115 init_pltgot(Obj_Entry *obj)
117 if (obj->pltgot != NULL) {
118 obj->pltgot[1] = (Elf_Addr) obj;
119 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
123 /* Process the non-PLT relocations. */
125 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
126 RtldLockState *lockstate)
128 const Elf_Rela *relalim;
129 const Elf_Rela *rela;
134 * The dynamic loader may be called from a thread, we have
135 * limited amounts of stack available so we cannot use alloca().
137 if (obj != obj_rtld) {
138 cache = calloc(obj->dynsymcount, sizeof(SymCache));
139 /* No need to check for NULL here */
143 relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
144 for (rela = obj->rela; rela < relalim; rela++) {
145 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
146 Elf32_Addr *where32 = (Elf32_Addr *)where;
148 switch (ELF_R_TYPE(rela->r_info)) {
156 const Obj_Entry *defobj;
158 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
159 flags, cache, lockstate);
163 *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend);
169 * I don't think the dynamic linker should ever see this
170 * type of relocation. But the binutils-2.6 tools sometimes
175 const Obj_Entry *defobj;
177 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
178 flags, cache, lockstate);
182 *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
183 def->st_value + rela->r_addend - (Elf_Addr) where);
186 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
190 * These are deferred until all other relocations have
191 * been done. All we do here is make sure that the COPY
192 * relocation is not in a shared library. They are allowed
193 * only in executable files.
195 if (!obj->mainprog) {
196 _rtld_error("%s: Unexpected R_X86_64_COPY relocation"
197 " in shared library", obj->path);
202 case R_X86_64_GLOB_DAT:
205 const Obj_Entry *defobj;
207 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
208 flags, cache, lockstate);
212 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
216 case R_X86_64_TPOFF64:
219 const Obj_Entry *defobj;
221 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
222 flags, cache, lockstate);
227 * We lazily allocate offsets for static TLS as we
228 * see the first relocation that references the
229 * TLS block. This allows us to support (small
230 * amounts of) static TLS in dynamically loaded
231 * modules. If we run out of space, we generate an
234 if (!defobj->tls_done) {
235 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
236 _rtld_error("%s: No space available for static "
237 "Thread Local Storage", obj->path);
242 *where = (Elf_Addr) (def->st_value - defobj->tlsoffset +
247 case R_X86_64_TPOFF32:
250 const Obj_Entry *defobj;
252 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
253 flags, cache, lockstate);
258 * We lazily allocate offsets for static TLS as we
259 * see the first relocation that references the
260 * TLS block. This allows us to support (small
261 * amounts of) static TLS in dynamically loaded
262 * modules. If we run out of space, we generate an
265 if (!defobj->tls_done) {
266 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
267 _rtld_error("%s: No space available for static "
268 "Thread Local Storage", obj->path);
273 *where32 = (Elf32_Addr) (def->st_value -
279 case R_X86_64_DTPMOD64:
282 const Obj_Entry *defobj;
284 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
285 flags, cache, lockstate);
289 *where += (Elf_Addr) defobj->tlsindex;
293 case R_X86_64_DTPOFF64:
296 const Obj_Entry *defobj;
298 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
299 flags, cache, lockstate);
303 *where += (Elf_Addr) (def->st_value + rela->r_addend);
307 case R_X86_64_DTPOFF32:
310 const Obj_Entry *defobj;
312 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
313 flags, cache, lockstate);
317 *where32 += (Elf32_Addr) (def->st_value + rela->r_addend);
321 case R_X86_64_RELATIVE:
322 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
325 /* 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 */
328 _rtld_error("%s: Unsupported relocation type %u"
329 " in non-PLT relocations\n", obj->path,
330 (unsigned int)ELF_R_TYPE(rela->r_info));
341 /* Process the PLT relocations. */
343 reloc_plt(Obj_Entry *obj)
345 const Elf_Rela *relalim;
346 const Elf_Rela *rela;
348 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
349 for (rela = obj->pltrela; rela < relalim; rela++) {
352 switch(ELF_R_TYPE(rela->r_info)) {
353 case R_X86_64_JMP_SLOT:
354 /* Relocate the GOT slot pointing into the PLT. */
355 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
356 *where += (Elf_Addr)obj->relocbase;
359 case R_X86_64_IRELATIVE:
360 obj->irelative = true;
364 _rtld_error("Unknown relocation type %x in PLT",
365 (unsigned int)ELF_R_TYPE(rela->r_info));
372 /* Relocate the jump slots in an object. */
374 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
376 const Elf_Rela *relalim;
377 const Elf_Rela *rela;
379 if (obj->jmpslots_done)
381 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
382 for (rela = obj->pltrela; rela < relalim; rela++) {
383 Elf_Addr *where, target;
385 const Obj_Entry *defobj;
387 switch (ELF_R_TYPE(rela->r_info)) {
388 case R_X86_64_JMP_SLOT:
389 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
390 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
391 SYMLOOK_IN_PLT | flags, NULL, lockstate);
394 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
395 obj->gnu_ifunc = true;
398 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
399 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
402 case R_X86_64_IRELATIVE:
406 _rtld_error("Unknown relocation type %x in PLT",
407 (unsigned int)ELF_R_TYPE(rela->r_info));
411 obj->jmpslots_done = true;
416 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
418 const Elf_Rela *relalim;
419 const Elf_Rela *rela;
423 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
424 for (rela = obj->pltrela; rela < relalim; rela++) {
425 Elf_Addr *where, target, *ptr;
427 switch (ELF_R_TYPE(rela->r_info)) {
428 case R_X86_64_JMP_SLOT:
431 case R_X86_64_IRELATIVE:
432 ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
433 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
434 lock_release(rtld_bind_lock, lockstate);
435 target = ((Elf_Addr (*)(void))ptr)();
436 wlock_acquire(rtld_bind_lock, lockstate);
441 obj->irelative = false;
446 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
448 const Elf_Rela *relalim;
449 const Elf_Rela *rela;
453 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
454 for (rela = obj->pltrela; rela < relalim; rela++) {
455 Elf_Addr *where, target;
457 const Obj_Entry *defobj;
459 switch (ELF_R_TYPE(rela->r_info)) {
460 case R_X86_64_JMP_SLOT:
461 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
462 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
463 SYMLOOK_IN_PLT | flags, NULL, lockstate);
466 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
468 lock_release(rtld_bind_lock, lockstate);
469 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
470 wlock_acquire(rtld_bind_lock, lockstate);
471 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
475 obj->gnu_ifunc = false;
479 void *__tls_get_addr(tls_index *ti)
484 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
488 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
490 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);