PowerPC specific implementation of llround.
[glibc/history.git] / sysdeps / m68k / dl-machine.h
blob01fc3399285601321a9ebcbdc700398c043deb90
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 (".Lhere: lea .Lhere(%%pc), %0\n"
60 " sub.l %#.Lhere, %0"
61 : "=a" (addr));
62 return addr;
65 /* The `subl' insn above will contain an R_68K_RELATIVE relocation
66 entry intended to insert the run-time address of the label `.Lhere'.
67 This will be the first relocation in the text of the dynamic
68 linker; we skip it to avoid trying to modify read-only text in this
69 early stage. */
70 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
71 ((dynamic_info)[DT_RELA]->d_un.d_ptr += sizeof (Elf32_Rela), \
72 (dynamic_info)[DT_RELASZ]->d_un.d_val -= sizeof (Elf32_Rela))
75 /* Set up the loaded object described by L so its unrelocated PLT
76 entries will jump to the on-demand fixup code in dl-runtime.c. */
78 static inline int
79 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
81 Elf32_Addr *got;
82 extern void _dl_runtime_resolve (Elf32_Word);
83 extern void _dl_runtime_profile (Elf32_Word);
85 if (l->l_info[DT_JMPREL] && lazy)
87 /* The GOT entries for functions in the PLT have not yet been
88 filled in. Their initial contents will arrange when called
89 to push an offset into the .rela.plt section, push
90 _GLOBAL_OFFSET_TABLE_[1], and then jump to
91 _GLOBAL_OFFSET_TABLE_[2]. */
92 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
93 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
95 /* The got[2] entry contains the address of a function which gets
96 called to get the address of a so far unresolved function and
97 jump to it. The profiling extension of the dynamic linker allows
98 to intercept the calls to collect information. In this case we
99 don't store the address in the GOT so that all future calls also
100 end in this function. */
101 if (profile)
103 got[2] = (Elf32_Addr) &_dl_runtime_profile;
104 /* Say that we really want profiling and the timers are started. */
105 _dl_profile_map = l;
107 else
108 /* This function will get called to fix up the GOT entry indicated by
109 the offset on the stack, and then jump to the resolved address. */
110 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
113 return lazy;
116 /* This code is used in dl-runtime.c to call the `fixup' function
117 and then redirect to the address it returns. */
118 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name) \
119 "| Trampoline for " #fixup_name "
120 .globl " #tramp_name "
121 .type " #tramp_name ", @function
122 " #tramp_name ":
123 | Save %a0 (struct return address) and %a1.
124 move.l %a0, -(%sp)
125 move.l %a1, -(%sp)
126 | Call the real address resolver.
127 jbsr " #fixup_name "
128 | Restore register %a0 and %a1.
129 move.l (%sp)+, %a1
130 move.l (%sp)+, %a0
131 | Pop parameters
132 addq.l #8, %sp
133 | Call real function.
134 jmp (%d0)
135 .size " #tramp_name ", . - " #tramp_name "\n"
136 #ifndef PROF
137 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
138 asm (TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup) \
139 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup));
140 #else
141 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
142 asm (TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup) \
143 ".globl _dl_runtime_profile\n" \
144 ".set _dl_runtime_profile, _dl_runtime_resolve");
145 #endif
146 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS long int save_a0, long int save_a1
147 /* The PLT uses Elf32_Rela relocs. */
148 #define elf_machine_relplt elf_machine_rela
151 /* Mask identifying addresses reserved for the user program,
152 where the dynamic linker should not map anything. */
153 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
155 /* Initial entry point code for the dynamic linker.
156 The C function `_dl_start' is the real entry point;
157 its return value is the user program's entry point. */
159 #define RTLD_START asm ("\
160 .text
161 .globl _start
162 .globl _dl_start_user
163 _start:
164 move.l %sp, -(%sp)
165 jbsr _dl_start
166 addq.l #4, %sp
167 _dl_start_user:
168 | Save the user entry point address in %a4.
169 move.l %d0, %a4
170 | Point %a5 at the GOT.
171 lea _GLOBAL_OFFSET_TABLE_@GOTPC(%pc), %a5
172 | See if we were run as a command with the executable file
173 | name as an extra leading argument.
174 move.l ([_dl_skip_args@GOT, %a5]), %d0
175 jeq 0f
176 | Pop the original argument count
177 move.l (%sp)+, %d1
178 | Subtract _dl_skip_args from it.
179 sub.l %d0, %d1
180 | Adjust the stack pointer to skip _dl_skip_args words.
181 lea (%sp, %d0*4), %sp
182 | Push back the modified argument count.
183 move.l %d1, -(%sp)
184 0: | Push _dl_default_scope[2] as argument in _dl_init_next call below.
185 move.l ([_dl_default_scope@GOT, %a5], 8), %d2
186 0: move.l %d2, -(%sp)
187 | Call _dl_init_next to return the address of an initializer
188 | function to run.
189 bsr.l _dl_init_next@PLTPC
190 add.l #4, %sp | Pop argument.
191 | Check for zero return, when out of initializers.
192 tst.l %d0
193 jeq 1f
194 | Call the shared object initializer function.
195 | NOTE: We depend only on the registers (%d2, %a4 and %a5)
196 | and the return address pushed by this call;
197 | the initializer is called with the stack just
198 | as it appears on entry, and it is free to move
199 | the stack around, as long as it winds up jumping to
200 | the return address on the top of the stack.
201 move.l %d0, %a0
202 jsr (%a0)
203 | Loop to call _dl_init_next for the next initializer.
204 jra 0b
205 1: | Clear the startup flag.
206 clr.l _dl_starting_up@GOT(%a5)
207 | Pass our finalizer function to the user in %a1.
208 move.l _dl_fini@GOT(%a5), %a1
209 | Initialize %fp with the stack pointer.
210 move.l %sp, %fp
211 | Jump to the user's entry point.
212 jmp (%a4)");
214 /* Nonzero iff TYPE describes a relocation that should
215 skip the executable when looking up the symbol value. */
216 #define elf_machine_lookup_noexec_p(type) ((type) == R_68K_COPY)
218 /* Nonzero iff TYPE describes relocation of a PLT entry, so
219 PLT entries should not be allowed to define the value. */
220 #define elf_machine_lookup_noplt_p(type) ((type) == R_68K_JMP_SLOT)
222 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
223 #define ELF_MACHINE_RELOC_NOPLT R_68K_JMP_SLOT
225 /* The m68k never uses Elf32_Rel relocations. */
226 #define ELF_MACHINE_NO_REL 1
228 #endif /* !dl_machine_h */
230 #ifdef RESOLVE
232 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
233 MAP is the object containing the reloc. */
235 static inline void
236 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
237 const Elf32_Sym *sym, const struct r_found_version *version,
238 Elf32_Addr *const reloc_addr)
240 if (ELF32_R_TYPE (reloc->r_info) == R_68K_RELATIVE)
241 *reloc_addr = map->l_addr + reloc->r_addend;
242 else
244 const Elf32_Sym *const refsym = sym;
245 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
246 if (sym)
247 value += sym->st_value;
249 switch (ELF32_R_TYPE (reloc->r_info))
251 case R_68K_COPY:
252 if (sym == NULL)
253 /* This can happen in trace mode if an object could not be
254 found. */
255 break;
256 if (sym->st_size > refsym->st_size
257 || (_dl_verbose && sym->st_size < refsym->st_size))
259 extern char **_dl_argv;
260 const char *strtab;
262 strtab = ((void *) map->l_addr
263 + map->l_info[DT_STRTAB]->d_un.d_ptr);
264 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
265 ": Symbol `", strtab + refsym->st_name,
266 "' has different size in shared object, "
267 "consider re-linking\n", NULL);
269 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
270 refsym->st_size));
271 break;
272 case R_68K_GLOB_DAT:
273 case R_68K_JMP_SLOT:
274 *reloc_addr = value;
275 break;
276 case R_68K_8:
277 *(char *) reloc_addr = value + reloc->r_addend;
278 break;
279 case R_68K_16:
280 *(short *) reloc_addr = value + reloc->r_addend;
281 break;
282 case R_68K_32:
283 *reloc_addr = value + reloc->r_addend;
284 break;
285 case R_68K_PC8:
286 *(char *) reloc_addr
287 = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
288 break;
289 case R_68K_PC16:
290 *(short *) reloc_addr
291 = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
292 break;
293 case R_68K_PC32:
294 *reloc_addr = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
295 break;
296 case R_68K_NONE: /* Alright, Wilbur. */
297 break;
298 default:
299 assert (! "unexpected dynamic reloc type");
300 break;
305 static inline void
306 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
308 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
309 switch (ELF32_R_TYPE (reloc->r_info))
311 case R_68K_JMP_SLOT:
312 *reloc_addr += map->l_addr;
313 break;
314 default:
315 assert (! "unexpected PLT reloc type");
316 break;
320 #endif /* RESOLVE */