1 /* $NetBSD: mdreloc.c,v 1.23 2003/07/26 15:04:38 mrg Exp $ */
14 #include "machine/sysarch.h"
21 arm_abi_variant_hook(Elf_Auxinfo **aux_info)
26 * If we're running an old kernel that doesn't provide any data fail
27 * safe by doing nothing.
29 if (aux_info[AT_EHDRFLAGS] == NULL)
31 ehdr = aux_info[AT_EHDRFLAGS]->a_un.a_val;
34 * Hard float ABI binaries are the default, and use the default paths
37 if ((ehdr & EF_ARM_VFP_FLOAT) != 0)
41 * This is a soft float ABI binary. We need to use the soft float
42 * settings. For the moment, the standard library path includes the hard
43 * float paths as well. When upgrading, we need to execute the wrong
44 * kind of binary until we've installed the new binaries. We could go
45 * off whether or not /libsoft exists, but the simplicity of having it
48 ld_elf_hints_default = _PATH_SOFT_ELF_HINTS;
49 ld_path_libmap_conf = _PATH_SOFT_LIBMAP_CONF;
50 ld_path_rtld = _PATH_SOFT_RTLD;
51 ld_standard_library_path = SOFT_STANDARD_LIBRARY_PATH;
52 ld_env_prefix = LD_SOFT_;
56 init_pltgot(Obj_Entry *obj)
58 if (obj->pltgot != NULL) {
59 obj->pltgot[1] = (Elf_Addr) obj;
60 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
65 do_copy_relocations(Obj_Entry *dstobj)
67 const Elf_Rel *rellim;
70 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
72 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
73 for (rel = dstobj->rel; rel < rellim; rel++) {
74 if (ELF_R_TYPE(rel->r_info) == R_ARM_COPY) {
76 const Elf_Sym *dstsym;
80 const Elf_Sym *srcsym;
81 const Obj_Entry *srcobj, *defobj;
85 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
86 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
87 name = dstobj->strtab + dstsym->st_name;
88 size = dstsym->st_size;
90 symlook_init(&req, name);
91 req.ventry = fetch_ventry(dstobj,
92 ELF_R_SYM(rel->r_info));
93 req.flags = SYMLOOK_EARLY;
95 for (srcobj = dstobj->next; srcobj != NULL;
96 srcobj = srcobj->next) {
97 res = symlook_obj(&req, srcobj);
100 defobj = req.defobj_out;
104 if (srcobj == NULL) {
106 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
111 srcaddr = (const void *)(defobj->relocbase +
113 memcpy(dstaddr, srcaddr, size);
119 void _rtld_bind_start(void);
120 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
125 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
127 const Elf_Rel *rel = 0, *rellim;
132 for (; dynp->d_tag != DT_NULL; dynp++) {
133 switch (dynp->d_tag) {
135 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
138 relsz = dynp->d_un.d_val;
142 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
143 size = (rellim - 1)->r_offset - rel->r_offset;
144 for (; rel < rellim; rel++) {
145 where = (Elf_Addr *)(relocbase + rel->r_offset);
147 *where += (Elf_Addr)relocbase;
151 * It is possible for the compiler to emit relocations for unaligned data.
152 * We handle this situation with these inlines.
154 #define RELOC_ALIGNED_P(x) \
155 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
157 static __inline Elf_Addr
158 load_ptr(void *where)
162 memcpy(&res, where, sizeof(res));
168 store_ptr(void *where, Elf_Addr val)
171 memcpy(where, &val, sizeof(val));
175 reloc_nonplt_object(Obj_Entry *obj, const Elf_Rel *rel, SymCache *cache,
176 int flags, RtldLockState *lockstate)
180 const Obj_Entry *defobj;
182 unsigned long symnum;
184 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
185 symnum = ELF_R_SYM(rel->r_info);
187 switch (ELF_R_TYPE(rel->r_info)) {
191 #if 1 /* XXX should not occur */
192 case R_ARM_PC24: { /* word32 S - P + A */
196 * Extract addend and sign-extend if needed.
199 if (addend & 0x00800000)
200 addend |= 0xff000000;
202 def = find_symdef(symnum, obj, &defobj, flags, cache,
206 tmp = (Elf_Addr)obj->relocbase + def->st_value
207 - (Elf_Addr)where + (addend << 2);
208 if ((tmp & 0xfe000000) != 0xfe000000 &&
209 (tmp & 0xfe000000) != 0) {
211 "%s: R_ARM_PC24 relocation @ %p to %s failed "
212 "(displacement %ld (%#lx) out of range)",
214 obj->strtab + obj->symtab[symnum].st_name,
215 (long) tmp, (long) tmp);
219 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
220 dbg("PC24 %s in %s --> %p @ %p in %s",
221 obj->strtab + obj->symtab[symnum].st_name,
222 obj->path, (void *)*where, where, defobj->path);
227 case R_ARM_ABS32: /* word32 B + S + A */
228 case R_ARM_GLOB_DAT: /* word32 B + S */
229 def = find_symdef(symnum, obj, &defobj, flags, cache,
233 if (__predict_true(RELOC_ALIGNED_P(where))) {
234 tmp = *where + (Elf_Addr)defobj->relocbase +
238 tmp = load_ptr(where) +
239 (Elf_Addr)defobj->relocbase +
241 store_ptr(where, tmp);
243 dbg("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
244 obj->strtab + obj->symtab[symnum].st_name,
245 obj->path, (void *)tmp, where, defobj->path);
248 case R_ARM_RELATIVE: /* word32 B + A */
249 if (__predict_true(RELOC_ALIGNED_P(where))) {
250 tmp = *where + (Elf_Addr)obj->relocbase;
253 tmp = load_ptr(where) +
254 (Elf_Addr)obj->relocbase;
255 store_ptr(where, tmp);
257 dbg("RELATIVE in %s --> %p", obj->path,
263 * These are deferred until all other relocations have
264 * been done. All we do here is make sure that the
265 * COPY relocation is not in a shared library. They
266 * are allowed only in executable files.
268 if (!obj->mainprog) {
270 "%s: Unexpected R_COPY relocation in shared library",
274 dbg("COPY (avoid in main)");
277 case R_ARM_TLS_DTPOFF32:
278 def = find_symdef(symnum, obj, &defobj, flags, cache,
283 tmp = (Elf_Addr)(def->st_value);
284 if (__predict_true(RELOC_ALIGNED_P(where)))
287 store_ptr(where, tmp);
289 dbg("TLS_DTPOFF32 %s in %s --> %p",
290 obj->strtab + obj->symtab[symnum].st_name,
291 obj->path, (void *)tmp);
294 case R_ARM_TLS_DTPMOD32:
295 def = find_symdef(symnum, obj, &defobj, flags, cache,
300 tmp = (Elf_Addr)(defobj->tlsindex);
301 if (__predict_true(RELOC_ALIGNED_P(where)))
304 store_ptr(where, tmp);
306 dbg("TLS_DTPMOD32 %s in %s --> %p",
307 obj->strtab + obj->symtab[symnum].st_name,
308 obj->path, (void *)tmp);
312 case R_ARM_TLS_TPOFF32:
313 def = find_symdef(symnum, obj, &defobj, flags, cache,
318 if (!defobj->tls_done && allocate_tls_offset(obj))
322 tmp = (Elf_Addr)def->st_value + defobj->tlsoffset +
324 if (__predict_true(RELOC_ALIGNED_P(where)))
327 store_ptr(where, tmp);
328 dbg("TLS_TPOFF32 %s in %s --> %p",
329 obj->strtab + obj->symtab[symnum].st_name,
330 obj->path, (void *)tmp);
335 dbg("sym = %lu, type = %lu, offset = %p, "
336 "contents = %p, symbol = %s",
337 symnum, (u_long)ELF_R_TYPE(rel->r_info),
338 (void *)rel->r_offset, (void *)load_ptr(where),
339 obj->strtab + obj->symtab[symnum].st_name);
340 _rtld_error("%s: Unsupported relocation type %ld "
341 "in non-PLT relocations\n",
342 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
349 * * Process non-PLT relocations
352 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
353 RtldLockState *lockstate)
355 const Elf_Rel *rellim;
360 /* The relocation for the dynamic loader has already been done. */
363 if ((flags & SYMLOOK_IFUNC) != 0)
364 /* XXX not implemented */
368 * The dynamic loader may be called from a thread, we have
369 * limited amounts of stack available so we cannot use alloca().
371 cache = calloc(obj->dynsymcount, sizeof(SymCache));
372 /* No need to check for NULL here */
374 rellim = (const Elf_Rel *)((caddr_t)obj->rel + obj->relsize);
375 for (rel = obj->rel; rel < rellim; rel++) {
376 if (reloc_nonplt_object(obj, rel, cache, flags, lockstate) < 0)
387 * * Process the PLT relocations.
390 reloc_plt(Obj_Entry *obj)
392 const Elf_Rel *rellim;
395 rellim = (const Elf_Rel *)((char *)obj->pltrel +
397 for (rel = obj->pltrel; rel < rellim; rel++) {
400 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
402 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
403 *where += (Elf_Addr )obj->relocbase;
410 * * LD_BIND_NOW was set - force relocation for all jump slots
413 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
415 const Obj_Entry *defobj;
416 const Elf_Rel *rellim;
422 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
423 for (rel = obj->pltrel; rel < rellim; rel++) {
424 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
425 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
426 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
427 SYMLOOK_IN_PLT | flags, NULL, lockstate);
429 dbg("reloc_jmpslots: sym not found");
433 target = (Elf_Addr)(defobj->relocbase + def->st_value);
434 reloc_jmpslot(where, target, defobj, obj,
435 (const Elf_Rel *) rel);
438 obj->jmpslots_done = true;
444 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
447 /* XXX not implemented */
452 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
453 struct Struct_RtldLockState *lockstate)
456 /* XXX not implemented */
461 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
462 const Obj_Entry *obj, const Elf_Rel *rel)
465 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
467 if (*where != target)
474 allocate_initial_tls(Obj_Entry *objs)
476 #ifdef ARM_TP_ADDRESS
477 void **_tp = (void **)ARM_TP_ADDRESS;
481 * Fix the size of the static TLS block by using the maximum
482 * offset allocated so far and adding a bit for dynamic modules to
486 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
488 #ifdef ARM_TP_ADDRESS
489 (*_tp) = (void *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
491 sysarch(ARM_SET_TP, allocate_tls(objs, NULL, TLS_TCB_SIZE, 8));
496 __tls_get_addr(tls_index* ti)
499 #ifdef ARM_TP_ADDRESS
500 void **_tp = (void **)ARM_TP_ADDRESS;
502 p = tls_get_addr_common((Elf_Addr **)(*_tp), ti->ti_module, ti->ti_offset);
505 __asm __volatile("mrc p15, 0, %0, c13, c0, 3" \
507 p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);