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));
87 req.flags = SYMLOOK_EARLY;
89 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) {
90 res = symlook_obj(&req, srcobj);
93 defobj = req.defobj_out;
99 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
100 " relocation in %s", name, dstobj->path);
104 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
105 memcpy(dstaddr, srcaddr, size);
112 /* Initialize the special GOT entries. */
114 init_pltgot(Obj_Entry *obj)
116 if (obj->pltgot != NULL) {
117 obj->pltgot[1] = (Elf_Addr) obj;
118 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
122 /* Process the non-PLT relocations. */
124 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
125 RtldLockState *lockstate)
127 const Elf_Rel *rellim;
133 * The dynamic loader may be called from a thread, we have
134 * limited amounts of stack available so we cannot use alloca().
136 if (obj != obj_rtld) {
137 cache = calloc(obj->dynsymcount, sizeof(SymCache));
138 /* No need to check for NULL here */
142 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
143 for (rel = obj->rel; rel < rellim; rel++) {
144 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
146 switch (ELF_R_TYPE(rel->r_info)) {
154 const Obj_Entry *defobj;
156 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
157 flags, cache, lockstate);
161 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
167 * I don't think the dynamic linker should ever see this
168 * type of relocation. But the binutils-2.6 tools sometimes
173 const Obj_Entry *defobj;
175 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
176 flags, cache, lockstate);
181 (Elf_Addr) (defobj->relocbase + def->st_value) -
188 * These are deferred until all other relocations have
189 * been done. All we do here is make sure that the COPY
190 * relocation is not in a shared library. They are allowed
191 * only in executable files.
193 if (!obj->mainprog) {
194 _rtld_error("%s: Unexpected R_386_COPY relocation"
195 " in shared library", obj->path);
203 const Obj_Entry *defobj;
205 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
206 flags, cache, lockstate);
210 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
215 *where += (Elf_Addr) obj->relocbase;
218 case R_386_TLS_TPOFF:
219 case R_386_TLS_TPOFF32:
222 const Obj_Entry *defobj;
225 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
226 flags, cache, lockstate);
231 * We lazily allocate offsets for static TLS as we
232 * see the first relocation that references the
233 * TLS block. This allows us to support (small
234 * amounts of) static TLS in dynamically loaded
235 * modules. If we run out of space, we generate an
238 if (!defobj->tls_done) {
239 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
240 _rtld_error("%s: No space available for static "
241 "Thread Local Storage", obj->path);
245 add = (Elf_Addr) (def->st_value - defobj->tlsoffset);
246 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
253 case R_386_TLS_DTPMOD32:
256 const Obj_Entry *defobj;
258 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
259 flags, cache, lockstate);
263 *where += (Elf_Addr) defobj->tlsindex;
267 case R_386_TLS_DTPOFF32:
270 const Obj_Entry *defobj;
272 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
273 flags, cache, lockstate);
277 *where += (Elf_Addr) def->st_value;
282 _rtld_error("%s: Unsupported relocation type %d"
283 " in non-PLT relocations\n", obj->path,
284 ELF_R_TYPE(rel->r_info));
295 /* Process the PLT relocations. */
297 reloc_plt(Obj_Entry *obj)
299 const Elf_Rel *rellim;
302 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
303 for (rel = obj->pltrel; rel < rellim; rel++) {
306 switch (ELF_R_TYPE(rel->r_info)) {
308 /* Relocate the GOT slot pointing into the PLT. */
309 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
310 *where += (Elf_Addr)obj->relocbase;
313 case R_386_IRELATIVE:
314 obj->irelative = true;
318 _rtld_error("Unknown relocation type %x in PLT",
319 ELF_R_TYPE(rel->r_info));
326 /* Relocate the jump slots in an object. */
328 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
330 const Elf_Rel *rellim;
333 if (obj->jmpslots_done)
335 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
336 for (rel = obj->pltrel; rel < rellim; rel++) {
337 Elf_Addr *where, target;
339 const Obj_Entry *defobj;
341 switch (ELF_R_TYPE(rel->r_info)) {
343 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
344 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
345 SYMLOOK_IN_PLT | flags, NULL, lockstate);
348 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
349 obj->gnu_ifunc = true;
352 target = (Elf_Addr)(defobj->relocbase + def->st_value);
353 reloc_jmpslot(where, target, defobj, obj, rel);
356 case R_386_IRELATIVE:
360 _rtld_error("Unknown relocation type %x in PLT",
361 ELF_R_TYPE(rel->r_info));
366 obj->jmpslots_done = true;
371 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
373 const Elf_Rel *rellim;
375 Elf_Addr *where, target;
379 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
380 for (rel = obj->pltrel; rel < rellim; rel++) {
381 switch (ELF_R_TYPE(rel->r_info)) {
382 case R_386_IRELATIVE:
383 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
384 lock_release(rtld_bind_lock, lockstate);
385 target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
386 wlock_acquire(rtld_bind_lock, lockstate);
391 obj->irelative = false;
396 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
398 const Elf_Rel *rellim;
403 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
404 for (rel = obj->pltrel; rel < rellim; rel++) {
405 Elf_Addr *where, target;
407 const Obj_Entry *defobj;
409 switch (ELF_R_TYPE(rel->r_info)) {
411 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
412 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
413 SYMLOOK_IN_PLT | flags, NULL, lockstate);
416 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
418 lock_release(rtld_bind_lock, lockstate);
419 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
420 wlock_acquire(rtld_bind_lock, lockstate);
421 reloc_jmpslot(where, target, defobj, obj, rel);
426 obj->gnu_ifunc = false;
431 __attribute__((__regparm__(1)))
433 ___tls_get_addr(tls_index *ti)
438 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
443 __tls_get_addr(tls_index *ti)
448 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
453 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
455 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);