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.6.2.2 2002/06/16 20:02:09 dillon Exp $
26 * $DragonFly: src/libexec/rtld-elf/i386/reloc.c,v 1.3 2005/02/04 00:24:23 joerg Exp $
30 * Dynamic linker for ELF.
32 * John Polstra <jdp@polstra.com>.
35 #include <sys/param.h>
52 * Process the special R_386_COPY relocations in the main program. These
53 * copy data from a shared object into a region in the main program's BSS
56 * Returns 0 on success, -1 on failure.
59 do_copy_relocations(Obj_Entry *dstobj)
61 const Elf_Rel *rellim;
64 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
66 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
67 for (rel = dstobj->rel; rel < rellim; rel++) {
68 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) {
70 const Elf_Sym *dstsym;
75 const Elf_Sym *srcsym;
78 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
79 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
80 name = dstobj->strtab + dstsym->st_name;
81 hash = elf_hash(name);
82 size = dstsym->st_size;
84 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
85 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL)
89 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
90 " relocation in %s", name, dstobj->path);
94 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value);
95 memcpy(dstaddr, srcaddr, size);
102 /* Initialize the special GOT entries. */
104 init_pltgot(Obj_Entry *obj)
106 if (obj->pltgot != NULL) {
107 obj->pltgot[1] = (Elf_Addr) obj;
108 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
112 /* Process the non-PLT relocations. */
114 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld)
116 const Elf_Rel *rellim;
119 int bytes = obj->nchains * sizeof(SymCache);
123 * The dynamic loader may be called from a thread, we have
124 * limited amounts of stack available so we cannot use alloca().
126 cache = mmap(NULL, bytes, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
127 if (cache == MAP_FAILED)
130 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
131 for (rel = obj->rel; rel < rellim; rel++) {
132 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
134 switch (ELF_R_TYPE(rel->r_info)) {
142 const Obj_Entry *defobj;
144 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
149 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
155 * I don't think the dynamic linker should ever see this
156 * type of relocation. But the binutils-2.6 tools sometimes
161 const Obj_Entry *defobj;
163 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
169 (Elf_Addr) (defobj->relocbase + def->st_value) -
176 * These are deferred until all other relocations have
177 * been done. All we do here is make sure that the COPY
178 * relocation is not in a shared library. They are allowed
179 * only in executable files.
181 if (!obj->mainprog) {
182 _rtld_error("%s: Unexpected R_386_COPY relocation"
183 " in shared library", obj->path);
191 const Obj_Entry *defobj;
193 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
198 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
203 *where += (Elf_Addr) obj->relocbase;
207 _rtld_error("%s: Unsupported relocation type %d"
208 " in non-PLT relocations\n", obj->path,
209 ELF_R_TYPE(rel->r_info));
216 munmap(cache, bytes);
220 /* Process the PLT relocations. */
222 reloc_plt(Obj_Entry *obj)
224 const Elf_Rel *rellim;
227 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
228 for (rel = obj->pltrel; rel < rellim; rel++) {
231 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
233 /* Relocate the GOT slot pointing into the PLT. */
234 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
235 *where += (Elf_Addr)obj->relocbase;
240 /* Relocate the jump slots in an object. */
242 reloc_jmpslots(Obj_Entry *obj)
244 const Elf_Rel *rellim;
247 if (obj->jmpslots_done)
249 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
250 for (rel = obj->pltrel; rel < rellim; rel++) {
253 const Obj_Entry *defobj;
255 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
256 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
257 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
260 reloc_jmpslot(where, (Elf_Addr)(defobj->relocbase + def->st_value));
262 obj->jmpslots_done = true;