malloc: Add realloc test.
[glibc.git] / elf / dynamic-link.h
blobd563cbaa9caf6da59e461872c02ccb295127a9a5
1 /* Inline functions for dynamic linking.
2 Copyright (C) 1995-2013 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* This macro is used as a callback from elf_machine_rel{a,} when a
20 static TLS reloc is about to be performed. Since (in dl-load.c) we
21 permit dynamic loading of objects that might use such relocs, we
22 have to check whether each use is actually doable. If the object
23 whose TLS segment the reference resolves to was allocated space in
24 the static TLS block at startup, then it's ok. Otherwise, we make
25 an attempt to allocate it in surplus space on the fly. If that
26 can't be done, we fall back to the error that DF_STATIC_TLS is
27 intended to produce. */
28 #define CHECK_STATIC_TLS(map, sym_map) \
29 do { \
30 if (__builtin_expect ((sym_map)->l_tls_offset == NO_TLS_OFFSET \
31 || ((sym_map)->l_tls_offset \
32 == FORCED_DYNAMIC_TLS_OFFSET), 0)) \
33 _dl_allocate_static_tls (sym_map); \
34 } while (0)
36 #define TRY_STATIC_TLS(map, sym_map) \
37 (__builtin_expect ((sym_map)->l_tls_offset \
38 != FORCED_DYNAMIC_TLS_OFFSET, 1) \
39 && (__builtin_expect ((sym_map)->l_tls_offset != NO_TLS_OFFSET, 1) \
40 || _dl_try_allocate_static_tls (sym_map) == 0))
42 int internal_function _dl_try_allocate_static_tls (struct link_map *map);
44 #include <elf.h>
46 #ifdef RESOLVE_MAP
47 /* We pass reloc_addr as a pointer to void, as opposed to a pointer to
48 ElfW(Addr), because not all architectures can assume that the
49 relocated address is properly aligned, whereas the compiler is
50 entitled to assume that a pointer to a type is properly aligned for
51 the type. Even if we cast the pointer back to some other type with
52 less strict alignment requirements, the compiler might still
53 remember that the pointer was originally more aligned, thereby
54 optimizing away alignment tests or using word instructions for
55 copying memory, breaking the very code written to handle the
56 unaligned cases. */
57 # if ! ELF_MACHINE_NO_REL
58 auto inline void __attribute__((always_inline))
59 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
60 const ElfW(Sym) *sym, const struct r_found_version *version,
61 void *const reloc_addr, int skip_ifunc);
62 auto inline void __attribute__((always_inline))
63 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
64 void *const reloc_addr);
65 # endif
66 # if ! ELF_MACHINE_NO_RELA
67 auto inline void __attribute__((always_inline))
68 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
69 const ElfW(Sym) *sym, const struct r_found_version *version,
70 void *const reloc_addr, int skip_ifunc);
71 auto inline void __attribute__((always_inline))
72 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
73 void *const reloc_addr);
74 # endif
75 # if ELF_MACHINE_NO_RELA || defined ELF_MACHINE_PLT_REL
76 auto inline void __attribute__((always_inline))
77 elf_machine_lazy_rel (struct link_map *map,
78 ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
79 int skip_ifunc);
80 # else
81 auto inline void __attribute__((always_inline))
82 elf_machine_lazy_rel (struct link_map *map,
83 ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
84 int skip_ifunc);
85 # endif
86 #endif
88 #include <dl-machine.h>
90 #include "get-dynamic-info.h"
92 #ifdef RESOLVE_MAP
94 # ifdef RTLD_BOOTSTRAP
95 # define ELF_DURING_STARTUP (1)
96 # else
97 # define ELF_DURING_STARTUP (0)
98 # endif
100 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
101 These functions are almost identical, so we use cpp magic to avoid
102 duplicating their code. It cannot be done in a more general function
103 because we must be able to completely inline. */
105 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
106 range. Note that according to the ELF spec, this is completely legal!
108 We are guarenteed that we have one of three situations. Either DT_JMPREL
109 comes immediately after DT_REL*, or there is overlap and DT_JMPREL
110 consumes precisely the very end of the DT_REL*, or DT_JMPREL and DT_REL*
111 are completely separate and there is a gap between them. */
113 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, skip_ifunc, test_rel) \
114 do { \
115 struct { ElfW(Addr) start, size; \
116 __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; } \
117 ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; \
119 if ((map)->l_info[DT_##RELOC]) \
121 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \
122 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \
123 if (map->l_info[VERSYMIDX (DT_##RELOC##COUNT)] != NULL) \
124 ranges[0].nrelative \
125 = MIN (map->l_info[VERSYMIDX (DT_##RELOC##COUNT)]->d_un.d_val, \
126 ranges[0].size / sizeof (ElfW(reloc))); \
128 if ((map)->l_info[DT_PLTREL] \
129 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
131 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \
132 ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \
134 if (ranges[0].start + ranges[0].size == (start + size)) \
135 ranges[0].size -= size; \
136 if (! ELF_DURING_STARTUP && ((do_lazy) || ranges[0].size == 0)) \
138 ranges[1].start = start; \
139 ranges[1].size = size; \
140 ranges[1].lazy = (do_lazy); \
142 else \
144 /* Combine processing the sections. */ \
145 ranges[0].size += size; \
149 if (ELF_DURING_STARTUP) \
150 elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size, \
151 ranges[0].nrelative, 0, skip_ifunc); \
152 else \
154 int ranges_index; \
155 for (ranges_index = 0; ranges_index < 2; ++ranges_index) \
156 elf_dynamic_do_##reloc ((map), \
157 ranges[ranges_index].start, \
158 ranges[ranges_index].size, \
159 ranges[ranges_index].nrelative, \
160 ranges[ranges_index].lazy, \
161 skip_ifunc); \
163 } while (0)
165 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
166 # define _ELF_CHECK_REL 0
167 # else
168 # define _ELF_CHECK_REL 1
169 # endif
171 # if ! ELF_MACHINE_NO_REL
172 # include "do-rel.h"
173 # define ELF_DYNAMIC_DO_REL(map, lazy, skip_ifunc) \
174 _ELF_DYNAMIC_DO_RELOC (REL, Rel, map, lazy, skip_ifunc, _ELF_CHECK_REL)
175 # else
176 # define ELF_DYNAMIC_DO_REL(map, lazy, skip_ifunc) /* Nothing to do. */
177 # endif
179 # if ! ELF_MACHINE_NO_RELA
180 # define DO_RELA
181 # include "do-rel.h"
182 # define ELF_DYNAMIC_DO_RELA(map, lazy, skip_ifunc) \
183 _ELF_DYNAMIC_DO_RELOC (RELA, Rela, map, lazy, skip_ifunc, _ELF_CHECK_REL)
184 # else
185 # define ELF_DYNAMIC_DO_RELA(map, lazy, skip_ifunc) /* Nothing to do. */
186 # endif
188 /* This can't just be an inline function because GCC is too dumb
189 to inline functions containing inlines themselves. */
190 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile, skip_ifunc) \
191 do { \
192 int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \
193 (consider_profile)); \
194 ELF_DYNAMIC_DO_REL ((map), edr_lazy, skip_ifunc); \
195 ELF_DYNAMIC_DO_RELA ((map), edr_lazy, skip_ifunc); \
196 } while (0)
198 #endif