1 /* Machine-dependent ELF dynamic relocation inline functions. Alpha version.
2 Copyright (C) 1996 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
18 not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, 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 return (Elf64_Addr
) &_DYNAMIC
;
49 /* Return the run-time load address of the shared object. */
50 static inline Elf64_Addr
51 elf_machine_load_address (void)
53 /* NOTE: While it is generally unfriendly to put data in the text
54 segment, it is only slightly less so when the "data" is an
55 instruction. While we don't have to worry about GLD just yet, an
56 optimizing linker might decide that our "data" is an unreachable
57 instruction and throw it away -- with the right switches, DEC's
58 linker will do this. What ought to happen is we should add
59 something to GAS to allow us access to the new GPREL_HI32/LO32
60 relocation types stolen from OSF/1 3.0. */
61 /* This code relies on the fact that BRADDR relocations do not
62 appear in dynamic relocation tables. Not that that would be very
63 useful anyway -- br/bsr has a 4MB range and the shared libraries
64 are usually many many terabytes away. */
70 ".weak __load_address_undefined\n\t"
71 "br $0, __load_address_undefined\n"
75 zero_disp
= *(int *)dot
;
76 zero_disp
= (zero_disp
<< 43) >> 41;
78 return dot
+ 4 + zero_disp
;
81 /* Set up the loaded object described by L so its unrelocated PLT
82 entries will jump to the on-demand fixup code in dl-runtime.c. */
85 elf_machine_runtime_setup (struct link_map
*l
, int lazy
)
88 extern void _dl_runtime_resolve (void);
90 if (l
->l_info
[DT_JMPREL
] && lazy
)
92 /* The GOT entries for the functions in the PLT have not been
93 filled in yet. Their initial contents are directed to the
94 PLT which arranges for the dynamic linker to be called. */
95 plt
= l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
;
97 /* This function will be called to perform the relocation. */
98 *(Elf64_Addr
*)(plt
+ 16) = (Elf64_Addr
) &_dl_runtime_resolve
;
100 /* Identify this shared object */
101 *(Elf64_Addr
*)(plt
+ 24) = (Elf64_Addr
) l
;
105 /* This code is used in dl-runtime.c to call the `fixup' function
106 and then redirect to the address it returns. */
107 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ( \
108 "/* Trampoline for _dl_runtime_resolver */
109 .globl _dl_runtime_resolve
110 .ent _dl_runtime_resolve
114 /* Preserve all registers that C normally doesn't. */
136 .mask 0x27ff01ff, -168
141 /* Set up the arguments for _dl_runtime_resolve. */
142 /* $16 = link_map out of plt0 */
144 /* $17 = offset of reloc entry */
148 /* Move the destination address into position. */
150 /* Restore program registers. */
172 /* Flush the Icache after having modified the .plt code. */
174 /* Clean up and turn control to the destination */
177 .end _dl_runtime_resolve");
179 /* The PLT uses Elf64_Rela relocs. */
180 #define elf_machine_relplt elf_machine_rela
182 /* Initial entry point code for the dynamic linker.
183 The C function `_dl_start' is the real entry point;
184 its return value is the user program's entry point. */
186 #define RTLD_START asm ("\
193 /* Pass pointer to argument block to _dl_start. */
195 bsr $26, _dl_start..ng
198 .globl _dl_start_user
201 /* Save the user entry point address in s0. */
203 /* See if we were run as a command with the executable file
204 name as an extra leading argument. If so, adjust the stack
205 pointer to skip _dl_skip_args words. */
206 ldl $1, _dl_skip_args
212 /* Load _dl_default_scope[2] into s1 to pass to _dl_init_next. */
213 0: ldq $10, _dl_default_scope+16
214 /* Call _dl_init_next to return the address of an initializer
217 jsr $26, _dl_init_next
224 2: /* Clear the startup flag. */
226 stl $31, _dl_starting_up
228 /* Pass our finalizer function to the user in $0. */
230 /* Jump to the user's entry point. */
233 .end _dl_start_user");
235 /* Nonzero iff TYPE describes relocation of a PLT entry, so
236 PLT entries should not be allowed to define the value. */
237 #define elf_machine_pltrel_p(type) ((type) == R_ALPHA_JMP_SLOT)
239 /* The alpha never uses Elf64_Rel relocations. */
240 #define ELF_MACHINE_NO_REL 1
242 #endif /* !dl_machine_h */
246 /* Fix up the instructions of a PLT entry to invoke the function
247 rather than the dynamic linker. */
249 elf_alpha_fix_plt(struct link_map
*l
,
250 const Elf64_Rela
*reloc
,
254 const Elf64_Rela
*rela_plt
;
258 /* Recover the PLT entry address by calculating reloc's index into the
259 .rela.plt, and finding that entry in the .plt. */
261 rela_plt
= (void *)(l
->l_addr
+ l
->l_info
[DT_JMPREL
]->d_un
.d_ptr
);
263 plte
= (void *)(l
->l_addr
+ l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
+ 32);
264 plte
+= 3 * (reloc
- rela_plt
);
266 /* Find the displacement from the plt entry to the function. */
268 edisp
= (long)(value
- (Elf64_Addr
)&plte
[3]) / 4;
270 if (edisp
>= -0x100000 && edisp
< 0x100000)
272 /* If we are in range, use br to perfect branch prediction and
273 elide the dependency on the address load. This case happens,
274 e.g., when a shared library call is resolved to the same library. */
277 hi
= value
- (Elf64_Addr
)&plte
[0];
279 hi
= (hi
- lo
) >> 16;
281 /* Emit "ldah $27,H($27)" */
282 plte
[0] = 0x277b0000 | (hi
& 0xffff);
284 /* Emit "lda $27,L($27)" */
285 plte
[1] = 0x237b0000 | (lo
& 0xffff);
287 /* Emit "br $31,function" */
288 plte
[2] = 0xc3e00000 | (edisp
& 0x1fffff);
292 /* Don't bother with the hint since we already know the hint is
293 wrong. Eliding it prevents the wrong page from getting pulled
297 hi
= got_addr
- (Elf64_Addr
)&plte
[0];
299 hi
= (hi
- lo
) >> 16;
301 /* Emit "ldah $27,H($27)" */
302 plte
[0] = 0x277b0000 | (hi
& 0xffff);
304 /* Emit "ldq $27,L($27)" */
305 plte
[1] = 0xa77b0000 | (lo
& 0xffff);
307 /* Emit "jmp $31,($27)" */
308 plte
[2] = 0x6bfb0000;
311 /* At this point, if we've been doing runtime resolution, Icache is dirty.
312 This will be taken care of in _dl_runtime_resolve. If instead we are
313 doing this as part of non-lazy startup relocation, that bit of code
314 hasn't made it into Icache yet, so there's nothing to clean up. */
317 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
318 MAP is the object containing the reloc. */
320 elf_machine_rela (struct link_map
*map
,
321 const Elf64_Rela
*reloc
,
322 const Elf64_Sym
*sym
)
324 Elf64_Addr
* const reloc_addr
= (void *)(map
->l_addr
+ reloc
->r_offset
);
325 unsigned long const r_info
= ELF64_R_TYPE (reloc
->r_info
);
327 #ifndef RTLD_BOOTSTRAP
328 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
329 reference weak so static programs can still link. This declaration
330 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
331 because rtld.c contains the common defn for _dl_rtld_map, which is
332 incompatible with a weak decl in the same file. */
333 weak_extern (_dl_rtld_map
);
336 /* We cannot use a switch here because we cannot locate the switch
337 jump table until we've self-relocated. */
339 if (r_info
== R_ALPHA_RELATIVE
)
341 #ifndef RTLD_BOOTSTRAP
342 /* Already done in dynamic linker. */
343 if (map
!= &_dl_rtld_map
)
345 *reloc_addr
+= map
->l_addr
;
347 else if (r_info
== R_ALPHA_NONE
)
351 Elf64_Addr loadbase
, sym_value
;
353 loadbase
= RESOLVE (&sym
,
354 r_info
== R_ALPHA_JMP_SLOT
? DL_LOOKUP_NOPLT
: 0);
355 sym_value
= sym
? loadbase
+ sym
->st_value
: 0;
357 if (r_info
== R_ALPHA_GLOB_DAT
)
358 *reloc_addr
= sym_value
;
359 else if (r_info
== R_ALPHA_JMP_SLOT
)
361 *reloc_addr
= sym_value
;
362 elf_alpha_fix_plt (map
, reloc
, (Elf64_Addr
) reloc_addr
, sym_value
);
364 else if (r_info
== R_ALPHA_REFQUAD
)
366 sym_value
+= *reloc_addr
;
367 #ifndef RTLD_BOOTSTRAP
368 if (map
== &_dl_rtld_map
)
370 /* Undo the relocation done here during bootstrapping.
371 Now we will relocate anew, possibly using a binding
372 found in the user program or a loaded library rather
373 than the dynamic linker's built-in definitions used
374 while loading those libraries. */
375 const Elf64_Sym
*const dlsymtab
376 = (void *)(map
->l_addr
+ map
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
377 sym_value
-= map
->l_addr
;
378 sym_value
-= dlsymtab
[ELF64_R_SYM(reloc
->r_info
)].st_value
;
382 sym_value
+= reloc
->r_addend
;
383 *reloc_addr
= sym_value
;
386 assert (! "unexpected dynamic reloc type");
391 elf_machine_lazy_rel (struct link_map
*map
, const Elf64_Rela
*reloc
)
393 Elf64_Addr
* const reloc_addr
= (void *)(map
->l_addr
+ reloc
->r_offset
);
394 unsigned long const r_info
= ELF64_R_TYPE (reloc
->r_info
);
396 if (r_info
== R_ALPHA_JMP_SLOT
)
398 /* Perform a RELATIVE reloc on the .got entry that transfers
400 *reloc_addr
+= map
->l_addr
;
402 else if (r_info
== R_ALPHA_NONE
)
405 assert (! "unexpected PLT reloc type");