Update to 2.1.x development version
[glibc.git] / sysdeps / alpha / dl-machine.h
blob316f717e0105f23095fffaf45f853816b9f3b9a8
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. */
24 #ifndef dl_machine_h
25 #define dl_machine_h 1
27 #define ELF_MACHINE_NAME "alpha"
29 #include <assert.h>
30 #include <string.h>
33 /* Return nonzero iff E_MACHINE is compatible with the running host. */
34 static inline int
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;
48 #else
49 register Elf64_Addr *gp __asm__ ("$29");
50 return gp[-4096];
51 #endif
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. */
71 Elf64_Addr dot;
72 long zero_disp;
74 asm("br %0, 1f\n\t"
75 ".weak __load_address_undefined\n\t"
76 "br $0, __load_address_undefined\n"
77 "1:"
78 : "=r"(dot));
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. */
89 static inline void
90 elf_machine_runtime_setup (struct link_map *l, int lazy)
92 Elf64_Addr plt;
93 extern void _dl_runtime_resolve (void);
95 if (l->l_info[DT_JMPREL] && lazy)
97 /* The GOT entries for the functions in the PLT have not been
98 filled in yet. Their initial contents are directed to the
99 PLT which arranges for the dynamic linker to be called. */
100 plt = l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr;
102 /* This function will be called to perform the relocation. */
103 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_resolve;
105 /* Identify this shared object */
106 *(Elf64_Addr *)(plt + 24) = (Elf64_Addr) l;
110 /* This code is used in dl-runtime.c to call the `fixup' function
111 and then redirect to the address it returns. */
112 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ( \
113 "/* Trampoline for _dl_runtime_resolver */
114 .globl _dl_runtime_resolve
115 .ent _dl_runtime_resolve
116 _dl_runtime_resolve:
117 lda $sp, -168($sp)
118 .frame $sp, 168, $26
119 /* Preserve all registers that C normally doesn't. */
120 stq $26, 0($sp)
121 stq $0, 8($sp)
122 stq $1, 16($sp)
123 stq $2, 24($sp)
124 stq $3, 32($sp)
125 stq $4, 40($sp)
126 stq $5, 48($sp)
127 stq $6, 56($sp)
128 stq $7, 64($sp)
129 stq $8, 72($sp)
130 stq $16, 80($sp)
131 stq $17, 88($sp)
132 stq $18, 96($sp)
133 stq $19, 104($sp)
134 stq $20, 112($sp)
135 stq $21, 120($sp)
136 stq $22, 128($sp)
137 stq $23, 136($sp)
138 stq $24, 144($sp)
139 stq $25, 152($sp)
140 stq $29, 160($sp)
141 .mask 0x27ff01ff, -168
142 /* Set up our $gp */
143 br $gp, 0f
144 0: ldgp $gp, 0($gp)
145 .prologue 1
146 /* Set up the arguments for _dl_runtime_resolve. */
147 /* $16 = link_map out of plt0 */
148 ldq $16, 8($27)
149 /* $17 = offset of reloc entry */
150 mov $28, $17
151 /* Do the fixup */
152 bsr $26, fixup..ng
153 /* Move the destination address into position. */
154 mov $0, $27
155 /* Restore program registers. */
156 ldq $26, 0($sp)
157 ldq $0, 8($sp)
158 ldq $1, 16($sp)
159 ldq $2, 24($sp)
160 ldq $3, 32($sp)
161 ldq $4, 40($sp)
162 ldq $5, 48($sp)
163 ldq $6, 56($sp)
164 ldq $7, 64($sp)
165 ldq $8, 72($sp)
166 ldq $16, 80($sp)
167 ldq $17, 88($sp)
168 ldq $18, 96($sp)
169 ldq $19, 104($sp)
170 ldq $20, 112($sp)
171 ldq $21, 120($sp)
172 ldq $22, 128($sp)
173 ldq $23, 136($sp)
174 ldq $24, 144($sp)
175 ldq $25, 152($sp)
176 ldq $29, 160($sp)
177 /* Flush the Icache after having modified the .plt code. */
179 /* Clean up and turn control to the destination */
180 lda $sp, 168($sp)
181 jmp $31, ($27)
182 .end _dl_runtime_resolve");
184 /* The PLT uses Elf64_Rela relocs. */
185 #define elf_machine_relplt elf_machine_rela
187 /* Initial entry point code for the dynamic linker.
188 The C function `_dl_start' is the real entry point;
189 its return value is the user program's entry point. */
191 #define RTLD_START asm ("\
192 .text
193 .globl _start
194 .ent _start
195 _start:
196 br $gp, 0f
197 0: ldgp $gp, 0($gp)
198 /* Pass pointer to argument block to _dl_start. */
199 mov $sp, $16
200 bsr $26, _dl_start..ng
201 .end _start
202 /* FALLTHRU */
203 .globl _dl_start_user
204 .ent _dl_start_user
205 _dl_start_user:
206 /* Save the user entry point address in s0. */
207 mov $0, $9
208 /* See if we were run as a command with the executable file
209 name as an extra leading argument. If so, adjust the stack
210 pointer to skip _dl_skip_args words. */
211 ldl $1, _dl_skip_args
212 beq $1, 0f
213 ldq $2, 0($sp)
214 subq $2, $1, $2
215 s8addq $1, $sp, $sp
216 stq $2, 0($sp)
217 /* Load _dl_default_scope[2] into s1 to pass to _dl_init_next. */
218 0: ldq $10, _dl_default_scope+16
219 /* Call _dl_init_next to return the address of an initializer
220 function to run. */
221 1: mov $10, $16
222 jsr $26, _dl_init_next
223 ldgp $gp, 0($26)
224 beq $0, 2f
225 mov $0, $27
226 jsr $26, ($0)
227 ldgp $gp, 0($26)
228 br 1b
229 2: /* Clear the startup flag. */
230 .set at
231 stl $31, _dl_starting_up
232 .set noat
233 /* Pass our finalizer function to the user in $0. */
234 lda $0, _dl_fini
235 /* Jump to the user's entry point. */
236 mov $9, $27
237 jmp ($9)
238 .end _dl_start_user");
240 /* Nonzero iff TYPE describes relocation of a PLT entry, so
241 PLT entries should not be allowed to define the value. */
242 #define elf_machine_pltrel_p(type) ((type) == R_ALPHA_JMP_SLOT)
244 /* The alpha never uses Elf64_Rel relocations. */
245 #define ELF_MACHINE_NO_REL 1
247 #endif /* !dl_machine_h */
249 #ifdef RESOLVE
251 /* Fix up the instructions of a PLT entry to invoke the function
252 rather than the dynamic linker. */
253 static inline void
254 elf_alpha_fix_plt(struct link_map *l,
255 const Elf64_Rela *reloc,
256 Elf64_Addr got_addr,
257 Elf64_Addr value)
259 const Elf64_Rela *rela_plt;
260 Elf64_Word *plte;
261 long edisp;
263 /* Recover the PLT entry address by calculating reloc's index into the
264 .rela.plt, and finding that entry in the .plt. */
266 rela_plt = (void *)(l->l_addr + l->l_info[DT_JMPREL]->d_un.d_ptr);
268 plte = (void *)(l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr + 32);
269 plte += 3 * (reloc - rela_plt);
271 /* Find the displacement from the plt entry to the function. */
273 edisp = (long)(value - (Elf64_Addr)&plte[3]) / 4;
275 if (edisp >= -0x100000 && edisp < 0x100000)
277 /* If we are in range, use br to perfect branch prediction and
278 elide the dependency on the address load. This case happens,
279 e.g., when a shared library call is resolved to the same library. */
281 int hi, lo;
282 hi = value - (Elf64_Addr)&plte[0];
283 lo = (short)hi;
284 hi = (hi - lo) >> 16;
286 /* Emit "ldah $27,H($27)" */
287 plte[0] = 0x277b0000 | (hi & 0xffff);
289 /* Emit "lda $27,L($27)" */
290 plte[1] = 0x237b0000 | (lo & 0xffff);
292 /* Emit "br $31,function" */
293 plte[2] = 0xc3e00000 | (edisp & 0x1fffff);
295 else
297 /* Don't bother with the hint since we already know the hint is
298 wrong. Eliding it prevents the wrong page from getting pulled
299 into the cache. */
301 int hi, lo;
302 hi = got_addr - (Elf64_Addr)&plte[0];
303 lo = (short)hi;
304 hi = (hi - lo) >> 16;
306 /* Emit "ldah $27,H($27)" */
307 plte[0] = 0x277b0000 | (hi & 0xffff);
309 /* Emit "ldq $27,L($27)" */
310 plte[1] = 0xa77b0000 | (lo & 0xffff);
312 /* Emit "jmp $31,($27)" */
313 plte[2] = 0x6bfb0000;
316 /* At this point, if we've been doing runtime resolution, Icache is dirty.
317 This will be taken care of in _dl_runtime_resolve. If instead we are
318 doing this as part of non-lazy startup relocation, that bit of code
319 hasn't made it into Icache yet, so there's nothing to clean up. */
322 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
323 MAP is the object containing the reloc. */
324 static inline void
325 elf_machine_rela (struct link_map *map,
326 const Elf64_Rela *reloc,
327 const Elf64_Sym *sym,
328 const hash_name_pair *version)
330 Elf64_Addr * const reloc_addr = (void *)(map->l_addr + reloc->r_offset);
331 unsigned long const r_info = ELF64_R_TYPE (reloc->r_info);
333 #ifndef RTLD_BOOTSTRAP
334 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
335 reference weak so static programs can still link. This declaration
336 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
337 because rtld.c contains the common defn for _dl_rtld_map, which is
338 incompatible with a weak decl in the same file. */
339 weak_extern (_dl_rtld_map);
340 #endif
342 /* We cannot use a switch here because we cannot locate the switch
343 jump table until we've self-relocated. */
345 if (r_info == R_ALPHA_RELATIVE)
347 #ifndef RTLD_BOOTSTRAP
348 /* Already done in dynamic linker. */
349 if (map != &_dl_rtld_map)
350 #endif
351 *reloc_addr += map->l_addr;
353 else if (r_info == R_ALPHA_NONE)
354 return;
355 else
357 Elf64_Addr loadbase, sym_value;
359 loadbase = RESOLVE (&sym, version,
360 r_info == R_ALPHA_JMP_SLOT ? DL_LOOKUP_NOPLT : 0);
361 sym_value = sym ? loadbase + sym->st_value : 0;
363 if (r_info == R_ALPHA_GLOB_DAT)
364 *reloc_addr = sym_value;
365 else if (r_info == R_ALPHA_JMP_SLOT)
367 *reloc_addr = sym_value;
368 elf_alpha_fix_plt (map, reloc, (Elf64_Addr) reloc_addr, sym_value);
370 else if (r_info == R_ALPHA_REFQUAD)
372 sym_value += *reloc_addr;
373 #ifndef RTLD_BOOTSTRAP
374 if (map == &_dl_rtld_map)
376 /* Undo the relocation done here during bootstrapping.
377 Now we will relocate anew, possibly using a binding
378 found in the user program or a loaded library rather
379 than the dynamic linker's built-in definitions used
380 while loading those libraries. */
381 const Elf64_Sym *const dlsymtab
382 = (void *)(map->l_addr + map->l_info[DT_SYMTAB]->d_un.d_ptr);
383 sym_value -= map->l_addr;
384 sym_value -= dlsymtab[ELF64_R_SYM(reloc->r_info)].st_value;
386 else
387 #endif
388 sym_value += reloc->r_addend;
389 *reloc_addr = sym_value;
391 else
392 assert (! "unexpected dynamic reloc type");
396 static inline void
397 elf_machine_lazy_rel (struct link_map *map, const Elf64_Rela *reloc)
399 Elf64_Addr * const reloc_addr = (void *)(map->l_addr + reloc->r_offset);
400 unsigned long const r_info = ELF64_R_TYPE (reloc->r_info);
402 if (r_info == R_ALPHA_JMP_SLOT)
404 /* Perform a RELATIVE reloc on the .got entry that transfers
405 to the .plt. */
406 *reloc_addr += map->l_addr;
408 else if (r_info == R_ALPHA_NONE)
409 return;
410 else
411 assert (! "unexpected PLT reloc type");
414 #endif /* RESOLVE */