695b9d6af39f4254f347cb96075298d8b49d5bc2
[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., 675 Mass Ave,
18 Cambridge, MA 02139, 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 run-time address of the _GLOBAL_OFFSET_TABLE_.
50    Must be inlined in a function which uses global data.  */
51 static inline Elf32_Addr *
52 elf_machine_got (void)
53 {
54   register Elf32_Addr *got asm ("%l7");
55   return got;
56 }
57
58
59 /* Return the run-time load address of the shared object.  */
60 static inline Elf32_Addr
61 elf_machine_load_address (void)
62 {
63   Elf32_Addr addr;
64 ???
65   return addr;
66 }
67
68 /* The `subl' insn above will contain an R_68K_RELATIVE relocation
69    entry intended to insert the run-time address of the label `here'.
70    This will be the first relocation in the text of the dynamic
71    linker; we skip it to avoid trying to modify read-only text in this
72    early stage.  */
73 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
74   ((dynamic_info)[DT_RELA]->d_un.d_ptr += sizeof (Elf32_Rela), \
75    (dynamic_info)[DT_RELASZ]->d_un.d_val -= sizeof (Elf32_Rela))
76
77 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
78    MAP is the object containing the reloc.  */
79
80 static inline void
81 elf_machine_rela (struct link_map *map,
82                   const Elf32_Rela *reloc, const Elf32_Sym *sym,
83                   Elf32_Addr (*resolve) (const Elf32_Sym **ref,
84                                          Elf32_Addr reloc_addr,
85                                          int noplt))
86 {
87   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
88   Elf32_Addr loadbase;
89
90   switch (ELF32_R_TYPE (reloc->r_info))
91     {
92     case R_SPARC_COPY:
93       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
94       memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
95       break;
96     case R_SPARC_GLOB_DAT:
97     case R_SPARC_32:
98       loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
99                   /* RESOLVE is null during bootstrap relocation.  */
100                   map->l_addr);
101       *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
102                      + reloc->r_addend);
103       break;
104     case R_SPARC_JMP_SLOT:
105       loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 1) :
106                   /* RESOLVE is null during bootstrap relocation.  */
107                   map->l_addr);
108       {
109         Elf32_Addr value = ((sym ? (loadbase + sym->st_value) : 0)
110                             + reloc->r_addend);
111         reloc_addr[1] = OPCODE_SETHI | (value >> 10);
112         reloc_addr[2] = OPCODE_JMP_G1 | (value & 0x3ff);
113       }
114       break;
115     case R_SPARC_8:
116       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
117       *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
118                               + reloc->r_addend);
119       break;
120     case R_SPARC_16:
121       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
122       *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
123                                + reloc->r_addend);
124       break;
125     case R_SPARC_32:
126       loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
127                   /* RESOLVE is null during bootstrap relocation.  */
128                   map->l_addr);
129       break;
130     case R_SPARC_RELATIVE:
131       *reloc_addr = map->l_addr + reloc->r_addend;
132       break;
133     case R_SPARC_DISP8:
134       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
135       *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
136                               + reloc->r_addend
137                               - (Elf32_Addr) reloc_addr);
138       break;
139     case R_SPARC_DISP16:
140       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
141       *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
142                                + reloc->r_addend
143                                - (Elf32_Addr) reloc_addr);
144       break;
145     case R_SPARC_DISP32:
146       loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
147       *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
148                      + reloc->r_addend
149                      - (Elf32_Addr) reloc_addr);
150       break;
151     case R_SPARC_NONE:          /* Alright, Wilbur.  */
152       break;
153     default:
154       assert (! "unexpected dynamic reloc type");
155       break;
156     }
157 }
158
159 static inline void
160 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
161 {
162   switch (ELF32_R_TYPE (reloc->r_info))
163     {
164     case R_SPARC_NONE:
165       break;
166     case R_SPARC_JMP_SLOT:
167       break;
168     default:
169       assert (! "unexpected PLT reloc type");
170       break;
171     }
172 }
173
174 /* Nonzero iff TYPE describes relocation of a PLT entry, so
175    PLT entries should not be allowed to define the value.  */
176 #define elf_machine_pltrel_p(type) ((type) == R_SPARC_JMP_SLOT)
177
178 /* The SPARC never uses Elf32_Rel relocations.  */
179 #define ELF_MACHINE_NO_REL 1
180
181
182 /* Set up the loaded object described by L so its unrelocated PLT
183    entries will jump to the on-demand fixup code in dl-runtime.c.  */
184
185 static inline void
186 elf_machine_runtime_setup (struct link_map *l, int lazy)
187 {
188   Elf32_Addr *plt;
189   extern void _dl_runtime_resolve (Elf32_Word);
190
191   if (l->l_info[DT_JMPREL] && lazy)
192     {
193       /* The entries for functions in the PLT have not yet been filled in.
194          Their initial contents will arrange when called to set the high 22
195          bits of %g1 with an offset into the .rela.plt section and jump to
196          the beginning of the PLT.  */
197       plt = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
198
199       /* The beginning of the PLT does:
200
201                 save %sp, -64, %sp
202          pltpc: call _dl_runtime_resolve
203                 nop
204                 .word MAP
205
206          This saves the register window containing the arguments, and the
207          PC value (pltpc) implicitly saved in %o7 by the call points near the
208          location where we store the link_map pointer for this object.  */
209
210       plt[0] = OPCODE_SAVE_SP64; /* save %sp, -64, %sp */
211       /* Construct PC-relative word address.  */
212       plt[1] = OPCODE_CALL | (((Elf32_Addr) &_dl_runtime_resolve -
213                                (Elf32_Addr) &plt[1]) >> 2);
214       plt[2] = OPCODE_NOP;      /* Fill call delay slot.  */
215       plt[3] = l;
216     }
217
218   /* This code is used in dl-runtime.c to call the `fixup' function
219      and then redirect to the address it returns.  */
220 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
221 | Trampoline for _dl_runtime_resolver
222         .globl _dl_runtime_resolve
223         .type _dl_runtime_resolve, @function
224 _dl_runtime_resolve:
225         | Pass two args to fixup: the PLT address computed from the PC saved
226         | in the PLT's call insn, and the reloc offset passed in %g1.
227         ld [%o7 + 8], %o1       | Second arg, loaded from PLTPC[2].
228         call fixup
229         shrl %g1, 22, %o0       | First arg, set in delay slot of call.
230         | Jump to the real function.
231         jmpl %o0, %g0
232         | In the delay slot of that jump, restore the register window
233         | saved by the first insn of the PLT.
234         restore
235         .size _dl_runtime_resolve, . - _dl_runtime_resolve
236 ");
237 /* The PLT uses Elf32_Rela relocs.  */
238 #define elf_machine_relplt elf_machine_rela
239 }
240
241
242 /* Mask identifying addresses reserved for the user program,
243    where the dynamic linker should not map anything.  */
244 #define ELF_MACHINE_USER_ADDRESS_MASK   ???
245
246 /* Initial entry point code for the dynamic linker.
247    The C function `_dl_start' is the real entry point;
248    its return value is the user program's entry point.  */
249
250 #define RTLD_START asm (???)