OSDN Git Service

07b9b79e004181f6d22998922ab01358387174f7
[uclinux-h8/uClibc.git] / ldso / ldso / xtensa / dl-sysdep.h
1 /* Machine-dependent ELF dynamic relocation.
2    Parts copied from glibc/sysdeps/xtensa/dl-machine.h
3    Copyright (C) 2001, 2007 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., 51 Franklin Street - Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
20
21 /* Define this if the system uses RELOCA.  */
22 #define ELF_USES_RELOCA
23 #include <elf.h>
24 #include <link.h>
25
26 /* Translate a processor specific dynamic tag to the index
27    in l_info array.  */
28 #define DT_XTENSA(x) (DT_XTENSA_##x - DT_LOPROC + DT_NUM + OS_NUM)
29
30 typedef struct xtensa_got_location_struct {
31   Elf32_Off offset;
32   Elf32_Word length;
33 } xtensa_got_location;
34
35 /* Initialization sequence for the GOT.  */
36 #define INIT_GOT(GOT_BASE, MODULE) \
37   do {                                                                        \
38     xtensa_got_location *got_loc;                                             \
39     Elf32_Addr l_addr = MODULE->loadaddr;                                     \
40     int x;                                                                    \
41                                                                               \
42     got_loc = (xtensa_got_location *)                                         \
43       (MODULE->dynamic_info[DT_XTENSA (GOT_LOC_OFF)] + l_addr);               \
44                                                                               \
45     for (x = 0; x < MODULE->dynamic_info[DT_XTENSA (GOT_LOC_SZ)]; x++)        \
46       {                                                                       \
47         Elf32_Addr got_start, got_end;                                        \
48         got_start = got_loc[x].offset & ~(PAGE_SIZE - 1);                     \
49         got_end = ((got_loc[x].offset + got_loc[x].length + PAGE_SIZE - 1)    \
50                    & ~(PAGE_SIZE - 1));                                       \
51         _dl_mprotect ((void *)(got_start + l_addr) , got_end - got_start,     \
52                       PROT_READ | PROT_WRITE | PROT_EXEC);                    \
53       }                                                                       \
54                                                                               \
55     /* Fill in first GOT entry according to the ABI.  */                      \
56     GOT_BASE[0] = (unsigned long) _dl_linux_resolve;                          \
57   } while (0)
58
59 /* Parse dynamic info */
60 #define ARCH_NUM 2
61 #define ARCH_DYNAMIC_INFO(dpnt, dynamic, debug_addr) \
62   do {                                                                  \
63     if (dpnt->d_tag == DT_XTENSA_GOT_LOC_OFF)                           \
64       dynamic[DT_XTENSA (GOT_LOC_OFF)] = dpnt->d_un.d_ptr;              \
65     else if (dpnt->d_tag == DT_XTENSA_GOT_LOC_SZ)                       \
66       dynamic[DT_XTENSA (GOT_LOC_SZ)] = dpnt->d_un.d_val;               \
67   } while (0)
68
69 /* Here we define the magic numbers that this dynamic loader should accept. */
70 #define MAGIC1 EM_XTENSA
71 #undef  MAGIC2
72
73 /* Used for error messages. */
74 #define ELF_TARGET "Xtensa"
75
76 /* Need bootstrap relocations */
77 #define ARCH_NEEDS_BOOTSTRAP_RELOCS
78
79 struct elf_resolve;
80 extern unsigned long _dl_linux_resolver (struct elf_resolve *, int);
81
82 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
83    undefined references should not be allowed to define the value.  */
84 #define elf_machine_type_class(type) \
85   (((type) == R_XTENSA_JMP_SLOT) * ELF_RTYPE_CLASS_PLT)
86
87 /* Return the link-time address of _DYNAMIC.  */
88 static __always_inline Elf32_Addr
89 elf_machine_dynamic (void)
90 {
91   /* This function is only used while bootstrapping the runtime linker.
92      The "_DYNAMIC" symbol is always local so its GOT entry will initially
93      contain the link-time address.  */
94   return (Elf32_Addr) &_DYNAMIC;
95 }
96
97 /* Return the run-time load address of the shared object.  */
98 static __always_inline Elf32_Addr
99 elf_machine_load_address (void)
100 {
101   Elf32_Addr addr, tmp;
102
103   /* At this point, the runtime linker is being bootstrapped and the GOT
104      entry used for ".Lhere" will contain the link address.  The CALL0 will
105      produce the dynamic address of ".Lhere" + 3.  Thus, the end result is
106      equal to "dynamic_address(.Lhere) - link_address(.Lhere)".  */
107   __asm__ ("\
108         movi    %0, .Lhere\n\
109         mov     %1, a0\n\
110 .Lhere: _call0  0f\n\
111         .align  4\n\
112 0:      sub     %0, a0, %0\n\
113         mov     a0, %1"
114            : "=a" (addr), "=a" (tmp));
115
116   return addr - 3;
117 }
118
119 static __always_inline void
120 elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
121                       Elf32_Word relative_count)
122 {
123   Elf32_Rela *rpnt = (Elf32_Rela *) rel_addr;
124   while (relative_count--)
125     {
126       Elf32_Addr *const reloc_addr = (Elf32_Addr *) (load_off + rpnt->r_offset);
127       *reloc_addr += load_off + rpnt->r_addend;
128       rpnt++;
129     }
130 }