]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/arm/dl-machine.h
2000-03-29 Andreas Jaeger <aj@suse.de>
[thirdparty/glibc.git] / sysdeps / arm / dl-machine.h
CommitLineData
04637865 1/* Machine-dependent ELF dynamic relocation inline functions. ARM version.
44bff568 2 Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
04637865
UD
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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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
26de9a82
UD
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
9d1306dd
UD
35#define CLEAR_CACHE(BEG,END) \
36{ \
37 register unsigned long _beg __asm ("a1") = (unsigned long)(BEG); \
2dfdce51 38 register unsigned long _end __asm ("a2") = (unsigned long)(END); \
9d1306dd 39 register unsigned long _flg __asm ("a3") = 0; \
9b1eef96
UD
40 __asm __volatile ("swi 0x9f0002 @ sys_cacheflush" \
41 : /* no outputs */ \
42 : /* no inputs */ \
43 : "a1"); \
9d1306dd
UD
44}
45
04637865
UD
46/* Return nonzero iff E_MACHINE is compatible with the running host. */
47static inline int __attribute__ ((unused))
48elf_machine_matches_host (Elf32_Half e_machine)
49{
50 switch (e_machine)
51 {
52 case EM_ARM:
53 return 1;
54 default:
55 return 0;
56 }
57}
58
59
60/* Return the link-time address of _DYNAMIC. Conveniently, this is the
61 first element of the GOT. This must be inlined in a function which
62 uses global data. */
63static inline Elf32_Addr __attribute__ ((unused))
64elf_machine_dynamic (void)
65{
66 register Elf32_Addr *got asm ("r10");
67 return *got;
68}
69
70
71/* Return the run-time load address of the shared object. */
04637865
UD
72static inline Elf32_Addr __attribute__ ((unused))
73elf_machine_load_address (void)
74{
e79468a2
UD
75 extern void __dl_start asm ("_dl_start");
76 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
77 Elf32_Addr pcrel_addr;
78 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
79 return pcrel_addr - got_addr;
04637865
UD
80}
81
82
83/* Set up the loaded object described by L so its unrelocated PLT
84 entries will jump to the on-demand fixup code in dl-runtime.c. */
85
86static inline int __attribute__ ((unused))
87elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
88{
89 Elf32_Addr *got;
90 extern void _dl_runtime_resolve (Elf32_Word);
91 extern void _dl_runtime_profile (Elf32_Word);
92
93 if (l->l_info[DT_JMPREL] && lazy)
94 {
95 /* patb: this is different than i386 */
96 /* The GOT entries for functions in the PLT have not yet been filled
97 in. Their initial contents will arrange when called to push an
98 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
99 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
a96af7b9 100 got = (Elf32_Addr *) l->l_info[DT_PLTGOT]->d_un.d_ptr;
04637865
UD
101 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
102
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. */
109 if (profile)
110 {
344b4b4e 111 got[2] = (Elf32_Addr) &_dl_runtime_profile;
04637865
UD
112 /* Say that we really want profiling and the timers are started. */
113 _dl_profile_map = l;
114 }
115 else
116 /* This function will get called to fix up the GOT entry indicated by
117 the offset on the stack, and then jump to the resolved address. */
118 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
119 }
120 return lazy;
121}
122
123/* This code is used in dl-runtime.c to call the `fixup' function
124 and then redirect to the address it returns. */
125 // macro for handling PIC situation....
126#ifdef PIC
127#define CALL_ROUTINE(x) " ldr sl,0f
128 add sl, pc, sl
1291: ldr r2, 2f
130 mov lr, pc
131 add pc, sl, r2
132 b 3f
1330: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
1342: .word " #x "(GOTOFF)
1353: "
136#else
137#define CALL_ROUTINE(x) " bl " #x
138#endif
139
140#ifndef PROF
141# define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
142 .text
143 .globl _dl_runtime_resolve
144 .type _dl_runtime_resolve, #function
145 .align 2
146_dl_runtime_resolve:
147 @ we get called with
148 @ stack[0] contains the return address from this call
149 @ ip contains &GOT[n+3] (pointer to function)
150 @ lr points to &GOT[2]
151
152 @ save almost everything; lr is already on the stack
153 stmdb sp!,{r0-r3,sl,fp}
154
155 @ prepare to call fixup()
04637865
UD
156 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
157 sub r1, ip, lr
158 sub r1, r1, #4
159 add r1, r1, r1
160
161 @ get pointer to linker struct
162 ldr r0, [lr, #-4]
163
333014b8 164 @ call fixup routine
04637865
UD
165 " CALL_ROUTINE(fixup) "
166
167 @ save the return
168 mov ip, r0
169
170 @ restore the stack
171 ldmia sp!,{r0-r3,sl,fp,lr}
172
173 @ jump to the newly found address
174 mov pc, ip
175
176 .size _dl_runtime_resolve, .-_dl_runtime_resolve
68d11b26 177
04637865
UD
178 .globl _dl_runtime_profile
179 .type _dl_runtime_profile, #function
180 .align 2
181_dl_runtime_profile:
333014b8
UD
182 @ save almost everything; lr is already on the stack
183 stmdb sp!,{r0-r3,sl,fp}
04637865
UD
184
185 @ prepare to call fixup()
04637865
UD
186 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
187 sub r1, ip, lr
188 sub r1, r1, #4
189 add r1, r1, r1
190
191 @ get pointer to linker struct
192 ldr r0, [lr, #-4]
193
333014b8 194 @ call profiling fixup routine
04637865
UD
195 " CALL_ROUTINE(profile_fixup) "
196
197 @ save the return
198 mov ip, r0
199
200 @ restore the stack
333014b8 201 ldmia sp!,{r0-r3,sl,fp,lr}
04637865
UD
202
203 @ jump to the newly found address
204 mov pc, ip
205
333014b8 206 .size _dl_runtime_resolve, .-_dl_runtime_resolve
04637865
UD
207 .previous
208");
209#else // PROF
210# define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
211 .text
212 .globl _dl_runtime_resolve
213 .globl _dl_runtime_profile
214 .type _dl_runtime_resolve, #function
215 .type _dl_runtime_profile, #function
216 .align 2
217_dl_runtime_resolve:
218_dl_runtime_profile:
333014b8
UD
219 @ we get called with
220 @ stack[0] contains the return address from this call
221 @ ip contains &GOT[n+3] (pointer to function)
222 @ lr points to &GOT[2]
223
224 @ save almost everything; return add is already on the stack
225 stmdb sp!,{r0-r3,sl,fp}
226
227 @ prepare to call fixup()
228 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
04637865
UD
229 sub r1, ip, lr
230 sub r1, r1, #4
231 add r1, r1, r1
333014b8
UD
232
233 @ get pointer to linker struct
04637865 234 ldr r0, [lr, #-4]
333014b8
UD
235
236 @ call profiling fixup routine
04637865 237 " CALL_ROUTINE(fixup) "
333014b8
UD
238
239 @ save the return
04637865 240 mov ip, r0
333014b8
UD
241
242 @ restore the stack
243 ldmia sp!,{r0-r3,sl,fp,lr}
244
245 @ jump to the newly found address
04637865
UD
246 mov pc, ip
247
248 .size _dl_runtime_profile, .-_dl_runtime_profile
249 .previous
250");
251#endif //PROF
252
253/* Mask identifying addresses reserved for the user program,
254 where the dynamic linker should not map anything. */
255#define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
256
257/* Initial entry point code for the dynamic linker.
258 The C function `_dl_start' is the real entry point;
259 its return value is the user program's entry point. */
260
261#define RTLD_START asm ("\
262.text
263.globl _start
264.globl _dl_start_user
265_start:
266 @ at start time, all the args are on the stack
267 mov r0, sp
268 bl _dl_start
269 @ returns user entry point in r0
270_dl_start_user:
271 mov r6, r0
272 @ we are PIC code, so get global offset table
273 ldr sl, .L_GET_GOT
274 add sl, pc, sl
275.L_GOT_GOT:
333014b8
UD
276 @ Store the highest stack address
277 ldr r1, .L_STACK_END
278 ldr r1, [sl, r1]
279 str sp, [r1]
04637865
UD
280 @ See if we were run as a command with the executable file
281 @ name as an extra leading argument.
282 ldr r1, .L_SKIP_ARGS
283 ldr r1, [sl, r1]
284 @ get the original arg count
285 ldr r0, [sp]
286 @ subtract _dl_skip_args from it
287 sub r0, r0, r1
288 @ adjust the stack pointer to skip them
289 add sp, sp, r1, lsl #2
290 @ store the new argc in the new stack location
291 str r0, [sp]
292
293 @ now we enter a _dl_init_next loop
344b4b4e
UD
294 ldr r4, .L_MAIN_SEARCHLIST
295 ldr r4, [sl, r4]
c0e2368b 296 ldr r4, [r4]
04637865
UD
297 @ call _dl_init_next to get the address of an initalizer
2980: mov r0, r4
299 bl _dl_init_next(PLT)
300 cmp r0, #0
301 beq 1f
302 @ call the shared-object initializer
303 @ during this call, the stack may get moved around
304 mov lr, pc
305 mov pc, r0
306 @ go back and look for another initializer
307 b 0b
3081: @ clear the startup flag
309 ldr r2, .L_STARTUP_FLAG
310 ldr r1, [sl, r2]
311 @ we know r0==0 at this point
312 str r0, [r1]
313 @ load the finalizer function
314 ldr r0, .L_FINI_PROC
315 ldr r0, [sl, r0]
316 @ jump to the user_s entry point
317 mov pc, r6
318.L_GET_GOT:
319 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
320.L_SKIP_ARGS: \n\
321 .word _dl_skip_args(GOTOFF) \n\
04637865
UD
322.L_STARTUP_FLAG:
323 .word _dl_starting_up(GOT)
324.L_FINI_PROC:
325 .word _dl_fini(GOT)
333014b8
UD
326.L_STACK_END:
327 .word __libc_stack_end(GOT)
f036f1a9 328.L_MAIN_SEARCHLIST:
344b4b4e 329 .word _dl_main_searchlist(GOT)
04637865
UD
330.previous\n\
331");
332
333/* Nonzero iff TYPE should not be allowed to resolve to one of
334 the main executable's symbols, as for a COPY reloc. */
335#define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
336
337/* Nonzero iff TYPE describes relocation of a PLT entry, so
338 PLT entries should not be allowed to define the value. */
70217455 339#define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
04637865
UD
340
341/* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
70217455 342#define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
04637865 343
56984f46 344/* The ARM never uses Elf32_Rela relocations. */
04637865
UD
345#define ELF_MACHINE_NO_RELA 1
346
347/* We define an initialization functions. This is called very early in
348 _dl_sysdep_start. */
349#define DL_PLATFORM_INIT dl_platform_init ()
350
351extern const char *_dl_platform;
352
353static inline void __attribute__ ((unused))
354dl_platform_init (void)
355{
9b1eef96
UD
356 if (_dl_platform != NULL && *_dl_platform == '\0')
357 /* Avoid an empty string which would disturb us. */
358 _dl_platform = NULL;
04637865
UD
359}
360
361static inline void
362elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
363 Elf32_Addr *reloc_addr, Elf32_Addr value)
364{
365 *reloc_addr = value;
366}
367
368/* Return the final value of a plt relocation. */
369static inline Elf32_Addr
370elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
371 Elf32_Addr value)
372{
373 return value;
374}
375
376#endif /* !dl_machine_h */
377
378#ifdef RESOLVE
379
380extern char **_dl_argv;
381
382/* Perform the relocation specified by RELOC and SYM (which is fully resolved).
383 MAP is the object containing the reloc. */
384
385static inline void
386elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
387 const Elf32_Sym *sym, const struct r_found_version *version,
388 Elf32_Addr *const reloc_addr)
389{
390 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_RELATIVE)
391 {
392#ifndef RTLD_BOOTSTRAP
393 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
394#endif
395 *reloc_addr += map->l_addr;
396 }
397 else if (ELF32_R_TYPE (reloc->r_info) != R_ARM_NONE)
398 {
399 const Elf32_Sym *const refsym = sym;
400 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
401 if (sym)
402 value += sym->st_value;
403
404 switch (ELF32_R_TYPE (reloc->r_info))
405 {
406 case R_ARM_COPY:
407 if (sym == NULL)
408 /* This can happen in trace mode if an object could not be
409 found. */
410 break;
411 if (sym->st_size > refsym->st_size
412 || (_dl_verbose && sym->st_size < refsym->st_size))
413 {
414 const char *strtab;
415
44bff568 416 strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
04637865
UD
417 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
418 ": Symbol `", strtab + refsym->st_name,
419 "' has different size in shared object, "
420 "consider re-linking\n", NULL);
421 }
422 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
423 refsym->st_size));
424 break;
425 case R_ARM_GLOB_DAT:
70217455 426 case R_ARM_JUMP_SLOT:
44bff568 427#ifdef RTLD_BOOTSTRAP
e7782e5e 428 /* Fix weak undefined references. */
44bff568 429 if (sym != NULL && sym->st_value == 0)
e7782e5e
UD
430 *reloc_addr = 0;
431 else
432#endif
433 *reloc_addr = value;
04637865 434 break;
70217455 435 case R_ARM_ABS32:
04637865
UD
436 {
437#ifndef RTLD_BOOTSTRAP
438 /* This is defined in rtld.c, but nowhere in the static
439 libc.a; make the reference weak so static programs can
440 still link. This declaration cannot be done when
441 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
442 rtld.c contains the common defn for _dl_rtld_map, which
443 is incompatible with a weak decl in the same file. */
444 weak_extern (_dl_rtld_map);
445 if (map == &_dl_rtld_map)
446 /* Undo the relocation done here during bootstrapping.
447 Now we will relocate it anew, possibly using a
448 binding found in the user program or a loaded library
449 rather than the dynamic linker's built-in definitions
450 used while loading those libraries. */
451 value -= map->l_addr + refsym->st_value;
452#endif
453 *reloc_addr += value;
454 break;
455 }
d1af493c
UD
456 case R_ARM_PC24:
457 {
458 signed int addend;
459
460 addend = *reloc_addr & 0x00ffffff;
461 if (addend & 0x00800000) addend |= 0xff000000;
462
463 value = value - (unsigned int)reloc_addr + (addend << 2);
464 if (value & 0xfc000003)
465 _dl_signal_error (0, map->l_name,
466 "R_ARM_PC24 relocation out of range");
467
468 value = value >> 2;
469 value = (*reloc_addr & 0xff000000) | (value & 0x00ffffff);
470 *reloc_addr = value;
471 }
472 break;
04637865 473 default:
893f3d10 474 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 0);
04637865
UD
475 break;
476 }
477 }
478}
479
480static inline void
893f3d10
RM
481elf_machine_lazy_rel (struct link_map *map,
482 Elf32_Addr l_addr, const Elf32_Rel *reloc)
04637865 483{
68d11b26 484 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
344b4b4e 485 /* Check for unexpected PLT reloc type. */
893f3d10
RM
486 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_JUMP_SLOT)
487 *reloc_addr += l_addr;
488 else
489 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
04637865
UD
490}
491
492#endif /* RESOLVE */