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>
36 #include <machine/sysarch.h>
52 * Process the special R_X86_64_COPY relocations in the main program. These
53 * copy data from a shared object into a region in the main program's BSS
56 * Returns 0 on success, -1 on failure.
59 do_copy_relocations(Obj_Entry *dstobj)
61 const Elf_Rela *relalim;
64 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
66 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize);
67 for (rela = dstobj->rela; rela < relalim; rela++) {
68 if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
70 const Elf_Sym *dstsym;
74 const Elf_Sym *srcsym;
75 const Obj_Entry *srcobj, *defobj;
79 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
80 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
81 name = dstobj->strtab + dstsym->st_name;
82 size = dstsym->st_size;
83 symlook_init(&req, name);
84 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
85 req.flags = SYMLOOK_EARLY;
87 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
88 res = symlook_obj(&req, srcobj);
91 defobj = req.defobj_out;
97 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
98 " relocation in %s", name, dstobj->path);
102 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
103 memcpy(dstaddr, srcaddr, size);
110 /* Initialize the special GOT entries. */
112 init_pltgot(Obj_Entry *obj)
114 if (obj->pltgot != NULL) {
115 obj->pltgot[1] = (Elf_Addr) obj;
116 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
120 /* Process the non-PLT relocations. */
122 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
123 RtldLockState *lockstate)
125 const Elf_Rela *relalim;
126 const Elf_Rela *rela;
131 * The dynamic loader may be called from a thread, we have
132 * limited amounts of stack available so we cannot use alloca().
134 if (obj != obj_rtld) {
135 cache = calloc(obj->dynsymcount, sizeof(SymCache));
136 /* No need to check for NULL here */
140 relalim = (const Elf_Rela *) ((caddr_t) obj->rela + obj->relasize);
141 for (rela = obj->rela; rela < relalim; rela++) {
142 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
143 Elf32_Addr *where32 = (Elf32_Addr *)where;
145 switch (ELF_R_TYPE(rela->r_info)) {
153 const Obj_Entry *defobj;
155 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
156 flags, cache, lockstate);
160 *where = (Elf_Addr) (defobj->relocbase + def->st_value + rela->r_addend);
166 * I don't think the dynamic linker should ever see this
167 * type of relocation. But the binutils-2.6 tools sometimes
172 const Obj_Entry *defobj;
174 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
175 flags, cache, lockstate);
179 *where32 = (Elf32_Addr) (unsigned long) (defobj->relocbase +
180 def->st_value + rela->r_addend - (Elf_Addr) where);
183 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
187 * These are deferred until all other relocations have
188 * been done. All we do here is make sure that the COPY
189 * relocation is not in a shared library. They are allowed
190 * only in executable files.
192 if (!obj->mainprog) {
193 _rtld_error("%s: Unexpected R_X86_64_COPY relocation"
194 " in shared library", obj->path);
199 case R_X86_64_GLOB_DAT:
202 const Obj_Entry *defobj;
204 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
205 flags, cache, lockstate);
209 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
213 case R_X86_64_TPOFF64:
216 const Obj_Entry *defobj;
218 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
219 flags, cache, lockstate);
224 * We lazily allocate offsets for static TLS as we
225 * see the first relocation that references the
226 * TLS block. This allows us to support (small
227 * amounts of) static TLS in dynamically loaded
228 * modules. If we run out of space, we generate an
231 if (!defobj->tls_done) {
232 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
233 _rtld_error("%s: No space available for static "
234 "Thread Local Storage", obj->path);
239 *where = (Elf_Addr) (def->st_value - defobj->tlsoffset +
244 case R_X86_64_TPOFF32:
247 const Obj_Entry *defobj;
249 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
250 flags, cache, lockstate);
255 * We lazily allocate offsets for static TLS as we
256 * see the first relocation that references the
257 * TLS block. This allows us to support (small
258 * amounts of) static TLS in dynamically loaded
259 * modules. If we run out of space, we generate an
262 if (!defobj->tls_done) {
263 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
264 _rtld_error("%s: No space available for static "
265 "Thread Local Storage", obj->path);
270 *where32 = (Elf32_Addr) (def->st_value -
276 case R_X86_64_DTPMOD64:
279 const Obj_Entry *defobj;
281 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
282 flags, cache, lockstate);
286 *where += (Elf_Addr) defobj->tlsindex;
290 case R_X86_64_DTPOFF64:
293 const Obj_Entry *defobj;
295 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
296 flags, cache, lockstate);
300 *where += (Elf_Addr) (def->st_value + rela->r_addend);
304 case R_X86_64_DTPOFF32:
307 const Obj_Entry *defobj;
309 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
310 flags, cache, lockstate);
314 *where32 += (Elf32_Addr) (def->st_value + rela->r_addend);
318 case R_X86_64_RELATIVE:
319 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
322 /* 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 */
325 _rtld_error("%s: Unsupported relocation type %u"
326 " in non-PLT relocations\n", obj->path,
327 (unsigned int)ELF_R_TYPE(rela->r_info));
338 /* Process the PLT relocations. */
340 reloc_plt(Obj_Entry *obj)
342 const Elf_Rela *relalim;
343 const Elf_Rela *rela;
345 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
346 for (rela = obj->pltrela; rela < relalim; rela++) {
349 switch(ELF_R_TYPE(rela->r_info)) {
350 case R_X86_64_JMP_SLOT:
351 /* Relocate the GOT slot pointing into the PLT. */
352 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
353 *where += (Elf_Addr)obj->relocbase;
356 case R_X86_64_IRELATIVE:
357 obj->irelative = true;
361 _rtld_error("Unknown relocation type %x in PLT",
362 (unsigned int)ELF_R_TYPE(rela->r_info));
369 /* Relocate the jump slots in an object. */
371 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
373 const Elf_Rela *relalim;
374 const Elf_Rela *rela;
376 if (obj->jmpslots_done)
378 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
379 for (rela = obj->pltrela; rela < relalim; rela++) {
380 Elf_Addr *where, target;
382 const Obj_Entry *defobj;
384 switch (ELF_R_TYPE(rela->r_info)) {
385 case R_X86_64_JMP_SLOT:
386 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
387 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
388 SYMLOOK_IN_PLT | flags, NULL, lockstate);
391 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
392 obj->gnu_ifunc = true;
395 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
396 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
399 case R_X86_64_IRELATIVE:
403 _rtld_error("Unknown relocation type %x in PLT",
404 (unsigned int)ELF_R_TYPE(rela->r_info));
408 obj->jmpslots_done = true;
413 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
415 const Elf_Rela *relalim;
416 const Elf_Rela *rela;
420 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
421 for (rela = obj->pltrela; rela < relalim; rela++) {
422 Elf_Addr *where, target, *ptr;
424 switch (ELF_R_TYPE(rela->r_info)) {
425 case R_X86_64_JMP_SLOT:
428 case R_X86_64_IRELATIVE:
429 ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
430 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
431 lock_release(rtld_bind_lock, lockstate);
432 target = ((Elf_Addr (*)(void))ptr)();
433 wlock_acquire(rtld_bind_lock, lockstate);
438 obj->irelative = false;
443 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
445 const Elf_Rela *relalim;
446 const Elf_Rela *rela;
450 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
451 for (rela = obj->pltrela; rela < relalim; rela++) {
452 Elf_Addr *where, target;
454 const Obj_Entry *defobj;
456 switch (ELF_R_TYPE(rela->r_info)) {
457 case R_X86_64_JMP_SLOT:
458 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
459 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
460 SYMLOOK_IN_PLT | flags, NULL, lockstate);
463 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
465 lock_release(rtld_bind_lock, lockstate);
466 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
467 wlock_acquire(rtld_bind_lock, lockstate);
468 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
472 obj->gnu_ifunc = false;
477 allocate_initial_tls(Obj_Entry *objs)
480 * Fix the size of the static TLS block by using the maximum
481 * offset allocated so far and adding a bit for dynamic modules to
484 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
485 amd64_set_fsbase(allocate_tls(objs, 0,
486 3*sizeof(Elf_Addr), sizeof(Elf_Addr)));
489 void *__tls_get_addr(tls_index *ti)
493 __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
495 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);