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>
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 = globallist_next(dstobj); srcobj != NULL;
90 srcobj = globallist_next(srcobj)) {
91 res = symlook_obj(&req, srcobj);
94 defobj = req.defobj_out;
100 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
101 " relocation in %s", name, dstobj->path);
105 srcaddr = (const void *) (defobj->relocbase + srcsym->st_value);
106 memcpy(dstaddr, srcaddr, size);
113 /* Initialize the special GOT entries. */
115 init_pltgot(Obj_Entry *obj)
117 if (obj->pltgot != NULL) {
118 obj->pltgot[1] = (Elf_Addr) obj;
119 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
123 /* Process the non-PLT relocations. */
125 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
126 RtldLockState *lockstate)
128 const Elf_Rel *rellim;
132 const Obj_Entry *defobj;
133 Elf_Addr *where, symval, add;
138 * The dynamic loader may be called from a thread, we have
139 * limited amounts of stack available so we cannot use alloca().
141 if (obj != obj_rtld) {
142 cache = calloc(obj->dynsymcount, sizeof(SymCache));
143 /* No need to check for NULL here */
147 rellim = (const Elf_Rel *)((caddr_t) obj->rel + obj->relsize);
148 for (rel = obj->rel; rel < rellim; rel++) {
149 switch (ELF_R_TYPE(rel->r_info)) {
153 case R_386_TLS_TPOFF:
154 case R_386_TLS_TPOFF32:
155 case R_386_TLS_DTPMOD32:
156 case R_386_TLS_DTPOFF32:
157 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
158 flags, cache, lockstate);
161 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
162 switch (ELF_R_TYPE(rel->r_info)) {
166 if ((flags & SYMLOOK_IFUNC) == 0) {
167 obj->non_plt_gnu_ifunc = true;
170 symval = (Elf_Addr)rtld_resolve_ifunc(
173 case R_386_TLS_TPOFF:
174 case R_386_TLS_TPOFF32:
175 case R_386_TLS_DTPMOD32:
176 case R_386_TLS_DTPOFF32:
177 _rtld_error("%s: IFUNC for TLS reloc",
182 if ((flags & SYMLOOK_IFUNC) != 0)
184 symval = (Elf_Addr)defobj->relocbase +
189 if ((flags & SYMLOOK_IFUNC) != 0)
193 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
195 switch (ELF_R_TYPE(rel->r_info)) {
203 * I don't think the dynamic linker should ever
204 * see this type of relocation. But the
205 * binutils-2.6 tools sometimes generate it.
207 *where += symval - (Elf_Addr)where;
211 * These are deferred until all other
212 * relocations have been done. All we do here
213 * is make sure that the COPY relocation is
214 * not in a shared library. They are allowed
215 * only in executable files.
217 if (!obj->mainprog) {
218 _rtld_error("%s: Unexpected R_386_COPY "
219 "relocation in shared library", obj->path);
227 *where += (Elf_Addr)obj->relocbase;
229 case R_386_TLS_TPOFF:
230 case R_386_TLS_TPOFF32:
232 * We lazily allocate offsets for static TLS
233 * as we see the first relocation that
234 * references the TLS block. This allows us to
235 * support (small amounts of) static TLS in
236 * dynamically loaded modules. If we run out
237 * of space, we generate an error.
239 if (!defobj->tls_done) {
240 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
241 _rtld_error("%s: No space available "
242 "for static Thread Local Storage",
247 add = (Elf_Addr)(def->st_value - defobj->tlsoffset);
248 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
253 case R_386_TLS_DTPMOD32:
254 *where += (Elf_Addr)defobj->tlsindex;
256 case R_386_TLS_DTPOFF32:
257 *where += (Elf_Addr) def->st_value;
260 _rtld_error("%s: Unsupported relocation type %d"
261 " in non-PLT relocations\n", obj->path,
262 ELF_R_TYPE(rel->r_info));
272 /* Process the PLT relocations. */
274 reloc_plt(Obj_Entry *obj)
276 const Elf_Rel *rellim;
279 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
280 for (rel = obj->pltrel; rel < rellim; rel++) {
281 Elf_Addr *where/*, val*/;
283 switch (ELF_R_TYPE(rel->r_info)) {
285 /* Relocate the GOT slot pointing into the PLT. */
286 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
287 *where += (Elf_Addr)obj->relocbase;
290 case R_386_IRELATIVE:
291 obj->irelative = true;
295 _rtld_error("Unknown relocation type %x in PLT",
296 ELF_R_TYPE(rel->r_info));
303 /* Relocate the jump slots in an object. */
305 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
307 const Elf_Rel *rellim;
310 if (obj->jmpslots_done)
312 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
313 for (rel = obj->pltrel; rel < rellim; rel++) {
314 Elf_Addr *where, target;
316 const Obj_Entry *defobj;
318 switch (ELF_R_TYPE(rel->r_info)) {
320 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
321 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
322 SYMLOOK_IN_PLT | flags, NULL, lockstate);
325 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
326 obj->gnu_ifunc = true;
329 target = (Elf_Addr)(defobj->relocbase + def->st_value);
330 reloc_jmpslot(where, target, defobj, obj, rel);
333 case R_386_IRELATIVE:
337 _rtld_error("Unknown relocation type %x in PLT",
338 ELF_R_TYPE(rel->r_info));
343 obj->jmpslots_done = true;
348 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
350 const Elf_Rel *rellim;
352 Elf_Addr *where, target;
356 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
357 for (rel = obj->pltrel; rel < rellim; rel++) {
358 switch (ELF_R_TYPE(rel->r_info)) {
359 case R_386_IRELATIVE:
360 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
361 lock_release(rtld_bind_lock, lockstate);
362 target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
363 wlock_acquire(rtld_bind_lock, lockstate);
368 obj->irelative = false;
373 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
375 const Elf_Rel *rellim;
380 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
381 for (rel = obj->pltrel; rel < rellim; rel++) {
382 Elf_Addr *where, target;
384 const Obj_Entry *defobj;
386 switch (ELF_R_TYPE(rel->r_info)) {
388 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
389 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
390 SYMLOOK_IN_PLT | flags, NULL, lockstate);
393 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
395 lock_release(rtld_bind_lock, lockstate);
396 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
397 wlock_acquire(rtld_bind_lock, lockstate);
398 reloc_jmpslot(where, target, defobj, obj, rel);
403 obj->gnu_ifunc = false;
408 allocate_initial_tls(Obj_Entry *objs)
413 * Fix the size of the static TLS block by using the maximum
414 * offset allocated so far and adding a bit for dynamic modules to
417 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
418 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
419 i386_set_gsbase(tls);
423 __attribute__((__regparm__(1)))
424 void *___tls_get_addr(tls_index *ti)
428 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
430 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
434 void *__tls_get_addr(tls_index *ti)
438 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
440 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);