OSDN Git Service

merge upstream changes
[uclinux-h8/uclibc-ng.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, see
18    <http://www.gnu.org/licenses/>.  */
19
20 /* Define this if the system uses RELOCA.  */
21 #define ELF_USES_RELOCA
22 #include <elf.h>
23 #include <link.h>
24
25 /* Translate a processor specific dynamic tag to the index
26    in l_info array.  */
27 #define DT_XTENSA(x) (DT_XTENSA_##x - DT_LOPROC + DT_NUM + OS_NUM)
28
29 typedef struct xtensa_got_location_struct {
30   Elf32_Off offset;
31   Elf32_Word length;
32 } xtensa_got_location;
33
34 /* Initialization sequence for the GOT.  */
35 #define INIT_GOT(GOT_BASE, MODULE) \
36   do {                                                                        \
37     xtensa_got_location *got_loc;                                             \
38     Elf32_Addr l_addr = MODULE->loadaddr;                                     \
39     Elf32_Addr prev_got_start = 0, prev_got_end = 0;                          \
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         if (got_end >= prev_got_start && got_start <= prev_got_end)           \
52           {                                                                   \
53             if (got_end > prev_got_end)                                       \
54                 prev_got_end = got_end;                                       \
55             if (got_start < prev_got_start)                                   \
56                 prev_got_start = got_start;                                   \
57             continue;                                                         \
58           }                                                                   \
59         else if (prev_got_start != prev_got_end)                              \
60           {                                                                   \
61             _dl_mprotect ((void *)(prev_got_start + l_addr),                  \
62                           prev_got_end - prev_got_start,                      \
63                           PROT_READ | PROT_WRITE | PROT_EXEC);                \
64           }                                                                   \
65         prev_got_start = got_start;                                           \
66         prev_got_end = got_end;                                               \
67       }                                                                       \
68                                                                               \
69     if (prev_got_start != prev_got_end)                                       \
70       {                                                                       \
71         _dl_mprotect ((void *)(prev_got_start + l_addr),                      \
72                       prev_got_end - prev_got_start,                          \
73                       PROT_READ | PROT_WRITE | PROT_EXEC);                    \
74       }                                                                       \
75                                                                               \
76     /* Fill in first GOT entry according to the ABI.  */                      \
77     GOT_BASE[0] = (unsigned long) _dl_linux_resolve;                          \
78   } while (0)
79
80 /* Parse dynamic info */
81 #define ARCH_NUM 2
82 #define ARCH_DYNAMIC_INFO(dpnt, dynamic, debug_addr) \
83   do {                                                                  \
84     if (dpnt->d_tag == DT_XTENSA_GOT_LOC_OFF)                           \
85       dynamic[DT_XTENSA (GOT_LOC_OFF)] = dpnt->d_un.d_ptr;              \
86     else if (dpnt->d_tag == DT_XTENSA_GOT_LOC_SZ)                       \
87       dynamic[DT_XTENSA (GOT_LOC_SZ)] = dpnt->d_un.d_val;               \
88   } while (0)
89
90 /* Here we define the magic numbers that this dynamic loader should accept. */
91 #define MAGIC1 EM_XTENSA
92 #undef  MAGIC2
93
94 /* Used for error messages. */
95 #define ELF_TARGET "Xtensa"
96
97 /* Need bootstrap relocations */
98 #define ARCH_NEEDS_BOOTSTRAP_RELOCS
99
100 struct elf_resolve;
101 extern unsigned long _dl_linux_resolver (struct elf_resolve *, int);
102
103 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
104    TLS variable, so undefined references should not be allowed to define
105    the value.  */
106 #define elf_machine_type_class(type) \
107   (((type) == R_XTENSA_JMP_SLOT || (type) == R_XTENSA_TLS_TPOFF \
108    || (type) == R_XTENSA_TLSDESC_FN || (type) == R_XTENSA_TLSDESC_ARG) \
109    * ELF_RTYPE_CLASS_PLT)
110
111 /* Return the link-time address of _DYNAMIC.  */
112 static __always_inline Elf32_Addr
113 elf_machine_dynamic (void)
114 {
115   /* This function is only used while bootstrapping the runtime linker.
116      The "_DYNAMIC" symbol is always local so its GOT entry will initially
117      contain the link-time address.  */
118   return (Elf32_Addr) &_DYNAMIC;
119 }
120
121 /* Return the run-time load address of the shared object.  */
122 static __always_inline Elf32_Addr
123 elf_machine_load_address (void)
124 {
125   Elf32_Addr addr, tmp;
126
127   /* At this point, the runtime linker is being bootstrapped and the GOT
128      entry used for ".Lhere" will contain the link address.  The CALL0 will
129      produce the dynamic address of ".Lhere" + 3.  Thus, the end result is
130      equal to "dynamic_address(.Lhere) - link_address(.Lhere)".  */
131   __asm__ ("\
132         movi    %0, .Lhere\n\
133         mov     %1, a0\n\
134 .Lhere: _call0  0f\n\
135         .align  4\n\
136 0:      sub     %0, a0, %0\n\
137         mov     a0, %1"
138            : "=a" (addr), "=a" (tmp));
139
140   return addr - 3;
141 }
142
143 static __always_inline void
144 elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
145                       Elf32_Word relative_count)
146 {
147   Elf32_Rela *rpnt = (Elf32_Rela *) rel_addr;
148   while (relative_count--)
149     {
150       Elf32_Addr *const reloc_addr = (Elf32_Addr *) (load_off + rpnt->r_offset);
151       *reloc_addr += load_off + rpnt->r_addend;
152       rpnt++;
153     }
154 }