Updated to fedora-glibc-20050520T0919
[glibc.git] / elf / dynamic-link.h
blobcf0d223c45c0521e33bbf6a253ea97b260dd6b85
1 /* Inline functions for dynamic linking.
2 Copyright (C) 1995-2002, 2003, 2004, 2005 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_MAP
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 # if ! ELF_MACHINE_NO_REL
35 auto inline void __attribute__((always_inline))
36 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
37 const ElfW(Sym) *sym, const struct r_found_version *version,
38 void *const reloc_addr);
39 auto inline void __attribute__((always_inline))
40 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
41 void *const reloc_addr);
42 # endif
43 # if ! ELF_MACHINE_NO_RELA
44 auto inline void __attribute__((always_inline))
45 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
46 const ElfW(Sym) *sym, const struct r_found_version *version,
47 void *const reloc_addr);
48 auto inline void __attribute__((always_inline))
49 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
50 void *const reloc_addr);
51 # endif
52 # if ELF_MACHINE_NO_RELA || defined ELF_MACHINE_PLT_REL
53 auto inline void __attribute__((always_inline))
54 elf_machine_lazy_rel (struct link_map *map,
55 ElfW(Addr) l_addr, const ElfW(Rel) *reloc);
56 # else
57 auto inline void __attribute__((always_inline))
58 elf_machine_lazy_rel (struct link_map *map,
59 ElfW(Addr) l_addr, const ElfW(Rela) *reloc);
60 # endif
61 #endif
63 #include <dl-machine.h>
65 #ifndef VERSYMIDX
66 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
67 #endif
70 /* Read the dynamic section at DYN and fill in INFO with indices DT_*. */
71 #ifndef RESOLVE_MAP
72 static
73 #else
74 auto
75 #endif
76 inline void __attribute__ ((unused, always_inline))
77 elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
79 ElfW(Dyn) *dyn = l->l_ld;
80 ElfW(Dyn) **info;
82 #ifndef RTLD_BOOTSTRAP
83 if (dyn == NULL)
84 return;
85 #endif
87 info = l->l_info;
89 while (dyn->d_tag != DT_NULL)
91 if (dyn->d_tag < DT_NUM)
92 info[dyn->d_tag] = dyn;
93 else if (dyn->d_tag >= DT_LOPROC &&
94 dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
95 info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
96 else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
97 info[VERSYMIDX (dyn->d_tag)] = dyn;
98 else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
99 info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
100 + DT_VERSIONTAGNUM] = dyn;
101 else if ((Elf32_Word) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
102 info[DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
103 + DT_VERSIONTAGNUM + DT_EXTRANUM] = dyn;
104 else if ((Elf32_Word) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
105 info[DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
106 + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] = dyn;
107 ++dyn;
110 #define DL_RO_DYN_TEMP_CNT 8
112 #ifndef DL_RO_DYN_SECTION
113 /* Don't adjust .dynamic unnecessarily. */
114 if (l->l_addr != 0)
116 ElfW(Addr) l_addr = l->l_addr;
117 int cnt = 0;
119 # define ADJUST_DYN_INFO(tag) \
120 do \
121 if (info[tag] != NULL) \
123 if (temp) \
125 temp[cnt].d_tag = info[tag]->d_tag; \
126 temp[cnt].d_un.d_ptr = info[tag]->d_un.d_ptr + l_addr; \
127 info[tag] = temp + cnt++; \
129 else \
130 info[tag]->d_un.d_ptr += l_addr; \
132 while (0)
134 ADJUST_DYN_INFO (DT_HASH);
135 ADJUST_DYN_INFO (DT_PLTGOT);
136 ADJUST_DYN_INFO (DT_STRTAB);
137 ADJUST_DYN_INFO (DT_SYMTAB);
138 # if ! ELF_MACHINE_NO_RELA
139 ADJUST_DYN_INFO (DT_RELA);
140 # endif
141 # if ! ELF_MACHINE_NO_REL
142 ADJUST_DYN_INFO (DT_REL);
143 # endif
144 ADJUST_DYN_INFO (DT_JMPREL);
145 ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
146 # undef ADJUST_DYN_INFO
147 assert (cnt <= DL_RO_DYN_TEMP_CNT);
149 #endif
150 if (info[DT_PLTREL] != NULL)
152 #if ELF_MACHINE_NO_RELA
153 assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
154 #elif ELF_MACHINE_NO_REL
155 assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
156 #else
157 assert (info[DT_PLTREL]->d_un.d_val == DT_REL
158 || info[DT_PLTREL]->d_un.d_val == DT_RELA);
159 #endif
161 #if ! ELF_MACHINE_NO_RELA
162 if (info[DT_RELA] != NULL)
163 assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
164 # endif
165 # if ! ELF_MACHINE_NO_REL
166 if (info[DT_REL] != NULL)
167 assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
168 #endif
169 #ifdef RTLD_BOOTSTRAP
170 /* Only the bind now flags are allowed. */
171 assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
172 || info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val == DF_1_NOW);
173 assert (info[DT_FLAGS] == NULL
174 || info[DT_FLAGS]->d_un.d_val == DF_BIND_NOW);
175 /* Flags must not be set for ld.so. */
176 assert (info[DT_RUNPATH] == NULL);
177 assert (info[DT_RPATH] == NULL);
178 #else
179 if (info[DT_FLAGS] != NULL)
181 /* Flags are used. Translate to the old form where available.
182 Since these l_info entries are only tested for NULL pointers it
183 is ok if they point to the DT_FLAGS entry. */
184 l->l_flags = info[DT_FLAGS]->d_un.d_val;
186 if (l->l_flags & DF_SYMBOLIC)
187 info[DT_SYMBOLIC] = info[DT_FLAGS];
188 if (l->l_flags & DF_TEXTREL)
189 info[DT_TEXTREL] = info[DT_FLAGS];
190 if (l->l_flags & DF_BIND_NOW)
191 info[DT_BIND_NOW] = info[DT_FLAGS];
193 if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
195 l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
197 if (l->l_flags_1 & DF_1_NOW)
198 info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)];
200 if (info[DT_RUNPATH] != NULL)
201 /* If both RUNPATH and RPATH are given, the latter is ignored. */
202 info[DT_RPATH] = NULL;
203 #endif
206 #ifdef RESOLVE_MAP
208 # ifdef RTLD_BOOTSTRAP
209 # define ELF_DURING_STARTUP (1)
210 # else
211 # define ELF_DURING_STARTUP (0)
212 # endif
214 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
215 These functions are almost identical, so we use cpp magic to avoid
216 duplicating their code. It cannot be done in a more general function
217 because we must be able to completely inline. */
219 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
220 range. Note that according to the ELF spec, this is completely legal!
221 But conditionally define things so that on machines we know this will
222 not happen we do something more optimal. */
224 # ifdef ELF_MACHINE_PLTREL_OVERLAP
225 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
226 do { \
227 struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \
228 int ranges_index; \
230 ranges[0].lazy = ranges[2].lazy = 0; \
231 ranges[1].lazy = 1; \
232 ranges[0].size = ranges[1].size = ranges[2].size = 0; \
234 if ((map)->l_info[DT_##RELOC]) \
236 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
237 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
240 if ((do_lazy) \
241 && (map)->l_info[DT_PLTREL] \
242 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
244 ranges[1].start = D_PTR ((map), l_info[DT_JMPREL]); \
245 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
246 ranges[2].start = ranges[1].start + ranges[1].size; \
247 ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \
248 ranges[0].size = ranges[1].start - ranges[0].start; \
251 for (ranges_index = 0; ranges_index < 3; ++ranges_index) \
252 elf_dynamic_do_##reloc ((map), \
253 ranges[ranges_index].start, \
254 ranges[ranges_index].size, \
255 ranges[ranges_index].lazy); \
256 } while (0)
257 # else
258 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
259 do { \
260 struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \
261 ranges[0].lazy = 0; \
262 ranges[0].size = ranges[1].size = 0; \
263 ranges[0].start = 0; \
265 if ((map)->l_info[DT_##RELOC]) \
267 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
268 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
270 if ((map)->l_info[DT_PLTREL] \
271 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
273 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \
275 if (! ELF_DURING_STARTUP \
276 && ((do_lazy) \
277 /* This test does not only detect whether the relocation \
278 sections are in the right order, it also checks whether \
279 there is a DT_REL/DT_RELA section. */ \
280 || ranges[0].start + ranges[0].size != start)) \
282 ranges[1].start = start; \
283 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
284 ranges[1].lazy = (do_lazy); \
286 else \
288 /* Combine processing the sections. */ \
289 assert (ranges[0].start + ranges[0].size == start); \
290 ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
294 if (ELF_DURING_STARTUP) \
295 elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size, 0); \
296 else \
298 int ranges_index; \
299 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
300 elf_dynamic_do_##reloc ((map), \
301 ranges[ranges_index].start, \
302 ranges[ranges_index].size, \
303 ranges[ranges_index].lazy); \
305 } while (0)
306 # endif
308 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
309 # define _ELF_CHECK_REL 0
310 # else
311 # define _ELF_CHECK_REL 1
312 # endif
314 # if ! ELF_MACHINE_NO_REL
315 # include "do-rel.h"
316 # define ELF_DYNAMIC_DO_REL(map, lazy) \
317 _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
318 # else
319 # define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */
320 # endif
322 # if ! ELF_MACHINE_NO_RELA
323 # define DO_RELA
324 # include "do-rel.h"
325 # define ELF_DYNAMIC_DO_RELA(map, lazy) \
326 _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
327 # else
328 # define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */
329 # endif
331 /* This can't just be an inline function because GCC is too dumb
332 to inline functions containing inlines themselves. */
333 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
334 do { \
335 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
336 (consider_profile)); \
337 ELF_DYNAMIC_DO_REL ((map), edr_lazy); \
338 ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \
339 } while (0)
341 #endif