Updated to fedora-glibc-2_3-20051023T0123
[glibc.git] / elf / dl-reloc.c
blob893b3c954a07da7e2dfb858f8dab8637782666dd
1 /* Relocate a shared object and resolve its references to other loaded objects.
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
18 02111-1307 USA. */
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"
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
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 fail. If it fits,
43 we set MAP->l_tls_offset and return.
44 This function intentionally does not return any value but signals error
45 directly, as static TLS should be rare and code handling it should
46 not be inlined as much as possible. */
47 void
48 internal_function __attribute_noinline__
49 _dl_allocate_static_tls (struct link_map *map)
51 size_t offset;
53 /* If the alignment requirements are too high fail. */
54 if (map->l_tls_align > GL(dl_tls_static_align))
56 fail:
57 _dl_signal_error (0, map->l_name, NULL, N_("\
58 cannot allocate memory in static TLS block"));
61 # if TLS_TCB_AT_TP
62 size_t freebytes;
63 size_t n;
64 size_t blsize;
66 freebytes = GL(dl_tls_static_size) - GL(dl_tls_static_used) - TLS_TCB_SIZE;
68 blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
69 if (freebytes < blsize)
70 goto fail;
72 n = (freebytes - blsize) / map->l_tls_align;
74 offset = GL(dl_tls_static_used) + (freebytes - n * map->l_tls_align
75 - map->l_tls_firstbyte_offset);
77 map->l_tls_offset = GL(dl_tls_static_used) = offset;
78 # elif TLS_DTV_AT_TP
79 size_t used;
80 size_t check;
82 offset = roundup (GL(dl_tls_static_used), map->l_tls_align);
83 used = offset + map->l_tls_blocksize;
84 check = used;
85 /* dl_tls_static_used includes the TCB at the beginning. */
87 if (check > GL(dl_tls_static_size))
88 goto fail;
90 map->l_tls_offset = offset;
91 GL(dl_tls_static_used) = used;
92 # else
93 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
94 # endif
96 if (map->l_relocated)
97 GL(dl_init_static_tls) (map);
98 else
99 map->l_need_tls_init = 1;
102 /* Initialize static TLS area and DTV for current (only) thread.
103 libpthread implementations should provide their own hook
104 to handle all threads. */
105 void
106 _dl_nothread_init_static_tls (struct link_map *map)
108 # if TLS_TCB_AT_TP
109 void *dest = (char *) THREAD_SELF - map->l_tls_offset;
110 # elif TLS_DTV_AT_TP
111 void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
112 # else
113 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
114 # endif
116 /* Fill in the DTV slot so that a later LD/GD access will find it. */
117 dtv_t *dtv = THREAD_DTV ();
118 assert (map->l_tls_modid <= dtv[-1].counter);
119 dtv[map->l_tls_modid].pointer = dest;
121 /* Initialize the memory. */
122 memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
123 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
125 #endif
128 void
129 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
130 int lazy, int consider_profiling)
132 struct textrels
134 caddr_t start;
135 size_t len;
136 int prot;
137 struct textrels *next;
138 } *textrels = NULL;
139 /* Initialize it to make the compiler happy. */
140 const char *errstring = NULL;
142 if (l->l_relocated)
143 return;
145 /* If DT_BIND_NOW is set relocate all references in this object. We
146 do not do this if we are profiling, of course. */
147 if (!consider_profiling
148 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
149 lazy = 0;
151 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_RELOC, 0))
152 _dl_debug_printf ("\nrelocation processing: %s%s\n",
153 l->l_name[0] ? l->l_name : rtld_progname,
154 lazy ? " (lazy)" : "");
156 /* DT_TEXTREL is now in level 2 and might phase out at some time.
157 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
158 testing easier and therefore it will be available at all time. */
159 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
161 /* Bletch. We must make read-only segments writable
162 long enough to relocate them. */
163 const ElfW(Phdr) *ph;
164 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
165 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
167 struct textrels *newp;
169 newp = (struct textrels *) alloca (sizeof (*newp));
170 newp->len = (((ph->p_vaddr + ph->p_memsz + GLRO(dl_pagesize) - 1)
171 & ~(GLRO(dl_pagesize) - 1))
172 - (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
173 newp->start = ((ph->p_vaddr & ~(GLRO(dl_pagesize) - 1))
174 + (caddr_t) l->l_addr);
176 if (__mprotect (newp->start, newp->len, PROT_READ|PROT_WRITE) < 0)
178 errstring = N_("cannot make segment writable for relocation");
179 call_error:
180 _dl_signal_error (errno, l->l_name, NULL, errstring);
183 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
184 newp->prot = (PF_TO_PROT
185 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
186 #else
187 newp->prot = 0;
188 if (ph->p_flags & PF_R)
189 newp->prot |= PROT_READ;
190 if (ph->p_flags & PF_W)
191 newp->prot |= PROT_WRITE;
192 if (ph->p_flags & PF_X)
193 newp->prot |= PROT_EXEC;
194 #endif
195 newp->next = textrels;
196 textrels = newp;
201 /* Do the actual relocation of the object's GOT and other data. */
203 /* String table object symbols. */
204 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
206 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
207 #define RESOLVE_MAP(ref, version, r_type) \
208 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
209 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
210 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
211 ? (bump_num_cache_relocations (), \
212 (*ref) = l->l_lookup_cache.ret, \
213 l->l_lookup_cache.value) \
214 : ({ lookup_t _lr; \
215 int _tc = elf_machine_type_class (r_type); \
216 l->l_lookup_cache.type_class = _tc; \
217 l->l_lookup_cache.sym = (*ref); \
218 const struct r_found_version *v = NULL; \
219 int flags = DL_LOOKUP_ADD_DEPENDENCY; \
220 if ((version) != NULL && (version)->hash != 0) \
222 v = (version); \
223 flags = 0; \
225 _lr = _dl_lookup_symbol_x (strtab + (*ref)->st_name, l, (ref), \
226 scope, v, _tc, flags, NULL); \
227 l->l_lookup_cache.ret = (*ref); \
228 l->l_lookup_cache.value = _lr; })) \
229 : l)
230 #define RESOLVE(ref, version, r_type) \
231 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
232 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
233 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
234 ? (bump_num_cache_relocations (), \
235 (*ref) = l->l_lookup_cache.ret, \
236 l->l_lookup_cache.value) \
237 : ({ lookup_t _lr; \
238 int _tc = elf_machine_type_class (r_type); \
239 l->l_lookup_cache.type_class = _tc; \
240 l->l_lookup_cache.sym = (*ref); \
241 const struct r_found_version *v = NULL; \
242 int flags = DL_LOOKUP_ADD_DEPENDENCY; \
243 if ((version) != NULL && (version)->hash != 0) \
245 v = (version); \
246 flags = 0; \
248 _lr = _dl_lookup_symbol_x (strtab + (*ref)->st_name, l, (ref), \
249 scope, v, _tc, flags, NULL); \
250 l->l_lookup_cache.ret = (*ref); \
251 l->l_lookup_cache.value = _lr; })) \
252 : l->l_addr)
254 /* This macro is used as a callback from elf_machine_rel{a,} when a
255 static TLS reloc is about to be performed. Since (in dl-load.c) we
256 permit dynamic loading of objects that might use such relocs, we
257 have to check whether each use is actually doable. If the object
258 whose TLS segment the reference resolves to was allocated space in
259 the static TLS block at startup, then it's ok. Otherwise, we make
260 an attempt to allocate it in surplus space on the fly. If that
261 can't be done, we fall back to the error that DF_STATIC_TLS is
262 intended to produce. */
263 #define CHECK_STATIC_TLS(map, sym_map) \
264 do { \
265 if (__builtin_expect ((sym_map)->l_tls_offset == NO_TLS_OFFSET, 0)) \
266 _dl_allocate_static_tls (sym_map); \
267 } while (0)
269 #include "dynamic-link.h"
271 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
273 if (__builtin_expect (consider_profiling, 0))
275 /* Allocate the array which will contain the already found
276 relocations. If the shared object lacks a PLT (for example
277 if it only contains lead function) the l_info[DT_PLTRELSZ]
278 will be NULL. */
279 if (l->l_info[DT_PLTRELSZ] == NULL)
281 errstring = N_("%s: profiler found no PLTREL in object %s\n");
282 fatal:
283 _dl_fatal_printf (errstring,
284 rtld_progname ?: "<program name unknown>",
285 l->l_name);
288 l->l_reloc_result =
289 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
290 l->l_info[DT_PLTRELSZ]->d_un.d_val);
291 if (l->l_reloc_result == NULL)
293 errstring = N_("\
294 %s: profiler out of memory shadowing PLTREL of %s\n");
295 goto fatal;
300 /* Mark the object so we know this work has been done. */
301 l->l_relocated = 1;
303 /* Undo the segment protection changes. */
304 while (__builtin_expect (textrels != NULL, 0))
306 if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0)
308 errstring = N_("cannot restore segment prot after reloc");
309 goto call_error;
312 textrels = textrels->next;
315 /* In case we can protect the data now that the relocations are
316 done, do it. */
317 if (l->l_relro_size != 0)
318 _dl_protect_relro (l);
322 void internal_function
323 _dl_protect_relro (struct link_map *l)
325 ElfW(Addr) start = ((l->l_addr + l->l_relro_addr)
326 & ~(GLRO(dl_pagesize) - 1));
327 ElfW(Addr) end = ((l->l_addr + l->l_relro_addr + l->l_relro_size)
328 & ~(GLRO(dl_pagesize) - 1));
330 if (start != end
331 && __mprotect ((void *) start, end - start, PROT_READ) < 0)
333 static const char errstring[] = N_("\
334 cannot apply additional memory protection after relocation");
335 _dl_signal_error (errno, l->l_name, NULL, errstring);
339 void
340 internal_function __attribute_noinline__
341 _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt)
343 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
344 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
346 /* XXX We cannot translate these messages. */
347 static const char msg[2][32
348 #if __ELF_NATIVE_CLASS == 64
350 #endif
351 ] = { "unexpected reloc type 0x",
352 "unexpected PLT reloc type 0x" };
353 char msgbuf[sizeof (msg[0])];
354 char *cp;
356 cp = __stpcpy (msgbuf, msg[plt]);
357 #if __ELF_NATIVE_CLASS == 64
358 if (__builtin_expect(type > 0xff, 0))
360 *cp++ = DIGIT (type >> 28);
361 *cp++ = DIGIT (type >> 24);
362 *cp++ = DIGIT (type >> 20);
363 *cp++ = DIGIT (type >> 16);
364 *cp++ = DIGIT (type >> 12);
365 *cp++ = DIGIT (type >> 8);
367 #endif
368 *cp++ = DIGIT (type >> 4);
369 *cp++ = DIGIT (type);
370 *cp = '\0';
372 _dl_signal_error (0, map->l_name, NULL, msgbuf);