Update.
[glibc.git] / sysdeps / arm / dl-machine.h
blobc40f9d72601e5ca35c5da23179509eebaef1f2b6
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 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
104 /* Say that we really want profiling and the timers are started. */
105 _dl_profile_map = l;
107 else
108 /* This function will get called to fix up the GOT entry indicated by
109 the offset on the stack, and then jump to the resolved address. */
110 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
112 return lazy;
115 /* This code is used in dl-runtime.c to call the `fixup' function
116 and then redirect to the address it returns. */
117 // macro for handling PIC situation....
118 #ifdef PIC
119 #define CALL_ROUTINE(x) " ldr sl,0f
120 add sl, pc, sl
121 1: ldr r2, 2f
122 mov lr, pc
123 add pc, sl, r2
124 b 3f
125 0: .word _GLOBAL_OFFSET_TABLE_ - 1b - 4
126 2: .word " #x "(GOTOFF)
127 3: "
128 #else
129 #define CALL_ROUTINE(x) " bl " #x
130 #endif
132 #ifndef PROF
133 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
134 .text
135 .globl _dl_runtime_resolve
136 .type _dl_runtime_resolve, #function
137 .align 2
138 _dl_runtime_resolve:
139 @ we get called with
140 @ stack[0] contains the return address from this call
141 @ ip contains &GOT[n+3] (pointer to function)
142 @ lr points to &GOT[2]
144 @ save almost everything; lr is already on the stack
145 stmdb sp!,{r0-r3,sl,fp}
147 @ prepare to call fixup()
148 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
149 sub r1, ip, lr
150 sub r1, r1, #4
151 add r1, r1, r1
153 @ get pointer to linker struct
154 ldr r0, [lr, #-4]
156 @ call fixup routine
157 " CALL_ROUTINE(fixup) "
159 @ save the return
160 mov ip, r0
162 @ restore the stack
163 ldmia sp!,{r0-r3,sl,fp,lr}
165 @ jump to the newly found address
166 mov pc, ip
168 .size _dl_runtime_resolve, .-_dl_runtime_resolve
170 .globl _dl_runtime_profile
171 .type _dl_runtime_profile, #function
172 .align 2
173 _dl_runtime_profile:
174 @ save almost everything; lr is already on the stack
175 stmdb sp!,{r0-r3,sl,fp}
177 @ prepare to call fixup()
178 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
179 sub r1, ip, lr
180 sub r1, r1, #4
181 add r1, r1, r1
183 @ get pointer to linker struct
184 ldr r0, [lr, #-4]
186 @ call profiling fixup routine
187 " CALL_ROUTINE(profile_fixup) "
189 @ save the return
190 mov ip, r0
192 @ restore the stack
193 ldmia sp!,{r0-r3,sl,fp,lr}
195 @ jump to the newly found address
196 mov pc, ip
198 .size _dl_runtime_resolve, .-_dl_runtime_resolve
199 .previous
201 #else // PROF
202 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
203 .text
204 .globl _dl_runtime_resolve
205 .globl _dl_runtime_profile
206 .type _dl_runtime_resolve, #function
207 .type _dl_runtime_profile, #function
208 .align 2
209 _dl_runtime_resolve:
210 _dl_runtime_profile:
211 @ we get called with
212 @ stack[0] contains the return address from this call
213 @ ip contains &GOT[n+3] (pointer to function)
214 @ lr points to &GOT[2]
216 @ save almost everything; return add is already on the stack
217 stmdb sp!,{r0-r3,sl,fp}
219 @ prepare to call fixup()
220 @ change &GOT[n+3] into 8*n NOTE: reloc are 8 bytes each
221 sub r1, ip, lr
222 sub r1, r1, #4
223 add r1, r1, r1
225 @ get pointer to linker struct
226 ldr r0, [lr, #-4]
228 @ call profiling fixup routine
229 " CALL_ROUTINE(fixup) "
231 @ save the return
232 mov ip, r0
234 @ restore the stack
235 ldmia sp!,{r0-r3,sl,fp,lr}
237 @ jump to the newly found address
238 mov pc, ip
240 .size _dl_runtime_profile, .-_dl_runtime_profile
241 .previous
243 #endif //PROF
245 /* Mask identifying addresses reserved for the user program,
246 where the dynamic linker should not map anything. */
247 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
249 /* Initial entry point code for the dynamic linker.
250 The C function `_dl_start' is the real entry point;
251 its return value is the user program's entry point. */
253 #define RTLD_START asm ("\
254 .text
255 .globl _start
256 .globl _dl_start_user
257 _start:
258 @ at start time, all the args are on the stack
259 mov r0, sp
260 bl _dl_start
261 @ returns user entry point in r0
262 _dl_start_user:
263 mov r6, r0
264 @ we are PIC code, so get global offset table
265 ldr sl, .L_GET_GOT
266 add sl, pc, sl
267 .L_GOT_GOT:
268 @ Store the highest stack address
269 ldr r1, .L_STACK_END
270 ldr r1, [sl, r1]
271 str sp, [r1]
272 @ See if we were run as a command with the executable file
273 @ name as an extra leading argument.
274 ldr r1, .L_SKIP_ARGS
275 ldr r1, [sl, r1]
276 @ get the original arg count
277 ldr r0, [sp]
278 @ subtract _dl_skip_args from it
279 sub r0, r0, r1
280 @ adjust the stack pointer to skip them
281 add sp, sp, r1, lsl #2
282 @ store the new argc in the new stack location
283 str r0, [sp]
285 @ now we enter a _dl_init_next loop
286 ldr r2, .L_DEF_SCOPE
287 ldr r2, [sl, r2]
288 ldr r4, [r2, #8]
289 @ call _dl_init_next to get the address of an initalizer
290 0: mov r0, r4
291 bl _dl_init_next(PLT)
292 cmp r0, #0
293 beq 1f
294 @ call the shared-object initializer
295 @ during this call, the stack may get moved around
296 mov lr, pc
297 mov pc, r0
298 @ go back and look for another initializer
299 b 0b
300 1: @ clear the startup flag
301 ldr r2, .L_STARTUP_FLAG
302 ldr r1, [sl, r2]
303 @ we know r0==0 at this point
304 str r0, [r1]
305 @ load the finalizer function
306 ldr r0, .L_FINI_PROC
307 ldr r0, [sl, r0]
308 @ jump to the user_s entry point
309 mov pc, r6
310 .L_GET_GOT:
311 .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4 \n\
312 .L_SKIP_ARGS: \n\
313 .word _dl_skip_args(GOTOFF) \n\
314 .L_DEF_SCOPE: \n\
315 .word _dl_default_scope(GOT) \n\
316 .L_STARTUP_FLAG:
317 .word _dl_starting_up(GOT)
318 .L_FINI_PROC:
319 .word _dl_fini(GOT)
320 .L_STACK_END:
321 .word __libc_stack_end(GOT)
322 .previous\n\
325 /* Nonzero iff TYPE should not be allowed to resolve to one of
326 the main executable's symbols, as for a COPY reloc. */
327 #define elf_machine_lookup_noexec_p(type) ((type) == R_ARM_COPY)
329 /* Nonzero iff TYPE describes relocation of a PLT entry, so
330 PLT entries should not be allowed to define the value. */
331 #define elf_machine_lookup_noplt_p(type) ((type) == R_ARM_JUMP_SLOT)
333 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
334 #define ELF_MACHINE_JMP_SLOT R_ARM_JUMP_SLOT
336 /* The ARM never uses Elf32_Rela relocations. */
337 #define ELF_MACHINE_NO_RELA 1
339 /* We define an initialization functions. This is called very early in
340 _dl_sysdep_start. */
341 #define DL_PLATFORM_INIT dl_platform_init ()
343 extern const char *_dl_platform;
345 static inline void __attribute__ ((unused))
346 dl_platform_init (void)
348 if (_dl_platform == NULL)
349 /* We default to ARM
350 This is where processors could be distinguished arm2, arm6, sa110, etc */
351 _dl_platform = "ARM";
354 static inline void
355 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
356 Elf32_Addr *reloc_addr, Elf32_Addr value)
358 *reloc_addr = value;
361 /* Return the final value of a plt relocation. */
362 static inline Elf32_Addr
363 elf_machine_plt_value (struct link_map *map, const Elf32_Rel *reloc,
364 Elf32_Addr value)
366 return value;
369 #endif /* !dl_machine_h */
371 #ifdef RESOLVE
373 extern char **_dl_argv;
375 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
376 MAP is the object containing the reloc. */
378 static inline void
379 elf_machine_rel (struct link_map *map, const Elf32_Rel *reloc,
380 const Elf32_Sym *sym, const struct r_found_version *version,
381 Elf32_Addr *const reloc_addr)
383 if (ELF32_R_TYPE (reloc->r_info) == R_ARM_RELATIVE)
385 #ifndef RTLD_BOOTSTRAP
386 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
387 #endif
388 *reloc_addr += map->l_addr;
390 else if (ELF32_R_TYPE (reloc->r_info) != R_ARM_NONE)
392 const Elf32_Sym *const refsym = sym;
393 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
394 if (sym)
395 value += sym->st_value;
397 switch (ELF32_R_TYPE (reloc->r_info))
399 case R_ARM_COPY:
400 if (sym == NULL)
401 /* This can happen in trace mode if an object could not be
402 found. */
403 break;
404 if (sym->st_size > refsym->st_size
405 || (_dl_verbose && sym->st_size < refsym->st_size))
407 const char *strtab;
409 strtab = ((const char *) map->l_addr
410 + map->l_info[DT_STRTAB]->d_un.d_ptr);
411 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
412 ": Symbol `", strtab + refsym->st_name,
413 "' has different size in shared object, "
414 "consider re-linking\n", NULL);
416 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
417 refsym->st_size));
418 break;
419 case R_ARM_GLOB_DAT:
420 case R_ARM_JUMP_SLOT:
421 *reloc_addr = value;
422 break;
423 case R_ARM_ABS32:
425 #ifndef RTLD_BOOTSTRAP
426 /* This is defined in rtld.c, but nowhere in the static
427 libc.a; make the reference weak so static programs can
428 still link. This declaration cannot be done when
429 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
430 rtld.c contains the common defn for _dl_rtld_map, which
431 is incompatible with a weak decl in the same file. */
432 weak_extern (_dl_rtld_map);
433 if (map == &_dl_rtld_map)
434 /* Undo the relocation done here during bootstrapping.
435 Now we will relocate it anew, possibly using a
436 binding found in the user program or a loaded library
437 rather than the dynamic linker's built-in definitions
438 used while loading those libraries. */
439 value -= map->l_addr + refsym->st_value;
440 #endif
441 *reloc_addr += value;
442 break;
444 default:
445 assert (! "unexpected dynamic reloc type");
446 break;
451 static inline void
452 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rel *reloc)
454 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
455 switch (ELF32_R_TYPE (reloc->r_info))
457 case R_ARM_JUMP_SLOT:
458 *reloc_addr += map->l_addr;
459 break;
460 default:
461 assert (! "unexpected PLT reloc type");
462 break;
466 #endif /* RESOLVE */