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.
25 * $FreeBSD: src/libexec/rtld-elf/i386/reloc.c,v 1.22 2010/12/25 08:51:20 kib Exp $
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 *) ((c_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;
77 const Elf_Sym *srcsym;
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 hash = elf_hash(name);
85 size = dstsym->st_size;
86 ve = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
88 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
89 if ((srcsym = symlook_obj(name, hash, srcobj, ve, 0)) != NULL)
93 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
94 " relocation in %s", name, dstobj->path);
98 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
99 memcpy(dstaddr, srcaddr, size);
106 /* Initialize the special GOT entries. */
108 init_pltgot(Obj_Entry *obj)
110 if (obj->pltgot != NULL) {
111 obj->pltgot[1] = (Elf_Addr) obj;
112 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
116 /* Process the non-PLT relocations. */
118 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
120 const Elf_Rel *rellim;
126 * The dynamic loader may be called from a thread, we have
127 * limited amounts of stack available so we cannot use alloca().
129 if (obj != obj_rtld) {
130 cache = calloc(obj->nchains, sizeof(SymCache));
131 /* No need to check for NULL here */
135 rellim = (const Elf_Rel *) ((c_caddr_t) obj->rel + obj->relsize);
136 for (rel = obj->rel; rel < rellim; rel++) {
137 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
139 switch (ELF_R_TYPE(rel->r_info)) {
147 const Obj_Entry *defobj;
149 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
154 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
160 * I don't think the dynamic linker should ever see this
161 * type of relocation. But the binutils-2.6 tools sometimes
166 const Obj_Entry *defobj;
168 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
174 (Elf_Addr) (defobj->relocbase + def->st_value) -
181 * These are deferred until all other relocations have
182 * been done. All we do here is make sure that the COPY
183 * relocation is not in a shared library. They are allowed
184 * only in executable files.
186 if (!obj->mainprog) {
187 _rtld_error("%s: Unexpected R_386_COPY relocation"
188 " in shared library", obj->path);
196 const Obj_Entry *defobj;
198 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
203 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
208 *where += (Elf_Addr) obj->relocbase;
211 case R_386_TLS_TPOFF:
214 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);
237 *where += (Elf_Addr) (def->st_value - defobj->tlsoffset);
241 case R_386_TLS_TPOFF32:
244 const Obj_Entry *defobj;
246 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
252 * We lazily allocate offsets for static TLS as we
253 * see the first relocation that references the
254 * TLS block. This allows us to support (small
255 * amounts of) static TLS in dynamically loaded
256 * modules. If we run out of space, we generate an
259 if (!defobj->tls_done) {
260 if (!allocate_tls_offset((Obj_Entry*) defobj)) {
261 _rtld_error("%s: No space available for static "
262 "Thread Local Storage", obj->path);
267 *where += (Elf_Addr) (defobj->tlsoffset - def->st_value);
271 case R_386_TLS_DTPMOD32:
274 const Obj_Entry *defobj;
276 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
281 *where += (Elf_Addr) defobj->tlsindex;
285 case R_386_TLS_DTPOFF32:
288 const Obj_Entry *defobj;
290 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
295 *where += (Elf_Addr) def->st_value;
300 _rtld_error("%s: Unsupported relocation type %d"
301 " in non-PLT relocations\n", obj->path,
302 ELF_R_TYPE(rel->r_info));
313 /* Process the PLT relocations. */
315 reloc_plt(Obj_Entry *obj)
317 const Elf_Rel *rellim;
320 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
321 for (rel = obj->pltrel; rel < rellim; rel++) {
324 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
326 /* Relocate the GOT slot pointing into the PLT. */
327 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
328 *where += (Elf_Addr)obj->relocbase;
333 /* Relocate the jump slots in an object. */
335 reloc_jmpslots(Obj_Entry *obj)
337 const Elf_Rel *rellim;
340 if (obj->jmpslots_done)
342 rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
343 for (rel = obj->pltrel; rel < rellim; rel++) {
344 Elf_Addr *where, target;
346 const Obj_Entry *defobj;
348 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
349 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
350 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
353 target = (Elf_Addr)(defobj->relocbase + def->st_value);
354 reloc_jmpslot(where, target, defobj, obj, rel);
356 obj->jmpslots_done = true;
361 __attribute__((__regparm__(1)))
363 ___tls_get_addr(tls_index *ti)
368 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
373 __tls_get_addr(tls_index *ti)
378 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);
383 __tls_get_addr_tcb(struct tls_tcb *tcb, tls_index *ti)
385 return tls_get_addr_common((Elf_Addr **)&tcb->tcb_dtv, ti->ti_module, ti->ti_offset);