8b9872c15adc6acb8c8ce4fcfa5b66e2f27c2ebb
[platform/upstream/glibc.git] / sysdeps / m68k / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  m68k 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 #ifndef dl_machine_h
21 #define dl_machine_h
22
23 #define ELF_MACHINE_NAME "m68k"
24
25 #include <assert.h>
26
27 /* Return nonzero iff E_MACHINE is compatible with the running host.  */
28 static inline int
29 elf_machine_matches_host (Elf32_Half e_machine)
30 {
31   switch (e_machine)
32     {
33     case EM_68K:
34       return 1;
35     default:
36       return 0;
37     }
38 }
39
40
41 /* Return the run-time address of the _GLOBAL_OFFSET_TABLE_.
42    Must be inlined in a function which uses global data.  */
43 static inline Elf32_Addr *
44 elf_machine_got (void)
45 {
46   register Elf32_Addr *got asm ("%a5");
47   return got;
48 }
49
50
51 /* Return the run-time load address of the shared object.  */
52 static inline Elf32_Addr
53 elf_machine_load_address (void)
54 {
55   Elf32_Addr addr;
56   asm ("here:   lea here(%%pc), %0\n"
57        "        sub.l %#here, %0"
58        : "=a" (addr));
59   return addr;
60 }
61
62 /* The `subl' insn above will contain an R_68K_RELATIVE relocation
63    entry intended to insert the run-time address of the label `here'.
64    This will be the first relocation in the text of the dynamic
65    linker; we skip it to avoid trying to modify read-only text in this
66    early stage.  */
67 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
68   ((dynamic_info)[DT_RELA]->d_un.d_ptr += sizeof (Elf32_Rela), \
69    (dynamic_info)[DT_RELASZ]->d_un.d_val -= sizeof (Elf32_Rela))
70
71
72 /* Set up the loaded object described by L so its unrelocated PLT
73    entries will jump to the on-demand fixup code in dl-runtime.c.  */
74
75 static inline void
76 elf_machine_runtime_setup (struct link_map *l, int lazy)
77 {
78   Elf32_Addr *got;
79   extern void _dl_runtime_resolve (Elf32_Word);
80
81   if (l->l_info[DT_JMPREL] && lazy)
82     {
83       /* The GOT entries for functions in the PLT have not yet been
84          filled in.  Their initial contents will arrange when called
85          to push an offset into the .rela.plt section, push
86          _GLOBAL_OFFSET_TABLE_[1], and then jump to
87          _GLOBAL_OFFSET_TABLE_[2].  */
88       got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
89       got[1] = (Elf32_Addr) l;  /* Identify this shared object.  */
90       /* This function will get called to fix up the GOT entry
91          indicated by the offset on the stack, and then jump to the
92          resolved address.  */
93       got[2] = (Elf32_Addr) &_dl_runtime_resolve;
94     }
95
96   /* This code is used in dl-runtime.c to call the `fixup' function
97      and then redirect to the address it returns.  */
98 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
99 | Trampoline for _dl_runtime_resolver
100         .globl _dl_runtime_resolve
101         .type _dl_runtime_resolve, @function
102 _dl_runtime_resolve:
103         | Save %a0 (struct return address) and %a1.
104         move.l %a0, -(%sp)
105         move.l %a1, -(%sp)
106         | Call the real address resolver.
107         jbsr fixup
108         | Restore register %a0 and %a1.
109         move.l (%sp)+, %a1
110         move.l (%sp)+, %a0
111         | Pop parameters
112         addq.l #8, %sp
113         | Call real function.
114         jmp (%d0)
115         .size _dl_runtime_resolve, . - _dl_runtime_resolve
116 ");
117 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS long int save_a0, long int save_a1
118 /* The PLT uses Elf32_Rela relocs.  */
119 #define elf_machine_relplt elf_machine_rela
120 }
121
122
123 /* Mask identifying addresses reserved for the user program,
124    where the dynamic linker should not map anything.  */
125 #define ELF_MACHINE_USER_ADDRESS_MASK   0x80000000UL
126
127 /* Initial entry point code for the dynamic linker.
128    The C function `_dl_start' is the real entry point;
129    its return value is the user program's entry point.  */
130
131 #define RTLD_START asm ("\
132 .text
133 .globl _start
134 .globl _dl_start_user
135 _start:
136         move.l %sp, -(%sp)
137         jbsr _dl_start
138         addq.l #4, %sp
139 _dl_start_user:
140         | Save the user entry point address in %a4.
141         move.l %d0, %a4
142         | Point %a5 at the GOT.
143         lea _GLOBAL_OFFSET_TABLE_@GOTPC(%pc), %a5
144         | See if we were run as a command with the executable file
145         | name as an extra leading argument.
146         move.l ([_dl_skip_args@GOT, %a5]), %d0
147         jeq 0f
148         | Pop the original argument count
149         move.l (%sp)+, %d1
150         | Subtract _dl_skip_args from it.
151         sub.l %d0, %d1
152         | Adjust the stack pointer to skip _dl_skip_args words.
153         lea (%sp, %d0*4), %sp
154         | Push back the modified argument count.
155         move.l %d1, -(%sp)
156 0:      | Push _dl_default_scope[2] as argument in _dl_init_next call below.
157         move.l ([_dl_default_scope@GOT, %a5], 8), %d2
158 0:      move.l %d2, -(%sp)
159         | Call _dl_init_next to return the address of an initializer
160         | function to run.
161         bsr.l _dl_init_next@PLTPC
162         add.l #4, %sp | Pop argument.
163         | Check for zero return, when out of initializers.
164         tst.l %d0
165         jeq 1f
166         | Call the shared object initializer function.
167         | NOTE: We depend only on the registers (%d2, %a4 and %a5)
168         | and the return address pushed by this call;
169         | the initializer is called with the stack just
170         | as it appears on entry, and it is free to move
171         | the stack around, as long as it winds up jumping to
172         | the return address on the top of the stack.
173         move.l %d0, %a0
174         jsr (%a0)
175         | Loop to call _dl_init_next for the next initializer.
176         jra 0b
177 1:      | Clear the startup flag.
178         clr.l _dl_starting_up@GOT(%a5)
179         | Pass our finalizer function to the user in %a1.
180         move.l _dl_fini@GOT(%a5), %a1
181         | Initialize %fp with the stack pointer.
182         move.l %sp, %fp
183         | Jump to the user's entry point.
184         jmp (%a4)");
185
186 /* Nonzero iff TYPE describes relocation of a PLT entry, so
187    PLT entries should not be allowed to define the value.  */
188 #define elf_machine_pltrel_p(type) ((type) == R_68K_JMP_SLOT)
189
190 /* The m68k never uses Elf32_Rel relocations.  */
191 #define ELF_MACHINE_NO_REL 1
192
193 #endif /* !dl_machine_h */
194
195 #ifdef RESOLVE
196
197 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
198    MAP is the object containing the reloc.  */
199
200 static inline void
201 elf_machine_rela (struct link_map *map,
202                   const Elf32_Rela *reloc, const Elf32_Sym *sym)
203 {
204   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
205   Elf32_Addr loadbase;
206
207   switch (ELF32_R_TYPE (reloc->r_info))
208     {
209     case R_68K_COPY:
210       loadbase = RESOLVE (&sym, DL_LOOKUP_NOEXEC);
211       memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
212       break;
213     case R_68K_GLOB_DAT:
214       loadbase = RESOLVE (&sym, 0);
215       *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
216       break;
217     case R_68K_JMP_SLOT:
218       loadbase = RESOLVE (&sym, DL_LOOKUP_NOPLT);
219       *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
220       break;
221     case R_68K_8:
222       loadbase = RESOLVE (&sym, 0);
223       *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
224                               + reloc->r_addend);
225       break;
226     case R_68K_16:
227       loadbase = RESOLVE (&sym, 0);
228       *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
229                                + reloc->r_addend);
230       break;
231     case R_68K_32:
232       loadbase = RESOLVE (&sym, 0);
233       *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
234                      + reloc->r_addend);
235       break;
236     case R_68K_RELATIVE:
237       *reloc_addr = map->l_addr + reloc->r_addend;
238       break;
239     case R_68K_PC8:
240       loadbase = RESOLVE (&sym, 0);
241       *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
242                               + reloc->r_addend - (Elf32_Addr) reloc_addr);
243       break;
244     case R_68K_PC16:
245       loadbase = RESOLVE (&sym, 0);
246       *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
247                                + reloc->r_addend - (Elf32_Addr) reloc_addr);
248       break;
249     case R_68K_PC32:
250       loadbase = RESOLVE (&sym, 0);
251       *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
252                      + reloc->r_addend - (Elf32_Addr) reloc_addr);
253       break;
254     case R_68K_NONE:            /* Alright, Wilbur.  */
255       break;
256     default:
257       assert (! "unexpected dynamic reloc type");
258       break;
259     }
260 }
261
262 static inline void
263 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
264 {
265   Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
266   switch (ELF32_R_TYPE (reloc->r_info))
267     {
268     case R_68K_JMP_SLOT:
269       *reloc_addr += map->l_addr;
270       break;
271     default:
272       assert (! "unexpected PLT reloc type");
273       break;
274     }
275 }
276
277 #endif /* RESOLVE */