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;
129 const Obj_Entry *defobj;
130 Elf_Addr *where, symval;
136 * The dynamic loader may be called from a thread, we have
137 * limited amounts of stack available so we cannot use alloca().
139 if (obj != obj_rtld) {
140 cache = calloc(obj->dynsymcount, sizeof(SymCache));
141 /* No need to check for NULL here */
145 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
146 for (rela = obj->rela; rela < relalim; rela++) {
148 * First, resolve symbol for relocations which
151 switch (ELF_R_TYPE(rela->r_info)) {
154 case R_X86_64_GLOB_DAT:
155 case R_X86_64_TPOFF64:
156 case R_X86_64_TPOFF32:
157 case R_X86_64_DTPMOD64:
158 case R_X86_64_DTPOFF64:
159 case R_X86_64_DTPOFF32:
160 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
161 &defobj, flags, cache, lockstate);
165 * If symbol is IFUNC, only perform relocation
166 * when caller allowed it by passing
167 * SYMLOOK_IFUNC flag. Skip the relocations
170 * Also error out in case IFUNC relocations
171 * are specified for TLS, which cannot be
172 * usefully interpreted.
174 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
175 switch (ELF_R_TYPE(rela->r_info)) {
178 case R_X86_64_GLOB_DAT:
179 if ((flags & SYMLOOK_IFUNC) == 0) {
180 obj->non_plt_gnu_ifunc = true;
183 symval = (Elf_Addr)rtld_resolve_ifunc(
186 case R_X86_64_TPOFF64:
187 case R_X86_64_TPOFF32:
188 case R_X86_64_DTPMOD64:
189 case R_X86_64_DTPOFF64:
190 case R_X86_64_DTPOFF32:
191 _rtld_error("%s: IFUNC for TLS reloc",
196 if ((flags & SYMLOOK_IFUNC) != 0)
198 symval = (Elf_Addr)defobj->relocbase +
203 if ((flags & SYMLOOK_IFUNC) != 0)
207 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
208 where32 = (Elf32_Addr *)where;
210 switch (ELF_R_TYPE(rela->r_info)) {
214 *where = symval + rela->r_addend;
218 * I don't think the dynamic linker should
219 * ever see this type of relocation. But the
220 * binutils-2.6 tools sometimes generate it.
222 *where32 = (Elf32_Addr)(unsigned long)(symval +
223 rela->r_addend - (Elf_Addr)where);
225 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
228 * These are deferred until all other relocations have
229 * been done. All we do here is make sure that the COPY
230 * relocation is not in a shared library. They are allowed
231 * only in executable files.
233 if (!obj->mainprog) {
234 _rtld_error("%s: Unexpected R_X86_64_COPY "
235 "relocation in shared library", obj->path);
239 case R_X86_64_GLOB_DAT:
242 case R_X86_64_TPOFF64:
244 * We lazily allocate offsets for static TLS
245 * as we see the first relocation that
246 * references the TLS block. This allows us to
247 * support (small amounts of) static TLS in
248 * dynamically loaded modules. If we run out
249 * of space, we generate an error.
251 if (!defobj->tls_done) {
252 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
253 _rtld_error("%s: No space available "
254 "for static Thread Local Storage",
259 *where = (Elf_Addr)(def->st_value - defobj->tlsoffset +
262 case R_X86_64_TPOFF32:
264 * We lazily allocate offsets for static TLS
265 * as we see the first relocation that
266 * references the TLS block. This allows us to
267 * support (small amounts of) static TLS in
268 * dynamically loaded modules. If we run out
269 * of space, we generate an error.
271 if (!defobj->tls_done) {
272 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
273 _rtld_error("%s: No space available "
274 "for static Thread Local Storage",
279 *where32 = (Elf32_Addr)(def->st_value -
280 defobj->tlsoffset + rela->r_addend);
282 case R_X86_64_DTPMOD64:
283 *where += (Elf_Addr)defobj->tlsindex;
285 case R_X86_64_DTPOFF64:
286 *where += (Elf_Addr)(def->st_value + rela->r_addend);
288 case R_X86_64_DTPOFF32:
289 *where32 += (Elf32_Addr)(def->st_value +
292 case R_X86_64_RELATIVE:
293 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
297 * R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16,
298 * R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8
301 _rtld_error("%s: Unsupported relocation type %u"
302 " in non-PLT relocations\n", obj->path,
303 (unsigned int)ELF_R_TYPE(rela->r_info));
313 /* Process the PLT relocations. */
315 reloc_plt(Obj_Entry *obj)
317 const Elf_Rela *relalim;
318 const Elf_Rela *rela;
320 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
321 for (rela = obj->pltrela; rela < relalim; rela++) {
324 switch(ELF_R_TYPE(rela->r_info)) {
325 case R_X86_64_JMP_SLOT:
326 /* Relocate the GOT slot pointing into the PLT. */
327 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
328 *where += (Elf_Addr)obj->relocbase;
331 case R_X86_64_IRELATIVE:
332 obj->irelative = true;
336 _rtld_error("Unknown relocation type %x in PLT",
337 (unsigned int)ELF_R_TYPE(rela->r_info));
344 /* Relocate the jump slots in an object. */
346 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
348 const Elf_Rela *relalim;
349 const Elf_Rela *rela;
351 if (obj->jmpslots_done)
353 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
354 for (rela = obj->pltrela; rela < relalim; rela++) {
355 Elf_Addr *where, target;
357 const Obj_Entry *defobj;
359 switch (ELF_R_TYPE(rela->r_info)) {
360 case R_X86_64_JMP_SLOT:
361 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
362 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
363 SYMLOOK_IN_PLT | flags, NULL, lockstate);
366 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
367 obj->gnu_ifunc = true;
370 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
371 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
374 case R_X86_64_IRELATIVE:
378 _rtld_error("Unknown relocation type %x in PLT",
379 (unsigned int)ELF_R_TYPE(rela->r_info));
383 obj->jmpslots_done = true;
388 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
390 const Elf_Rela *relalim;
391 const Elf_Rela *rela;
395 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
396 for (rela = obj->pltrela; rela < relalim; rela++) {
397 Elf_Addr *where, target, *ptr;
399 switch (ELF_R_TYPE(rela->r_info)) {
400 case R_X86_64_JMP_SLOT:
403 case R_X86_64_IRELATIVE:
404 ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
405 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
406 lock_release(rtld_bind_lock, lockstate);
407 target = ((Elf_Addr (*)(void))ptr)();
408 wlock_acquire(rtld_bind_lock, lockstate);
413 obj->irelative = false;
418 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
420 const Elf_Rela *relalim;
421 const Elf_Rela *rela;
425 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
426 for (rela = obj->pltrela; rela < relalim; rela++) {
427 Elf_Addr *where, target;
429 const Obj_Entry *defobj;
431 switch (ELF_R_TYPE(rela->r_info)) {
432 case R_X86_64_JMP_SLOT:
433 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
434 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
435 SYMLOOK_IN_PLT | flags, NULL, lockstate);
438 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
440 lock_release(rtld_bind_lock, lockstate);
441 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
442 wlock_acquire(rtld_bind_lock, lockstate);
443 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
447 obj->gnu_ifunc = false;
452 allocate_initial_tls(Obj_Entry *objs)
455 * Fix the size of the static TLS block by using the maximum
456 * offset allocated so far and adding a bit for dynamic modules to
459 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
460 amd64_set_fsbase(allocate_tls(objs, 0,
461 3*sizeof(Elf_Addr), sizeof(Elf_Addr)));
464 void *__tls_get_addr(tls_index *ti)
468 __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
470 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);