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;
76 const Elf_Sym *srcsym;
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 hash = elf_hash(name);
84 size = dstsym->st_size;
85 ve = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
87 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
88 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) != NULL)
92 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
93 " relocation in %s", name, dstobj->path);
97 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
98 memcpy(dstaddr, srcaddr, size);
105 /* Initialize the special GOT entries. */
107 init_pltgot(Obj_Entry *obj)
109 if (obj->pltgot != NULL) {
110 obj->pltgot[1] = (Elf_Addr) obj;
111 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
115 /* Process the non-PLT relocations. */
117 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
119 const Elf_Rel *rellim;
122 int bytes = obj->nchains * sizeof(SymCache);
126 * The dynamic loader may be called from a thread, we have
127 * limited amounts of stack available so we cannot use alloca().
129 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
130 if (cache == MAP_FAILED)
133 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
134 for (rel = obj->rel; rel < rellim; rel++) {
135 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
137 switch (ELF_R_TYPE(rel->r_info)) {
145 const Obj_Entry *defobj;
147 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
152 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
158 * I don't think the dynamic linker should ever see this
159 * type of relocation. But the binutils-2.6 tools sometimes
164 const Obj_Entry *defobj;
166 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
172 (Elf_Addr) (defobj->relocbase + def->st_value) -
179 * These are deferred until all other relocations have
180 * been done. All we do here is make sure that the COPY
181 * relocation is not in a shared library. They are allowed
182 * only in executable files.
184 if (!obj->mainprog) {
185 _rtld_error("%s: Unexpected R_386_COPY relocation"
186 " in shared library", obj->path);
194 const Obj_Entry *defobj;
196 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
201 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
206 *where += (Elf_Addr) obj->relocbase;
209 case R_386_TLS_TPOFF:
210 case R_386_TLS_TPOFF32:
213 const Obj_Entry *defobj;
216 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
222 * We lazily allocate offsets for static TLS as we
223 * see the first relocation that references the
224 * TLS block. This allows us to support (small
225 * amounts of) static TLS in dynamically loaded
226 * modules. If we run out of space, we generate an
229 if (!defobj->tls_done) {
230 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
231 _rtld_error("%s: No space available for static "
232 "Thread Local Storage", obj->path);
236 add = (Elf_Addr) (def->st_value - defobj->tlsoffset);
237 if (ELF_R_TYPE(rel->r_info) == R_386_TLS_TPOFF)
244 case R_386_TLS_DTPMOD32:
247 const Obj_Entry *defobj;
249 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
254 *where += (Elf_Addr) defobj->tlsindex;
258 case R_386_TLS_DTPOFF32:
261 const Obj_Entry *defobj;
263 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
268 *where += (Elf_Addr) def->st_value;
273 _rtld_error("%s: Unsupported relocation type %d"
274 " in non-PLT relocations\n", obj->path,
275 ELF_R_TYPE(rel->r_info));
282 munmap(cache, bytes);
286 /* Process the PLT relocations. */
288 reloc_plt(Obj_Entry *obj)
290 const Elf_Rel *rellim;
293 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
294 for (rel = obj->pltrel; rel < rellim; rel++) {
297 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
299 /* Relocate the GOT slot pointing into the PLT. */
300 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
301 *where += (Elf_Addr)obj->relocbase;
306 /* Relocate the jump slots in an object. */
308 reloc_jmpslots(Obj_Entry *obj)
310 const Elf_Rel *rellim;
313 if (obj->jmpslots_done)
315 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
316 for (rel = obj->pltrel; rel < rellim; rel++) {
317 Elf_Addr *where, target;
319 const Obj_Entry *defobj;
321 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
322 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
323 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
326 target = (Elf_Addr)(defobj->relocbase + def->st_value);
327 reloc_jmpslot(where, target, defobj, obj, rel);
329 obj->jmpslots_done = true;
334 allocate_initial_tls(Obj_Entry *objs)
339 * Fix the size of the static TLS block by using the maximum
340 * offset allocated so far and adding a bit for dynamic modules to
343 tls_static_space = tls_last_offset + RTLD_STATIC_TLS_EXTRA;
344 tls = allocate_tls(objs, NULL, 3*sizeof(Elf_Addr), sizeof(Elf_Addr));
345 i386_set_gsbase(tls);
349 __attribute__((__regparm__(1)))
350 void *___tls_get_addr(tls_index *ti)
355 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
358 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);
362 void *__tls_get_addr(tls_index *ti)
367 __asm __volatile("movl %%gs:0, %0" : "=r" (segbase));
370 return tls_get_addr_common(&segbase[1], ti->ti_module, ti->ti_offset);