]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-reloc.c
Update.
[thirdparty/glibc.git] / elf / dl-reloc.c
1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995-2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
19
20 #include <errno.h>
21 #include <libintl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25 #include <sys/mman.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include "dynamic-link.h"
29
30 /* Statistics function. */
31 #ifdef SHARED
32 # define bump_num_cache_relocations() ++GL(dl_num_cache_relocations)
33 #else
34 # define bump_num_cache_relocations() ((void) 0)
35 #endif
36
37
38 #ifdef USE_TLS
39 /* We are trying to perform a static TLS relocation in MAP, but it was
40 dynamically loaded. This can only work if there is enough surplus in
41 the static TLS area already allocated for each running thread. If this
42 object's TLS segment is too big to fit, we return false. If it fits,
43 we set MAP->l_tls_offset and return true. */
44 static bool
45 allocate_static_tls (struct link_map *map)
46 {
47 size_t offset = roundup (GL(dl_tls_static_used), map->l_tls_align);
48 if (offset + map->l_tls_blocksize
49 # if TLS_TCB_AT_TP
50 + TLS_TCB_SIZE
51 # elif TLS_DTV_AT_TP
52 /* dl_tls_static_used includes the TCB at the beginning. */
53 # else
54 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
55 # endif
56 > GL(dl_tls_static_size))
57 return false;
58 map->l_tls_offset = offset;
59 GL(dl_tls_static_used) = offset + map->l_tls_blocksize;
60 return true;
61 }
62 #endif
63
64
65 void
66 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
67 int lazy, int consider_profiling)
68 {
69 struct textrels
70 {
71 caddr_t start;
72 size_t len;
73 int prot;
74 struct textrels *next;
75 } *textrels = NULL;
76 /* Initialize it to make the compiler happy. */
77 const char *errstring = NULL;
78
79 if (l->l_relocated)
80 return;
81
82 /* If DT_BIND_NOW is set relocate all references in this object. We
83 do not do this if we are profiling, of course. */
84 if (!consider_profiling
85 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
86 lazy = 0;
87
88 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_RELOC, 0))
89 INTUSE(_dl_debug_printf) ("\nrelocation processing: %s%s\n",
90 l->l_name[0] ? l->l_name : rtld_progname,
91 lazy ? " (lazy)" : "");
92
93 /* DT_TEXTREL is now in level 2 and might phase out at some time.
94 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
95 testing easier and therefore it will be available at all time. */
96 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
97 {
98 /* Bletch. We must make read-only segments writable
99 long enough to relocate them. */
100 const ElfW(Phdr) *ph;
101 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
102 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
103 {
104 struct textrels *newp;
105
106 newp = (struct textrels *) alloca (sizeof (*newp));
107 newp->len = (((ph->p_vaddr + ph->p_memsz + GL(dl_pagesize) - 1)
108 & ~(GL(dl_pagesize) - 1))
109 - (ph->p_vaddr & ~(GL(dl_pagesize) - 1)));
110 newp->start = ((ph->p_vaddr & ~(GL(dl_pagesize) - 1))
111 + (caddr_t) l->l_addr);
112
113 if (__mprotect (newp->start, newp->len, PROT_READ|PROT_WRITE) < 0)
114 {
115 errstring = N_("cannot make segment writable for relocation");
116 call_error:
117 INTUSE(_dl_signal_error) (errno, l->l_name, NULL, errstring);
118 }
119
120 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
121 newp->prot = (PF_TO_PROT
122 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
123 #else
124 newp->prot = 0;
125 if (ph->p_flags & PF_R)
126 newp->prot |= PROT_READ;
127 if (ph->p_flags & PF_W)
128 newp->prot |= PROT_WRITE;
129 if (ph->p_flags & PF_X)
130 newp->prot |= PROT_EXEC;
131 #endif
132 newp->next = textrels;
133 textrels = newp;
134 }
135 }
136
137 {
138 /* Do the actual relocation of the object's GOT and other data. */
139
140 /* String table object symbols. */
141 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
142
143 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
144 #define RESOLVE_MAP(ref, version, r_type) \
145 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
146 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
147 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
148 ? (bump_num_cache_relocations (), \
149 (*ref) = l->l_lookup_cache.ret, \
150 l->l_lookup_cache.value) \
151 : ({ lookup_t _lr; \
152 int _tc = elf_machine_type_class (r_type); \
153 l->l_lookup_cache.type_class = _tc; \
154 l->l_lookup_cache.sym = (*ref); \
155 _lr = ((version) != NULL && (version)->hash != 0 \
156 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
157 + (*ref)->st_name, \
158 l, (ref), scope, \
159 (version), _tc, 0) \
160 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
161 (ref), scope, _tc, \
162 DL_LOOKUP_ADD_DEPENDENCY)); \
163 l->l_lookup_cache.ret = (*ref); \
164 l->l_lookup_cache.value = _lr; })) \
165 : l)
166 #define RESOLVE(ref, version, r_type) \
167 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
168 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
169 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
170 ? (bump_num_cache_relocations (), \
171 (*ref) = l->l_lookup_cache.ret, \
172 l->l_lookup_cache.value) \
173 : ({ lookup_t _lr; \
174 int _tc = elf_machine_type_class (r_type); \
175 l->l_lookup_cache.type_class = _tc; \
176 l->l_lookup_cache.sym = (*ref); \
177 _lr = ((version) != NULL && (version)->hash != 0 \
178 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
179 + (*ref)->st_name, \
180 l, (ref), scope, \
181 (version), _tc, 0) \
182 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
183 (ref), scope, _tc, \
184 DL_LOOKUP_ADD_DEPENDENCY)); \
185 l->l_lookup_cache.ret = (*ref); \
186 l->l_lookup_cache.value = _lr; })) \
187 : l->l_addr)
188
189 /* This macro is used as a callback from elf_machine_rel{a,} when a
190 static TLS reloc is about to be performed. Since (in dl-load.c) we
191 permit dynamic loading of objects that might use such relocs, we
192 have to check whether each use is actually doable. If the object
193 whose TLS segment the reference resolves to was allocated space in
194 the static TLS block at startup, then it's ok. Otherwise, we make
195 an attempt to allocate it in surplus space on the fly. If that
196 can't be done, we fall back to the error that DF_STATIC_TLS is
197 intended to produce. */
198 #define CHECK_STATIC_TLS(map, sym_map) \
199 do { \
200 if (__builtin_expect ((sym_map)->l_tls_offset == 0, 0) \
201 && !allocate_static_tls (sym_map)) \
202 { \
203 errstring = N_("shared object cannot be dlopen()ed"); \
204 INTUSE(_dl_signal_error) (0, (map)->l_name, NULL, errstring); \
205 } \
206 } while (0)
207
208 #include "dynamic-link.h"
209
210 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
211
212 if (__builtin_expect (consider_profiling, 0))
213 {
214 /* Allocate the array which will contain the already found
215 relocations. If the shared object lacks a PLT (for example
216 if it only contains lead function) the l_info[DT_PLTRELSZ]
217 will be NULL. */
218 if (l->l_info[DT_PLTRELSZ] == NULL)
219 {
220 errstring = N_("%s: profiler found no PLTREL in object %s\n");
221 fatal:
222 _dl_fatal_printf (errstring,
223 rtld_progname ?: "<program name unknown>",
224 l->l_name);
225 }
226
227 l->l_reloc_result =
228 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
229 l->l_info[DT_PLTRELSZ]->d_un.d_val);
230 if (l->l_reloc_result == NULL)
231 {
232 errstring = N_("\
233 %s: profiler out of memory shadowing PLTREL of %s\n");
234 goto fatal;
235 }
236 }
237 }
238
239 /* Mark the object so we know this work has been done. */
240 l->l_relocated = 1;
241
242 /* Undo the segment protection changes. */
243 while (__builtin_expect (textrels != NULL, 0))
244 {
245 if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0)
246 {
247 errstring = N_("cannot restore segment prot after reloc");
248 goto call_error;
249 }
250
251 textrels = textrels->next;
252 }
253 }
254 INTDEF (_dl_relocate_object)
255
256
257 void
258 internal_function
259 _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt)
260 {
261 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
262 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
263
264 /* XXX We cannot translate these messages. */
265 static const char msg[2][32
266 #if __ELF_NATIVE_CLASS == 64
267 + 6
268 #endif
269 ] = { "unexpected reloc type 0x",
270 "unexpected PLT reloc type 0x" };
271 char msgbuf[sizeof (msg[0])];
272 char *cp;
273
274 cp = __stpcpy (msgbuf, msg[plt]);
275 #if __ELF_NATIVE_CLASS == 64
276 if (__builtin_expect(type > 0xff, 0))
277 {
278 *cp++ = DIGIT (type >> 28);
279 *cp++ = DIGIT (type >> 24);
280 *cp++ = DIGIT (type >> 20);
281 *cp++ = DIGIT (type >> 16);
282 *cp++ = DIGIT (type >> 12);
283 *cp++ = DIGIT (type >> 8);
284 }
285 #endif
286 *cp++ = DIGIT (type >> 4);
287 *cp++ = DIGIT (type);
288 *cp = '\0';
289
290 INTUSE(_dl_signal_error) (0, map->l_name, NULL, msgbuf);
291 }