Remove building with NOCRYPTO option
[minix.git] / libexec / ld.elf_so / arch / i386 / mdreloc.c
blob1ef4d0b57a5fff36ef3f37cb83b9dc8733a4d4dc
1 /* $NetBSD: mdreloc.c,v 1.37 2014/08/31 20:06:22 joerg Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.37 2014/08/31 20:06:22 joerg Exp $");
6 #endif /* not lint */
8 #include <sys/types.h>
9 #include <sys/ucontext.h>
11 #include "debug.h"
12 #include "rtld.h"
14 void _rtld_bind_start(void);
15 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
16 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
18 void
19 _rtld_setup_pltgot(const Obj_Entry *obj)
21 obj->pltgot[1] = (Elf_Addr) obj;
22 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
25 void
26 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
28 const Elf_Rel *rel = 0, *rellim;
29 Elf_Addr relsz = 0;
30 Elf_Addr *where;
32 for (; dynp->d_tag != DT_NULL; dynp++) {
33 switch (dynp->d_tag) {
34 case DT_REL:
35 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
36 break;
37 case DT_RELSZ:
38 relsz = dynp->d_un.d_val;
39 break;
42 if (rel == 0 || relsz == 0)
43 return;
44 rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
45 for (; rel < rellim; rel++) {
46 where = (Elf_Addr *)(relocbase + rel->r_offset);
47 *where += (Elf_Addr)relocbase;
51 int
52 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
54 const Elf_Rel *rel;
55 Elf_Addr target = 0;
57 for (rel = obj->rel; rel < obj->rellim; rel++) {
58 Elf_Addr *where;
59 const Elf_Sym *def;
60 const Obj_Entry *defobj;
61 Elf_Addr tmp;
62 unsigned long symnum;
64 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
65 symnum = ELF_R_SYM(rel->r_info);
67 switch (ELF_R_TYPE(rel->r_info)) {
68 case R_TYPE(NONE):
69 break;
71 #if 1 /* XXX should not occur */
72 case R_TYPE(PC32):
73 def = _rtld_find_symdef(symnum, obj, &defobj, false);
74 if (def == NULL)
75 return -1;
76 target = (Elf_Addr)(defobj->relocbase + def->st_value);
78 *where += target - (Elf_Addr)where;
79 rdbg(("PC32 %s in %s --> %p in %s",
80 obj->strtab + obj->symtab[symnum].st_name,
81 obj->path, (void *)*where, defobj->path));
82 break;
84 case R_TYPE(GOT32):
85 #endif
86 case R_TYPE(32):
87 case R_TYPE(GLOB_DAT):
88 def = _rtld_find_symdef(symnum, obj, &defobj, false);
89 if (def == NULL)
90 return -1;
91 target = (Elf_Addr)(defobj->relocbase + def->st_value);
93 tmp = target + *where;
94 if (*where != tmp)
95 *where = tmp;
96 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
97 obj->strtab + obj->symtab[symnum].st_name,
98 obj->path, (void *)*where, defobj->path));
99 break;
101 case R_TYPE(RELATIVE):
102 *where += (Elf_Addr)obj->relocbase;
103 rdbg(("RELATIVE in %s --> %p", obj->path,
104 (void *)*where));
105 break;
107 case R_TYPE(COPY):
109 * These are deferred until all other relocations have
110 * been done. All we do here is make sure that the
111 * COPY relocation is not in a shared library. They
112 * are allowed only in executable files.
114 if (obj->isdynamic) {
115 _rtld_error(
116 "%s: Unexpected R_COPY relocation in shared library",
117 obj->path);
118 return -1;
120 rdbg(("COPY (avoid in main)"));
121 break;
123 #if defined(__minix) && defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
124 case R_TYPE(TLS_TPOFF):
125 def = _rtld_find_symdef(symnum, obj, &defobj, false);
126 if (def == NULL)
127 return -1;
129 if (!defobj->tls_done &&
130 _rtld_tls_offset_allocate(obj))
131 return -1;
133 *where += (Elf_Addr)(def->st_value - defobj->tlsoffset);
135 rdbg(("TLS_TPOFF %s in %s --> %p",
136 obj->strtab + obj->symtab[symnum].st_name,
137 obj->path, (void *)*where));
138 break;
140 case R_TYPE(TLS_TPOFF32):
141 def = _rtld_find_symdef(symnum, obj, &defobj, false);
142 if (def == NULL)
143 return -1;
145 if (!defobj->tls_done &&
146 _rtld_tls_offset_allocate(obj))
147 return -1;
149 *where += (Elf_Addr)(defobj->tlsoffset - def->st_value);
150 rdbg(("TLS_TPOFF32 %s in %s --> %p",
151 obj->strtab + obj->symtab[symnum].st_name,
152 obj->path, (void *)*where));
153 break;
155 case R_TYPE(TLS_DTPMOD32):
156 def = _rtld_find_symdef(symnum, obj, &defobj, false);
157 if (def == NULL)
158 return -1;
160 *where = (Elf_Addr)(defobj->tlsindex);
162 rdbg(("TLS_DTPMOD32 %s in %s --> %p",
163 obj->strtab + obj->symtab[symnum].st_name,
164 obj->path, (void *)*where));
165 break;
167 case R_TYPE(TLS_DTPOFF32):
168 def = _rtld_find_symdef(symnum, obj, &defobj, false);
169 if (def == NULL)
170 return -1;
172 *where = (Elf_Addr)(def->st_value);
174 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
175 obj->strtab + obj->symtab[symnum].st_name,
176 obj->path, (void *)*where));
178 break;
179 #endif /* defined(__minix) && defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) */
181 default:
182 rdbg(("sym = %lu, type = %lu, offset = %p, "
183 "contents = %p, symbol = %s",
184 symnum, (u_long)ELF_R_TYPE(rel->r_info),
185 (void *)rel->r_offset, (void *)*where,
186 obj->strtab + obj->symtab[symnum].st_name));
187 _rtld_error("%s: Unsupported relocation type %ld "
188 "in non-PLT relocations",
189 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
190 return -1;
193 return 0;
197 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
199 const Elf_Rel *rel;
201 if (!obj->relocbase)
202 return 0;
204 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
205 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
207 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
209 /* Just relocate the GOT slots pointing into the PLT */
210 *where += (Elf_Addr)obj->relocbase;
211 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
214 return 0;
217 static inline int
218 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
219 Elf_Addr *tp)
221 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
222 Elf_Addr target;
223 const Elf_Sym *def;
224 const Obj_Entry *defobj;
225 unsigned long info = rel->r_info;
227 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
229 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
230 if (__predict_false(def == NULL))
231 return -1;
232 if (__predict_false(def == &_rtld_sym_zero))
233 return 0;
235 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
236 if (tp == NULL)
237 return 0;
238 target = _rtld_resolve_ifunc(defobj, def);
239 } else {
240 target = (Elf_Addr)(defobj->relocbase + def->st_value);
243 rdbg(("bind now/fixup in %s --> old=%p new=%p",
244 defobj->strtab + def->st_name, (void *)*where,
245 (void *)target));
246 if (*where != target)
247 *where = target;
248 if (tp)
249 *tp = target;
250 return 0;
253 caddr_t
254 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
256 const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel
257 + reloff);
258 Elf_Addr new_value;
259 int err;
261 new_value = 0; /* XXX gcc */
263 _rtld_shared_enter();
264 err = _rtld_relocate_plt_object(obj, rel, &new_value);
265 if (err)
266 _rtld_die();
267 _rtld_shared_exit();
269 return (caddr_t)new_value;
273 _rtld_relocate_plt_objects(const Obj_Entry *obj)
275 const Elf_Rel *rel;
276 int err = 0;
278 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
279 err = _rtld_relocate_plt_object(obj, rel, NULL);
280 if (err)
281 break;
283 return err;
286 #if defined(__minix) && defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
288 * i386 specific GNU variant of __tls_get_addr using register based
289 * argument passing.
291 #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1]))
293 __dso_public __attribute__((__regparm__(1))) void *
294 ___tls_get_addr(void *arg_)
296 size_t *arg = (size_t *)arg_;
297 void **dtv;
298 struct tls_tcb *tcb = __lwp_getprivate_fast();
299 size_t idx = arg[0], offset = arg[1];
301 dtv = tcb->tcb_dtv;
303 if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
304 return (uint8_t *)dtv[idx] + offset;
306 return _rtld_tls_get_addr(tcb, idx, offset);
308 #endif /* defined(__minix) && defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) */