* sysdeps/unix/sysv/linux/m68k/bits/fcntl.h: New file.
[glibc.git] / sysdeps / alpha / dl-machine.h
blob33c32fa5e8095d5ac62ebc317aeac985848176c3
1 /* Machine-dependent ELF dynamic relocation inline functions. Alpha version.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 <string.h>
32 /* Return nonzero iff E_MACHINE is compatible with the running host. */
33 static inline int
34 elf_machine_matches_host (Elf64_Word e_machine)
36 return e_machine == EM_ALPHA;
39 /* Return the link-time address of _DYNAMIC. The multiple-got-capable
40 linker no longer allocates the first .got entry for this. But not to
41 worry, no special tricks are needed. */
42 static inline Elf64_Addr
43 elf_machine_dynamic (void)
45 #ifndef NO_AXP_MULTI_GOT_LD
46 return (Elf64_Addr) &_DYNAMIC;
47 #else
48 register Elf64_Addr *gp __asm__ ("$29");
49 return gp[-4096];
50 #endif
53 /* Return the run-time load address of the shared object. */
54 static inline Elf64_Addr
55 elf_machine_load_address (void)
57 /* NOTE: While it is generally unfriendly to put data in the text
58 segment, it is only slightly less so when the "data" is an
59 instruction. While we don't have to worry about GLD just yet, an
60 optimizing linker might decide that our "data" is an unreachable
61 instruction and throw it away -- with the right switches, DEC's
62 linker will do this. What ought to happen is we should add
63 something to GAS to allow us access to the new GPREL_HI32/LO32
64 relocation types stolen from OSF/1 3.0. */
65 /* This code relies on the fact that BRADDR relocations do not
66 appear in dynamic relocation tables. Not that that would be very
67 useful anyway -- br/bsr has a 4MB range and the shared libraries
68 are usually many many terabytes away. */
70 Elf64_Addr dot;
71 long int zero_disp;
73 asm("br %0, 1f\n\t"
74 ".weak __load_address_undefined\n\t"
75 "br $0, __load_address_undefined\n"
76 "1:"
77 : "=r"(dot));
79 zero_disp = *(int *)dot;
80 zero_disp = (zero_disp << 43) >> 41;
82 return dot + 4 + zero_disp;
85 /* Set up the loaded object described by L so its unrelocated PLT
86 entries will jump to the on-demand fixup code in dl-runtime.c. */
88 static inline int
89 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
91 Elf64_Addr plt;
92 extern void _dl_runtime_resolve (void);
93 extern void _dl_runtime_profile (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 = D_PTR (l, l_info[DT_PLTGOT]);
102 /* This function will be called to perform the relocation. */
103 if (!profile)
104 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_resolve;
105 else
107 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_profile;
109 if (_dl_name_match_p (_dl_profile, l))
111 /* This is the object we are looking for. Say that we really
112 want profiling and the timers are started. */
113 _dl_profile_map = l;
117 /* Identify this shared object */
118 *(Elf64_Addr *)(plt + 24) = (Elf64_Addr) l;
120 /* If the first instruction of the plt entry is not
121 "br $28, plt0", we cannot do lazy relocation. */
122 lazy = (*(unsigned int *)(plt + 32) == 0xc39ffff7);
125 return lazy;
128 /* This code is used in dl-runtime.c to call the `fixup' function
129 and then redirect to the address it returns. */
130 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name, IMB) \
131 extern void tramp_name (void); \
132 asm ( "\
133 .globl " #tramp_name "
134 .ent " #tramp_name "
135 " #tramp_name ":
136 lda $sp, -44*8($sp)
137 .frame $sp, 44*8, $26
138 /* Preserve all integer registers that C normally doesn't. */
139 stq $26, 0*8($sp)
140 stq $0, 1*8($sp)
141 stq $1, 2*8($sp)
142 stq $2, 3*8($sp)
143 stq $3, 4*8($sp)
144 stq $4, 5*8($sp)
145 stq $5, 6*8($sp)
146 stq $6, 7*8($sp)
147 stq $7, 8*8($sp)
148 stq $8, 9*8($sp)
149 stq $16, 10*8($sp)
150 stq $17, 11*8($sp)
151 stq $18, 12*8($sp)
152 stq $19, 13*8($sp)
153 stq $20, 14*8($sp)
154 stq $21, 15*8($sp)
155 stq $22, 16*8($sp)
156 stq $23, 17*8($sp)
157 stq $24, 18*8($sp)
158 stq $25, 19*8($sp)
159 stq $29, 20*8($sp)
160 stt $f0, 21*8($sp)
161 stt $f1, 22*8($sp)
162 stt $f10, 23*8($sp)
163 stt $f11, 24*8($sp)
164 stt $f12, 25*8($sp)
165 stt $f13, 26*8($sp)
166 stt $f14, 27*8($sp)
167 stt $f15, 28*8($sp)
168 stt $f16, 29*8($sp)
169 stt $f17, 30*8($sp)
170 stt $f18, 31*8($sp)
171 stt $f19, 32*8($sp)
172 stt $f20, 33*8($sp)
173 stt $f21, 34*8($sp)
174 stt $f22, 35*8($sp)
175 stt $f23, 36*8($sp)
176 stt $f24, 37*8($sp)
177 stt $f25, 38*8($sp)
178 stt $f26, 39*8($sp)
179 stt $f27, 40*8($sp)
180 stt $f28, 41*8($sp)
181 stt $f29, 42*8($sp)
182 stt $f30, 43*8($sp)
183 .mask 0x27ff01ff, -44*8
184 .fmask 0xfffffc03, -(44-21)*8
185 /* Set up our $gp */
186 br $gp, .+4
187 ldgp $gp, 0($gp)
188 .prologue 0
189 /* Set up the arguments for fixup: */
190 /* $16 = link_map out of plt0 */
191 /* $17 = offset of reloc entry = ($28 - $27 - 20) /12 * 24 */
192 /* $18 = return address */
193 subq $28, $27, $17
194 ldq $16, 8($27)
195 subq $17, 20, $17
196 mov $26, $18
197 addq $17, $17, $17
198 /* Do the fixup */
199 bsr $26, " ASM_ALPHA_NG_SYMBOL_PREFIX #fixup_name "..ng
200 /* Move the destination address into position. */
201 mov $0, $27
202 /* Restore program registers. */
203 ldq $26, 0*8($sp)
204 ldq $0, 1*8($sp)
205 ldq $1, 2*8($sp)
206 ldq $2, 3*8($sp)
207 ldq $3, 4*8($sp)
208 ldq $4, 5*8($sp)
209 ldq $5, 6*8($sp)
210 ldq $6, 7*8($sp)
211 ldq $7, 8*8($sp)
212 ldq $8, 9*8($sp)
213 ldq $16, 10*8($sp)
214 ldq $17, 11*8($sp)
215 ldq $18, 12*8($sp)
216 ldq $19, 13*8($sp)
217 ldq $20, 14*8($sp)
218 ldq $21, 15*8($sp)
219 ldq $22, 16*8($sp)
220 ldq $23, 17*8($sp)
221 ldq $24, 18*8($sp)
222 ldq $25, 19*8($sp)
223 ldq $29, 20*8($sp)
224 ldt $f0, 21*8($sp)
225 ldt $f1, 22*8($sp)
226 ldt $f10, 23*8($sp)
227 ldt $f11, 24*8($sp)
228 ldt $f12, 25*8($sp)
229 ldt $f13, 26*8($sp)
230 ldt $f14, 27*8($sp)
231 ldt $f15, 28*8($sp)
232 ldt $f16, 29*8($sp)
233 ldt $f17, 30*8($sp)
234 ldt $f18, 31*8($sp)
235 ldt $f19, 32*8($sp)
236 ldt $f20, 33*8($sp)
237 ldt $f21, 34*8($sp)
238 ldt $f22, 35*8($sp)
239 ldt $f23, 36*8($sp)
240 ldt $f24, 37*8($sp)
241 ldt $f25, 38*8($sp)
242 ldt $f26, 39*8($sp)
243 ldt $f27, 40*8($sp)
244 ldt $f28, 41*8($sp)
245 ldt $f29, 42*8($sp)
246 ldt $f30, 43*8($sp)
247 /* Flush the Icache after having modified the .plt code. */
248 " #IMB "
249 /* Clean up and turn control to the destination */
250 lda $sp, 44*8($sp)
251 jmp $31, ($27)
252 .end " #tramp_name)
254 #ifndef PROF
255 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
256 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup, imb); \
257 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup, /* nop */);
258 #else
259 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
260 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup, imb); \
261 strong_alias (_dl_runtime_resolve, _dl_runtime_profile);
262 #endif
264 /* Initial entry point code for the dynamic linker.
265 The C function `_dl_start' is the real entry point;
266 its return value is the user program's entry point. */
268 #define RTLD_START asm ("\
269 .text
270 .set at
271 .globl _start
272 .ent _start
273 _start:
274 br $gp, 0f
275 0: ldgp $gp, 0($gp)
276 .prologue 0
277 /* Pass pointer to argument block to _dl_start. */
278 mov $sp, $16
279 bsr $26, "ASM_ALPHA_NG_SYMBOL_PREFIX"_dl_start..ng
280 .end _start
281 /* FALLTHRU */
282 .globl _dl_start_user
283 .ent _dl_start_user
284 _dl_start_user:
285 .frame $30,0,$31,0
286 .prologue 0
287 /* Save the user entry point address in s0. */
288 mov $0, $9
289 /* Store the highest stack address. */
290 stq $30, __libc_stack_end
291 /* See if we were run as a command with the executable file
292 name as an extra leading argument. */
293 ldl $1, _dl_skip_args
294 bne $1, $fixup_stack
295 $fixup_stack_ret:
296 /* The special initializer gets called with the stack just
297 as the application's entry point will see it; it can
298 switch stacks if it moves these contents over. */
299 " RTLD_START_SPECIAL_INIT "
300 /* Call _dl_init(_dl_loaded, argc, argv, envp) to run initializers. */
301 ldq $16, _dl_loaded
302 ldq $17, 0($sp)
303 lda $18, 8($sp)
304 s8addq $17, 8, $19
305 addq $19, $18, $19
306 jsr $26, _dl_init
307 /* Pass our finalizer function to the user in $0. */
308 lda $0, _dl_fini
309 /* Jump to the user's entry point. */
310 mov $9, $27
311 jmp ($9)
312 $fixup_stack:
313 /* Adjust the stack pointer to skip _dl_skip_args words. This
314 involves copying everything down, since the stack pointer must
315 always be 16-byte aligned. */
316 ldq $2, 0($sp)
317 subq $2, $1, $2
318 mov $sp, $4
319 s8addq $1, $sp, $3
320 stq $2, 0($sp)
321 /* Copy down argv. */
322 0: ldq $5, 8($3)
323 addq $4, 8, $4
324 addq $3, 8, $3
325 stq $5, 0($4)
326 bne $5, 0b
327 /* Copy down envp. */
328 1: ldq $5, 8($3)
329 addq $4, 8, $4
330 addq $3, 8, $3
331 stq $5, 0($4)
332 bne $5, 1b
333 /* Copy down auxiliary table. */
334 2: ldq $5, 8($3)
335 ldq $6, 16($3)
336 addq $4, 16, $4
337 addq $3, 16, $3
338 stq $5, -8($4)
339 stq $6, 0($4)
340 bne $5, 2b
341 br $fixup_stack_ret
342 .end _dl_start_user
343 .set noat
344 .previous");
346 #ifndef RTLD_START_SPECIAL_INIT
347 #define RTLD_START_SPECIAL_INIT /* nothing */
348 #endif
350 /* Nonzero iff TYPE describes relocation of a PLT entry, so
351 PLT entries should not be allowed to define the value. */
352 #define elf_machine_lookup_noplt_p(type) ((type) == R_ALPHA_JMP_SLOT)
354 /* Nonzero iff TYPE should not be allowed to resolve to one of
355 the main executable's symbols, as for a COPY reloc, which we don't use. */
356 #define elf_machine_lookup_noexec_p(type) (0)
358 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
359 #define ELF_MACHINE_JMP_SLOT R_ALPHA_JMP_SLOT
361 /* The alpha never uses Elf64_Rel relocations. */
362 #define ELF_MACHINE_NO_REL 1
364 /* Fix up the instructions of a PLT entry to invoke the function
365 rather than the dynamic linker. */
366 static inline Elf64_Addr
367 elf_machine_fixup_plt (struct link_map *l, lookup_t t,
368 const Elf64_Rela *reloc,
369 Elf64_Addr *got_addr, Elf64_Addr value)
371 const Elf64_Rela *rela_plt;
372 Elf64_Word *plte;
373 long int edisp;
375 /* Store the value we are going to load. */
376 *got_addr = value;
378 /* Recover the PLT entry address by calculating reloc's index into the
379 .rela.plt, and finding that entry in the .plt. */
380 rela_plt = (void *) D_PTR (l, l_info[DT_JMPREL]);
381 plte = (void *) (D_PTR (l, l_info[DT_PLTGOT]) + 32);
382 plte += 3 * (reloc - rela_plt);
384 /* Find the displacement from the plt entry to the function. */
385 edisp = (long int) (value - (Elf64_Addr)&plte[3]) / 4;
387 if (edisp >= -0x100000 && edisp < 0x100000)
389 /* If we are in range, use br to perfect branch prediction and
390 elide the dependency on the address load. This case happens,
391 e.g., when a shared library call is resolved to the same library. */
393 int hi, lo;
394 hi = value - (Elf64_Addr)&plte[0];
395 lo = (short int) hi;
396 hi = (hi - lo) >> 16;
398 /* Emit "lda $27,lo($27)" */
399 plte[1] = 0x237b0000 | (lo & 0xffff);
401 /* Emit "br $31,function" */
402 plte[2] = 0xc3e00000 | (edisp & 0x1fffff);
404 /* Think about thread-safety -- the previous instructions must be
405 committed to memory before the first is overwritten. */
406 __asm__ __volatile__("wmb" : : : "memory");
408 /* Emit "ldah $27,hi($27)" */
409 plte[0] = 0x277b0000 | (hi & 0xffff);
411 else
413 /* Don't bother with the hint since we already know the hint is
414 wrong. Eliding it prevents the wrong page from getting pulled
415 into the cache. */
417 int hi, lo;
418 hi = (Elf64_Addr)got_addr - (Elf64_Addr)&plte[0];
419 lo = (short)hi;
420 hi = (hi - lo) >> 16;
422 /* Emit "ldq $27,lo($27)" */
423 plte[1] = 0xa77b0000 | (lo & 0xffff);
425 /* Emit "jmp $31,($27)" */
426 plte[2] = 0x6bfb0000;
428 /* Think about thread-safety -- the previous instructions must be
429 committed to memory before the first is overwritten. */
430 __asm__ __volatile__("wmb" : : : "memory");
432 /* Emit "ldah $27,hi($27)" */
433 plte[0] = 0x277b0000 | (hi & 0xffff);
436 /* At this point, if we've been doing runtime resolution, Icache is dirty.
437 This will be taken care of in _dl_runtime_resolve. If instead we are
438 doing this as part of non-lazy startup relocation, that bit of code
439 hasn't made it into Icache yet, so there's nothing to clean up. */
441 return value;
444 /* Return the final value of a plt relocation. */
445 static inline Elf64_Addr
446 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
447 Elf64_Addr value)
449 return value + reloc->r_addend;
452 #endif /* !dl_machine_h */
454 #ifdef RESOLVE
456 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
457 MAP is the object containing the reloc. */
458 static inline void
459 elf_machine_rela (struct link_map *map,
460 const Elf64_Rela *reloc,
461 const Elf64_Sym *sym,
462 const struct r_found_version *version,
463 Elf64_Addr *const reloc_addr)
465 unsigned long int const r_type = ELF64_R_TYPE (reloc->r_info);
467 #ifndef RTLD_BOOTSTRAP
468 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
469 reference weak so static programs can still link. This declaration
470 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
471 because rtld.c contains the common defn for _dl_rtld_map, which is
472 incompatible with a weak decl in the same file. */
473 weak_extern (_dl_rtld_map);
474 #endif
476 /* We cannot use a switch here because we cannot locate the switch
477 jump table until we've self-relocated. */
479 if (r_type == R_ALPHA_RELATIVE)
481 #ifndef RTLD_BOOTSTRAP
482 /* Already done in dynamic linker. */
483 if (map != &_dl_rtld_map)
484 #endif
485 *reloc_addr += map->l_addr;
487 else if (r_type == R_ALPHA_NONE)
488 return;
489 else
491 Elf64_Addr loadbase, sym_value;
493 loadbase = RESOLVE (&sym, version, r_type);
494 sym_value = sym ? loadbase + sym->st_value : 0;
495 sym_value += reloc->r_addend;
497 if (r_type == R_ALPHA_GLOB_DAT)
498 *reloc_addr = sym_value;
499 else if (r_type == R_ALPHA_JMP_SLOT)
500 elf_machine_fixup_plt (map, 0, reloc, reloc_addr, sym_value);
501 else if (r_type == R_ALPHA_REFQUAD)
503 void *reloc_addr_1 = reloc_addr;
504 Elf64_Addr reloc_addr_val;
506 /* Load value without causing unaligned trap. */
507 memcpy (&reloc_addr_val, reloc_addr_1, 8);
508 sym_value += reloc_addr_val;
509 #ifndef RTLD_BOOTSTRAP
510 if (map == &_dl_rtld_map)
512 /* Undo the relocation done here during bootstrapping.
513 Now we will relocate anew, possibly using a binding
514 found in the user program or a loaded library rather
515 than the dynamic linker's built-in definitions used
516 while loading those libraries. */
517 const Elf64_Sym *const dlsymtab
518 = (void *) D_PTR (map, l_info[DT_SYMTAB]);
519 sym_value -= map->l_addr;
520 sym_value -= dlsymtab[ELF64_R_SYM(reloc->r_info)].st_value;
521 sym_value -= reloc->r_addend;
523 #endif
524 /* Store value without causing unaligned trap. */
525 memcpy (reloc_addr_1, &sym_value, 8);
527 else
528 _dl_reloc_bad_type (map, r_type, 0);
532 static inline void
533 elf_machine_lazy_rel (struct link_map *map,
534 Elf64_Addr l_addr, const Elf64_Rela *reloc)
536 Elf64_Addr * const reloc_addr = (void *)(l_addr + reloc->r_offset);
537 unsigned long int const r_type = ELF64_R_TYPE (reloc->r_info);
539 if (r_type == R_ALPHA_JMP_SLOT)
541 /* Perform a RELATIVE reloc on the .got entry that transfers
542 to the .plt. */
543 *reloc_addr += l_addr;
545 else if (r_type == R_ALPHA_NONE)
546 return;
547 else
548 _dl_reloc_bad_type (map, r_type, 1);
551 #endif /* RESOLVE */