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