(ELF_MACHINE_NO_RELA): Define unconditionally to defined RTLD_BOOTSTRAP.
[glibc-ports.git] / sysdeps / alpha / dl-machine.h
blob4166e8c498a72702c4d3d07c36a36d4354cdd04e
1 /* Machine-dependent ELF dynamic relocation inline functions. Alpha version.
2 Copyright (C) 1996-2002, 2003, 2004 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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 /* Mask identifying addresses reserved for the user program,
33 where the dynamic linker should not map anything. */
34 #define ELF_MACHINE_USER_ADDRESS_MASK 0x120000000UL
36 /* Return nonzero iff ELF header is compatible with the running host. */
37 static inline int
38 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
40 return ehdr->e_machine == EM_ALPHA;
43 /* Return the link-time address of _DYNAMIC. The multiple-got-capable
44 linker no longer allocates the first .got entry for this. But not to
45 worry, no special tricks are needed. */
46 static inline Elf64_Addr
47 elf_machine_dynamic (void)
49 #ifndef NO_AXP_MULTI_GOT_LD
50 return (Elf64_Addr) &_DYNAMIC;
51 #else
52 register Elf64_Addr *gp __asm__ ("$29");
53 return gp[-4096];
54 #endif
57 /* Return the run-time load address of the shared object. */
58 static inline Elf64_Addr
59 elf_machine_load_address (void)
61 /* NOTE: While it is generally unfriendly to put data in the text
62 segment, it is only slightly less so when the "data" is an
63 instruction. While we don't have to worry about GLD just yet, an
64 optimizing linker might decide that our "data" is an unreachable
65 instruction and throw it away -- with the right switches, DEC's
66 linker will do this. What ought to happen is we should add
67 something to GAS to allow us access to the new GPREL_HI32/LO32
68 relocation types stolen from OSF/1 3.0. */
69 /* This code relies on the fact that BRADDR relocations do not
70 appear in dynamic relocation tables. Not that that would be very
71 useful anyway -- br/bsr has a 4MB range and the shared libraries
72 are usually many many terabytes away. */
74 Elf64_Addr dot;
75 long int zero_disp;
77 asm("br %0, 1f\n"
78 "0:\n\t"
79 "br $0, 2f\n"
80 "1:\n\t"
81 ".section\t.data\n"
82 "2:\n\t"
83 ".quad 0b\n\t"
84 ".previous"
85 : "=r"(dot));
87 zero_disp = *(int *) dot;
88 zero_disp = (zero_disp << 43) >> 41;
90 return dot - *(Elf64_Addr *) (dot + 4 + zero_disp);
93 /* Set up the loaded object described by L so its unrelocated PLT
94 entries will jump to the on-demand fixup code in dl-runtime.c. */
96 static inline int
97 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
99 Elf64_Addr plt;
100 extern void _dl_runtime_resolve (void);
101 extern void _dl_runtime_profile (void);
103 if (l->l_info[DT_JMPREL] && lazy)
105 /* The GOT entries for the functions in the PLT have not been
106 filled in yet. Their initial contents are directed to the
107 PLT which arranges for the dynamic linker to be called. */
108 plt = D_PTR (l, l_info[DT_PLTGOT]);
110 /* This function will be called to perform the relocation. */
111 if (__builtin_expect (profile, 0))
113 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_profile;
115 if (GLRO(dl_profile) != NULL
116 && _dl_name_match_p (GLRO(dl_profile), l))
118 /* This is the object we are looking for. Say that we really
119 want profiling and the timers are started. */
120 GL(dl_profile_map) = l;
123 else
124 *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_resolve;
126 /* Identify this shared object */
127 *(Elf64_Addr *)(plt + 24) = (Elf64_Addr) l;
129 /* If the first instruction of the plt entry is not
130 "br $28, plt0", we have to reinitialize .plt for lazy relocation. */
131 if (*(unsigned int *)(plt + 32) != 0xc39ffff7)
133 unsigned int val = 0xc39ffff7;
134 unsigned int *slot, *end;
135 const Elf64_Rela *rela = (const Elf64_Rela *)
136 D_PTR (l, l_info[DT_JMPREL]);
137 Elf64_Addr l_addr = l->l_addr;
139 /* br t12,.+4; ldq t12,12(t12); nop; jmp t12,(t12),.+4 */
140 *(unsigned long *)plt = 0xa77b000cc3600000;
141 *(unsigned long *)(plt + 8) = 0x6b7b000047ff041f;
142 slot = (unsigned int *)(plt + 32);
143 end = (unsigned int *)(plt + 32
144 + l->l_info[DT_PLTRELSZ]->d_un.d_val / 2);
145 while (slot < end)
147 /* br at,.plt+0 */
148 *slot = val;
149 *(Elf64_Addr *) rela->r_offset = (Elf64_Addr) slot - l_addr;
150 val -= 3;
151 slot += 3;
152 ++rela;
157 return lazy;
160 /* Initial entry point code for the dynamic linker.
161 The C function `_dl_start' is the real entry point;
162 its return value is the user program's entry point. */
164 #define RTLD_START asm ("\
165 .section .text \n\
166 .set at \n\
167 .globl _start \n\
168 .ent _start \n\
169 _start: \n\
170 .frame $31,0,$31,0 \n\
171 br $gp, 0f \n\
172 0: ldgp $gp, 0($gp) \n\
173 .prologue 0 \n\
174 /* Pass pointer to argument block to _dl_start. */ \n\
175 mov $sp, $16 \n\
176 bsr $26, _dl_start !samegp \n\
177 .end _start \n\
178 /* FALLTHRU */ \n\
179 .globl _dl_start_user \n\
180 .ent _dl_start_user \n\
181 _dl_start_user: \n\
182 .frame $31,0,$31,0 \n\
183 .prologue 0 \n\
184 /* Save the user entry point address in s0. */ \n\
185 mov $0, $9 \n\
186 /* See if we were run as a command with the executable \n\
187 file name as an extra leading argument. */ \n\
188 ldah $1, _dl_skip_args($gp) !gprelhigh \n\
189 ldl $1, _dl_skip_args($1) !gprellow \n\
190 bne $1, $fixup_stack \n\
191 $fixup_stack_ret: \n\
192 /* The special initializer gets called with the stack \n\
193 just as the application's entry point will see it; \n\
194 it can switch stacks if it moves these contents \n\
195 over. */ \n\
196 " RTLD_START_SPECIAL_INIT " \n\
197 /* Call _dl_init(_dl_loaded, argc, argv, envp) to run \n\
198 initializers. */ \n\
199 ldah $16, _rtld_local($gp) !gprelhigh \n\
200 ldq $16, _rtld_local($16) !gprellow \n\
201 ldq $17, 0($sp) \n\
202 lda $18, 8($sp) \n\
203 s8addq $17, 8, $19 \n\
204 addq $19, $18, $19 \n\
205 bsr $26, _dl_init_internal !samegp \n\
206 /* Pass our finalizer function to the user in $0. */ \n\
207 ldah $0, _dl_fini($gp) !gprelhigh \n\
208 lda $0, _dl_fini($0) !gprellow \n\
209 /* Jump to the user's entry point. */ \n\
210 mov $9, $27 \n\
211 jmp ($9) \n\
212 $fixup_stack: \n\
213 /* Adjust the stack pointer to skip _dl_skip_args words.\n\
214 This involves copying everything down, since the \n\
215 stack pointer must always be 16-byte aligned. */ \n\
216 ldah $7, _dl_argv_internal($gp) !gprelhigh \n\
217 ldq $2, 0($sp) \n\
218 ldq $5, _dl_argv_internal($7) !gprellow \n\
219 subq $31, $1, $6 \n\
220 subq $2, $1, $2 \n\
221 s8addq $6, $5, $5 \n\
222 mov $sp, $4 \n\
223 s8addq $1, $sp, $3 \n\
224 stq $2, 0($sp) \n\
225 stq $5, _dl_argv_internal($7) !gprellow \n\
226 /* Copy down argv. */ \n\
227 0: ldq $5, 8($3) \n\
228 addq $4, 8, $4 \n\
229 addq $3, 8, $3 \n\
230 stq $5, 0($4) \n\
231 bne $5, 0b \n\
232 /* Copy down envp. */ \n\
233 1: ldq $5, 8($3) \n\
234 addq $4, 8, $4 \n\
235 addq $3, 8, $3 \n\
236 stq $5, 0($4) \n\
237 bne $5, 1b \n\
238 /* Copy down auxiliary table. */ \n\
239 2: ldq $5, 8($3) \n\
240 ldq $6, 16($3) \n\
241 addq $4, 16, $4 \n\
242 addq $3, 16, $3 \n\
243 stq $5, -8($4) \n\
244 stq $6, 0($4) \n\
245 bne $5, 2b \n\
246 br $fixup_stack_ret \n\
247 .end _dl_start_user \n\
248 .set noat \n\
249 .previous");
251 #ifndef RTLD_START_SPECIAL_INIT
252 #define RTLD_START_SPECIAL_INIT /* nothing */
253 #endif
255 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry
256 or TLS variables, so undefined references should not be allowed
257 to define the value.
259 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve
260 to one of the main executable's symbols, as for a COPY reloc.
261 This is unused on Alpha. */
263 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
264 #define elf_machine_type_class(type) \
265 (((type) == R_ALPHA_JMP_SLOT \
266 || (type) == R_ALPHA_DTPMOD64 \
267 || (type) == R_ALPHA_DTPREL64 \
268 || (type) == R_ALPHA_TPREL64) * ELF_RTYPE_CLASS_PLT)
269 #else
270 #define elf_machine_type_class(type) \
271 (((type) == R_ALPHA_JMP_SLOT) * ELF_RTYPE_CLASS_PLT)
272 #endif
274 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
275 #define ELF_MACHINE_JMP_SLOT R_ALPHA_JMP_SLOT
277 /* The alpha never uses Elf64_Rel relocations. */
278 #define ELF_MACHINE_NO_REL 1
280 /* Fix up the instructions of a PLT entry to invoke the function
281 rather than the dynamic linker. */
282 static inline Elf64_Addr
283 elf_machine_fixup_plt (struct link_map *l, lookup_t t,
284 const Elf64_Rela *reloc,
285 Elf64_Addr *got_addr, Elf64_Addr value)
287 const Elf64_Rela *rela_plt;
288 Elf64_Word *plte;
289 long int edisp;
291 /* Store the value we are going to load. */
292 *got_addr = value;
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 *) D_PTR (l, l_info[DT_JMPREL]);
297 plte = (void *) (D_PTR (l, l_info[DT_PLTGOT]) + 32);
298 plte += 3 * (reloc - rela_plt);
300 /* Find the displacement from the plt entry to the function. */
301 edisp = (long int) (value - (Elf64_Addr)&plte[3]) / 4;
303 if (edisp >= -0x100000 && edisp < 0x100000)
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. */
309 int hi, lo;
310 hi = value - (Elf64_Addr)&plte[0];
311 lo = (short int) hi;
312 hi = (hi - lo) >> 16;
314 /* Emit "lda $27,lo($27)" */
315 plte[1] = 0x237b0000 | (lo & 0xffff);
317 /* Emit "br $31,function" */
318 plte[2] = 0xc3e00000 | (edisp & 0x1fffff);
320 /* Think about thread-safety -- the previous instructions must be
321 committed to memory before the first is overwritten. */
322 __asm__ __volatile__("wmb" : : : "memory");
324 /* Emit "ldah $27,hi($27)" */
325 plte[0] = 0x277b0000 | (hi & 0xffff);
327 else
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. */
333 int hi, lo;
334 hi = (Elf64_Addr)got_addr - (Elf64_Addr)&plte[0];
335 lo = (short)hi;
336 hi = (hi - lo) >> 16;
338 /* Emit "ldq $27,lo($27)" */
339 plte[1] = 0xa77b0000 | (lo & 0xffff);
341 /* Emit "jmp $31,($27)" */
342 plte[2] = 0x6bfb0000;
344 /* Think about thread-safety -- the previous instructions must be
345 committed to memory before the first is overwritten. */
346 __asm__ __volatile__("wmb" : : : "memory");
348 /* Emit "ldah $27,hi($27)" */
349 plte[0] = 0x277b0000 | (hi & 0xffff);
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. */
357 return value;
360 /* Return the final value of a plt relocation. */
361 static inline Elf64_Addr
362 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
363 Elf64_Addr value)
365 return value + reloc->r_addend;
368 /* Names of the architecture-specific auditing callback functions. */
369 #define ARCH_LA_PLTENTER alpha_gnu_pltenter
370 #define ARCH_LA_PLTEXIT alpha_gnu_pltexit
372 #endif /* !dl_machine_h */
374 #ifdef RESOLVE_MAP
376 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
377 MAP is the object containing the reloc. */
378 auto inline void
379 elf_machine_rela (struct link_map *map,
380 const Elf64_Rela *reloc,
381 const Elf64_Sym *sym,
382 const struct r_found_version *version,
383 void *const reloc_addr_arg)
385 Elf64_Addr *const reloc_addr = reloc_addr_arg;
386 unsigned long int const r_type = ELF64_R_TYPE (reloc->r_info);
388 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC && !defined SHARED
389 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
390 reference weak so static programs can still link. This declaration
391 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
392 because rtld.c contains the common defn for _dl_rtld_map, which is
393 incompatible with a weak decl in the same file. */
394 weak_extern (_dl_rtld_map);
395 #endif
397 /* We cannot use a switch here because we cannot locate the switch
398 jump table until we've self-relocated. */
400 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
401 if (__builtin_expect (r_type == R_ALPHA_RELATIVE, 0))
403 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
404 /* Already done in dynamic linker. */
405 if (map != &GL(dl_rtld_map))
406 # endif
408 /* XXX Make some timings. Maybe it's preferable to test for
409 unaligned access and only do it the complex way if necessary. */
410 Elf64_Addr reloc_addr_val;
412 /* Load value without causing unaligned trap. */
413 memcpy (&reloc_addr_val, reloc_addr_arg, 8);
414 reloc_addr_val += map->l_addr;
416 /* Store value without causing unaligned trap. */
417 memcpy (reloc_addr_arg, &reloc_addr_val, 8);
420 else
421 #endif
422 if (__builtin_expect (r_type == R_ALPHA_NONE, 0))
423 return;
424 else
426 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
427 Elf64_Addr sym_value;
428 Elf64_Addr sym_raw_value;
430 sym_raw_value = sym_value = reloc->r_addend;
431 if (sym_map)
433 sym_raw_value += sym->st_value;
434 sym_value = sym_raw_value + sym_map->l_addr;
437 if (r_type == R_ALPHA_GLOB_DAT)
438 *reloc_addr = sym_value;
439 #ifdef RESOLVE_CONFLICT_FIND_MAP
440 /* In .gnu.conflict section, R_ALPHA_JMP_SLOT relocations have
441 R_ALPHA_JMP_SLOT in lower 8 bits and the remaining 24 bits
442 are .rela.plt index. */
443 else if ((r_type & 0xff) == R_ALPHA_JMP_SLOT)
445 /* elf_machine_fixup_plt needs the map reloc_addr points into,
446 while in _dl_resolve_conflicts map is _dl_loaded. */
447 RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr);
448 reloc = ((const Elf64_Rela *) D_PTR (map, l_info[DT_JMPREL]))
449 + (r_type >> 8);
450 elf_machine_fixup_plt (map, 0, reloc, reloc_addr, sym_value);
452 #else
453 else if (r_type == R_ALPHA_JMP_SLOT)
454 elf_machine_fixup_plt (map, 0, reloc, reloc_addr, sym_value);
455 #endif
456 #ifndef RTLD_BOOTSTRAP
457 else if (r_type == R_ALPHA_REFQUAD)
459 /* Store value without causing unaligned trap. */
460 memcpy (reloc_addr_arg, &sym_value, 8);
462 #endif
463 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
464 else if (r_type == R_ALPHA_DTPMOD64)
466 #ifdef RTLD_BOOTSTRAP
467 /* During startup the dynamic linker is always index 1. */
468 *reloc_addr = 1;
469 #else
470 /* Get the information from the link map returned by the
471 resolv function. */
472 if (sym_map != NULL)
473 *reloc_addr = sym_map->l_tls_modid;
474 #endif
476 else if (r_type == R_ALPHA_DTPREL64)
478 #ifndef RTLD_BOOTSTRAP
479 /* During relocation all TLS symbols are defined and used.
480 Therefore the offset is already correct. */
481 *reloc_addr = sym_raw_value;
482 #endif
484 else if (r_type == R_ALPHA_TPREL64)
486 #ifdef RTLD_BOOTSTRAP
487 *reloc_addr = sym_raw_value + map->l_tls_offset;
488 #else
489 if (sym_map)
491 CHECK_STATIC_TLS (map, sym_map);
492 *reloc_addr = sym_raw_value + sym_map->l_tls_offset;
494 #endif
496 #endif /* USE_TLS */
497 else
498 _dl_reloc_bad_type (map, r_type, 0);
502 /* Let do-rel.h know that on Alpha if l_addr is 0, all RELATIVE relocs
503 can be skipped. */
504 #define ELF_MACHINE_REL_RELATIVE 1
506 auto inline void
507 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
508 void *const reloc_addr_arg)
510 /* XXX Make some timings. Maybe it's preferable to test for
511 unaligned access and only do it the complex way if necessary. */
512 Elf64_Addr reloc_addr_val;
514 /* Load value without causing unaligned trap. */
515 memcpy (&reloc_addr_val, reloc_addr_arg, 8);
516 reloc_addr_val += l_addr;
518 /* Store value without causing unaligned trap. */
519 memcpy (reloc_addr_arg, &reloc_addr_val, 8);
522 auto inline void
523 elf_machine_lazy_rel (struct link_map *map,
524 Elf64_Addr l_addr, const Elf64_Rela *reloc)
526 Elf64_Addr * const reloc_addr = (void *)(l_addr + reloc->r_offset);
527 unsigned long int const r_type = ELF64_R_TYPE (reloc->r_info);
529 if (r_type == R_ALPHA_JMP_SLOT)
531 /* Perform a RELATIVE reloc on the .got entry that transfers
532 to the .plt. */
533 *reloc_addr += l_addr;
535 else if (r_type == R_ALPHA_NONE)
536 return;
537 else
538 _dl_reloc_bad_type (map, r_type, 1);
541 #endif /* RESOLVE_MAP */