mkfs: drop support for zone != block
[minix.git] / libexec / ld.elf_so / arch / i386 / mdreloc.c
blobb1657c682153836da12b3ccacdf5a68e91c968b7
1 /* $NetBSD: mdreloc.c,v 1.32 2010/08/06 16:33:18 joerg Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.32 2010/08/06 16:33:18 joerg Exp $");
6 #endif /* not lint */
8 #include <sys/types.h>
10 #include "debug.h"
11 #include "rtld.h"
13 void _rtld_bind_start(void);
14 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
15 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
17 void
18 _rtld_setup_pltgot(const Obj_Entry *obj)
20 obj->pltgot[1] = (Elf_Addr) obj;
21 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
24 void
25 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
27 const Elf_Rel *rel = 0, *rellim;
28 Elf_Addr relsz = 0;
29 Elf_Addr *where;
31 for (; dynp->d_tag != DT_NULL; dynp++) {
32 switch (dynp->d_tag) {
33 case DT_REL:
34 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
35 break;
36 case DT_RELSZ:
37 relsz = dynp->d_un.d_val;
38 break;
41 if (rel == 0 || relsz == 0)
42 return;
43 rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
44 for (; rel < rellim; rel++) {
45 where = (Elf_Addr *)(relocbase + rel->r_offset);
46 *where += (Elf_Addr)relocbase;
50 int
51 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
53 const Elf_Rel *rel;
54 Elf_Addr target = 0;
56 for (rel = obj->rel; rel < obj->rellim; rel++) {
57 Elf_Addr *where;
58 const Elf_Sym *def;
59 const Obj_Entry *defobj;
60 Elf_Addr tmp;
61 unsigned long symnum;
63 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
64 symnum = ELF_R_SYM(rel->r_info);
66 switch (ELF_R_TYPE(rel->r_info)) {
67 case R_TYPE(NONE):
68 break;
70 #if 1 /* XXX should not occur */
71 case R_TYPE(PC32):
72 def = _rtld_find_symdef(symnum, obj, &defobj, false);
73 if (def == NULL)
74 return -1;
75 target = (Elf_Addr)(defobj->relocbase + def->st_value);
77 *where += target - (Elf_Addr)where;
78 rdbg(("PC32 %s in %s --> %p in %s",
79 obj->strtab + obj->symtab[symnum].st_name,
80 obj->path, (void *)*where, defobj->path));
81 break;
83 case R_TYPE(GOT32):
84 #endif
85 case R_TYPE(32):
86 case R_TYPE(GLOB_DAT):
87 def = _rtld_find_symdef(symnum, obj, &defobj, false);
88 if (def == NULL)
89 return -1;
90 target = (Elf_Addr)(defobj->relocbase + def->st_value);
92 tmp = target + *where;
93 if (*where != tmp)
94 *where = tmp;
95 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
96 obj->strtab + obj->symtab[symnum].st_name,
97 obj->path, (void *)*where, defobj->path));
98 break;
100 case R_TYPE(RELATIVE):
101 *where += (Elf_Addr)obj->relocbase;
102 rdbg(("RELATIVE in %s --> %p", obj->path,
103 (void *)*where));
104 break;
106 case R_TYPE(COPY):
108 * These are deferred until all other relocations have
109 * been done. All we do here is make sure that the
110 * COPY relocation is not in a shared library. They
111 * are allowed only in executable files.
113 if (obj->isdynamic) {
114 _rtld_error(
115 "%s: Unexpected R_COPY relocation in shared library",
116 obj->path);
117 return -1;
119 rdbg(("COPY (avoid in main)"));
120 break;
122 default:
123 rdbg(("sym = %lu, type = %lu, offset = %p, "
124 "contents = %p, symbol = %s",
125 symnum, (u_long)ELF_R_TYPE(rel->r_info),
126 (void *)rel->r_offset, (void *)*where,
127 obj->strtab + obj->symtab[symnum].st_name));
128 _rtld_error("%s: Unsupported relocation type %ld "
129 "in non-PLT relocations",
130 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
131 return -1;
134 return 0;
138 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
140 const Elf_Rel *rel;
142 if (!obj->relocbase)
143 return 0;
145 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
146 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
148 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
150 /* Just relocate the GOT slots pointing into the PLT */
151 *where += (Elf_Addr)obj->relocbase;
152 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
155 return 0;
158 static inline int
159 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
160 Elf_Addr *tp)
162 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
163 Elf_Addr target;
164 const Elf_Sym *def;
165 const Obj_Entry *defobj;
166 unsigned long info = rel->r_info;
168 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
170 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
171 if (__predict_false(def == NULL))
172 return -1;
173 if (__predict_false(def == &_rtld_sym_zero))
174 return 0;
176 target = (Elf_Addr)(defobj->relocbase + def->st_value);
177 rdbg(("bind now/fixup in %s --> old=%p new=%p",
178 defobj->strtab + def->st_name, (void *)*where,
179 (void *)target));
180 if (*where != target)
181 *where = target;
182 if (tp)
183 *tp = target;
184 return 0;
187 caddr_t
188 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
190 const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel
191 + reloff);
192 Elf_Addr new_value;
193 int err;
195 new_value = 0; /* XXX gcc */
197 err = _rtld_relocate_plt_object(obj, rel, &new_value);
198 if (err)
199 _rtld_die();
201 return (caddr_t)new_value;
205 _rtld_relocate_plt_objects(const Obj_Entry *obj)
207 const Elf_Rel *rel;
208 int err = 0;
210 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
211 err = _rtld_relocate_plt_object(obj, rel, NULL);
212 if (err)
213 break;
215 return err;