(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / arm / dl-machine.h
blob761f8daeaabe75e002285c9f9e429c7200c291d6
1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef dl_machine_h
22 #define dl_machine_h
24 #define ELF_MACHINE_NAME "ARM"
26 #include <sys/param.h>
28 #define VALID_ELF_ABIVERSION(ver) (ver == 0)
29 #define VALID_ELF_OSABI(osabi) \
30 (osabi == ELFOSABI_SYSV || osabi == ELFOSABI_ARM)
31 #define VALID_ELF_HEADER(hdr,exp,size) \
32 memcmp (hdr,exp,size-2) == 0 \
33 && VALID_ELF_OSABI (hdr[EI_OSABI]) \
34 && VALID_ELF_ABIVERSION (hdr[EI_ABIVERSION])
36 #define CLEAR_CACHE(BEG,END) \
37 { \
38 register unsigned long _beg __asm ("a1") = (unsigned long)(BEG); \
39 register unsigned long _end __asm ("a2") = (unsigned long)(END); \
40 register unsigned long _flg __asm ("a3") = 0; \
41 __asm __volatile ("swi 0x9f0002 @ sys_cacheflush" \
42 : /* no outputs */ \
43 : /* no inputs */ \
44 : "a1"); \
47 /* Return nonzero iff ELF header is compatible with the running host. */
48 static inline int __attribute__ ((unused))
49 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
51 return ehdr->e_machine == EM_ARM;
55 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
56 first element of the GOT. This must be inlined in a function which
57 uses global data. */
58 static inline Elf32_Addr __attribute__ ((unused))
59 elf_machine_dynamic (void)
61 register Elf32_Addr *got asm ("r10");
62 return *got;
66 /* Return the run-time load address of the shared object. */
67 static inline Elf32_Addr __attribute__ ((unused))
68 elf_machine_load_address (void)
70 extern void __dl_start asm ("_dl_start");
71 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
72 Elf32_Addr pcrel_addr;
73 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
74 return pcrel_addr - got_addr;
78 /* Set up the loaded object described by L so its unrelocated PLT
79 entries will jump to the on-demand fixup code in dl-runtime.c. */
81 static inline int __attribute__ ((unused))
82 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
84 Elf32_Addr *got;
85 extern void _dl_runtime_resolve (Elf32_Word);
86 extern void _dl_runtime_profile (Elf32_Word);
88 if (l->l_info[DT_JMPREL] && lazy)
90 /* patb: this is different than i386 */
91 /* The GOT entries for functions in the PLT have not yet been filled
92 in. Their initial contents will arrange when called to push an
93 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
94 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
95 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
96 /* If a library is prelinked but we have to relocate anyway,
97 we have to be able to undo the prelinking of .got.plt.
98 The prelinker saved us here address of .plt. */
99 if (got[1])
100 l->l_mach.plt = got[1] + l->l_addr;
101 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
103 /* The got[2] entry contains the address of a function which gets
104 called to get the address of a so far unresolved function and
105 jump to it. The profiling extension of the dynamic linker allows
106 to intercept the calls to collect information. In this case we
107 don't store the address in the GOT so that all future calls also
108 end in this function. */
109 if (profile)
111 got[2] = (Elf32_Addr) &_dl_runtime_profile;
113 if (_dl_name_match_p (GLRO(dl_profile), l))
114 /* Say that we really want profiling and the timers are
115 started. */
116 GL(dl_profile_map) = l;
118 else
119 /* This function will get called to fix up the GOT entry indicated by
120 the offset on the stack, and then jump to the resolved address. */
121 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
123 return lazy;
126 #if defined(__USE_BX__)
127 #define BX(x) "bx\t" #x
128 #else
129 #define BX(x) "mov\tpc, " #x
130 #endif
132 #ifndef PROF
133 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
134 .text\n\
135 .globl _dl_runtime_resolve\n\
136 .type _dl_runtime_resolve, #function\n\
137 .align 2\n\
138 _dl_runtime_resolve:\n\
139 @ we get called with\n\
140 @ stack[0] contains the return address from this call\n\
141 @ ip contains &GOT[n+3] (pointer to function)\n\
142 @ lr points to &GOT[2]\n\
144 @ stack arguments\n\
145 stmdb sp!,{r0-r3}\n\
147 @ get pointer to linker struct\n\
148 ldr r0, [lr, #-4]\n\
150 @ prepare to call fixup()\n\
151 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each\n\
152 sub r1, ip, lr\n\
153 sub r1, r1, #4\n\
154 add r1, r1, r1\n\
156 @ call fixup routine\n\
157 bl fixup\n\
159 @ save the return\n\
160 mov ip, r0\n\
162 @ get arguments and return address back\n\
163 ldmia sp!, {r0-r3,lr}\n\
165 @ jump to the newly found address\n\
166 " BX(ip) "\n\
168 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
170 .globl _dl_runtime_profile\n\
171 .type _dl_runtime_profile, #function\n\
172 .align 2\n\
173 _dl_runtime_profile:\n\
174 @ stack arguments\n\
175 stmdb sp!, {r0-r3}\n\
177 @ get pointer to linker struct\n\
178 ldr r0, [lr, #-4]\n\
180 @ prepare to call fixup()\n\
181 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each\n\
182 sub r1, ip, lr\n\
183 sub r1, r1, #4\n\
184 add r1, r1, r1\n\
186 @ call profiling fixup routine\n\
187 bl profile_fixup\n\
189 @ save the return\n\
190 mov ip, r0\n\
192 @ get arguments and return address back\n\
193 ldmia sp!, {r0-r3,lr}\n\
195 @ jump to the newly found address\n\
196 " BX(ip) "\n\
198 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
199 .previous\n\
201 #else // PROF
202 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
203 .text\n\
204 .globl _dl_runtime_resolve\n\
205 .globl _dl_runtime_profile\n\
206 .type _dl_runtime_resolve, #function\n\
207 .type _dl_runtime_profile, #function\n\
208 .align 2\n\
209 _dl_runtime_resolve:\n\
210 _dl_runtime_profile:\n\
211 @ we get called with\n\
212 @ stack[0] contains the return address from this call\n\
213 @ ip contains &GOT[n+3] (pointer to function)\n\
214 @ lr points to &GOT[2]\n\
216 @ stack arguments\n\
217 stmdb sp!, {r0-r3}\n\
219 @ get pointer to linker struct\n\
220 ldr r0, [lr, #-4]\n\
222 @ prepare to call fixup()\n\
223 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each\n\
224 sub r1, ip, lr\n\
225 sub r1, r1, #4\n\
226 add r1, r1, r1\n\
228 @ call profiling fixup routine\n\
229 bl fixup\n\
231 @ save the return\n\
232 mov ip, r0\n\
234 @ get arguments and return address back\n\
235 ldmia sp!, {r0-r3,lr}\n\
237 @ jump to the newly found address\n\
238 " BX(ip) "\n\
240 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
241 .previous\n\
243 #endif //PROF
245 /* Mask identifying addresses reserved for the user program,
246 where the dynamic linker should not map anything. */
247 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
249 /* Initial entry point code for the dynamic linker.
250 The C function `_dl_start' is the real entry point;
251 its return value is the user program's entry point. */
253 #define RTLD_START asm ("\
254 .text\n\
255 .globl _start\n\
256 .globl _dl_start_user\n\
257 _start:\n\
258 @ we are PIC code, so get global offset table\n\
259 ldr sl, .L_GET_GOT\n\
260 @ See if we were run as a command with the executable file\n\
261 @ name as an extra leading argument.\n\
262 ldr r4, .L_SKIP_ARGS\n\
263 @ at start time, all the args are on the stack\n\
264 mov r0, sp\n\
265 bl _dl_start\n\
266 @ returns user entry point in r0\n\
267 _dl_start_user:\n\
268 add sl, pc, sl\n\
269 .L_GOT_GOT:\n\
270 ldr r4, [sl, r4]\n\
271 @ get the original arg count\n\
272 ldr r1, [sp]\n\
273 @ save the entry point in another register\n\
274 mov r6, r0\n\
275 @ adjust the stack pointer to skip the extra args\n\
276 add sp, sp, r4, lsl #2\n\
277 @ subtract _dl_skip_args from original arg count\n\
278 sub r1, r1, r4\n\
279 @ get the argv address\n\
280 add r2, sp, #4\n\
281 @ store the new argc in the new stack location\n\
282 str r1, [sp]\n\
283 @ compute envp\n\
284 add r3, r2, r1, lsl #2\n\
285 add r3, r3, #4\n\
287 @ now we call _dl_init\n\
288 ldr r0, .L_LOADED\n\
289 ldr r0, [sl, r0]\n\
290 @ call _dl_init\n\
291 bl _dl_init_internal(PLT)\n\
292 @ load the finalizer function\n\
293 ldr r0, .L_FINI_PROC\n\
294 add r0, sl, r0\n\
295 @ jump to the user_s entry point\n\
296 " BX(r6) "\n\
297 .L_GET_GOT:\n\
298 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4\n\
299 .L_SKIP_ARGS:\n\
300 .word _dl_skip_args(GOTOFF)\n\
301 .L_FINI_PROC:\n\
302 .word _dl_fini(GOTOFF)\n\
303 .L_LOADED:\n\
304 .word _rtld_local(GOTOFF)\n\
305 .previous\n\
308 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
309 PLT entries should not be allowed to define the value.
310 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
311 of the main executable's symbols, as for a COPY reloc. */
312 #define elf_machine_type_class(type) \
313 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
314 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
316 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
317 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
319 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
320 Prelinked libraries may use Elf32_Rela though. */
321 #define ELF_MACHINE_PLT_REL 1
323 /* We define an initialization functions. This is called very early in
324 _dl_sysdep_start. */
325 #define DL_PLATFORM_INIT dl_platform_init ()
327 static inline void __attribute__ ((unused))
328 dl_platform_init (void)
330 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
331 /* Avoid an empty string which would disturb us. */
332 GLRO(dl_platform) = NULL;
335 static inline Elf32_Addr
336 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
337 const Elf32_Rel *reloc,
338 Elf32_Addr *reloc_addr, Elf32_Addr value)
340 return *reloc_addr = value;
343 /* Return the final value of a plt relocation. */
344 static inline Elf32_Addr
345 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
346 Elf32_Addr value)
348 return value;
351 #endif /* !dl_machine_h */
353 #ifdef RESOLVE
355 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
356 Prelinked libraries may use Elf32_Rela though. */
357 # ifdef RTLD_BOOTSTRAP
358 # define ELF_MACHINE_NO_RELA 1
359 # endif
361 /* Deal with an out-of-range PC24 reloc. */
362 static Elf32_Addr
363 fix_bad_pc24 (Elf32_Addr *const reloc_addr, Elf32_Addr value)
365 static void *fix_page;
366 static unsigned int fix_offset;
367 static size_t pagesize;
368 Elf32_Word *fix_address;
370 if (! fix_page)
372 if (! pagesize)
373 pagesize = getpagesize ();
374 fix_page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC,
375 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
376 if (! fix_page)
377 assert (! "could not map page for fixup");
378 fix_offset = 0;
381 fix_address = (Elf32_Word *)(fix_page + fix_offset);
382 fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
383 fix_address[1] = value;
385 fix_offset += 8;
386 if (fix_offset >= pagesize)
387 fix_page = NULL;
389 return (Elf32_Addr)fix_address;
392 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
393 MAP is the object containing the reloc. */
395 static inline void
396 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
397 const Elf32_Sym *sym, const struct r_found_version *version,
398 void *const reloc_addr_arg)
400 Elf32_Addr *const reloc_addr = reloc_addr_arg;
401 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
403 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
404 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
406 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
407 /* This is defined in rtld.c, but nowhere in the static libc.a;
408 make the reference weak so static programs can still link.
409 This declaration cannot be done when compiling rtld.c
410 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
411 common defn for _dl_rtld_map, which is incompatible with a
412 weak decl in the same file. */
413 # ifndef SHARED
414 weak_extern (_dl_rtld_map);
415 # endif
416 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
417 # endif
418 *reloc_addr += map->l_addr;
420 # ifndef RTLD_BOOTSTRAP
421 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
422 return;
423 # endif
424 else
425 #endif
427 const Elf32_Sym *const refsym = sym;
428 Elf32_Addr value = RESOLVE (&sym, version, r_type);
429 if (sym)
430 value += sym->st_value;
432 switch (r_type)
434 case R_ARM_COPY:
435 if (sym == NULL)
436 /* This can happen in trace mode if an object could not be
437 found. */
438 break;
439 if (sym->st_size > refsym->st_size
440 || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
442 const char *strtab;
444 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
445 _dl_error_printf ("\
446 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
447 rtld_progname ?: "<program name unknown>",
448 strtab + refsym->st_name);
450 memcpy (reloc_addr_arg, (void *) value,
451 MIN (sym->st_size, refsym->st_size));
452 break;
453 case R_ARM_GLOB_DAT:
454 case R_ARM_JUMP_SLOT:
455 # ifdef RTLD_BOOTSTRAP
456 /* Fix weak undefined references. */
457 if (sym != NULL && sym->st_value == 0)
458 *reloc_addr = 0;
459 else
460 # endif
461 *reloc_addr = value;
462 break;
463 case R_ARM_ABS32:
465 # ifndef RTLD_BOOTSTRAP
466 /* This is defined in rtld.c, but nowhere in the static
467 libc.a; make the reference weak so static programs can
468 still link. This declaration cannot be done when
469 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
470 rtld.c contains the common defn for _dl_rtld_map, which
471 is incompatible with a weak decl in the same file. */
472 # ifndef SHARED
473 weak_extern (_dl_rtld_map);
474 # endif
475 if (map == &GL(dl_rtld_map))
476 /* Undo the relocation done here during bootstrapping.
477 Now we will relocate it anew, possibly using a
478 binding found in the user program or a loaded library
479 rather than the dynamic linker's built-in definitions
480 used while loading those libraries. */
481 value -= map->l_addr + refsym->st_value;
482 # endif
483 *reloc_addr += value;
484 break;
486 case R_ARM_PC24:
488 Elf32_Sword addend;
489 Elf32_Addr newvalue, topbits;
491 addend = *reloc_addr & 0x00ffffff;
492 if (addend & 0x00800000) addend |= 0xff000000;
494 newvalue = value - (Elf32_Addr)reloc_addr + (addend << 2);
495 topbits = newvalue & 0xfe000000;
496 if (topbits != 0xfe000000 && topbits != 0x00000000)
498 newvalue = fix_bad_pc24(reloc_addr, value)
499 - (Elf32_Addr)reloc_addr + (addend << 2);
500 topbits = newvalue & 0xfe000000;
501 if (topbits != 0xfe000000 && topbits != 0x00000000)
503 _dl_signal_error (0, map->l_name, NULL,
504 "R_ARM_PC24 relocation out of range");
507 newvalue >>= 2;
508 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
509 *reloc_addr = value;
511 break;
512 default:
513 _dl_reloc_bad_type (map, r_type, 0);
514 break;
519 # ifndef RTLD_BOOTSTRAP
520 static inline void
521 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
522 const Elf32_Sym *sym, const struct r_found_version *version,
523 void *const reloc_addr_arg)
525 Elf32_Addr *const reloc_addr = reloc_addr_arg;
526 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
528 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
529 *reloc_addr = map->l_addr + reloc->r_addend;
530 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
531 return;
532 else
534 # ifndef RESOLVE_CONFLICT_FIND_MAP
535 const Elf32_Sym *const refsym = sym;
536 # endif
537 Elf32_Addr value = RESOLVE (&sym, version, r_type);
538 if (sym)
539 value += sym->st_value;
541 switch (r_type)
543 # ifndef RESOLVE_CONFLICT_FIND_MAP
544 /* Not needed for dl-conflict.c. */
545 case R_ARM_COPY:
546 if (sym == NULL)
547 /* This can happen in trace mode if an object could not be
548 found. */
549 break;
550 if (sym->st_size > refsym->st_size
551 || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
553 const char *strtab;
555 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
556 _dl_error_printf ("\
557 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
558 rtld_progname ?: "<program name unknown>",
559 strtab + refsym->st_name);
561 memcpy (reloc_addr_arg, (void *) value,
562 MIN (sym->st_size, refsym->st_size));
563 break;
564 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
565 case R_ARM_GLOB_DAT:
566 case R_ARM_JUMP_SLOT:
567 case R_ARM_ABS32:
568 *reloc_addr = value + reloc->r_addend;
569 break;
570 case R_ARM_PC24:
572 Elf32_Addr newvalue, topbits;
574 newvalue = value + reloc->r_addend - (Elf32_Addr)reloc_addr;
575 topbits = newvalue & 0xfe000000;
576 if (topbits != 0xfe000000 && topbits != 0x00000000)
578 newvalue = fix_bad_pc24(reloc_addr, value)
579 - (Elf32_Addr)reloc_addr + (reloc->r_addend << 2);
580 topbits = newvalue & 0xfe000000;
581 if (topbits != 0xfe000000 && topbits != 0x00000000)
583 _dl_signal_error (0, map->l_name, NULL,
584 "R_ARM_PC24 relocation out of range");
587 newvalue >>= 2;
588 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
589 *reloc_addr = value;
591 break;
592 default:
593 _dl_reloc_bad_type (map, r_type, 0);
594 break;
598 # endif
600 static inline void
601 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
602 void *const reloc_addr_arg)
604 Elf32_Addr *const reloc_addr = reloc_addr_arg;
605 *reloc_addr += l_addr;
608 # ifndef RTLD_BOOTSTRAP
609 static inline void
610 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
611 void *const reloc_addr_arg)
613 Elf32_Addr *const reloc_addr = reloc_addr_arg;
614 *reloc_addr = l_addr + reloc->r_addend;
616 # endif
618 static inline void
619 elf_machine_lazy_rel (struct link_map *map,
620 Elf32_Addr l_addr, const Elf32_Rel *reloc)
622 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
623 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
624 /* Check for unexpected PLT reloc type. */
625 if (__builtin_expect (r_type == R_ARM_JUMP_SLOT, 1))
627 if (__builtin_expect (map->l_mach.plt, 0) == 0)
628 *reloc_addr += l_addr;
629 else
630 *reloc_addr = map->l_mach.plt;
632 else
633 _dl_reloc_bad_type (map, r_type, 1);
636 #endif /* RESOLVE */