]> 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, 1996, 1997, 1998, 1999 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 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
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"); \
41 }
42
43 /* Return nonzero iff E_MACHINE is compatible with the running host. */
44 static inline int __attribute__ ((unused))
45 elf_machine_matches_host (Elf32_Half e_machine)
46 {
47 switch (e_machine)
48 {
49 case EM_ARM:
50 return 1;
51 default:
52 return 0;
53 }
54 }
55
56
57 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
58 first element of the GOT. This must be inlined in a function which
59 uses global data. */
60 static inline Elf32_Addr __attribute__ ((unused))
61 elf_machine_dynamic (void)
62 {
63 register Elf32_Addr *got asm ("r10");
64 return *got;
65 }
66
67
68 /* Return the run-time load address of the shared object. */
69 static inline Elf32_Addr __attribute__ ((unused))
70 elf_machine_load_address (void)
71 {
72 extern void __dl_start asm ("_dl_start");
73 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
74 Elf32_Addr pcrel_addr;
75 asm ("adr %0, _dl_start" : "=r" (pcrel_addr));
76 return pcrel_addr - got_addr;
77 }
78
79
80 /* Set up the loaded object described by L so its unrelocated PLT
81 entries will jump to the on-demand fixup code in dl-runtime.c. */
82
83 static inline int __attribute__ ((unused))
84 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
85 {
86 Elf32_Addr *got;
87 extern void _dl_runtime_resolve (Elf32_Word);
88 extern void _dl_runtime_profile (Elf32_Word);
89
90 if (l->l_info[DT_JMPREL] && lazy)
91 {
92 /* patb: this is different than i386 */
93 /* The GOT entries for functions in the PLT have not yet been filled
94 in. Their initial contents will arrange when called to push an
95 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
96 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
97 got = (Elf32_Addr *) l->l_info[DT_PLTGOT]->d_un.d_ptr;
98 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
99
100 /* The got[2] entry contains the address of a function which gets
101 called to get the address of a so far unresolved function and
102 jump to it. The profiling extension of the dynamic linker allows
103 to intercept the calls to collect information. In this case we
104 don't store the address in the GOT so that all future calls also
105 end in this function. */
106 if (profile)
107 {
108 got[2] = (Elf32_Addr) &_dl_runtime_profile;
109 /* Say that we really want profiling and the timers are started. */
110 _dl_profile_map = l;
111 }
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;
116 }
117 return lazy;
118 }
119
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
136
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]
148
149 @ save almost everything; lr is already on the stack
150 stmdb sp!,{r0-r3,sl,fp}
151
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
157
158 @ get pointer to linker struct
159 ldr r0, [lr, #-4]
160
161 @ call fixup routine
162 " CALL_ROUTINE(fixup) "
163
164 @ save the return
165 mov ip, r0
166
167 @ restore the stack
168 ldmia sp!,{r0-r3,sl,fp,lr}
169
170 @ jump to the newly found address
171 mov pc, ip
172
173 .size _dl_runtime_resolve, .-_dl_runtime_resolve
174
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}
181
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
187
188 @ get pointer to linker struct
189 ldr r0, [lr, #-4]
190
191 @ call profiling fixup routine
192 " CALL_ROUTINE(profile_fixup) "
193
194 @ save the return
195 mov ip, r0
196
197 @ restore the stack
198 ldmia sp!,{r0-r3,sl,fp,lr}
199
200 @ jump to the newly found address
201 mov pc, ip
202
203 .size _dl_runtime_resolve, .-_dl_runtime_resolve
204 .previous
205 ");
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]
220
221 @ save almost everything; return add is already on the stack
222 stmdb sp!,{r0-r3,sl,fp}
223
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
229
230 @ get pointer to linker struct
231 ldr r0, [lr, #-4]
232
233 @ call profiling fixup routine
234 " CALL_ROUTINE(fixup) "
235
236 @ save the return
237 mov ip, r0
238
239 @ restore the stack
240 ldmia sp!,{r0-r3,sl,fp,lr}
241
242 @ jump to the newly found address
243 mov pc, ip
244
245 .size _dl_runtime_profile, .-_dl_runtime_profile
246 .previous
247 ");
248 #endif //PROF
249
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
253
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. */
257
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 r1, .L_SKIP_ARGS
280 ldr r1, [sl, r1]
281 @ get the original arg count
282 ldr r0, [sp]
283 @ subtract _dl_skip_args from it
284 sub r0, r0, r1
285 @ adjust the stack pointer to skip them
286 add sp, sp, r1, lsl #2
287 @ store the new argc in the new stack location
288 str r0, [sp]
289
290 @ now we enter a _dl_init_next loop
291 ldr r4, .L_MAIN_SEARCHLIST
292 ldr r4, [sl, r4]
293 ldr r4, [r4]
294 @ call _dl_init_next to get the address of an initalizer
295 0: mov r0, r4
296 bl _dl_init_next(PLT)
297 cmp r0, #0
298 beq 1f
299 @ call the shared-object initializer
300 @ during this call, the stack may get moved around
301 mov lr, pc
302 mov pc, r0
303 @ go back and look for another initializer
304 b 0b
305 1: @ clear the startup flag
306 ldr r2, .L_STARTUP_FLAG
307 ldr r1, [sl, r2]
308 @ we know r0==0 at this point
309 str r0, [r1]
310 @ load the finalizer function
311 ldr r0, .L_FINI_PROC
312 ldr r0, [sl, r0]
313 @ jump to the user_s entry point
314 mov pc, r6
315 .L_GET_GOT:
316 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
317 .L_SKIP_ARGS: \n\
318 .word _dl_skip_args(GOTOFF) \n\
319 .L_STARTUP_FLAG:
320 .word _dl_starting_up(GOT)
321 .L_FINI_PROC:
322 .word _dl_fini(GOT)
323 .L_STACK_END:
324 .word __libc_stack_end(GOT)
325 .L_MAIN_SEARCHLIST:
326 .word _dl_main_searchlist(GOT)
327 .previous\n\
328 ");
329
330 /* Nonzero iff TYPE should not be allowed to resolve to one of
331 the main executable's symbols, as for a COPY reloc. */
332 #define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
333
334 /* Nonzero iff TYPE describes relocation of a PLT entry, so
335 PLT entries should not be allowed to define the value. */
336 #define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
337
338 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
339 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
340
341 /* The ARM never uses Elf32_Rela relocations. */
342 #define ELF_MACHINE_NO_RELA 1
343
344 /* We define an initialization functions. This is called very early in
345 _dl_sysdep_start. */
346 #define DL_PLATFORM_INIT dl_platform_init ()
347
348 extern const char *_dl_platform;
349
350 static inline void __attribute__ ((unused))
351 dl_platform_init (void)
352 {
353 if (_dl_platform == NULL)
354 /* We default to ARM
355 This is where processors could be distinguished arm2, arm6, sa110, etc */
356 _dl_platform = "ARM";
357 }
358
359 static inline void
360 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
361 Elf32_Addr *reloc_addr, Elf32_Addr value)
362 {
363 *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 extern char **_dl_argv;
379
380 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
381 MAP is the object containing the reloc. */
382
383 static inline void
384 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
385 const Elf32_Sym *sym, const struct r_found_version *version,
386 Elf32_Addr *const reloc_addr)
387 {
388 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_RELATIVE)
389 {
390 #ifndef RTLD_BOOTSTRAP
391 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
392 #endif
393 *reloc_addr += map->l_addr;
394 }
395 else if (ELF32_R_TYPE (reloc->r_info) != R_ARM_NONE)
396 {
397 const Elf32_Sym *const refsym = sym;
398 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
399 if (sym)
400 value += sym->st_value;
401
402 switch (ELF32_R_TYPE (reloc->r_info))
403 {
404 case R_ARM_COPY:
405 if (sym == NULL)
406 /* This can happen in trace mode if an object could not be
407 found. */
408 break;
409 if (sym->st_size > refsym->st_size
410 || (_dl_verbose && sym->st_size < refsym->st_size))
411 {
412 const char *strtab;
413
414 strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
415 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
416 ": Symbol `", strtab + refsym->st_name,
417 "' has different size in shared object, "
418 "consider re-linking\n", NULL);
419 }
420 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
421 refsym->st_size));
422 break;
423 case R_ARM_GLOB_DAT:
424 case R_ARM_JUMP_SLOT:
425 #ifdef RTLD_BOOTSTRAP
426 /* Fix weak undefined references. */
427 if (sym != NULL && sym->st_value == 0)
428 *reloc_addr = 0;
429 else
430 #endif
431 *reloc_addr = value;
432 break;
433 case R_ARM_ABS32:
434 {
435 #ifndef RTLD_BOOTSTRAP
436 /* This is defined in rtld.c, but nowhere in the static
437 libc.a; make the reference weak so static programs can
438 still link. This declaration cannot be done when
439 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
440 rtld.c contains the common defn for _dl_rtld_map, which
441 is incompatible with a weak decl in the same file. */
442 weak_extern (_dl_rtld_map);
443 if (map == &_dl_rtld_map)
444 /* Undo the relocation done here during bootstrapping.
445 Now we will relocate it anew, possibly using a
446 binding found in the user program or a loaded library
447 rather than the dynamic linker's built-in definitions
448 used while loading those libraries. */
449 value -= map->l_addr + refsym->st_value;
450 #endif
451 *reloc_addr += value;
452 break;
453 }
454 case R_ARM_PC24:
455 {
456 signed int addend;
457
458 addend = *reloc_addr & 0x00ffffff;
459 if (addend & 0x00800000) addend |= 0xff000000;
460
461 value = value - (unsigned int)reloc_addr + (addend << 2);
462 if (value & 0xfc000003)
463 _dl_signal_error (0, map->l_name,
464 "R_ARM_PC24 relocation out of range");
465
466 value = value >> 2;
467 value = (*reloc_addr & 0xff000000) | (value & 0x00ffffff);
468 *reloc_addr = value;
469 }
470 break;
471 default:
472 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 0);
473 break;
474 }
475 }
476 }
477
478 static inline void
479 elf_machine_lazy_rel (struct link_map *map,
480 Elf32_Addr l_addr, const Elf32_Rel *reloc)
481 {
482 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
483 /* Check for unexpected PLT reloc type. */
484 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_JUMP_SLOT)
485 *reloc_addr += l_addr;
486 else
487 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
488 }
489
490 #endif /* RESOLVE */