1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995-2004, 2005 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
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include "dynamic-link.h"
30 /* Statistics function. */
32 # define bump_num_cache_relocations() ++GL(dl_num_cache_relocations)
34 # define bump_num_cache_relocations() ((void) 0)
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. */
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))
55 _dl_signal_error (0, map->l_name, NULL, N_("\
56 cannot allocate memory in static TLS block"));
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)
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;
80 size_t offset = roundup (GL(dl_tls_static_used), map->l_tls_align);
81 used = offset + map->l_tls_blocksize;
83 /* dl_tls_static_used includes the TCB at the beginning. */
85 if (check > GL(dl_tls_static_size))
88 map->l_tls_offset = offset;
89 GL(dl_tls_static_used) = used;
91 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
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)
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);
106 GL(dl_init_static_tls) (map);
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. */
116 _dl_nothread_init_static_tls (struct link_map *map)
119 void *dest = (char *) THREAD_SELF - map->l_tls_offset;
121 void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
123 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
126 /* Fill in the DTV slot so that a later LD/GD access will find it. */
127 dtv_t *dtv = THREAD_DTV ();
128 _dl_printf("modid=%u, counter=%u\n", map->l_tls_modid, dtv[-1].counter);
129 assert (map->l_tls_modid <= dtv[-1].counter);
130 dtv[map->l_tls_modid].pointer.val = dest;
131 dtv[map->l_tls_modid].pointer.is_static = true;
133 /* Initialize the memory. */
134 memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
135 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
141 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
142 int lazy, int consider_profiling)
149 struct textrels *next;
151 /* Initialize it to make the compiler happy. */
152 const char *errstring = NULL;
155 /* If we are auditing, install the same handlers we need for profiling. */
156 consider_profiling |= GLRO(dl_audit) != NULL;
162 /* If DT_BIND_NOW is set relocate all references in this object. We
163 do not do this if we are profiling, of course. */
164 // XXX Correct for auditing?
165 if (!consider_profiling
166 && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0))
169 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_RELOC, 0))
170 _dl_debug_printf ("\nrelocation processing: %s%s\n",
171 l->l_name[0] ? l->l_name : rtld_progname,
172 lazy ? " (lazy)" : "");
174 /* DT_TEXTREL is now in level 2 and might phase out at some time.
175 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
176 testing easier and therefore it will be available at all time. */
177 if (__builtin_expect (l->l_info[DT_TEXTREL] != NULL, 0))
179 /* Bletch. We must make read-only segments writable
180 long enough to relocate them. */
181 const ElfW(Phdr) *ph;
182 for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
183 if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0)
185 struct textrels *newp;
187 newp = (struct textrels *) alloca (sizeof (*newp));
188 newp->len = (((ph->p_vaddr + ph->p_memsz + GLRO(dl_pagesize) - 1)
189 & ~(GLRO(dl_pagesize) - 1))
190 - (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
191 newp->start = ((ph->p_vaddr & ~(GLRO(dl_pagesize) - 1))
192 + (caddr_t) l->l_addr);
194 if (__mprotect (newp->start, newp->len, PROT_READ|PROT_WRITE) < 0)
196 errstring = N_("cannot make segment writable for relocation");
198 _dl_signal_error (errno, l->l_name, NULL, errstring);
201 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
202 newp->prot = (PF_TO_PROT
203 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
206 if (ph->p_flags & PF_R)
207 newp->prot |= PROT_READ;
208 if (ph->p_flags & PF_W)
209 newp->prot |= PROT_WRITE;
210 if (ph->p_flags & PF_X)
211 newp->prot |= PROT_EXEC;
213 newp->next = textrels;
219 /* Do the actual relocation of the object's GOT and other data. */
221 /* String table object symbols. */
222 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
224 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
225 #define RESOLVE_MAP(ref, version, r_type) \
226 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
227 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
228 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
229 ? (bump_num_cache_relocations (), \
230 (*ref) = l->l_lookup_cache.ret, \
231 l->l_lookup_cache.value) \
233 int _tc = elf_machine_type_class (r_type); \
234 l->l_lookup_cache.type_class = _tc; \
235 l->l_lookup_cache.sym = (*ref); \
236 const struct r_found_version *v = NULL; \
237 int flags = DL_LOOKUP_ADD_DEPENDENCY; \
238 if ((version) != NULL && (version)->hash != 0) \
243 _lr = _dl_lookup_symbol_x (strtab + (*ref)->st_name, l, (ref), \
244 scope, v, _tc, flags, NULL); \
245 l->l_lookup_cache.ret = (*ref); \
246 l->l_lookup_cache.value = _lr; })) \
249 /* This macro is used as a callback from elf_machine_rel{a,} when a
250 static TLS reloc is about to be performed. Since (in dl-load.c) we
251 permit dynamic loading of objects that might use such relocs, we
252 have to check whether each use is actually doable. If the object
253 whose TLS segment the reference resolves to was allocated space in
254 the static TLS block at startup, then it's ok. Otherwise, we make
255 an attempt to allocate it in surplus space on the fly. If that
256 can't be done, we fall back to the error that DF_STATIC_TLS is
257 intended to produce. */
258 #define CHECK_STATIC_TLS(map, sym_map) \
260 if (__builtin_expect ((sym_map)->l_tls_offset == NO_TLS_OFFSET, 0)) \
261 _dl_allocate_static_tls (sym_map); \
264 #include "dynamic-link.h"
266 ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling);
268 if (__builtin_expect (consider_profiling, 0))
270 /* Allocate the array which will contain the already found
271 relocations. If the shared object lacks a PLT (for example
272 if it only contains lead function) the l_info[DT_PLTRELSZ]
274 if (l->l_info[DT_PLTRELSZ] == NULL)
276 errstring = N_("%s: no PLTREL found in object %s\n");
278 _dl_fatal_printf (errstring,
279 rtld_progname ?: "<program name unknown>",
283 l->l_reloc_result = calloc (sizeof (l->l_reloc_result[0]),
284 l->l_info[DT_PLTRELSZ]->d_un.d_val);
285 if (l->l_reloc_result == NULL)
288 %s: out of memory to store relocation results for %s\n");
294 /* Mark the object so we know this work has been done. */
297 /* Undo the segment protection changes. */
298 while (__builtin_expect (textrels != NULL, 0))
300 if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0)
302 errstring = N_("cannot restore segment prot after reloc");
306 textrels = textrels->next;
309 /* In case we can protect the data now that the relocations are
311 if (l->l_relro_size != 0)
312 _dl_protect_relro (l);
316 void internal_function
317 _dl_protect_relro (struct link_map *l)
319 ElfW(Addr) start = ((l->l_addr + l->l_relro_addr)
320 & ~(GLRO(dl_pagesize) - 1));
321 ElfW(Addr) end = ((l->l_addr + l->l_relro_addr + l->l_relro_size)
322 & ~(GLRO(dl_pagesize) - 1));
325 && __mprotect ((void *) start, end - start, PROT_READ) < 0)
327 static const char errstring[] = N_("\
328 cannot apply additional memory protection after relocation");
329 _dl_signal_error (errno, l->l_name, NULL, errstring);
334 internal_function __attribute_noinline__
335 _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt)
337 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
338 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
340 /* XXX We cannot translate these messages. */
341 static const char msg[2][32
342 #if __ELF_NATIVE_CLASS == 64
345 ] = { "unexpected reloc type 0x",
346 "unexpected PLT reloc type 0x" };
347 char msgbuf[sizeof (msg[0])];
350 cp = __stpcpy (msgbuf, msg[plt]);
351 #if __ELF_NATIVE_CLASS == 64
352 if (__builtin_expect(type > 0xff, 0))
354 *cp++ = DIGIT (type >> 28);
355 *cp++ = DIGIT (type >> 24);
356 *cp++ = DIGIT (type >> 20);
357 *cp++ = DIGIT (type >> 16);
358 *cp++ = DIGIT (type >> 12);
359 *cp++ = DIGIT (type >> 8);
362 *cp++ = DIGIT (type >> 4);
363 *cp++ = DIGIT (type);
366 _dl_signal_error (0, map->l_name, NULL, msgbuf);