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;
132 * The dynamic loader may be called from a thread, we have
133 * limited amounts of stack available so we cannot use alloca().
135 if (obj != obj_rtld) {
136 cache = calloc(obj->dynsymcount, sizeof(SymCache));
137 /* No need to check for NULL here */
141 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
142 for (rel = obj->rel; rel < rellim; rel++) {
143 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
145 switch (ELF_R_TYPE(rel->r_info)) {
153 const Obj_Entry *defobj;
155 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
156 flags, cache, lockstate);
160 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
166 * I don't think the dynamic linker should ever see this
167 * type of relocation. But the binutils-2.6 tools sometimes
172 const Obj_Entry *defobj;
174 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
175 flags, cache, lockstate);
180 (Elf_Addr) (defobj->relocbase + def->st_value) -
187 * These are deferred until all other relocations have
188 * been done. All we do here is make sure that the COPY
189 * relocation is not in a shared library. They are allowed
190 * only in executable files.
192 if (!obj->mainprog) {
193 _rtld_error("%s: Unexpected R_386_COPY relocation"
194 " in shared library", obj->path);
202 const Obj_Entry *defobj;
204 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
205 flags, cache, lockstate);
209 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
214 *where += (Elf_Addr) obj->relocbase;
217 case R_386_TLS_TPOFF:
218 case R_386_TLS_TPOFF32:
221 const Obj_Entry *defobj;
224 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
225 flags, cache, lockstate);
230 * We lazily allocate offsets for static TLS as we
231 * see the first relocation that references the
232 * TLS block. This allows us to support (small
233 * amounts of) static TLS in dynamically loaded
234 * modules. If we run out of space, we generate an
237 if (!defobj->tls_done) {
238 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
239 _rtld_error("%s: No space available for static "
240 "Thread Local Storage", obj->path);
244 add = (Elf_Addr) (def->st_value - defobj->tlsoffset);
245 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
252 case R_386_TLS_DTPMOD32:
255 const Obj_Entry *defobj;
257 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
258 flags, cache, lockstate);
262 *where += (Elf_Addr) defobj->tlsindex;
266 case R_386_TLS_DTPOFF32:
269 const Obj_Entry *defobj;
271 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
272 flags, cache, lockstate);
276 *where += (Elf_Addr) def->st_value;
281 _rtld_error("%s: Unsupported relocation type %d"
282 " in non-PLT relocations\n", obj->path,
283 ELF_R_TYPE(rel->r_info));
294 /* Process the PLT relocations. */
296 reloc_plt(Obj_Entry *obj)
298 const Elf_Rel *rellim;
301 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
302 for (rel = obj->pltrel; rel < rellim; rel++) {
303 Elf_Addr *where/*, val*/;
305 switch (ELF_R_TYPE(rel->r_info)) {
307 /* Relocate the GOT slot pointing into the PLT. */
308 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
309 *where += (Elf_Addr)obj->relocbase;
312 case R_386_IRELATIVE:
313 obj->irelative = true;
317 _rtld_error("Unknown relocation type %x in PLT",
318 ELF_R_TYPE(rel->r_info));
325 /* Relocate the jump slots in an object. */
327 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
329 const Elf_Rel *rellim;
332 if (obj->jmpslots_done)
334 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
335 for (rel = obj->pltrel; rel < rellim; rel++) {
336 Elf_Addr *where, target;
338 const Obj_Entry *defobj;
340 switch (ELF_R_TYPE(rel->r_info)) {
342 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
343 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
344 SYMLOOK_IN_PLT | flags, NULL, lockstate);
347 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
348 obj->gnu_ifunc = true;
351 target = (Elf_Addr)(defobj->relocbase + def->st_value);
352 reloc_jmpslot(where, target, defobj, obj, rel);
355 case R_386_IRELATIVE:
359 _rtld_error("Unknown relocation type %x in PLT",
360 ELF_R_TYPE(rel->r_info));
365 obj->jmpslots_done = true;
370 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
372 const Elf_Rel *rellim;
374 Elf_Addr *where, target;
378 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
379 for (rel = obj->pltrel; rel < rellim; rel++) {
380 switch (ELF_R_TYPE(rel->r_info)) {
381 case R_386_IRELATIVE:
382 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
383 lock_release(rtld_bind_lock, lockstate);
384 target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
385 wlock_acquire(rtld_bind_lock, lockstate);
390 obj->irelative = false;
395 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
397 const Elf_Rel *rellim;
402 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
403 for (rel = obj->pltrel; rel < rellim; rel++) {
404 Elf_Addr *where, target;
406 const Obj_Entry *defobj;
408 switch (ELF_R_TYPE(rel->r_info)) {
410 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
411 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
412 SYMLOOK_IN_PLT | flags, NULL, lockstate);
415 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
417 lock_release(rtld_bind_lock, lockstate);
418 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
419 wlock_acquire(rtld_bind_lock, lockstate);
420 reloc_jmpslot(where, target, defobj, obj, rel);
425 obj->gnu_ifunc = false;
430 allocate_initial_tls(Obj_Entry *objs)
435 * Fix the size of the static TLS block by using the maximum
436 * offset allocated so far and adding a bit for dynamic modules to
439 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
440 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
441 i386_set_gsbase(tls);
445 __attribute__((__regparm__(1)))
446 void *___tls_get_addr(tls_index *ti)
450 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
452 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
456 void *__tls_get_addr(tls_index *ti)
460 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
462 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);