update from main archive 961203
[platform/upstream/glibc.git] / sysdeps / sparc / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  SPARC version.
2    Copyright (C) 1996 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If
17    not, write to the Free Software Foundation, Inc.,
18    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #define ELF_MACHINE_NAME "sparc"
21
22 #include <assert.h>
23 #include <string.h>
24 #include <link.h>
25
26
27 /* Some SPARC opcodes we need to use for self-modifying code.  */
28 #define OPCODE_NOP      0x01000000 /* nop */
29 #define OPCODE_CALL     0x04000000 /* call ?; add PC-rel word address */
30 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
31 #define OPCODE_JMP_G1   0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
32 #define OPCODE_SAVE_SP64 0x9de3bfc0 /* save %sp, -64, %sp */
33
34
35 /* Return nonzero iff E_MACHINE is compatible with the running host.  */
36 static inline int
37 elf_machine_matches_host (Elf32_Half e_machine)
38 {
39   switch (e_machine)
40     {
41     case EM_SPARC:
42       return 1;
43     default:
44       return 0;
45     }
46 }
47
48
49 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
50    first element of the GOT.  This must be inlined in a function which
51    uses global data.  */
52 static inline Elf32_Addr
53 elf_machine_dynamic (void)
54 {
55   register Elf32_Addr *got asm ("%l7");
56   return *got;
57 }
58
59
60 /* Return the run-time load address of the shared object.  */
61 static inline Elf32_Addr
62 elf_machine_load_address (void)
63 {
64   Elf32_Addr addr;
65 ???
66   return addr;
67 }
68
69 /* The `subl' insn above will contain an R_68K_RELATIVE relocation
70    entry intended to insert the run-time address of the label `here'.
71    This will be the first relocation in the text of the dynamic
72    linker; we skip it to avoid trying to modify read-only text in this
73    early stage.  */
74 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
75   ((dynamic_info)[DT_RELA]->d_un.d_ptr += sizeof (Elf32_Rela), \
76    (dynamic_info)[DT_RELASZ]->d_un.d_val -= sizeof (Elf32_Rela))
77
78 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
79    MAP is the object containing the reloc.  */
80
81 static inline void
82 elf_machine_rela (struct link_map *map,
83                   const Elf32_Rela *reloc, const Elf32_Sym *sym,
84                   Elf32_Addr (*resolve) (const Elf32_Sym **ref,
85                                          Elf32_Addr reloc_addr,
86                                          int noplt))
87 {
88   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
89   Elf32_Addr loadbase;
90
91   switch (ELF32_R_TYPE (reloc->r_info))
92     {
93     case R_SPARC_COPY:
94       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
95       memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
96       break;
97     case R_SPARC_GLOB_DAT:
98     case R_SPARC_32:
99       loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
100                   /* RESOLVE is null during bootstrap relocation.  */
101                   map->l_addr);
102       *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
103                      + reloc->r_addend);
104       break;
105     case R_SPARC_JMP_SLOT:
106       loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 1) :
107                   /* RESOLVE is null during bootstrap relocation.  */
108                   map->l_addr);
109       {
110         Elf32_Addr value = ((sym ? (loadbase + sym->st_value) : 0)
111                             + reloc->r_addend);
112         reloc_addr[1] = OPCODE_SETHI | (value >> 10);
113         reloc_addr[2] = OPCODE_JMP_G1 | (value & 0x3ff);
114       }
115       break;
116     case R_SPARC_8:
117       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
118       *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
119                               + reloc->r_addend);
120       break;
121     case R_SPARC_16:
122       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
123       *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
124                                + reloc->r_addend);
125       break;
126     case R_SPARC_32:
127       loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
128                   /* RESOLVE is null during bootstrap relocation.  */
129                   map->l_addr);
130       break;
131     case R_SPARC_RELATIVE:
132       *reloc_addr = map->l_addr + reloc->r_addend;
133       break;
134     case R_SPARC_DISP8:
135       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
136       *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
137                               + reloc->r_addend
138                               - (Elf32_Addr) reloc_addr);
139       break;
140     case R_SPARC_DISP16:
141       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
142       *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
143                                + reloc->r_addend
144                                - (Elf32_Addr) reloc_addr);
145       break;
146     case R_SPARC_DISP32:
147       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
148       *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
149                      + reloc->r_addend
150                      - (Elf32_Addr) reloc_addr);
151       break;
152     case R_SPARC_NONE:          /* Alright, Wilbur.  */
153       break;
154     default:
155       assert (! "unexpected dynamic reloc type");
156       break;
157     }
158 }
159
160 static inline void
161 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
162 {
163   switch (ELF32_R_TYPE (reloc->r_info))
164     {
165     case R_SPARC_NONE:
166       break;
167     case R_SPARC_JMP_SLOT:
168       break;
169     default:
170       assert (! "unexpected PLT reloc type");
171       break;
172     }
173 }
174
175 /* Nonzero iff TYPE describes relocation of a PLT entry, so
176    PLT entries should not be allowed to define the value.  */
177 #define elf_machine_pltrel_p(type) ((type) == R_SPARC_JMP_SLOT)
178
179 /* The SPARC never uses Elf32_Rel relocations.  */
180 #define ELF_MACHINE_NO_REL 1
181
182
183 /* Set up the loaded object described by L so its unrelocated PLT
184    entries will jump to the on-demand fixup code in dl-runtime.c.  */
185
186 static inline void
187 elf_machine_runtime_setup (struct link_map *l, int lazy)
188 {
189   Elf32_Addr *plt;
190   extern void _dl_runtime_resolve (Elf32_Word);
191
192   if (l->l_info[DT_JMPREL] && lazy)
193     {
194       /* The entries for functions in the PLT have not yet been filled in.
195          Their initial contents will arrange when called to set the high 22
196          bits of %g1 with an offset into the .rela.plt section and jump to
197          the beginning of the PLT.  */
198       plt = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
199
200       /* The beginning of the PLT does:
201
202                 save %sp, -64, %sp
203          pltpc: call _dl_runtime_resolve
204                 nop
205                 .word MAP
206
207          This saves the register window containing the arguments, and the
208          PC value (pltpc) implicitly saved in %o7 by the call points near the
209          location where we store the link_map pointer for this object.  */
210
211       plt[0] = OPCODE_SAVE_SP64; /* save %sp, -64, %sp */
212       /* Construct PC-relative word address.  */
213       plt[1] = OPCODE_CALL | (((Elf32_Addr) &_dl_runtime_resolve -
214                                (Elf32_Addr) &plt[1]) >> 2);
215       plt[2] = OPCODE_NOP;      /* Fill call delay slot.  */
216       plt[3] = l;
217     }
218
219   /* This code is used in dl-runtime.c to call the `fixup' function
220      and then redirect to the address it returns.  */
221 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
222 | Trampoline for _dl_runtime_resolver
223         .globl _dl_runtime_resolve
224         .type _dl_runtime_resolve, @function
225 _dl_runtime_resolve:
226         | Pass two args to fixup: the PLT address computed from the PC saved
227         | in the PLT's call insn, and the reloc offset passed in %g1.
228         ld [%o7 + 8], %o1       | Second arg, loaded from PLTPC[2].
229         call fixup
230         shrl %g1, 22, %o0       | First arg, set in delay slot of call.
231         | Jump to the real function.
232         jmpl %o0, %g0
233         | In the delay slot of that jump, restore the register window
234         | saved by the first insn of the PLT.
235         restore
236         .size _dl_runtime_resolve, . - _dl_runtime_resolve
237 ");
238 /* The PLT uses Elf32_Rela relocs.  */
239 #define elf_machine_relplt elf_machine_rela
240 }
241
242
243 /* Mask identifying addresses reserved for the user program,
244    where the dynamic linker should not map anything.  */
245 #define ELF_MACHINE_USER_ADDRESS_MASK   ???
246
247 /* Initial entry point code for the dynamic linker.
248    The C function `_dl_start' is the real entry point;
249    its return value is the user program's entry point.  */
250
251 #define RTLD_START asm (???)