]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/alpha/dl-machine.h
Update.
[thirdparty/glibc.git] / sysdeps / alpha / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. Alpha version.
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Richard Henderson <rth@tamu.edu>.
5
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.
10
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.
15
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. */
20
21 /* This was written in the absence of an ABI -- don't expect
22 it to remain unchanged. */
23
24 #ifndef dl_machine_h
25 #define dl_machine_h 1
26
27 #define ELF_MACHINE_NAME "alpha"
28
29 #include <assert.h>
30 #include <string.h>
31
32
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)
36 {
37 return e_machine == EM_ALPHA;
38 }
39
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)
45 {
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
52 }
53
54 /* Return the run-time load address of the shared object. */
55 static inline Elf64_Addr
56 elf_machine_load_address (void)
57 {
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. */
70
71 Elf64_Addr dot;
72 long zero_disp;
73
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));
79
80 zero_disp = *(int *)dot;
81 zero_disp = (zero_disp << 43) >> 41;
82
83 return dot + 4 + zero_disp;
84 }
85
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. */
88
89 static inline int
90 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
91 {
92 Elf64_Addr plt;
93 extern void _dl_runtime_resolve (void);
94 extern void _dl_runtime_profile (void);
95
96 if (l->l_info[DT_JMPREL] && lazy)
97 {
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;
102
103 /* This function will be called to perform the relocation. */
104 if (!profile)
105 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_resolve;
106 else
107 {
108 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_profile;
109 /* Say that we really want profiling and the timers are started. */
110 _dl_profile_map = l;
111 }
112
113 /* Identify this shared object */
114 *(Elf64_Addr *)(plt + 24) = (Elf64_Addr) l;
115
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);
119 }
120
121 return lazy;
122 }
123
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 "
128 .ent " #tramp_name "
129 " #tramp_name ":
130 lda $sp, -168($sp)
131 .frame $sp, 168, $26
132 /* Preserve all registers that C normally doesn't. */
133 stq $26, 0($sp)
134 stq $0, 8($sp)
135 stq $1, 16($sp)
136 stq $2, 24($sp)
137 stq $3, 32($sp)
138 stq $4, 40($sp)
139 stq $5, 48($sp)
140 stq $6, 56($sp)
141 stq $7, 64($sp)
142 stq $8, 72($sp)
143 stq $16, 80($sp)
144 stq $17, 88($sp)
145 stq $18, 96($sp)
146 stq $19, 104($sp)
147 stq $20, 112($sp)
148 stq $21, 120($sp)
149 stq $22, 128($sp)
150 stq $23, 136($sp)
151 stq $24, 144($sp)
152 stq $25, 152($sp)
153 stq $29, 160($sp)
154 .mask 0x27ff01ff, -168
155 /* Set up our $gp */
156 br $gp, .+4
157 ldgp $gp, 0($gp)
158 .prologue 1
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 */
163 subq $28, $27, $17
164 ldq $16, 8($27)
165 subq $17, 20, $17
166 mov $26, $18
167 addq $17, $17, $17
168 /* Do the fixup */
169 bsr $26, " ASM_ALPHA_NG_SYMBOL_PREFIX #fixup_name "..ng
170 /* Move the destination address into position. */
171 mov $0, $27
172 /* Restore program registers. */
173 ldq $26, 0($sp)
174 ldq $0, 8($sp)
175 ldq $1, 16($sp)
176 ldq $2, 24($sp)
177 ldq $3, 32($sp)
178 ldq $4, 40($sp)
179 ldq $5, 48($sp)
180 ldq $6, 56($sp)
181 ldq $7, 64($sp)
182 ldq $8, 72($sp)
183 ldq $16, 80($sp)
184 ldq $17, 88($sp)
185 ldq $18, 96($sp)
186 ldq $19, 104($sp)
187 ldq $20, 112($sp)
188 ldq $21, 120($sp)
189 ldq $22, 128($sp)
190 ldq $23, 136($sp)
191 ldq $24, 144($sp)
192 ldq $25, 152($sp)
193 ldq $29, 160($sp)
194 /* Flush the Icache after having modified the .plt code. */
195 " #IMB "
196 /* Clean up and turn control to the destination */
197 lda $sp, 168($sp)
198 jmp $31, ($27)
199 .end " #tramp_name)
200
201 #ifndef PROF
202 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
203 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup, imb); \
204 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup, #nop);
205 #else
206 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
207 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup, imb); \
208 extern void _dl_runtime_resolve (void); \
209 extern void _dl_runtime_profile (void); \
210 strong_alias (_dl_runtime_resolve, _dl_runtime_profile);
211 #endif
212
213 /* Initial entry point code for the dynamic linker.
214 The C function `_dl_start' is the real entry point;
215 its return value is the user program's entry point. */
216
217 #define RTLD_START asm ("\
218 .text
219 .globl _start
220 .ent _start
221 _start:
222 br $gp, 0f
223 0: ldgp $gp, 0($gp)
224 /* Pass pointer to argument block to _dl_start. */
225 mov $sp, $16
226 bsr $26, "ASM_ALPHA_NG_SYMBOL_PREFIX"_dl_start..ng
227 .end _start
228 /* FALLTHRU */
229 .globl _dl_start_user
230 .ent _dl_start_user
231 _dl_start_user:
232 /* Save the user entry point address in s0. */
233 mov $0, $9
234 /* See if we were run as a command with the executable file
235 name as an extra leading argument. If so, adjust the stack
236 pointer to skip _dl_skip_args words. */
237 ldl $1, _dl_skip_args
238 beq $1, 0f
239 ldq $2, 0($sp)
240 subq $2, $1, $2
241 s8addq $1, $sp, $sp
242 stq $2, 0($sp)
243 /* Load _dl_default_scope[2] into s1 to pass to _dl_init_next. */
244 0: ldq $10, _dl_default_scope+16
245 /* Call _dl_init_next to return the address of an initializer
246 function to run. */
247 1: mov $10, $16
248 jsr $26, _dl_init_next
249 ldgp $gp, 0($26)
250 beq $0, 2f
251 mov $0, $27
252 jsr $26, ($0)
253 ldgp $gp, 0($26)
254 br 1b
255 2: /* Clear the startup flag. */
256 .set at
257 stl $31, _dl_starting_up
258 .set noat
259 /* Pass our finalizer function to the user in $0. */
260 lda $0, _dl_fini
261 /* Jump to the user's entry point. */
262 mov $9, $27
263 jmp ($9)
264 .end _dl_start_user
265 .previous");
266
267 /* Nonzero iff TYPE describes relocation of a PLT entry, so
268 PLT entries should not be allowed to define the value. */
269 #define elf_machine_lookup_noplt_p(type) ((type) == R_ALPHA_JMP_SLOT)
270
271 /* Nonzero iff TYPE should not be allowed to resolve to one of
272 the main executable's symbols, as for a COPY reloc, which we don't use. */
273 #define elf_machine_lookup_noexec_p(type) (0)
274
275 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
276 #define ELF_MACHINE_JMP_SLOT R_ALPHA_JMP_SLOT
277
278 /* The alpha never uses Elf64_Rel relocations. */
279 #define ELF_MACHINE_NO_REL 1
280
281 /* Fix up the instructions of a PLT entry to invoke the function
282 rather than the dynamic linker. */
283 static inline void
284 elf_machine_fixup_plt(struct link_map *l, const Elf64_Rela *reloc,
285 Elf64_Addr *got_addr, Elf64_Addr value)
286 {
287 const Elf64_Rela *rela_plt;
288 Elf64_Word *plte;
289 long edisp;
290
291 /* Store the value we are going to load. */
292 *got_addr = value;
293
294 /* Recover the PLT entry address by calculating reloc's index into the
295 .rela.plt, and finding that entry in the .plt. */
296 rela_plt = (void *)(l->l_addr + l->l_info[DT_JMPREL]->d_un.d_ptr);
297 plte = (void *)(l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr + 32);
298 plte += 3 * (reloc - rela_plt);
299
300 /* Find the displacement from the plt entry to the function. */
301 edisp = (long)(value - (Elf64_Addr)&plte[3]) / 4;
302
303 if (edisp >= -0x100000 && edisp < 0x100000)
304 {
305 /* If we are in range, use br to perfect branch prediction and
306 elide the dependency on the address load. This case happens,
307 e.g., when a shared library call is resolved to the same library. */
308
309 int hi, lo;
310 hi = value - (Elf64_Addr)&plte[0];
311 lo = (short)hi;
312 hi = (hi - lo) >> 16;
313
314 /* Emit "lda $27,lo($27)" */
315 plte[1] = 0x237b0000 | (lo & 0xffff);
316
317 /* Emit "br $31,function" */
318 plte[2] = 0xc3e00000 | (edisp & 0x1fffff);
319
320 /* Think about thread-safety -- the previous instructions must be
321 committed to memory before the first is overwritten. */
322 __asm__ __volatile__("wmb" : : : "memory");
323
324 /* Emit "ldah $27,hi($27)" */
325 plte[0] = 0x277b0000 | (hi & 0xffff);
326 }
327 else
328 {
329 /* Don't bother with the hint since we already know the hint is
330 wrong. Eliding it prevents the wrong page from getting pulled
331 into the cache. */
332
333 int hi, lo;
334 hi = (Elf64_Addr)got_addr - (Elf64_Addr)&plte[0];
335 lo = (short)hi;
336 hi = (hi - lo) >> 16;
337
338 /* Emit "ldq $27,lo($27)" */
339 plte[1] = 0xa77b0000 | (lo & 0xffff);
340
341 /* Emit "jmp $31,($27)" */
342 plte[2] = 0x6bfb0000;
343
344 /* Think about thread-safety -- the previous instructions must be
345 committed to memory before the first is overwritten. */
346 __asm__ __volatile__("wmb" : : : "memory");
347
348 /* Emit "ldah $27,hi($27)" */
349 plte[0] = 0x277b0000 | (hi & 0xffff);
350 }
351
352 /* At this point, if we've been doing runtime resolution, Icache is dirty.
353 This will be taken care of in _dl_runtime_resolve. If instead we are
354 doing this as part of non-lazy startup relocation, that bit of code
355 hasn't made it into Icache yet, so there's nothing to clean up. */
356 }
357
358 /* Return the final value of a plt relocation. */
359 static inline Elf64_Addr
360 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
361 Elf64_Addr value)
362 {
363 return value + reloc->r_addend;
364 }
365
366 #endif /* !dl_machine_h */
367
368 #ifdef RESOLVE
369
370 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
371 MAP is the object containing the reloc. */
372 static inline void
373 elf_machine_rela (struct link_map *map,
374 const Elf64_Rela *reloc,
375 const Elf64_Sym *sym,
376 const struct r_found_version *version,
377 Elf64_Addr *const reloc_addr)
378 {
379 unsigned long const r_type = ELF64_R_TYPE (reloc->r_info);
380
381 #ifndef RTLD_BOOTSTRAP
382 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
383 reference weak so static programs can still link. This declaration
384 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
385 because rtld.c contains the common defn for _dl_rtld_map, which is
386 incompatible with a weak decl in the same file. */
387 weak_extern (_dl_rtld_map);
388 #endif
389
390 /* We cannot use a switch here because we cannot locate the switch
391 jump table until we've self-relocated. */
392
393 if (r_type == R_ALPHA_RELATIVE)
394 {
395 #ifndef RTLD_BOOTSTRAP
396 /* Already done in dynamic linker. */
397 if (map != &_dl_rtld_map)
398 #endif
399 *reloc_addr += map->l_addr;
400 }
401 else if (r_type == R_ALPHA_NONE)
402 return;
403 else
404 {
405 Elf64_Addr loadbase, sym_value;
406
407 loadbase = RESOLVE (&sym, version, r_type);
408 sym_value = sym ? loadbase + sym->st_value : 0;
409 sym_value += reloc->r_addend;
410
411 if (r_type == R_ALPHA_GLOB_DAT)
412 *reloc_addr = sym_value;
413 else if (r_type == R_ALPHA_JMP_SLOT)
414 elf_machine_fixup_plt (map, reloc, reloc_addr, sym_value);
415 else if (r_type == R_ALPHA_REFQUAD)
416 {
417 sym_value += *reloc_addr;
418 #ifndef RTLD_BOOTSTRAP
419 if (map == &_dl_rtld_map)
420 {
421 /* Undo the relocation done here during bootstrapping.
422 Now we will relocate anew, possibly using a binding
423 found in the user program or a loaded library rather
424 than the dynamic linker's built-in definitions used
425 while loading those libraries. */
426 const Elf64_Sym *const dlsymtab
427 = (void *)(map->l_addr + map->l_info[DT_SYMTAB]->d_un.d_ptr);
428 sym_value -= map->l_addr;
429 sym_value -= dlsymtab[ELF64_R_SYM(reloc->r_info)].st_value;
430 sym_value -= reloc->r_addend;
431 }
432 #endif
433 *reloc_addr = sym_value;
434 }
435 else
436 assert (! "unexpected dynamic reloc type");
437 }
438 }
439
440 static inline void
441 elf_machine_lazy_rel (struct link_map *map, const Elf64_Rela *reloc)
442 {
443 Elf64_Addr * const reloc_addr = (void *)(map->l_addr + reloc->r_offset);
444 unsigned long const r_type = ELF64_R_TYPE (reloc->r_info);
445
446 if (r_type == R_ALPHA_JMP_SLOT)
447 {
448 /* Perform a RELATIVE reloc on the .got entry that transfers
449 to the .plt. */
450 *reloc_addr += map->l_addr;
451 }
452 else if (r_type == R_ALPHA_NONE)
453 return;
454 else
455 assert (! "unexpected PLT reloc type");
456 }
457
458 #endif /* RESOLVE */