Tue Jul 16 00:31:31 1996 Richard Henderson <rth@tamu.edu>
[platform/upstream/glibc.git] / sysdeps / alpha / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  Alpha version.
2 Copyright (C) 1996 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
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, 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 run-time address of the _GLOBAL_OFFSET_TABLE_.
41    Must be inlined in a function which uses global data.  */
42 static inline Elf64_Addr *
43 elf_machine_got (void)
44 {
45   register Elf64_Addr gp __asm__("$29");
46   return (Elf64_Addr *)(gp - 0x8000);
47 }
48
49 /* Return the run-time load address of the shared object.  */
50 static inline Elf64_Addr
51 elf_machine_load_address (void)
52 {
53   /* NOTE: While it is generally unfriendly to put data in the text
54      segment, it is only slightly less so when the "data" is an
55      instruction.  While we don't have to worry about GLD just yet, an
56      optimizing linker might decide that our "data" is an unreachable
57      instruction and throw it away -- with the right switches, DEC's
58      linker will do this.  What ought to happen is we should add
59      something to GAS to allow us access to the new GPREL_HI32/LO32
60      relocation types stolen from OSF/1 3.0.  */
61   /* This code relies on the fact that BRADDR relocations do not
62      appear in dynamic relocation tables.  Not that that would be very
63      useful anyway -- br/bsr has a 4MB range and the shared libraries
64      are usually many many terabytes away.  */
65
66   Elf64_Addr dot;
67   long zero_disp;
68
69   asm("br %0, 1f\n\t"
70       ".weak __load_address_undefined\n\t"
71       "br $0, __load_address_undefined\n"
72       "1:"
73       : "=r"(dot));
74
75   zero_disp = *(int *)dot;
76   zero_disp = (zero_disp << 43) >> 41;
77
78   return dot + 4 + zero_disp;
79 }
80
81 /* Set up the loaded object described by L so its unrelocated PLT
82    entries will jump to the on-demand fixup code in dl-runtime.c.  */
83
84 static inline void
85 elf_machine_runtime_setup (struct link_map *l, int lazy)
86 {
87   Elf64_Addr plt;
88   extern void _dl_runtime_resolve (void);
89
90   if (l->l_info[DT_JMPREL] && lazy)
91     {
92       /* The GOT entries for the functions in the PLT have not been
93          filled in yet.  Their initial contents are directed to the
94          PLT which arranges for the dynamic linker to be called.  */
95       plt = l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr;
96
97       /* This function will be called to perform the relocation.  */
98       *(Elf64_Addr *)(plt + 16) = (Elf64_Addr) &_dl_runtime_resolve;
99
100       /* Identify this shared object */
101       *(Elf64_Addr *)(plt + 24) = (Elf64_Addr) l;
102     }
103 }
104
105 /* This code is used in dl-runtime.c to call the `fixup' function
106    and then redirect to the address it returns.  */
107 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ( \
108 "/* Trampoline for _dl_runtime_resolver */
109         .globl _dl_runtime_resolve
110         .ent _dl_runtime_resolve
111 _dl_runtime_resolve:
112         lda     $sp, -168($sp)
113         .frame  $sp, 168, $26
114         /* Preserve all registers that C normally doesn't.  */
115         stq     $26, 0($sp)
116         stq     $0, 8($sp)
117         stq     $1, 16($sp)
118         stq     $2, 24($sp)
119         stq     $3, 32($sp)
120         stq     $4, 40($sp)
121         stq     $5, 48($sp)
122         stq     $6, 56($sp)
123         stq     $7, 64($sp)
124         stq     $8, 72($sp)
125         stq     $16, 80($sp)
126         stq     $17, 88($sp)
127         stq     $18, 96($sp)
128         stq     $19, 104($sp)
129         stq     $20, 112($sp)
130         stq     $21, 120($sp)
131         stq     $22, 128($sp)
132         stq     $23, 136($sp)
133         stq     $24, 144($sp)
134         stq     $25, 152($sp)
135         stq     $29, 160($sp)
136         .mask   0x27ff01ff, -168
137         /* Set up our $gp */
138         br      $gp, .+4
139         ldgp    $gp, 0($gp)
140         .prologue 1
141         /* Set up the arguments for _dl_runtime_resolve. */
142         /* $16 = link_map out of plt0 */
143         ldq     $16, 8($27)
144         /* $17 = offset of reloc entry */
145         mov     $28, $17
146         /* Do the fixup */
147         bsr     $26, fixup..ng
148         /* Move the destination address to a safe place.  */
149         mov     $0, $27
150         /* Restore program registers.  */
151         ldq     $26, 0($sp)
152         ldq     $0, 8($sp)
153         ldq     $1, 16($sp)
154         ldq     $2, 24($sp)
155         ldq     $3, 32($sp)
156         ldq     $4, 40($sp)
157         ldq     $5, 48($sp)
158         ldq     $6, 56($sp)
159         ldq     $7, 64($sp)
160         ldq     $8, 72($sp)
161         ldq     $16, 80($sp)
162         ldq     $17, 88($sp)
163         ldq     $18, 96($sp)
164         ldq     $19, 104($sp)
165         ldq     $20, 112($sp)
166         ldq     $21, 120($sp)
167         ldq     $22, 128($sp)
168         ldq     $23, 136($sp)
169         ldq     $24, 144($sp)
170         ldq     $25, 152($sp)
171         ldq     $29, 160($sp)
172         /* Clean up and turn control to the destination */
173         lda     $sp, 168($sp)
174         jmp     $31, ($27)
175         .end _dl_runtime_resolve");
176
177 /* The PLT uses Elf_Rel relocs.  */
178 #define elf_machine_relplt elf_machine_rela
179
180 /* Mask identifying addresses reserved for the user program,
181    where the dynamic linker should not map anything.  */
182 /* FIXME */
183 #define ELF_MACHINE_USER_ADDRESS_MASK   (~0x1FFFFFFFFUL)
184
185 /* Initial entry point code for the dynamic linker.
186    The C function `_dl_start' is the real entry point;
187    its return value is the user program's entry point.  */
188
189 #define RTLD_START asm ("\
190 .text
191         .globl _start
192         .globl _dl_start_user
193 _start:
194         br      $gp,.+4
195         ldgp    $gp, 0($gp)
196         /* Pass pointer to argument block to _dl_start.  */
197         mov     $sp, $16
198         bsr     $26, _dl_start..ng
199 _dl_start_user:
200         /* Save the user entry point address in s0.  */
201         mov     $0, $9
202         /* See if we were run as a command with the executable file
203            name as an extra leading argument.  If so, adjust the stack
204            pointer to skip _dl_skip_args words.  */
205         ldl     $1, _dl_skip_args
206         beq     $1, 0f
207         ldq     $2, 0($sp)
208         subq    $2, $1, $2
209         s8addq  $1, $sp, $sp
210         stq     $2, 0($sp)
211         /* Load _dl_default_scope[2] into s1 to pass to _dl_init_next.  */
212 0:      ldq     $10, _dl_default_scope+16
213         /* Call _dl_init_next to return the address of an initalizer
214            function to run.  */
215 1:      mov     $10, $16
216         jsr     $26, _dl_init_next
217         ldgp    $gp, 0($26)
218         beq     $0, 2f
219         mov     $0, $27
220         jsr     $26, ($0)
221         ldgp    $gp, 0($26)
222         br      1b
223 2:      /* Pass our finalizer function to the user in $0. */
224         lda     $0, _dl_fini
225         /* Jump to the user's entry point.  */
226         mov     $9, $27
227         jmp     ($9)");
228
229 /* Nonzero iff TYPE describes relocation of a PLT entry, so 
230    PLT entries should not be allowed to define the value.  */
231 #define elf_machine_pltrel_p(type)  ((type) == R_ALPHA_JMP_SLOT)
232
233 /* The alpha never uses Elf64_Rel relocations.  */
234 #define ELF_MACHINE_NO_REL 1
235
236 #endif /* !dl_machine_h */
237
238 #ifdef RESOLVE
239
240 /* Fix up the instructions of a PLT entry to invoke the function
241    rather than the dynamic linker.  */
242 static inline void
243 elf_alpha_fix_plt(struct link_map *l,
244                   const Elf64_Rela *reloc,
245                   Elf64_Addr got_addr,
246                   Elf64_Addr value)
247 {
248   const Elf64_Rela *rela_plt;
249   Elf64_Word *plte;
250   long edisp;
251
252   /* Recover the PLT entry address by calculating reloc's index into the
253      .rela.plt, and finding that entry in the .plt.  */
254
255   rela_plt = (void *)(l->l_addr + l->l_info[DT_JMPREL]->d_un.d_ptr);
256
257   plte = (void *)(l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr + 32);
258   plte += 3 * (reloc - rela_plt);
259
260   /* Find the displacement from the plt entry to the function.  */
261
262   edisp = (long)(value - (Elf64_Addr)&plte[3]) / 4;
263
264   if (edisp >= -0x100000 && edisp < 0x100000)
265     {
266       /* If we are in range, use br to perfect branch prediction and
267          elide the dependency on the address load.  This case happens,
268          e.g., when a shared library call is resolved to the same library.  */
269
270       int hi, lo;
271       hi = value - (Elf64_Addr)&plte[0];
272       lo = (short)hi;
273       hi = (hi - lo) >> 16;
274
275       /* Emit "ldah $27,H($27)" */
276       plte[0] = 0x277b0000 | (hi & 0xffff);
277
278       /* Emit "lda $27,L($27)" */
279       plte[1] = 0x237b0000 | (lo & 0xffff);
280
281       /* Emit "br $31,function" */
282       plte[2] = 0xc3e00000 | (edisp & 0x1fffff);
283     }
284   else
285     {
286       /* Don't bother with the hint since we already know the hint is
287          wrong.  Eliding it prevents the wrong page from getting pulled
288          into the cache.  */
289
290       int hi, lo;
291       hi = got_addr - (Elf64_Addr)&plte[0];
292       lo = (short)hi;
293       hi = (hi - lo) >> 16;
294
295       /* Emit "ldah $27,H($27)" */
296       plte[0] = 0x277b0000 | (hi & 0xffff);
297
298       /* Emit "ldq $27,L($27)" */
299       plte[1] = 0xa77b0000 | (lo & 0xffff);
300
301       /* Emit "jmp $31,($27)" */
302       plte[2] = 0x6bfb0000;
303     }
304
305   /* Flush the instruction cache now that we've diddled.   Tag it as
306      modifying memory to checkpoint memory writes during optimization.  */
307   asm volatile("call_pal 0x86" : : : "memory");
308 }
309
310 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
311    MAP is the object containing the reloc.  */
312 static inline void
313 elf_machine_rela (struct link_map *map,
314                   const Elf64_Rela *reloc,
315                   const Elf64_Sym *sym)
316 {
317   Elf64_Addr * const reloc_addr = (void *)(map->l_addr + reloc->r_offset);
318   unsigned long const r_info = ELF64_R_TYPE (reloc->r_info);
319
320 #ifndef RTLD_BOOTSTRAP
321   /* This is defined in rtld.c, but nowhere in the static libc.a; make the
322      reference weak so static programs can still link.  This declaration
323      cannot be done when compiling rtld.c (i.e.  #ifdef RTLD_BOOTSTRAP)
324      because rtld.c contains the common defn for _dl_rtld_map, which is
325      incompatible with a weak decl in the same file.  */
326   weak_extern (_dl_rtld_map);
327 #endif
328
329   /* We cannot use a switch here because we cannot locate the switch
330      jump table until we've self-relocated.  */
331
332   if (r_info == R_ALPHA_RELATIVE)
333     {
334 #ifndef RTLD_BOOTSTRAP
335       /* Already done in dynamic linker.  */
336       if (map != &_dl_rtld_map)
337 #endif
338         *reloc_addr += map->l_addr;
339     }
340   else if (r_info == R_ALPHA_NONE)
341     return;
342   else
343     {
344       Elf64_Addr loadbase, sym_value;
345
346       loadbase = RESOLVE (&sym, (Elf64_Addr)reloc_addr,
347                           r_info == R_ALPHA_JMP_SLOT);
348       sym_value = sym ? loadbase + sym->st_value : 0;
349
350       if (r_info == R_ALPHA_GLOB_DAT)
351         *reloc_addr = sym_value;
352       else if (r_info == R_ALPHA_JMP_SLOT)
353         {
354           *reloc_addr = sym_value;
355           elf_alpha_fix_plt (map, reloc, (Elf64_Addr) reloc_addr, sym_value);
356         }
357       else if (r_info == R_ALPHA_REFQUAD)
358         {
359           sym_value += *reloc_addr;
360 #ifndef RTLD_BOOTSTRAP
361           if (map == &_dl_rtld_map)
362             {
363               /* Undo the relocation done here during bootstrapping.
364                  Now we will relocate anew, possibly using a binding
365                  found in the user program or a loaded library rather
366                  than the dynamic linker's built-in definitions used
367                  while loading those libraries.  */
368               const Elf64_Sym *const dlsymtab
369                 = (void *)(map->l_addr + map->l_info[DT_SYMTAB]->d_un.d_ptr);
370               sym_value -= map->l_addr;
371               sym_value -= dlsymtab[ELF64_R_SYM(reloc->r_info)].st_value;
372             }
373           else
374 #endif
375             sym_value += reloc->r_addend;
376           *reloc_addr = sym_value;
377         }
378       else if (r_info == R_ALPHA_COPY)
379         memcpy (reloc_addr, (void *) sym_value, sym->st_size);
380       else
381         assert (! "unexpected dynamic reloc type");
382     }
383 }
384
385 static inline void
386 elf_machine_lazy_rel (struct link_map *map, const Elf64_Rela *reloc)
387 {
388   Elf64_Addr * const reloc_addr = (void *)(map->l_addr + reloc->r_offset);
389   unsigned long const r_info = ELF64_R_TYPE (reloc->r_info);
390
391   if (r_info == R_ALPHA_JMP_SLOT)
392     {
393       /* Perform a RELATIVE reloc on the .got entry that transfers
394          to the .plt.  */
395       *reloc_addr += map->l_addr;
396     }
397   else if (r_info == R_ALPHA_NONE)
398     return;
399   else
400     assert (! "unexpected PLT reloc type");
401 }
402
403 #endif /* RESOLVE */