Remove building with NOCRYPTO option
[minix.git] / libexec / ld.elf_so / arch / sh3 / mdreloc.c
blob691175d8b2072695877afe526a1181ebeadddc68
1 /* $NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $");
6 #endif /* not lint */
8 #include <sys/cdefs.h>
9 #ifndef lint
10 __RCSID("$NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $");
11 #endif /* not lint */
13 #include <sys/types.h>
14 #include <sys/tls.h>
16 #include "debug.h"
17 #include "rtld.h"
19 void _rtld_bind_start(void);
20 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
21 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
22 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
23 const Elf_Rela *, Elf_Addr *);
25 void
26 _rtld_setup_pltgot(const Obj_Entry *obj)
28 obj->pltgot[1] = (Elf_Addr) obj;
29 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
32 void
33 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
35 const Elf_Rela *rela = 0, *relalim;
36 Elf_Addr relasz = 0;
37 Elf_Addr *where;
39 for (; dynp->d_tag != DT_NULL; dynp++) {
40 switch (dynp->d_tag) {
41 case DT_RELA:
42 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
43 break;
44 case DT_RELASZ:
45 relasz = dynp->d_un.d_val;
46 break;
49 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
50 for (; rela < relalim; rela++) {
51 where = (Elf_Addr *)(relocbase + rela->r_offset);
52 *where = (Elf_Addr)(relocbase + rela->r_addend);
56 int
57 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
59 const Elf_Rela *rela;
61 for (rela = obj->rela; rela < obj->relalim; rela++) {
62 Elf_Addr *where;
63 const Elf_Sym *def;
64 const Obj_Entry *defobj;
65 Elf_Addr tmp;
66 unsigned long symnum;
68 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
69 symnum = ELF_R_SYM(rela->r_info);
71 switch (ELF_R_TYPE(rela->r_info)) {
72 case R_TYPE(NONE):
73 break;
75 #if 1 /* XXX should not occur */
76 case R_TYPE(GOT32):
77 def = _rtld_find_symdef(symnum, obj, &defobj, false);
78 if (def == NULL)
79 return -1;
81 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
82 rela->r_addend);
83 if (*where != tmp)
84 *where = tmp;
85 rdbg(("GOT32 %s in %s --> %p in %s",
86 obj->strtab + obj->symtab[symnum].st_name,
87 obj->path, (void *)*where, defobj->path));
88 break;
90 case R_TYPE(REL32):
91 def = _rtld_find_symdef(symnum, obj, &defobj, false);
92 if (def == NULL)
93 return -1;
95 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
96 rela->r_addend) - (Elf_Addr)where;
97 if (*where != tmp)
98 *where = tmp;
99 rdbg(("PC32 %s in %s --> %p in %s",
100 obj->strtab + obj->symtab[symnum].st_name,
101 obj->path, (void *)*where, defobj->path));
102 break;
103 #endif
105 case R_TYPE(DIR32):
106 def = _rtld_find_symdef(symnum, obj, &defobj, false);
107 if (def == NULL)
108 return -1;
110 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
111 rela->r_addend);
112 if (*where != tmp)
113 *where = tmp;
114 rdbg(("32 %s in %s --> %p in %s",
115 obj->strtab + obj->symtab[symnum].st_name,
116 obj->path, (void *)*where, defobj->path));
117 break;
119 case R_TYPE(GLOB_DAT):
120 def = _rtld_find_symdef(symnum, obj, &defobj, false);
121 if (def == NULL)
122 return -1;
124 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
125 rela->r_addend;
126 if (*where != tmp)
127 *where = tmp;
128 rdbg(("GLOB_DAT %s in %s --> %p in %s",
129 obj->strtab + obj->symtab[symnum].st_name,
130 obj->path, (void *)*where, defobj->path));
131 break;
133 case R_TYPE(RELATIVE):
134 if (rela->r_addend)
135 *where = (Elf_Addr)obj->relocbase + rela->r_addend;
136 else
137 *where += (Elf_Addr)obj->relocbase;
138 rdbg(("RELATIVE in %s --> %p", obj->path,
139 (void *)*where));
140 break;
142 case R_TYPE(COPY):
144 * These are deferred until all other relocations have
145 * been done. All we do here is make sure that the
146 * COPY relocation is not in a shared library. They
147 * are allowed only in executable files.
149 if (obj->isdynamic) {
150 _rtld_error(
151 "%s: Unexpected R_COPY relocation in shared library",
152 obj->path);
153 return -1;
155 rdbg(("COPY (avoid in main)"));
156 break;
158 case R_TYPE(TLS_DTPOFF32):
159 def = _rtld_find_symdef(symnum, obj, &defobj, false);
160 if (def == NULL)
161 return -1;
163 *where = (Elf_Addr)(def->st_value);
165 rdbg(("TLS_DTPOFF32 %s in %s --> %p",
166 obj->strtab + obj->symtab[symnum].st_name,
167 obj->path, (void *)*where));
169 break;
170 case R_TYPE(TLS_DTPMOD32):
171 def = _rtld_find_symdef(symnum, obj, &defobj, false);
172 if (def == NULL)
173 return -1;
175 *where = (Elf_Addr)(defobj->tlsindex);
177 rdbg(("TLS_DTPMOD32 %s in %s --> %p",
178 obj->strtab + obj->symtab[symnum].st_name,
179 obj->path, (void *)*where));
181 break;
183 case R_TYPE(TLS_TPOFF32):
184 def = _rtld_find_symdef(symnum, obj, &defobj, false);
185 if (def == NULL)
186 return -1;
188 if (!defobj->tls_done &&
189 _rtld_tls_offset_allocate(obj))
190 return -1;
192 *where = (Elf_Addr)def->st_value +
193 rela->r_addend + defobj->tlsoffset +
194 sizeof(struct tls_tcb);
196 rdbg(("TLS_TPOFF32 %s in %s --> %p",
197 obj->strtab + obj->symtab[symnum].st_name,
198 obj->path, (void *)*where));
199 break;
201 default:
202 rdbg(("sym = %lu, type = %lu, offset = %p, "
203 "addend = %p, contents = %p, symbol = %s",
204 symnum, (u_long)ELF_R_TYPE(rela->r_info),
205 (void *)rela->r_offset, (void *)rela->r_addend,
206 (void *)*where,
207 obj->strtab + obj->symtab[symnum].st_name));
208 _rtld_error("%s: Unsupported relocation type %ld "
209 "in non-PLT relocations",
210 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
211 return -1;
214 return 0;
218 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
220 const Elf_Rela *rela;
222 if (!obj->relocbase)
223 return 0;
225 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
226 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
228 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
230 /* Just relocate the GOT slots pointing into the PLT */
231 *where += (Elf_Addr)obj->relocbase;
232 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
235 return 0;
238 caddr_t
239 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
241 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
242 Elf_Addr new_value;
243 int err;
245 new_value = 0; /* XXX gcc */
247 _rtld_shared_enter();
248 err = _rtld_relocate_plt_object(obj, rela, &new_value);
249 if (err)
250 _rtld_die();
251 _rtld_shared_exit();
253 return (caddr_t)new_value;
257 _rtld_relocate_plt_objects(const Obj_Entry *obj)
259 const Elf_Rela *rela = obj->pltrela;
261 for (; rela < obj->pltrelalim; rela++)
262 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
263 return -1;
265 return 0;
268 static inline int
269 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
271 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
272 Elf_Addr new_value;
273 const Elf_Sym *def;
274 const Obj_Entry *defobj;
275 unsigned long info = rela->r_info;
277 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
279 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
280 if (__predict_false(def == NULL))
281 return -1;
282 if (__predict_false(def == &_rtld_sym_zero))
283 return 0;
285 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
286 if (tp == NULL)
287 return 0;
288 new_value = _rtld_resolve_ifunc(defobj, def);
289 } else {
290 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
292 rdbg(("bind now/fixup in %s --> old=%p new=%p",
293 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
294 if (*where != new_value)
295 *where = new_value;
297 if (tp)
298 *tp = new_value;
300 return 0;