Update.
[glibc.git] / sysdeps / arm / dl-machine.h
blobb8a1e19359c75e7ee8d08e79fbcb03ad84c8fda2
1 /* Machine-dependent ELF dynamic relocation inline functions. ARM version.
2 Copyright (C) 1995, 1996, 1997, 1998 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 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.
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.
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. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "ARM"
25 #include <sys/param.h>
27 #include <assert.h>
29 /* Return nonzero iff E_MACHINE is compatible with the running host. */
30 static inline int __attribute__ ((unused))
31 elf_machine_matches_host (Elf32_Half e_machine)
33 switch (e_machine)
35 case EM_ARM:
36 return 1;
37 default:
38 return 0;
43 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
44 first element of the GOT. This must be inlined in a function which
45 uses global data. */
46 static inline Elf32_Addr __attribute__ ((unused))
47 elf_machine_dynamic (void)
49 register Elf32_Addr *got asm ("r10");
50 return *got;
54 /* Return the run-time load address of the shared object. */
55 // patb
56 static inline Elf32_Addr __attribute__ ((unused))
57 elf_machine_load_address (void)
59 Elf32_Addr addr;
60 asm (" ldr ip,.L1
61 ldr r3,.L3
62 add r3, r3, sl
63 ldr ip,[sl, ip]
64 sub ip, r3, ip
65 b .L2
66 .L1: .word _dl_start(GOT)
67 .L3: .word _dl_start(GOTOFF)
68 .L2: mov %0, ip"
69 : "=r" (addr) : : "ip", "r3");
70 return addr;
74 /* Set up the loaded object described by L so its unrelocated PLT
75 entries will jump to the on-demand fixup code in dl-runtime.c. */
77 static inline int __attribute__ ((unused))
78 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
80 Elf32_Addr *got;
81 extern void _dl_runtime_resolve (Elf32_Word);
82 extern void _dl_runtime_profile (Elf32_Word);
84 if (l->l_info[DT_JMPREL] && lazy)
86 /* patb: this is different than i386 */
87 /* The GOT entries for functions in the PLT have not yet been filled
88 in. Their initial contents will arrange when called to push an
89 index into the .got section, load ip with &_GLOBAL_OFFSET_TABLE_[3],
90 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
91 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
92 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
94 /* The got[2] entry contains the address of a function which gets
95 called to get the address of a so far unresolved function and
96 jump to it. The profiling extension of the dynamic linker allows
97 to intercept the calls to collect information. In this case we
98 don't store the address in the GOT so that all future calls also
99 end in this function. */
100 if (profile)
102 got[2] = (Elf32_Addr) &_dl_runtime_profile;
103 /* Say that we really want profiling and the timers are started. */
104 _dl_profile_map = l;
106 else
107 /* This function will get called to fix up the GOT entry indicated by
108 the offset on the stack, and then jump to the resolved address. */
109 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
111 return lazy;
114 /* This code is used in dl-runtime.c to call the `fixup' function
115 and then redirect to the address it returns. */
116 // macro for handling PIC situation....
117 #ifdef PIC
118 #define CALL_ROUTINE(x) " ldr sl,0f
119 add sl, pc, sl
120 1: ldr r2, 2f
121 mov lr, pc
122 add pc, sl, r2
123 b 3f
124 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
125 2: .word " #x "(GOTOFF)
126 3: "
127 #else
128 #define CALL_ROUTINE(x) " bl " #x
129 #endif
131 #ifndef PROF
132 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
133 .text
134 .globl _dl_runtime_resolve
135 .type _dl_runtime_resolve, #function
136 .align 2
137 _dl_runtime_resolve:
138 @ we get called with
139 @ stack[0] contains the return address from this call
140 @ ip contains &GOT[n+3] (pointer to function)
141 @ lr points to &GOT[2]
143 @ save almost everything; lr is already on the stack
144 stmdb sp!,{r0-r3,sl,fp}
146 @ prepare to call fixup()
147 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
148 sub r1, ip, lr
149 sub r1, r1, #4
150 add r1, r1, r1
152 @ get pointer to linker struct
153 ldr r0, [lr, #-4]
155 @ call fixup routine
156 " CALL_ROUTINE(fixup) "
158 @ save the return
159 mov ip, r0
161 @ restore the stack
162 ldmia sp!,{r0-r3,sl,fp,lr}
164 @ jump to the newly found address
165 mov pc, ip
167 .size _dl_runtime_resolve, .-_dl_runtime_resolve
169 .globl _dl_runtime_profile
170 .type _dl_runtime_profile, #function
171 .align 2
172 _dl_runtime_profile:
173 @ save almost everything; lr is already on the stack
174 stmdb sp!,{r0-r3,sl,fp}
176 @ prepare to call fixup()
177 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
178 sub r1, ip, lr
179 sub r1, r1, #4
180 add r1, r1, r1
182 @ get pointer to linker struct
183 ldr r0, [lr, #-4]
185 @ call profiling fixup routine
186 " CALL_ROUTINE(profile_fixup) "
188 @ save the return
189 mov ip, r0
191 @ restore the stack
192 ldmia sp!,{r0-r3,sl,fp,lr}
194 @ jump to the newly found address
195 mov pc, ip
197 .size _dl_runtime_resolve, .-_dl_runtime_resolve
198 .previous
200 #else // PROF
201 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
202 .text
203 .globl _dl_runtime_resolve
204 .globl _dl_runtime_profile
205 .type _dl_runtime_resolve, #function
206 .type _dl_runtime_profile, #function
207 .align 2
208 _dl_runtime_resolve:
209 _dl_runtime_profile:
210 @ we get called with
211 @ stack[0] contains the return address from this call
212 @ ip contains &GOT[n+3] (pointer to function)
213 @ lr points to &GOT[2]
215 @ save almost everything; return add is already on the stack
216 stmdb sp!,{r0-r3,sl,fp}
218 @ prepare to call fixup()
219 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
220 sub r1, ip, lr
221 sub r1, r1, #4
222 add r1, r1, r1
224 @ get pointer to linker struct
225 ldr r0, [lr, #-4]
227 @ call profiling fixup routine
228 " CALL_ROUTINE(fixup) "
230 @ save the return
231 mov ip, r0
233 @ restore the stack
234 ldmia sp!,{r0-r3,sl,fp,lr}
236 @ jump to the newly found address
237 mov pc, ip
239 .size _dl_runtime_profile, .-_dl_runtime_profile
240 .previous
242 #endif //PROF
244 /* Mask identifying addresses reserved for the user program,
245 where the dynamic linker should not map anything. */
246 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
248 /* Initial entry point code for the dynamic linker.
249 The C function `_dl_start' is the real entry point;
250 its return value is the user program's entry point. */
252 #define RTLD_START asm ("\
253 .text
254 .globl _start
255 .globl _dl_start_user
256 _start:
257 @ at start time, all the args are on the stack
258 mov r0, sp
259 bl _dl_start
260 @ returns user entry point in r0
261 _dl_start_user:
262 mov r6, r0
263 @ we are PIC code, so get global offset table
264 ldr sl, .L_GET_GOT
265 add sl, pc, sl
266 .L_GOT_GOT:
267 @ Store the highest stack address
268 ldr r1, .L_STACK_END
269 ldr r1, [sl, r1]
270 str sp, [r1]
271 @ See if we were run as a command with the executable file
272 @ name as an extra leading argument.
273 ldr r1, .L_SKIP_ARGS
274 ldr r1, [sl, r1]
275 @ get the original arg count
276 ldr r0, [sp]
277 @ subtract _dl_skip_args from it
278 sub r0, r0, r1
279 @ adjust the stack pointer to skip them
280 add sp, sp, r1, lsl #2
281 @ store the new argc in the new stack location
282 str r0, [sp]
284 @ now we enter a _dl_init_next loop
285 ldr r4, .L_MAIN_SEARCHLIST
286 ldr r4, [sl, r4]
287 ldr r4, [r4]
288 @ call _dl_init_next to get the address of an initalizer
289 0: mov r0, r4
290 bl _dl_init_next(PLT)
291 cmp r0, #0
292 beq 1f
293 @ call the shared-object initializer
294 @ during this call, the stack may get moved around
295 mov lr, pc
296 mov pc, r0
297 @ go back and look for another initializer
298 b 0b
299 1: @ clear the startup flag
300 ldr r2, .L_STARTUP_FLAG
301 ldr r1, [sl, r2]
302 @ we know r0==0 at this point
303 str r0, [r1]
304 @ load the finalizer function
305 ldr r0, .L_FINI_PROC
306 ldr r0, [sl, r0]
307 @ jump to the user_s entry point
308 mov pc, r6
309 .L_GET_GOT:
310 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
311 .L_SKIP_ARGS: \n\
312 .word _dl_skip_args(GOTOFF) \n\
313 .L_STARTUP_FLAG:
314 .word _dl_starting_up(GOT)
315 .L_FINI_PROC:
316 .word _dl_fini(GOT)
317 .L_STACK_END:
318 .word __libc_stack_end(GOT)
319 .L_MAIN_SEARCHLIST:
320 .word _dl_main_searchlist(GOT)
321 .previous\n\
324 /* Nonzero iff TYPE should not be allowed to resolve to one of
325 the main executable's symbols, as for a COPY reloc. */
326 #define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
328 /* Nonzero iff TYPE describes relocation of a PLT entry, so
329 PLT entries should not be allowed to define the value. */
330 #define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
332 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
333 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
335 /* The ARM never uses Elf32_Rela relocations. */
336 #define ELF_MACHINE_NO_RELA 1
338 /* We define an initialization functions. This is called very early in
339 _dl_sysdep_start. */
340 #define DL_PLATFORM_INIT dl_platform_init ()
342 extern const char *_dl_platform;
344 static inline void __attribute__ ((unused))
345 dl_platform_init (void)
347 if (_dl_platform == NULL)
348 /* We default to ARM
349 This is where processors could be distinguished arm2, arm6, sa110, etc */
350 _dl_platform = "ARM";
353 static inline void
354 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
355 Elf32_Addr *reloc_addr, Elf32_Addr value)
357 *reloc_addr = value;
360 /* Return the final value of a plt relocation. */
361 static inline Elf32_Addr
362 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
363 Elf32_Addr value)
365 return value;
368 #endif /* !dl_machine_h */
370 #ifdef RESOLVE
372 extern char **_dl_argv;
374 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
375 MAP is the object containing the reloc. */
377 static inline void
378 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
379 const Elf32_Sym *sym, const struct r_found_version *version,
380 Elf32_Addr *const reloc_addr)
382 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_RELATIVE)
384 #ifndef RTLD_BOOTSTRAP
385 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
386 #endif
387 *reloc_addr += map->l_addr;
389 else if (ELF32_R_TYPE (reloc->r_info) != R_ARM_NONE)
391 const Elf32_Sym *const refsym = sym;
392 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
393 if (sym)
394 value += sym->st_value;
396 switch (ELF32_R_TYPE (reloc->r_info))
398 case R_ARM_COPY:
399 if (sym == NULL)
400 /* This can happen in trace mode if an object could not be
401 found. */
402 break;
403 if (sym->st_size > refsym->st_size
404 || (_dl_verbose && sym->st_size < refsym->st_size))
406 const char *strtab;
408 strtab = ((const char *) map->l_addr
409 + map->l_info[DT_STRTAB]->d_un.d_ptr);
410 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
411 ": Symbol `", strtab + refsym->st_name,
412 "' has different size in shared object, "
413 "consider re-linking\n", NULL);
415 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
416 refsym->st_size));
417 break;
418 case R_ARM_GLOB_DAT:
419 case R_ARM_JUMP_SLOT:
420 #ifdef RTLD_BOOTSTRAP
421 /* Fix weak undefined references. */
422 if (sym != NULL && sym->st_value == 0)
423 *reloc_addr = 0;
424 else
425 #endif
426 *reloc_addr = value;
427 break;
428 case R_ARM_ABS32:
430 #ifndef RTLD_BOOTSTRAP
431 /* This is defined in rtld.c, but nowhere in the static
432 libc.a; make the reference weak so static programs can
433 still link. This declaration cannot be done when
434 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
435 rtld.c contains the common defn for _dl_rtld_map, which
436 is incompatible with a weak decl in the same file. */
437 weak_extern (_dl_rtld_map);
438 if (map == &_dl_rtld_map)
439 /* Undo the relocation done here during bootstrapping.
440 Now we will relocate it anew, possibly using a
441 binding found in the user program or a loaded library
442 rather than the dynamic linker's built-in definitions
443 used while loading those libraries. */
444 value -= map->l_addr + refsym->st_value;
445 #endif
446 *reloc_addr += value;
447 break;
449 default:
450 assert (! "unexpected dynamic reloc type");
451 break;
456 static inline void
457 elf_machine_lazy_rel (Elf32_Addr l_addr, const Elf32_Rel *reloc)
459 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
460 /* Check for unexpected PLT reloc type. */
461 assert (ELF32_R_TYPE (reloc->r_info) == R_ARM_JUMP_SLOT);
462 *reloc_addr += l_addr;
465 #endif /* RESOLVE */