Update.
[glibc.git] / sysdeps / arm / dl-machine.h
blob2d802b7e9b13b3fed74be0cba001773ce17d4c3c
1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995,96,97,98,99,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "ARM"
25 #include <sys/param.h>
27 #define VALID_ELF_ABIVERSION(ver) (ver == 0)
28 #define VALID_ELF_OSABI(osabi) \
29 (osabi == ELFOSABI_SYSV || osabi == ELFOSABI_ARM)
30 #define VALID_ELF_HEADER(hdr,exp,size) \
31 memcmp (hdr,exp,size-2) == 0 \
32 && VALID_ELF_OSABI (hdr[EI_OSABI]) \
33 && VALID_ELF_ABIVERSION (hdr[EI_ABIVERSION])
35 #define CLEAR_CACHE(BEG,END) \
36 { \
37 register unsigned long _beg __asm ("a1") = (unsigned long)(BEG); \
38 register unsigned long _end __asm ("a2") = (unsigned long)(END); \
39 register unsigned long _flg __asm ("a3") = 0; \
40 __asm __volatile ("swi 0x9f0002 @ sys_cacheflush" \
41 : /* no outputs */ \
42 : /* no inputs */ \
43 : "a1"); \
46 /* Return nonzero iff ELF header is compatible with the running host. */
47 static inline int __attribute__ ((unused))
48 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
50 return ehdr->e_machine == EM_ARM;
54 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
55 first element of the GOT. This must be inlined in a function which
56 uses global data. */
57 static inline Elf32_Addr __attribute__ ((unused))
58 elf_machine_dynamic (void)
60 register Elf32_Addr *got asm ("r10");
61 return *got;
65 /* Return the run-time load address of the shared object. */
66 static inline Elf32_Addr __attribute__ ((unused))
67 elf_machine_load_address (void)
69 extern void __dl_start asm ("_dl_start");
70 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
71 Elf32_Addr pcrel_addr;
72 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
73 return pcrel_addr - got_addr;
77 /* Set up the loaded object described by L so its unrelocated PLT
78 entries will jump to the on-demand fixup code in dl-runtime.c. */
80 static inline int __attribute__ ((unused))
81 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
83 Elf32_Addr *got;
84 extern void _dl_runtime_resolve (Elf32_Word);
85 extern void _dl_runtime_profile (Elf32_Word);
87 if (l->l_info[DT_JMPREL] && lazy)
89 /* patb: this is different than i386 */
90 /* The GOT entries for functions in the PLT have not yet been filled
91 in. Their initial contents will arrange when called to push an
92 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
93 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
94 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
95 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
97 /* The got[2] entry contains the address of a function which gets
98 called to get the address of a so far unresolved function and
99 jump to it. The profiling extension of the dynamic linker allows
100 to intercept the calls to collect information. In this case we
101 don't store the address in the GOT so that all future calls also
102 end in this function. */
103 if (profile)
105 got[2] = (Elf32_Addr) &_dl_runtime_profile;
107 if (_dl_name_match_p (_dl_profile, l))
108 /* Say that we really want profiling and the timers are
109 started. */
110 _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 /* This code is used in dl-runtime.c to call the `fixup' function
121 and then redirect to the address it returns. */
122 // macro for handling PIC situation....
123 #ifdef PIC
124 #define CALL_ROUTINE(x) " ldr sl,0f
125 add sl, pc, sl
126 1: ldr r2, 2f
127 mov lr, pc
128 add pc, sl, r2
129 b 3f
130 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
131 2: .word " #x "(GOTOFF)
132 3: "
133 #else
134 #define CALL_ROUTINE(x) " bl " #x
135 #endif
137 #ifndef PROF
138 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
139 .text
140 .globl _dl_runtime_resolve
141 .type _dl_runtime_resolve, #function
142 .align 2
143 _dl_runtime_resolve:
144 @ we get called with
145 @ stack[0] contains the return address from this call
146 @ ip contains &GOT[n+3] (pointer to function)
147 @ lr points to &GOT[2]
149 @ save almost everything; lr is already on the stack
150 stmdb sp!,{r0-r3,sl,fp}
152 @ prepare to call fixup()
153 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
154 sub r1, ip, lr
155 sub r1, r1, #4
156 add r1, r1, r1
158 @ get pointer to linker struct
159 ldr r0, [lr, #-4]
161 @ call fixup routine
162 " CALL_ROUTINE(fixup) "
164 @ save the return
165 mov ip, r0
167 @ restore the stack
168 ldmia sp!,{r0-r3,sl,fp,lr}
170 @ jump to the newly found address
171 mov pc, ip
173 .size _dl_runtime_resolve, .-_dl_runtime_resolve
175 .globl _dl_runtime_profile
176 .type _dl_runtime_profile, #function
177 .align 2
178 _dl_runtime_profile:
179 @ save almost everything; lr is already on the stack
180 stmdb sp!,{r0-r3,sl,fp}
182 @ prepare to call fixup()
183 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
184 sub r1, ip, lr
185 sub r1, r1, #4
186 add r1, r1, r1
188 @ get pointer to linker struct
189 ldr r0, [lr, #-4]
191 @ call profiling fixup routine
192 " CALL_ROUTINE(profile_fixup) "
194 @ save the return
195 mov ip, r0
197 @ restore the stack
198 ldmia sp!,{r0-r3,sl,fp,lr}
200 @ jump to the newly found address
201 mov pc, ip
203 .size _dl_runtime_resolve, .-_dl_runtime_resolve
204 .previous
206 #else // PROF
207 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
208 .text
209 .globl _dl_runtime_resolve
210 .globl _dl_runtime_profile
211 .type _dl_runtime_resolve, #function
212 .type _dl_runtime_profile, #function
213 .align 2
214 _dl_runtime_resolve:
215 _dl_runtime_profile:
216 @ we get called with
217 @ stack[0] contains the return address from this call
218 @ ip contains &GOT[n+3] (pointer to function)
219 @ lr points to &GOT[2]
221 @ save almost everything; return add is already on the stack
222 stmdb sp!,{r0-r3,sl,fp}
224 @ prepare to call fixup()
225 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
226 sub r1, ip, lr
227 sub r1, r1, #4
228 add r1, r1, r1
230 @ get pointer to linker struct
231 ldr r0, [lr, #-4]
233 @ call profiling fixup routine
234 " CALL_ROUTINE(fixup) "
236 @ save the return
237 mov ip, r0
239 @ restore the stack
240 ldmia sp!,{r0-r3,sl,fp,lr}
242 @ jump to the newly found address
243 mov pc, ip
245 .size _dl_runtime_profile, .-_dl_runtime_profile
246 .previous
248 #endif //PROF
250 /* Mask identifying addresses reserved for the user program,
251 where the dynamic linker should not map anything. */
252 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
254 /* Initial entry point code for the dynamic linker.
255 The C function `_dl_start' is the real entry point;
256 its return value is the user program's entry point. */
258 #define RTLD_START asm ("\
259 .text
260 .globl _start
261 .globl _dl_start_user
262 _start:
263 @ at start time, all the args are on the stack
264 mov r0, sp
265 bl _dl_start
266 @ returns user entry point in r0
267 _dl_start_user:
268 mov r6, r0
269 @ we are PIC code, so get global offset table
270 ldr sl, .L_GET_GOT
271 add sl, pc, sl
272 .L_GOT_GOT:
273 @ Store the highest stack address
274 ldr r1, .L_STACK_END
275 ldr r1, [sl, r1]
276 str sp, [r1]
277 @ See if we were run as a command with the executable file
278 @ name as an extra leading argument.
279 ldr r4, .L_SKIP_ARGS
280 ldr r4, [sl, r4]
281 @ get the original arg count
282 ldr r1, [sp]
283 @ subtract _dl_skip_args from it
284 sub r1, r1, r4
285 @ adjust the stack pointer to skip them
286 add sp, sp, r4, lsl #2
287 @ get the argv address
288 add r2, sp, #4
289 @ store the new argc in the new stack location
290 str r1, [sp]
291 @ compute envp
292 add r3, r2, r1, lsl #2
293 add r3, r3, #4
295 @ now we call _dl_init
296 ldr r0, .L_LOADED
297 ldr r0, [sl, r0]
298 ldr r0, [r0]
299 @ call _dl_init
300 bl _dl_init(PLT)
301 @ clear the startup flag
302 ldr r2, .L_STARTUP_FLAG
303 ldr r1, [sl, r2]
304 mov r0, #0
305 str r0, [r1]
306 @ load the finalizer function
307 ldr r0, .L_FINI_PROC
308 ldr r0, [sl, r0]
309 @ jump to the user_s entry point
310 mov pc, r6
311 .L_GET_GOT:
312 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
313 .L_SKIP_ARGS: \n\
314 .word _dl_skip_args(GOTOFF) \n\
315 .L_STARTUP_FLAG:
316 .word _dl_starting_up(GOT)
317 .L_FINI_PROC:
318 .word _dl_fini(GOT)
319 .L_STACK_END:
320 .word __libc_stack_end(GOT)
321 .L_LOADED:
322 .word _dl_loaded(GOT)
323 .previous\n\
326 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
327 PLT entries should not be allowed to define the value.
328 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
329 of the main executable's symbols, as for a COPY reloc. */
330 #define elf_machine_type_class(type) \
331 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
332 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
334 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
335 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
337 /* The ARM never uses Elf32_Rela relocations. */
338 #define ELF_MACHINE_NO_RELA 1
340 /* We define an initialization functions. This is called very early in
341 _dl_sysdep_start. */
342 #define DL_PLATFORM_INIT dl_platform_init ()
344 extern const char *_dl_platform;
346 static inline void __attribute__ ((unused))
347 dl_platform_init (void)
349 if (_dl_platform != NULL && *_dl_platform == '\0')
350 /* Avoid an empty string which would disturb us. */
351 _dl_platform = NULL;
354 static inline Elf32_Addr
355 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
356 const Elf32_Rel *reloc,
357 Elf32_Addr *reloc_addr, Elf32_Addr value)
359 return *reloc_addr = value;
362 /* Return the final value of a plt relocation. */
363 static inline Elf32_Addr
364 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
365 Elf32_Addr value)
367 return value;
370 #endif /* !dl_machine_h */
372 #ifdef RESOLVE
374 extern char **_dl_argv;
376 /* Deal with an out-of-range PC24 reloc. */
377 static Elf32_Addr
378 fix_bad_pc24 (Elf32_Addr *const reloc_addr, Elf32_Addr value)
380 static void *fix_page;
381 static unsigned int fix_offset;
382 static size_t pagesize;
383 Elf32_Word *fix_address;
385 if (! fix_page)
387 if (! pagesize)
388 pagesize = getpagesize ();
389 fix_page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC,
390 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
391 if (! fix_page)
392 assert (! "could not map page for fixup");
393 fix_offset = 0;
396 fix_address = (Elf32_Word *)(fix_page + fix_offset);
397 fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
398 fix_address[1] = value;
400 fix_offset += 8;
401 if (fix_offset >= pagesize)
402 fix_page = NULL;
404 return (Elf32_Addr)fix_address;
407 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
408 MAP is the object containing the reloc. */
410 static inline void
411 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
412 const Elf32_Sym *sym, const struct r_found_version *version,
413 Elf32_Addr *const reloc_addr)
415 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
417 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
419 #ifndef RTLD_BOOTSTRAP
420 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
421 #endif
422 *reloc_addr += map->l_addr;
424 #ifndef RTLD_BOOTSTRAP
425 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
426 return;
427 #endif
428 else
430 const Elf32_Sym *const refsym = sym;
431 Elf32_Addr value = RESOLVE (&sym, version, r_type);
432 if (sym)
433 value += sym->st_value;
435 switch (r_type)
437 case R_ARM_COPY:
438 if (sym == NULL)
439 /* This can happen in trace mode if an object could not be
440 found. */
441 break;
442 if (sym->st_size > refsym->st_size
443 || (_dl_verbose && sym->st_size < refsym->st_size))
445 const char *strtab;
447 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
448 _dl_error_printf ("\
449 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
450 _dl_argv[0] ?: "<program name unknown>",
451 strtab + refsym->st_name);
453 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
454 refsym->st_size));
455 break;
456 case R_ARM_GLOB_DAT:
457 case R_ARM_JUMP_SLOT:
458 #ifdef RTLD_BOOTSTRAP
459 /* Fix weak undefined references. */
460 if (sym != NULL && sym->st_value == 0)
461 *reloc_addr = 0;
462 else
463 #endif
464 *reloc_addr = value;
465 break;
466 case R_ARM_ABS32:
468 #ifndef RTLD_BOOTSTRAP
469 /* This is defined in rtld.c, but nowhere in the static
470 libc.a; make the reference weak so static programs can
471 still link. This declaration cannot be done when
472 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
473 rtld.c contains the common defn for _dl_rtld_map, which
474 is incompatible with a weak decl in the same file. */
475 weak_extern (_dl_rtld_map);
476 if (map == &_dl_rtld_map)
477 /* Undo the relocation done here during bootstrapping.
478 Now we will relocate it anew, possibly using a
479 binding found in the user program or a loaded library
480 rather than the dynamic linker's built-in definitions
481 used while loading those libraries. */
482 value -= map->l_addr + refsym->st_value;
483 #endif
484 *reloc_addr += value;
485 break;
487 case R_ARM_PC24:
489 Elf32_Sword addend;
490 Elf32_Addr newvalue, topbits;
492 addend = *reloc_addr & 0x00ffffff;
493 if (addend & 0x00800000) addend |= 0xff000000;
495 newvalue = value - (Elf32_Addr)reloc_addr + (addend << 2);
496 topbits = newvalue & 0xfe000000;
497 if (topbits != 0xfe000000 && topbits != 0x00000000)
499 newvalue = fix_bad_pc24(reloc_addr, value)
500 - (Elf32_Addr)reloc_addr + (addend << 2);
501 topbits = newvalue & 0xfe000000;
502 if (topbits != 0xfe000000 && topbits != 0x00000000)
504 _dl_signal_error (0, map->l_name, NULL,
505 "R_ARM_PC24 relocation out of range");
508 newvalue >>= 2;
509 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
510 *reloc_addr = value;
512 break;
513 default:
514 _dl_reloc_bad_type (map, r_type, 0);
515 break;
520 static inline void
521 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
522 Elf32_Addr *const reloc_addr)
524 *reloc_addr += l_addr;
527 static inline void
528 elf_machine_lazy_rel (struct link_map *map,
529 Elf32_Addr l_addr, const Elf32_Rel *reloc)
531 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
532 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
533 /* Check for unexpected PLT reloc type. */
534 if (__builtin_expect (r_type == R_ARM_JUMP_SLOT, 1))
535 *reloc_addr += l_addr;
536 else
537 _dl_reloc_bad_type (map, r_type, 1);
540 #endif /* RESOLVE */