(yn_test): Expect invalid exception for negative arguments. (y0_test): Likewise....
[glibc.git] / elf / dynamic-link.h
blob0179de3273ddaa7fda64388ad365e3732807a367
1 /* Inline functions for dynamic linking.
2 Copyright (C) 1995-2002, 2003 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <elf.h>
21 #include <assert.h>
23 #ifdef RESOLVE
24 /* We pass reloc_addr as a pointer to void, as opposed to a pointer to
25 ElfW(Addr), because not all architectures can assume that the
26 relocated address is properly aligned, whereas the compiler is
27 entitled to assume that a pointer to a type is properly aligned for
28 the type. Even if we cast the pointer back to some other type with
29 less strict alignment requirements, the compiler might still
30 remember that the pointer was originally more aligned, thereby
31 optimizing away alignment tests or using word instructions for
32 copying memory, breaking the very code written to handle the
33 unaligned cases. */
34 auto void __attribute__((always_inline))
35 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
36 const ElfW(Sym) *sym, const struct r_found_version *version,
37 void *const reloc_addr);
38 auto void __attribute__((always_inline))
39 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
40 const ElfW(Sym) *sym, const struct r_found_version *version,
41 void *const reloc_addr);
42 auto void __attribute__((always_inline))
43 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
44 void *const reloc_addr);
45 auto void __attribute__((always_inline))
46 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
47 void *const reloc_addr);
48 # if ELF_MACHINE_NO_RELA || defined ELF_MACHINE_PLT_REL
49 auto void __attribute__((always_inline))
50 elf_machine_lazy_rel (struct link_map *map,
51 ElfW(Addr) l_addr, const ElfW(Rel) *reloc);
52 # else
53 auto void __attribute__((always_inline))
54 elf_machine_lazy_rel (struct link_map *map,
55 ElfW(Addr) l_addr, const ElfW(Rela) *reloc);
56 # endif
57 #endif
59 #include <dl-machine.h>
61 #ifndef VERSYMIDX
62 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
63 #endif
66 /* Read the dynamic section at DYN and fill in INFO with indices DT_*. */
68 static inline void __attribute__ ((unused, always_inline))
69 elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
71 ElfW(Dyn) *dyn = l->l_ld;
72 ElfW(Dyn) **info;
74 #ifndef RTLD_BOOTSTRAP
75 if (dyn == NULL)
76 return;
77 #endif
79 info = l->l_info;
81 while (dyn->d_tag != DT_NULL)
83 if (dyn->d_tag < DT_NUM)
84 info[dyn->d_tag] = dyn;
85 else if (dyn->d_tag >= DT_LOPROC &&
86 dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
87 info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
88 else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
89 info[VERSYMIDX (dyn->d_tag)] = dyn;
90 else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
91 info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
92 + DT_VERSIONTAGNUM] = dyn;
93 else if ((Elf32_Word) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
94 info[DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
95 + DT_VERSIONTAGNUM + DT_EXTRANUM] = dyn;
96 else if ((Elf32_Word) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
97 info[DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
98 + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] = dyn;
99 ++dyn;
102 #define DL_RO_DYN_TEMP_CNT 8
104 #ifndef DL_RO_DYN_SECTION
105 /* Don't adjust .dynamic unnecessarily. */
106 if (l->l_addr != 0)
108 ElfW(Addr) l_addr = l->l_addr;
109 int cnt = 0;
111 # define ADJUST_DYN_INFO(tag) \
112 do \
113 if (info[tag] != NULL) \
115 if (temp) \
117 temp[cnt].d_tag = info[tag]->d_tag; \
118 temp[cnt].d_un.d_ptr = info[tag]->d_un.d_ptr + l_addr; \
119 info[tag] = temp + cnt++; \
121 else \
122 info[tag]->d_un.d_ptr += l_addr; \
124 while (0)
126 ADJUST_DYN_INFO (DT_HASH);
127 ADJUST_DYN_INFO (DT_PLTGOT);
128 ADJUST_DYN_INFO (DT_STRTAB);
129 ADJUST_DYN_INFO (DT_SYMTAB);
130 # if ! ELF_MACHINE_NO_RELA
131 ADJUST_DYN_INFO (DT_RELA);
132 # endif
133 # if ! ELF_MACHINE_NO_REL
134 ADJUST_DYN_INFO (DT_REL);
135 # endif
136 ADJUST_DYN_INFO (DT_JMPREL);
137 ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
138 # undef ADJUST_DYN_INFO
139 assert (cnt <= DL_RO_DYN_TEMP_CNT);
141 #endif
142 if (info[DT_PLTREL] != NULL)
144 #if ELF_MACHINE_NO_RELA
145 assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
146 #elif ELF_MACHINE_NO_REL
147 assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
148 #else
149 assert (info[DT_PLTREL]->d_un.d_val == DT_REL
150 || info[DT_PLTREL]->d_un.d_val == DT_RELA);
151 #endif
153 #if ! ELF_MACHINE_NO_RELA
154 if (info[DT_RELA] != NULL)
155 assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
156 # endif
157 # if ! ELF_MACHINE_NO_REL
158 if (info[DT_REL] != NULL)
159 assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
160 #endif
161 if (info[DT_FLAGS] != NULL)
163 /* Flags are used. Translate to the old form where available.
164 Since these l_info entries are only tested for NULL pointers it
165 is ok if they point to the DT_FLAGS entry. */
166 l->l_flags = info[DT_FLAGS]->d_un.d_val;
167 #ifdef RTLD_BOOTSTRAP
168 /* These three flags must not be set for ld.so. */
169 assert ((l->l_flags & (DF_SYMBOLIC | DF_TEXTREL | DF_BIND_NOW)) == 0);
170 #else
171 if (l->l_flags & DF_SYMBOLIC)
172 info[DT_SYMBOLIC] = info[DT_FLAGS];
173 if (l->l_flags & DF_TEXTREL)
174 info[DT_TEXTREL] = info[DT_FLAGS];
175 if (l->l_flags & DF_BIND_NOW)
176 info[DT_BIND_NOW] = info[DT_FLAGS];
177 #endif
179 if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
180 l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
181 #ifdef RTLD_BOOTSTRAP
182 /* The dynamic linker should have none of these set. */
183 assert (info[DT_RUNPATH] == NULL);
184 assert (info[DT_RPATH] == NULL);
185 #else
186 if (info[DT_RUNPATH] != NULL)
187 /* If both RUNPATH and RPATH are given, the latter is ignored. */
188 info[DT_RPATH] = NULL;
189 #endif
192 #ifdef RESOLVE
194 # ifdef RTLD_BOOTSTRAP
195 # define ELF_DURING_STARTUP (1)
196 # else
197 # define ELF_DURING_STARTUP (0)
198 # endif
200 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
201 These functions are almost identical, so we use cpp magic to avoid
202 duplicating their code. It cannot be done in a more general function
203 because we must be able to completely inline. */
205 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
206 range. Note that according to the ELF spec, this is completely legal!
207 But conditionally define things so that on machines we know this will
208 not happen we do something more optimal. */
210 # ifdef ELF_MACHINE_PLTREL_OVERLAP
211 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
212 do { \
213 struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \
214 int ranges_index; \
216 ranges[0].lazy = ranges[2].lazy = 0; \
217 ranges[1].lazy = 1; \
218 ranges[0].size = ranges[1].size = ranges[2].size = 0; \
220 if ((map)->l_info[DT_##RELOC]) \
222 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
223 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
226 if ((do_lazy) \
227 && (map)->l_info[DT_PLTREL] \
228 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
230 ranges[1].start = D_PTR ((map), l_info[DT_JMPREL]); \
231 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
232 ranges[2].start = ranges[1].start + ranges[1].size; \
233 ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \
234 ranges[0].size = ranges[1].start - ranges[0].start; \
237 for (ranges_index = 0; ranges_index < 3; ++ranges_index) \
238 elf_dynamic_do_##reloc ((map), \
239 ranges[ranges_index].start, \
240 ranges[ranges_index].size, \
241 ranges[ranges_index].lazy); \
242 } while (0)
243 # else
244 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
245 do { \
246 struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \
247 ranges[0].lazy = 0; \
248 ranges[0].size = ranges[1].size = 0; \
249 ranges[0].start = 0; \
251 if ((map)->l_info[DT_##RELOC]) \
253 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
254 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
256 if ((map)->l_info[DT_PLTREL] \
257 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
259 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \
261 if (! ELF_DURING_STARTUP \
262 && ((do_lazy) \
263 /* This test does not only detect whether the relocation \
264 sections are in the right order, it also checks whether \
265 there is a DT_REL/DT_RELA section. */ \
266 || ranges[0].start + ranges[0].size != start)) \
268 ranges[1].start = start; \
269 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
270 ranges[1].lazy = (do_lazy); \
272 else \
274 /* Combine processing the sections. */ \
275 assert (ranges[0].start + ranges[0].size == start); \
276 ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
280 if (ELF_DURING_STARTUP) \
281 elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size, 0); \
282 else \
284 int ranges_index; \
285 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
286 elf_dynamic_do_##reloc ((map), \
287 ranges[ranges_index].start, \
288 ranges[ranges_index].size, \
289 ranges[ranges_index].lazy); \
291 } while (0)
292 # endif
294 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
295 # define _ELF_CHECK_REL 0
296 # else
297 # define _ELF_CHECK_REL 1
298 # endif
300 # if ! ELF_MACHINE_NO_REL
301 # include "do-rel.h"
302 # define ELF_DYNAMIC_DO_REL(map, lazy) \
303 _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
304 # else
305 # define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */
306 # endif
308 # if ! ELF_MACHINE_NO_RELA
309 # define DO_RELA
310 # include "do-rel.h"
311 # define ELF_DYNAMIC_DO_RELA(map, lazy) \
312 _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
313 # else
314 # define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */
315 # endif
317 /* This can't just be an inline function because GCC is too dumb
318 to inline functions containing inlines themselves. */
319 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
320 do { \
321 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
322 (consider_profile)); \
323 ELF_DYNAMIC_DO_REL ((map), edr_lazy); \
324 ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \
325 } while (0)
327 #endif