Update.
[glibc.git] / sysdeps / m68k / dl-machine.h
blob1d2045dd87495640edbfec8fa5949b5577778a31
1 /* Machine-dependent ELF dynamic relocation inline functions. m68k version.
2 Copyright (C) 1996, 1997 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.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "m68k"
25 #include <sys/param.h>
27 #include <assert.h>
29 /* Return nonzero iff E_MACHINE is compatible with the running host. */
30 static inline int
31 elf_machine_matches_host (Elf32_Half e_machine)
33 switch (e_machine)
35 case EM_68K:
36 return 1;
37 default:
38 return 0;
43 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
44 first element of the GOT. This must be inlined in a function which
45 uses global data. */
46 static inline Elf32_Addr
47 elf_machine_dynamic (void)
49 register Elf32_Addr *got asm ("%a5");
50 return *got;
54 /* Return the run-time load address of the shared object. */
55 static inline Elf32_Addr
56 elf_machine_load_address (void)
58 Elf32_Addr addr;
59 asm ("lea _dl_start(%%pc), %0\n\t"
60 "sub.l _dl_start@GOTPC(%%pc), %0"
61 : "=a" (addr));
62 return addr;
66 /* Set up the loaded object described by L so its unrelocated PLT
67 entries will jump to the on-demand fixup code in dl-runtime.c. */
69 static inline int
70 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
72 Elf32_Addr *got;
73 extern void _dl_runtime_resolve (Elf32_Word);
74 extern void _dl_runtime_profile (Elf32_Word);
76 if (l->l_info[DT_JMPREL] && lazy)
78 /* The GOT entries for functions in the PLT have not yet been
79 filled in. Their initial contents will arrange when called
80 to push an offset into the .rela.plt section, push
81 _GLOBAL_OFFSET_TABLE_[1], and then jump to
82 _GLOBAL_OFFSET_TABLE_[2]. */
83 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
84 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
86 /* The got[2] entry contains the address of a function which gets
87 called to get the address of a so far unresolved function and
88 jump to it. The profiling extension of the dynamic linker allows
89 to intercept the calls to collect information. In this case we
90 don't store the address in the GOT so that all future calls also
91 end in this function. */
92 if (profile)
94 got[2] = (Elf32_Addr) &_dl_runtime_profile;
95 /* Say that we really want profiling and the timers are started. */
96 _dl_profile_map = l;
98 else
99 /* This function will get called to fix up the GOT entry indicated by
100 the offset on the stack, and then jump to the resolved address. */
101 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
104 return lazy;
107 /* This code is used in dl-runtime.c to call the `fixup' function
108 and then redirect to the address it returns. */
109 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name) \
110 "| Trampoline for " #fixup_name "
111 .globl " #tramp_name "
112 .type " #tramp_name ", @function
113 " #tramp_name ":
114 | Save %a0 (struct return address) and %a1.
115 move.l %a0, -(%sp)
116 move.l %a1, -(%sp)
117 | Call the real address resolver.
118 jbsr " #fixup_name "
119 | Restore register %a0 and %a1.
120 move.l (%sp)+, %a1
121 move.l (%sp)+, %a0
122 | Pop parameters
123 addq.l #8, %sp
124 | Call real function.
125 jmp (%d0)
126 .size " #tramp_name ", . - " #tramp_name "\n"
127 #ifndef PROF
128 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
129 asm (TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup) \
130 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup));
131 #else
132 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
133 asm (TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup) \
134 ".globl _dl_runtime_profile\n" \
135 ".set _dl_runtime_profile, _dl_runtime_resolve");
136 #endif
137 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS long int save_a0, long int save_a1
140 /* Mask identifying addresses reserved for the user program,
141 where the dynamic linker should not map anything. */
142 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
144 /* Initial entry point code for the dynamic linker.
145 The C function `_dl_start' is the real entry point;
146 its return value is the user program's entry point. */
148 #define RTLD_START asm ("\
149 .text
150 .globl _start
151 .type _start,@function
152 _start:
153 move.l %sp, -(%sp)
154 jbsr _dl_start
155 addq.l #4, %sp
157 .globl _dl_start_user
158 .type _dl_start_user,@function
159 _dl_start_user:
160 | Save the user entry point address in %a4.
161 move.l %d0, %a4
162 | Point %a5 at the GOT.
163 lea _GLOBAL_OFFSET_TABLE_@GOTPC(%pc), %a5
164 | See if we were run as a command with the executable file
165 | name as an extra leading argument.
166 move.l ([_dl_skip_args@GOT, %a5]), %d0
167 jeq 0f
168 | Pop the original argument count
169 move.l (%sp)+, %d1
170 | Subtract _dl_skip_args from it.
171 sub.l %d0, %d1
172 | Adjust the stack pointer to skip _dl_skip_args words.
173 lea (%sp, %d0*4), %sp
174 | Push back the modified argument count.
175 move.l %d1, -(%sp)
176 0: | Push _dl_default_scope[2] as argument in _dl_init_next call below.
177 move.l ([_dl_default_scope@GOT, %a5], 8), %d2
178 0: move.l %d2, -(%sp)
179 | Call _dl_init_next to return the address of an initializer
180 | function to run.
181 bsr.l _dl_init_next@PLTPC
182 add.l #4, %sp | Pop argument.
183 | Check for zero return, when out of initializers.
184 tst.l %d0
185 jeq 1f
186 | Call the shared object initializer function.
187 | NOTE: We depend only on the registers (%d2, %a4 and %a5)
188 | and the return address pushed by this call;
189 | the initializer is called with the stack just
190 | as it appears on entry, and it is free to move
191 | the stack around, as long as it winds up jumping to
192 | the return address on the top of the stack.
193 move.l %d0, %a0
194 jsr (%a0)
195 | Loop to call _dl_init_next for the next initializer.
196 jra 0b
197 1: | Clear the startup flag.
198 clr.l _dl_starting_up@GOT(%a5)
199 | Pass our finalizer function to the user in %a1.
200 move.l _dl_fini@GOT(%a5), %a1
201 | Initialize %fp with the stack pointer.
202 move.l %sp, %fp
203 | Jump to the user's entry point.
204 jmp (%a4)
205 .size _dl_start_user, . - _dl_start_user
206 .previous");
208 /* Nonzero iff TYPE describes a relocation that should
209 skip the executable when looking up the symbol value. */
210 #define elf_machine_lookup_noexec_p(type) ((type) == R_68K_COPY)
212 /* Nonzero iff TYPE describes relocation of a PLT entry, so
213 PLT entries should not be allowed to define the value. */
214 #define elf_machine_lookup_noplt_p(type) ((type) == R_68K_JMP_SLOT)
216 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
217 #define ELF_MACHINE_JMP_SLOT R_68K_JMP_SLOT
219 /* The m68k never uses Elf32_Rel relocations. */
220 #define ELF_MACHINE_NO_REL 1
222 static inline void
223 elf_machine_fixup_plt (struct link_map *map, const Elf32_Rela *reloc,
224 Elf32_Addr *reloc_addr, Elf32_Addr value)
226 *reloc_addr = value;
229 /* Return the final value of a plt relocation. On the m68k the JMP_SLOT
230 relocation ignores the addend. */
231 static inline Elf32_Addr
232 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
233 Elf32_Addr value)
235 return value;
238 #endif /* !dl_machine_h */
240 #ifdef RESOLVE
242 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
243 MAP is the object containing the reloc. */
245 static inline void
246 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
247 const Elf32_Sym *sym, const struct r_found_version *version,
248 Elf32_Addr *const reloc_addr)
250 if (ELF32_R_TYPE (reloc->r_info) == R_68K_RELATIVE)
251 *reloc_addr = map->l_addr + reloc->r_addend;
252 else
254 const Elf32_Sym *const refsym = sym;
255 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
256 if (sym)
257 value += sym->st_value;
259 switch (ELF32_R_TYPE (reloc->r_info))
261 case R_68K_COPY:
262 if (sym == NULL)
263 /* This can happen in trace mode if an object could not be
264 found. */
265 break;
266 if (sym->st_size > refsym->st_size
267 || (_dl_verbose && sym->st_size < refsym->st_size))
269 extern char **_dl_argv;
270 const char *strtab;
272 strtab = ((void *) map->l_addr
273 + map->l_info[DT_STRTAB]->d_un.d_ptr);
274 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
275 ": Symbol `", strtab + refsym->st_name,
276 "' has different size in shared object, "
277 "consider re-linking\n", NULL);
279 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
280 refsym->st_size));
281 break;
282 case R_68K_GLOB_DAT:
283 case R_68K_JMP_SLOT:
284 *reloc_addr = value;
285 break;
286 case R_68K_8:
287 *(char *) reloc_addr = value + reloc->r_addend;
288 break;
289 case R_68K_16:
290 *(short *) reloc_addr = value + reloc->r_addend;
291 break;
292 case R_68K_32:
293 *reloc_addr = value + reloc->r_addend;
294 break;
295 case R_68K_PC8:
296 *(char *) reloc_addr
297 = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
298 break;
299 case R_68K_PC16:
300 *(short *) reloc_addr
301 = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
302 break;
303 case R_68K_PC32:
304 *reloc_addr = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
305 break;
306 case R_68K_NONE: /* Alright, Wilbur. */
307 break;
308 default:
309 assert (! "unexpected dynamic reloc type");
310 break;
315 static inline void
316 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
318 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
319 switch (ELF32_R_TYPE (reloc->r_info))
321 case R_68K_JMP_SLOT:
322 *reloc_addr += map->l_addr;
323 break;
324 default:
325 assert (! "unexpected PLT reloc type");
326 break;
330 #endif /* RESOLVE */