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>
38 #include <machine/tls.h>
54 * Process the special R_386_COPY relocations in the main program. These
55 * copy data from a shared object into a region in the main program's BSS
58 * Returns 0 on success, -1 on failure.
61 do_copy_relocations(Obj_Entry *dstobj)
63 const Elf_Rel *rellim;
66 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
68 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
69 for (rel = dstobj->rel; rel < rellim; rel++) {
70 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
72 const Elf_Sym *dstsym;
76 const Elf_Sym *srcsym;
77 const Obj_Entry *srcobj, *defobj;
81 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
82 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
83 name = dstobj->strtab + dstsym->st_name;
84 size = dstsym->st_size;
85 symlook_init(&req, name);
86 req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
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, RtldLockState *lockstate)
125 const Elf_Rel *rellim;
131 * The dynamic loader may be called from a thread, we have
132 * limited amounts of stack available so we cannot use alloca().
134 if (obj != obj_rtld) {
135 cache = calloc(obj->nchains, sizeof(SymCache));
136 /* No need to check for NULL here */
140 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
141 for (rel = obj->rel; rel < rellim; rel++) {
142 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
144 switch (ELF_R_TYPE(rel->r_info)) {
152 const Obj_Entry *defobj;
154 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
155 false, cache, lockstate);
159 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
165 * I don't think the dynamic linker should ever see this
166 * type of relocation. But the binutils-2.6 tools sometimes
171 const Obj_Entry *defobj;
173 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
174 false, cache, lockstate);
179 (Elf_Addr) (defobj->relocbase + def->st_value) -
186 * These are deferred until all other relocations have
187 * been done. All we do here is make sure that the COPY
188 * relocation is not in a shared library. They are allowed
189 * only in executable files.
191 if (!obj->mainprog) {
192 _rtld_error("%s: Unexpected R_386_COPY relocation"
193 " in shared library", obj->path);
201 const Obj_Entry *defobj;
203 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
204 false, cache, lockstate);
208 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
213 *where += (Elf_Addr) obj->relocbase;
216 case R_386_TLS_TPOFF:
217 case R_386_TLS_TPOFF32:
220 const Obj_Entry *defobj;
223 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
224 false, cache, lockstate);
229 * We lazily allocate offsets for static TLS as we
230 * see the first relocation that references the
231 * TLS block. This allows us to support (small
232 * amounts of) static TLS in dynamically loaded
233 * modules. If we run out of space, we generate an
236 if (!defobj->tls_done) {
237 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
238 _rtld_error("%s: No space available for static "
239 "Thread Local Storage", obj->path);
243 add = (Elf_Addr) (def->st_value - defobj->tlsoffset);
244 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
251 case R_386_TLS_DTPMOD32:
254 const Obj_Entry *defobj;
256 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
257 false, cache, lockstate);
261 *where += (Elf_Addr) defobj->tlsindex;
265 case R_386_TLS_DTPOFF32:
268 const Obj_Entry *defobj;
270 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
271 false, cache, lockstate);
275 *where += (Elf_Addr) def->st_value;
280 _rtld_error("%s: Unsupported relocation type %d"
281 " in non-PLT relocations\n", obj->path,
282 ELF_R_TYPE(rel->r_info));
293 /* Process the PLT relocations. */
295 reloc_plt(Obj_Entry *obj)
297 const Elf_Rel *rellim;
300 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
301 for (rel = obj->pltrel; rel < rellim; rel++) {
304 switch (ELF_R_TYPE(rel->r_info)) {
306 /* Relocate the GOT slot pointing into the PLT. */
307 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
308 *where += (Elf_Addr)obj->relocbase;
311 case R_386_IRELATIVE:
312 obj->irelative = true;
316 _rtld_error("Unknown relocation type %x in PLT",
317 ELF_R_TYPE(rel->r_info));
324 /* Relocate the jump slots in an object. */
326 reloc_jmpslots(Obj_Entry *obj, RtldLockState *lockstate)
328 const Elf_Rel *rellim;
331 if (obj->jmpslots_done)
333 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
334 for (rel = obj->pltrel; rel < rellim; rel++) {
335 Elf_Addr *where, target;
337 const Obj_Entry *defobj;
339 switch (ELF_R_TYPE(rel->r_info)) {
341 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
342 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL,
346 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
347 obj->gnu_ifunc = true;
350 target = (Elf_Addr)(defobj->relocbase + def->st_value);
351 reloc_jmpslot(where, target, defobj, obj, rel);
354 case R_386_IRELATIVE:
358 _rtld_error("Unknown relocation type %x in PLT",
359 ELF_R_TYPE(rel->r_info));
364 obj->jmpslots_done = true;
369 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
371 const Elf_Rel *rellim;
373 Elf_Addr *where, target;
377 rellim = (const Elf_Rel *)((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 = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
384 wlock_acquire(rtld_bind_lock, lockstate);
389 obj->irelative = false;
394 reloc_gnu_ifunc(Obj_Entry *obj, RtldLockState *lockstate)
396 const Elf_Rel *rellim;
401 rellim = (const Elf_Rel *)((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, true, NULL,
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;
429 __attribute__((__regparm__(1)))
431 ___tls_get_addr(tls_index *ti)
436 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
441 __tls_get_addr(tls_index *ti)
446 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
451 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
453 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);