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
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) \
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" \
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
58 static inline Elf32_Addr
__attribute__ ((unused
))
59 elf_machine_dynamic (void)
61 register Elf32_Addr
*got
asm ("r10");
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
)
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. */
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. */
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
116 GL(dl_profile_map
) = l
;
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
;
126 #if defined(__THUMB_INTERWORK__)
127 #define BX(x) "bx\t" #x
129 #define BX(x) "mov\tpc, " #x
133 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
135 .globl _dl_runtime_resolve\n\
136 .type _dl_runtime_resolve, #function\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\
147 @ get pointer to linker struct\n\
150 @ prepare to call fixup()\n\
151 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each\n\
156 @ call fixup routine\n\
162 @ get arguments and return address back\n\
163 ldmia sp!, {r0-r3,lr}\n\
165 @ jump to the newly found address\n\
168 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
170 .globl _dl_runtime_profile\n\
171 .type _dl_runtime_profile, #function\n\
173 _dl_runtime_profile:\n\
175 stmdb sp!, {r0-r3}\n\
177 @ get pointer to linker struct\n\
180 @ prepare to call fixup()\n\
181 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each\n\
186 @ call profiling fixup routine\n\
192 @ get arguments and return address back\n\
193 ldmia sp!, {r0-r3,lr}\n\
195 @ jump to the newly found address\n\
198 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
202 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
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\
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\
217 stmdb sp!, {r0-r3}\n\
219 @ get pointer to linker struct\n\
222 @ prepare to call fixup()\n\
223 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each\n\
228 @ call profiling fixup routine\n\
234 @ get arguments and return address back\n\
235 ldmia sp!, {r0-r3,lr}\n\
237 @ jump to the newly found address\n\
240 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
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 ("\
256 .globl _dl_start_user\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\
266 @ returns user entry point in r0\n\
271 @ get the original arg count\n\
273 @ save the entry point in another register\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\
279 @ get the argv address\n\
281 @ store the new argc in the new stack location\n\
284 add r3, r2, r1, lsl #2\n\
287 @ now we call _dl_init\n\
291 bl _dl_init_internal(PLT)\n\
292 @ load the finalizer function\n\
293 ldr r0, .L_FINI_PROC\n\
295 @ jump to the user_s entry point\n\
298 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4\n\
300 .word _dl_skip_args(GOTOFF)\n\
302 .word _dl_fini(GOTOFF)\n\
304 .word _rtld_local(GOTOFF)\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
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
,
351 #endif /* !dl_machine_h */
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
361 /* Deal with an out-of-range PC24 reloc. */
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
;
373 pagesize
= getpagesize ();
374 fix_page
= mmap (NULL
, pagesize
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
375 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
377 assert (! "could not map page for fixup");
381 fix_address
= (Elf32_Word
*)(fix_page
+ fix_offset
);
382 fix_address
[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
383 fix_address
[1] = value
;
386 if (fix_offset
>= pagesize
)
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. */
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. */
414 weak_extern (_dl_rtld_map
);
416 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
418 *reloc_addr
+= map
->l_addr
;
420 # ifndef RTLD_BOOTSTRAP
421 else if (__builtin_expect (r_type
== R_ARM_NONE
, 0))
427 const Elf32_Sym
*const refsym
= sym
;
428 Elf32_Addr value
= RESOLVE (&sym
, version
, r_type
);
430 value
+= sym
->st_value
;
436 /* This can happen in trace mode if an object could not be
439 if (sym
->st_size
> refsym
->st_size
440 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
444 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
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
));
454 case R_ARM_JUMP_SLOT
:
455 # ifdef RTLD_BOOTSTRAP
456 /* Fix weak undefined references. */
457 if (sym
!= NULL
&& sym
->st_value
== 0)
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. */
473 weak_extern (_dl_rtld_map
);
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
;
483 *reloc_addr
+= value
;
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");
508 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
513 _dl_reloc_bad_type (map
, r_type
, 0);
519 # ifndef RTLD_BOOTSTRAP
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))
534 # ifndef RESOLVE_CONFLICT_FIND_MAP
535 const Elf32_Sym
*const refsym
= sym
;
537 Elf32_Addr value
= RESOLVE (&sym
, version
, r_type
);
539 value
+= sym
->st_value
;
543 # ifndef RESOLVE_CONFLICT_FIND_MAP
544 /* Not needed for dl-conflict.c. */
547 /* This can happen in trace mode if an object could not be
550 if (sym
->st_size
> refsym
->st_size
551 || (GLRO(dl_verbose
) && sym
->st_size
< refsym
->st_size
))
555 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
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
));
564 # endif /* !RESOLVE_CONFLICT_FIND_MAP */
566 case R_ARM_JUMP_SLOT
:
568 *reloc_addr
= value
+ reloc
->r_addend
;
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");
588 value
= (*reloc_addr
& 0xff000000) | (newvalue
& 0x00ffffff);
593 _dl_reloc_bad_type (map
, r_type
, 0);
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
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
;
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
;
630 *reloc_addr
= map
->l_mach
.plt
;
633 _dl_reloc_bad_type (map
, r_type
, 1);