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 = 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;
131 const Obj_Entry *defobj;
132 Elf_Addr *where, symval, add;
137 * The dynamic loader may be called from a thread, we have
138 * limited amounts of stack available so we cannot use alloca().
140 if (obj != obj_rtld) {
141 cache = calloc(obj->dynsymcount, sizeof(SymCache));
142 /* No need to check for NULL here */
146 rellim = (const Elf_Rel *)((caddr_t) obj->rel + obj->relsize);
147 for (rel = obj->rel; rel < rellim; rel++) {
148 switch (ELF_R_TYPE(rel->r_info)) {
152 case R_386_TLS_TPOFF:
153 case R_386_TLS_TPOFF32:
154 case R_386_TLS_DTPMOD32:
155 case R_386_TLS_DTPOFF32:
156 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
157 flags, cache, lockstate);
160 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
161 switch (ELF_R_TYPE(rel->r_info)) {
165 if ((flags & SYMLOOK_IFUNC) == 0) {
166 obj->non_plt_gnu_ifunc = true;
169 symval = (Elf_Addr)rtld_resolve_ifunc(
172 case R_386_TLS_TPOFF:
173 case R_386_TLS_TPOFF32:
174 case R_386_TLS_DTPMOD32:
175 case R_386_TLS_DTPOFF32:
176 _rtld_error("%s: IFUNC for TLS reloc",
181 if ((flags & SYMLOOK_IFUNC) != 0)
183 symval = (Elf_Addr)defobj->relocbase +
188 if ((flags & SYMLOOK_IFUNC) != 0)
192 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
194 switch (ELF_R_TYPE(rel->r_info)) {
202 * I don't think the dynamic linker should ever
203 * see this type of relocation. But the
204 * binutils-2.6 tools sometimes generate it.
206 *where += symval - (Elf_Addr)where;
210 * These are deferred until all other
211 * relocations have been done. All we do here
212 * is make sure that the COPY relocation is
213 * not in a shared library. They are allowed
214 * only in executable files.
216 if (!obj->mainprog) {
217 _rtld_error("%s: Unexpected R_386_COPY "
218 "relocation in shared library", obj->path);
226 *where += (Elf_Addr)obj->relocbase;
228 case R_386_TLS_TPOFF:
229 case R_386_TLS_TPOFF32:
231 * We lazily allocate offsets for static TLS
232 * as we see the first relocation that
233 * references the TLS block. This allows us to
234 * support (small amounts of) static TLS in
235 * dynamically loaded modules. If we run out
236 * of space, we generate an error.
238 if (!defobj->tls_done) {
239 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
240 _rtld_error("%s: No space available "
241 "for static Thread Local Storage",
246 add = (Elf_Addr)(def->st_value - defobj->tlsoffset);
247 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
252 case R_386_TLS_DTPMOD32:
253 *where += (Elf_Addr)defobj->tlsindex;
255 case R_386_TLS_DTPOFF32:
256 *where += (Elf_Addr) def->st_value;
259 _rtld_error("%s: Unsupported relocation type %d"
260 " in non-PLT relocations\n", obj->path,
261 ELF_R_TYPE(rel->r_info));
271 /* Process the PLT relocations. */
273 reloc_plt(Obj_Entry *obj)
275 const Elf_Rel *rellim;
278 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
279 for (rel = obj->pltrel; rel < rellim; rel++) {
280 Elf_Addr *where/*, val*/;
282 switch (ELF_R_TYPE(rel->r_info)) {
284 /* Relocate the GOT slot pointing into the PLT. */
285 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
286 *where += (Elf_Addr)obj->relocbase;
289 case R_386_IRELATIVE:
290 obj->irelative = true;
294 _rtld_error("Unknown relocation type %x in PLT",
295 ELF_R_TYPE(rel->r_info));
302 /* Relocate the jump slots in an object. */
304 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
306 const Elf_Rel *rellim;
309 if (obj->jmpslots_done)
311 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
312 for (rel = obj->pltrel; rel < rellim; rel++) {
313 Elf_Addr *where, target;
315 const Obj_Entry *defobj;
317 switch (ELF_R_TYPE(rel->r_info)) {
319 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
320 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
321 SYMLOOK_IN_PLT | flags, NULL, lockstate);
324 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
325 obj->gnu_ifunc = true;
328 target = (Elf_Addr)(defobj->relocbase + def->st_value);
329 reloc_jmpslot(where, target, defobj, obj, rel);
332 case R_386_IRELATIVE:
336 _rtld_error("Unknown relocation type %x in PLT",
337 ELF_R_TYPE(rel->r_info));
342 obj->jmpslots_done = true;
347 reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
349 const Elf_Rel *rellim;
351 Elf_Addr *where, target;
355 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
356 for (rel = obj->pltrel; rel < rellim; rel++) {
357 switch (ELF_R_TYPE(rel->r_info)) {
358 case R_386_IRELATIVE:
359 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
360 lock_release(rtld_bind_lock, lockstate);
361 target = ((Elf_Addr (*)(void))(obj->relocbase + *where))();
362 wlock_acquire(rtld_bind_lock, lockstate);
367 obj->irelative = false;
372 reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
374 const Elf_Rel *rellim;
379 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
380 for (rel = obj->pltrel; rel < rellim; rel++) {
381 Elf_Addr *where, target;
383 const Obj_Entry *defobj;
385 switch (ELF_R_TYPE(rel->r_info)) {
387 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
388 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
389 SYMLOOK_IN_PLT | flags, NULL, lockstate);
392 if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
394 lock_release(rtld_bind_lock, lockstate);
395 target = (Elf_Addr)rtld_resolve_ifunc(defobj, def);
396 wlock_acquire(rtld_bind_lock, lockstate);
397 reloc_jmpslot(where, target, defobj, obj, rel);
402 obj->gnu_ifunc = false;
407 allocate_initial_tls(Obj_Entry *objs)
412 * Fix the size of the static TLS block by using the maximum
413 * offset allocated so far and adding a bit for dynamic modules to
416 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
417 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
418 i386_set_gsbase(tls);
422 __attribute__((__regparm__(1)))
423 void *___tls_get_addr(tls_index *ti)
427 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
429 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
433 void *__tls_get_addr(tls_index *ti)
437 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
439 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);