1 /* $NetBSD: mdreloc.c,v 1.23 2003/07/26 15:04:38 mrg Exp $ */
15 #include "machine/sysarch.h"
22 arm_abi_variant_hook(Elf_Auxinfo **aux_info)
28 * If we're running an old kernel that doesn't provide any data fail
29 * safe by doing nothing.
31 if (aux_info[AT_EHDRFLAGS] == NULL)
33 ehdr = aux_info[AT_EHDRFLAGS]->a_un.a_val;
36 * Hard float ABI binaries are the default, and use the default paths
39 if ((ehdr & EF_ARM_VFP_FLOAT) != 0)
43 * If there's no /usr/libsoft, then we don't have a system with both
44 * hard and soft float. In that case, hope for the best and just
45 * return. Such systems are required to have all soft or all hard
46 * float ABI binaries and libraries. This is, at best, a transition
47 * compatibility hack. Once we're fully hard-float, this should
50 if (stat("/usr/libsoft", &sb) != 0 || !S_ISDIR(sb.st_mode))
54 * This is a soft float ABI binary. We need to use the soft float
57 ld_elf_hints_default = _PATH_SOFT_ELF_HINTS;
58 ld_path_libmap_conf = _PATH_SOFT_LIBMAP_CONF;
59 ld_path_rtld = _PATH_SOFT_RTLD;
60 ld_standard_library_path = SOFT_STANDARD_LIBRARY_PATH;
61 ld_env_prefix = LD_SOFT_;
65 init_pltgot(Obj_Entry *obj)
67 if (obj->pltgot != NULL) {
68 obj->pltgot[1] = (Elf_Addr) obj;
69 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
74 do_copy_relocations(Obj_Entry *dstobj)
76 const Elf_Rel *rellim;
79 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
81 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
82 for (rel = dstobj->rel; rel < rellim; rel++) {
83 if (ELF_R_TYPE(rel->r_info) == R_ARM_COPY) {
85 const Elf_Sym *dstsym;
89 const Elf_Sym *srcsym;
90 const Obj_Entry *srcobj, *defobj;
94 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
95 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
96 name = dstobj->strtab + dstsym->st_name;
97 size = dstsym->st_size;
99 symlook_init(&req, name);
100 req.ventry = fetch_ventry(dstobj,
101 ELF_R_SYM(rel->r_info));
102 req.flags = SYMLOOK_EARLY;
104 for (srcobj = globallist_next(dstobj); srcobj != NULL;
105 srcobj = globallist_next(srcobj)) {
106 res = symlook_obj(&req, srcobj);
108 srcsym = req.sym_out;
109 defobj = req.defobj_out;
113 if (srcobj == NULL) {
115 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
120 srcaddr = (const void *)(defobj->relocbase +
122 memcpy(dstaddr, srcaddr, size);
128 void _rtld_bind_start(void);
129 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
134 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
136 const Elf_Rel *rel = NULL, *rellim;
141 for (; dynp->d_tag != DT_NULL; dynp++) {
142 switch (dynp->d_tag) {
144 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
147 relsz = dynp->d_un.d_val;
151 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
152 size = (rellim - 1)->r_offset - rel->r_offset;
153 for (; rel < rellim; rel++) {
154 where = (Elf_Addr *)(relocbase + rel->r_offset);
156 *where += (Elf_Addr)relocbase;
160 * It is possible for the compiler to emit relocations for unaligned data.
161 * We handle this situation with these inlines.
163 #define RELOC_ALIGNED_P(x) \
164 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
166 static __inline Elf_Addr
167 load_ptr(void *where)
171 memcpy(&res, where, sizeof(res));
177 store_ptr(void *where, Elf_Addr val)
180 memcpy(where, &val, sizeof(val));
184 reloc_nonplt_object(Obj_Entry *obj, const Elf_Rel *rel, SymCache *cache,
185 int flags, RtldLockState *lockstate)
189 const Obj_Entry *defobj;
191 unsigned long symnum;
193 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
194 symnum = ELF_R_SYM(rel->r_info);
196 switch (ELF_R_TYPE(rel->r_info)) {
200 #if 1 /* XXX should not occur */
201 case R_ARM_PC24: { /* word32 S - P + A */
205 * Extract addend and sign-extend if needed.
208 if (addend & 0x00800000)
209 addend |= 0xff000000;
211 def = find_symdef(symnum, obj, &defobj, flags, cache,
215 tmp = (Elf_Addr)obj->relocbase + def->st_value
216 - (Elf_Addr)where + (addend << 2);
217 if ((tmp & 0xfe000000) != 0xfe000000 &&
218 (tmp & 0xfe000000) != 0) {
220 "%s: R_ARM_PC24 relocation @ %p to %s failed "
221 "(displacement %ld (%#lx) out of range)",
223 obj->strtab + obj->symtab[symnum].st_name,
224 (long) tmp, (long) tmp);
228 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
229 dbg("PC24 %s in %s --> %p @ %p in %s",
230 obj->strtab + obj->symtab[symnum].st_name,
231 obj->path, (void *)*where, where, defobj->path);
236 case R_ARM_ABS32: /* word32 B + S + A */
237 case R_ARM_GLOB_DAT: /* word32 B + S */
238 def = find_symdef(symnum, obj, &defobj, flags, cache,
242 if (__predict_true(RELOC_ALIGNED_P(where))) {
243 tmp = *where + (Elf_Addr)defobj->relocbase +
247 tmp = load_ptr(where) +
248 (Elf_Addr)defobj->relocbase +
250 store_ptr(where, tmp);
252 dbg("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
253 obj->strtab + obj->symtab[symnum].st_name,
254 obj->path, (void *)tmp, where, defobj->path);
257 case R_ARM_RELATIVE: /* word32 B + A */
258 if (__predict_true(RELOC_ALIGNED_P(where))) {
259 tmp = *where + (Elf_Addr)obj->relocbase;
262 tmp = load_ptr(where) +
263 (Elf_Addr)obj->relocbase;
264 store_ptr(where, tmp);
266 dbg("RELATIVE in %s --> %p", obj->path,
272 * These are deferred until all other relocations have
273 * been done. All we do here is make sure that the
274 * COPY relocation is not in a shared library. They
275 * are allowed only in executable files.
277 if (!obj->mainprog) {
279 "%s: Unexpected R_COPY relocation in shared library",
283 dbg("COPY (avoid in main)");
286 case R_ARM_TLS_DTPOFF32:
287 def = find_symdef(symnum, obj, &defobj, flags, cache,
292 tmp = (Elf_Addr)(def->st_value);
293 if (__predict_true(RELOC_ALIGNED_P(where)))
296 store_ptr(where, tmp);
298 dbg("TLS_DTPOFF32 %s in %s --> %p",
299 obj->strtab + obj->symtab[symnum].st_name,
300 obj->path, (void *)tmp);
303 case R_ARM_TLS_DTPMOD32:
304 def = find_symdef(symnum, obj, &defobj, flags, cache,
309 tmp = (Elf_Addr)(defobj->tlsindex);
310 if (__predict_true(RELOC_ALIGNED_P(where)))
313 store_ptr(where, tmp);
315 dbg("TLS_DTPMOD32 %s in %s --> %p",
316 obj->strtab + obj->symtab[symnum].st_name,
317 obj->path, (void *)tmp);
321 case R_ARM_TLS_TPOFF32:
322 def = find_symdef(symnum, obj, &defobj, flags, cache,
327 if (!defobj->tls_done && allocate_tls_offset(obj))
331 tmp = (Elf_Addr)def->st_value + defobj->tlsoffset +
333 if (__predict_true(RELOC_ALIGNED_P(where)))
336 store_ptr(where, tmp);
337 dbg("TLS_TPOFF32 %s in %s --> %p",
338 obj->strtab + obj->symtab[symnum].st_name,
339 obj->path, (void *)tmp);
344 dbg("sym = %lu, type = %lu, offset = %p, "
345 "contents = %p, symbol = %s",
346 symnum, (u_long)ELF_R_TYPE(rel->r_info),
347 (void *)rel->r_offset, (void *)load_ptr(where),
348 obj->strtab + obj->symtab[symnum].st_name);
349 _rtld_error("%s: Unsupported relocation type %ld "
350 "in non-PLT relocations\n",
351 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
358 * * Process non-PLT relocations
361 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
362 RtldLockState *lockstate)
364 const Elf_Rel *rellim;
369 /* The relocation for the dynamic loader has already been done. */
372 if ((flags & SYMLOOK_IFUNC) != 0)
373 /* XXX not implemented */
377 * The dynamic loader may be called from a thread, we have
378 * limited amounts of stack available so we cannot use alloca().
380 cache = calloc(obj->dynsymcount, sizeof(SymCache));
381 /* No need to check for NULL here */
383 rellim = (const Elf_Rel *)((caddr_t)obj->rel + obj->relsize);
384 for (rel = obj->rel; rel < rellim; rel++) {
385 if (reloc_nonplt_object(obj, rel, cache, flags, lockstate) < 0)
396 * * Process the PLT relocations.
399 reloc_plt(Obj_Entry *obj)
401 const Elf_Rel *rellim;
404 rellim = (const Elf_Rel *)((char *)obj->pltrel +
406 for (rel = obj->pltrel; rel < rellim; rel++) {
409 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
411 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
412 *where += (Elf_Addr )obj->relocbase;
419 * * LD_BIND_NOW was set - force relocation for all jump slots
422 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
424 const Obj_Entry *defobj;
425 const Elf_Rel *rellim;
431 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
432 for (rel = obj->pltrel; rel < rellim; rel++) {
433 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
434 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
435 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
436 SYMLOOK_IN_PLT | flags, NULL, lockstate);
438 dbg("reloc_jmpslots: sym not found");
442 target = (Elf_Addr)(defobj->relocbase + def->st_value);
443 reloc_jmpslot(where, target, defobj, obj,
444 (const Elf_Rel *) rel);
447 obj->jmpslots_done = true;
453 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
456 /* XXX not implemented */
461 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
462 struct Struct_RtldLockState *lockstate)
465 /* XXX not implemented */
470 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
471 const Obj_Entry *obj, const Elf_Rel *rel)
474 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
476 if (*where != target)
483 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
488 allocate_initial_tls(Obj_Entry *objs)
490 #ifdef ARM_TP_ADDRESS
491 void **_tp = (void **)ARM_TP_ADDRESS;
495 * Fix the size of the static TLS block by using the maximum
496 * offset allocated so far and adding a bit for dynamic modules to
500 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
502 #ifdef ARM_TP_ADDRESS
503 (*_tp) = (void *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
505 sysarch(ARM_SET_TP, allocate_tls(objs, NULL, TLS_TCB_SIZE, 8));
510 __tls_get_addr(tls_index* ti)
513 #ifdef ARM_TP_ADDRESS
514 void **_tp = (void **)ARM_TP_ADDRESS;
516 p = tls_get_addr_common((Elf_Addr **)(*_tp), ti->ti_module, ti->ti_offset);
519 __asm __volatile("mrc p15, 0, %0, c13, c0, 3" \
521 p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);