2005-01-28 Martin Schwidefsky <schwidefsky@de.ibm.com>
[glibc.git] / elf / dl-reloc.c
blobb680683198bf49dfde52e09e3f0422c35494d15c
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 /* If the alignment requirements are too high fail. */
52 if (map->l_tls_align > GL(dl_tls_static_align))
54 fail:
55 _dl_signal_error (0, map->l_name, NULL, N_("\
56 cannot allocate memory in static TLS block"));
59 # if TLS_TCB_AT_TP
60 size_t freebytes;
61 size_t n;
62 size_t blsize;
64 freebytes = GL(dl_tls_static_size) - GL(dl_tls_static_used) - TLS_TCB_SIZE;
66 blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
67 if (freebytes < blsize)
68 goto fail;
70 n = (freebytes - blsize) / map->l_tls_align;
72 size_t offset = GL(dl_tls_static_used) + (freebytes - n * map->l_tls_align
73 - map->l_tls_firstbyte_offset);
75 map->l_tls_offset = GL(dl_tls_static_used) = offset;
76 # elif TLS_DTV_AT_TP
77 size_t used;
78 size_t check;
80 size_t offset = roundup (GL(dl_tls_static_used), map->l_tls_align);
81 used = offset + map->l_tls_blocksize;
82 check = used;
83 /* dl_tls_static_used includes the TCB at the beginning. */
85 if (check > GL(dl_tls_static_size))
86 goto fail;
88 map->l_tls_offset = offset;
89 GL(dl_tls_static_used) = used;
90 # else
91 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
92 # endif
94 /* If the object is not yet relocated we cannot initialize the
95 static TLS region. Delay it. */
96 if (map->l_real->l_relocated)
98 #ifdef SHARED
99 if (__builtin_expect (THREAD_DTV()[0].counter != GL(dl_tls_generation),
101 /* Update the slot information data for at least the generation of
102 the DSO we are allocating data for. */
103 (void) _dl_update_slotinfo (map->l_tls_modid);
104 #endif
106 GL(dl_init_static_tls) (map);
108 else
109 map->l_need_tls_init = 1;
112 /* Initialize static TLS area and DTV for current (only) thread.
113 libpthread implementations should provide their own hook
114 to handle all threads. */
115 void
116 _dl_nothread_init_static_tls (struct link_map *map)
118 # if TLS_TCB_AT_TP
119 void *dest = (char *) THREAD_SELF - map->l_tls_offset;
120 # elif TLS_DTV_AT_TP
121 void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
122 # else
123 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
124 # endif
126 /* Fill in the DTV slot so that a later LD/GD access will find it. */
127 THREAD_DTV ()[map->l_tls_modid].pointer.val = dest;
128 THREAD_DTV ()[map->l_tls_modid].pointer.is_static = true;
130 /* Initialize the memory. */
131 memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
132 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
134 #endif
137 void
138 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
139 int lazy, int consider_profiling)
141 struct textrels
143 caddr_t start;
144 size_t len;
145 int prot;
146 struct textrels *next;
147 } *textrels = NULL;
148 /* Initialize it to make the compiler happy. */
149 const char *errstring = NULL;
151 #ifdef SHARED
152 /* If we are auditing, install the same handlers we need for profiling. */
153 consider_profiling |= GLRO(dl_audit) != NULL;
154 #endif
156 if (l->l_relocated)
157 return;
159 /* If DT_BIND_NOW is set relocate all references in this object. We
160 do not do this if we are profiling, of course. */
161 // XXX Correct for auditing?
162 if (!consider_profiling
163 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
164 lazy = 0;
166 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_RELOC, 0))
167 _dl_debug_printf ("\nrelocation processing: %s%s\n",
168 l->l_name[0] ? l->l_name : rtld_progname,
169 lazy ? " (lazy)" : "");
171 /* DT_TEXTREL is now in level 2 and might phase out at some time.
172 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
173 testing easier and therefore it will be available at all time. */
174 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
176 /* Bletch. We must make read-only segments writable
177 long enough to relocate them. */
178 const ElfW(Phdr) *ph;
179 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
180 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
182 struct textrels *newp;
184 newp = (struct textrels *) alloca (sizeof (*newp));
185 newp->len = (((ph->p_vaddr + ph->p_memsz + GLRO(dl_pagesize) - 1)
186 & ~(GLRO(dl_pagesize) - 1))
187 - (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
188 newp->start = ((ph->p_vaddr & ~(GLRO(dl_pagesize) - 1))
189 + (caddr_t) l->l_addr);
191 if (__mprotect (newp->start, newp->len, PROT_READ|PROT_WRITE) < 0)
193 errstring = N_("cannot make segment writable for relocation");
194 call_error:
195 _dl_signal_error (errno, l->l_name, NULL, errstring);
198 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
199 newp->prot = (PF_TO_PROT
200 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
201 #else
202 newp->prot = 0;
203 if (ph->p_flags & PF_R)
204 newp->prot |= PROT_READ;
205 if (ph->p_flags & PF_W)
206 newp->prot |= PROT_WRITE;
207 if (ph->p_flags & PF_X)
208 newp->prot |= PROT_EXEC;
209 #endif
210 newp->next = textrels;
211 textrels = newp;
216 /* Do the actual relocation of the object's GOT and other data. */
218 /* String table object symbols. */
219 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
221 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
222 #define RESOLVE_MAP(ref, version, r_type) \
223 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
224 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
225 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
226 ? (bump_num_cache_relocations (), \
227 (*ref) = l->l_lookup_cache.ret, \
228 l->l_lookup_cache.value) \
229 : ({ lookup_t _lr; \
230 int _tc = elf_machine_type_class (r_type); \
231 l->l_lookup_cache.type_class = _tc; \
232 l->l_lookup_cache.sym = (*ref); \
233 const struct r_found_version *v = NULL; \
234 int flags = DL_LOOKUP_ADD_DEPENDENCY; \
235 if ((version) != NULL && (version)->hash != 0) \
237 v = (version); \
238 flags = 0; \
240 _lr = _dl_lookup_symbol_x (strtab + (*ref)->st_name, l, (ref), \
241 scope, v, _tc, flags, NULL); \
242 l->l_lookup_cache.ret = (*ref); \
243 l->l_lookup_cache.value = _lr; })) \
244 : l)
246 /* This macro is used as a callback from elf_machine_rel{a,} when a
247 static TLS reloc is about to be performed. Since (in dl-load.c) we
248 permit dynamic loading of objects that might use such relocs, we
249 have to check whether each use is actually doable. If the object
250 whose TLS segment the reference resolves to was allocated space in
251 the static TLS block at startup, then it's ok. Otherwise, we make
252 an attempt to allocate it in surplus space on the fly. If that
253 can't be done, we fall back to the error that DF_STATIC_TLS is
254 intended to produce. */
255 #define CHECK_STATIC_TLS(map, sym_map) \
256 do { \
257 if (__builtin_expect ((sym_map)->l_tls_offset == NO_TLS_OFFSET, 0)) \
258 _dl_allocate_static_tls (sym_map); \
259 } while (0)
261 #include "dynamic-link.h"
263 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
265 if (__builtin_expect (consider_profiling, 0))
267 /* Allocate the array which will contain the already found
268 relocations. If the shared object lacks a PLT (for example
269 if it only contains lead function) the l_info[DT_PLTRELSZ]
270 will be NULL. */
271 if (l->l_info[DT_PLTRELSZ] == NULL)
273 errstring = N_("%s: no PLTREL found in object %s\n");
274 fatal:
275 _dl_fatal_printf (errstring,
276 rtld_progname ?: "<program name unknown>",
277 l->l_name);
280 l->l_reloc_result = calloc (sizeof (l->l_reloc_result[0]),
281 l->l_info[DT_PLTRELSZ]->d_un.d_val);
282 if (l->l_reloc_result == NULL)
284 errstring = N_("\
285 %s: out of memory to store relocation results for %s\n");
286 goto fatal;
291 /* Mark the object so we know this work has been done. */
292 l->l_relocated = 1;
294 /* Undo the segment protection changes. */
295 while (__builtin_expect (textrels != NULL, 0))
297 if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0)
299 errstring = N_("cannot restore segment prot after reloc");
300 goto call_error;
303 textrels = textrels->next;
306 /* In case we can protect the data now that the relocations are
307 done, do it. */
308 if (l->l_relro_size != 0)
309 _dl_protect_relro (l);
313 void internal_function
314 _dl_protect_relro (struct link_map *l)
316 ElfW(Addr) start = ((l->l_addr + l->l_relro_addr)
317 & ~(GLRO(dl_pagesize) - 1));
318 ElfW(Addr) end = ((l->l_addr + l->l_relro_addr + l->l_relro_size)
319 & ~(GLRO(dl_pagesize) - 1));
321 if (start != end
322 && __mprotect ((void *) start, end - start, PROT_READ) < 0)
324 static const char errstring[] = N_("\
325 cannot apply additional memory protection after relocation");
326 _dl_signal_error (errno, l->l_name, NULL, errstring);
330 void
331 internal_function __attribute_noinline__
332 _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt)
334 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
335 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
337 /* XXX We cannot translate these messages. */
338 static const char msg[2][32
339 #if __ELF_NATIVE_CLASS == 64
341 #endif
342 ] = { "unexpected reloc type 0x",
343 "unexpected PLT reloc type 0x" };
344 char msgbuf[sizeof (msg[0])];
345 char *cp;
347 cp = __stpcpy (msgbuf, msg[plt]);
348 #if __ELF_NATIVE_CLASS == 64
349 if (__builtin_expect(type > 0xff, 0))
351 *cp++ = DIGIT (type >> 28);
352 *cp++ = DIGIT (type >> 24);
353 *cp++ = DIGIT (type >> 20);
354 *cp++ = DIGIT (type >> 16);
355 *cp++ = DIGIT (type >> 12);
356 *cp++ = DIGIT (type >> 8);
358 #endif
359 *cp++ = DIGIT (type >> 4);
360 *cp++ = DIGIT (type);
361 *cp = '\0';
363 _dl_signal_error (0, map->l_name, NULL, msgbuf);