* NEWS: Added 2.2.6 text from 2.2 branch version.
[glibc.git] / elf / dynamic-link.h
blobd9cb63299f164bc55d431271e0fa36380bc75927
1 /* Inline functions for dynamic linking.
2 Copyright (C) 1995,96,97,98,99,2000,2001,2002 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 <dl-machine.h>
22 #include <assert.h>
24 #ifndef VERSYMIDX
25 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
26 #endif
29 /* Read the dynamic section at DYN and fill in INFO with indices DT_*. */
31 static inline void __attribute__ ((unused))
32 elf_get_dynamic_info (struct link_map *l)
34 ElfW(Dyn) *dyn = l->l_ld;
35 ElfW(Dyn) **info;
37 #ifndef RTLD_BOOTSTRAP
38 if (dyn == NULL)
39 return;
40 #endif
42 info = l->l_info;
44 while (dyn->d_tag != DT_NULL)
46 if (dyn->d_tag < DT_NUM)
47 info[dyn->d_tag] = dyn;
48 else if (dyn->d_tag >= DT_LOPROC &&
49 dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
50 info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
51 else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
52 info[VERSYMIDX (dyn->d_tag)] = dyn;
53 else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
54 info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
55 + DT_VERSIONTAGNUM] = dyn;
56 else if ((Elf32_Word) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
57 info[DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
58 + DT_VERSIONTAGNUM + DT_EXTRANUM] = dyn;
59 else if ((Elf32_Word) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
60 info[DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
61 + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] = dyn;
62 ++dyn;
64 #ifndef DL_RO_DYN_SECTION
65 /* Don't adjust .dynamic unnecessarily. */
66 if (l->l_addr != 0)
68 ElfW(Addr) l_addr = l->l_addr;
70 if (info[DT_PLTGOT] != NULL)
71 info[DT_PLTGOT]->d_un.d_ptr += l_addr;
72 if (info[DT_STRTAB] != NULL)
73 info[DT_STRTAB]->d_un.d_ptr += l_addr;
74 if (info[DT_SYMTAB] != NULL)
75 info[DT_SYMTAB]->d_un.d_ptr += l_addr;
76 # if ! ELF_MACHINE_NO_RELA
77 if (info[DT_RELA] != NULL)
78 info[DT_RELA]->d_un.d_ptr += l_addr;
79 # endif
80 # if ! ELF_MACHINE_NO_REL
81 if (info[DT_REL] != NULL)
82 info[DT_REL]->d_un.d_ptr += l_addr;
83 # endif
84 if (info[DT_JMPREL] != NULL)
85 info[DT_JMPREL]->d_un.d_ptr += l_addr;
86 if (info[VERSYMIDX (DT_VERSYM)] != NULL)
87 info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr += l_addr;
89 #endif
90 if (info[DT_PLTREL] != NULL)
92 #if ELF_MACHINE_NO_RELA
93 assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
94 #elif ELF_MACHINE_NO_REL
95 assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
96 #else
97 assert (info[DT_PLTREL]->d_un.d_val == DT_REL
98 || info[DT_PLTREL]->d_un.d_val == DT_RELA);
99 #endif
101 #if ! ELF_MACHINE_NO_RELA
102 if (info[DT_RELA] != NULL)
103 assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
104 # endif
105 # if ! ELF_MACHINE_NO_REL
106 if (info[DT_REL] != NULL)
107 assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
108 #endif
109 if (info[DT_FLAGS] != NULL)
111 /* Flags are used. Translate to the old form where available.
112 Since these l_info entries are only tested for NULL pointers it
113 is ok if they point to the DT_FLAGS entry. */
114 l->l_flags = info[DT_FLAGS]->d_un.d_val;
115 #ifdef RTLD_BOOTSTRAP
116 /* These three flags must not be set for ld.so. */
117 assert ((l->l_flags & (DF_SYMBOLIC | DF_TEXTREL | DF_BIND_NOW)) == 0);
118 #else
119 if (l->l_flags & DF_SYMBOLIC)
120 info[DT_SYMBOLIC] = info[DT_FLAGS];
121 if (l->l_flags & DF_TEXTREL)
122 info[DT_TEXTREL] = info[DT_FLAGS];
123 if (l->l_flags & DF_BIND_NOW)
124 info[DT_BIND_NOW] = info[DT_FLAGS];
125 #endif
127 if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
128 l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
129 #ifdef RTLD_BOOTSTRAP
130 /* The dynamic linker should have none of these set. */
131 assert (info[DT_RUNPATH] == NULL);
132 assert (info[DT_RPATH] == NULL);
133 #else
134 if (info[DT_RUNPATH] != NULL)
135 /* If both RUNPATH and RPATH are given, the latter is ignored. */
136 info[DT_RPATH] = NULL;
137 #endif
140 #ifdef RESOLVE
142 # ifdef RTLD_BOOTSTRAP
143 # define ELF_DURING_STARTUP (1)
144 # else
145 # define ELF_DURING_STARTUP (0)
146 # endif
148 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
149 These functions are almost identical, so we use cpp magic to avoid
150 duplicating their code. It cannot be done in a more general function
151 because we must be able to completely inline. */
153 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
154 range. Note that according to the ELF spec, this is completely legal!
155 But conditionally define things so that on machines we know this will
156 not happen we do something more optimal. */
158 # ifdef ELF_MACHINE_PLTREL_OVERLAP
159 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
160 do { \
161 struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \
162 int ranges_index; \
164 ranges[0].lazy = ranges[2].lazy = 0; \
165 ranges[1].lazy = 1; \
166 ranges[0].size = ranges[1].size = ranges[2].size = 0; \
168 if ((map)->l_info[DT_##RELOC]) \
170 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
171 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
174 if ((do_lazy) \
175 && (map)->l_info[DT_PLTREL] \
176 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
178 ranges[1].start = D_PTR ((map), l_info[DT_JMPREL]); \
179 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
180 ranges[2].start = ranges[1].start + ranges[1].size; \
181 ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \
182 ranges[0].size = ranges[1].start - ranges[0].start; \
185 for (ranges_index = 0; ranges_index < 3; ++ranges_index) \
186 elf_dynamic_do_##reloc ((map), \
187 ranges[ranges_index].start, \
188 ranges[ranges_index].size, \
189 ranges[ranges_index].lazy); \
190 } while (0)
191 # else
192 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
193 do { \
194 struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \
195 ranges[0].lazy = 0; \
196 ranges[0].size = ranges[1].size = 0; \
197 ranges[0].start = 0; \
199 if ((map)->l_info[DT_##RELOC]) \
201 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
202 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
204 if ((map)->l_info[DT_PLTREL] \
205 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
207 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \
209 if (! ELF_DURING_STARTUP \
210 && ((do_lazy) \
211 /* This test does not only detect whether the relocation \
212 sections are in the right order, it also checks whether \
213 there is a DT_REL/DT_RELA section. */ \
214 || ranges[0].start + ranges[0].size != start)) \
216 ranges[1].start = start; \
217 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
218 ranges[1].lazy = (do_lazy); \
220 else \
222 /* Combine processing the sections. */ \
223 assert (ranges[0].start + ranges[0].size == start); \
224 ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
228 if (ELF_DURING_STARTUP) \
229 elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size, 0); \
230 else \
232 int ranges_index; \
233 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
234 elf_dynamic_do_##reloc ((map), \
235 ranges[ranges_index].start, \
236 ranges[ranges_index].size, \
237 ranges[ranges_index].lazy); \
239 } while (0)
240 # endif
242 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
243 # define _ELF_CHECK_REL 0
244 # else
245 # define _ELF_CHECK_REL 1
246 # endif
248 # if ! ELF_MACHINE_NO_REL
249 # include "do-rel.h"
250 # define ELF_DYNAMIC_DO_REL(map, lazy) \
251 _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
252 # else
253 # define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */
254 # endif
256 # if ! ELF_MACHINE_NO_RELA
257 # define DO_RELA
258 # include "do-rel.h"
259 # define ELF_DYNAMIC_DO_RELA(map, lazy) \
260 _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
261 # else
262 # define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */
263 # endif
265 /* This can't just be an inline function because GCC is too dumb
266 to inline functions containing inlines themselves. */
267 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
268 do { \
269 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
270 (consider_profile)); \
271 ELF_DYNAMIC_DO_REL ((map), edr_lazy); \
272 ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \
273 } while (0)
275 #endif