Update.
[glibc.git] / elf / dynamic-link.h
blob7edac41cc0d0da52604a9a2b191b9ff684a0718c
1 /* Inline functions for dynamic linking.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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_PROCNUM + DT_VERSIONTAGIDX (sym))
26 #endif
29 /* Global read-only variable defined in rtld.c which is nonzero if we
30 shall give more warning messages. */
31 extern int _dl_verbose __attribute__ ((unused));
34 /* Read the dynamic section at DYN and fill in INFO with indices DT_*. */
36 static inline void __attribute__ ((unused))
37 elf_get_dynamic_info (struct link_map *l)
39 ElfW(Dyn) *dyn = l->l_ld;
40 ElfW(Addr) l_addr;
41 ElfW(Dyn) **info;
43 if (! dyn)
44 return;
46 l_addr = l->l_addr;
47 info = l->l_info;
49 while (dyn->d_tag != DT_NULL)
51 if (dyn->d_tag < DT_NUM)
52 info[dyn->d_tag] = dyn;
53 else if (dyn->d_tag >= DT_LOPROC &&
54 dyn->d_tag < DT_LOPROC + DT_PROCNUM)
55 info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
56 else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
57 info[VERSYMIDX (dyn->d_tag)] = dyn;
58 else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
59 info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_PROCNUM
60 + DT_VERSIONTAGNUM] = dyn;
61 else
62 assert (! "bad dynamic tag");
63 ++dyn;
66 if (info[DT_PLTGOT] != NULL)
67 info[DT_PLTGOT]->d_un.d_ptr += l_addr;
68 if (info[DT_STRTAB] != NULL)
69 info[DT_STRTAB]->d_un.d_ptr += l_addr;
70 if (info[DT_SYMTAB] != NULL)
71 info[DT_SYMTAB]->d_un.d_ptr += l_addr;
72 #if ! ELF_MACHINE_NO_RELA
73 if (info[DT_RELA] != NULL)
75 assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
76 info[DT_RELA]->d_un.d_ptr += l_addr;
78 #endif
79 #if ! ELF_MACHINE_NO_REL
80 if (info[DT_REL] != NULL)
82 assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
83 info[DT_REL]->d_un.d_ptr += l_addr;
85 #endif
86 if (info[DT_PLTREL] != NULL)
88 #if ELF_MACHINE_NO_RELA
89 assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
90 #elif ELF_MACHINE_NO_REL
91 assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
92 #else
93 assert (info[DT_PLTREL]->d_un.d_val == DT_REL
94 || info[DT_PLTREL]->d_un.d_val == DT_RELA);
95 #endif
97 if (info[DT_JMPREL] != NULL)
98 info[DT_JMPREL]->d_un.d_ptr += l_addr;
99 if (info[VERSYMIDX (DT_VERSYM)] != NULL)
100 info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr += l_addr;
101 if (info[DT_FLAGS] != NULL)
103 /* Flags are used. Translate to the old form where available.
104 Since these l_info entries are only tested for NULL pointers it
105 is ok if they point to the DT_FLAGS entry. */
106 ElfW(Word) flags = info[DT_FLAGS]->d_un.d_val;
107 if (flags & DF_SYMBOLIC)
108 info[DT_SYMBOLIC] = info[DT_FLAGS];
109 if (flags & DF_TEXTREL)
110 info[DT_TEXTREL] = info[DT_FLAGS];
111 if (flags & DF_BIND_NOW)
112 info[DT_BIND_NOW] = info[DT_FLAGS];
114 /* Determine how many constructors there are. */
115 if (info[DT_INIT_ARRAY] != NULL)
116 info[DT_INIT_ARRAY]->d_un.d_ptr += l_addr;
117 l->l_initcount = 1 + (info[DT_INIT_ARRAY]
118 ? (info[DT_INIT_ARRAYSZ]->d_un.d_val
119 / sizeof (ElfW(Addr)))
120 : 0);
121 l->l_preinitcount = (info[DT_PREINIT_ARRAY]
122 ? (info[DT_PREINIT_ARRAYSZ]->d_un.d_val
123 / sizeof (ElfW(Addr)))
124 : 0);
125 if (info[DT_RUNPATH] != NULL)
126 /* If both RUNPATH and RPATH are given, the latter is ignored. */
127 info[DT_RPATH] = NULL;
130 #ifdef RESOLVE
132 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
133 These functions are almost identical, so we use cpp magic to avoid
134 duplicating their code. It cannot be done in a more general function
135 because we must be able to completely inline. */
137 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
138 range. Note that according to the ELF spec, this is completely legal!
139 But conditionally define things so that on machines we know this will
140 not happen we do something more optimal. */
142 # ifdef ELF_MACHINE_PLTREL_OVERLAP
143 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
144 do { \
145 struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \
146 int ranges_index; \
148 ranges[0].lazy = ranges[2].lazy = 0; \
149 ranges[1].lazy = 1; \
150 ranges[0].size = ranges[1].size = ranges[2].size = 0; \
152 if ((map)->l_info[DT_##RELOC]) \
154 ranges[0].start = (map)->l_info[DT_##RELOC]->d_un.d_ptr; \
155 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
158 if ((do_lazy) \
159 && (map)->l_info[DT_PLTREL] \
160 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
162 ranges[1].start = (map)->l_info[DT_JMPREL]->d_un.d_ptr; \
163 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
164 ranges[2].start = ranges[1].start + ranges[1].size; \
165 ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \
166 ranges[0].size = ranges[1].start - ranges[0].start; \
169 for (ranges_index = 0; ranges_index < 3; ++ranges_index) \
170 elf_dynamic_do_##reloc ((map), \
171 ranges[ranges_index].start, \
172 ranges[ranges_index].size, \
173 ranges[ranges_index].lazy); \
174 } while (0)
175 # else
176 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
177 do { \
178 struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \
179 int ranges_index; \
180 ranges[0].lazy = 0; \
181 ranges[0].size = ranges[1].size = 0; \
182 ranges[0].start = 0; \
184 if ((map)->l_info[DT_##RELOC]) \
186 ranges[0].start = (map)->l_info[DT_##RELOC]->d_un.d_ptr; \
187 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
189 if ((map)->l_info[DT_PLTREL] \
190 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
192 ElfW(Addr) start = (map)->l_info[DT_JMPREL]->d_un.d_ptr; \
194 if ((do_lazy) \
195 /* This test does not only detect whether the relocation \
196 sections are in the right order, it also checks whether \
197 there is a DT_REL/DT_RELA section. */ \
198 || ranges[0].start + ranges[0].size != start) \
200 ranges[1].start = start; \
201 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
202 ranges[1].lazy = (do_lazy); \
204 else \
205 /* Combine processing the sections. */ \
206 ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
209 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
210 elf_dynamic_do_##reloc ((map), \
211 ranges[ranges_index].start, \
212 ranges[ranges_index].size, \
213 ranges[ranges_index].lazy); \
214 } while (0)
215 # endif
217 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
218 # define _ELF_CHECK_REL 0
219 # else
220 # define _ELF_CHECK_REL 1
221 # endif
223 # if ! ELF_MACHINE_NO_REL
224 # include "do-rel.h"
225 # define ELF_DYNAMIC_DO_REL(map, lazy) \
226 _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
227 # else
228 # define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */
229 # endif
231 # if ! ELF_MACHINE_NO_RELA
232 # define DO_RELA
233 # include "do-rel.h"
234 # define ELF_DYNAMIC_DO_RELA(map, lazy) \
235 _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
236 # else
237 # define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */
238 # endif
240 /* This can't just be an inline function because GCC is too dumb
241 to inline functions containing inlines themselves. */
242 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
243 do { \
244 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
245 (consider_profile)); \
246 ELF_DYNAMIC_DO_REL ((map), edr_lazy); \
247 ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \
248 } while (0)
250 #endif