2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 1996, 1997, 1998, 1999 John D. Polstra.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Dynamic linker for ELF.
33 * John Polstra <jdp@polstra.com>.
36 #include <sys/param.h>
38 #include <machine/cpufunc.h>
39 #include <machine/specialreg.h>
40 #include <machine/sysarch.h>
57 * Process the special R_X86_64_COPY relocations in the main program. These
58 * copy data from a shared object into a region in the main program's BSS
61 * Returns 0 on success, -1 on failure.
64 do_copy_relocations(Obj_Entry *dstobj)
66 const Elf_Rela *relalim;
69 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
71 relalim = (const Elf_Rela *)((const char *) dstobj->rela + dstobj->relasize);
72 for (rela = dstobj->rela; rela < relalim; rela++) {
73 if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
75 const Elf_Sym *dstsym;
79 const Elf_Sym *srcsym;
80 const Obj_Entry *srcobj, *defobj;
84 dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
85 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
86 name = dstobj->strtab + dstsym->st_name;
87 size = dstsym->st_size;
88 symlook_init(&req, name);
89 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
90 req.flags = SYMLOOK_EARLY;
92 for (srcobj = globallist_next(dstobj); srcobj != NULL;
93 srcobj = globallist_next(srcobj)) {
94 res = symlook_obj(&req, srcobj);
97 defobj = req.defobj_out;
102 if (srcobj == NULL) {
103 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
104 " relocation in %s", name, dstobj->path);
108 srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
109 memcpy(dstaddr, srcaddr, size);
116 /* Initialize the special GOT entries. */
118 init_pltgot(Obj_Entry *obj)
120 if (obj->pltgot != NULL) {
121 obj->pltgot[1] = (Elf_Addr) obj;
122 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
126 /* Process the non-PLT relocations. */
128 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
129 RtldLockState *lockstate)
131 const Elf_Rela *relalim;
132 const Elf_Rela *rela;
135 const Obj_Entry *defobj;
136 Elf_Addr *where, symval;
145 * The dynamic loader may be called from a thread, we have
146 * limited amounts of stack available so we cannot use alloca().
148 if (obj != obj_rtld) {
149 cache = calloc(obj->dynsymcount, sizeof(SymCache));
150 /* No need to check for NULL here */
154 relalim = (const Elf_Rela *)((const char*)obj->rela + obj->relasize);
155 for (rela = obj->rela; rela < relalim; rela++) {
157 * First, resolve symbol for relocations which
160 switch (ELF_R_TYPE(rela->r_info)) {
163 case R_X86_64_GLOB_DAT:
164 case R_X86_64_TPOFF64:
165 case R_X86_64_TPOFF32:
166 case R_X86_64_DTPMOD64:
167 case R_X86_64_DTPOFF64:
168 case R_X86_64_DTPOFF32:
169 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
170 &defobj, flags, cache, lockstate);
174 * If symbol is IFUNC, only perform relocation
175 * when caller allowed it by passing
176 * SYMLOOK_IFUNC flag. Skip the relocations
179 * Also error out in case IFUNC relocations
180 * are specified for TLS, which cannot be
181 * usefully interpreted.
183 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
184 switch (ELF_R_TYPE(rela->r_info)) {
187 case R_X86_64_GLOB_DAT:
188 if ((flags & SYMLOOK_IFUNC) == 0) {
189 obj->non_plt_gnu_ifunc = true;
192 symval = (Elf_Addr)rtld_resolve_ifunc(
195 case R_X86_64_TPOFF64:
196 case R_X86_64_TPOFF32:
197 case R_X86_64_DTPMOD64:
198 case R_X86_64_DTPOFF64:
199 case R_X86_64_DTPOFF32:
200 _rtld_error("%s: IFUNC for TLS reloc",
205 if ((flags & SYMLOOK_IFUNC) != 0)
207 symval = (Elf_Addr)defobj->relocbase +
212 if ((flags & SYMLOOK_IFUNC) != 0)
216 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
217 where32 = (Elf32_Addr *)where;
219 switch (ELF_R_TYPE(rela->r_info)) {
223 *where = symval + rela->r_addend;
227 * I don't think the dynamic linker should
228 * ever see this type of relocation. But the
229 * binutils-2.6 tools sometimes generate it.
231 *where32 = (Elf32_Addr)(unsigned long)(symval +
232 rela->r_addend - (Elf_Addr)where);
234 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
237 * These are deferred until all other relocations have
238 * been done. All we do here is make sure that the COPY
239 * relocation is not in a shared library. They are
240 * allowed only in executable files.
242 if (!obj->mainprog) {
243 _rtld_error("%s: Unexpected R_X86_64_COPY "
244 "relocation in shared library", obj->path);
248 case R_X86_64_GLOB_DAT:
251 case R_X86_64_TPOFF64:
253 * We lazily allocate offsets for static TLS
254 * as we see the first relocation that
255 * references the TLS block. This allows us to
256 * support (small amounts of) static TLS in
257 * dynamically loaded modules. If we run out
258 * of space, we generate an error.
260 if (!defobj->tls_done) {
261 if (!allocate_tls_offset(
262 __DECONST(Obj_Entry *, defobj))) {
263 _rtld_error("%s: No space available "
264 "for static Thread Local Storage",
269 *where = (Elf_Addr)(def->st_value - defobj->tlsoffset +
272 case R_X86_64_TPOFF32:
274 * We lazily allocate offsets for static TLS
275 * as we see the first relocation that
276 * references the TLS block. This allows us to
277 * support (small amounts of) static TLS in
278 * dynamically loaded modules. If we run out
279 * of space, we generate an error.
281 if (!defobj->tls_done) {
282 if (!allocate_tls_offset(
283 __DECONST(Obj_Entry *, defobj))) {
284 _rtld_error("%s: No space available "
285 "for static Thread Local Storage",
290 *where32 = (Elf32_Addr)(def->st_value -
291 defobj->tlsoffset + rela->r_addend);
293 case R_X86_64_DTPMOD64:
294 *where += (Elf_Addr)defobj->tlsindex;
296 case R_X86_64_DTPOFF64:
297 *where += (Elf_Addr)(def->st_value + rela->r_addend);
299 case R_X86_64_DTPOFF32:
300 *where32 += (Elf32_Addr)(def->st_value +
303 case R_X86_64_RELATIVE:
304 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
308 * R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16,
309 * R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8
312 _rtld_error("%s: Unsupported relocation type %u"
313 " in non-PLT relocations\n", obj->path,
314 (unsigned int)ELF_R_TYPE(rela->r_info));
324 /* Process the PLT relocations. */
326 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
328 const Elf_Rela *relalim;
329 const Elf_Rela *rela;
331 relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
332 for (rela = obj->pltrela; rela < relalim; rela++) {
335 switch(ELF_R_TYPE(rela->r_info)) {
336 case R_X86_64_JMP_SLOT:
337 /* Relocate the GOT slot pointing into the PLT. */
338 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
339 *where += (Elf_Addr)obj->relocbase;
342 case R_X86_64_IRELATIVE:
343 obj->irelative = true;
347 _rtld_error("Unknown relocation type %x in PLT",
348 (unsigned int)ELF_R_TYPE(rela->r_info));
355 /* Relocate the jump slots in an object. */
357 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
359 const Elf_Rela *relalim;
360 const Elf_Rela *rela;
362 if (obj->jmpslots_done)
364 relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
365 for (rela = obj->pltrela; rela < relalim; rela++) {
366 Elf_Addr *where, target;
368 const Obj_Entry *defobj;
370 switch (ELF_R_TYPE(rela->r_info)) {
371 case R_X86_64_JMP_SLOT:
372 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
373 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
374 SYMLOOK_IN_PLT | flags, NULL, lockstate);
377 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
378 obj->gnu_ifunc = true;
381 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
382 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
385 case R_X86_64_IRELATIVE:
389 _rtld_error("Unknown relocation type %x in PLT",
390 (unsigned int)ELF_R_TYPE(rela->r_info));
394 obj->jmpslots_done = true;
398 /* Fixup the jump slot at "where" to transfer control to "target". */
400 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
401 const struct Struct_Obj_Entry *obj __unused,
402 const struct Struct_Obj_Entry *refobj __unused,
403 const Elf_Rel *rel __unused)
406 dbg("reloc_jmpslot: *%p = %p", where, (void *)target);
414 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
416 const Elf_Rela *relalim;
417 const Elf_Rela *rela;
421 relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
422 for (rela = obj->pltrela; rela < relalim; rela++) {
423 Elf_Addr *where, target, *ptr;
425 switch (ELF_R_TYPE(rela->r_info)) {
426 case R_X86_64_JMP_SLOT:
429 case R_X86_64_IRELATIVE:
430 ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
431 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
432 lock_release(rtld_bind_lock, lockstate);
433 target = call_ifunc_resolver(ptr);
434 wlock_acquire(rtld_bind_lock, lockstate);
439 obj->irelative = false;
444 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
446 const Elf_Rela *relalim;
447 const Elf_Rela *rela;
451 relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
452 for (rela = obj->pltrela; rela < relalim; rela++) {
453 Elf_Addr *where, target;
455 const Obj_Entry *defobj;
457 switch (ELF_R_TYPE(rela->r_info)) {
458 case R_X86_64_JMP_SLOT:
459 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
460 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
461 SYMLOOK_IN_PLT | flags, NULL, lockstate);
464 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
466 lock_release(rtld_bind_lock, lockstate);
467 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
468 wlock_acquire(rtld_bind_lock, lockstate);
469 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
473 obj->gnu_ifunc = false;
477 uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2;
480 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
482 u_int p[4], cpu_high;
490 cpuid_count(7, 0, p);
491 cpu_stdext_feature = p[1];
492 cpu_stdext_feature2 = p[2];
502 int __getosreldate(void);
505 allocate_initial_tls(Obj_Entry *objs)
510 * Fix the size of the static TLS block by using the maximum
511 * offset allocated so far and adding a bit for dynamic
514 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
516 addr = allocate_tls(objs, 0, 3 * sizeof(Elf_Addr), sizeof(Elf_Addr));
517 if (__getosreldate() >= P_OSREL_WRFSBASE &&
518 (cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) != 0)
519 wrfsbase((uintptr_t)addr);
521 sysarch(AMD64_SET_FSBASE, &addr);
524 void *__tls_get_addr(tls_index *ti)
528 __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
530 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);