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/segments.h>
37 #include <machine/sysarch.h>
53 * Process the special R_386_COPY relocations in the main program. These
54 * copy data from a shared object into a region in the main program's BSS
57 * Returns 0 on success, -1 on failure.
60 do_copy_relocations(Obj_Entry *dstobj)
62 const Elf_Rel *rellim;
65 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
67 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
68 for (rel = dstobj->rel; rel < rellim; rel++) {
69 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
71 const Elf_Sym *dstsym;
75 const Elf_Sym *srcsym;
76 const Obj_Entry *srcobj, *defobj;
80 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
81 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
82 name = dstobj->strtab + dstsym->st_name;
83 size = dstsym->st_size;
84 symlook_init(&req, name);
85 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
86 req.flags = SYMLOOK_EARLY;
88 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
89 res = symlook_obj(&req, srcobj);
92 defobj = req.defobj_out;
98 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
99 " relocation in %s", name, dstobj->path);
103 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
104 memcpy(dstaddr, srcaddr, size);
111 /* Initialize the special GOT entries. */
113 init_pltgot(Obj_Entry *obj)
115 if (obj->pltgot != NULL) {
116 obj->pltgot[1] = (Elf_Addr) obj;
117 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
121 /* Process the non-PLT relocations. */
123 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
124 RtldLockState *lockstate)
126 const Elf_Rel *rellim;
130 const Obj_Entry *defobj;
131 Elf_Addr *where, symval, add;
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 rellim = (const Elf_Rel *)((caddr_t) obj->rel + obj->relsize);
146 for (rel = obj->rel; rel < rellim; rel++) {
147 switch (ELF_R_TYPE(rel->r_info)) {
151 case R_386_TLS_TPOFF:
152 case R_386_TLS_TPOFF32:
153 case R_386_TLS_DTPMOD32:
154 case R_386_TLS_DTPOFF32:
155 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
156 flags, cache, lockstate);
159 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
160 switch (ELF_R_TYPE(rel->r_info)) {
164 if ((flags & SYMLOOK_IFUNC) == 0) {
165 obj->non_plt_gnu_ifunc = true;
168 symval = (Elf_Addr)rtld_resolve_ifunc(
171 case R_386_TLS_TPOFF:
172 case R_386_TLS_TPOFF32:
173 case R_386_TLS_DTPMOD32:
174 case R_386_TLS_DTPOFF32:
175 _rtld_error("%s: IFUNC for TLS reloc",
180 if ((flags & SYMLOOK_IFUNC) != 0)
182 symval = (Elf_Addr)defobj->relocbase +
187 if ((flags & SYMLOOK_IFUNC) != 0)
191 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
193 switch (ELF_R_TYPE(rel->r_info)) {
201 * I don't think the dynamic linker should ever
202 * see this type of relocation. But the
203 * binutils-2.6 tools sometimes generate it.
205 *where += symval - (Elf_Addr)where;
209 * These are deferred until all other
210 * relocations have been done. All we do here
211 * is make sure that the COPY relocation is
212 * not in a shared library. They are allowed
213 * only in executable files.
215 if (!obj->mainprog) {
216 _rtld_error("%s: Unexpected R_386_COPY "
217 "relocation in shared library", obj->path);
225 *where += (Elf_Addr)obj->relocbase;
227 case R_386_TLS_TPOFF:
228 case R_386_TLS_TPOFF32:
230 * We lazily allocate offsets for static TLS
231 * as we see the first relocation that
232 * references the TLS block. This allows us to
233 * support (small amounts of) static TLS in
234 * dynamically loaded modules. If we run out
235 * of space, we generate an error.
237 if (!defobj->tls_done) {
238 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
239 _rtld_error("%s: No space available "
240 "for static Thread Local Storage",
245 add = (Elf_Addr)(def->st_value - defobj->tlsoffset);
246 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
251 case R_386_TLS_DTPMOD32:
252 *where += (Elf_Addr)defobj->tlsindex;
254 case R_386_TLS_DTPOFF32:
255 *where += (Elf_Addr) def->st_value;
258 _rtld_error("%s: Unsupported relocation type %d"
259 " in non-PLT relocations\n", obj->path,
260 ELF_R_TYPE(rel->r_info));
270 /* Process the PLT relocations. */
272 reloc_plt(Obj_Entry *obj)
274 const Elf_Rel *rellim;
277 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
278 for (rel = obj->pltrel; rel < rellim; rel++) {
279 Elf_Addr *where/*, val*/;
281 switch (ELF_R_TYPE(rel->r_info)) {
283 /* Relocate the GOT slot pointing into the PLT. */
284 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
285 *where += (Elf_Addr)obj->relocbase;
288 case R_386_IRELATIVE:
289 obj->irelative = true;
293 _rtld_error("Unknown relocation type %x in PLT",
294 ELF_R_TYPE(rel->r_info));
301 /* Relocate the jump slots in an object. */
303 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
305 const Elf_Rel *rellim;
308 if (obj->jmpslots_done)
310 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
311 for (rel = obj->pltrel; rel < rellim; rel++) {
312 Elf_Addr *where, target;
314 const Obj_Entry *defobj;
316 switch (ELF_R_TYPE(rel->r_info)) {
318 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
319 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
320 SYMLOOK_IN_PLT | flags, NULL, lockstate);
323 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
324 obj->gnu_ifunc = true;
327 target = (Elf_Addr)(defobj->relocbase + def->st_value);
328 reloc_jmpslot(where, target, defobj, obj, rel);
331 case R_386_IRELATIVE:
335 _rtld_error("Unknown relocation type %x in PLT",
336 ELF_R_TYPE(rel->r_info));
341 obj->jmpslots_done = true;
346 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
348 const Elf_Rel *rellim;
350 Elf_Addr *where, target;
354 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
355 for (rel = obj->pltrel; rel < rellim; rel++) {
356 switch (ELF_R_TYPE(rel->r_info)) {
357 case R_386_IRELATIVE:
358 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
359 lock_release(rtld_bind_lock, lockstate);
360 target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
361 wlock_acquire(rtld_bind_lock, lockstate);
366 obj->irelative = false;
371 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
373 const Elf_Rel *rellim;
378 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
379 for (rel = obj->pltrel; rel < rellim; rel++) {
380 Elf_Addr *where, target;
382 const Obj_Entry *defobj;
384 switch (ELF_R_TYPE(rel->r_info)) {
386 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
387 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
388 SYMLOOK_IN_PLT | flags, NULL, lockstate);
391 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
393 lock_release(rtld_bind_lock, lockstate);
394 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
395 wlock_acquire(rtld_bind_lock, lockstate);
396 reloc_jmpslot(where, target, defobj, obj, rel);
401 obj->gnu_ifunc = false;
406 allocate_initial_tls(Obj_Entry *objs)
411 * Fix the size of the static TLS block by using the maximum
412 * offset allocated so far and adding a bit for dynamic modules to
415 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
416 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
417 i386_set_gsbase(tls);
421 __attribute__((__regparm__(1)))
422 void *___tls_get_addr(tls_index *ti)
426 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
428 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
432 void *__tls_get_addr(tls_index *ti)
436 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
438 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);