No empty .Rs/.Re
[netbsd-mini2440.git] / libexec / ld.elf_so / arch / arm / mdreloc.c
blob746b6e2ea1040e7e0a25c6ce94f337d79f9afd00
1 /* $NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $");
6 #endif /* not lint */
8 #include <sys/types.h>
9 #include <sys/stat.h>
11 #include <string.h>
13 #include "debug.h"
14 #include "rtld.h"
16 void _rtld_bind_start(void);
17 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
18 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
20 void
21 _rtld_setup_pltgot(const Obj_Entry *obj)
23 obj->pltgot[1] = (Elf_Addr) obj;
24 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
27 void
28 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
30 const Elf_Rel *rel = 0, *rellim;
31 Elf_Addr relsz = 0;
32 Elf_Addr *where;
34 for (; dynp->d_tag != DT_NULL; dynp++) {
35 switch (dynp->d_tag) {
36 case DT_REL:
37 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
38 break;
39 case DT_RELSZ:
40 relsz = dynp->d_un.d_val;
41 break;
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;
52 * It is possible for the compiler to emit relocations for unaligned data.
53 * We handle this situation with these inlines.
55 #define RELOC_ALIGNED_P(x) \
56 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
58 static inline Elf_Addr
59 load_ptr(void *where)
61 Elf_Addr res;
63 memcpy(&res, where, sizeof(res));
65 return (res);
68 static inline void
69 store_ptr(void *where, Elf_Addr val)
72 memcpy(where, &val, sizeof(val));
75 int
76 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
78 const Elf_Rel *rel;
80 for (rel = obj->rel; rel < obj->rellim; rel++) {
81 Elf_Addr *where;
82 const Elf_Sym *def;
83 const Obj_Entry *defobj;
84 Elf_Addr tmp;
85 unsigned long symnum;
87 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
88 symnum = ELF_R_SYM(rel->r_info);
90 switch (ELF_R_TYPE(rel->r_info)) {
91 case R_TYPE(NONE):
92 break;
94 #if 1 /* XXX should not occur */
95 case R_TYPE(PC24): { /* word32 S - P + A */
96 Elf32_Sword addend;
99 * Extract addend and sign-extend if needed.
101 addend = *where;
102 if (addend & 0x00800000)
103 addend |= 0xff000000;
105 def = _rtld_find_symdef(symnum, obj, &defobj, false);
106 if (def == NULL)
107 return -1;
108 tmp = (Elf_Addr)obj->relocbase + def->st_value
109 - (Elf_Addr)where + (addend << 2);
110 if ((tmp & 0xfe000000) != 0xfe000000 &&
111 (tmp & 0xfe000000) != 0) {
112 _rtld_error(
113 "%s: R_ARM_PC24 relocation @ %p to %s failed "
114 "(displacement %ld (%#lx) out of range)",
115 obj->path, where,
116 obj->strtab + obj->symtab[symnum].st_name,
117 (long) tmp, (long) tmp);
118 return -1;
120 tmp >>= 2;
121 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
122 rdbg(("PC24 %s in %s --> %p @ %p in %s",
123 obj->strtab + obj->symtab[symnum].st_name,
124 obj->path, (void *)*where, where, defobj->path));
125 break;
127 #endif
129 case R_TYPE(ABS32): /* word32 B + S + A */
130 case R_TYPE(GLOB_DAT): /* word32 B + S */
131 def = _rtld_find_symdef(symnum, obj, &defobj, false);
132 if (def == NULL)
133 return -1;
134 if (__predict_true(RELOC_ALIGNED_P(where))) {
135 tmp = *where + (Elf_Addr)defobj->relocbase +
136 def->st_value;
137 /* Set the Thumb bit, if needed. */
138 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
139 tmp |= 1;
140 *where = tmp;
141 } else {
142 tmp = load_ptr(where) +
143 (Elf_Addr)defobj->relocbase +
144 def->st_value;
145 /* Set the Thumb bit, if needed. */
146 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
147 tmp |= 1;
148 store_ptr(where, tmp);
150 rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
151 obj->strtab + obj->symtab[symnum].st_name,
152 obj->path, (void *)tmp, where, defobj->path));
153 break;
155 case R_TYPE(RELATIVE): /* word32 B + A */
156 if (__predict_true(RELOC_ALIGNED_P(where))) {
157 tmp = *where + (Elf_Addr)obj->relocbase;
158 *where = tmp;
159 } else {
160 tmp = load_ptr(where) +
161 (Elf_Addr)obj->relocbase;
162 store_ptr(where, tmp);
164 rdbg(("RELATIVE in %s --> %p", obj->path,
165 (void *)tmp));
166 break;
168 case R_TYPE(COPY):
170 * These are deferred until all other relocations have
171 * been done. All we do here is make sure that the
172 * COPY relocation is not in a shared library. They
173 * are allowed only in executable files.
175 if (obj->isdynamic) {
176 _rtld_error(
177 "%s: Unexpected R_COPY relocation in shared library",
178 obj->path);
179 return -1;
181 rdbg(("COPY (avoid in main)"));
182 break;
184 default:
185 rdbg(("sym = %lu, type = %lu, offset = %p, "
186 "contents = %p, symbol = %s",
187 symnum, (u_long)ELF_R_TYPE(rel->r_info),
188 (void *)rel->r_offset, (void *)load_ptr(where),
189 obj->strtab + obj->symtab[symnum].st_name));
190 _rtld_error("%s: Unsupported relocation type %ld "
191 "in non-PLT relocations",
192 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
193 return -1;
196 return 0;
200 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
202 const Elf_Rel *rel;
204 if (!obj->relocbase)
205 return 0;
207 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
208 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
210 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
212 /* Just relocate the GOT slots pointing into the PLT */
213 *where += (Elf_Addr)obj->relocbase;
214 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
217 return 0;
220 static int
221 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
222 Elf_Addr *tp)
224 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
225 Elf_Addr new_value;
226 const Elf_Sym *def;
227 const Obj_Entry *defobj;
229 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
231 def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
232 if (def == NULL)
233 return -1;
235 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
236 /* Set the Thumb bit, if needed. */
237 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
238 new_value |= 1;
239 rdbg(("bind now/fixup in %s --> old=%p new=%p",
240 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
241 if (*where != new_value)
242 *where = new_value;
243 if (tp)
244 *tp = new_value;
246 return 0;
249 caddr_t
250 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
252 const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
253 Elf_Addr new_value;
254 int err;
256 err = _rtld_relocate_plt_object(obj, rel, &new_value);
257 if (err || new_value == 0)
258 _rtld_die();
260 return (caddr_t)new_value;
263 _rtld_relocate_plt_objects(const Obj_Entry *obj)
265 const Elf_Rel *rel;
266 int err = 0;
268 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
269 err = _rtld_relocate_plt_object(obj, rel, NULL);
270 if (err)
271 break;
274 return err;