]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/arm/dl-machine.h
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 2006, 2009, 2010 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
24 #define ELF_MACHINE_NAME "ARM"
26 #include <sys/param.h>
29 #define CLEAR_CACHE(BEG,END) \
30 INTERNAL_SYSCALL_ARM (cacheflush, , 3, (BEG), (END), 0)
32 /* Return nonzero iff ELF header is compatible with the running host. */
33 static inline int __attribute__ ((unused
))
34 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
36 return ehdr
->e_machine
== EM_ARM
;
40 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
41 first element of the GOT. We used to use the PIC register to do this
42 without a constant pool reference, but GCC 4.2 will use a pseudo-register
43 for the PIC base, so it may not be in r10. */
44 static inline Elf32_Addr
__attribute__ ((unused
))
45 elf_machine_dynamic (void)
50 asm ("ldr\t%0, 1f\n\t"
52 "ldr\t%0, [%0, %1]\n\t"
55 "1: .word _GLOBAL_OFFSET_TABLE_ - 1b\n"
56 "2:" : "=r" (dynamic
), "=r"(tmp
));
59 "1: ldr %0, [pc, %0]\n"
61 "2: .word _GLOBAL_OFFSET_TABLE_ - (1b+8)\n"
62 "3:" : "=r" (dynamic
));
68 /* Return the run-time load address of the shared object. */
69 static inline Elf32_Addr
__attribute__ ((unused
))
70 elf_machine_load_address (void)
72 extern void __dl_start
asm ("_dl_start");
73 Elf32_Addr got_addr
= (Elf32_Addr
) &__dl_start
;
74 Elf32_Addr pcrel_addr
;
76 /* Clear the low bit of the funciton address. */
77 got_addr
&= ~(Elf32_Addr
) 1;
79 asm ("adr %0, _dl_start" : "=r" (pcrel_addr
));
80 return pcrel_addr
- got_addr
;
84 /* Set up the loaded object described by L so its unrelocated PLT
85 entries will jump to the on-demand fixup code in dl-runtime.c. */
87 static inline int __attribute__ ((unused
))
88 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
91 extern void _dl_runtime_resolve (Elf32_Word
);
92 extern void _dl_runtime_profile (Elf32_Word
);
94 if (l
->l_info
[DT_JMPREL
] && lazy
)
96 /* patb: this is different than i386 */
97 /* The GOT entries for functions in the PLT have not yet been filled
98 in. Their initial contents will arrange when called to push an
99 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
100 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
101 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
102 /* If a library is prelinked but we have to relocate anyway,
103 we have to be able to undo the prelinking of .got.plt.
104 The prelinker saved us here address of .plt. */
106 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
107 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
109 /* The got[2] entry contains the address of a function which gets
110 called to get the address of a so far unresolved function and
111 jump to it. The profiling extension of the dynamic linker allows
112 to intercept the calls to collect information. In this case we
113 don't store the address in the GOT so that all future calls also
114 end in this function. */
117 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
119 if (GLRO(dl_profile
) != NULL
120 && _dl_name_match_p (GLRO(dl_profile
), l
))
121 /* Say that we really want profiling and the timers are
123 GL(dl_profile_map
) = l
;
126 /* This function will get called to fix up the GOT entry indicated by
127 the offset on the stack, and then jump to the resolved address. */
128 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
133 #if defined(__USE_BX__)
134 #define BX(x) "bx\t" #x
136 #define BX(x) "mov\tpc, " #x
139 /* Mask identifying addresses reserved for the user program,
140 where the dynamic linker should not map anything. */
141 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
143 /* Initial entry point code for the dynamic linker.
144 The C function `_dl_start' is the real entry point;
145 its return value is the user program's entry point. */
147 #define RTLD_START asm ("\
150 .type _start, %function\n\
151 .globl _dl_start_user\n\
152 .type _dl_start_user, %function\n\
154 @ we are PIC code, so get global offset table\n\
155 ldr sl, .L_GET_GOT\n\
156 @ See if we were run as a command with the executable file\n\
157 @ name as an extra leading argument.\n\
158 ldr r4, .L_SKIP_ARGS\n\
159 @ at start time, all the args are on the stack\n\
162 @ returns user entry point in r0\n\
164 adr r6, .L_GET_GOT\n\
167 @ save the entry point in another register\n\
169 @ get the original arg count\n\
171 @ get the argv address\n\
173 @ Fix up the stack if necessary.\n\
175 bne .L_fixup_stack\n\
178 add r3, r2, r1, lsl #2\n\
180 @ now we call _dl_init\n\
184 bl _dl_init_internal(PLT)\n\
185 @ load the finalizer function\n\
186 ldr r0, .L_FINI_PROC\n\
188 @ jump to the user_s entry point\n\
191 @ iWMMXt and EABI targets require the stack to be eight byte\n\
192 @ aligned - shuffle arguments etc.\n\
194 @ subtract _dl_skip_args from original arg count\n\
196 @ store the new argc in the new stack location\n\
198 @ find the first unskipped argument\n\
200 add r4, r2, r4, lsl #2\n\
201 @ shuffle argv down\n\
202 1: ldr r5, [r4], #4\n\
206 @ shuffle envp down\n\
207 1: ldr r5, [r4], #4\n\
211 @ shuffle auxv down\n\
212 1: ldmia r4!, {r0, r5}\n\
213 stmia r3!, {r0, r5}\n\
222 .word _GLOBAL_OFFSET_TABLE_ - .L_GET_GOT\n\
224 .word _dl_skip_args(GOTOFF)\n\
226 .word _dl_fini(GOTOFF)\n\
228 .word _dl_argv(GOTOFF)\n\
230 .word _rtld_local(GOTOFF)\n\
234 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
235 TLS variable, so undefined references should not be allowed to
237 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
238 of the main executable's symbols, as for a COPY reloc. */
239 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
240 # define elf_machine_type_class(type) \
241 ((((type) == R_ARM_JUMP_SLOT || (type) == R_ARM_TLS_DTPMOD32 \
242 || (type) == R_ARM_TLS_DTPOFF32 || (type) == R_ARM_TLS_TPOFF32) \
243 * ELF_RTYPE_CLASS_PLT) \
244 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
246 #define elf_machine_type_class(type) \
247 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
248 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
251 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
252 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
254 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
255 Prelinked libraries may use Elf32_Rela though. */
256 #define ELF_MACHINE_PLT_REL 1
258 /* We define an initialization functions. This is called very early in
260 #define DL_PLATFORM_INIT dl_platform_init ()
262 static inline void __attribute__ ((unused
))
263 dl_platform_init (void)
265 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
266 /* Avoid an empty string which would disturb us. */
267 GLRO(dl_platform
) = NULL
;
270 static inline Elf32_Addr
271 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
272 const Elf32_Rel
*reloc
,
273 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
275 return *reloc_addr
= value
;
278 /* Return the final value of a plt relocation. */
279 static inline Elf32_Addr
280 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rel
*reloc
,
286 #endif /* !dl_machine_h */
289 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
290 Prelinked libraries may use Elf32_Rela though. */
291 #define ELF_MACHINE_NO_RELA defined RTLD_BOOTSTRAP
293 /* Names of the architecture-specific auditing callback functions. */
294 #define ARCH_LA_PLTENTER arm_gnu_pltenter
295 #define ARCH_LA_PLTEXIT arm_gnu_pltexit
299 /* Deal with an out-of-range PC24 reloc. */
301 fix_bad_pc24 (Elf32_Addr
*const reloc_addr
, Elf32_Addr value
)
303 static void *fix_page
;
304 static unsigned int fix_offset
;
305 static size_t pagesize
;
306 Elf32_Word
*fix_address
;
311 pagesize
= getpagesize ();
312 fix_page
= mmap (NULL
, pagesize
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
313 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
315 assert (! "could not map page for fixup");
319 fix_address
= (Elf32_Word
*)(fix_page
+ fix_offset
);
320 fix_address
[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
321 fix_address
[1] = value
;
324 if (fix_offset
>= pagesize
)
327 return (Elf32_Addr
)fix_address
;
330 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
331 MAP is the object containing the reloc. */
334 __attribute__ ((always_inline
))
335 elf_machine_rel (struct link_map
*map
, const Elf32_Rel
*reloc
,
336 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
337 void *const reloc_addr_arg
)
339 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
340 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
342 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
343 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
345 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
346 /* This is defined in rtld.c, but nowhere in the static libc.a;
347 make the reference weak so static programs can still link.
348 This declaration cannot be done when compiling rtld.c
349 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
350 common defn for _dl_rtld_map, which is incompatible with a
351 weak decl in the same file. */
353 weak_extern (_dl_rtld_map
);
355 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
357 *reloc_addr
+= map
->l_addr
;
359 # ifndef RTLD_BOOTSTRAP
360 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
366 const Elf32_Sym
*const refsym
= sym
;
367 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
368 Elf32_Addr value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
374 /* This can happen in trace mode if an object could not be
377 if (sym
->st_size
> refsym
->st_size
378 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
382 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
384 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
385 rtld_progname
?: "<program name unknown>",
386 strtab
+ refsym
->st_name
);
388 memcpy (reloc_addr_arg
, (void *) value
,
389 MIN (sym
->st_size
, refsym
->st_size
));
392 case R_ARM_JUMP_SLOT
:
393 # ifdef RTLD_BOOTSTRAP
394 /* Fix weak undefined references. */
395 if (sym
!= NULL
&& sym
->st_value
== 0)
403 # ifndef RTLD_BOOTSTRAP
404 /* This is defined in rtld.c, but nowhere in the static
405 libc.a; make the reference weak so static programs can
406 still link. This declaration cannot be done when
407 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
408 rtld.c contains the common defn for _dl_rtld_map, which
409 is incompatible with a weak decl in the same file. */
411 weak_extern (_dl_rtld_map
);
413 if (map
== &GL(dl_rtld_map
))
414 /* Undo the relocation done here during bootstrapping.
415 Now we will relocate it anew, possibly using a
416 binding found in the user program or a loaded library
417 rather than the dynamic linker's built-in definitions
418 used while loading those libraries. */
419 value
-= map
->l_addr
+ refsym
->st_value
;
421 *reloc_addr
+= value
;
427 Elf32_Addr newvalue
, topbits
;
429 addend
= *reloc_addr
& 0x00ffffff;
430 if (addend
& 0x00800000) addend
|= 0xff000000;
432 newvalue
= value
- (Elf32_Addr
)reloc_addr
+ (addend
<< 2);
433 topbits
= newvalue
& 0xfe000000;
434 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
436 newvalue
= fix_bad_pc24(reloc_addr
, value
)
437 - (Elf32_Addr
)reloc_addr
+ (addend
<< 2);
438 topbits
= newvalue
& 0xfe000000;
439 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
441 _dl_signal_error (0, map
->l_name
, NULL
,
442 "R_ARM_PC24 relocation out of range");
446 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
450 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
451 case R_ARM_TLS_DTPMOD32
:
452 /* Get the information from the link map returned by the
455 *reloc_addr
= sym_map
->l_tls_modid
;
458 case R_ARM_TLS_DTPOFF32
:
460 *reloc_addr
+= sym
->st_value
;
463 case R_ARM_TLS_TPOFF32
:
466 CHECK_STATIC_TLS (map
, sym_map
);
467 *reloc_addr
+= sym
->st_value
+ sym_map
->l_tls_offset
;
472 _dl_reloc_bad_type (map
, r_type
, 0);
478 # ifndef RTLD_BOOTSTRAP
480 __attribute__ ((always_inline
))
481 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
482 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
483 void *const reloc_addr_arg
)
485 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
486 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
488 if (__builtin_expect (r_type
== R_ARM_RELATIVE
, 0))
489 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
490 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
494 # ifndef RESOLVE_CONFLICT_FIND_MAP
495 const Elf32_Sym
*const refsym
= sym
;
497 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
498 Elf32_Addr value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
502 # ifndef RESOLVE_CONFLICT_FIND_MAP
503 /* Not needed for dl-conflict.c. */
506 /* This can happen in trace mode if an object could not be
509 if (sym
->st_size
> refsym
->st_size
510 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
514 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
516 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
517 rtld_progname
?: "<program name unknown>",
518 strtab
+ refsym
->st_name
);
520 memcpy (reloc_addr_arg
, (void *) value
,
521 MIN (sym
->st_size
, refsym
->st_size
));
523 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
525 case R_ARM_JUMP_SLOT
:
527 *reloc_addr
= value
+ reloc
->r_addend
;
531 Elf32_Addr newvalue
, topbits
;
533 newvalue
= value
+ reloc
->r_addend
- (Elf32_Addr
)reloc_addr
;
534 topbits
= newvalue
& 0xfe000000;
535 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
537 newvalue
= fix_bad_pc24(reloc_addr
, value
)
538 - (Elf32_Addr
)reloc_addr
+ (reloc
->r_addend
<< 2);
539 topbits
= newvalue
& 0xfe000000;
540 if (topbits
!= 0xfe000000 && topbits
!= 0x00000000)
542 _dl_signal_error (0, map
->l_name
, NULL
,
543 "R_ARM_PC24 relocation out of range");
547 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
551 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
552 case R_ARM_TLS_DTPMOD32
:
553 /* Get the information from the link map returned by the
556 *reloc_addr
= sym_map
->l_tls_modid
;
559 case R_ARM_TLS_DTPOFF32
:
560 *reloc_addr
= (sym
== NULL
? 0 : sym
->st_value
) + reloc
->r_addend
;
563 case R_ARM_TLS_TPOFF32
:
566 CHECK_STATIC_TLS (map
, sym_map
);
567 *reloc_addr
= (sym
->st_value
+ sym_map
->l_tls_offset
573 _dl_reloc_bad_type (map
, r_type
, 0);
581 __attribute__ ((always_inline
))
582 elf_machine_rel_relative (Elf32_Addr l_addr
, const Elf32_Rel
*reloc
,
583 void *const reloc_addr_arg
)
585 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
586 *reloc_addr
+= l_addr
;
589 # ifndef RTLD_BOOTSTRAP
591 __attribute__ ((always_inline
))
592 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
593 void *const reloc_addr_arg
)
595 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
596 *reloc_addr
= l_addr
+ reloc
->r_addend
;
601 __attribute__ ((always_inline
))
602 elf_machine_lazy_rel (struct link_map
*map
,
603 Elf32_Addr l_addr
, const Elf32_Rel
*reloc
)
605 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
606 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
607 /* Check for unexpected PLT reloc type. */
608 if (__builtin_expect (r_type
== R_ARM_JUMP_SLOT
, 1))
610 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
611 *reloc_addr
+= l_addr
;
613 *reloc_addr
= map
->l_mach
.plt
;
616 _dl_reloc_bad_type (map
, r_type
, 1);
619 #endif /* RESOLVE_MAP */