(elf_machine_rela): Mention program name in warning message.
[glibc/history.git] / sysdeps / m68k / dl-machine.h
blobc0a17c73c23286ffea83d76e409c63e4add500e7
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)
81 Elf32_Addr *got;
82 extern void _dl_runtime_resolve (Elf32_Word);
84 if (l->l_info[DT_JMPREL] && lazy)
86 /* The GOT entries for functions in the PLT have not yet been
87 filled in. Their initial contents will arrange when called
88 to push an offset into the .rela.plt section, push
89 _GLOBAL_OFFSET_TABLE_[1], and then jump to
90 _GLOBAL_OFFSET_TABLE_[2]. */
91 got = (Elf32_Addr *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
92 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
93 /* This function will get called to fix up the GOT entry
94 indicated by the offset on the stack, and then jump to the
95 resolved address. */
96 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
99 return lazy;
102 /* This code is used in dl-runtime.c to call the `fixup' function
103 and then redirect to the address it returns. */
104 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
105 | Trampoline for _dl_runtime_resolver
106 .globl _dl_runtime_resolve
107 .type _dl_runtime_resolve, @function
108 _dl_runtime_resolve:
109 | Save %a0 (struct return address) and %a1.
110 move.l %a0, -(%sp)
111 move.l %a1, -(%sp)
112 | Call the real address resolver.
113 jbsr fixup
114 | Restore register %a0 and %a1.
115 move.l (%sp)+, %a1
116 move.l (%sp)+, %a0
117 | Pop parameters
118 addq.l #8, %sp
119 | Call real function.
120 jmp (%d0)
121 .size _dl_runtime_resolve, . - _dl_runtime_resolve
123 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS long int save_a0, long int save_a1
124 /* The PLT uses Elf32_Rela relocs. */
125 #define elf_machine_relplt elf_machine_rela
128 /* Mask identifying addresses reserved for the user program,
129 where the dynamic linker should not map anything. */
130 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
132 /* Initial entry point code for the dynamic linker.
133 The C function `_dl_start' is the real entry point;
134 its return value is the user program's entry point. */
136 #define RTLD_START asm ("\
137 .text
138 .globl _start
139 .globl _dl_start_user
140 _start:
141 move.l %sp, -(%sp)
142 jbsr _dl_start
143 addq.l #4, %sp
144 _dl_start_user:
145 | Save the user entry point address in %a4.
146 move.l %d0, %a4
147 | Point %a5 at the GOT.
148 lea _GLOBAL_OFFSET_TABLE_@GOTPC(%pc), %a5
149 | See if we were run as a command with the executable file
150 | name as an extra leading argument.
151 move.l ([_dl_skip_args@GOT, %a5]), %d0
152 jeq 0f
153 | Pop the original argument count
154 move.l (%sp)+, %d1
155 | Subtract _dl_skip_args from it.
156 sub.l %d0, %d1
157 | Adjust the stack pointer to skip _dl_skip_args words.
158 lea (%sp, %d0*4), %sp
159 | Push back the modified argument count.
160 move.l %d1, -(%sp)
161 0: | Push _dl_default_scope[2] as argument in _dl_init_next call below.
162 move.l ([_dl_default_scope@GOT, %a5], 8), %d2
163 0: move.l %d2, -(%sp)
164 | Call _dl_init_next to return the address of an initializer
165 | function to run.
166 bsr.l _dl_init_next@PLTPC
167 add.l #4, %sp | Pop argument.
168 | Check for zero return, when out of initializers.
169 tst.l %d0
170 jeq 1f
171 | Call the shared object initializer function.
172 | NOTE: We depend only on the registers (%d2, %a4 and %a5)
173 | and the return address pushed by this call;
174 | the initializer is called with the stack just
175 | as it appears on entry, and it is free to move
176 | the stack around, as long as it winds up jumping to
177 | the return address on the top of the stack.
178 move.l %d0, %a0
179 jsr (%a0)
180 | Loop to call _dl_init_next for the next initializer.
181 jra 0b
182 1: | Clear the startup flag.
183 clr.l _dl_starting_up@GOT(%a5)
184 | Pass our finalizer function to the user in %a1.
185 move.l _dl_fini@GOT(%a5), %a1
186 | Initialize %fp with the stack pointer.
187 move.l %sp, %fp
188 | Jump to the user's entry point.
189 jmp (%a4)");
191 /* Nonzero iff TYPE describes a relocation that should
192 skip the executable when looking up the symbol value. */
193 #define elf_machine_lookup_noexec_p(type) ((type) == R_68K_COPY)
195 /* Nonzero iff TYPE describes relocation of a PLT entry, so
196 PLT entries should not be allowed to define the value. */
197 #define elf_machine_lookup_noplt_p(type) ((type) == R_68K_JMP_SLOT)
199 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
200 #define ELF_MACHINE_RELOC_NOPLT R_68K_JMP_SLOT
202 /* The m68k never uses Elf32_Rel relocations. */
203 #define ELF_MACHINE_NO_REL 1
205 #endif /* !dl_machine_h */
207 #ifdef RESOLVE
209 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
210 MAP is the object containing the reloc. */
212 static inline void
213 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
214 const Elf32_Sym *sym, const struct r_found_version *version)
216 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
218 if (ELF32_R_TYPE (reloc->r_info) == R_68K_RELATIVE)
219 *reloc_addr = map->l_addr + reloc->r_addend;
220 else
222 const Elf32_Sym *const refsym = sym;
223 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
224 if (sym)
225 value += sym->st_value;
227 switch (ELF32_R_TYPE (reloc->r_info))
229 case R_68K_COPY:
230 if (sym == NULL)
231 /* This can happen in trace mode if an object could not be
232 found. */
233 break;
234 if (sym->st_size > refsym->st_size
235 || (_dl_verbose && sym->st_size < refsym->st_size))
237 extern char **_dl_argv;
238 const char *strtab;
240 strtab = ((void *) map->l_addr
241 + map->l_info[DT_STRTAB]->d_un.d_ptr);
242 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
243 ": Symbol `", strtab + refsym->st_name,
244 "' has different size in shared object, "
245 "consider re-linking\n", NULL);
247 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
248 refsym->st_size));
249 break;
250 case R_68K_GLOB_DAT:
251 case R_68K_JMP_SLOT:
252 *reloc_addr = value;
253 break;
254 case R_68K_8:
255 *(char *) reloc_addr = value + reloc->r_addend;
256 break;
257 case R_68K_16:
258 *(short *) reloc_addr = value + reloc->r_addend;
259 break;
260 case R_68K_32:
261 *reloc_addr = value + reloc->r_addend;
262 break;
263 case R_68K_PC8:
264 *(char *) reloc_addr
265 = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
266 break;
267 case R_68K_PC16:
268 *(short *) reloc_addr
269 = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
270 break;
271 case R_68K_PC32:
272 *reloc_addr = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
273 break;
274 case R_68K_NONE: /* Alright, Wilbur. */
275 break;
276 default:
277 assert (! "unexpected dynamic reloc type");
278 break;
283 static inline void
284 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
286 Elf32_Addr *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
287 switch (ELF32_R_TYPE (reloc->r_info))
289 case R_68K_JMP_SLOT:
290 *reloc_addr += map->l_addr;
291 break;
292 default:
293 assert (! "unexpected PLT reloc type");
294 break;
298 #endif /* RESOLVE */