]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/alpha/dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. Alpha version.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Richard Henderson <rth@tamu.edu>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This was written in the absence of an ABI -- don't expect
22 it to remain unchanged. */
25 #define dl_machine_h 1
27 #define ELF_MACHINE_NAME "alpha"
33 /* Return nonzero iff E_MACHINE is compatible with the running host. */
35 elf_machine_matches_host (Elf64_Word e_machine
)
37 return e_machine
== EM_ALPHA
;
40 /* Return the link-time address of _DYNAMIC. The multiple-got-capable
41 linker no longer allocates the first .got entry for this. But not to
42 worry, no special tricks are needed. */
43 static inline Elf64_Addr
44 elf_machine_dynamic (void)
46 #ifndef NO_AXP_MULTI_GOT_LD
47 return (Elf64_Addr
) &_DYNAMIC
;
49 register Elf64_Addr
*gp
__asm__ ("$29");
54 /* Return the run-time load address of the shared object. */
55 static inline Elf64_Addr
56 elf_machine_load_address (void)
58 /* NOTE: While it is generally unfriendly to put data in the text
59 segment, it is only slightly less so when the "data" is an
60 instruction. While we don't have to worry about GLD just yet, an
61 optimizing linker might decide that our "data" is an unreachable
62 instruction and throw it away -- with the right switches, DEC's
63 linker will do this. What ought to happen is we should add
64 something to GAS to allow us access to the new GPREL_HI32/LO32
65 relocation types stolen from OSF/1 3.0. */
66 /* This code relies on the fact that BRADDR relocations do not
67 appear in dynamic relocation tables. Not that that would be very
68 useful anyway -- br/bsr has a 4MB range and the shared libraries
69 are usually many many terabytes away. */
75 ".weak __load_address_undefined\n\t"
76 "br $0, __load_address_undefined\n"
80 zero_disp
= *(int *)dot
;
81 zero_disp
= (zero_disp
<< 43) >> 41;
83 return dot
+ 4 + zero_disp
;
86 /* Set up the loaded object described by L so its unrelocated PLT
87 entries will jump to the on-demand fixup code in dl-runtime.c. */
90 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
93 extern void _dl_runtime_resolve (void);
94 extern void _dl_runtime_profile (void);
96 if (l
->l_info
[DT_JMPREL
] && lazy
)
98 /* The GOT entries for the functions in the PLT have not been
99 filled in yet. Their initial contents are directed to the
100 PLT which arranges for the dynamic linker to be called. */
101 plt
= l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
;
103 /* This function will be called to perform the relocation. */
105 *(Elf64_Addr
*)(plt
+ 16) = (Elf64_Addr
) &_dl_runtime_resolve
;
108 *(Elf64_Addr
*)(plt
+ 16) = (Elf64_Addr
) &_dl_runtime_profile
;
109 /* Say that we really want profiling and the timers are started. */
113 /* Identify this shared object */
114 *(Elf64_Addr
*)(plt
+ 24) = (Elf64_Addr
) l
;
116 /* If the first instruction of the plt entry is not
117 "br $28, plt0", we cannot do lazy relocation. */
118 lazy
= (*(unsigned *)(plt
+ 32) == 0xc39ffff7);
124 /* This code is used in dl-runtime.c to call the `fixup' function
125 and then redirect to the address it returns. */
126 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name, IMB) asm ( "\
127 .globl " #tramp_name "
132 /* Preserve all registers that C normally doesn't. */
154 .mask 0x27ff01ff, -168
159 /* Set up the arguments for fixup: */
160 /* $16 = link_map out of plt0 */
161 /* $17 = offset of reloc entry = ($28 - $27 - 20) /12 * 24 */
162 /* $18 = return address */
169 bsr $26, " ASM_ALPHA_NG_SYMBOL_PREFIX #fixup_name "..ng
170 /* Move the destination address into position. */
172 /* Restore program registers. */
194 /* Flush the Icache after having modified the .plt code. */
196 /* Clean up and turn control to the destination */
202 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
203 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup, imb); \
204 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup, #nop);
206 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
207 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup, imb); \
208 strong_alias (_dl_runtime_resolve, _dl_runtime_profile, #nop);
211 /* Initial entry point code for the dynamic linker.
212 The C function `_dl_start' is the real entry point;
213 its return value is the user program's entry point. */
215 #define RTLD_START asm ("\
222 /* Pass pointer to argument block to _dl_start. */
224 bsr $26, "ASM_ALPHA_NG_SYMBOL_PREFIX"_dl_start..ng
227 .globl _dl_start_user
230 /* Save the user entry point address in s0. */
232 /* See if we were run as a command with the executable file
233 name as an extra leading argument. If so, adjust the stack
234 pointer to skip _dl_skip_args words. */
235 ldl $1, _dl_skip_args
241 /* Load _dl_default_scope[2] into s1 to pass to _dl_init_next. */
242 0: ldq $10, _dl_default_scope+16
243 /* Call _dl_init_next to return the address of an initializer
246 jsr $26, _dl_init_next
253 2: /* Clear the startup flag. */
255 stl $31, _dl_starting_up
257 /* Pass our finalizer function to the user in $0. */
259 /* Jump to the user's entry point. */
265 /* Nonzero iff TYPE describes relocation of a PLT entry, so
266 PLT entries should not be allowed to define the value. */
267 #define elf_machine_lookup_noplt_p(type) ((type) == R_ALPHA_JMP_SLOT)
269 /* Nonzero iff TYPE should not be allowed to resolve to one of
270 the main executable's symbols, as for a COPY reloc, which we don't use. */
271 #define elf_machine_lookup_noexec_p(type) (0)
273 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
274 #define ELF_MACHINE_JMP_SLOT R_ALPHA_JMP_SLOT
276 /* The alpha never uses Elf64_Rel relocations. */
277 #define ELF_MACHINE_NO_REL 1
279 /* Fix up the instructions of a PLT entry to invoke the function
280 rather than the dynamic linker. */
282 elf_machine_fixup_plt(struct link_map
*l
, const Elf64_Rela
*reloc
,
283 Elf64_Addr
*got_addr
, Elf64_Addr value
)
285 const Elf64_Rela
*rela_plt
;
289 /* Store the value we are going to load. */
292 /* Recover the PLT entry address by calculating reloc's index into the
293 .rela.plt, and finding that entry in the .plt. */
294 rela_plt
= (void *)(l
->l_addr
+ l
->l_info
[DT_JMPREL
]->d_un
.d_ptr
);
295 plte
= (void *)(l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
+ 32);
296 plte
+= 3 * (reloc
- rela_plt
);
298 /* Find the displacement from the plt entry to the function. */
299 edisp
= (long)(value
- (Elf64_Addr
)&plte
[3]) / 4;
301 if (edisp
>= -0x100000 && edisp
< 0x100000)
303 /* If we are in range, use br to perfect branch prediction and
304 elide the dependency on the address load. This case happens,
305 e.g., when a shared library call is resolved to the same library. */
308 hi
= value
- (Elf64_Addr
)&plte
[0];
310 hi
= (hi
- lo
) >> 16;
312 /* Emit "lda $27,lo($27)" */
313 plte
[1] = 0x237b0000 | (lo
& 0xffff);
315 /* Emit "br $31,function" */
316 plte
[2] = 0xc3e00000 | (edisp
& 0x1fffff);
318 /* Think about thread-safety -- the previous instructions must be
319 committed to memory before the first is overwritten. */
320 __asm__
__volatile__("wmb" : : : "memory");
322 /* Emit "ldah $27,hi($27)" */
323 plte
[0] = 0x277b0000 | (hi
& 0xffff);
327 /* Don't bother with the hint since we already know the hint is
328 wrong. Eliding it prevents the wrong page from getting pulled
332 hi
= (Elf64_Addr
)got_addr
- (Elf64_Addr
)&plte
[0];
334 hi
= (hi
- lo
) >> 16;
336 /* Emit "ldq $27,lo($27)" */
337 plte
[1] = 0xa77b0000 | (lo
& 0xffff);
339 /* Emit "jmp $31,($27)" */
340 plte
[2] = 0x6bfb0000;
342 /* Think about thread-safety -- the previous instructions must be
343 committed to memory before the first is overwritten. */
344 __asm__
__volatile__("wmb" : : : "memory");
346 /* Emit "ldah $27,hi($27)" */
347 plte
[0] = 0x277b0000 | (hi
& 0xffff);
350 /* At this point, if we've been doing runtime resolution, Icache is dirty.
351 This will be taken care of in _dl_runtime_resolve. If instead we are
352 doing this as part of non-lazy startup relocation, that bit of code
353 hasn't made it into Icache yet, so there's nothing to clean up. */
356 /* Return the final value of a plt relocation. */
357 static inline Elf64_Addr
358 elf_machine_plt_value (struct link_map
*map
, const Elf64_Rela
*reloc
,
361 return value
+ reloc
->r_addend
;
364 #endif /* !dl_machine_h */
368 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
369 MAP is the object containing the reloc. */
371 elf_machine_rela (struct link_map
*map
,
372 const Elf64_Rela
*reloc
,
373 const Elf64_Sym
*sym
,
374 const struct r_found_version
*version
,
375 Elf64_Addr
*const reloc_addr
)
377 unsigned long const r_type
= ELF64_R_TYPE (reloc
->r_info
);
379 #ifndef RTLD_BOOTSTRAP
380 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
381 reference weak so static programs can still link. This declaration
382 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
383 because rtld.c contains the common defn for _dl_rtld_map, which is
384 incompatible with a weak decl in the same file. */
385 weak_extern (_dl_rtld_map
);
388 /* We cannot use a switch here because we cannot locate the switch
389 jump table until we've self-relocated. */
391 if (r_type
== R_ALPHA_RELATIVE
)
393 #ifndef RTLD_BOOTSTRAP
394 /* Already done in dynamic linker. */
395 if (map
!= &_dl_rtld_map
)
397 *reloc_addr
+= map
->l_addr
;
399 else if (r_type
== R_ALPHA_NONE
)
403 Elf64_Addr loadbase
, sym_value
;
405 loadbase
= RESOLVE (&sym
, version
, r_type
);
406 sym_value
= sym
? loadbase
+ sym
->st_value
: 0;
407 sym_value
+= reloc
->r_addend
;
409 if (r_type
== R_ALPHA_GLOB_DAT
)
410 *reloc_addr
= sym_value
;
411 else if (r_type
== R_ALPHA_JMP_SLOT
)
412 elf_machine_fixup_plt (map
, reloc
, reloc_addr
, sym_value
);
413 else if (r_type
== R_ALPHA_REFQUAD
)
415 sym_value
+= *reloc_addr
;
416 #ifndef RTLD_BOOTSTRAP
417 if (map
== &_dl_rtld_map
)
419 /* Undo the relocation done here during bootstrapping.
420 Now we will relocate anew, possibly using a binding
421 found in the user program or a loaded library rather
422 than the dynamic linker's built-in definitions used
423 while loading those libraries. */
424 const Elf64_Sym
*const dlsymtab
425 = (void *)(map
->l_addr
+ map
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
426 sym_value
-= map
->l_addr
;
427 sym_value
-= dlsymtab
[ELF64_R_SYM(reloc
->r_info
)].st_value
;
428 sym_value
-= reloc
->r_addend
;
431 *reloc_addr
= sym_value
;
434 assert (! "unexpected dynamic reloc type");
439 elf_machine_lazy_rel (struct link_map
*map
, const Elf64_Rela
*reloc
)
441 Elf64_Addr
* const reloc_addr
= (void *)(map
->l_addr
+ reloc
->r_offset
);
442 unsigned long const r_type
= ELF64_R_TYPE (reloc
->r_info
);
444 if (r_type
== R_ALPHA_JMP_SLOT
)
446 /* Perform a RELATIVE reloc on the .got entry that transfers
448 *reloc_addr
+= map
->l_addr
;
450 else if (r_type
== R_ALPHA_NONE
)
453 assert (! "unexpected PLT reloc type");