2005-12-27 Roland McGrath <roland@redhat.com>
[glibc-ports.git] / sysdeps / arm / dl-machine.h
blobf176ff1372b32699cc2bafd8cb5836e0925dd698
1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005
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>
27 #include <tls.h>
29 #define VALID_ELF_ABIVERSION(ver) (ver == 0)
30 #define VALID_ELF_OSABI(osabi) \
31 (osabi == ELFOSABI_SYSV || osabi == ELFOSABI_ARM)
32 #define VALID_ELF_HEADER(hdr,exp,size) \
33 memcmp (hdr,exp,size-2) == 0 \
34 && VALID_ELF_OSABI (hdr[EI_OSABI]) \
35 && VALID_ELF_ABIVERSION (hdr[EI_ABIVERSION])
37 #define CLEAR_CACHE(BEG,END) \
38 INTERNAL_SYSCALL_ARM (cacheflush, , 3, (BEG), (END), 0)
40 /* Return nonzero iff ELF header is compatible with the running host. */
41 static inline int __attribute__ ((unused))
42 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
44 return ehdr->e_machine == EM_ARM;
48 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
49 first element of the GOT. This must be inlined in a function which
50 uses global data. */
51 static inline Elf32_Addr __attribute__ ((unused))
52 elf_machine_dynamic (void)
54 register Elf32_Addr *got asm ("r10");
55 return *got;
59 /* Return the run-time load address of the shared object. */
60 static inline Elf32_Addr __attribute__ ((unused))
61 elf_machine_load_address (void)
63 extern void __dl_start asm ("_dl_start");
64 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
65 Elf32_Addr pcrel_addr;
66 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
67 return pcrel_addr - got_addr;
71 /* Set up the loaded object described by L so its unrelocated PLT
72 entries will jump to the on-demand fixup code in dl-runtime.c. */
74 static inline int __attribute__ ((unused))
75 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
77 Elf32_Addr *got;
78 extern void _dl_runtime_resolve (Elf32_Word);
79 extern void _dl_runtime_profile (Elf32_Word);
81 if (l->l_info[DT_JMPREL] && lazy)
83 /* patb: this is different than i386 */
84 /* The GOT entries for functions in the PLT have not yet been filled
85 in. Their initial contents will arrange when called to push an
86 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
87 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
88 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
89 /* If a library is prelinked but we have to relocate anyway,
90 we have to be able to undo the prelinking of .got.plt.
91 The prelinker saved us here address of .plt. */
92 if (got[1])
93 l->l_mach.plt = got[1] + l->l_addr;
94 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
96 /* The got[2] entry contains the address of a function which gets
97 called to get the address of a so far unresolved function and
98 jump to it. The profiling extension of the dynamic linker allows
99 to intercept the calls to collect information. In this case we
100 don't store the address in the GOT so that all future calls also
101 end in this function. */
102 if (profile)
104 got[2] = (Elf32_Addr) &_dl_runtime_profile;
106 if (GLRO(dl_profile) != NULL
107 && _dl_name_match_p (GLRO(dl_profile), l))
108 /* Say that we really want profiling and the timers are
109 started. */
110 GL(dl_profile_map) = l;
112 else
113 /* This function will get called to fix up the GOT entry indicated by
114 the offset on the stack, and then jump to the resolved address. */
115 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
117 return lazy;
120 #if defined(__USE_BX__)
121 #define BX(x) "bx\t" #x
122 #else
123 #define BX(x) "mov\tpc, " #x
124 #endif
126 /* Mask identifying addresses reserved for the user program,
127 where the dynamic linker should not map anything. */
128 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
130 /* Initial entry point code for the dynamic linker.
131 The C function `_dl_start' is the real entry point;
132 its return value is the user program's entry point. */
134 #define RTLD_START asm ("\
135 .text\n\
136 .globl _start\n\
137 .globl _dl_start_user\n\
138 _start:\n\
139 @ we are PIC code, so get global offset table\n\
140 ldr sl, .L_GET_GOT\n\
141 @ See if we were run as a command with the executable file\n\
142 @ name as an extra leading argument.\n\
143 ldr r4, .L_SKIP_ARGS\n\
144 @ at start time, all the args are on the stack\n\
145 mov r0, sp\n\
146 bl _dl_start\n\
147 @ returns user entry point in r0\n\
148 _dl_start_user:\n\
149 add sl, pc, sl\n\
150 .L_GOT_GOT:\n\
151 ldr r4, [sl, r4]\n\
152 @ save the entry point in another register\n\
153 mov r6, r0\n\
154 @ get the original arg count\n\
155 ldr r1, [sp]\n\
156 @ get the argv address\n\
157 add r2, sp, #4\n\
158 @ Fix up the stack if necessary.\n\
159 cmp r4, #0\n\
160 bne .L_fixup_stack\n\
161 .L_done_fixup:\n\
162 @ compute envp\n\
163 add r3, r2, r1, lsl #2\n\
164 add r3, r3, #4\n\
165 @ now we call _dl_init\n\
166 ldr r0, .L_LOADED\n\
167 ldr r0, [sl, r0]\n\
168 @ call _dl_init\n\
169 bl _dl_init_internal(PLT)\n\
170 @ load the finalizer function\n\
171 ldr r0, .L_FINI_PROC\n\
172 add r0, sl, r0\n\
173 @ jump to the user_s entry point\n\
174 " BX(r6) "\n\
176 @ iWMMXt and EABI targets require the stack to be eight byte\n\
177 @ aligned - shuffle arguments etc.\n\
178 .L_fixup_stack:\n\
179 @ subtract _dl_skip_args from original arg count\n\
180 sub r1, r1, r4\n\
181 @ store the new argc in the new stack location\n\
182 str r1, [sp]\n\
183 @ find the first unskipped argument\n\
184 mov r3, r2\n\
185 add r4, r2, r4, lsl #2\n\
186 @ shuffle argv down\n\
187 1: ldr r5, [r4], #4\n\
188 str r5, [r3], #4\n\
189 cmp r5, #0\n\
190 bne 1b\n\
191 @ shuffle envp down\n\
192 1: ldr r5, [r4], #4\n\
193 str r5, [r3], #4\n\
194 cmp r5, #0\n\
195 bne 1b\n\
196 @ shuffle auxv down\n\
197 1: ldmia r4!, {r0, r5}\n\
198 stmia r3!, {r0, r5}\n\
199 cmp r0, #0\n\
200 bne 1b\n\
201 @ Update _dl_argv\n\
202 ldr r3, .L_ARGV\n\
203 str r2, [sl, r3]\n\
204 b .L_done_fixup\n\
206 .L_GET_GOT:\n\
207 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4\n\
208 .L_SKIP_ARGS:\n\
209 .word _dl_skip_args(GOTOFF)\n\
210 .L_FINI_PROC:\n\
211 .word _dl_fini(GOTOFF)\n\
212 .L_ARGV:\n\
213 .word _dl_argv(GOTOFF)\n\
214 .L_LOADED:\n\
215 .word _rtld_local(GOTOFF)\n\
216 .previous\n\
219 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
220 TLS variable, so undefined references should not be allowed to
221 define the value.
222 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
223 of the main executable's symbols, as for a COPY reloc. */
224 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
225 # define elf_machine_type_class(type) \
226 ((((type) == R_ARM_JUMP_SLOT || (type) == R_ARM_TLS_DTPMOD32 \
227 || (type) == R_ARM_TLS_DTPOFF32 || (type) == R_ARM_TLS_TPOFF32) \
228 * ELF_RTYPE_CLASS_PLT) \
229 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
230 #else
231 #define elf_machine_type_class(type) \
232 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
233 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
234 #endif
236 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
237 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
239 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
240 Prelinked libraries may use Elf32_Rela though. */
241 #define ELF_MACHINE_PLT_REL 1
243 /* We define an initialization functions. This is called very early in
244 _dl_sysdep_start. */
245 #define DL_PLATFORM_INIT dl_platform_init ()
247 static inline void __attribute__ ((unused))
248 dl_platform_init (void)
250 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
251 /* Avoid an empty string which would disturb us. */
252 GLRO(dl_platform) = NULL;
255 static inline Elf32_Addr
256 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
257 const Elf32_Rel *reloc,
258 Elf32_Addr *reloc_addr, Elf32_Addr value)
260 return *reloc_addr = value;
263 /* Return the final value of a plt relocation. */
264 static inline Elf32_Addr
265 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
266 Elf32_Addr value)
268 return value;
271 #endif /* !dl_machine_h */
274 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
275 Prelinked libraries may use Elf32_Rela though. */
276 #define ELF_MACHINE_NO_RELA defined RTLD_BOOTSTRAP
278 /* Names of the architecture-specific auditing callback functions. */
279 #define ARCH_LA_PLTENTER arm_gnu_pltenter
280 #define ARCH_LA_PLTEXIT arm_gnu_pltexit
282 #ifdef RESOLVE_MAP
284 /* Deal with an out-of-range PC24 reloc. */
285 auto Elf32_Addr
286 fix_bad_pc24 (Elf32_Addr *const reloc_addr, Elf32_Addr value)
288 static void *fix_page;
289 static unsigned int fix_offset;
290 static size_t pagesize;
291 Elf32_Word *fix_address;
293 if (! fix_page)
295 if (! pagesize)
296 pagesize = getpagesize ();
297 fix_page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC,
298 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
299 if (! fix_page)
300 assert (! "could not map page for fixup");
301 fix_offset = 0;
304 fix_address = (Elf32_Word *)(fix_page + fix_offset);
305 fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
306 fix_address[1] = value;
308 fix_offset += 8;
309 if (fix_offset >= pagesize)
310 fix_page = NULL;
312 return (Elf32_Addr)fix_address;
315 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
316 MAP is the object containing the reloc. */
318 auto inline void
319 __attribute__ ((always_inline))
320 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
321 const Elf32_Sym *sym, const struct r_found_version *version,
322 void *const reloc_addr_arg)
324 Elf32_Addr *const reloc_addr = reloc_addr_arg;
325 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
327 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
328 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
330 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
331 /* This is defined in rtld.c, but nowhere in the static libc.a;
332 make the reference weak so static programs can still link.
333 This declaration cannot be done when compiling rtld.c
334 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
335 common defn for _dl_rtld_map, which is incompatible with a
336 weak decl in the same file. */
337 # ifndef SHARED
338 weak_extern (_dl_rtld_map);
339 # endif
340 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
341 # endif
342 *reloc_addr += map->l_addr;
344 # ifndef RTLD_BOOTSTRAP
345 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
346 return;
347 # endif
348 else
349 #endif
351 const Elf32_Sym *const refsym = sym;
352 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
353 Elf32_Addr value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
355 switch (r_type)
357 case R_ARM_COPY:
358 if (sym == NULL)
359 /* This can happen in trace mode if an object could not be
360 found. */
361 break;
362 if (sym->st_size > refsym->st_size
363 || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
365 const char *strtab;
367 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
368 _dl_error_printf ("\
369 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
370 rtld_progname ?: "<program name unknown>",
371 strtab + refsym->st_name);
373 memcpy (reloc_addr_arg, (void *) value,
374 MIN (sym->st_size, refsym->st_size));
375 break;
376 case R_ARM_GLOB_DAT:
377 case R_ARM_JUMP_SLOT:
378 # ifdef RTLD_BOOTSTRAP
379 /* Fix weak undefined references. */
380 if (sym != NULL && sym->st_value == 0)
381 *reloc_addr = 0;
382 else
383 # endif
384 *reloc_addr = value;
385 break;
386 case R_ARM_ABS32:
388 # ifndef RTLD_BOOTSTRAP
389 /* This is defined in rtld.c, but nowhere in the static
390 libc.a; make the reference weak so static programs can
391 still link. This declaration cannot be done when
392 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
393 rtld.c contains the common defn for _dl_rtld_map, which
394 is incompatible with a weak decl in the same file. */
395 # ifndef SHARED
396 weak_extern (_dl_rtld_map);
397 # endif
398 if (map == &GL(dl_rtld_map))
399 /* Undo the relocation done here during bootstrapping.
400 Now we will relocate it anew, possibly using a
401 binding found in the user program or a loaded library
402 rather than the dynamic linker's built-in definitions
403 used while loading those libraries. */
404 value -= map->l_addr + refsym->st_value;
405 # endif
406 *reloc_addr += value;
407 break;
409 case R_ARM_PC24:
411 Elf32_Sword addend;
412 Elf32_Addr newvalue, topbits;
414 addend = *reloc_addr & 0x00ffffff;
415 if (addend & 0x00800000) addend |= 0xff000000;
417 newvalue = value - (Elf32_Addr)reloc_addr + (addend << 2);
418 topbits = newvalue & 0xfe000000;
419 if (topbits != 0xfe000000 && topbits != 0x00000000)
421 newvalue = fix_bad_pc24(reloc_addr, value)
422 - (Elf32_Addr)reloc_addr + (addend << 2);
423 topbits = newvalue & 0xfe000000;
424 if (topbits != 0xfe000000 && topbits != 0x00000000)
426 _dl_signal_error (0, map->l_name, NULL,
427 "R_ARM_PC24 relocation out of range");
430 newvalue >>= 2;
431 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
432 *reloc_addr = value;
434 break;
435 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
436 case R_ARM_TLS_DTPMOD32:
437 /* Get the information from the link map returned by the
438 resolv function. */
439 if (sym_map != NULL)
440 *reloc_addr = sym_map->l_tls_modid;
441 break;
443 case R_ARM_TLS_DTPOFF32:
444 *reloc_addr += sym->st_value;
445 break;
447 case R_ARM_TLS_TPOFF32:
448 CHECK_STATIC_TLS (map, sym_map);
449 *reloc_addr += sym->st_value + sym_map->l_tls_offset;
450 break;
451 #endif
452 default:
453 _dl_reloc_bad_type (map, r_type, 0);
454 break;
459 # ifndef RTLD_BOOTSTRAP
460 auto inline void
461 __attribute__ ((always_inline))
462 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
463 const Elf32_Sym *sym, const struct r_found_version *version,
464 void *const reloc_addr_arg)
466 Elf32_Addr *const reloc_addr = reloc_addr_arg;
467 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
469 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
470 *reloc_addr = map->l_addr + reloc->r_addend;
471 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
472 return;
473 else
475 # ifndef RESOLVE_CONFLICT_FIND_MAP
476 const Elf32_Sym *const refsym = sym;
477 # endif
478 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
479 Elf32_Addr value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
481 switch (r_type)
483 # ifndef RESOLVE_CONFLICT_FIND_MAP
484 /* Not needed for dl-conflict.c. */
485 case R_ARM_COPY:
486 if (sym == NULL)
487 /* This can happen in trace mode if an object could not be
488 found. */
489 break;
490 if (sym->st_size > refsym->st_size
491 || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
493 const char *strtab;
495 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
496 _dl_error_printf ("\
497 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
498 rtld_progname ?: "<program name unknown>",
499 strtab + refsym->st_name);
501 memcpy (reloc_addr_arg, (void *) value,
502 MIN (sym->st_size, refsym->st_size));
503 break;
504 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
505 case R_ARM_GLOB_DAT:
506 case R_ARM_JUMP_SLOT:
507 case R_ARM_ABS32:
508 *reloc_addr = value + reloc->r_addend;
509 break;
510 case R_ARM_PC24:
512 Elf32_Addr newvalue, topbits;
514 newvalue = value + reloc->r_addend - (Elf32_Addr)reloc_addr;
515 topbits = newvalue & 0xfe000000;
516 if (topbits != 0xfe000000 && topbits != 0x00000000)
518 newvalue = fix_bad_pc24(reloc_addr, value)
519 - (Elf32_Addr)reloc_addr + (reloc->r_addend << 2);
520 topbits = newvalue & 0xfe000000;
521 if (topbits != 0xfe000000 && topbits != 0x00000000)
523 _dl_signal_error (0, map->l_name, NULL,
524 "R_ARM_PC24 relocation out of range");
527 newvalue >>= 2;
528 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
529 *reloc_addr = value;
531 break;
532 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
533 case R_ARM_TLS_DTPMOD32:
534 /* Get the information from the link map returned by the
535 resolv function. */
536 if (sym_map != NULL)
537 *reloc_addr = sym_map->l_tls_modid;
538 break;
540 case R_ARM_TLS_DTPOFF32:
541 *reloc_addr = sym->st_value + reloc->r_addend;
542 break;
544 case R_ARM_TLS_TPOFF32:
545 CHECK_STATIC_TLS (map, sym_map);
546 *reloc_addr = (sym->st_value + sym_map->l_tls_offset
547 + reloc->r_addend);
548 break;
549 #endif
550 default:
551 _dl_reloc_bad_type (map, r_type, 0);
552 break;
556 # endif
558 auto inline void
559 __attribute__ ((always_inline))
560 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
561 void *const reloc_addr_arg)
563 Elf32_Addr *const reloc_addr = reloc_addr_arg;
564 *reloc_addr += l_addr;
567 # ifndef RTLD_BOOTSTRAP
568 auto inline void
569 __attribute__ ((always_inline))
570 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
571 void *const reloc_addr_arg)
573 Elf32_Addr *const reloc_addr = reloc_addr_arg;
574 *reloc_addr = l_addr + reloc->r_addend;
576 # endif
578 auto inline void
579 __attribute__ ((always_inline))
580 elf_machine_lazy_rel (struct link_map *map,
581 Elf32_Addr l_addr, const Elf32_Rel *reloc)
583 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
584 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
585 /* Check for unexpected PLT reloc type. */
586 if (__builtin_expect (r_type == R_ARM_JUMP_SLOT, 1))
588 if (__builtin_expect (map->l_mach.plt, 0) == 0)
589 *reloc_addr += l_addr;
590 else
591 *reloc_addr = map->l_mach.plt;
593 else
594 _dl_reloc_bad_type (map, r_type, 1);
597 #endif /* RESOLVE_MAP */