* elf/dl-reloc.c (_dl_allocate_static_tls): Add internal_function.
[glibc.git] / elf / dl-reloc.c
blob5d8dc0d6e16cd6c9d6d28e5beb4d47f140633032
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.
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 void
45 internal_function __attribute_noinline__
46 _dl_allocate_static_tls (struct link_map *map)
48 size_t offset, used, check;
50 # if TLS_TCB_AT_TP
51 offset = roundup (GL(dl_tls_static_used) + map->l_tls_blocksize,
52 map->l_tls_align);
53 used = offset;
54 check = offset + TLS_TCB_SIZE;
55 # elif TLS_DTV_AT_TP
56 offset = roundup (GL(dl_tls_static_used), map->l_tls_align);
57 used = offset + map->l_tls_blocksize;
58 check = used;
59 /* dl_tls_static_used includes the TCB at the beginning. */
60 # else
61 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
62 # endif
64 if (check > GL(dl_tls_static_size))
66 const char *errstring = N_("\
67 shared object cannot be dlopen()ed: static TLS memory too small");
68 INTUSE(_dl_signal_error) (0, (map)->l_name, NULL, errstring);
71 map->l_tls_offset = offset;
72 GL(dl_tls_static_used) = used;
74 #endif
77 void
78 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
79 int lazy, int consider_profiling)
81 struct textrels
83 caddr_t start;
84 size_t len;
85 int prot;
86 struct textrels *next;
87 } *textrels = NULL;
88 /* Initialize it to make the compiler happy. */
89 const char *errstring = NULL;
91 if (l->l_relocated)
92 return;
94 /* If DT_BIND_NOW is set relocate all references in this object. We
95 do not do this if we are profiling, of course. */
96 if (!consider_profiling
97 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
98 lazy = 0;
100 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_RELOC, 0))
101 INTUSE(_dl_debug_printf) ("\nrelocation processing: %s%s\n",
102 l->l_name[0] ? l->l_name : rtld_progname,
103 lazy ? " (lazy)" : "");
105 /* DT_TEXTREL is now in level 2 and might phase out at some time.
106 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
107 testing easier and therefore it will be available at all time. */
108 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
110 /* Bletch. We must make read-only segments writable
111 long enough to relocate them. */
112 const ElfW(Phdr) *ph;
113 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
114 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
116 struct textrels *newp;
118 newp = (struct textrels *) alloca (sizeof (*newp));
119 newp->len = (((ph->p_vaddr + ph->p_memsz + GL(dl_pagesize) - 1)
120 & ~(GL(dl_pagesize) - 1))
121 - (ph->p_vaddr & ~(GL(dl_pagesize) - 1)));
122 newp->start = ((ph->p_vaddr & ~(GL(dl_pagesize) - 1))
123 + (caddr_t) l->l_addr);
125 if (__mprotect (newp->start, newp->len, PROT_READ|PROT_WRITE) < 0)
127 errstring = N_("cannot make segment writable for relocation");
128 call_error:
129 INTUSE(_dl_signal_error) (errno, l->l_name, NULL, errstring);
132 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
133 newp->prot = (PF_TO_PROT
134 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
135 #else
136 newp->prot = 0;
137 if (ph->p_flags & PF_R)
138 newp->prot |= PROT_READ;
139 if (ph->p_flags & PF_W)
140 newp->prot |= PROT_WRITE;
141 if (ph->p_flags & PF_X)
142 newp->prot |= PROT_EXEC;
143 #endif
144 newp->next = textrels;
145 textrels = newp;
150 /* Do the actual relocation of the object's GOT and other data. */
152 /* String table object symbols. */
153 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
155 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
156 #define RESOLVE_MAP(ref, version, r_type) \
157 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
158 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
159 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
160 ? (bump_num_cache_relocations (), \
161 (*ref) = l->l_lookup_cache.ret, \
162 l->l_lookup_cache.value) \
163 : ({ lookup_t _lr; \
164 int _tc = elf_machine_type_class (r_type); \
165 l->l_lookup_cache.type_class = _tc; \
166 l->l_lookup_cache.sym = (*ref); \
167 _lr = ((version) != NULL && (version)->hash != 0 \
168 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
169 + (*ref)->st_name, \
170 l, (ref), scope, \
171 (version), _tc, 0) \
172 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
173 (ref), scope, _tc, \
174 DL_LOOKUP_ADD_DEPENDENCY)); \
175 l->l_lookup_cache.ret = (*ref); \
176 l->l_lookup_cache.value = _lr; })) \
177 : l)
178 #define RESOLVE(ref, version, r_type) \
179 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
180 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
181 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
182 ? (bump_num_cache_relocations (), \
183 (*ref) = l->l_lookup_cache.ret, \
184 l->l_lookup_cache.value) \
185 : ({ lookup_t _lr; \
186 int _tc = elf_machine_type_class (r_type); \
187 l->l_lookup_cache.type_class = _tc; \
188 l->l_lookup_cache.sym = (*ref); \
189 _lr = ((version) != NULL && (version)->hash != 0 \
190 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
191 + (*ref)->st_name, \
192 l, (ref), scope, \
193 (version), _tc, 0) \
194 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
195 (ref), scope, _tc, \
196 DL_LOOKUP_ADD_DEPENDENCY)); \
197 l->l_lookup_cache.ret = (*ref); \
198 l->l_lookup_cache.value = _lr; })) \
199 : l->l_addr)
201 /* This macro is used as a callback from elf_machine_rel{a,} when a
202 static TLS reloc is about to be performed. Since (in dl-load.c) we
203 permit dynamic loading of objects that might use such relocs, we
204 have to check whether each use is actually doable. If the object
205 whose TLS segment the reference resolves to was allocated space in
206 the static TLS block at startup, then it's ok. Otherwise, we make
207 an attempt to allocate it in surplus space on the fly. If that
208 can't be done, we fall back to the error that DF_STATIC_TLS is
209 intended to produce. */
210 #define CHECK_STATIC_TLS(map, sym_map) \
211 do { \
212 if (__builtin_expect ((sym_map)->l_tls_offset == 0, 0)) \
213 _dl_allocate_static_tls (sym_map); \
214 } while (0)
216 #include "dynamic-link.h"
218 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
220 if (__builtin_expect (consider_profiling, 0))
222 /* Allocate the array which will contain the already found
223 relocations. If the shared object lacks a PLT (for example
224 if it only contains lead function) the l_info[DT_PLTRELSZ]
225 will be NULL. */
226 if (l->l_info[DT_PLTRELSZ] == NULL)
228 errstring = N_("%s: profiler found no PLTREL in object %s\n");
229 fatal:
230 _dl_fatal_printf (errstring,
231 rtld_progname ?: "<program name unknown>",
232 l->l_name);
235 l->l_reloc_result =
236 (ElfW(Addr) *) calloc (sizeof (ElfW(Addr)),
237 l->l_info[DT_PLTRELSZ]->d_un.d_val);
238 if (l->l_reloc_result == NULL)
240 errstring = N_("\
241 %s: profiler out of memory shadowing PLTREL of %s\n");
242 goto fatal;
247 /* Mark the object so we know this work has been done. */
248 l->l_relocated = 1;
250 /* Undo the segment protection changes. */
251 while (__builtin_expect (textrels != NULL, 0))
253 if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0)
255 errstring = N_("cannot restore segment prot after reloc");
256 goto call_error;
259 textrels = textrels->next;
262 INTDEF (_dl_relocate_object)
265 void
266 internal_function __attribute_noinline__
267 _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt)
269 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
270 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
272 /* XXX We cannot translate these messages. */
273 static const char msg[2][32
274 #if __ELF_NATIVE_CLASS == 64
276 #endif
277 ] = { "unexpected reloc type 0x",
278 "unexpected PLT reloc type 0x" };
279 char msgbuf[sizeof (msg[0])];
280 char *cp;
282 cp = __stpcpy (msgbuf, msg[plt]);
283 #if __ELF_NATIVE_CLASS == 64
284 if (__builtin_expect(type > 0xff, 0))
286 *cp++ = DIGIT (type >> 28);
287 *cp++ = DIGIT (type >> 24);
288 *cp++ = DIGIT (type >> 20);
289 *cp++ = DIGIT (type >> 16);
290 *cp++ = DIGIT (type >> 12);
291 *cp++ = DIGIT (type >> 8);
293 #endif
294 *cp++ = DIGIT (type >> 4);
295 *cp++ = DIGIT (type);
296 *cp = '\0';
298 INTUSE(_dl_signal_error) (0, map->l_name, NULL, msgbuf);