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
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
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
);
53 auto void __attribute__((always_inline
))
54 elf_machine_lazy_rel (struct link_map
*map
,
55 ElfW(Addr
) l_addr
, const ElfW(Rela
) *reloc
);
59 #include <dl-machine.h>
62 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
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
;
74 #ifndef RTLD_BOOTSTRAP
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
;
102 #define DL_RO_DYN_TEMP_CNT 8
104 #ifndef DL_RO_DYN_SECTION
105 /* Don't adjust .dynamic unnecessarily. */
108 ElfW(Addr
) l_addr
= l
->l_addr
;
111 # define ADJUST_DYN_INFO(tag) \
113 if (info[tag] != NULL) \
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++; \
122 info[tag]->d_un.d_ptr += l_addr; \
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
);
133 # if ! ELF_MACHINE_NO_REL
134 ADJUST_DYN_INFO (DT_REL
);
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
);
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
);
149 assert (info
[DT_PLTREL
]->d_un
.d_val
== DT_REL
150 || info
[DT_PLTREL
]->d_un
.d_val
== DT_RELA
);
153 #if ! ELF_MACHINE_NO_RELA
154 if (info
[DT_RELA
] != NULL
)
155 assert (info
[DT_RELAENT
]->d_un
.d_val
== sizeof (ElfW(Rela
)));
157 # if ! ELF_MACHINE_NO_REL
158 if (info
[DT_REL
] != NULL
)
159 assert (info
[DT_RELENT
]->d_un
.d_val
== sizeof (ElfW(Rel
)));
161 #ifdef RTLD_BOOTSTRAP
162 /* Only the bind now flags are allowed. */
163 assert (info
[VERSYMIDX (DT_FLAGS_1
)] == NULL
164 || info
[VERSYMIDX (DT_FLAGS_1
)]->d_un
.d_val
== DF_1_NOW
);
165 assert (info
[DT_FLAGS
] == NULL
166 || info
[DT_FLAGS
]->d_un
.d_val
== DF_BIND_NOW
);
167 /* Flags must not be set for ld.so. */
168 assert (info
[DT_RUNPATH
] == NULL
);
169 assert (info
[DT_RPATH
] == NULL
);
171 if (info
[DT_FLAGS
] != NULL
)
173 /* Flags are used. Translate to the old form where available.
174 Since these l_info entries are only tested for NULL pointers it
175 is ok if they point to the DT_FLAGS entry. */
176 l
->l_flags
= info
[DT_FLAGS
]->d_un
.d_val
;
178 if (l
->l_flags
& DF_SYMBOLIC
)
179 info
[DT_SYMBOLIC
] = info
[DT_FLAGS
];
180 if (l
->l_flags
& DF_TEXTREL
)
181 info
[DT_TEXTREL
] = info
[DT_FLAGS
];
182 if (l
->l_flags
& DF_BIND_NOW
)
183 info
[DT_BIND_NOW
] = info
[DT_FLAGS
];
185 if (info
[VERSYMIDX (DT_FLAGS_1
)] != NULL
)
187 l
->l_flags_1
= info
[VERSYMIDX (DT_FLAGS_1
)]->d_un
.d_val
;
189 if (l
->l_flags_1
& DF_1_NOW
)
190 info
[DT_BIND_NOW
] = info
[VERSYMIDX (DT_FLAGS_1
)];
192 if (info
[DT_RUNPATH
] != NULL
)
193 /* If both RUNPATH and RPATH are given, the latter is ignored. */
194 info
[DT_RPATH
] = NULL
;
200 # ifdef RTLD_BOOTSTRAP
201 # define ELF_DURING_STARTUP (1)
203 # define ELF_DURING_STARTUP (0)
206 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
207 These functions are almost identical, so we use cpp magic to avoid
208 duplicating their code. It cannot be done in a more general function
209 because we must be able to completely inline. */
211 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
212 range. Note that according to the ELF spec, this is completely legal!
213 But conditionally define things so that on machines we know this will
214 not happen we do something more optimal. */
216 # ifdef ELF_MACHINE_PLTREL_OVERLAP
217 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
219 struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \
222 ranges[0].lazy = ranges[2].lazy = 0; \
223 ranges[1].lazy = 1; \
224 ranges[0].size = ranges[1].size = ranges[2].size = 0; \
226 if ((map)->l_info[DT_##RELOC]) \
228 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
229 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
233 && (map)->l_info[DT_PLTREL] \
234 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
236 ranges[1].start = D_PTR ((map), l_info[DT_JMPREL]); \
237 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
238 ranges[2].start = ranges[1].start + ranges[1].size; \
239 ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \
240 ranges[0].size = ranges[1].start - ranges[0].start; \
243 for (ranges_index = 0; ranges_index < 3; ++ranges_index) \
244 elf_dynamic_do_##reloc ((map), \
245 ranges[ranges_index].start, \
246 ranges[ranges_index].size, \
247 ranges[ranges_index].lazy); \
250 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
252 struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \
253 ranges[0].lazy = 0; \
254 ranges[0].size = ranges[1].size = 0; \
255 ranges[0].start = 0; \
257 if ((map)->l_info[DT_##RELOC]) \
259 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
260 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
262 if ((map)->l_info[DT_PLTREL] \
263 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
265 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \
267 if (! ELF_DURING_STARTUP \
269 /* This test does not only detect whether the relocation \
270 sections are in the right order, it also checks whether \
271 there is a DT_REL/DT_RELA section. */ \
272 || ranges[0].start + ranges[0].size != start)) \
274 ranges[1].start = start; \
275 ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
276 ranges[1].lazy = (do_lazy); \
280 /* Combine processing the sections. */ \
281 assert (ranges[0].start + ranges[0].size == start); \
282 ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
286 if (ELF_DURING_STARTUP) \
287 elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size, 0); \
291 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
292 elf_dynamic_do_##reloc ((map), \
293 ranges[ranges_index].start, \
294 ranges[ranges_index].size, \
295 ranges[ranges_index].lazy); \
300 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
301 # define _ELF_CHECK_REL 0
303 # define _ELF_CHECK_REL 1
306 # if ! ELF_MACHINE_NO_REL
308 # define ELF_DYNAMIC_DO_REL(map, lazy) \
309 _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
311 # define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */
314 # if ! ELF_MACHINE_NO_RELA
317 # define ELF_DYNAMIC_DO_RELA(map, lazy) \
318 _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
320 # define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */
323 /* This can't just be an inline function because GCC is too dumb
324 to inline functions containing inlines themselves. */
325 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
327 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
328 (consider_profile)); \
329 ELF_DYNAMIC_DO_REL ((map), edr_lazy); \
330 ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \