1 /* $NetBSD: mdreloc.c,v 1.23 2003/07/26 15:04:38 mrg Exp $ */
15 #include "machine/sysarch.h"
23 * On processors that have hard floating point supported, we also support
24 * running soft float binaries. If we're being built with hard float support,
25 * check the ELF headers to make sure that this is a hard float binary. If it is
26 * a soft float binary, force the dynamic linker to use the alternative soft
30 arm_abi_variant_hook(Elf_Auxinfo **aux_info)
35 * If we're running an old kernel that doesn't provide any data fail
36 * safe by doing nothing.
38 if (aux_info[AT_EHDRFLAGS] == NULL)
40 ehdr = aux_info[AT_EHDRFLAGS]->a_un.a_val;
43 * Hard float ABI binaries are the default, and use the default paths
46 if ((ehdr & EF_ARM_VFP_FLOAT) != 0)
50 * This is a soft float ABI binary. We need to use the soft float
53 ld_elf_hints_default = _PATH_SOFT_ELF_HINTS;
54 ld_path_libmap_conf = _PATH_SOFT_LIBMAP_CONF;
55 ld_path_rtld = _PATH_SOFT_RTLD;
56 ld_standard_library_path = SOFT_STANDARD_LIBRARY_PATH;
57 ld_env_prefix = LD_SOFT_;
62 init_pltgot(Obj_Entry *obj)
64 if (obj->pltgot != NULL) {
65 obj->pltgot[1] = (Elf_Addr) obj;
66 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
71 do_copy_relocations(Obj_Entry *dstobj)
73 const Elf_Rel *rellim;
76 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */
78 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize);
79 for (rel = dstobj->rel; rel < rellim; rel++) {
80 if (ELF_R_TYPE(rel->r_info) == R_ARM_COPY) {
82 const Elf_Sym *dstsym;
86 const Elf_Sym *srcsym;
87 const Obj_Entry *srcobj, *defobj;
91 dstaddr = (void *) (dstobj->relocbase + rel->r_offset);
92 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
93 name = dstobj->strtab + dstsym->st_name;
94 size = dstsym->st_size;
96 symlook_init(&req, name);
97 req.ventry = fetch_ventry(dstobj,
98 ELF_R_SYM(rel->r_info));
99 req.flags = SYMLOOK_EARLY;
101 for (srcobj = globallist_next(dstobj); srcobj != NULL;
102 srcobj = globallist_next(srcobj)) {
103 res = symlook_obj(&req, srcobj);
105 srcsym = req.sym_out;
106 defobj = req.defobj_out;
110 if (srcobj == NULL) {
112 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
117 srcaddr = (const void *)(defobj->relocbase +
119 memcpy(dstaddr, srcaddr, size);
125 void _rtld_bind_start(void);
126 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
131 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
133 const Elf_Rel *rel = NULL, *rellim;
138 for (; dynp->d_tag != DT_NULL; dynp++) {
139 switch (dynp->d_tag) {
141 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
144 relsz = dynp->d_un.d_val;
148 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
149 size = (rellim - 1)->r_offset - rel->r_offset;
150 for (; rel < rellim; rel++) {
151 where = (Elf_Addr *)(relocbase + rel->r_offset);
153 *where += (Elf_Addr)relocbase;
157 * It is possible for the compiler to emit relocations for unaligned data.
158 * We handle this situation with these inlines.
160 #define RELOC_ALIGNED_P(x) \
161 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
163 static __inline Elf_Addr
164 load_ptr(void *where)
168 memcpy(&res, where, sizeof(res));
174 store_ptr(void *where, Elf_Addr val)
177 memcpy(where, &val, sizeof(val));
181 reloc_nonplt_object(Obj_Entry *obj, const Elf_Rel *rel, SymCache *cache,
182 int flags, RtldLockState *lockstate)
186 const Obj_Entry *defobj;
188 unsigned long symnum;
190 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
191 symnum = ELF_R_SYM(rel->r_info);
193 switch (ELF_R_TYPE(rel->r_info)) {
197 #if 1 /* XXX should not occur */
198 case R_ARM_PC24: { /* word32 S - P + A */
202 * Extract addend and sign-extend if needed.
205 if (addend & 0x00800000)
206 addend |= 0xff000000;
208 def = find_symdef(symnum, obj, &defobj, flags, cache,
212 tmp = (Elf_Addr)obj->relocbase + def->st_value
213 - (Elf_Addr)where + (addend << 2);
214 if ((tmp & 0xfe000000) != 0xfe000000 &&
215 (tmp & 0xfe000000) != 0) {
217 "%s: R_ARM_PC24 relocation @ %p to %s failed "
218 "(displacement %ld (%#lx) out of range)",
220 obj->strtab + obj->symtab[symnum].st_name,
221 (long) tmp, (long) tmp);
225 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
226 dbg("PC24 %s in %s --> %p @ %p in %s",
227 obj->strtab + obj->symtab[symnum].st_name,
228 obj->path, (void *)*where, where, defobj->path);
233 case R_ARM_ABS32: /* word32 B + S + A */
234 case R_ARM_GLOB_DAT: /* word32 B + S */
235 def = find_symdef(symnum, obj, &defobj, flags, cache,
239 if (__predict_true(RELOC_ALIGNED_P(where))) {
240 tmp = *where + (Elf_Addr)defobj->relocbase +
244 tmp = load_ptr(where) +
245 (Elf_Addr)defobj->relocbase +
247 store_ptr(where, tmp);
249 dbg("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
250 obj->strtab + obj->symtab[symnum].st_name,
251 obj->path, (void *)tmp, where, defobj->path);
254 case R_ARM_RELATIVE: /* word32 B + A */
255 if (__predict_true(RELOC_ALIGNED_P(where))) {
256 tmp = *where + (Elf_Addr)obj->relocbase;
259 tmp = load_ptr(where) +
260 (Elf_Addr)obj->relocbase;
261 store_ptr(where, tmp);
263 dbg("RELATIVE in %s --> %p", obj->path,
269 * These are deferred until all other relocations have
270 * been done. All we do here is make sure that the
271 * COPY relocation is not in a shared library. They
272 * are allowed only in executable files.
274 if (!obj->mainprog) {
276 "%s: Unexpected R_COPY relocation in shared library",
280 dbg("COPY (avoid in main)");
283 case R_ARM_TLS_DTPOFF32:
284 def = find_symdef(symnum, obj, &defobj, flags, cache,
289 tmp = (Elf_Addr)(def->st_value);
290 if (__predict_true(RELOC_ALIGNED_P(where)))
293 store_ptr(where, tmp);
295 dbg("TLS_DTPOFF32 %s in %s --> %p",
296 obj->strtab + obj->symtab[symnum].st_name,
297 obj->path, (void *)tmp);
300 case R_ARM_TLS_DTPMOD32:
301 def = find_symdef(symnum, obj, &defobj, flags, cache,
306 tmp = (Elf_Addr)(defobj->tlsindex);
307 if (__predict_true(RELOC_ALIGNED_P(where)))
310 store_ptr(where, tmp);
312 dbg("TLS_DTPMOD32 %s in %s --> %p",
313 obj->strtab + obj->symtab[symnum].st_name,
314 obj->path, (void *)tmp);
318 case R_ARM_TLS_TPOFF32:
319 def = find_symdef(symnum, obj, &defobj, flags, cache,
324 if (!defobj->tls_done && allocate_tls_offset(obj))
328 tmp = (Elf_Addr)def->st_value + defobj->tlsoffset +
330 if (__predict_true(RELOC_ALIGNED_P(where)))
333 store_ptr(where, tmp);
334 dbg("TLS_TPOFF32 %s in %s --> %p",
335 obj->strtab + obj->symtab[symnum].st_name,
336 obj->path, (void *)tmp);
341 dbg("sym = %lu, type = %lu, offset = %p, "
342 "contents = %p, symbol = %s",
343 symnum, (u_long)ELF_R_TYPE(rel->r_info),
344 (void *)rel->r_offset, (void *)load_ptr(where),
345 obj->strtab + obj->symtab[symnum].st_name);
346 _rtld_error("%s: Unsupported relocation type %ld "
347 "in non-PLT relocations\n",
348 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
355 * * Process non-PLT relocations
358 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
359 RtldLockState *lockstate)
361 const Elf_Rel *rellim;
366 /* The relocation for the dynamic loader has already been done. */
369 if ((flags & SYMLOOK_IFUNC) != 0)
370 /* XXX not implemented */
374 * The dynamic loader may be called from a thread, we have
375 * limited amounts of stack available so we cannot use alloca().
377 cache = calloc(obj->dynsymcount, sizeof(SymCache));
378 /* No need to check for NULL here */
380 rellim = (const Elf_Rel *)((caddr_t)obj->rel + obj->relsize);
381 for (rel = obj->rel; rel < rellim; rel++) {
382 if (reloc_nonplt_object(obj, rel, cache, flags, lockstate) < 0)
393 * * Process the PLT relocations.
396 reloc_plt(Obj_Entry *obj)
398 const Elf_Rel *rellim;
401 rellim = (const Elf_Rel *)((char *)obj->pltrel +
403 for (rel = obj->pltrel; rel < rellim; rel++) {
406 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
408 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
409 *where += (Elf_Addr )obj->relocbase;
416 * * LD_BIND_NOW was set - force relocation for all jump slots
419 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
421 const Obj_Entry *defobj;
422 const Elf_Rel *rellim;
428 rellim = (const Elf_Rel *)((char *)obj->pltrel + obj->pltrelsize);
429 for (rel = obj->pltrel; rel < rellim; rel++) {
430 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
431 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
432 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
433 SYMLOOK_IN_PLT | flags, NULL, lockstate);
435 dbg("reloc_jmpslots: sym not found");
439 target = (Elf_Addr)(defobj->relocbase + def->st_value);
440 reloc_jmpslot(where, target, defobj, obj,
441 (const Elf_Rel *) rel);
444 obj->jmpslots_done = true;
450 reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
453 /* XXX not implemented */
458 reloc_gnu_ifunc(Obj_Entry *obj, int flags,
459 struct Struct_RtldLockState *lockstate)
462 /* XXX not implemented */
467 reloc_jmpslot(Elf_Addr *where, Elf_Addr target, const Obj_Entry *defobj,
468 const Obj_Entry *obj, const Elf_Rel *rel)
471 assert(ELF_R_TYPE(rel->r_info) == R_ARM_JUMP_SLOT);
473 if (*where != target && !ld_bind_not)
479 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
491 allocate_initial_tls(Obj_Entry *objs)
493 #ifdef ARM_TP_ADDRESS
494 void **_tp = (void **)ARM_TP_ADDRESS;
498 * Fix the size of the static TLS block by using the maximum
499 * offset allocated so far and adding a bit for dynamic modules to
503 tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
505 #ifdef ARM_TP_ADDRESS
506 (*_tp) = (void *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
508 sysarch(ARM_SET_TP, allocate_tls(objs, NULL, TLS_TCB_SIZE, 8));
513 __tls_get_addr(tls_index* ti)
516 #ifdef ARM_TP_ADDRESS
517 void **_tp = (void **)ARM_TP_ADDRESS;
519 p = tls_get_addr_common((Elf_Addr **)(*_tp), ti->ti_module, ti->ti_offset);
522 __asm __volatile("mrc p15, 0, %0, c13, c0, 3" \
524 p = tls_get_addr_common((Elf_Addr **)(_tp), ti->ti_module, ti->ti_offset);