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.2 2003/06/17 04:27:08 dillon 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 memset(cache, 0, bytes);
132 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize);
133 for (rel = obj->rel; rel < rellim; rel++) {
134 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset);
136 switch (ELF_R_TYPE(rel->r_info)) {
144 const Obj_Entry *defobj;
146 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
151 *where += (Elf_Addr) (defobj->relocbase + def->st_value);
157 * I don't think the dynamic linker should ever see this
158 * type of relocation. But the binutils-2.6 tools sometimes
163 const Obj_Entry *defobj;
165 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
171 (Elf_Addr) (defobj->relocbase + def->st_value) -
178 * These are deferred until all other relocations have
179 * been done. All we do here is make sure that the COPY
180 * relocation is not in a shared library. They are allowed
181 * only in executable files.
183 if (!obj->mainprog) {
184 _rtld_error("%s: Unexpected R_386_COPY relocation"
185 " in shared library", obj->path);
193 const Obj_Entry *defobj;
195 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
200 *where = (Elf_Addr) (defobj->relocbase + def->st_value);
205 *where += (Elf_Addr) obj->relocbase;
209 _rtld_error("%s: Unsupported relocation type %d"
210 " in non-PLT relocations\n", obj->path,
211 ELF_R_TYPE(rel->r_info));
218 munmap(cache, bytes);
222 /* Process the PLT relocations. */
224 reloc_plt(Obj_Entry *obj)
226 const Elf_Rel *rellim;
229 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
230 for (rel = obj->pltrel; rel < rellim; rel++) {
233 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
235 /* Relocate the GOT slot pointing into the PLT. */
236 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
237 *where += (Elf_Addr)obj->relocbase;
242 /* Relocate the jump slots in an object. */
244 reloc_jmpslots(Obj_Entry *obj)
246 const Elf_Rel *rellim;
249 if (obj->jmpslots_done)
251 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
252 for (rel = obj->pltrel; rel < rellim; rel++) {
255 const Obj_Entry *defobj;
257 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT);
258 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
259 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true, NULL);
262 reloc_jmpslot(where, (Elf_Addr)(defobj->relocbase + def->st_value));
264 obj->jmpslots_done = true;