(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / elf / dynamic-link.h
blobf9559dc59c203133607bdce55ca8e99c1cb5bb15
1 /* Inline functions for dynamic linking.
2 Copyright (C) 1995-2002, 2003, 2004 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_*. */
67 #ifndef RESOLVE
68 static
69 #else
70 auto
71 #endif
72 inline void __attribute__ ((unused, always_inline))
73 elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
75 ElfW(Dyn) *dyn = l->l_ld;
76 ElfW(Dyn) **info;
78 #ifndef RTLD_BOOTSTRAP
79 if (dyn == NULL)
80 return;
81 #endif
83 info = l->l_info;
85 while (dyn->d_tag != DT_NULL)
87 if (dyn->d_tag < DT_NUM)
88 info[dyn->d_tag] = dyn;
89 else if (dyn->d_tag >= DT_LOPROC &&
90 dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
91 info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
92 else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
93 info[VERSYMIDX (dyn->d_tag)] = dyn;
94 else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
95 info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
96 + DT_VERSIONTAGNUM] = dyn;
97 else if ((Elf32_Word) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
98 info[DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
99 + DT_VERSIONTAGNUM + DT_EXTRANUM] = dyn;
100 else if ((Elf32_Word) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
101 info[DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
102 + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] = dyn;
103 ++dyn;
106 #define DL_RO_DYN_TEMP_CNT 8
108 #ifndef DL_RO_DYN_SECTION
109 /* Don't adjust .dynamic unnecessarily. */
110 if (l->l_addr != 0)
112 ElfW(Addr) l_addr = l->l_addr;
113 int cnt = 0;
115 # define ADJUST_DYN_INFO(tag) \
116 do \
117 if (info[tag] != NULL) \
119 if (temp) \
121 temp[cnt].d_tag = info[tag]->d_tag; \
122 temp[cnt].d_un.d_ptr = info[tag]->d_un.d_ptr + l_addr; \
123 info[tag] = temp + cnt++; \
125 else \
126 info[tag]->d_un.d_ptr += l_addr; \
128 while (0)
130 ADJUST_DYN_INFO (DT_HASH);
131 ADJUST_DYN_INFO (DT_PLTGOT);
132 ADJUST_DYN_INFO (DT_STRTAB);
133 ADJUST_DYN_INFO (DT_SYMTAB);
134 # if ! ELF_MACHINE_NO_RELA
135 ADJUST_DYN_INFO (DT_RELA);
136 # endif
137 # if ! ELF_MACHINE_NO_REL
138 ADJUST_DYN_INFO (DT_REL);
139 # endif
140 ADJUST_DYN_INFO (DT_JMPREL);
141 ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
142 # undef ADJUST_DYN_INFO
143 assert (cnt <= DL_RO_DYN_TEMP_CNT);
145 #endif
146 if (info[DT_PLTREL] != NULL)
148 #if ELF_MACHINE_NO_RELA
149 assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
150 #elif ELF_MACHINE_NO_REL
151 assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
152 #else
153 assert (info[DT_PLTREL]->d_un.d_val == DT_REL
154 || info[DT_PLTREL]->d_un.d_val == DT_RELA);
155 #endif
157 #if ! ELF_MACHINE_NO_RELA
158 if (info[DT_RELA] != NULL)
159 assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
160 # endif
161 # if ! ELF_MACHINE_NO_REL
162 if (info[DT_REL] != NULL)
163 assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
164 #endif
165 #ifdef RTLD_BOOTSTRAP
166 /* Only the bind now flags are allowed. */
167 assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
168 || info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val == DF_1_NOW);
169 assert (info[DT_FLAGS] == NULL
170 || info[DT_FLAGS]->d_un.d_val == DF_BIND_NOW);
171 /* Flags must not be set for ld.so. */
172 assert (info[DT_RUNPATH] == NULL);
173 assert (info[DT_RPATH] == NULL);
174 #else
175 if (info[DT_FLAGS] != NULL)
177 /* Flags are used. Translate to the old form where available.
178 Since these l_info entries are only tested for NULL pointers it
179 is ok if they point to the DT_FLAGS entry. */
180 l->l_flags = info[DT_FLAGS]->d_un.d_val;
182 if (l->l_flags & DF_SYMBOLIC)
183 info[DT_SYMBOLIC] = info[DT_FLAGS];
184 if (l->l_flags & DF_TEXTREL)
185 info[DT_TEXTREL] = info[DT_FLAGS];
186 if (l->l_flags & DF_BIND_NOW)
187 info[DT_BIND_NOW] = info[DT_FLAGS];
189 if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
191 l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
193 if (l->l_flags_1 & DF_1_NOW)
194 info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)];
196 if (info[DT_RUNPATH] != NULL)
197 /* If both RUNPATH and RPATH are given, the latter is ignored. */
198 info[DT_RPATH] = NULL;
199 #endif
202 #ifdef RESOLVE
204 # ifdef RTLD_BOOTSTRAP
205 # define ELF_DURING_STARTUP (1)
206 # else
207 # define ELF_DURING_STARTUP (0)
208 # endif
210 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
211 These functions are almost identical, so we use cpp magic to avoid
212 duplicating their code. It cannot be done in a more general function
213 because we must be able to completely inline. */
215 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
216 range. Note that according to the ELF spec, this is completely legal!
217 But conditionally define things so that on machines we know this will
218 not happen we do something more optimal. */
220 # ifdef ELF_MACHINE_PLTREL_OVERLAP
221 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
222 do { \
223 struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \
224 int ranges_index; \
226 ranges[0].lazy = ranges[2].lazy = 0; \
227 ranges[1].lazy = 1; \
228 ranges[0].size = ranges[1].size = ranges[2].size = 0; \
230 if ((map)->l_info[DT_##RELOC]) \
232 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
233 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
236 if ((do_lazy) \
237 && (map)->l_info[DT_PLTREL] \
238 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
240 ranges[1].start = D_PTR ((map), l_info[DT_JMPREL]); \
241 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
242 ranges[2].start = ranges[1].start + ranges[1].size; \
243 ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \
244 ranges[0].size = ranges[1].start - ranges[0].start; \
247 for (ranges_index = 0; ranges_index < 3; ++ranges_index) \
248 elf_dynamic_do_##reloc ((map), \
249 ranges[ranges_index].start, \
250 ranges[ranges_index].size, \
251 ranges[ranges_index].lazy); \
252 } while (0)
253 # else
254 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
255 do { \
256 struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \
257 ranges[0].lazy = 0; \
258 ranges[0].size = ranges[1].size = 0; \
259 ranges[0].start = 0; \
261 if ((map)->l_info[DT_##RELOC]) \
263 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
264 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
266 if ((map)->l_info[DT_PLTREL] \
267 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
269 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \
271 if (! ELF_DURING_STARTUP \
272 && ((do_lazy) \
273 /* This test does not only detect whether the relocation \
274 sections are in the right order, it also checks whether \
275 there is a DT_REL/DT_RELA section. */ \
276 || ranges[0].start + ranges[0].size != start)) \
278 ranges[1].start = start; \
279 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
280 ranges[1].lazy = (do_lazy); \
282 else \
284 /* Combine processing the sections. */ \
285 assert (ranges[0].start + ranges[0].size == start); \
286 ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
290 if (ELF_DURING_STARTUP) \
291 elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size, 0); \
292 else \
294 int ranges_index; \
295 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
296 elf_dynamic_do_##reloc ((map), \
297 ranges[ranges_index].start, \
298 ranges[ranges_index].size, \
299 ranges[ranges_index].lazy); \
301 } while (0)
302 # endif
304 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
305 # define _ELF_CHECK_REL 0
306 # else
307 # define _ELF_CHECK_REL 1
308 # endif
310 # if ! ELF_MACHINE_NO_REL
311 # include "do-rel.h"
312 # define ELF_DYNAMIC_DO_REL(map, lazy) \
313 _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
314 # else
315 # define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */
316 # endif
318 # if ! ELF_MACHINE_NO_RELA
319 # define DO_RELA
320 # include "do-rel.h"
321 # define ELF_DYNAMIC_DO_RELA(map, lazy) \
322 _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
323 # else
324 # define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */
325 # endif
327 /* This can't just be an inline function because GCC is too dumb
328 to inline functions containing inlines themselves. */
329 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
330 do { \
331 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
332 (consider_profile)); \
333 ELF_DYNAMIC_DO_REL ((map), edr_lazy); \
334 ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \
335 } while (0)
337 #endif