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/sysarch.h>
39 #include <machine/cpufunc.h>
56 * Process the special R_X86_64_COPY relocations in the main program. These
57 * copy data from a shared object into a region in the main program's BSS
60 * Returns 0 on success, -1 on failure.
63 do_copy_relocations(Obj_Entry *dstobj)
65 const Elf_Rela *relalim;
68 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
70 relalim = (const Elf_Rela *) ((caddr_t) dstobj->rela + dstobj->relasize);
71 for (rela = dstobj->rela; rela < relalim; rela++) {
72 if (ELF_R_TYPE(rela->r_info) == R_X86_64_COPY) {
74 const Elf_Sym *dstsym;
78 const Elf_Sym *srcsym;
79 const Obj_Entry *srcobj, *defobj;
83 dstaddr = (void *) (dstobj->relocbase + rela->r_offset);
84 dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
85 name = dstobj->strtab + dstsym->st_name;
86 size = dstsym->st_size;
87 symlook_init(&req, name);
88 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rela->r_info));
89 req.flags = SYMLOOK_EARLY;
91 for (srcobj = globallist_next(dstobj); srcobj != NULL;
92 srcobj = globallist_next(srcobj)) {
93 res = symlook_obj(&req, srcobj);
96 defobj = req.defobj_out;
101 if (srcobj == NULL) {
102 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
103 " relocation in %s", name, dstobj->path);
107 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
108 memcpy(dstaddr, srcaddr, size);
115 /* Initialize the special GOT entries. */
117 init_pltgot(Obj_Entry *obj)
119 if (obj->pltgot != NULL) {
120 obj->pltgot[1] = (Elf_Addr) obj;
121 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
125 /* Process the non-PLT relocations. */
127 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
128 RtldLockState *lockstate)
130 const Elf_Rela *relalim;
131 const Elf_Rela *rela;
134 const Obj_Entry *defobj;
135 Elf_Addr *where, symval;
141 * The dynamic loader may be called from a thread, we have
142 * limited amounts of stack available so we cannot use alloca().
144 if (obj != obj_rtld) {
145 cache = calloc(obj->dynsymcount, sizeof(SymCache));
146 /* No need to check for NULL here */
150 relalim = (const Elf_Rela *)((caddr_t)obj->rela + obj->relasize);
151 for (rela = obj->rela; rela < relalim; rela++) {
153 * First, resolve symbol for relocations which
156 switch (ELF_R_TYPE(rela->r_info)) {
159 case R_X86_64_GLOB_DAT:
160 case R_X86_64_TPOFF64:
161 case R_X86_64_TPOFF32:
162 case R_X86_64_DTPMOD64:
163 case R_X86_64_DTPOFF64:
164 case R_X86_64_DTPOFF32:
165 def = find_symdef(ELF_R_SYM(rela->r_info), obj,
166 &defobj, flags, cache, lockstate);
170 * If symbol is IFUNC, only perform relocation
171 * when caller allowed it by passing
172 * SYMLOOK_IFUNC flag. Skip the relocations
175 * Also error out in case IFUNC relocations
176 * are specified for TLS, which cannot be
177 * usefully interpreted.
179 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
180 switch (ELF_R_TYPE(rela->r_info)) {
183 case R_X86_64_GLOB_DAT:
184 if ((flags & SYMLOOK_IFUNC) == 0) {
185 obj->non_plt_gnu_ifunc = true;
188 symval = (Elf_Addr)rtld_resolve_ifunc(
191 case R_X86_64_TPOFF64:
192 case R_X86_64_TPOFF32:
193 case R_X86_64_DTPMOD64:
194 case R_X86_64_DTPOFF64:
195 case R_X86_64_DTPOFF32:
196 _rtld_error("%s: IFUNC for TLS reloc",
201 if ((flags & SYMLOOK_IFUNC) != 0)
203 symval = (Elf_Addr)defobj->relocbase +
208 if ((flags & SYMLOOK_IFUNC) != 0)
212 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
213 where32 = (Elf32_Addr *)where;
215 switch (ELF_R_TYPE(rela->r_info)) {
219 *where = symval + rela->r_addend;
223 * I don't think the dynamic linker should
224 * ever see this type of relocation. But the
225 * binutils-2.6 tools sometimes generate it.
227 *where32 = (Elf32_Addr)(unsigned long)(symval +
228 rela->r_addend - (Elf_Addr)where);
230 /* missing: R_X86_64_GOT32 R_X86_64_PLT32 */
233 * These are deferred until all other relocations have
234 * been done. All we do here is make sure that the COPY
235 * relocation is not in a shared library. They are
236 * allowed only in executable files.
238 if (!obj->mainprog) {
239 _rtld_error("%s: Unexpected R_X86_64_COPY "
240 "relocation in shared library", obj->path);
244 case R_X86_64_GLOB_DAT:
247 case R_X86_64_TPOFF64:
249 * We lazily allocate offsets for static TLS
250 * as we see the first relocation that
251 * references the TLS block. This allows us to
252 * support (small amounts of) static TLS in
253 * dynamically loaded modules. If we run out
254 * of space, we generate an error.
256 if (!defobj->tls_done) {
257 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
258 _rtld_error("%s: No space available "
259 "for static Thread Local Storage",
264 *where = (Elf_Addr)(def->st_value - defobj->tlsoffset +
267 case R_X86_64_TPOFF32:
269 * We lazily allocate offsets for static TLS
270 * as we see the first relocation that
271 * references the TLS block. This allows us to
272 * support (small amounts of) static TLS in
273 * dynamically loaded modules. If we run out
274 * of space, we generate an error.
276 if (!defobj->tls_done) {
277 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
278 _rtld_error("%s: No space available "
279 "for static Thread Local Storage",
284 *where32 = (Elf32_Addr)(def->st_value -
285 defobj->tlsoffset + rela->r_addend);
287 case R_X86_64_DTPMOD64:
288 *where += (Elf_Addr)defobj->tlsindex;
290 case R_X86_64_DTPOFF64:
291 *where += (Elf_Addr)(def->st_value + rela->r_addend);
293 case R_X86_64_DTPOFF32:
294 *where32 += (Elf32_Addr)(def->st_value +
297 case R_X86_64_RELATIVE:
298 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
302 * R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S, R_X86_64_16,
303 * R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8
306 _rtld_error("%s: Unsupported relocation type %u"
307 " in non-PLT relocations\n", obj->path,
308 (unsigned int)ELF_R_TYPE(rela->r_info));
318 /* Process the PLT relocations. */
320 reloc_plt(Obj_Entry *obj)
322 const Elf_Rela *relalim;
323 const Elf_Rela *rela;
325 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
326 for (rela = obj->pltrela; rela < relalim; rela++) {
329 switch(ELF_R_TYPE(rela->r_info)) {
330 case R_X86_64_JMP_SLOT:
331 /* Relocate the GOT slot pointing into the PLT. */
332 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
333 *where += (Elf_Addr)obj->relocbase;
336 case R_X86_64_IRELATIVE:
337 obj->irelative = true;
341 _rtld_error("Unknown relocation type %x in PLT",
342 (unsigned int)ELF_R_TYPE(rela->r_info));
349 /* Relocate the jump slots in an object. */
351 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
353 const Elf_Rela *relalim;
354 const Elf_Rela *rela;
356 if (obj->jmpslots_done)
358 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
359 for (rela = obj->pltrela; rela < relalim; rela++) {
360 Elf_Addr *where, target;
362 const Obj_Entry *defobj;
364 switch (ELF_R_TYPE(rela->r_info)) {
365 case R_X86_64_JMP_SLOT:
366 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
367 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
368 SYMLOOK_IN_PLT | flags, NULL, lockstate);
371 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
372 obj->gnu_ifunc = true;
375 target = (Elf_Addr)(defobj->relocbase + def->st_value + rela->r_addend);
376 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
379 case R_X86_64_IRELATIVE:
383 _rtld_error("Unknown relocation type %x in PLT",
384 (unsigned int)ELF_R_TYPE(rela->r_info));
388 obj->jmpslots_done = true;
392 /* Fixup the jump slot at "where" to transfer control to "target". */
394 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
395 const struct Struct_Obj_Entry *obj, const struct Struct_Obj_Entry *refobj,
399 dbg("reloc_jmpslot: *%p = %p", where, (void *)target);
407 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
409 const Elf_Rela *relalim;
410 const Elf_Rela *rela;
414 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
415 for (rela = obj->pltrela; rela < relalim; rela++) {
416 Elf_Addr *where, target, *ptr;
418 switch (ELF_R_TYPE(rela->r_info)) {
419 case R_X86_64_JMP_SLOT:
422 case R_X86_64_IRELATIVE:
423 ptr = (Elf_Addr *)(obj->relocbase + rela->r_addend);
424 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
425 lock_release(rtld_bind_lock, lockstate);
426 target = call_ifunc_resolver(ptr);
427 wlock_acquire(rtld_bind_lock, lockstate);
432 obj->irelative = false;
437 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
439 const Elf_Rela *relalim;
440 const Elf_Rela *rela;
444 relalim = (const Elf_Rela *)((char *)obj->pltrela + obj->pltrelasize);
445 for (rela = obj->pltrela; rela < relalim; rela++) {
446 Elf_Addr *where, target;
448 const Obj_Entry *defobj;
450 switch (ELF_R_TYPE(rela->r_info)) {
451 case R_X86_64_JMP_SLOT:
452 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
453 def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
454 SYMLOOK_IN_PLT | flags, NULL, lockstate);
457 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
459 lock_release(rtld_bind_lock, lockstate);
460 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
461 wlock_acquire(rtld_bind_lock, lockstate);
462 reloc_jmpslot(where, target, defobj, obj, (const Elf_Rel *)rela);
466 obj->gnu_ifunc = false;
470 uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2;
473 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
475 u_int p[4], cpu_high;
483 cpuid_count(7, 0, p);
484 cpu_stdext_feature = p[1];
485 cpu_stdext_feature2 = p[2];
496 allocate_initial_tls(Obj_Entry *objs)
499 * Fix the size of the static TLS block by using the maximum
500 * offset allocated so far and adding a bit for dynamic modules to
503 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
504 amd64_set_fsbase(allocate_tls(objs, 0,
505 3*sizeof(Elf_Addr), sizeof(Elf_Addr)));
508 void *__tls_get_addr(tls_index *ti)
512 __asm __volatile("movq %%fs:0, %0" : "=r" (segbase));
514 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);