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/segments.h>
39 #include <machine/sysarch.h>
56 * Process the special R_386_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_Rel *rellim;
68 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
70 rellim = (const Elf_Rel *)((const char *)dstobj->rel + dstobj->relsize);
71 for (rel = dstobj->rel; rel < rellim; rel++) {
72 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
74 const Elf_Sym *dstsym;
78 const Elf_Sym *srcsym;
79 const Obj_Entry *srcobj, *defobj;
83 dstaddr = (void *)(dstobj->relocbase + rel->r_offset);
84 dstsym = dstobj->symtab + ELF_R_SYM(rel->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(rel->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_Rel *rellim;
134 const Obj_Entry *defobj;
135 Elf_Addr *where, symval, add;
140 * The dynamic loader may be called from a thread, we have
141 * limited amounts of stack available so we cannot use alloca().
143 if (obj != obj_rtld) {
144 cache = calloc(obj->dynsymcount, sizeof(SymCache));
145 /* No need to check for NULL here */
149 /* Appease some compilers. */
153 rellim = (const Elf_Rel *)((const char *)obj->rel + obj->relsize);
154 for (rel = obj->rel; rel < rellim; rel++) {
155 switch (ELF_R_TYPE(rel->r_info)) {
159 case R_386_TLS_TPOFF:
160 case R_386_TLS_TPOFF32:
161 case R_386_TLS_DTPMOD32:
162 case R_386_TLS_DTPOFF32:
163 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
164 flags, cache, lockstate);
167 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
168 switch (ELF_R_TYPE(rel->r_info)) {
172 if ((flags & SYMLOOK_IFUNC) == 0) {
173 obj->non_plt_gnu_ifunc = true;
176 symval = (Elf_Addr)rtld_resolve_ifunc(
179 case R_386_TLS_TPOFF:
180 case R_386_TLS_TPOFF32:
181 case R_386_TLS_DTPMOD32:
182 case R_386_TLS_DTPOFF32:
183 _rtld_error("%s: IFUNC for TLS reloc",
188 if ((flags & SYMLOOK_IFUNC) != 0)
190 symval = (Elf_Addr)defobj->relocbase +
195 if ((flags & SYMLOOK_IFUNC) != 0)
199 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
201 switch (ELF_R_TYPE(rel->r_info)) {
209 * I don't think the dynamic linker should ever
210 * see this type of relocation. But the
211 * binutils-2.6 tools sometimes generate it.
213 *where += symval - (Elf_Addr)where;
217 * These are deferred until all other
218 * relocations have been done. All we do here
219 * is make sure that the COPY relocation is
220 * not in a shared library. They are allowed
221 * only in executable files.
223 if (!obj->mainprog) {
224 _rtld_error("%s: Unexpected R_386_COPY "
225 "relocation in shared library", obj->path);
233 *where += (Elf_Addr)obj->relocbase;
235 case R_386_TLS_TPOFF:
236 case R_386_TLS_TPOFF32:
238 * We lazily allocate offsets for static TLS
239 * as we see the first relocation that
240 * references the TLS block. This allows us to
241 * support (small amounts of) static TLS in
242 * dynamically loaded modules. If we run out
243 * of space, we generate an error.
245 if (!defobj->tls_done) {
246 if (!allocate_tls_offset(
247 __DECONST(Obj_Entry *, defobj))) {
248 _rtld_error("%s: No space available "
249 "for static Thread Local Storage",
254 add = (Elf_Addr)(def->st_value - defobj->tlsoffset);
255 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
260 case R_386_TLS_DTPMOD32:
261 *where += (Elf_Addr)defobj->tlsindex;
263 case R_386_TLS_DTPOFF32:
264 *where += (Elf_Addr) def->st_value;
267 _rtld_error("%s: Unsupported relocation type %d"
268 " in non-PLT relocations\n", obj->path,
269 ELF_R_TYPE(rel->r_info));
279 /* Process the PLT relocations. */
281 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
283 const Elf_Rel *rellim;
286 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
287 for (rel = obj->pltrel; rel < rellim; rel++) {
288 Elf_Addr *where/*, val*/;
290 switch (ELF_R_TYPE(rel->r_info)) {
292 /* Relocate the GOT slot pointing into the PLT. */
293 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
294 *where += (Elf_Addr)obj->relocbase;
297 case R_386_IRELATIVE:
298 obj->irelative = true;
302 _rtld_error("Unknown relocation type %x in PLT",
303 ELF_R_TYPE(rel->r_info));
310 /* Relocate the jump slots in an object. */
312 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
314 const Elf_Rel *rellim;
317 if (obj->jmpslots_done)
319 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
320 for (rel = obj->pltrel; rel < rellim; rel++) {
321 Elf_Addr *where, target;
323 const Obj_Entry *defobj;
325 switch (ELF_R_TYPE(rel->r_info)) {
327 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
328 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
329 SYMLOOK_IN_PLT | flags, NULL, lockstate);
332 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
333 obj->gnu_ifunc = true;
336 target = (Elf_Addr)(defobj->relocbase + def->st_value);
337 reloc_jmpslot(where, target, defobj, obj, rel);
340 case R_386_IRELATIVE:
344 _rtld_error("Unknown relocation type %x in PLT",
345 ELF_R_TYPE(rel->r_info));
350 obj->jmpslots_done = true;
354 /* Fixup the jump slot at "where" to transfer control to "target". */
356 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
357 const Obj_Entry *obj __unused, const Obj_Entry *refobj __unused,
358 const Elf_Rel *rel __unused)
361 dbg("reloc_jmpslot: *%p = %p", where, (void *)target);
369 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
371 const Elf_Rel *rellim;
373 Elf_Addr *where, target;
377 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
378 for (rel = obj->pltrel; rel < rellim; rel++) {
379 switch (ELF_R_TYPE(rel->r_info)) {
380 case R_386_IRELATIVE:
381 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
382 lock_release(rtld_bind_lock, lockstate);
383 target = call_ifunc_resolver(obj->relocbase + *where);
384 wlock_acquire(rtld_bind_lock, lockstate);
389 obj->irelative = false;
394 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
396 const Elf_Rel *rellim;
401 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
402 for (rel = obj->pltrel; rel < rellim; rel++) {
403 Elf_Addr *where, target;
405 const Obj_Entry *defobj;
407 switch (ELF_R_TYPE(rel->r_info)) {
409 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
410 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
411 SYMLOOK_IN_PLT | flags, NULL, lockstate);
414 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
416 lock_release(rtld_bind_lock, lockstate);
417 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
418 wlock_acquire(rtld_bind_lock, lockstate);
419 reloc_jmpslot(where, target, defobj, obj, rel);
424 obj->gnu_ifunc = false;
428 uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2;
431 rtld_cpuid_count(int idx, int cnt, u_int *p)
439 : "=a" (p[0]), "=r" (p[1]), "=c" (p[2]), "=d" (p[3])
440 : "0" (idx), "2" (cnt));
444 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
446 u_int p[4], cpu_high;
452 " movl %%eax,%%ecx\n"
453 " xorl $0x200000,%%eax\n"
458 " xorl %%eax,%%ecx\n"
464 : "=r" (cpuid_supported) : : "eax", "ecx");
465 if (!cpuid_supported)
468 rtld_cpuid_count(1, 0, p);
471 rtld_cpuid_count(0, 0, p);
474 rtld_cpuid_count(7, 0, p);
475 cpu_stdext_feature = p[1];
476 cpu_stdext_feature2 = p[2];
487 allocate_initial_tls(Obj_Entry *objs)
492 * Fix the size of the static TLS block by using the maximum
493 * offset allocated so far and adding a bit for dynamic modules to
496 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
497 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
498 i386_set_gsbase(tls);
502 __attribute__((__regparm__(1)))
503 void *___tls_get_addr(tls_index *ti)
507 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
509 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
513 void *__tls_get_addr(tls_index *ti)
517 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
519 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);