Tue Jun 4 18:57:57 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / sysdeps / m68k / dl-machine.h
blob6c6b01de2b40140e94230a3bdbfbc0173493f3d9
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.
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.
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.
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. */
20 #define ELF_MACHINE_NAME "m68k"
22 #include <assert.h>
23 #include <string.h>
24 #include <link.h>
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)
31 switch (e_machine)
33 case EM_68K:
34 return 1;
35 default:
36 return 0;
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)
46 register Elf32_Addr *got asm ("%a5");
47 return got;
51 /* Return the run-time load address of the shared object. */
52 static inline Elf32_Addr
53 elf_machine_load_address (void)
55 Elf32_Addr addr;
56 asm ("here: lea here(%%pc), %0\n"
57 " sub.l %#here, %0"
58 : "=a" (addr));
59 return addr;
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))
71 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
72 MAP is the object containing the reloc. */
74 static inline void
75 elf_machine_rela (struct link_map *map,
76 const Elf32_Rel *reloc, const Elf32_Sym *sym,
77 Elf32_Addr (*resolve) (const Elf32_Sym **ref,
78 Elf32_Addr reloc_addr,
79 int noplt))
81 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
82 Elf32_Addr loadbase;
84 switch (ELF32_R_TYPE (reloc->r_info))
86 case R_68K_COPY:
87 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
88 memcpy (reloc_addr, (void *) (loadbase + sym->st_value), sym->st_size);
89 break;
90 case R_68K_GLOB_DAT:
91 loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0) :
92 /* RESOLVE is null during bootstrap relocation. */
93 map->l_addr);
94 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
95 break;
96 case R_68K_JMP_SLOT:
97 loadbase = (resolve ? (*resolve) (&sym, (Elf32_Addr) reloc_addr, 1) :
98 /* RESOLVE is null during bootstrap relocation. */
99 map->l_addr);
100 *reloc_addr = sym ? (loadbase + sym->st_value) : 0;
101 break;
102 case R_68K_8:
103 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
104 *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
105 + reloc->r_addend);
106 break;
107 case R_68K_16:
108 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
109 *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
110 + reloc->r_addend);
111 break;
112 case R_68K_32:
113 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
114 *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
115 + reloc->r_addend);
116 break;
117 case R_68K_RELATIVE:
118 *reloc_addr = map->l_addr + reloc->r_addend;
119 break;
120 case R_68K_PC8:
121 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
122 *(char *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
123 + reloc->r_addend
124 - (Elf32_Addr) reloc_addr);
125 break;
126 case R_68K_PC16:
127 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
128 *(short *) reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
129 + reloc->r_addend
130 - (Elf32_Addr) reloc_addr);
131 break;
132 case R_68K_PC32:
133 loadbase = (*resolve) (&sym, (Elf32_Addr) reloc_addr, 0);
134 *reloc_addr = ((sym ? (loadbase + sym->st_value) : 0)
135 + reloc->r_addend
136 - (Elf32_Addr) reloc_addr);
137 break;
138 case R_68K_NONE: /* Alright, Wilbur. */
139 break;
140 default:
141 assert (! "unexpected dynamic reloc type");
142 break;
146 static inline void
147 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
149 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
150 switch (ELF32_R_TYPE (reloc->r_info))
152 case R_68K_NONE:
153 break;
154 case R_68K_JMP_SLOT:
155 *reloc_addr += map->l_addr;
156 break;
157 default:
158 assert (! "unexpected PLT reloc type");
159 break;
163 /* Nonzero iff TYPE describes relocation of a PLT entry, so
164 PLT entries should not be allowed to define the value. */
165 #define elf_machine_pltrel_p(type) ((type) == R_68K_JMP_SLOT)
167 /* The m68k never uses Elf32_Rel relocations. */
168 #define ELF_MACHINE_NO_REL 1
171 /* Set up the loaded object described by L so its unrelocated PLT
172 entries will jump to the on-demand fixup code in dl-runtime.c. */
174 static inline void
175 elf_machine_runtime_setup (struct link_map *l, int lazy)
177 Elf32_Addr *got;
178 extern void _dl_runtime_resolve (Elf32_Word);
180 if (l->l_info[DT_JMPREL] && lazy)
182 /* The GOT entries for functions in the PLT have not yet been
183 filled in. Their initial contents will arrange when called
184 to push an offset into the .rela.plt section, push
185 _GLOBAL_OFFSET_TABLE_[1], and then jump to
186 _GLOBAL_OFFSET_TABLE_[2]. */
187 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
188 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
189 /* This function will get called to fix up the GOT entry
190 indicated by the offset on the stack, and then jump to the
191 resolved address. */
192 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
195 /* This code is used in dl-runtime.c to call the `fixup' function
196 and then redirect to the address it returns. */
197 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
198 | Trampoline for _dl_runtime_resolver
199 .globl _dl_runtime_resolve
200 .type _dl_runtime_resolve, @function
201 _dl_runtime_resolve:
202 | Save %a0 (struct return address) and %a1.
203 move.l %a0, -(%sp)
204 move.l %a1, -(%sp)
205 | Call the real address resolver.
206 jbsr fixup
207 | Restore register %a0 and %a1.
208 move.l (%sp)+, %a1
209 move.l (%sp)+, %a0
210 | Pop parameters
211 addq.l #8, %sp
212 | Call real function.
213 jmp (%d0)
214 .size _dl_runtime_resolve, . - _dl_runtime_resolve
216 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS long int save_a0, long int save_a1
217 /* The PLT uses Elf32_Rela relocs. */
218 #define elf_machine_relplt elf_machine_rela
222 /* Mask identifying addresses reserved for the user program,
223 where the dynamic linker should not map anything. */
224 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
226 /* Initial entry point code for the dynamic linker.
227 The C function `_dl_start' is the real entry point;
228 its return value is the user program's entry point. */
230 #define RTLD_START asm ("\
231 .text
232 .globl _start
233 .globl _dl_start_user
234 _start:
235 jbsr _dl_start
236 _dl_start_user:
237 | Save the user entry point address in %a4.
238 move.l %d0, %a4
239 | Point %a5 at the GOT.
240 lea _GLOBAL_OFFSET_TABLE_@GOTPC(%pc), %a5
241 | See if we were run as a command with the executable file
242 | name as an extra leading argument.
243 move.l ([_dl_skip_args@GOT, %a5]), %d0
244 jeq 0f
245 | Pop the original argument count
246 move.l (%sp)+, %d1
247 | Subtract _dl_skip_args from it.
248 sub.l %d0, %d1
249 | Adjust the stack pointer to skip _dl_skip_args words.
250 lea (%sp, %d0*4), %sp
251 | Push back the modified argument count.
252 move.l %d1, -(%sp)
253 | Push _dl_loaded as argument in _dl_init_next call below.
254 move.l ([_dl_loaded@GOT, %a5]), %d2
255 0: move.l %d2, -(%sp)
256 | Call _dl_init_next to return the address of an initializer
257 | function to run.
258 bsr.l _dl_init_next@PLTPC
259 add.l #4, %sp | Pop argument.
260 | Check for zero return, when out of initializers.
261 tst.l %d0
262 jeq 1f
263 | Call the shared object initializer function.
264 | NOTE: We depend only on the registers (%d2, %a4 and %a5)
265 | and the return address pushed by this call;
266 | the initializer is called with the stack just
267 | as it appears on entry, and it is free to move
268 | the stack around, as long as it winds up jumping to
269 | the return address on the top of the stack.
270 move.l %d0, %a0
271 jsr (%a0)
272 | Loop to call _dl_init_next for the next initializer.
273 jra 0b
274 1: | Pass our finalizer function to the user in %a1.
275 move.l _dl_fini@GOT(%a5), %a1
276 | Initialize %fp with the stack pointer.
277 move.l %sp, %fp
278 | Jump to the user's entry point.
279 jmp (%a4)");