]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/arm/dl-machine.h
Update.
[thirdparty/glibc.git] / sysdeps / arm / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
19
20 #ifndef dl_machine_h
21 #define dl_machine_h
22
23 #define ELF_MACHINE_NAME "ARM"
24
25 #include <sys/param.h>
26
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])
34
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"); \
44 }
45
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)
49 {
50 return ehdr->e_machine == EM_ARM;
51 }
52
53
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)
59 {
60 register Elf32_Addr *got asm ("r10");
61 return *got;
62 }
63
64
65 /* Return the run-time load address of the shared object. */
66 static inline Elf32_Addr __attribute__ ((unused))
67 elf_machine_load_address (void)
68 {
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;
74 }
75
76
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. */
79
80 static inline int __attribute__ ((unused))
81 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
82 {
83 Elf32_Addr *got;
84 extern void _dl_runtime_resolve (Elf32_Word);
85 extern void _dl_runtime_profile (Elf32_Word);
86
87 if (l->l_info[DT_JMPREL] && lazy)
88 {
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 /* If a library is prelinked but we have to relocate anyway,
96 we have to be able to undo the prelinking of .got.plt.
97 The prelinker saved us here address of .plt. */
98 if (got[1])
99 l->l_mach.plt = got[1] + l->l_addr;
100 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
101
102 /* The got[2] entry contains the address of a function which gets
103 called to get the address of a so far unresolved function and
104 jump to it. The profiling extension of the dynamic linker allows
105 to intercept the calls to collect information. In this case we
106 don't store the address in the GOT so that all future calls also
107 end in this function. */
108 if (profile)
109 {
110 got[2] = (Elf32_Addr) &_dl_runtime_profile;
111
112 if (_dl_name_match_p (GL(dl_profile), l))
113 /* Say that we really want profiling and the timers are
114 started. */
115 GL(dl_profile_map) = l;
116 }
117 else
118 /* This function will get called to fix up the GOT entry indicated by
119 the offset on the stack, and then jump to the resolved address. */
120 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
121 }
122 return lazy;
123 }
124
125 /* This code is used in dl-runtime.c to call the `fixup' function
126 and then redirect to the address it returns. */
127 // macro for handling PIC situation....
128 #ifdef PIC
129 #define CALL_ROUTINE(x) " ldr sl,0f
130 add sl, pc, sl
131 1: ldr r2, 2f
132 mov lr, pc
133 add pc, sl, r2
134 b 3f
135 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
136 2: .word " #x "(GOTOFF)
137 3: "
138 #else
139 #define CALL_ROUTINE(x) " bl " #x
140 #endif
141
142 #ifndef PROF
143 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
144 .text
145 .globl _dl_runtime_resolve
146 .type _dl_runtime_resolve, #function
147 .align 2
148 _dl_runtime_resolve:
149 @ we get called with
150 @ stack[0] contains the return address from this call
151 @ ip contains &GOT[n+3] (pointer to function)
152 @ lr points to &GOT[2]
153
154 @ save almost everything; lr is already on the stack
155 stmdb sp!,{r0-r3,sl,fp}
156
157 @ prepare to call fixup()
158 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
159 sub r1, ip, lr
160 sub r1, r1, #4
161 add r1, r1, r1
162
163 @ get pointer to linker struct
164 ldr r0, [lr, #-4]
165
166 @ call fixup routine
167 " CALL_ROUTINE(fixup) "
168
169 @ save the return
170 mov ip, r0
171
172 @ restore the stack
173 ldmia sp!,{r0-r3,sl,fp,lr}
174
175 @ jump to the newly found address
176 mov pc, ip
177
178 .size _dl_runtime_resolve, .-_dl_runtime_resolve
179
180 .globl _dl_runtime_profile
181 .type _dl_runtime_profile, #function
182 .align 2
183 _dl_runtime_profile:
184 @ save almost everything; lr is already on the stack
185 stmdb sp!,{r0-r3,sl,fp}
186
187 @ prepare to call fixup()
188 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
189 sub r1, ip, lr
190 sub r1, r1, #4
191 add r1, r1, r1
192
193 @ get pointer to linker struct
194 ldr r0, [lr, #-4]
195
196 @ call profiling fixup routine
197 " CALL_ROUTINE(profile_fixup) "
198
199 @ save the return
200 mov ip, r0
201
202 @ restore the stack
203 ldmia sp!,{r0-r3,sl,fp,lr}
204
205 @ jump to the newly found address
206 mov pc, ip
207
208 .size _dl_runtime_resolve, .-_dl_runtime_resolve
209 .previous
210 ");
211 #else // PROF
212 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
213 .text
214 .globl _dl_runtime_resolve
215 .globl _dl_runtime_profile
216 .type _dl_runtime_resolve, #function
217 .type _dl_runtime_profile, #function
218 .align 2
219 _dl_runtime_resolve:
220 _dl_runtime_profile:
221 @ we get called with
222 @ stack[0] contains the return address from this call
223 @ ip contains &GOT[n+3] (pointer to function)
224 @ lr points to &GOT[2]
225
226 @ save almost everything; return add is already on the stack
227 stmdb sp!,{r0-r3,sl,fp}
228
229 @ prepare to call fixup()
230 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
231 sub r1, ip, lr
232 sub r1, r1, #4
233 add r1, r1, r1
234
235 @ get pointer to linker struct
236 ldr r0, [lr, #-4]
237
238 @ call profiling fixup routine
239 " CALL_ROUTINE(fixup) "
240
241 @ save the return
242 mov ip, r0
243
244 @ restore the stack
245 ldmia sp!,{r0-r3,sl,fp,lr}
246
247 @ jump to the newly found address
248 mov pc, ip
249
250 .size _dl_runtime_profile, .-_dl_runtime_profile
251 .previous
252 ");
253 #endif //PROF
254
255 /* Mask identifying addresses reserved for the user program,
256 where the dynamic linker should not map anything. */
257 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
258
259 /* Initial entry point code for the dynamic linker.
260 The C function `_dl_start' is the real entry point;
261 its return value is the user program's entry point. */
262
263 #define RTLD_START asm ("\
264 .text
265 .globl _start
266 .globl _dl_start_user
267 _start:
268 @ at start time, all the args are on the stack
269 mov r0, sp
270 bl _dl_start
271 @ returns user entry point in r0
272 _dl_start_user:
273 mov r6, r0
274 @ we are PIC code, so get global offset table
275 ldr sl, .L_GET_GOT
276 add sl, pc, sl
277 .L_GOT_GOT:
278 @ Store the highest stack address
279 ldr r1, .L_STACK_END
280 ldr r1, [sl, r1]
281 str sp, [r1]
282 @ See if we were run as a command with the executable file
283 @ name as an extra leading argument.
284 ldr r4, .L_SKIP_ARGS
285 ldr r4, [sl, r4]
286 @ get the original arg count
287 ldr r1, [sp]
288 @ subtract _dl_skip_args from it
289 sub r1, r1, r4
290 @ adjust the stack pointer to skip them
291 add sp, sp, r4, lsl #2
292 @ get the argv address
293 add r2, sp, #4
294 @ store the new argc in the new stack location
295 str r1, [sp]
296 @ compute envp
297 add r3, r2, r1, lsl #2
298 add r3, r3, #4
299
300 @ now we call _dl_init
301 ldr r0, .L_LOADED
302 ldr r0, [sl, r0]
303 ldr r0, [r0]
304 @ call _dl_init
305 bl _dl_init_internal(PLT)
306 @ clear the startup flag
307 ldr r2, .L_STARTUP_FLAG
308 ldr r1, [sl, r2]
309 mov r0, #0
310 str r0, [r1]
311 @ load the finalizer function
312 ldr r0, .L_FINI_PROC
313 ldr r0, [sl, r0]
314 @ jump to the user_s entry point
315 mov pc, r6
316 .L_GET_GOT:
317 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
318 .L_SKIP_ARGS: \n\
319 .word _dl_skip_args(GOTOFF) \n\
320 .L_STARTUP_FLAG:
321 .word _dl_starting_up(GOT)
322 .L_FINI_PROC:
323 .word _dl_fini(GOT)
324 .L_STACK_END:
325 .word __libc_stack_end(GOT)
326 .L_LOADED:
327 .word _rtld_local(GOT)
328 .previous\n\
329 ");
330
331 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
332 PLT entries should not be allowed to define the value.
333 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
334 of the main executable's symbols, as for a COPY reloc. */
335 #define elf_machine_type_class(type) \
336 ((((type) == R_ARM_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
337 | (((type) == R_ARM_COPY) * ELF_RTYPE_CLASS_COPY))
338
339 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
340 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
341
342 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
343 Prelinked libraries may use Elf32_Rela though. */
344 #define ELF_MACHINE_PLT_REL 1
345
346 /* We define an initialization functions. This is called very early in
347 _dl_sysdep_start. */
348 #define DL_PLATFORM_INIT dl_platform_init ()
349
350 static inline void __attribute__ ((unused))
351 dl_platform_init (void)
352 {
353 if (GL(dl_platform) != NULL && *GL(dl_platform) == '\0')
354 /* Avoid an empty string which would disturb us. */
355 GL(dl_platform) = NULL;
356 }
357
358 static inline Elf32_Addr
359 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
360 const Elf32_Rel *reloc,
361 Elf32_Addr *reloc_addr, Elf32_Addr value)
362 {
363 return *reloc_addr = value;
364 }
365
366 /* Return the final value of a plt relocation. */
367 static inline Elf32_Addr
368 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
369 Elf32_Addr value)
370 {
371 return value;
372 }
373
374 #endif /* !dl_machine_h */
375
376 #ifdef RESOLVE
377
378 /* ARM never uses Elf32_Rela relocations for the dynamic linker.
379 Prelinked libraries may use Elf32_Rela though. */
380 # ifdef RTLD_BOOTSTRAP
381 # define ELF_MACHINE_NO_RELA 1
382 # endif
383
384 /* Deal with an out-of-range PC24 reloc. */
385 static Elf32_Addr
386 fix_bad_pc24 (Elf32_Addr *const reloc_addr, Elf32_Addr value)
387 {
388 static void *fix_page;
389 static unsigned int fix_offset;
390 static size_t pagesize;
391 Elf32_Word *fix_address;
392
393 if (! fix_page)
394 {
395 if (! pagesize)
396 pagesize = getpagesize ();
397 fix_page = mmap (NULL, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC,
398 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
399 if (! fix_page)
400 assert (! "could not map page for fixup");
401 fix_offset = 0;
402 }
403
404 fix_address = (Elf32_Word *)(fix_page + fix_offset);
405 fix_address[0] = 0xe51ff004; /* ldr pc, [pc, #-4] */
406 fix_address[1] = value;
407
408 fix_offset += 8;
409 if (fix_offset >= pagesize)
410 fix_page = NULL;
411
412 return (Elf32_Addr)fix_address;
413 }
414
415 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
416 MAP is the object containing the reloc. */
417
418 static inline void
419 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
420 const Elf32_Sym *sym, const struct r_found_version *version,
421 Elf32_Addr *const reloc_addr)
422 {
423 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
424
425 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
426 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
427 {
428 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
429 /* This is defined in rtld.c, but nowhere in the static libc.a;
430 make the reference weak so static programs can still link.
431 This declaration cannot be done when compiling rtld.c
432 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
433 common defn for _dl_rtld_map, which is incompatible with a
434 weak decl in the same file. */
435 # ifndef SHARED
436 weak_extern (_dl_rtld_map);
437 # endif
438 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
439 # endif
440 *reloc_addr += map->l_addr;
441 }
442 # ifndef RTLD_BOOTSTRAP
443 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
444 return;
445 # endif
446 else
447 #endif
448 {
449 const Elf32_Sym *const refsym = sym;
450 Elf32_Addr value = RESOLVE (&sym, version, r_type);
451 if (sym)
452 value += sym->st_value;
453
454 switch (r_type)
455 {
456 case R_ARM_COPY:
457 if (sym == NULL)
458 /* This can happen in trace mode if an object could not be
459 found. */
460 break;
461 if (sym->st_size > refsym->st_size
462 || (GL(dl_verbose) && sym->st_size < refsym->st_size))
463 {
464 const char *strtab;
465
466 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
467 _dl_error_printf ("\
468 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
469 rtld_progname ?: "<program name unknown>",
470 strtab + refsym->st_name);
471 }
472 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
473 refsym->st_size));
474 break;
475 case R_ARM_GLOB_DAT:
476 case R_ARM_JUMP_SLOT:
477 # ifdef RTLD_BOOTSTRAP
478 /* Fix weak undefined references. */
479 if (sym != NULL && sym->st_value == 0)
480 *reloc_addr = 0;
481 else
482 # endif
483 *reloc_addr = value;
484 break;
485 case R_ARM_ABS32:
486 {
487 # ifndef RTLD_BOOTSTRAP
488 /* This is defined in rtld.c, but nowhere in the static
489 libc.a; make the reference weak so static programs can
490 still link. This declaration cannot be done when
491 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
492 rtld.c contains the common defn for _dl_rtld_map, which
493 is incompatible with a weak decl in the same file. */
494 # ifndef SHARED
495 weak_extern (_dl_rtld_map);
496 # endif
497 if (map == &GL(dl_rtld_map))
498 /* Undo the relocation done here during bootstrapping.
499 Now we will relocate it anew, possibly using a
500 binding found in the user program or a loaded library
501 rather than the dynamic linker's built-in definitions
502 used while loading those libraries. */
503 value -= map->l_addr + refsym->st_value;
504 # endif
505 *reloc_addr += value;
506 break;
507 }
508 case R_ARM_PC24:
509 {
510 Elf32_Sword addend;
511 Elf32_Addr newvalue, topbits;
512
513 addend = *reloc_addr & 0x00ffffff;
514 if (addend & 0x00800000) addend |= 0xff000000;
515
516 newvalue = value - (Elf32_Addr)reloc_addr + (addend << 2);
517 topbits = newvalue & 0xfe000000;
518 if (topbits != 0xfe000000 && topbits != 0x00000000)
519 {
520 newvalue = fix_bad_pc24(reloc_addr, value)
521 - (Elf32_Addr)reloc_addr + (addend << 2);
522 topbits = newvalue & 0xfe000000;
523 if (topbits != 0xfe000000 && topbits != 0x00000000)
524 {
525 _dl_signal_error (0, map->l_name, NULL,
526 "R_ARM_PC24 relocation out of range");
527 }
528 }
529 newvalue >>= 2;
530 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
531 *reloc_addr = value;
532 }
533 break;
534 default:
535 _dl_reloc_bad_type (map, r_type, 0);
536 break;
537 }
538 }
539 }
540
541 # ifndef RTLD_BOOTSTRAP
542 static inline void
543 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
544 const Elf32_Sym *sym, const struct r_found_version *version,
545 Elf32_Addr *const reloc_addr)
546 {
547 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
548
549 if (__builtin_expect (r_type == R_ARM_RELATIVE, 0))
550 *reloc_addr = map->l_addr + reloc->r_addend;
551 else if (__builtin_expect (r_type == R_ARM_NONE, 0))
552 return;
553 else
554 {
555 Elf32_Addr value = RESOLVE (&sym, version, r_type);
556 if (sym)
557 value += sym->st_value;
558
559 switch (r_type)
560 {
561 case R_ARM_GLOB_DAT:
562 case R_ARM_JUMP_SLOT:
563 case R_ARM_ABS32:
564 *reloc_addr = value + reloc->r_addend;
565 break;
566 case R_ARM_PC24:
567 {
568 Elf32_Addr newvalue, topbits;
569
570 newvalue = value + reloc->r_addend - (Elf32_Addr)reloc_addr;
571 topbits = newvalue & 0xfe000000;
572 if (topbits != 0xfe000000 && topbits != 0x00000000)
573 {
574 newvalue = fix_bad_pc24(reloc_addr, value)
575 - (Elf32_Addr)reloc_addr + (reloc->r_addend << 2);
576 topbits = newvalue & 0xfe000000;
577 if (topbits != 0xfe000000 && topbits != 0x00000000)
578 {
579 _dl_signal_error (0, map->l_name, NULL,
580 "R_ARM_PC24 relocation out of range");
581 }
582 }
583 newvalue >>= 2;
584 value = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
585 *reloc_addr = value;
586 }
587 break;
588 default:
589 _dl_reloc_bad_type (map, r_type, 0);
590 break;
591 }
592 }
593 }
594 # endif
595
596 static inline void
597 elf_machine_rel_relative (Elf32_Addr l_addr, const Elf32_Rel *reloc,
598 Elf32_Addr *const reloc_addr)
599 {
600 *reloc_addr += l_addr;
601 }
602
603 # ifndef RTLD_BOOTSTRAP
604 static inline void
605 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
606 Elf32_Addr *const reloc_addr)
607 {
608 *reloc_addr = l_addr + reloc->r_addend;
609 }
610 # endif
611
612 static inline void
613 elf_machine_lazy_rel (struct link_map *map,
614 Elf32_Addr l_addr, const Elf32_Rel *reloc)
615 {
616 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
617 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
618 /* Check for unexpected PLT reloc type. */
619 if (__builtin_expect (r_type == R_ARM_JUMP_SLOT, 1))
620 {
621 if (__builtin_expect (map->l_mach.plt, 0) == 0)
622 *reloc_addr += l_addr;
623 else
624 *reloc_addr = map->l_mach.plt;
625 }
626 else
627 _dl_reloc_bad_type (map, r_type, 1);
628 }
629
630 #endif /* RESOLVE */