1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995-2006, 2008-2010, 2011 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, see
17 <http://www.gnu.org/licenses/>. */
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include "dynamic-link.h"
29 /* Statistics function. */
31 # define bump_num_cache_relocations() ++GL(dl_num_cache_relocations)
33 # define bump_num_cache_relocations() ((void) 0)
37 /* We are trying to perform a static TLS relocation in MAP, but it was
38 dynamically loaded. This can only work if there is enough surplus in
39 the static TLS area already allocated for each running thread. If this
40 object's TLS segment is too big to fit, we fail. If it fits,
41 we set MAP->l_tls_offset and return.
42 This function intentionally does not return any value but signals error
43 directly, as static TLS should be rare and code handling it should
44 not be inlined as much as possible. */
47 _dl_try_allocate_static_tls (struct link_map
*map
)
49 /* If we've already used the variable with dynamic access, or if the
50 alignment requirements are too high, fail. */
51 if (map
->l_tls_offset
== FORCED_DYNAMIC_TLS_OFFSET
52 || map
->l_tls_align
> GL(dl_tls_static_align
))
59 size_t freebytes
= GL(dl_tls_static_size
) - GL(dl_tls_static_used
);
60 if (freebytes
< TLS_TCB_SIZE
)
62 freebytes
-= TLS_TCB_SIZE
;
64 size_t blsize
= map
->l_tls_blocksize
+ map
->l_tls_firstbyte_offset
;
65 if (freebytes
< blsize
)
68 size_t n
= (freebytes
- blsize
) / map
->l_tls_align
;
70 size_t offset
= GL(dl_tls_static_used
) + (freebytes
- n
* map
->l_tls_align
71 - map
->l_tls_firstbyte_offset
);
73 map
->l_tls_offset
= GL(dl_tls_static_used
) = offset
;
75 /* dl_tls_static_used includes the TCB at the beginning. */
76 size_t offset
= (((GL(dl_tls_static_used
)
77 - map
->l_tls_firstbyte_offset
78 + map
->l_tls_align
- 1) & -map
->l_tls_align
)
79 + map
->l_tls_firstbyte_offset
);
80 size_t used
= offset
+ map
->l_tls_blocksize
;
82 if (used
> GL(dl_tls_static_size
))
85 map
->l_tls_offset
= offset
;
86 map
->l_tls_firstbyte_offset
= GL(dl_tls_static_used
);
87 GL(dl_tls_static_used
) = used
;
89 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
92 /* If the object is not yet relocated we cannot initialize the
93 static TLS region. Delay it. */
94 if (map
->l_real
->l_relocated
)
97 if (__builtin_expect (THREAD_DTV()[0].counter
!= GL(dl_tls_generation
),
99 /* Update the slot information data for at least the generation of
100 the DSO we are allocating data for. */
101 (void) _dl_update_slotinfo (map
->l_tls_modid
);
104 GL(dl_init_static_tls
) (map
);
107 map
->l_need_tls_init
= 1;
113 internal_function __attribute_noinline__
114 _dl_allocate_static_tls (struct link_map
*map
)
116 if (map
->l_tls_offset
== FORCED_DYNAMIC_TLS_OFFSET
117 || _dl_try_allocate_static_tls (map
))
119 _dl_signal_error (0, map
->l_name
, NULL
, N_("\
120 cannot allocate memory in static TLS block"));
124 /* Initialize static TLS area and DTV for current (only) thread.
125 libpthread implementations should provide their own hook
126 to handle all threads. */
128 _dl_nothread_init_static_tls (struct link_map
*map
)
131 void *dest
= (char *) THREAD_SELF
- map
->l_tls_offset
;
133 void *dest
= (char *) THREAD_SELF
+ map
->l_tls_offset
+ TLS_PRE_TCB_SIZE
;
135 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
138 /* Fill in the DTV slot so that a later LD/GD access will find it. */
139 dtv_t
*dtv
= THREAD_DTV ();
140 assert (map
->l_tls_modid
<= dtv
[-1].counter
);
141 dtv
[map
->l_tls_modid
].pointer
.val
= dest
;
142 dtv
[map
->l_tls_modid
].pointer
.is_static
= true;
144 /* Initialize the memory. */
145 memset (__mempcpy (dest
, map
->l_tls_initimage
, map
->l_tls_initimage_size
),
146 '\0', map
->l_tls_blocksize
- map
->l_tls_initimage_size
);
151 _dl_relocate_object (struct link_map
*l
, struct r_scope_elem
*scope
[],
152 int reloc_mode
, int consider_profiling
)
159 struct textrels
*next
;
161 /* Initialize it to make the compiler happy. */
162 const char *errstring
= NULL
;
163 int lazy
= reloc_mode
& RTLD_LAZY
;
164 int skip_ifunc
= reloc_mode
& __RTLD_NOIFUNC
;
167 /* If we are auditing, install the same handlers we need for profiling. */
168 if ((reloc_mode
& __RTLD_AUDIT
) == 0)
169 consider_profiling
|= GLRO(dl_audit
) != NULL
;
171 /* Never use dynamic linker profiling for gprof profiling code. */
172 # define consider_profiling 0
178 /* If DT_BIND_NOW is set relocate all references in this object. We
179 do not do this if we are profiling, of course. */
180 // XXX Correct for auditing?
181 if (!consider_profiling
182 && __builtin_expect (l
->l_info
[DT_BIND_NOW
] != NULL
, 0))
185 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_RELOC
, 0))
186 _dl_debug_printf ("\nrelocation processing: %s%s\n",
187 l
->l_name
[0] ? l
->l_name
: rtld_progname
,
188 lazy
? " (lazy)" : "");
190 /* DT_TEXTREL is now in level 2 and might phase out at some time.
191 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
192 testing easier and therefore it will be available at all time. */
193 if (__builtin_expect (l
->l_info
[DT_TEXTREL
] != NULL
, 0))
195 /* Bletch. We must make read-only segments writable
196 long enough to relocate them. */
197 const ElfW(Phdr
) *ph
;
198 for (ph
= l
->l_phdr
; ph
< &l
->l_phdr
[l
->l_phnum
]; ++ph
)
199 if (ph
->p_type
== PT_LOAD
&& (ph
->p_flags
& PF_W
) == 0)
201 struct textrels
*newp
;
203 newp
= (struct textrels
*) alloca (sizeof (*newp
));
204 newp
->len
= (((ph
->p_vaddr
+ ph
->p_memsz
+ GLRO(dl_pagesize
) - 1)
205 & ~(GLRO(dl_pagesize
) - 1))
206 - (ph
->p_vaddr
& ~(GLRO(dl_pagesize
) - 1)));
207 newp
->start
= ((ph
->p_vaddr
& ~(GLRO(dl_pagesize
) - 1))
208 + (caddr_t
) l
->l_addr
);
210 if (__mprotect (newp
->start
, newp
->len
, PROT_READ
|PROT_WRITE
) < 0)
212 errstring
= N_("cannot make segment writable for relocation");
214 _dl_signal_error (errno
, l
->l_name
, NULL
, errstring
);
217 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
218 newp
->prot
= (PF_TO_PROT
219 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
222 if (ph
->p_flags
& PF_R
)
223 newp
->prot
|= PROT_READ
;
224 if (ph
->p_flags
& PF_W
)
225 newp
->prot
|= PROT_WRITE
;
226 if (ph
->p_flags
& PF_X
)
227 newp
->prot
|= PROT_EXEC
;
229 newp
->next
= textrels
;
235 /* Do the actual relocation of the object's GOT and other data. */
237 /* String table object symbols. */
238 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
240 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
241 #define RESOLVE_MAP(ref, version, r_type) \
242 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
243 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
244 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
245 ? (bump_num_cache_relocations (), \
246 (*ref) = l->l_lookup_cache.ret, \
247 l->l_lookup_cache.value) \
249 int _tc = elf_machine_type_class (r_type); \
250 l->l_lookup_cache.type_class = _tc; \
251 l->l_lookup_cache.sym = (*ref); \
252 const struct r_found_version *v = NULL; \
253 if ((version) != NULL && (version)->hash != 0) \
255 _lr = _dl_lookup_symbol_x (strtab + (*ref)->st_name, l, (ref), \
257 DL_LOOKUP_ADD_DEPENDENCY, NULL); \
258 l->l_lookup_cache.ret = (*ref); \
259 l->l_lookup_cache.value = _lr; })) \
262 #include "dynamic-link.h"
264 ELF_DYNAMIC_RELOCATE (l
, lazy
, consider_profiling
, skip_ifunc
);
267 if (__builtin_expect (consider_profiling
, 0))
269 /* Allocate the array which will contain the already found
270 relocations. If the shared object lacks a PLT (for example
271 if it only contains lead function) the l_info[DT_PLTRELSZ]
273 if (l
->l_info
[DT_PLTRELSZ
] == NULL
)
275 errstring
= N_("%s: no PLTREL found in object %s\n");
277 _dl_fatal_printf (errstring
,
278 rtld_progname
?: "<program name unknown>",
282 l
->l_reloc_result
= calloc (sizeof (l
->l_reloc_result
[0]),
283 l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
284 if (l
->l_reloc_result
== NULL
)
287 %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");
307 CLEAR_CACHE (textrels
->start
, textrels
->start
+ textrels
->len
);
310 textrels
= textrels
->next
;
313 /* In case we can protect the data now that the relocations are
315 if (l
->l_relro_size
!= 0)
316 _dl_protect_relro (l
);
320 void internal_function
321 _dl_protect_relro (struct link_map
*l
)
323 ElfW(Addr
) start
= ((l
->l_addr
+ l
->l_relro_addr
)
324 & ~(GLRO(dl_pagesize
) - 1));
325 ElfW(Addr
) end
= ((l
->l_addr
+ l
->l_relro_addr
+ l
->l_relro_size
)
326 & ~(GLRO(dl_pagesize
) - 1));
329 && __mprotect ((void *) start
, end
- start
, PROT_READ
) < 0)
331 static const char errstring
[] = N_("\
332 cannot apply additional memory protection after relocation");
333 _dl_signal_error (errno
, l
->l_name
, NULL
, errstring
);
338 internal_function __attribute_noinline__
339 _dl_reloc_bad_type (struct link_map
*map
, unsigned int type
, int plt
)
341 extern const char INTUSE(_itoa_lower_digits
)[] attribute_hidden
;
342 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
344 /* XXX We cannot translate these messages. */
345 static const char msg
[2][32
346 #if __ELF_NATIVE_CLASS == 64
349 ] = { "unexpected reloc type 0x",
350 "unexpected PLT reloc type 0x" };
351 char msgbuf
[sizeof (msg
[0])];
354 cp
= __stpcpy (msgbuf
, msg
[plt
]);
355 #if __ELF_NATIVE_CLASS == 64
356 if (__builtin_expect(type
> 0xff, 0))
358 *cp
++ = DIGIT (type
>> 28);
359 *cp
++ = DIGIT (type
>> 24);
360 *cp
++ = DIGIT (type
>> 20);
361 *cp
++ = DIGIT (type
>> 16);
362 *cp
++ = DIGIT (type
>> 12);
363 *cp
++ = DIGIT (type
>> 8);
366 *cp
++ = DIGIT (type
>> 4);
367 *cp
++ = DIGIT (type
);
370 _dl_signal_error (0, map
->l_name
, NULL
, msgbuf
);