2.5-18.1
[jlayton/glibc.git] / sysdeps / sh / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  SH version.
2    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #ifndef dl_machine_h
22 #define dl_machine_h
23
24 #define ELF_MACHINE_NAME "SH"
25
26 #include <sys/param.h>
27 #include <sysdep.h>
28 #include <assert.h>
29
30 /* Return nonzero iff ELF header is compatible with the running host.  */
31 static inline int __attribute__ ((unused))
32 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
33 {
34   return ehdr->e_machine == EM_SH;
35 }
36
37
38 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
39    first element of the GOT.  This must be inlined in a function which
40    uses global data.  */
41 static inline Elf32_Addr __attribute__ ((unused))
42 elf_machine_dynamic (void)
43 {
44   register Elf32_Addr *got;
45   asm ("mov r12,%0" :"=r" (got));
46   return *got;
47 }
48
49
50 /* Return the run-time load address of the shared object.  */
51 static inline Elf32_Addr __attribute__ ((unused))
52 elf_machine_load_address (void)
53 {
54   Elf32_Addr addr;
55   asm ("mov.l 1f,r0\n\
56         mov.l 3f,r2\n\
57         add r12,r2\n\
58         mov.l @(r0,r12),r0\n\
59         bra 2f\n\
60          sub r0,r2\n\
61         .align 2\n\
62         1: .long _dl_start@GOT\n\
63         3: .long _dl_start@GOTOFF\n\
64         2: mov r2,%0"
65        : "=r" (addr) : : "r0", "r1", "r2");
66   return addr;
67 }
68
69
70 /* Set up the loaded object described by L so its unrelocated PLT
71    entries will jump to the on-demand fixup code in dl-runtime.c.  */
72
73 static inline int __attribute__ ((unused, always_inline))
74 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
75 {
76   Elf32_Addr *got;
77   extern void _dl_runtime_resolve (Elf32_Word);
78   extern void _dl_runtime_profile (Elf32_Word);
79
80   if (l->l_info[DT_JMPREL] && lazy)
81     {
82       /* The GOT entries for functions in the PLT have not yet been filled
83          in.  Their initial contents will arrange when called to load an
84          offset into the .rela.plt section and _GLOBAL_OFFSET_TABLE_[1],
85          and then jump to _GLOBAL_OFFSET_TABLE[2].  */
86       got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
87       /* If a library is prelinked but we have to relocate anyway,
88          we have to be able to undo the prelinking of .got.plt.
89          The prelinker saved us here address of .plt + 36.  */
90       if (got[1])
91         {
92           l->l_mach.plt = got[1] + l->l_addr;
93           l->l_mach.gotplt = (Elf32_Addr) &got[3];
94         }
95       got[1] = (Elf32_Addr) l;  /* Identify this shared object.  */
96
97       /* The got[2] entry contains the address of a function which gets
98          called to get the address of a so far unresolved function and
99          jump to it.  The profiling extension of the dynamic linker allows
100          to intercept the calls to collect information.  In this case we
101          don't store the address in the GOT so that all future calls also
102          end in this function.  */
103       if (profile)
104         {
105           got[2] = (Elf32_Addr) &_dl_runtime_profile;
106           /* Say that we really want profiling and the timers are started.  */
107           if (GLRO(dl_profile) != NULL
108               && _dl_name_match_p (GLRO(dl_profile), l))
109             GL(dl_profile_map) = l;
110         }
111       else
112         /* This function will get called to fix up the GOT entry indicated by
113            the offset on the stack, and then jump to the resolved address.  */
114         got[2] = (Elf32_Addr) &_dl_runtime_resolve;
115     }
116   return lazy;
117 }
118
119 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
120
121 /* Mask identifying addresses reserved for the user program,
122    where the dynamic linker should not map anything.  */
123 #define ELF_MACHINE_USER_ADDRESS_MASK   0x80000000UL
124
125 /* Initial entry point code for the dynamic linker.
126    The C function `_dl_start' is the real entry point;
127    its return value is the user program's entry point.  */
128
129 #define RTLD_START asm ("\
130 .text\n\
131 .globl _start\n\
132 .globl _dl_start_user\n\
133 _start:\n\
134         mov r15,r4\n\
135         mov.l .L_dl_start,r1\n\
136         mova .L_dl_start,r0\n\
137         add r1,r0\n\
138         jsr @r0\n\
139          nop\n\
140 _dl_start_user:\n\
141         ! Save the user entry point address in r8.\n\
142         mov r0,r8\n\
143         ! Point r12 at the GOT.\n\
144         mov.l 1f,r12\n\
145         mova 1f,r0\n\
146         bra 2f\n\
147          add r0,r12\n\
148         .align 2\n\
149 1:      .long _GLOBAL_OFFSET_TABLE_\n\
150 2:      ! See if we were run as a command with the executable file\n\
151         ! name as an extra leading argument.\n\
152         mov.l .L_dl_skip_args,r0\n\
153         mov.l @(r0,r12),r0\n\
154         mov.l @r0,r0\n\
155         ! Get the original argument count.\n\
156         mov.l @r15,r5\n\
157         ! Subtract _dl_skip_args from it.\n\
158         sub r0,r5\n\
159         ! Adjust the stack pointer to skip _dl_skip_args words.\n\
160         shll2 r0\n\
161         add r0,r15\n\
162         ! Store back the modified argument count.\n\
163         mov.l r5,@r15\n\
164         ! Compute argv address and envp.\n\
165         mov r15,r6\n\
166         add #4,r6\n\
167         mov r5,r7\n\
168         shll2 r7\n\
169         add r15,r7\n\
170         add #8,r7\n\
171         mov.l .L_dl_loaded,r0\n\
172         mov.l @(r0,r12),r0\n\
173         mov.l @r0,r4\n\
174         ! Call _dl_init.\n\
175         mov.l .L_dl_init,r1\n\
176         mova .L_dl_init,r0\n\
177         add r1,r0\n\
178         jsr @r0\n\
179          nop\n\
180 1:      ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
181         mov.l .L_dl_fini,r0\n\
182         mov.l @(r0,r12),r4\n\
183         ! Jump to the user's entry point.\n\
184         jmp @r8\n\
185          nop\n\
186         .align 2\n\
187 .L_dl_start:\n\
188         .long _dl_start@PLT\n\
189 .L_dl_skip_args:\n\
190         .long _dl_skip_args@GOT\n\
191 .L_dl_init:\n\
192         .long _dl_init_internal@PLT\n\
193 .L_dl_loaded:\n\
194         .long _rtld_local@GOT\n\
195 .L_dl_fini:\n\
196         .long _dl_fini@GOT\n\
197         .type __fpscr_values,@object\n\
198         .global __fpscr_values\n\
199 __fpscr_values:\n\
200         .long   0\n\
201         .long   0x80000\n\
202         .weak __fpscr_values\n\
203 .previous\n\
204 ");
205
206 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
207    TLS variable, so undefined references should not be allowed to
208    define the value.
209    ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
210    of the main executable's symbols, as for a COPY reloc.  */
211 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
212 # define elf_machine_type_class(type) \
213   ((((type) == R_SH_JMP_SLOT || (type) == R_SH_TLS_DTPMOD32                   \
214      || (type) == R_SH_TLS_DTPOFF32 || (type) == R_SH_TLS_TPOFF32)            \
215     * ELF_RTYPE_CLASS_PLT)                                                    \
216    | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
217 #else
218 #define elf_machine_type_class(type) \
219   ((((type) == R_SH_JMP_SLOT) * ELF_RTYPE_CLASS_PLT)    \
220    | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
221 #endif
222
223 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.  */
224 #define ELF_MACHINE_JMP_SLOT    R_SH_JMP_SLOT
225
226 /* We define an initialization functions.  This is called very early in
227    _dl_sysdep_start.  */
228 #define DL_PLATFORM_INIT dl_platform_init ()
229
230 static inline void __attribute__ ((unused))
231 dl_platform_init (void)
232 {
233   if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
234     /* Avoid an empty string which would disturb us.  */
235     GLRO(dl_platform) = NULL;
236 }
237
238 static inline Elf32_Addr
239 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
240                        const Elf32_Rela *reloc,
241                        Elf32_Addr *reloc_addr, Elf32_Addr value)
242 {
243   return *reloc_addr = value;
244 }
245
246 /* Return the final value of a plt relocation.  */
247 static inline Elf32_Addr
248 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
249                        Elf32_Addr value)
250 {
251   return value + reloc->r_addend;
252 }
253
254 #define ARCH_LA_PLTENTER sh_gnu_pltenter
255 #define ARCH_LA_PLTEXIT sh_gnu_pltexit
256
257 #endif /* !dl_machine_h */
258
259 /* SH never uses Elf32_Rel relocations.  */
260 #define ELF_MACHINE_NO_REL 1
261
262 #ifdef RESOLVE_MAP
263
264 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
265    MAP is the object containing the reloc.  */
266
267 auto inline void
268 __attribute ((always_inline))
269 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
270                   const Elf32_Sym *sym, const struct r_found_version *version,
271                   void *const reloc_addr_arg)
272 {
273   Elf32_Addr *const reloc_addr = reloc_addr_arg;
274   const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
275   Elf32_Addr value;
276
277 #define COPY_UNALIGNED_WORD(swp, twp, align) \
278   { \
279     void *__s = (swp), *__t = (twp); \
280     unsigned char *__s1 = __s, *__t1 = __t; \
281     unsigned short *__s2 = __s, *__t2 = __t; \
282     unsigned long *__s4 = __s, *__t4 = __t; \
283     switch ((align)) \
284     { \
285     case 0: \
286       *__t4 = *__s4; \
287       break; \
288     case 2: \
289       *__t2++ = *__s2++; \
290       *__t2 = *__s2; \
291       break; \
292     default: \
293       *__t1++ = *__s1++; \
294       *__t1++ = *__s1++; \
295       *__t1++ = *__s1++; \
296       *__t1 = *__s1; \
297       break; \
298     } \
299   }
300
301   if (__builtin_expect (r_type == R_SH_RELATIVE, 0))
302     {
303 #ifndef RTLD_BOOTSTRAP
304       if (map != &GL(dl_rtld_map)) /* Already done in rtld itself.       */
305 #endif
306         {
307           if (reloc->r_addend)
308             value = map->l_addr + reloc->r_addend;
309           else
310             {
311               COPY_UNALIGNED_WORD (reloc_addr_arg, &value,
312                                    (int) reloc_addr_arg & 3);
313               value += map->l_addr;
314             }
315           COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
316                                (int) reloc_addr_arg & 3);
317         }
318     }
319 #ifndef RTLD_BOOTSTRAP
320   else if (__builtin_expect (r_type == R_SH_NONE, 0))
321     return;
322 #endif
323   else
324     {
325       const Elf32_Sym *const refsym = sym;
326       struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
327
328       value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
329       value += reloc->r_addend;
330
331       switch (r_type)
332         {
333         case R_SH_COPY:
334           if (sym == NULL)
335             /* This can happen in trace mode if an object could not be
336                found.  */
337             break;
338           if (sym->st_size > refsym->st_size
339               || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
340             {
341               const char *strtab;
342
343               strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
344               _dl_error_printf ("\
345 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
346                                 rtld_progname ?: "<program name unknown>",
347                                 strtab + refsym->st_name);
348             }
349           memcpy (reloc_addr_arg, (void *) value,
350                   MIN (sym->st_size, refsym->st_size));
351           break;
352         case R_SH_GLOB_DAT:
353         case R_SH_JMP_SLOT:
354           /* These addresses are always aligned.  */
355           *reloc_addr = value;
356           break;
357 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
358           /* XXX Remove TLS relocations which are not needed.  */
359         case R_SH_TLS_DTPMOD32:
360 # ifdef RTLD_BOOTSTRAP
361           /* During startup the dynamic linker is always the module
362              with index 1.
363              XXX If this relocation is necessary move before RESOLVE
364              call.  */
365           *reloc_addr = 1;
366 # else
367           /* Get the information from the link map returned by the
368              resolv function.  */
369           if (sym_map != NULL)
370             *reloc_addr = sym_map->l_tls_modid;
371 # endif
372           break;
373         case R_SH_TLS_DTPOFF32:
374 # ifndef RTLD_BOOTSTRAP
375           /* During relocation all TLS symbols are defined and used.
376              Therefore the offset is already correct.  */
377           if (sym != NULL)
378             *reloc_addr = sym->st_value;
379 # endif
380           break;
381         case R_SH_TLS_TPOFF32:
382           /* The offset is positive, afterward from the thread pointer.  */
383 # ifdef RTLD_BOOTSTRAP
384           *reloc_addr = map->l_tls_offset + sym->st_value + reloc->r_addend;
385 # else
386           /* We know the offset of object the symbol is contained in.
387              It is a positive value which will be added to the thread
388              pointer.  To get the variable position in the TLS block
389              we add the offset from that of the TLS block.  */
390           if (sym != NULL)
391             {
392               CHECK_STATIC_TLS (map, sym_map);
393               *reloc_addr = sym_map->l_tls_offset + sym->st_value
394                             + reloc->r_addend;
395             }
396 # endif
397           break;
398 #endif  /* use TLS */
399         case R_SH_DIR32:
400           {
401 #ifndef RTLD_BOOTSTRAP
402            /* This is defined in rtld.c, but nowhere in the static
403               libc.a; make the reference weak so static programs can
404               still link.  This declaration cannot be done when
405               compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
406               rtld.c contains the common defn for _dl_rtld_map, which
407               is incompatible with a weak decl in the same file.  */
408 # ifndef SHARED
409             weak_extern (_dl_rtld_map);
410 # endif
411             if (map == &GL(dl_rtld_map))
412               /* Undo the relocation done here during bootstrapping.
413                  Now we will relocate it anew, possibly using a
414                  binding found in the user program or a loaded library
415                  rather than the dynamic linker's built-in definitions
416                  used while loading those libraries.  */
417               value -= map->l_addr + refsym->st_value + reloc->r_addend;
418 #endif
419             COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
420                                  (int) reloc_addr_arg & 3);
421             break;
422           }
423         case R_SH_REL32:
424           value = (value - (Elf32_Addr) reloc_addr);
425           COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
426                                (int) reloc_addr_arg & 3);
427           break;
428         default:
429           _dl_reloc_bad_type (map, r_type, 0);
430           break;
431         }
432     }
433 }
434
435 auto inline void
436 __attribute__ ((always_inline))
437 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
438                            void *const reloc_addr_arg)
439 {
440   Elf32_Addr value;
441
442   if (reloc->r_addend)
443     value = l_addr + reloc->r_addend;
444   else
445     {
446       COPY_UNALIGNED_WORD (reloc_addr_arg, &value, (int) reloc_addr_arg & 3);
447       value += l_addr;
448     }
449   COPY_UNALIGNED_WORD (&value, reloc_addr_arg, (int) reloc_addr_arg & 3);
450
451 #undef COPY_UNALIGNED_WORD
452 }
453
454 auto inline void
455 __attribute__ ((always_inline))
456 elf_machine_lazy_rel (struct link_map *map,
457                       Elf32_Addr l_addr, const Elf32_Rela *reloc)
458 {
459   Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
460   /* Check for unexpected PLT reloc type.  */
461   if (ELF32_R_TYPE (reloc->r_info) == R_SH_JMP_SLOT)
462     {
463       if (__builtin_expect (map->l_mach.plt, 0) == 0)
464         *reloc_addr += l_addr;
465       else
466         *reloc_addr =
467           map->l_mach.plt
468           + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 7;
469     }
470   else
471     _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
472 }
473
474 #endif /* RESOLVE_MAP */