Remove building with NOCRYPTO option
[minix.git] / libexec / ld.elf_so / arch / or1k / mdreloc.c
blob035e6add6cd99a7567214f688864f47784858953
1 /* $NetBSD: mdreloc.c,v 1.1 2014/09/03 19:34:26 matt Exp $ */
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: mdreloc.c,v 1.1 2014/09/03 19:34:26 matt Exp $");
35 #endif /* not lint */
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <machine/cpu.h>
44 #include "debug.h"
45 #include "rtld.h"
47 void _rtld_bind_start(void);
48 Elf_Addr _rtld_bind(const Obj_Entry *, Elf_Word);
49 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
50 static int _rtld_relocate_plt_object(const Obj_Entry *,
51 const Elf_Rela *, int, Elf_Addr *);
54 * The OR1k PLT format is simple.
57 void
58 _rtld_setup_pltgot(const Obj_Entry *obj)
60 obj->pltgot[1] = (Elf_Addr) obj;
61 obj->pltgot[2] = (Elf_Addr) _rtld_bind_start;
63 dbg(("obj %s plt pltgot=%p start=%p obj=%p",
64 obj->path, obj->pltgot,
65 (void *) obj->pltgot[1], (void *) obj->pltgot[2]));
68 void
69 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
71 const Elf_Rela *rela = 0, *relalim;
72 Elf_Addr relasz = 0;
73 Elf_Addr *where;
75 for (; dynp->d_tag != DT_NULL; dynp++) {
76 switch (dynp->d_tag) {
77 case DT_RELA:
78 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
79 break;
80 case DT_RELASZ:
81 relasz = dynp->d_un.d_val;
82 break;
85 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
86 for (; rela < relalim; rela++) {
87 where = (Elf_Addr *)(relocbase + rela->r_offset);
88 *where = (Elf_Addr)(relocbase + rela->r_addend);
92 int
93 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
95 const Elf_Rela *rela;
97 for (rela = obj->rela; rela < obj->relalim; rela++) {
98 Elf_Addr *where;
99 const Elf_Sym *def;
100 const Obj_Entry *defobj;
101 Elf_Addr tmp;
102 unsigned long symnum;
104 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
105 symnum = ELF_R_SYM(rela->r_info);
107 switch (ELF_R_TYPE(rela->r_info)) {
108 #if 1 /* XXX Should not be necessary. */
109 case R_TYPE(JMP_SLOT):
110 #endif
111 case R_TYPE(NONE):
112 break;
114 case R_TYPE(32): /* <address> S + A */
115 case R_TYPE(GLOB_DAT): /* <address> S + A */
116 def = _rtld_find_symdef(symnum, obj, &defobj, false);
117 if (def == NULL)
118 return -1;
120 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
121 rela->r_addend);
122 if (*where != tmp)
123 *where = tmp;
124 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
125 obj->strtab + obj->symtab[symnum].st_name,
126 obj->path, (void *)*where, defobj->path));
127 break;
129 case R_TYPE(RELATIVE): /* <address> B + A */
130 *where = (Elf_Addr)(obj->relocbase + rela->r_addend);
131 rdbg(("RELATIVE in %s --> %p", obj->path,
132 (void *)*where));
133 break;
135 case R_TYPE(COPY):
137 * These are deferred until all other relocations have
138 * been done. All we do here is make sure that the
139 * COPY relocation is not in a shared library. They
140 * are allowed only in executable files.
142 if (obj->isdynamic) {
143 _rtld_error(
144 "%s: Unexpected R_COPY relocation in shared library",
145 obj->path);
146 return -1;
148 rdbg(("COPY (avoid in main)"));
149 break;
151 case R_TYPE(TLS_DTPMOD):
152 def = _rtld_find_symdef(symnum, obj, &defobj, false);
153 if (def == NULL)
154 return -1;
156 *where = (Elf_Addr)defobj->tlsindex;
157 rdbg(("DTPMOD32 %s in %s --> %p in %s",
158 obj->strtab + obj->symtab[symnum].st_name,
159 obj->path, (void *)*where, defobj->path));
160 break;
162 case R_TYPE(TLS_DTPOFF):
163 def = _rtld_find_symdef(symnum, obj, &defobj, false);
164 if (def == NULL)
165 return -1;
167 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
168 return -1;
170 *where = (Elf_Addr)(def->st_value + rela->r_addend
171 - TLS_DTV_OFFSET);
172 rdbg(("DTPOFF %s in %s --> %p in %s",
173 obj->strtab + obj->symtab[symnum].st_name,
174 obj->path, (void *)*where, defobj->path));
175 break;
177 case R_TYPE(TLS_TPOFF):
178 def = _rtld_find_symdef(symnum, obj, &defobj, false);
179 if (def == NULL)
180 return -1;
182 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
183 return -1;
185 *where = (Elf_Addr)(def->st_value + rela->r_addend
186 + defobj->tlsoffset - TLS_TP_OFFSET);
187 rdbg(("TPREL %s in %s --> %p in %s",
188 obj->strtab + obj->symtab[symnum].st_name,
189 obj->path, (void *)*where, defobj->path));
190 break;
192 default:
193 rdbg(("sym = %lu, type = %lu, offset = %p, "
194 "addend = %p, contents = %p, symbol = %s",
195 symnum, (u_long)ELF_R_TYPE(rela->r_info),
196 (void *)rela->r_offset, (void *)rela->r_addend,
197 (void *)*where,
198 obj->strtab + obj->symtab[symnum].st_name));
199 _rtld_error("%s: Unsupported relocation type %ld "
200 "in non-PLT relocations",
201 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
202 return -1;
205 return 0;
209 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
211 const Elf_Rela *rela;
212 int reloff;
214 for (rela = obj->pltrela, reloff = 0;
215 rela < obj->pltrelalim;
216 rela++, reloff++) {
217 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
219 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
222 * For now, simply treat then as relative.
224 *where += (Elf_Addr)obj->relocbase;
227 return 0;
230 static int
231 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, int reloff, Elf_Addr *tp)
233 Elf_Word *where = (Elf_Word *)(obj->relocbase + rela->r_offset);
234 Elf_Addr value;
235 const Elf_Sym *def;
236 const Obj_Entry *defobj;
237 unsigned long info = rela->r_info;
239 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
241 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
242 if (__predict_false(def == NULL))
243 return -1;
244 if (__predict_false(def == &_rtld_sym_zero))
245 return 0;
247 value = (Elf_Addr)(defobj->relocbase + def->st_value);
248 rdbg(("bind now/fixup in %s --> new=%p",
249 defobj->strtab + def->st_name, (void *)value));
252 * Simply replace the entry in GOT with the
253 * address of the routine.
255 assert(where >= (Elf_Word *)obj->pltgot);
256 assert(where < (Elf_Word *)obj->pltgot + (obj->pltrelalim - obj->pltrela));
257 *where = value;
259 if (tp)
260 *tp = value;
261 return 0;
264 Elf_Addr
265 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
267 const Elf_Rela *rela = obj->pltrela + reloff;
268 Elf_Addr new_value;
269 int err;
271 new_value = 0; /* XXX gcc */
273 _rtld_shared_enter();
274 err = _rtld_relocate_plt_object(obj, rela, reloff, &new_value);
275 if (err)
276 _rtld_die();
277 _rtld_shared_exit();
279 return new_value;
283 _rtld_relocate_plt_objects(const Obj_Entry *obj)
285 const Elf_Rela *rela;
286 int reloff;
288 for (rela = obj->pltrela, reloff = 0; rela < obj->pltrelalim; rela++, reloff++) {
289 if (_rtld_relocate_plt_object(obj, rela, reloff, NULL) < 0)
290 return -1;
292 return 0;