]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Run time dynamic linker. |
11bf311e | 2 | Copyright (C) 1995-2002,2003,2004,2005,2006 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 AJ |
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. */ | |
d66e34cd | 19 | |
7d0b1164 | 20 | #include <errno.h> |
154d10bd | 21 | #include <dlfcn.h> |
7dea968e | 22 | #include <fcntl.h> |
164a7164 | 23 | #include <stdbool.h> |
d66e34cd | 24 | #include <stdlib.h> |
f51d1dfd | 25 | #include <string.h> |
d66e34cd | 26 | #include <unistd.h> |
2064087b | 27 | #include <sys/mman.h> /* Check if MAP_ANON is defined. */ |
af8bf6bd | 28 | #include <sys/param.h> |
ba9fcb3f | 29 | #include <sys/stat.h> |
a42195db | 30 | #include <ldsodefs.h> |
ce37fa88 | 31 | #include <stdio-common/_itoa.h> |
f21acc89 | 32 | #include <entry.h> |
c94a8080 | 33 | #include <fpu_control.h> |
db276fa1 | 34 | #include <hp-timing.h> |
cf197e41 | 35 | #include <bits/libc-lock.h> |
f5348425 | 36 | #include "dynamic-link.h" |
8f480b4b | 37 | #include <dl-librecon.h> |
74955460 | 38 | #include <unsecvars.h> |
5688da55 | 39 | #include <dl-cache.h> |
2f4db0df | 40 | #include <dl-osinfo.h> |
5688da55 | 41 | #include <dl-procinfo.h> |
5f5843e3 | 42 | #include <tls.h> |
f5348425 | 43 | |
a853022c | 44 | #include <assert.h> |
f5348425 | 45 | |
6ce3881d RM |
46 | /* Avoid PLT use for our local calls at startup. */ |
47 | extern __typeof (__mempcpy) __mempcpy attribute_hidden; | |
48 | ||
49 | /* GCC has mental blocks about _exit. */ | |
50 | extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden; | |
51 | #define _exit exit_internal | |
52 | ||
fd26970f | 53 | /* Helper function to handle errors while resolving symbols. */ |
c84142e8 UD |
54 | static void print_unresolved (int errcode, const char *objname, |
55 | const char *errsting); | |
56 | ||
57 | /* Helper function to handle errors when a version is missing. */ | |
58 | static void print_missing_version (int errcode, const char *objname, | |
59 | const char *errsting); | |
fd26970f | 60 | |
db276fa1 | 61 | /* Print the various times we collected. */ |
392a6b52 | 62 | static void print_statistics (hp_timing_t *total_timep); |
ea278354 | 63 | |
74780cf6 UD |
64 | /* Add audit objects. */ |
65 | static void process_dl_audit (char *str); | |
66 | ||
ea278354 UD |
67 | /* This is a list of all the modes the dynamic loader can be in. */ |
68 | enum mode { normal, list, verify, trace }; | |
69 | ||
70 | /* Process all environments variables the dynamic linker must recognize. | |
71 | Since all of them start with `LD_' we are a bit smarter while finding | |
72 | all the entries. */ | |
ba9fcb3f | 73 | static void process_envvars (enum mode *modep); |
ea278354 | 74 | |
11986c68 | 75 | #ifdef DL_ARGV_NOT_RELRO |
22aa06a5 | 76 | int _dl_argc attribute_hidden; |
11986c68 | 77 | char **_dl_argv = NULL; |
22aa06a5 RM |
78 | /* Nonzero if we were run directly. */ |
79 | unsigned int _dl_skip_args attribute_hidden; | |
11986c68 | 80 | #else |
22aa06a5 | 81 | int _dl_argc attribute_relro attribute_hidden; |
697afbe1 | 82 | char **_dl_argv attribute_relro = NULL; |
22aa06a5 | 83 | unsigned int _dl_skip_args attribute_relro attribute_hidden; |
11986c68 | 84 | #endif |
e6caf4e1 | 85 | INTDEF(_dl_argv) |
5c82e15e | 86 | |
35f1e827 UD |
87 | #ifndef THREAD_SET_STACK_GUARD |
88 | /* Only exported for architectures that don't store the stack guard canary | |
89 | in thread local area. */ | |
90 | uintptr_t __stack_chk_guard attribute_relro; | |
91 | #endif | |
92 | ||
827b7087 UD |
93 | /* Only exported for architectures that don't store the pointer guard |
94 | value in thread local area. */ | |
95 | uintptr_t __pointer_chk_guard_local | |
96 | attribute_relro attribute_hidden __attribute__ ((nocommon)); | |
97 | #ifndef THREAD_SET_POINTER_GUARD | |
98 | strong_alias (__pointer_chk_guard_local, __pointer_chk_guard) | |
99 | #endif | |
100 | ||
101 | ||
9dcafc55 UD |
102 | /* List of auditing DSOs. */ |
103 | static struct audit_list | |
104 | { | |
105 | const char *name; | |
106 | struct audit_list *next; | |
107 | } *audit_list; | |
108 | ||
ce6e047f | 109 | #ifndef HAVE_INLINED_SYSCALLS |
39778c6c UD |
110 | /* Set nonzero during loading and initialization of executable and |
111 | libraries, cleared before the executable's entry point runs. This | |
112 | must not be initialized to nonzero, because the unused dynamic | |
113 | linker loaded in for libc.so's "ld.so.1" dep will provide the | |
114 | definition seen by libc.so's initializer; that value must be zero, | |
115 | and will be since that dynamic linker's _dl_start and dl_main will | |
116 | never be called. */ | |
e6caf4e1 UD |
117 | int _dl_starting_up = 0; |
118 | INTVARDEF(_dl_starting_up) | |
ce6e047f | 119 | #endif |
39778c6c | 120 | |
d6b5d570 UD |
121 | /* This is the structure which defines all variables global to ld.so |
122 | (except those which cannot be added for some reason). */ | |
5688da55 UD |
123 | struct rtld_global _rtld_global = |
124 | { | |
ecdeaac0 RM |
125 | /* Default presumption without further information is executable stack. */ |
126 | ._dl_stack_flags = PF_R|PF_W|PF_X, | |
ffa8d2a0 | 127 | #ifdef _LIBC_REENTRANT |
d3c9f895 | 128 | ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER |
ffa8d2a0 | 129 | #endif |
5688da55 | 130 | }; |
27a754a9 UD |
131 | /* If we would use strong_alias here the compiler would see a |
132 | non-hidden definition. This would undo the effect of the previous | |
133 | declaration. So spell out was strong_alias does plus add the | |
134 | visibility attribute. */ | |
135 | extern struct rtld_global _rtld_local | |
136 | __attribute__ ((alias ("_rtld_global"), visibility ("hidden"))); | |
c0fb8a56 | 137 | |
afdca0f2 UD |
138 | |
139 | /* This variable is similar to _rtld_local, but all values are | |
140 | read-only after relocation. */ | |
141 | struct rtld_global_ro _rtld_global_ro attribute_relro = | |
142 | { | |
c31e278f UD |
143 | /* Get architecture specific initializer. */ |
144 | #include <dl-procinfo.c> | |
afdca0f2 UD |
145 | #ifdef NEED_DL_SYSINFO |
146 | ._dl_sysinfo = DL_SYSINFO_DEFAULT, | |
147 | #endif | |
dd70526e | 148 | ._dl_debug_fd = STDERR_FILENO, |
afdca0f2 UD |
149 | ._dl_use_load_bias = -2, |
150 | ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID, | |
151 | ._dl_hwcap_mask = HWCAP_IMPORTANT, | |
152 | ._dl_lazy = 1, | |
153 | ._dl_fpu_control = _FPU_DEFAULT, | |
827b7087 | 154 | ._dl_pointer_guard = 1, |
154d10bd UD |
155 | |
156 | /* Function pointers. */ | |
154d10bd UD |
157 | ._dl_debug_printf = _dl_debug_printf, |
158 | ._dl_catch_error = _dl_catch_error, | |
159 | ._dl_signal_error = _dl_signal_error, | |
154d10bd | 160 | ._dl_mcount = _dl_mcount_internal, |
021723ab | 161 | ._dl_lookup_symbol_x = _dl_lookup_symbol_x, |
9dcafc55 UD |
162 | ._dl_check_caller = _dl_check_caller, |
163 | ._dl_open = _dl_open, | |
164 | ._dl_close = _dl_close | |
afdca0f2 UD |
165 | }; |
166 | /* If we would use strong_alias here the compiler would see a | |
167 | non-hidden definition. This would undo the effect of the previous | |
168 | declaration. So spell out was strong_alias does plus add the | |
169 | visibility attribute. */ | |
170 | extern struct rtld_global_ro _rtld_local_ro | |
171 | __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden"))); | |
172 | ||
173 | ||
67ddea92 | 174 | static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum, |
266180eb | 175 | ElfW(Addr) *user_entry); |
d66e34cd | 176 | |
392a6b52 | 177 | /* These two variables cannot be moved into .data.rel.ro. */ |
d6b5d570 UD |
178 | static struct libname_list _dl_rtld_libname; |
179 | static struct libname_list _dl_rtld_libname2; | |
86d2c878 | 180 | |
eaad82e0 UD |
181 | /* We expect less than a second for relocation. */ |
182 | #ifdef HP_SMALL_TIMING_AVAIL | |
183 | # undef HP_TIMING_AVAIL | |
184 | # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL | |
185 | #endif | |
186 | ||
db276fa1 | 187 | /* Variable for statistics. */ |
5732c4df | 188 | #ifndef HP_TIMING_NONAVAIL |
db276fa1 | 189 | static hp_timing_t relocate_time; |
392a6b52 UD |
190 | static hp_timing_t load_time attribute_relro; |
191 | static hp_timing_t start_time attribute_relro; | |
5732c4df | 192 | #endif |
db276fa1 | 193 | |
2a76f7ef UD |
194 | /* Additional definitions needed by TLS initialization. */ |
195 | #ifdef TLS_INIT_HELPER | |
196 | TLS_INIT_HELPER | |
5e289179 UD |
197 | #endif |
198 | ||
199 | /* Helper function for syscall implementation. */ | |
200 | #ifdef DL_SYSINFO_IMPLEMENTATION | |
201 | DL_SYSINFO_IMPLEMENTATION | |
2a76f7ef UD |
202 | #endif |
203 | ||
01d8e36d UD |
204 | /* Before ld.so is relocated we must not access variables which need |
205 | relocations. This means variables which are exported. Variables | |
206 | declared as static are fine. If we can mark a variable hidden this | |
27a754a9 | 207 | is fine, too. The latter is important here. We can avoid setting |
01d8e36d UD |
208 | up a temporary link map for ld.so if we can mark _rtld_global as |
209 | hidden. */ | |
11bf311e | 210 | #ifdef PI_STATIC_AND_HIDDEN |
01d8e36d UD |
211 | # define DONT_USE_BOOTSTRAP_MAP 1 |
212 | #endif | |
213 | ||
214 | #ifdef DONT_USE_BOOTSTRAP_MAP | |
215 | static ElfW(Addr) _dl_start_final (void *arg); | |
216 | #else | |
4874b009 RM |
217 | struct dl_start_final_info |
218 | { | |
219 | struct link_map l; | |
220 | #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE | |
221 | hp_timing_t start_time; | |
222 | #endif | |
223 | }; | |
01d8e36d | 224 | static ElfW(Addr) _dl_start_final (void *arg, |
4874b009 | 225 | struct dl_start_final_info *info); |
01d8e36d | 226 | #endif |
6a1db4ff | 227 | |
65da9563 RM |
228 | /* These defined magically in the linker script. */ |
229 | extern char _begin[] attribute_hidden; | |
eec8b6ca | 230 | extern char _etext[] attribute_hidden; |
65da9563 RM |
231 | extern char _end[] attribute_hidden; |
232 | ||
233 | ||
b1dbbaa4 RM |
234 | #ifdef RTLD_START |
235 | RTLD_START | |
236 | #else | |
eaad82e0 | 237 | # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START" |
b1dbbaa4 RM |
238 | #endif |
239 | ||
c2248c44 RM |
240 | #ifndef VALIDX |
241 | # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ | |
242 | + DT_EXTRANUM + DT_VALTAGIDX (tag)) | |
243 | #endif | |
244 | #ifndef ADDRIDX | |
245 | # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ | |
246 | + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag)) | |
247 | #endif | |
248 | ||
249 | /* This is the second half of _dl_start (below). It can be inlined safely | |
250 | under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT | |
251 | references. When the tools don't permit us to avoid using a GOT entry | |
252 | for _dl_rtld_global (no attribute_hidden support), we must make sure | |
253 | this function is not inlined (see below). */ | |
254 | ||
255 | #ifdef DONT_USE_BOOTSTRAP_MAP | |
256 | static inline ElfW(Addr) __attribute__ ((always_inline)) | |
257 | _dl_start_final (void *arg) | |
258 | #else | |
259 | static ElfW(Addr) __attribute__ ((noinline)) | |
4874b009 | 260 | _dl_start_final (void *arg, struct dl_start_final_info *info) |
c2248c44 RM |
261 | #endif |
262 | { | |
263 | ElfW(Addr) start_addr; | |
c2248c44 RM |
264 | |
265 | if (HP_TIMING_AVAIL) | |
266 | { | |
267 | /* If it hasn't happen yet record the startup time. */ | |
268 | if (! HP_TIMING_INLINE) | |
269 | HP_TIMING_NOW (start_time); | |
735d67f2 | 270 | #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL |
4874b009 RM |
271 | else |
272 | start_time = info->start_time; | |
273 | #endif | |
c2248c44 RM |
274 | |
275 | /* Initialize the timing functions. */ | |
276 | HP_TIMING_DIFF_INIT (); | |
277 | } | |
278 | ||
279 | /* Transfer data about ourselves to the permanent link_map structure. */ | |
280 | #ifndef DONT_USE_BOOTSTRAP_MAP | |
4874b009 RM |
281 | GL(dl_rtld_map).l_addr = info->l.l_addr; |
282 | GL(dl_rtld_map).l_ld = info->l.l_ld; | |
283 | memcpy (GL(dl_rtld_map).l_info, info->l.l_info, | |
c2248c44 | 284 | sizeof GL(dl_rtld_map).l_info); |
4874b009 | 285 | GL(dl_rtld_map).l_mach = info->l.l_mach; |
82221992 | 286 | GL(dl_rtld_map).l_relocated = 1; |
c2248c44 RM |
287 | #endif |
288 | _dl_setup_hash (&GL(dl_rtld_map)); | |
c0f62c56 | 289 | GL(dl_rtld_map).l_real = &GL(dl_rtld_map); |
c2248c44 RM |
290 | GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin; |
291 | GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end; | |
eec8b6ca | 292 | GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext; |
c2248c44 | 293 | /* Copy the TLS related data if necessary. */ |
11bf311e | 294 | #ifndef DONT_USE_BOOTSTRAP_MAP |
ce460d04 | 295 | # if USE___THREAD |
4874b009 | 296 | assert (info->l.l_tls_modid != 0); |
ce460d04 RM |
297 | GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize; |
298 | GL(dl_rtld_map).l_tls_align = info->l.l_tls_align; | |
99fe3b0e | 299 | GL(dl_rtld_map).l_tls_firstbyte_offset = info->l.l_tls_firstbyte_offset; |
ce460d04 RM |
300 | GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size; |
301 | GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage; | |
302 | GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset; | |
303 | GL(dl_rtld_map).l_tls_modid = 1; | |
c2248c44 | 304 | # else |
299601a1 UD |
305 | # if NO_TLS_OFFSET != 0 |
306 | GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET; | |
307 | # endif | |
c2248c44 | 308 | # endif |
ce460d04 | 309 | |
c2248c44 RM |
310 | #endif |
311 | ||
312 | #if HP_TIMING_AVAIL | |
313 | HP_TIMING_NOW (GL(dl_cpuclock_offset)); | |
314 | #endif | |
315 | ||
ea4f25a7 UD |
316 | /* Initialize the stack end variable. */ |
317 | __libc_stack_end = __builtin_frame_address (0); | |
318 | ||
c2248c44 RM |
319 | /* Call the OS-dependent function to set up life so we can do things like |
320 | file access. It will call `dl_main' (below) to do all the real work | |
321 | of the dynamic linker, and then unwind our frame and run the user | |
322 | entry point on the same stack we entered on. */ | |
ecdeaac0 | 323 | start_addr = _dl_sysdep_start (arg, &dl_main); |
c2248c44 RM |
324 | |
325 | #ifndef HP_TIMING_NONAVAIL | |
392a6b52 | 326 | hp_timing_t rtld_total_time; |
c2248c44 RM |
327 | if (HP_TIMING_AVAIL) |
328 | { | |
329 | hp_timing_t end_time; | |
330 | ||
331 | /* Get the current time. */ | |
332 | HP_TIMING_NOW (end_time); | |
333 | ||
334 | /* Compute the difference. */ | |
335 | HP_TIMING_DIFF (rtld_total_time, start_time, end_time); | |
336 | } | |
337 | #endif | |
338 | ||
afdca0f2 | 339 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0)) |
39b04aa3 UD |
340 | { |
341 | #ifndef HP_TIMING_NONAVAIL | |
342 | print_statistics (&rtld_total_time); | |
343 | #else | |
344 | print_statistics (NULL); | |
345 | #endif | |
346 | } | |
c2248c44 RM |
347 | |
348 | return start_addr; | |
349 | } | |
350 | ||
50746436 | 351 | static ElfW(Addr) __attribute_used__ internal_function |
d66e34cd RM |
352 | _dl_start (void *arg) |
353 | { | |
01d8e36d UD |
354 | #ifdef DONT_USE_BOOTSTRAP_MAP |
355 | # define bootstrap_map GL(dl_rtld_map) | |
356 | #else | |
4874b009 RM |
357 | struct dl_start_final_info info; |
358 | # define bootstrap_map info.l | |
739d440d | 359 | #endif |
d66e34cd | 360 | |
b1dbbaa4 RM |
361 | /* This #define produces dynamic linking inline functions for |
362 | bootstrap relocation instead of general-purpose relocation. */ | |
363 | #define RTLD_BOOTSTRAP | |
c0282c06 UD |
364 | #define RESOLVE_MAP(sym, version, flags) \ |
365 | ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map) | |
b1dbbaa4 RM |
366 | #include "dynamic-link.h" |
367 | ||
db276fa1 | 368 | if (HP_TIMING_INLINE && HP_TIMING_AVAIL) |
4874b009 | 369 | #ifdef DONT_USE_BOOTSTRAP_MAP |
db276fa1 | 370 | HP_TIMING_NOW (start_time); |
4874b009 RM |
371 | #else |
372 | HP_TIMING_NOW (info.start_time); | |
373 | #endif | |
db276fa1 | 374 | |
e66d0a4c UD |
375 | /* Partly clean the `bootstrap_map' structure up. Don't use |
376 | `memset' since it might not be built in or inlined and we cannot | |
377 | make function calls at this point. Use '__builtin_memset' if we | |
01d8e36d UD |
378 | know it is available. We do not have to clear the memory if we |
379 | do not have to use the temporary bootstrap_map. Global variables | |
380 | are initialized to zero by default. */ | |
381 | #ifndef DONT_USE_BOOTSTRAP_MAP | |
382 | # ifdef HAVE_BUILTIN_MEMSET | |
e66d0a4c | 383 | __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info)); |
01d8e36d | 384 | # else |
ce460d04 | 385 | for (size_t cnt = 0; |
264ec183 UD |
386 | cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]); |
387 | ++cnt) | |
388 | bootstrap_map.l_info[cnt] = 0; | |
01d8e36d | 389 | # endif |
154619b5 UD |
390 | # if USE___THREAD |
391 | bootstrap_map.l_tls_modid = 0; | |
392 | # endif | |
e66d0a4c | 393 | #endif |
264ec183 | 394 | |
d66e34cd | 395 | /* Figure out the run-time load address of the dynamic linker itself. */ |
86d2c878 | 396 | bootstrap_map.l_addr = elf_machine_load_address (); |
d66e34cd | 397 | |
47707456 UD |
398 | /* Read our own dynamic section and fill in the info array. */ |
399 | bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); | |
479aa8ec | 400 | elf_get_dynamic_info (&bootstrap_map, NULL); |
d66e34cd | 401 | |
11bf311e | 402 | #if NO_TLS_OFFSET != 0 |
299601a1 UD |
403 | bootstrap_map.l_tls_offset = NO_TLS_OFFSET; |
404 | #endif | |
405 | ||
65da9563 RM |
406 | /* Get the dynamic linker's own program header. First we need the ELF |
407 | file header. The `_begin' symbol created by the linker script points | |
408 | to it. When we have something like GOTOFF relocs, we can use a plain | |
409 | reference to find the runtime address. Without that, we have to rely | |
410 | on the `l_addr' value, which is not the value we want when prelinked. */ | |
ed20b3d9 | 411 | #if USE___THREAD |
5f5843e3 | 412 | dtv_t initdtv[3]; |
ce460d04 RM |
413 | ElfW(Ehdr) *ehdr |
414 | # ifdef DONT_USE_BOOTSTRAP_MAP | |
415 | = (ElfW(Ehdr) *) &_begin; | |
416 | # else | |
e8ed861d | 417 | # error This will not work with prelink. |
ce460d04 RM |
418 | = (ElfW(Ehdr) *) bootstrap_map.l_addr; |
419 | # endif | |
420 | ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff); | |
421 | size_t cnt = ehdr->e_phnum; /* PT_TLS is usually the last phdr. */ | |
422 | while (cnt-- > 0) | |
739d440d UD |
423 | if (phdr[cnt].p_type == PT_TLS) |
424 | { | |
425 | void *tlsblock; | |
426 | size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align); | |
f884de5b | 427 | char *p; |
739d440d UD |
428 | |
429 | bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz; | |
430 | bootstrap_map.l_tls_align = phdr[cnt].p_align; | |
99fe3b0e UD |
431 | if (phdr[cnt].p_align == 0) |
432 | bootstrap_map.l_tls_firstbyte_offset = 0; | |
433 | else | |
434 | bootstrap_map.l_tls_firstbyte_offset = (phdr[cnt].p_vaddr | |
435 | & (phdr[cnt].p_align - 1)); | |
739d440d UD |
436 | assert (bootstrap_map.l_tls_blocksize != 0); |
437 | bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz; | |
438 | bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr | |
a816b435 | 439 | + phdr[cnt].p_vaddr); |
739d440d UD |
440 | |
441 | /* We can now allocate the initial TLS block. This can happen | |
442 | on the stack. We'll get the final memory later when we | |
443 | know all about the various objects loaded at startup | |
444 | time. */ | |
445 | # if TLS_TCB_AT_TP | |
446 | tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize, | |
447 | TLS_INIT_TCB_ALIGN) | |
448 | + TLS_INIT_TCB_SIZE | |
449 | + max_align); | |
450 | # elif TLS_DTV_AT_TP | |
451 | tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE, | |
452 | bootstrap_map.l_tls_align) | |
453 | + bootstrap_map.l_tls_blocksize | |
454 | + max_align); | |
455 | # else | |
456 | /* In case a model with a different layout for the TCB and DTV | |
457 | is defined add another #elif here and in the following #ifs. */ | |
458 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
459 | # endif | |
460 | /* Align the TLS block. */ | |
461 | tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1) | |
462 | & ~(max_align - 1)); | |
463 | ||
464 | /* Initialize the dtv. [0] is the length, [1] the generation | |
465 | counter. */ | |
466 | initdtv[0].counter = 1; | |
467 | initdtv[1].counter = 0; | |
468 | ||
469 | /* Initialize the TLS block. */ | |
470 | # if TLS_TCB_AT_TP | |
471 | initdtv[2].pointer = tlsblock; | |
472 | # elif TLS_DTV_AT_TP | |
473 | bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE, | |
474 | bootstrap_map.l_tls_align); | |
475 | initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset; | |
476 | # else | |
477 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
478 | # endif | |
f884de5b UD |
479 | p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage, |
480 | bootstrap_map.l_tls_initimage_size); | |
481 | # ifdef HAVE_BUILTIN_MEMSET | |
482 | __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize | |
483 | - bootstrap_map.l_tls_initimage_size)); | |
484 | # else | |
485 | { | |
486 | size_t remaining = (bootstrap_map.l_tls_blocksize | |
487 | - bootstrap_map.l_tls_initimage_size); | |
488 | while (remaining-- > 0) | |
489 | *p++ = '\0'; | |
490 | } | |
9dcafc55 | 491 | # endif |
739d440d UD |
492 | |
493 | /* Install the pointer to the dtv. */ | |
494 | ||
495 | /* Initialize the thread pointer. */ | |
496 | # if TLS_TCB_AT_TP | |
497 | bootstrap_map.l_tls_offset | |
498 | = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN); | |
499 | ||
500 | INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset, | |
501 | initdtv); | |
502 | ||
fde89ad0 RM |
503 | const char *lossage = TLS_INIT_TP ((char *) tlsblock |
504 | + bootstrap_map.l_tls_offset, 0); | |
739d440d UD |
505 | # elif TLS_DTV_AT_TP |
506 | INSTALL_DTV (tlsblock, initdtv); | |
fde89ad0 | 507 | const char *lossage = TLS_INIT_TP (tlsblock, 0); |
739d440d UD |
508 | # else |
509 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
510 | # endif | |
fde89ad0 RM |
511 | if (__builtin_expect (lossage != NULL, 0)) |
512 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", | |
513 | lossage); | |
739d440d UD |
514 | |
515 | /* So far this is module number one. */ | |
516 | bootstrap_map.l_tls_modid = 1; | |
e8ed861d UD |
517 | |
518 | /* There can only be one PT_TLS entry. */ | |
519 | break; | |
739d440d | 520 | } |
ce460d04 | 521 | #endif /* USE___THREAD */ |
739d440d | 522 | |
d66e34cd | 523 | #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC |
86d2c878 | 524 | ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info); |
d66e34cd RM |
525 | #endif |
526 | ||
32e6df36 UD |
527 | if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)]) |
528 | { | |
529 | /* Relocate ourselves so we can do normal function calls and | |
530 | data access using the global offset table. */ | |
531 | ||
532 | ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0); | |
533 | } | |
f85f3563 | 534 | bootstrap_map.l_relocated = 1; |
421f82e5 | 535 | |
ea7eb7e3 UD |
536 | /* Please note that we don't allow profiling of this object and |
537 | therefore need not test whether we have to allocate the array | |
538 | for the relocation results (as done in dl-reloc.c). */ | |
421f82e5 | 539 | |
d66e34cd RM |
540 | /* Now life is sane; we can call functions and access global data. |
541 | Set up to use the operating system facilities, and find out from | |
542 | the operating system's program loader where to find the program | |
6a1db4ff UD |
543 | header table in core. Put the rest of _dl_start into a separate |
544 | function, that way the compiler cannot put accesses to the GOT | |
545 | before ELF_DYNAMIC_RELOCATE. */ | |
c0282c06 | 546 | { |
01d8e36d UD |
547 | #ifdef DONT_USE_BOOTSTRAP_MAP |
548 | ElfW(Addr) entry = _dl_start_final (arg); | |
549 | #else | |
4874b009 | 550 | ElfW(Addr) entry = _dl_start_final (arg, &info); |
01d8e36d | 551 | #endif |
c0282c06 UD |
552 | |
553 | #ifndef ELF_MACHINE_START_ADDRESS | |
554 | # define ELF_MACHINE_START_ADDRESS(map, start) (start) | |
555 | #endif | |
556 | ||
7cb92a99 | 557 | return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry); |
c0282c06 | 558 | } |
6a1db4ff UD |
559 | } |
560 | ||
561 | ||
d66e34cd | 562 | |
d66e34cd RM |
563 | /* Now life is peachy; we can do all normal operations. |
564 | On to the real work. */ | |
565 | ||
993b3242 UD |
566 | /* Some helper functions. */ |
567 | ||
568 | /* Arguments to relocate_doit. */ | |
569 | struct relocate_args | |
570 | { | |
571 | struct link_map *l; | |
572 | int lazy; | |
573 | }; | |
574 | ||
575 | struct map_args | |
576 | { | |
577 | /* Argument to map_doit. */ | |
578 | char *str; | |
f04b9a68 UD |
579 | struct link_map *loader; |
580 | int is_preloaded; | |
581 | int mode; | |
993b3242 | 582 | /* Return value of map_doit. */ |
f04b9a68 | 583 | struct link_map *map; |
993b3242 UD |
584 | }; |
585 | ||
9dcafc55 UD |
586 | struct dlmopen_args |
587 | { | |
588 | const char *fname; | |
589 | struct link_map *map; | |
590 | }; | |
591 | ||
592 | struct lookup_args | |
593 | { | |
594 | const char *name; | |
595 | struct link_map *map; | |
596 | void *result; | |
597 | }; | |
598 | ||
993b3242 UD |
599 | /* Arguments to version_check_doit. */ |
600 | struct version_check_args | |
601 | { | |
993b3242 | 602 | int doexit; |
145b8413 | 603 | int dotrace; |
993b3242 UD |
604 | }; |
605 | ||
606 | static void | |
607 | relocate_doit (void *a) | |
608 | { | |
609 | struct relocate_args *args = (struct relocate_args *) a; | |
610 | ||
c0a777e8 | 611 | _dl_relocate_object (args->l, args->l->l_scope, args->lazy, 0); |
993b3242 UD |
612 | } |
613 | ||
614 | static void | |
615 | map_doit (void *a) | |
616 | { | |
be935610 | 617 | struct map_args *args = (struct map_args *) a; |
154d10bd | 618 | args->map = _dl_map_object (args->loader, args->str, |
c0f62c56 UD |
619 | args->is_preloaded, lt_library, 0, args->mode, |
620 | LM_ID_BASE); | |
993b3242 UD |
621 | } |
622 | ||
9dcafc55 UD |
623 | static void |
624 | dlmopen_doit (void *a) | |
625 | { | |
626 | struct dlmopen_args *args = (struct dlmopen_args *) a; | |
627 | args->map = _dl_open (args->fname, RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT, | |
628 | dl_main, LM_ID_NEWLM, _dl_argc, INTUSE(_dl_argv), | |
629 | __environ); | |
630 | } | |
631 | ||
632 | static void | |
633 | lookup_doit (void *a) | |
634 | { | |
635 | struct lookup_args *args = (struct lookup_args *) a; | |
636 | const ElfW(Sym) *ref = NULL; | |
637 | args->result = NULL; | |
638 | lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref, | |
639 | args->map->l_local_scope, NULL, 0, | |
640 | DL_LOOKUP_RETURN_NEWEST, NULL); | |
641 | if (ref != NULL) | |
642 | args->result = DL_SYMBOL_ADDRESS (l, ref); | |
643 | } | |
644 | ||
993b3242 UD |
645 | static void |
646 | version_check_doit (void *a) | |
647 | { | |
be935610 | 648 | struct version_check_args *args = (struct version_check_args *) a; |
c0f62c56 UD |
649 | if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1, |
650 | args->dotrace) && args->doexit) | |
993b3242 UD |
651 | /* We cannot start the application. Abort now. */ |
652 | _exit (1); | |
653 | } | |
654 | ||
ce37fa88 UD |
655 | |
656 | static inline struct link_map * | |
657 | find_needed (const char *name) | |
658 | { | |
c0f62c56 UD |
659 | struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist; |
660 | unsigned int n = scope->r_nlist; | |
ce37fa88 | 661 | |
be935610 | 662 | while (n-- > 0) |
c0f62c56 UD |
663 | if (_dl_name_match_p (name, scope->r_list[n])) |
664 | return scope->r_list[n]; | |
ce37fa88 UD |
665 | |
666 | /* Should never happen. */ | |
667 | return NULL; | |
668 | } | |
669 | ||
670 | static int | |
671 | match_version (const char *string, struct link_map *map) | |
672 | { | |
a42195db | 673 | const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
ce37fa88 UD |
674 | ElfW(Verdef) *def; |
675 | ||
b0982c4a | 676 | #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF)) |
ce37fa88 UD |
677 | if (map->l_info[VERDEFTAG] == NULL) |
678 | /* The file has no symbol versioning. */ | |
679 | return 0; | |
680 | ||
681 | def = (ElfW(Verdef) *) ((char *) map->l_addr | |
682 | + map->l_info[VERDEFTAG]->d_un.d_ptr); | |
683 | while (1) | |
684 | { | |
685 | ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux); | |
686 | ||
687 | /* Compare the version strings. */ | |
688 | if (strcmp (string, strtab + aux->vda_name) == 0) | |
689 | /* Bingo! */ | |
690 | return 1; | |
691 | ||
692 | /* If no more definitions we failed to find what we want. */ | |
693 | if (def->vd_next == 0) | |
694 | break; | |
695 | ||
696 | /* Next definition. */ | |
697 | def = (ElfW(Verdef) *) ((char *) def + def->vd_next); | |
698 | } | |
699 | ||
700 | return 0; | |
701 | } | |
702 | ||
9dcafc55 UD |
703 | static bool tls_init_tp_called; |
704 | ||
705 | static void * | |
706 | init_tls (void) | |
707 | { | |
708 | /* Number of elements in the static TLS block. */ | |
709 | GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx); | |
710 | ||
711 | /* Do not do this twice. The audit interface might have required | |
712 | the DTV interfaces to be set up early. */ | |
713 | if (GL(dl_initial_dtv) != NULL) | |
714 | return NULL; | |
715 | ||
716 | /* Allocate the array which contains the information about the | |
717 | dtv slots. We allocate a few entries more than needed to | |
718 | avoid the need for reallocation. */ | |
719 | size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS; | |
720 | ||
721 | /* Allocate. */ | |
722 | GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *) | |
723 | calloc (sizeof (struct dtv_slotinfo_list) | |
724 | + nelem * sizeof (struct dtv_slotinfo), 1); | |
725 | /* No need to check the return value. If memory allocation failed | |
726 | the program would have been terminated. */ | |
727 | ||
728 | struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo; | |
729 | GL(dl_tls_dtv_slotinfo_list)->len = nelem; | |
730 | GL(dl_tls_dtv_slotinfo_list)->next = NULL; | |
731 | ||
732 | /* Fill in the information from the loaded modules. No namespace | |
733 | but the base one can be filled at this time. */ | |
734 | assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL); | |
735 | int i = 0; | |
736 | for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL; | |
737 | l = l->l_next) | |
738 | if (l->l_tls_blocksize != 0) | |
739 | { | |
740 | /* This is a module with TLS data. Store the map reference. | |
741 | The generation counter is zero. */ | |
742 | slotinfo[i].map = l; | |
743 | /* slotinfo[i].gen = 0; */ | |
744 | ++i; | |
745 | } | |
746 | assert (i == GL(dl_tls_max_dtv_idx)); | |
747 | ||
748 | /* Compute the TLS offsets for the various blocks. */ | |
749 | _dl_determine_tlsoffset (); | |
750 | ||
751 | /* Construct the static TLS block and the dtv for the initial | |
752 | thread. For some platforms this will include allocating memory | |
753 | for the thread descriptor. The memory for the TLS block will | |
754 | never be freed. It should be allocated accordingly. The dtv | |
755 | array can be changed if dynamic loading requires it. */ | |
756 | void *tcbp = _dl_allocate_tls_storage (); | |
757 | if (tcbp == NULL) | |
758 | _dl_fatal_printf ("\ | |
759 | cannot allocate TLS data structures for initial thread"); | |
760 | ||
761 | /* Store for detection of the special case by __tls_get_addr | |
762 | so it knows not to pass this dtv to the normal realloc. */ | |
763 | GL(dl_initial_dtv) = GET_DTV (tcbp); | |
764 | ||
765 | /* And finally install it for the main thread. If ld.so itself uses | |
766 | TLS we know the thread pointer was initialized earlier. */ | |
767 | const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD); | |
768 | if (__builtin_expect (lossage != NULL, 0)) | |
769 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage); | |
770 | tls_init_tp_called = true; | |
771 | ||
772 | return tcbp; | |
773 | } | |
9dcafc55 | 774 | |
bf2cc5fb | 775 | #ifdef _LIBC_REENTRANT |
ce460d04 RM |
776 | /* _dl_error_catch_tsd points to this for the single-threaded case. |
777 | It's reset by the thread library for multithreaded programs. */ | |
216455bc RM |
778 | void ** __attribute__ ((const)) |
779 | _dl_initial_error_catch_tsd (void) | |
ce460d04 RM |
780 | { |
781 | static void *data; | |
782 | return &data; | |
783 | } | |
bf2cc5fb | 784 | #endif |
ce460d04 | 785 | |
20fe49b9 UD |
786 | |
787 | static unsigned int | |
788 | do_preload (char *fname, struct link_map *main_map, const char *where) | |
789 | { | |
790 | const char *objname; | |
791 | const char *err_str = NULL; | |
792 | struct map_args args; | |
74780cf6 | 793 | bool malloced; |
20fe49b9 UD |
794 | |
795 | args.str = fname; | |
796 | args.loader = main_map; | |
797 | args.is_preloaded = 1; | |
798 | args.mode = 0; | |
799 | ||
800 | unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded; | |
801 | ||
74780cf6 | 802 | (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args); |
20fe49b9 UD |
803 | if (__builtin_expect (err_str != NULL, 0)) |
804 | { | |
805 | _dl_error_printf ("\ | |
806 | ERROR: ld.so: object '%s' from %s cannot be preloaded: ignored.\n", | |
807 | fname, where); | |
808 | /* No need to call free, this is still before | |
809 | the libc's malloc is used. */ | |
810 | } | |
811 | else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded) | |
812 | /* It is no duplicate. */ | |
813 | return 1; | |
814 | ||
815 | /* Nothing loaded. */ | |
816 | return 0; | |
817 | } | |
818 | ||
334fcf2a UD |
819 | #if defined SHARED && defined _LIBC_REENTRANT \ |
820 | && defined __rtld_lock_default_lock_recursive | |
20fe49b9 UD |
821 | static void |
822 | rtld_lock_default_lock_recursive (void *lock) | |
334fcf2a UD |
823 | { |
824 | __rtld_lock_default_lock_recursive (lock); | |
825 | } | |
826 | ||
20fe49b9 UD |
827 | static void |
828 | rtld_lock_default_unlock_recursive (void *lock) | |
334fcf2a UD |
829 | { |
830 | __rtld_lock_default_unlock_recursive (lock); | |
831 | } | |
832 | #endif | |
833 | ||
834 | ||
392a6b52 UD |
835 | /* The library search path. */ |
836 | static const char *library_path attribute_relro; | |
837 | /* The list preloaded objects. */ | |
838 | static const char *preloadlist attribute_relro; | |
839 | /* Nonzero if information about versions has to be printed. */ | |
840 | static int version_info attribute_relro; | |
a1a9d215 | 841 | |
d66e34cd | 842 | static void |
266180eb | 843 | dl_main (const ElfW(Phdr) *phdr, |
72f70279 | 844 | ElfW(Word) phnum, |
266180eb | 845 | ElfW(Addr) *user_entry) |
d66e34cd | 846 | { |
266180eb | 847 | const ElfW(Phdr) *ph; |
ea278354 | 848 | enum mode mode; |
c0f62c56 | 849 | struct link_map *main_map; |
14bab8de UD |
850 | size_t file_size; |
851 | char *file; | |
164a7164 | 852 | bool has_interp = false; |
77aba05b | 853 | unsigned int i; |
164a7164 UD |
854 | bool prelinked = false; |
855 | bool rtld_is_main = false; | |
5732c4df | 856 | #ifndef HP_TIMING_NONAVAIL |
db276fa1 UD |
857 | hp_timing_t start; |
858 | hp_timing_t stop; | |
859 | hp_timing_t diff; | |
5732c4df | 860 | #endif |
9dcafc55 | 861 | void *tcbp = NULL; |
d66e34cd | 862 | |
bf2cc5fb | 863 | #ifdef _LIBC_REENTRANT |
ce460d04 | 864 | /* Explicit initialization since the reloc would just be more work. */ |
216455bc | 865 | GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd; |
bf2cc5fb | 866 | #endif |
ce460d04 | 867 | |
adc12574 | 868 | GL(dl_init_static_tls) = &_dl_nothread_init_static_tls; |
adc12574 | 869 | |
334fcf2a UD |
870 | #if defined SHARED && defined _LIBC_REENTRANT \ |
871 | && defined __rtld_lock_default_lock_recursive | |
872 | GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive; | |
873 | GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive; | |
874 | #endif | |
875 | ||
c70ba488 RM |
876 | /* The explicit initialization here is cheaper than processing the reloc |
877 | in the _rtld_local definition's initializer. */ | |
878 | GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable; | |
879 | ||
ea278354 | 880 | /* Process the environment variable which control the behaviour. */ |
ba9fcb3f | 881 | process_envvars (&mode); |
3996f34b | 882 | |
ce6e047f | 883 | #ifndef HAVE_INLINED_SYSCALLS |
46ec036d | 884 | /* Set up a flag which tells we are just starting. */ |
e6caf4e1 | 885 | INTUSE(_dl_starting_up) = 1; |
ce6e047f | 886 | #endif |
46ec036d | 887 | |
a16956f3 | 888 | if (*user_entry == (ElfW(Addr)) ENTRY_POINT) |
0200214b RM |
889 | { |
890 | /* Ho ho. We are not the program interpreter! We are the program | |
891 | itself! This means someone ran ld.so as a command. Well, that | |
892 | might be convenient to do sometimes. We support it by | |
893 | interpreting the args like this: | |
894 | ||
895 | ld.so PROGRAM ARGS... | |
896 | ||
897 | The first argument is the name of a file containing an ELF | |
898 | executable we will load and run with the following arguments. | |
899 | To simplify life here, PROGRAM is searched for using the | |
900 | normal rules for shared objects, rather than $PATH or anything | |
901 | like that. We just load it and use its entry point; we don't | |
902 | pay attention to its PT_INTERP command (we are the interpreter | |
903 | ourselves). This is an easy way to test a new ld.so before | |
904 | installing it. */ | |
164a7164 | 905 | rtld_is_main = true; |
421f82e5 | 906 | |
ffee1316 | 907 | /* Note the place where the dynamic linker actually came from. */ |
e6caf4e1 | 908 | GL(dl_rtld_map).l_name = rtld_progname; |
6a76c115 | 909 | |
fd26970f | 910 | while (_dl_argc > 1) |
e6caf4e1 | 911 | if (! strcmp (INTUSE(_dl_argv)[1], "--list")) |
fd26970f UD |
912 | { |
913 | mode = list; | |
afdca0f2 | 914 | GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */ |
61965e9b | 915 | |
fd26970f UD |
916 | ++_dl_skip_args; |
917 | --_dl_argc; | |
e6caf4e1 | 918 | ++INTUSE(_dl_argv); |
fd26970f | 919 | } |
e6caf4e1 | 920 | else if (! strcmp (INTUSE(_dl_argv)[1], "--verify")) |
fd26970f UD |
921 | { |
922 | mode = verify; | |
6a76c115 | 923 | |
fd26970f UD |
924 | ++_dl_skip_args; |
925 | --_dl_argc; | |
e6caf4e1 | 926 | ++INTUSE(_dl_argv); |
fd26970f | 927 | } |
e6caf4e1 UD |
928 | else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path") |
929 | && _dl_argc > 2) | |
880f421f | 930 | { |
e6caf4e1 | 931 | library_path = INTUSE(_dl_argv)[2]; |
880f421f | 932 | |
310930c1 UD |
933 | _dl_skip_args += 2; |
934 | _dl_argc -= 2; | |
e6caf4e1 | 935 | INTUSE(_dl_argv) += 2; |
310930c1 | 936 | } |
e6caf4e1 UD |
937 | else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath") |
938 | && _dl_argc > 2) | |
310930c1 | 939 | { |
afdca0f2 | 940 | GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2]; |
310930c1 | 941 | |
74780cf6 UD |
942 | _dl_skip_args += 2; |
943 | _dl_argc -= 2; | |
944 | INTUSE(_dl_argv) += 2; | |
945 | } | |
946 | else if (! strcmp (INTUSE(_dl_argv)[1], "--audit") && _dl_argc > 2) | |
947 | { | |
948 | process_dl_audit (INTUSE(_dl_argv)[2]); | |
949 | ||
880f421f UD |
950 | _dl_skip_args += 2; |
951 | _dl_argc -= 2; | |
e6caf4e1 | 952 | INTUSE(_dl_argv) += 2; |
880f421f | 953 | } |
fd26970f UD |
954 | else |
955 | break; | |
d66e34cd | 956 | |
61eb22d3 UD |
957 | /* If we have no further argument the program was called incorrectly. |
958 | Grant the user some education. */ | |
959 | if (_dl_argc < 2) | |
35fc382a | 960 | _dl_fatal_printf ("\ |
2bcf29ba | 961 | Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ |
61eb22d3 UD |
962 | You have invoked `ld.so', the helper program for shared library executables.\n\ |
963 | This program usually lives in the file `/lib/ld.so', and special directives\n\ | |
964 | in executable files using ELF shared libraries tell the system's program\n\ | |
965 | loader to load the helper program from this file. This helper program loads\n\ | |
966 | the shared libraries needed by the program executable, prepares the program\n\ | |
967 | to run, and runs it. You may invoke this helper program directly from the\n\ | |
968 | command line to load and run an ELF executable file; this is like executing\n\ | |
969 | that file itself, but always uses this helper program from the file you\n\ | |
970 | specified, instead of the helper program file specified in the executable\n\ | |
971 | file you run. This is mostly of use for maintainers to test new versions\n\ | |
2bcf29ba UD |
972 | of this helper program; chances are you did not intend to run this program.\n\ |
973 | \n\ | |
b0a01055 UD |
974 | --list list all dependencies and how they are resolved\n\ |
975 | --verify verify that given object really is a dynamically linked\n\ | |
e8b1163e | 976 | object we can handle\n\ |
b0a01055 UD |
977 | --library-path PATH use given PATH instead of content of the environment\n\ |
978 | variable LD_LIBRARY_PATH\n\ | |
fcf70d41 | 979 | --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\ |
35fc382a | 980 | in LIST\n"); |
61eb22d3 | 981 | |
0200214b RM |
982 | ++_dl_skip_args; |
983 | --_dl_argc; | |
e6caf4e1 | 984 | ++INTUSE(_dl_argv); |
91f62ce6 | 985 | |
c70ba488 RM |
986 | /* The initialization of _dl_stack_flags done below assumes the |
987 | executable's PT_GNU_STACK may have been honored by the kernel, and | |
988 | so a PT_GNU_STACK with PF_X set means the stack started out with | |
989 | execute permission. However, this is not really true if the | |
990 | dynamic linker is the executable the kernel loaded. For this | |
991 | case, we must reinitialize _dl_stack_flags to match the dynamic | |
992 | linker itself. If the dynamic linker was built with a | |
993 | PT_GNU_STACK, then the kernel may have loaded us with a | |
994 | nonexecutable stack that we will have to make executable when we | |
995 | load the program below unless it has a PT_GNU_STACK indicating | |
996 | nonexecutable stack is ok. */ | |
997 | ||
998 | for (ph = phdr; ph < &phdr[phnum]; ++ph) | |
999 | if (ph->p_type == PT_GNU_STACK) | |
1000 | { | |
1001 | GL(dl_stack_flags) = ph->p_flags; | |
1002 | break; | |
1003 | } | |
1004 | ||
9a821cf9 | 1005 | if (__builtin_expect (mode, normal) == verify) |
2de99474 | 1006 | { |
8e17ea58 UD |
1007 | const char *objname; |
1008 | const char *err_str = NULL; | |
993b3242 | 1009 | struct map_args args; |
74780cf6 | 1010 | bool malloced; |
2de99474 | 1011 | |
e6caf4e1 | 1012 | args.str = rtld_progname; |
f04b9a68 UD |
1013 | args.loader = NULL; |
1014 | args.is_preloaded = 0; | |
1015 | args.mode = __RTLD_OPENEXEC; | |
74780cf6 UD |
1016 | (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, |
1017 | &args); | |
8e17ea58 | 1018 | if (__builtin_expect (err_str != NULL, 0)) |
e6caf4e1 UD |
1019 | /* We don't free the returned string, the programs stops |
1020 | anyway. */ | |
1021 | _exit (EXIT_FAILURE); | |
2de99474 UD |
1022 | } |
1023 | else | |
db276fa1 UD |
1024 | { |
1025 | HP_TIMING_NOW (start); | |
154d10bd | 1026 | _dl_map_object (NULL, rtld_progname, 0, lt_library, 0, |
c0f62c56 | 1027 | __RTLD_OPENEXEC, LM_ID_BASE); |
db276fa1 | 1028 | HP_TIMING_NOW (stop); |
61e0617a | 1029 | |
db276fa1 UD |
1030 | HP_TIMING_DIFF (load_time, start, stop); |
1031 | } | |
2de99474 | 1032 | |
c0f62c56 UD |
1033 | /* Now the map for the main executable is available. */ |
1034 | main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded; | |
1035 | ||
1036 | phdr = main_map->l_phdr; | |
1037 | phnum = main_map->l_phnum; | |
143e2b96 UD |
1038 | /* We overwrite here a pointer to a malloc()ed string. But since |
1039 | the malloc() implementation used at this point is the dummy | |
1040 | implementations which has no real free() function it does not | |
1041 | makes sense to free the old string first. */ | |
c0f62c56 UD |
1042 | main_map->l_name = (char *) ""; |
1043 | *user_entry = main_map->l_entry; | |
0200214b RM |
1044 | } |
1045 | else | |
1046 | { | |
1047 | /* Create a link_map for the executable itself. | |
1048 | This will be what dlopen on "" returns. */ | |
9fbdeb41 UD |
1049 | main_map = _dl_new_object ((char *) "", "", lt_executable, NULL, |
1050 | __RTLD_OPENEXEC, LM_ID_BASE); | |
9dcafc55 UD |
1051 | assert (main_map != NULL); |
1052 | assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded); | |
c0f62c56 UD |
1053 | main_map->l_phdr = phdr; |
1054 | main_map->l_phnum = phnum; | |
1055 | main_map->l_entry = *user_entry; | |
da832465 | 1056 | |
61e0617a UD |
1057 | /* At this point we are in a bit of trouble. We would have to |
1058 | fill in the values for l_dev and l_ino. But in general we | |
1059 | do not know where the file is. We also do not handle AT_EXECFD | |
1060 | even if it would be passed up. | |
1061 | ||
1062 | We leave the values here defined to 0. This is normally no | |
1063 | problem as the program code itself is normally no shared | |
1064 | object and therefore cannot be loaded dynamically. Nothing | |
1065 | prevent the use of dynamic binaries and in these situations | |
1066 | we might get problems. We might not be able to find out | |
1067 | whether the object is already loaded. But since there is no | |
1068 | easy way out and because the dynamic binary must also not | |
1069 | have an SONAME we ignore this program for now. If it becomes | |
1070 | a problem we can force people using SONAMEs. */ | |
1071 | ||
97a51d8a UD |
1072 | /* We delay initializing the path structure until we got the dynamic |
1073 | information for the program. */ | |
0200214b RM |
1074 | } |
1075 | ||
c0f62c56 UD |
1076 | main_map->l_map_end = 0; |
1077 | main_map->l_text_end = 0; | |
052b6a6c | 1078 | /* Perhaps the executable has no PT_LOAD header entries at all. */ |
c0f62c56 | 1079 | main_map->l_map_start = ~0; |
c0f62c56 UD |
1080 | /* And it was opened directly. */ |
1081 | ++main_map->l_direct_opencount; | |
052b6a6c | 1082 | |
0200214b | 1083 | /* Scan the program header table for the dynamic section. */ |
72f70279 | 1084 | for (ph = phdr; ph < &phdr[phnum]; ++ph) |
0200214b RM |
1085 | switch (ph->p_type) |
1086 | { | |
da832465 UD |
1087 | case PT_PHDR: |
1088 | /* Find out the load address. */ | |
c0f62c56 | 1089 | main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr; |
da832465 | 1090 | break; |
0200214b RM |
1091 | case PT_DYNAMIC: |
1092 | /* This tells us where to find the dynamic section, | |
1093 | which tells us everything we need to do. */ | |
c0f62c56 | 1094 | main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr; |
0200214b RM |
1095 | break; |
1096 | case PT_INTERP: | |
1097 | /* This "interpreter segment" was used by the program loader to | |
1098 | find the program interpreter, which is this program itself, the | |
1099 | dynamic linker. We note what name finds us, so that a future | |
1100 | dlopen call or DT_NEEDED entry, for something that wants to link | |
1101 | against the dynamic linker as a shared library, will know that | |
1102 | the shared object is already loaded. */ | |
c0f62c56 | 1103 | _dl_rtld_libname.name = ((const char *) main_map->l_addr |
be935610 | 1104 | + ph->p_vaddr); |
752a2a50 | 1105 | /* _dl_rtld_libname.next = NULL; Already zero. */ |
d6b5d570 | 1106 | GL(dl_rtld_map).l_libname = &_dl_rtld_libname; |
f41c8091 UD |
1107 | |
1108 | /* Ordinarilly, we would get additional names for the loader from | |
1109 | our DT_SONAME. This can't happen if we were actually linked as | |
1110 | a static executable (detect this case when we have no DYNAMIC). | |
1111 | If so, assume the filename component of the interpreter path to | |
1112 | be our SONAME, and add it to our name list. */ | |
d6b5d570 | 1113 | if (GL(dl_rtld_map).l_ld == NULL) |
f41c8091 | 1114 | { |
88794e30 UD |
1115 | const char *p = NULL; |
1116 | const char *cp = _dl_rtld_libname.name; | |
1117 | ||
1118 | /* Find the filename part of the path. */ | |
1119 | while (*cp != '\0') | |
1120 | if (*cp++ == '/') | |
1121 | p = cp; | |
1122 | ||
1123 | if (p != NULL) | |
f41c8091 | 1124 | { |
88794e30 | 1125 | _dl_rtld_libname2.name = p; |
752a2a50 | 1126 | /* _dl_rtld_libname2.next = NULL; Already zero. */ |
f41c8091 UD |
1127 | _dl_rtld_libname.next = &_dl_rtld_libname2; |
1128 | } | |
1129 | } | |
1130 | ||
164a7164 | 1131 | has_interp = true; |
0200214b | 1132 | break; |
052b6a6c | 1133 | case PT_LOAD: |
052b6a6c UD |
1134 | { |
1135 | ElfW(Addr) mapstart; | |
2373b30e UD |
1136 | ElfW(Addr) allocend; |
1137 | ||
1138 | /* Remember where the main program starts in memory. */ | |
c0f62c56 UD |
1139 | mapstart = (main_map->l_addr + (ph->p_vaddr & ~(ph->p_align - 1))); |
1140 | if (main_map->l_map_start > mapstart) | |
1141 | main_map->l_map_start = mapstart; | |
2373b30e UD |
1142 | |
1143 | /* Also where it ends. */ | |
c0f62c56 UD |
1144 | allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz; |
1145 | if (main_map->l_map_end < allocend) | |
1146 | main_map->l_map_end = allocend; | |
1147 | if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end) | |
1148 | main_map->l_text_end = allocend; | |
052b6a6c UD |
1149 | } |
1150 | break; | |
9dcafc55 | 1151 | |
a334319f | 1152 | case PT_TLS: |
aed283dd UD |
1153 | if (ph->p_memsz > 0) |
1154 | { | |
1155 | /* Note that in the case the dynamic linker we duplicate work | |
1156 | here since we read the PT_TLS entry already in | |
1157 | _dl_start_final. But the result is repeatable so do not | |
1158 | check for this special but unimportant case. */ | |
c0f62c56 UD |
1159 | main_map->l_tls_blocksize = ph->p_memsz; |
1160 | main_map->l_tls_align = ph->p_align; | |
99fe3b0e | 1161 | if (ph->p_align == 0) |
c0f62c56 | 1162 | main_map->l_tls_firstbyte_offset = 0; |
99fe3b0e | 1163 | else |
c0f62c56 UD |
1164 | main_map->l_tls_firstbyte_offset = (ph->p_vaddr |
1165 | & (ph->p_align - 1)); | |
1166 | main_map->l_tls_initimage_size = ph->p_filesz; | |
1167 | main_map->l_tls_initimage = (void *) ph->p_vaddr; | |
aed283dd UD |
1168 | |
1169 | /* This image gets the ID one. */ | |
c0f62c56 | 1170 | GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1; |
aed283dd | 1171 | } |
9dcafc55 UD |
1172 | break; |
1173 | ||
ecdeaac0 RM |
1174 | case PT_GNU_STACK: |
1175 | GL(dl_stack_flags) = ph->p_flags; | |
1176 | break; | |
e8ed861d UD |
1177 | |
1178 | case PT_GNU_RELRO: | |
c0f62c56 UD |
1179 | main_map->l_relro_addr = ph->p_vaddr; |
1180 | main_map->l_relro_size = ph->p_memsz; | |
e8ed861d | 1181 | break; |
0200214b | 1182 | } |
11bf311e UD |
1183 | |
1184 | /* Adjust the address of the TLS initialization image in case | |
1185 | the executable is actually an ET_DYN object. */ | |
1186 | if (main_map->l_tls_initimage != NULL) | |
1187 | main_map->l_tls_initimage | |
1188 | = (char *) main_map->l_tls_initimage + main_map->l_addr; | |
c0f62c56 UD |
1189 | if (! main_map->l_map_end) |
1190 | main_map->l_map_end = ~0; | |
1191 | if (! main_map->l_text_end) | |
1192 | main_map->l_text_end = ~0; | |
d6b5d570 | 1193 | if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name) |
c84142e8 UD |
1194 | { |
1195 | /* We were invoked directly, so the program might not have a | |
1196 | PT_INTERP. */ | |
d6b5d570 | 1197 | _dl_rtld_libname.name = GL(dl_rtld_map).l_name; |
3fb55878 | 1198 | /* _dl_rtld_libname.next = NULL; Already zero. */ |
d6b5d570 | 1199 | GL(dl_rtld_map).l_libname = &_dl_rtld_libname; |
c84142e8 | 1200 | } |
ffee1316 | 1201 | else |
d6b5d570 | 1202 | assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */ |
0200214b | 1203 | |
9dcafc55 UD |
1204 | /* If the current libname is different from the SONAME, add the |
1205 | latter as well. */ | |
1206 | if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL | |
1207 | && strcmp (GL(dl_rtld_map).l_libname->name, | |
1208 | (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) | |
1209 | + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0) | |
1210 | { | |
1211 | static struct libname_list newname; | |
1212 | newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) | |
1213 | + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr); | |
1214 | newname.next = NULL; | |
1215 | newname.dont_free = 1; | |
1216 | ||
1217 | assert (GL(dl_rtld_map).l_libname->next == NULL); | |
1218 | GL(dl_rtld_map).l_libname->next = &newname; | |
1219 | } | |
1220 | /* The ld.so must be relocated since otherwise loading audit modules | |
1221 | will fail since they reuse the very same ld.so. */ | |
1222 | assert (GL(dl_rtld_map).l_relocated); | |
1223 | ||
9a51759b UD |
1224 | if (! rtld_is_main) |
1225 | { | |
1226 | /* Extract the contents of the dynamic section for easy access. */ | |
c0f62c56 | 1227 | elf_get_dynamic_info (main_map, NULL); |
efec5079 | 1228 | /* Set up our cache of pointers into the hash table. */ |
c0f62c56 | 1229 | _dl_setup_hash (main_map); |
9a51759b | 1230 | } |
0200214b | 1231 | |
9a821cf9 | 1232 | if (__builtin_expect (mode, normal) == verify) |
e2102c14 UD |
1233 | { |
1234 | /* We were called just to verify that this is a dynamic | |
1235 | executable using us as the program interpreter. Exit with an | |
1236 | error if we were not able to load the binary or no interpreter | |
1237 | is specified (i.e., this is no dynamically linked binary. */ | |
c0f62c56 | 1238 | if (main_map->l_ld == NULL) |
e2102c14 | 1239 | _exit (1); |
e2102c14 UD |
1240 | |
1241 | /* We allow here some platform specific code. */ | |
1242 | #ifdef DISTINGUISH_LIB_VERSIONS | |
1243 | DISTINGUISH_LIB_VERSIONS; | |
1244 | #endif | |
eb406346 | 1245 | _exit (has_interp ? 0 : 2); |
e2102c14 UD |
1246 | } |
1247 | ||
ab1d521d RM |
1248 | struct link_map **first_preload = &GL(dl_rtld_map).l_next; |
1249 | #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO | |
1250 | /* Set up the data structures for the system-supplied DSO early, | |
1251 | so they can influence _dl_init_paths. */ | |
1252 | if (GLRO(dl_sysinfo_dso) != NULL) | |
1253 | { | |
1254 | /* Do an abridged version of the work _dl_map_object_from_fd would do | |
1255 | to map in the object. It's already mapped and prelinked (and | |
1256 | better be, since it's read-only and so we couldn't relocate it). | |
1257 | We just want our data structures to describe it as if we had just | |
1258 | mapped and relocated it normally. */ | |
1259 | struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL, | |
1260 | 0, LM_ID_BASE); | |
1261 | if (__builtin_expect (l != NULL, 1)) | |
1262 | { | |
1263 | static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro; | |
1264 | ||
1265 | l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso) | |
1266 | + GLRO(dl_sysinfo_dso)->e_phoff); | |
1267 | l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum; | |
1268 | for (uint_fast16_t i = 0; i < l->l_phnum; ++i) | |
1269 | { | |
1270 | const ElfW(Phdr) *const ph = &l->l_phdr[i]; | |
1271 | if (ph->p_type == PT_DYNAMIC) | |
1272 | { | |
1273 | l->l_ld = (void *) ph->p_vaddr; | |
1274 | l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); | |
1275 | } | |
1276 | else if (ph->p_type == PT_LOAD) | |
1277 | { | |
1278 | if (! l->l_addr) | |
1279 | l->l_addr = ph->p_vaddr; | |
1280 | if (ph->p_vaddr + ph->p_memsz >= l->l_map_end) | |
1281 | l->l_map_end = ph->p_vaddr + ph->p_memsz; | |
1282 | if ((ph->p_flags & PF_X) | |
1283 | && ph->p_vaddr + ph->p_memsz >= l->l_text_end) | |
1284 | l->l_text_end = ph->p_vaddr + ph->p_memsz; | |
1285 | } | |
1286 | else | |
1287 | /* There must be no TLS segment. */ | |
1288 | assert (ph->p_type != PT_TLS); | |
1289 | } | |
1290 | l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso); | |
1291 | l->l_addr = l->l_map_start - l->l_addr; | |
1292 | l->l_map_end += l->l_addr; | |
1293 | l->l_text_end += l->l_addr; | |
1294 | l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr); | |
1295 | elf_get_dynamic_info (l, dyn_temp); | |
1296 | _dl_setup_hash (l); | |
1297 | l->l_relocated = 1; | |
1298 | ||
8c2e201b UD |
1299 | /* Initialize l_local_scope to contain just this map. This allows |
1300 | the use of dl_lookup_symbol_x to resolve symbols within the vdso. | |
1301 | So we create a single entry list pointing to l_real as its only | |
1302 | element */ | |
1303 | l->l_local_scope[0]->r_nlist = 1; | |
1304 | l->l_local_scope[0]->r_list = &l->l_real; | |
1305 | ||
ab1d521d RM |
1306 | /* Now that we have the info handy, use the DSO image's soname |
1307 | so this object can be looked up by name. Note that we do not | |
1308 | set l_name here. That field gives the file name of the DSO, | |
1309 | and this DSO is not associated with any file. */ | |
1310 | if (l->l_info[DT_SONAME] != NULL) | |
1311 | { | |
1312 | /* Work around a kernel problem. The kernel cannot handle | |
1313 | addresses in the vsyscall DSO pages in writev() calls. */ | |
1314 | const char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB]) | |
1315 | + l->l_info[DT_SONAME]->d_un.d_val); | |
1316 | size_t len = strlen (dsoname); | |
1317 | char *copy = malloc (len); | |
1318 | if (copy == NULL) | |
1319 | _dl_fatal_printf ("out of memory\n"); | |
1320 | l->l_libname->name = memcpy (copy, dsoname, len); | |
1321 | } | |
1322 | ||
1323 | /* Rearrange the list so this DSO appears after rtld_map. */ | |
1324 | assert (l->l_next == NULL); | |
1325 | assert (l->l_prev == main_map); | |
1326 | GL(dl_rtld_map).l_next = l; | |
1327 | l->l_prev = &GL(dl_rtld_map); | |
1328 | first_preload = &l->l_next; | |
1329 | ||
1330 | /* We have a prelinked DSO preloaded by the system. */ | |
1331 | GLRO(dl_sysinfo_map) = l; | |
1332 | # ifdef NEED_DL_SYSINFO | |
1333 | if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT) | |
1334 | GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr; | |
1335 | # endif | |
1336 | } | |
1337 | } | |
1338 | #endif | |
1339 | ||
1340 | #ifdef DL_SYSDEP_OSCHECK | |
1341 | DL_SYSDEP_OSCHECK (dl_fatal); | |
1342 | #endif | |
1343 | ||
1344 | /* Initialize the data structures for the search paths for shared | |
1345 | objects. */ | |
1346 | _dl_init_paths (library_path); | |
97a51d8a | 1347 | |
9dcafc55 | 1348 | /* Initialize _r_debug. */ |
29f97654 UD |
1349 | struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr, |
1350 | LM_ID_BASE); | |
9dcafc55 UD |
1351 | r->r_state = RT_CONSISTENT; |
1352 | ||
0200214b | 1353 | /* Put the link_map for ourselves on the chain so it can be found by |
ceb2d9aa | 1354 | name. Note that at this point the global chain of link maps contains |
d6b5d570 UD |
1355 | exactly one element, which is pointed to by dl_loaded. */ |
1356 | if (! GL(dl_rtld_map).l_name) | |
ffee1316 RM |
1357 | /* If not invoked directly, the dynamic linker shared object file was |
1358 | found by the PT_INTERP name. */ | |
d6b5d570 UD |
1359 | GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name; |
1360 | GL(dl_rtld_map).l_type = lt_library; | |
c0f62c56 UD |
1361 | main_map->l_next = &GL(dl_rtld_map); |
1362 | GL(dl_rtld_map).l_prev = main_map; | |
1363 | ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded; | |
e8ed861d | 1364 | ++GL(dl_load_adds); |
0200214b | 1365 | |
97fd3a30 UD |
1366 | /* If LD_USE_LOAD_BIAS env variable has not been seen, default |
1367 | to not using bias for non-prelinked PIEs and libraries | |
1368 | and using it for executables or prelinked PIEs or libraries. */ | |
afdca0f2 | 1369 | if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2) |
c0f62c56 | 1370 | GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0; |
97fd3a30 | 1371 | |
553eca26 UD |
1372 | /* Set up the program header information for the dynamic linker |
1373 | itself. It is needed in the dl_iterate_phdr() callbacks. */ | |
4cfde896 | 1374 | ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start; |
e8ed861d UD |
1375 | ElfW(Phdr) *rtld_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start |
1376 | + rtld_ehdr->e_phoff); | |
1377 | GL(dl_rtld_map).l_phdr = rtld_phdr; | |
553eca26 UD |
1378 | GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum; |
1379 | ||
9dcafc55 | 1380 | |
e8ed861d UD |
1381 | /* PT_GNU_RELRO is usually the last phdr. */ |
1382 | size_t cnt = rtld_ehdr->e_phnum; | |
1383 | while (cnt-- > 0) | |
1384 | if (rtld_phdr[cnt].p_type == PT_GNU_RELRO) | |
1385 | { | |
1386 | GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr; | |
1387 | GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz; | |
1388 | break; | |
1389 | } | |
1390 | ||
9dcafc55 UD |
1391 | /* Add the dynamic linker to the TLS list if it also uses TLS. */ |
1392 | if (GL(dl_rtld_map).l_tls_blocksize != 0) | |
1393 | /* Assign a module ID. Do this before loading any audit modules. */ | |
1394 | GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid (); | |
9dcafc55 UD |
1395 | |
1396 | /* If we have auditing DSOs to load, do it now. */ | |
1397 | if (__builtin_expect (audit_list != NULL, 0)) | |
1398 | { | |
1399 | /* Iterate over all entries in the list. The order is important. */ | |
1400 | struct audit_ifaces *last_audit = NULL; | |
1401 | struct audit_list *al = audit_list->next; | |
1402 | do | |
1403 | { | |
9dcafc55 UD |
1404 | int tls_idx = GL(dl_tls_max_dtv_idx); |
1405 | ||
1406 | /* Now it is time to determine the layout of the static TLS | |
1407 | block and allocate it for the initial thread. Note that we | |
1408 | always allocate the static block, we never defer it even if | |
1409 | no DF_STATIC_TLS bit is set. The reason is that we know | |
1410 | glibc will use the static model. */ | |
11bf311e UD |
1411 | |
1412 | /* Since we start using the auditing DSOs right away we need to | |
1413 | initialize the data structures now. */ | |
1414 | tcbp = init_tls (); | |
1415 | ||
9dcafc55 UD |
1416 | struct dlmopen_args dlmargs; |
1417 | dlmargs.fname = al->name; | |
1418 | dlmargs.map = NULL; | |
1419 | ||
1420 | const char *objname; | |
1421 | const char *err_str = NULL; | |
74780cf6 UD |
1422 | bool malloced; |
1423 | (void) _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit, | |
1424 | &dlmargs); | |
9dcafc55 UD |
1425 | if (__builtin_expect (err_str != NULL, 0)) |
1426 | { | |
1427 | not_loaded: | |
1428 | _dl_error_printf ("\ | |
1429 | ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", | |
1430 | al->name, err_str); | |
74780cf6 UD |
1431 | if (malloced) |
1432 | free ((char *) err_str); | |
9dcafc55 UD |
1433 | } |
1434 | else | |
1435 | { | |
1436 | struct lookup_args largs; | |
1437 | largs.name = "la_version"; | |
1438 | largs.map = dlmargs.map; | |
1439 | ||
1440 | /* Check whether the interface version matches. */ | |
74780cf6 UD |
1441 | (void) _dl_catch_error (&objname, &err_str, &malloced, |
1442 | lookup_doit, &largs); | |
9dcafc55 UD |
1443 | |
1444 | unsigned int (*laversion) (unsigned int); | |
1445 | unsigned int lav; | |
1446 | if (err_str == NULL | |
1447 | && (laversion = largs.result) != NULL | |
1448 | && (lav = laversion (LAV_CURRENT)) > 0 | |
1449 | && lav <= LAV_CURRENT) | |
1450 | { | |
1451 | /* Allocate structure for the callback function pointers. | |
1452 | This call can never fail. */ | |
1453 | union | |
1454 | { | |
1455 | struct audit_ifaces ifaces; | |
1456 | #define naudit_ifaces 8 | |
1457 | void (*fptr[naudit_ifaces]) (void); | |
1458 | } *newp = malloc (sizeof (*newp)); | |
1459 | ||
1460 | /* Names of the auditing interfaces. All in one | |
1461 | long string. */ | |
1462 | static const char audit_iface_names[] = | |
1463 | "la_activity\0" | |
1464 | "la_objsearch\0" | |
1465 | "la_objopen\0" | |
1466 | "la_preinit\0" | |
1467 | #if __ELF_NATIVE_CLASS == 32 | |
1468 | "la_symbind32\0" | |
1469 | #elif __ELF_NATIVE_CLASS == 64 | |
1470 | "la_symbind64\0" | |
1471 | #else | |
1472 | # error "__ELF_NATIVE_CLASS must be defined" | |
1473 | #endif | |
1474 | #define STRING(s) __STRING (s) | |
1475 | "la_" STRING (ARCH_LA_PLTENTER) "\0" | |
1476 | "la_" STRING (ARCH_LA_PLTEXIT) "\0" | |
1477 | "la_objclose\0"; | |
1478 | unsigned int cnt = 0; | |
1479 | const char *cp = audit_iface_names; | |
1480 | do | |
1481 | { | |
1482 | largs.name = cp; | |
74780cf6 UD |
1483 | (void) _dl_catch_error (&objname, &err_str, &malloced, |
1484 | lookup_doit, &largs); | |
9dcafc55 UD |
1485 | |
1486 | /* Store the pointer. */ | |
1487 | if (err_str == NULL && largs.result != NULL) | |
1488 | { | |
1489 | newp->fptr[cnt] = largs.result; | |
1490 | ||
1491 | /* The dynamic linker link map is statically | |
1492 | allocated, initialize the data now. */ | |
1493 | GL(dl_rtld_map).l_audit[cnt].cookie | |
1494 | = (intptr_t) &GL(dl_rtld_map); | |
1495 | } | |
1496 | else | |
1497 | newp->fptr[cnt] = NULL; | |
1498 | ++cnt; | |
1499 | ||
1500 | cp = (char *) rawmemchr (cp, '\0') + 1; | |
1501 | } | |
1502 | while (*cp != '\0'); | |
1503 | assert (cnt == naudit_ifaces); | |
1504 | ||
1505 | /* Now append the new auditing interface to the list. */ | |
1506 | newp->ifaces.next = NULL; | |
1507 | if (last_audit == NULL) | |
1508 | last_audit = GLRO(dl_audit) = &newp->ifaces; | |
1509 | else | |
1510 | last_audit = last_audit->next = &newp->ifaces; | |
1511 | ++GLRO(dl_naudit); | |
1512 | ||
1513 | /* Mark the DSO as being used for auditing. */ | |
1514 | dlmargs.map->l_auditing = 1; | |
1515 | } | |
1516 | else | |
1517 | { | |
1518 | /* We cannot use the DSO, it does not have the | |
1519 | appropriate interfaces or it expects something | |
1520 | more recent. */ | |
1521 | #ifndef NDEBUG | |
1522 | Lmid_t ns = dlmargs.map->l_ns; | |
1523 | #endif | |
1524 | _dl_close (dlmargs.map); | |
1525 | ||
1526 | /* Make sure the namespace has been cleared entirely. */ | |
1527 | assert (GL(dl_ns)[ns]._ns_loaded == NULL); | |
1528 | assert (GL(dl_ns)[ns]._ns_nloaded == 0); | |
1529 | ||
9dcafc55 | 1530 | GL(dl_tls_max_dtv_idx) = tls_idx; |
9dcafc55 UD |
1531 | goto not_loaded; |
1532 | } | |
1533 | } | |
1534 | ||
1535 | al = al->next; | |
1536 | } | |
1537 | while (al != audit_list->next); | |
1538 | ||
1539 | /* If we have any auditing modules, announce that we already | |
1540 | have two objects loaded. */ | |
1541 | if (__builtin_expect (GLRO(dl_naudit) > 0, 0)) | |
1542 | { | |
1543 | struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) }; | |
1544 | ||
1545 | for (unsigned int outer = 0; outer < 2; ++outer) | |
1546 | { | |
1547 | struct audit_ifaces *afct = GLRO(dl_audit); | |
1548 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
1549 | { | |
1550 | if (afct->objopen != NULL) | |
1551 | { | |
1552 | ls[outer]->l_audit[cnt].bindflags | |
1553 | = afct->objopen (ls[outer], LM_ID_BASE, | |
1554 | &ls[outer]->l_audit[cnt].cookie); | |
1555 | ||
1556 | ls[outer]->l_audit_any_plt | |
1557 | |= ls[outer]->l_audit[cnt].bindflags != 0; | |
1558 | } | |
1559 | ||
1560 | afct = afct->next; | |
1561 | } | |
1562 | } | |
1563 | } | |
1564 | } | |
1565 | ||
c63d8f80 UD |
1566 | /* Set up debugging before the debugger is notified for the first time. */ |
1567 | #ifdef ELF_MACHINE_DEBUG_SETUP | |
1568 | /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */ | |
1569 | ELF_MACHINE_DEBUG_SETUP (main_map, r); | |
1570 | ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r); | |
1571 | #else | |
1572 | if (main_map->l_info[DT_DEBUG] != NULL) | |
1573 | /* There is a DT_DEBUG entry in the dynamic section. Fill it in | |
1574 | with the run-time address of the r_debug structure */ | |
1575 | main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | |
1576 | ||
1577 | /* Fill in the pointer in the dynamic linker's own dynamic section, in | |
1578 | case you run gdb on the dynamic linker directly. */ | |
1579 | if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL) | |
1580 | GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | |
1581 | #endif | |
1582 | ||
9dcafc55 UD |
1583 | /* We start adding objects. */ |
1584 | r->r_state = RT_ADD; | |
1585 | _dl_debug_state (); | |
1586 | ||
1587 | /* Auditing checkpoint: we are ready to signal that the initial map | |
1588 | is being constructed. */ | |
1589 | if (__builtin_expect (GLRO(dl_naudit) > 0, 0)) | |
1590 | { | |
1591 | struct audit_ifaces *afct = GLRO(dl_audit); | |
1592 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
1593 | { | |
1594 | if (afct->activity != NULL) | |
1595 | afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD); | |
1596 | ||
1597 | afct = afct->next; | |
1598 | } | |
1599 | } | |
1600 | ||
14bab8de | 1601 | /* We have two ways to specify objects to preload: via environment |
49c091e5 | 1602 | variable and via the file /etc/ld.so.preload. The latter can also |
14bab8de | 1603 | be used when security is enabled. */ |
ab1d521d | 1604 | assert (*first_preload == NULL); |
20fe49b9 UD |
1605 | struct link_map **preloads = NULL; |
1606 | unsigned int npreloads = 0; | |
14bab8de | 1607 | |
db33f7d4 | 1608 | if (__builtin_expect (preloadlist != NULL, 0)) |
c4029823 | 1609 | { |
566efee2 UD |
1610 | /* The LD_PRELOAD environment variable gives list of libraries |
1611 | separated by white space or colons that are loaded before the | |
fd26970f UD |
1612 | executable's dependencies and prepended to the global scope |
1613 | list. If the binary is running setuid all elements | |
1614 | containing a '/' are ignored since it is insecure. */ | |
1615 | char *list = strdupa (preloadlist); | |
1616 | char *p; | |
db276fa1 UD |
1617 | |
1618 | HP_TIMING_NOW (start); | |
1619 | ||
9710f75d UD |
1620 | /* Prevent optimizing strsep. Speed is not important here. */ |
1621 | while ((p = (strsep) (&list, " :")) != NULL) | |
e2102c14 | 1622 | if (p[0] != '\0' |
e6caf4e1 | 1623 | && (__builtin_expect (! INTUSE(__libc_enable_secure), 1) |
db33f7d4 | 1624 | || strchr (p, '/') == NULL)) |
20fe49b9 | 1625 | npreloads += do_preload (p, main_map, "LD_PRELOAD"); |
db276fa1 UD |
1626 | |
1627 | HP_TIMING_NOW (stop); | |
1628 | HP_TIMING_DIFF (diff, start, stop); | |
1629 | HP_TIMING_ACCUM_NT (load_time, diff); | |
c4029823 UD |
1630 | } |
1631 | ||
761490a1 UD |
1632 | /* There usually is no ld.so.preload file, it should only be used |
1633 | for emergencies and testing. So the open call etc should usually | |
1634 | fail. Using access() on a non-existing file is faster than using | |
1635 | open(). So we do this first. If it succeeds we do almost twice | |
1636 | the work but this does not matter, since it is not for production | |
1637 | use. */ | |
1638 | static const char preload_file[] = "/etc/ld.so.preload"; | |
1639 | if (__builtin_expect (__access (preload_file, R_OK) == 0, 0)) | |
14bab8de | 1640 | { |
761490a1 UD |
1641 | /* Read the contents of the file. */ |
1642 | file = _dl_sysdep_read_whole_file (preload_file, &file_size, | |
1643 | PROT_READ | PROT_WRITE); | |
1644 | if (__builtin_expect (file != MAP_FAILED, 0)) | |
14bab8de | 1645 | { |
761490a1 UD |
1646 | /* Parse the file. It contains names of libraries to be loaded, |
1647 | separated by white spaces or `:'. It may also contain | |
1648 | comments introduced by `#'. */ | |
1649 | char *problem; | |
1650 | char *runp; | |
1651 | size_t rest; | |
1652 | ||
1653 | /* Eliminate comments. */ | |
e2102c14 | 1654 | runp = file; |
761490a1 UD |
1655 | rest = file_size; |
1656 | while (rest > 0) | |
1657 | { | |
1658 | char *comment = memchr (runp, '#', rest); | |
1659 | if (comment == NULL) | |
1660 | break; | |
1661 | ||
1662 | rest -= comment - runp; | |
1663 | do | |
1664 | *comment = ' '; | |
1665 | while (--rest > 0 && *++comment != '\n'); | |
1666 | } | |
1667 | ||
1668 | /* We have one problematic case: if we have a name at the end of | |
1669 | the file without a trailing terminating characters, we cannot | |
1670 | place the \0. Handle the case separately. */ | |
1671 | if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t' | |
1672 | && file[file_size - 1] != '\n' && file[file_size - 1] != ':') | |
1673 | { | |
1674 | problem = &file[file_size]; | |
1675 | while (problem > file && problem[-1] != ' ' | |
1676 | && problem[-1] != '\t' | |
1677 | && problem[-1] != '\n' && problem[-1] != ':') | |
1678 | --problem; | |
1679 | ||
1680 | if (problem > file) | |
1681 | problem[-1] = '\0'; | |
1682 | } | |
1683 | else | |
1684 | { | |
1685 | problem = NULL; | |
1686 | file[file_size - 1] = '\0'; | |
1687 | } | |
f04b9a68 | 1688 | |
761490a1 | 1689 | HP_TIMING_NOW (start); |
f04b9a68 | 1690 | |
761490a1 UD |
1691 | if (file != problem) |
1692 | { | |
1693 | char *p; | |
1694 | runp = file; | |
1695 | while ((p = strsep (&runp, ": \t\n")) != NULL) | |
1696 | if (p[0] != '\0') | |
20fe49b9 | 1697 | npreloads += do_preload (p, main_map, preload_file); |
761490a1 UD |
1698 | } |
1699 | ||
1700 | if (problem != NULL) | |
1701 | { | |
1702 | char *p = strndupa (problem, file_size - (problem - file)); | |
20fe49b9 UD |
1703 | |
1704 | npreloads += do_preload (p, main_map, preload_file); | |
761490a1 | 1705 | } |
14bab8de | 1706 | |
761490a1 UD |
1707 | HP_TIMING_NOW (stop); |
1708 | HP_TIMING_DIFF (diff, start, stop); | |
1709 | HP_TIMING_ACCUM_NT (load_time, diff); | |
db276fa1 | 1710 | |
761490a1 UD |
1711 | /* We don't need the file anymore. */ |
1712 | __munmap (file, file_size); | |
1713 | } | |
14bab8de UD |
1714 | } |
1715 | ||
ab1d521d | 1716 | if (__builtin_expect (*first_preload != NULL, 0)) |
14bab8de UD |
1717 | { |
1718 | /* Set up PRELOADS with a vector of the preloaded libraries. */ | |
ab1d521d | 1719 | struct link_map *l = *first_preload; |
14bab8de | 1720 | preloads = __alloca (npreloads * sizeof preloads[0]); |
14bab8de UD |
1721 | i = 0; |
1722 | do | |
1723 | { | |
1724 | preloads[i++] = l; | |
1725 | l = l->l_next; | |
1726 | } while (l); | |
1727 | assert (i == npreloads); | |
1728 | } | |
1729 | ||
2064087b RM |
1730 | /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD |
1731 | specified some libraries to load, these are inserted before the actual | |
1732 | dependencies in the executable's searchlist for symbol resolution. */ | |
db276fa1 | 1733 | HP_TIMING_NOW (start); |
c0f62c56 | 1734 | _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0); |
db276fa1 UD |
1735 | HP_TIMING_NOW (stop); |
1736 | HP_TIMING_DIFF (diff, start, stop); | |
1737 | HP_TIMING_ACCUM_NT (load_time, diff); | |
e3e35cfc | 1738 | |
20fe49b9 | 1739 | /* Mark all objects as being in the global scope. */ |
c0f62c56 | 1740 | for (i = main_map->l_searchlist.r_nlist; i > 0; ) |
20fe49b9 | 1741 | main_map->l_searchlist.r_list[--i]->l_global = 1; |
d66e34cd | 1742 | |
2064087b | 1743 | #ifndef MAP_ANON |
f332db02 RM |
1744 | /* We are done mapping things, so close the zero-fill descriptor. */ |
1745 | __close (_dl_zerofd); | |
1746 | _dl_zerofd = -1; | |
2064087b | 1747 | #endif |
f332db02 | 1748 | |
f9496a7b | 1749 | /* Remove _dl_rtld_map from the chain. */ |
d6b5d570 | 1750 | GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next; |
20fe49b9 | 1751 | if (GL(dl_rtld_map).l_next != NULL) |
d6b5d570 | 1752 | GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev; |
f9496a7b | 1753 | |
20fe49b9 UD |
1754 | for (i = 1; i < main_map->l_searchlist.r_nlist; ++i) |
1755 | if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map)) | |
1756 | break; | |
1757 | ||
1758 | bool rtld_multiple_ref = false; | |
1759 | if (__builtin_expect (i < main_map->l_searchlist.r_nlist, 1)) | |
0200214b | 1760 | { |
f9496a7b RM |
1761 | /* Some DT_NEEDED entry referred to the interpreter object itself, so |
1762 | put it back in the list of visible objects. We insert it into the | |
1763 | chain in symbol search order because gdb uses the chain's order as | |
1764 | its symbol search order. */ | |
20fe49b9 UD |
1765 | rtld_multiple_ref = true; |
1766 | ||
c0f62c56 | 1767 | GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1]; |
b2bcd61a | 1768 | if (__builtin_expect (mode, normal) == normal) |
3b3ddb4f | 1769 | { |
c0f62c56 UD |
1770 | GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist |
1771 | ? main_map->l_searchlist.r_list[i + 1] | |
3b3ddb4f | 1772 | : NULL); |
30e32d23 | 1773 | #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO |
ab1d521d RM |
1774 | if (GLRO(dl_sysinfo_map) != NULL |
1775 | && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map) | |
1776 | && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map)) | |
1777 | GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map); | |
3b3ddb4f UD |
1778 | #endif |
1779 | } | |
b2bcd61a UD |
1780 | else |
1781 | /* In trace mode there might be an invisible object (which we | |
1782 | could not find) after the previous one in the search list. | |
1783 | In this case it doesn't matter much where we put the | |
1784 | interpreter object, so we just initialize the list pointer so | |
1785 | that the assertion below holds. */ | |
d6b5d570 | 1786 | GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next; |
b2bcd61a | 1787 | |
d6b5d570 UD |
1788 | assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next); |
1789 | GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map); | |
3fb55878 | 1790 | if (GL(dl_rtld_map).l_next != NULL) |
f9496a7b | 1791 | { |
d6b5d570 UD |
1792 | assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev); |
1793 | GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map); | |
f9496a7b | 1794 | } |
0200214b | 1795 | } |
d66e34cd | 1796 | |
c84142e8 UD |
1797 | /* Now let us see whether all libraries are available in the |
1798 | versions we need. */ | |
1799 | { | |
993b3242 UD |
1800 | struct version_check_args args; |
1801 | args.doexit = mode == normal; | |
145b8413 | 1802 | args.dotrace = mode == trace; |
993b3242 | 1803 | _dl_receive_error (print_missing_version, version_check_doit, &args); |
c84142e8 UD |
1804 | } |
1805 | ||
2d148689 RM |
1806 | /* We do not initialize any of the TLS functionality unless any of the |
1807 | initial modules uses TLS. This makes dynamic loading of modules with | |
1808 | TLS impossible, but to support it requires either eagerly doing setup | |
1809 | now or lazily doing it later. Doing it now makes us incompatible with | |
1810 | an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever | |
1811 | used. Trying to do it lazily is too hairy to try when there could be | |
1812 | multiple threads (from a non-TLS-using libpthread). */ | |
9dcafc55 | 1813 | bool was_tls_init_tp_called = tls_init_tp_called; |
35f1e827 | 1814 | if (tcbp == NULL) |
9dcafc55 | 1815 | tcbp = init_tls (); |
0ecb606c | 1816 | |
35f1e827 UD |
1817 | /* Set up the stack checker's canary. */ |
1818 | uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (); | |
1819 | #ifdef THREAD_SET_STACK_GUARD | |
1820 | THREAD_SET_STACK_GUARD (stack_chk_guard); | |
1821 | #else | |
1822 | __stack_chk_guard = stack_chk_guard; | |
1823 | #endif | |
1824 | ||
827b7087 UD |
1825 | /* Set up the pointer guard as well, if necessary. */ |
1826 | if (GLRO(dl_pointer_guard)) | |
1827 | { | |
1828 | // XXX If it is cheap, we should use a separate value. | |
7725f874 UD |
1829 | uintptr_t pointer_chk_guard = stack_chk_guard; |
1830 | #ifndef HP_TIMING_NONAVAIL | |
827b7087 UD |
1831 | hp_timing_t now; |
1832 | HP_TIMING_NOW (now); | |
7725f874 UD |
1833 | pointer_chk_guard ^= now; |
1834 | #endif | |
827b7087 UD |
1835 | #ifdef THREAD_SET_POINTER_GUARD |
1836 | THREAD_SET_POINTER_GUARD (pointer_chk_guard); | |
1837 | #endif | |
1838 | __pointer_chk_guard_local = pointer_chk_guard; | |
1839 | } | |
1840 | ||
9a821cf9 | 1841 | if (__builtin_expect (mode, normal) != normal) |
0200214b RM |
1842 | { |
1843 | /* We were run just to list the shared libraries. It is | |
1844 | important that we do this before real relocation, because the | |
1845 | functions we call below for output may no longer work properly | |
1846 | after relocation. */ | |
81f3ac4c UD |
1847 | struct link_map *l; |
1848 | ||
afdca0f2 | 1849 | if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) |
ceb2d9aa | 1850 | { |
c0f62c56 | 1851 | struct r_scope_elem *scope = &main_map->l_searchlist; |
ceb2d9aa | 1852 | |
81f3ac4c | 1853 | for (i = 0; i < scope->r_nlist; i++) |
32e6df36 | 1854 | { |
81f3ac4c UD |
1855 | l = scope->r_list [i]; |
1856 | if (l->l_faked) | |
32e6df36 | 1857 | { |
81f3ac4c UD |
1858 | _dl_printf ("\t%s => not found\n", l->l_libname->name); |
1859 | continue; | |
1860 | } | |
afdca0f2 UD |
1861 | if (_dl_name_match_p (GLRO(dl_trace_prelink), l)) |
1862 | GLRO(dl_trace_prelink_map) = l; | |
81f3ac4c UD |
1863 | _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)", |
1864 | l->l_libname->name[0] ? l->l_libname->name | |
1865 | : rtld_progname ?: "<main program>", | |
1866 | l->l_name[0] ? l->l_name | |
1867 | : rtld_progname ?: "<main program>", | |
d347a4ab UD |
1868 | (int) sizeof l->l_map_start * 2, |
1869 | (size_t) l->l_map_start, | |
1870 | (int) sizeof l->l_addr * 2, | |
1871 | (size_t) l->l_addr); | |
11bf311e | 1872 | |
81f3ac4c UD |
1873 | if (l->l_tls_modid) |
1874 | _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid, | |
1875 | (int) sizeof l->l_tls_offset * 2, | |
d347a4ab | 1876 | (size_t) l->l_tls_offset); |
81f3ac4c | 1877 | else |
81f3ac4c | 1878 | _dl_printf ("\n"); |
32e6df36 | 1879 | } |
ceb2d9aa | 1880 | } |
7a11603d UD |
1881 | else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) |
1882 | { | |
1883 | /* Look through the dependencies of the main executable | |
1884 | and determine which of them is not actually | |
1885 | required. */ | |
c0f62c56 | 1886 | struct link_map *l = main_map; |
7a11603d UD |
1887 | |
1888 | /* Relocate the main executable. */ | |
1889 | struct relocate_args args = { .l = l, .lazy = GLRO(dl_lazy) }; | |
1890 | _dl_receive_error (print_unresolved, relocate_doit, &args); | |
1891 | ||
1892 | /* This loop depends on the dependencies of the executable to | |
1893 | correspond in number and order to the DT_NEEDED entries. */ | |
c0f62c56 | 1894 | ElfW(Dyn) *dyn = main_map->l_ld; |
7a11603d UD |
1895 | bool first = true; |
1896 | while (dyn->d_tag != DT_NULL) | |
1897 | { | |
1898 | if (dyn->d_tag == DT_NEEDED) | |
1899 | { | |
1900 | l = l->l_next; | |
1901 | ||
1902 | if (!l->l_used) | |
1903 | { | |
1904 | if (first) | |
1905 | { | |
1906 | _dl_printf ("Unused direct dependencies:\n"); | |
1907 | first = false; | |
1908 | } | |
1909 | ||
1910 | _dl_printf ("\t%s\n", l->l_name); | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | ++dyn; | |
1915 | } | |
1916 | ||
1917 | _exit (first != true); | |
1918 | } | |
c0f62c56 | 1919 | else if (! main_map->l_info[DT_NEEDED]) |
81f3ac4c UD |
1920 | _dl_printf ("\tstatically linked\n"); |
1921 | else | |
1922 | { | |
c0f62c56 | 1923 | for (l = main_map->l_next; l; l = l->l_next) |
81f3ac4c UD |
1924 | if (l->l_faked) |
1925 | /* The library was not found. */ | |
1926 | _dl_printf ("\t%s => not found\n", l->l_libname->name); | |
75489693 | 1927 | else if (strcmp (l->l_libname->name, l->l_name) == 0) |
7a11603d UD |
1928 | _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name, |
1929 | (int) sizeof l->l_map_start * 2, | |
1930 | (size_t) l->l_map_start); | |
81f3ac4c UD |
1931 | else |
1932 | _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name, | |
1933 | l->l_name, (int) sizeof l->l_map_start * 2, | |
d347a4ab | 1934 | (size_t) l->l_map_start); |
81f3ac4c | 1935 | } |
1a3a58fd | 1936 | |
9a821cf9 | 1937 | if (__builtin_expect (mode, trace) != trace) |
5a47e7f2 | 1938 | for (i = 1; i < (unsigned int) _dl_argc; ++i) |
cddcfecf RM |
1939 | { |
1940 | const ElfW(Sym) *ref = NULL; | |
c0282c06 UD |
1941 | ElfW(Addr) loadbase; |
1942 | lookup_t result; | |
c0282c06 | 1943 | |
c0f62c56 | 1944 | result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map, |
11bf311e UD |
1945 | &ref, main_map->l_scope, |
1946 | NULL, ELF_RTYPE_CLASS_PLT, | |
021723ab | 1947 | DL_LOOKUP_ADD_DEPENDENCY, NULL); |
c0282c06 UD |
1948 | |
1949 | loadbase = LOOKUP_VALUE_ADDRESS (result); | |
1950 | ||
35fc382a | 1951 | _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n", |
e6caf4e1 | 1952 | INTUSE(_dl_argv)[i], |
d347a4ab UD |
1953 | (int) sizeof ref->st_value * 2, |
1954 | (size_t) ref->st_value, | |
1955 | (int) sizeof loadbase * 2, (size_t) loadbase); | |
cddcfecf | 1956 | } |
ce37fa88 | 1957 | else |
fd26970f | 1958 | { |
20fe49b9 | 1959 | /* If LD_WARN is set, warn about undefined symbols. */ |
afdca0f2 | 1960 | if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose)) |
ce37fa88 UD |
1961 | { |
1962 | /* We have to do symbol dependency testing. */ | |
1963 | struct relocate_args args; | |
1964 | struct link_map *l; | |
993b3242 | 1965 | |
afdca0f2 | 1966 | args.lazy = GLRO(dl_lazy); |
fd26970f | 1967 | |
c0f62c56 | 1968 | l = main_map; |
20fe49b9 | 1969 | while (l->l_next != NULL) |
ce37fa88 UD |
1970 | l = l->l_next; |
1971 | do | |
1972 | { | |
d6b5d570 | 1973 | if (l != &GL(dl_rtld_map) && ! l->l_faked) |
ce37fa88 UD |
1974 | { |
1975 | args.l = l; | |
1976 | _dl_receive_error (print_unresolved, relocate_doit, | |
1977 | &args); | |
ce37fa88 UD |
1978 | } |
1979 | l = l->l_prev; | |
20fe49b9 UD |
1980 | } |
1981 | while (l != NULL); | |
32e6df36 | 1982 | |
afdca0f2 | 1983 | if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) |
20fe49b9 | 1984 | && rtld_multiple_ref) |
e38c954b UD |
1985 | { |
1986 | /* Mark the link map as not yet relocated again. */ | |
1987 | GL(dl_rtld_map).l_relocated = 0; | |
11bf311e UD |
1988 | _dl_relocate_object (&GL(dl_rtld_map), |
1989 | main_map->l_scope, 0, 0); | |
e38c954b UD |
1990 | } |
1991 | } | |
b0982c4a | 1992 | #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED)) |
120b4c49 | 1993 | if (version_info) |
fd26970f | 1994 | { |
ce37fa88 UD |
1995 | /* Print more information. This means here, print information |
1996 | about the versions needed. */ | |
1997 | int first = 1; | |
c0f62c56 | 1998 | struct link_map *map; |
ce37fa88 | 1999 | |
c0f62c56 | 2000 | for (map = main_map; map != NULL; map = map->l_next) |
fd26970f | 2001 | { |
f41c8091 | 2002 | const char *strtab; |
ce37fa88 | 2003 | ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG]; |
f41c8091 UD |
2004 | ElfW(Verneed) *ent; |
2005 | ||
2006 | if (dyn == NULL) | |
2007 | continue; | |
2008 | ||
a42195db | 2009 | strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
f41c8091 | 2010 | ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr); |
ce37fa88 | 2011 | |
f41c8091 | 2012 | if (first) |
ce37fa88 | 2013 | { |
35fc382a | 2014 | _dl_printf ("\n\tVersion information:\n"); |
f41c8091 UD |
2015 | first = 0; |
2016 | } | |
ce37fa88 | 2017 | |
35fc382a | 2018 | _dl_printf ("\t%s:\n", |
e6caf4e1 | 2019 | map->l_name[0] ? map->l_name : rtld_progname); |
f41c8091 UD |
2020 | |
2021 | while (1) | |
2022 | { | |
2023 | ElfW(Vernaux) *aux; | |
2024 | struct link_map *needed; | |
ce37fa88 | 2025 | |
f41c8091 UD |
2026 | needed = find_needed (strtab + ent->vn_file); |
2027 | aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux); | |
ce37fa88 UD |
2028 | |
2029 | while (1) | |
2030 | { | |
f41c8091 UD |
2031 | const char *fname = NULL; |
2032 | ||
f41c8091 | 2033 | if (needed != NULL |
ba9fcb3f UD |
2034 | && match_version (strtab + aux->vna_name, |
2035 | needed)) | |
f41c8091 UD |
2036 | fname = needed->l_name; |
2037 | ||
35fc382a UD |
2038 | _dl_printf ("\t\t%s (%s) %s=> %s\n", |
2039 | strtab + ent->vn_file, | |
2040 | strtab + aux->vna_name, | |
2041 | aux->vna_flags & VER_FLG_WEAK | |
2042 | ? "[WEAK] " : "", | |
2043 | fname ?: "not found"); | |
ce37fa88 | 2044 | |
f41c8091 UD |
2045 | if (aux->vna_next == 0) |
2046 | /* No more symbols. */ | |
ce37fa88 UD |
2047 | break; |
2048 | ||
f41c8091 UD |
2049 | /* Next symbol. */ |
2050 | aux = (ElfW(Vernaux) *) ((char *) aux | |
2051 | + aux->vna_next); | |
ce37fa88 | 2052 | } |
f41c8091 UD |
2053 | |
2054 | if (ent->vn_next == 0) | |
2055 | /* No more dependencies. */ | |
2056 | break; | |
2057 | ||
2058 | /* Next dependency. */ | |
2059 | ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next); | |
ce37fa88 | 2060 | } |
fd26970f | 2061 | } |
ce37fa88 | 2062 | } |
fd26970f | 2063 | } |
d66e34cd | 2064 | |
0200214b RM |
2065 | _exit (0); |
2066 | } | |
86d2c878 | 2067 | |
c0f62c56 | 2068 | if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)] |
768027a4 UD |
2069 | && ! __builtin_expect (GLRO(dl_profile) != NULL, 0) |
2070 | && ! __builtin_expect (GLRO(dl_dynamic_weak), 0)) | |
32e6df36 UD |
2071 | { |
2072 | ElfW(Lib) *liblist, *liblistend; | |
2073 | struct link_map **r_list, **r_listend, *l; | |
c0f62c56 | 2074 | const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]); |
32e6df36 | 2075 | |
c0f62c56 | 2076 | assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL); |
32e6df36 | 2077 | liblist = (ElfW(Lib) *) |
c0f62c56 | 2078 | main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr; |
32e6df36 | 2079 | liblistend = (ElfW(Lib) *) |
c0f62c56 UD |
2080 | ((char *) liblist + |
2081 | main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val); | |
2082 | r_list = main_map->l_searchlist.r_list; | |
2083 | r_listend = r_list + main_map->l_searchlist.r_nlist; | |
32e6df36 UD |
2084 | |
2085 | for (; r_list < r_listend && liblist < liblistend; r_list++) | |
2086 | { | |
2087 | l = *r_list; | |
2088 | ||
c0f62c56 | 2089 | if (l == main_map) |
32e6df36 UD |
2090 | continue; |
2091 | ||
2092 | /* If the library is not mapped where it should, fail. */ | |
2093 | if (l->l_addr) | |
2094 | break; | |
2095 | ||
2096 | /* Next, check if checksum matches. */ | |
2097 | if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL | |
2098 | || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val | |
2099 | != liblist->l_checksum) | |
2100 | break; | |
2101 | ||
2102 | if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL | |
2103 | || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val | |
2104 | != liblist->l_time_stamp) | |
2105 | break; | |
2106 | ||
2107 | if (! _dl_name_match_p (strtab + liblist->l_name, l)) | |
2108 | break; | |
2109 | ||
2110 | ++liblist; | |
2111 | } | |
2112 | ||
2113 | ||
2114 | if (r_list == r_listend && liblist == liblistend) | |
164a7164 | 2115 | prelinked = true; |
32e6df36 | 2116 | |
afdca0f2 | 2117 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)) |
b85a0f39 UD |
2118 | _dl_debug_printf ("\nprelink checking: %s\n", |
2119 | prelinked ? "ok" : "failed"); | |
32e6df36 UD |
2120 | } |
2121 | ||
ed20b3d9 | 2122 | |
c31e278f | 2123 | /* Now set up the variable which helps the assembler startup code. */ |
c0f62c56 | 2124 | GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist; |
c31e278f UD |
2125 | |
2126 | /* Save the information about the original global scope list since | |
2127 | we need it in the memory handling later. */ | |
c0f62c56 | 2128 | GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist; |
c31e278f | 2129 | |
32e6df36 UD |
2130 | if (prelinked) |
2131 | { | |
c0f62c56 | 2132 | if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL) |
32e6df36 UD |
2133 | { |
2134 | ElfW(Rela) *conflict, *conflictend; | |
2135 | #ifndef HP_TIMING_NONAVAIL | |
2136 | hp_timing_t start; | |
2137 | hp_timing_t stop; | |
2138 | #endif | |
2139 | ||
2140 | HP_TIMING_NOW (start); | |
c0f62c56 | 2141 | assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL); |
32e6df36 | 2142 | conflict = (ElfW(Rela) *) |
c0f62c56 | 2143 | main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr; |
32e6df36 | 2144 | conflictend = (ElfW(Rela) *) |
d89ae1d5 | 2145 | ((char *) conflict |
c0f62c56 UD |
2146 | + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val); |
2147 | _dl_resolve_conflicts (main_map, conflict, conflictend); | |
32e6df36 UD |
2148 | HP_TIMING_NOW (stop); |
2149 | HP_TIMING_DIFF (relocate_time, start, stop); | |
2150 | } | |
2151 | ||
d89ae1d5 RM |
2152 | |
2153 | /* Mark all the objects so we know they have been already relocated. */ | |
9dcafc55 | 2154 | for (struct link_map *l = main_map; l != NULL; l = l->l_next) |
e8648a5a UD |
2155 | { |
2156 | l->l_relocated = 1; | |
2157 | if (l->l_relro_size) | |
2158 | _dl_protect_relro (l); | |
9dcafc55 UD |
2159 | |
2160 | /* Add object to slot information data if necessasy. */ | |
2161 | if (l->l_tls_blocksize != 0 && tls_init_tp_called) | |
2162 | _dl_add_to_slotinfo (l); | |
e8648a5a | 2163 | } |
d89ae1d5 | 2164 | |
32e6df36 UD |
2165 | _dl_sysdep_start_cleanup (); |
2166 | } | |
2167 | else | |
164a7164 UD |
2168 | { |
2169 | /* Now we have all the objects loaded. Relocate them all except for | |
2170 | the dynamic linker itself. We do this in reverse order so that copy | |
2171 | relocs of earlier objects overwrite the data written by later | |
2172 | objects. We do not re-relocate the dynamic linker itself in this | |
2173 | loop because that could result in the GOT entries for functions we | |
2174 | call being changed, and that would break us. It is safe to relocate | |
2175 | the dynamic linker out of order because it has no copy relocs (we | |
2176 | know that because it is self-contained). */ | |
2177 | ||
afdca0f2 | 2178 | int consider_profiling = GLRO(dl_profile) != NULL; |
8b07d6a8 | 2179 | #ifndef HP_TIMING_NONAVAIL |
164a7164 UD |
2180 | hp_timing_t start; |
2181 | hp_timing_t stop; | |
8b07d6a8 | 2182 | #endif |
c0fb8a56 | 2183 | |
164a7164 | 2184 | /* If we are profiling we also must do lazy reloaction. */ |
afdca0f2 | 2185 | GLRO(dl_lazy) |= consider_profiling; |
c0fb8a56 | 2186 | |
9dcafc55 | 2187 | struct link_map *l = main_map; |
164a7164 UD |
2188 | while (l->l_next) |
2189 | l = l->l_next; | |
db276fa1 | 2190 | |
164a7164 UD |
2191 | HP_TIMING_NOW (start); |
2192 | do | |
2193 | { | |
2194 | /* While we are at it, help the memory handling a bit. We have to | |
2195 | mark some data structures as allocated with the fake malloc() | |
2196 | implementation in ld.so. */ | |
2197 | struct libname_list *lnp = l->l_libname->next; | |
752a2a50 | 2198 | |
164a7164 UD |
2199 | while (__builtin_expect (lnp != NULL, 0)) |
2200 | { | |
2201 | lnp->dont_free = 1; | |
2202 | lnp = lnp->next; | |
2203 | } | |
752a2a50 | 2204 | |
164a7164 | 2205 | if (l != &GL(dl_rtld_map)) |
c0a777e8 | 2206 | _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy), |
154d10bd | 2207 | consider_profiling); |
be935610 | 2208 | |
9dcafc55 UD |
2209 | /* Add object to slot information data if necessasy. */ |
2210 | if (l->l_tls_blocksize != 0 && tls_init_tp_called) | |
2211 | _dl_add_to_slotinfo (l); | |
2212 | ||
164a7164 UD |
2213 | l = l->l_prev; |
2214 | } | |
2215 | while (l); | |
2216 | HP_TIMING_NOW (stop); | |
2217 | ||
2218 | HP_TIMING_DIFF (relocate_time, start, stop); | |
2219 | ||
2220 | /* Do any necessary cleanups for the startup OS interface code. | |
2221 | We do these now so that no calls are made after rtld re-relocation | |
2222 | which might be resolved to different functions than we expect. | |
2223 | We cannot do this before relocating the other objects because | |
2224 | _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */ | |
2225 | _dl_sysdep_start_cleanup (); | |
2226 | ||
2227 | /* Now enable profiling if needed. Like the previous call, | |
2228 | this has to go here because the calls it makes should use the | |
2229 | rtld versions of the functions (particularly calloc()), but it | |
2230 | needs to have _dl_profile_map set up by the relocator. */ | |
2231 | if (__builtin_expect (GL(dl_profile_map) != NULL, 0)) | |
2232 | /* We must prepare the profiling. */ | |
53bfdc1c | 2233 | _dl_start_profile (); |
164a7164 | 2234 | } |
ac16e905 | 2235 | |
131fd126 UD |
2236 | #ifndef NONTLS_INIT_TP |
2237 | # define NONTLS_INIT_TP do { } while (0) | |
2238 | #endif | |
2239 | ||
35f1e827 UD |
2240 | if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0) |
2241 | ++GL(dl_tls_generation); | |
9dcafc55 | 2242 | |
35f1e827 UD |
2243 | /* Now that we have completed relocation, the initializer data |
2244 | for the TLS blocks has its final values and we can copy them | |
2245 | into the main thread's TLS area, which we allocated above. */ | |
2246 | _dl_allocate_tls_init (tcbp); | |
a334319f | 2247 | |
35f1e827 UD |
2248 | /* And finally install it for the main thread. If ld.so itself uses |
2249 | TLS we know the thread pointer was initialized earlier. */ | |
2250 | if (! tls_init_tp_called) | |
2251 | { | |
2252 | const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD); | |
2253 | if (__builtin_expect (lossage != NULL, 0)) | |
2254 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", | |
2255 | lossage); | |
0ecb606c | 2256 | } |
0ecb606c | 2257 | |
cafdfdb6 RM |
2258 | if (! prelinked && rtld_multiple_ref) |
2259 | { | |
2260 | /* There was an explicit ref to the dynamic linker as a shared lib. | |
2261 | Re-relocate ourselves with user-controlled symbol definitions. | |
2262 | ||
2263 | We must do this after TLS initialization in case after this | |
2264 | re-relocation, we might call a user-supplied function | |
2265 | (e.g. calloc from _dl_relocate_object) that uses TLS data. */ | |
2266 | ||
2267 | #ifndef HP_TIMING_NONAVAIL | |
2268 | hp_timing_t start; | |
2269 | hp_timing_t stop; | |
2270 | hp_timing_t add; | |
2271 | #endif | |
2272 | ||
2273 | HP_TIMING_NOW (start); | |
2274 | /* Mark the link map as not yet relocated again. */ | |
2275 | GL(dl_rtld_map).l_relocated = 0; | |
c0a777e8 | 2276 | _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0); |
cafdfdb6 RM |
2277 | HP_TIMING_NOW (stop); |
2278 | HP_TIMING_DIFF (add, start, stop); | |
2279 | HP_TIMING_ACCUM_NT (relocate_time, add); | |
2280 | } | |
2281 | ||
9dcafc55 UD |
2282 | #ifdef SHARED |
2283 | /* Auditing checkpoint: we have added all objects. */ | |
2284 | if (__builtin_expect (GLRO(dl_naudit) > 0, 0)) | |
2285 | { | |
2286 | struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded; | |
2287 | /* Do not call the functions for any auditing object. */ | |
2288 | if (head->l_auditing == 0) | |
2289 | { | |
2290 | struct audit_ifaces *afct = GLRO(dl_audit); | |
2291 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) | |
2292 | { | |
2293 | if (afct->activity != NULL) | |
2294 | afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT); | |
2295 | ||
2296 | afct = afct->next; | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | #endif | |
2301 | ||
2302 | /* Notify the debugger all new objects are now ready to go. We must re-get | |
2303 | the address since by now the variable might be in another object. */ | |
29f97654 | 2304 | r = _dl_debug_initialize (0, LM_ID_BASE); |
9dcafc55 | 2305 | r->r_state = RT_CONSISTENT; |
154d10bd | 2306 | _dl_debug_state (); |
0200214b | 2307 | |
08cac4ac UD |
2308 | #ifndef MAP_COPY |
2309 | /* We must munmap() the cache file. */ | |
154d10bd | 2310 | _dl_unload_cache (); |
08cac4ac UD |
2311 | #endif |
2312 | ||
d66e34cd RM |
2313 | /* Once we return, _dl_sysdep_start will invoke |
2314 | the DT_INIT functions and then *USER_ENTRY. */ | |
2315 | } | |
fd26970f UD |
2316 | \f |
2317 | /* This is a little helper function for resolving symbols while | |
2318 | tracing the binary. */ | |
2319 | static void | |
c84142e8 UD |
2320 | print_unresolved (int errcode __attribute__ ((unused)), const char *objname, |
2321 | const char *errstring) | |
fd26970f | 2322 | { |
3996f34b | 2323 | if (objname[0] == '\0') |
e6caf4e1 | 2324 | objname = rtld_progname ?: "<main program>"; |
35fc382a | 2325 | _dl_error_printf ("%s (%s)\n", errstring, objname); |
fd26970f | 2326 | } |
c84142e8 UD |
2327 | \f |
2328 | /* This is a little helper function for resolving symbols while | |
2329 | tracing the binary. */ | |
2330 | static void | |
2331 | print_missing_version (int errcode __attribute__ ((unused)), | |
2332 | const char *objname, const char *errstring) | |
2333 | { | |
e6caf4e1 | 2334 | _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>", |
35fc382a | 2335 | objname, errstring); |
c84142e8 | 2336 | } |
ea278354 | 2337 | \f |
7dea968e | 2338 | /* Nonzero if any of the debugging options is enabled. */ |
392a6b52 | 2339 | static int any_debug attribute_relro; |
7dea968e | 2340 | |
b5efde2f UD |
2341 | /* Process the string given as the parameter which explains which debugging |
2342 | options are enabled. */ | |
2343 | static void | |
14c44e2e | 2344 | process_dl_debug (const char *dl_debug) |
b5efde2f | 2345 | { |
3e2040c8 UD |
2346 | /* When adding new entries make sure that the maximal length of a name |
2347 | is correctly handled in the LD_DEBUG_HELP code below. */ | |
2348 | static const struct | |
2349 | { | |
379d4ec4 UD |
2350 | unsigned char len; |
2351 | const char name[10]; | |
3e2040c8 UD |
2352 | const char helptext[41]; |
2353 | unsigned short int mask; | |
2354 | } debopts[] = | |
2355 | { | |
379d4ec4 UD |
2356 | #define LEN_AND_STR(str) sizeof (str) - 1, str |
2357 | { LEN_AND_STR ("libs"), "display library search paths", | |
3e2040c8 | 2358 | DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2359 | { LEN_AND_STR ("reloc"), "display relocation processing", |
3e2040c8 | 2360 | DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2361 | { LEN_AND_STR ("files"), "display progress for input file", |
3e2040c8 | 2362 | DL_DEBUG_FILES | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2363 | { LEN_AND_STR ("symbols"), "display symbol table processing", |
3e2040c8 | 2364 | DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2365 | { LEN_AND_STR ("bindings"), "display information about symbol binding", |
3e2040c8 | 2366 | DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2367 | { LEN_AND_STR ("versions"), "display version dependencies", |
3e2040c8 | 2368 | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2369 | { LEN_AND_STR ("all"), "all previous options combined", |
3e2040c8 UD |
2370 | DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS |
2371 | | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS }, | |
379d4ec4 | 2372 | { LEN_AND_STR ("statistics"), "display relocation statistics", |
3e2040c8 | 2373 | DL_DEBUG_STATISTICS }, |
7a11603d UD |
2374 | { LEN_AND_STR ("unused"), "determined unused DSOs", |
2375 | DL_DEBUG_UNUSED }, | |
379d4ec4 | 2376 | { LEN_AND_STR ("help"), "display this help message and exit", |
3e2040c8 UD |
2377 | DL_DEBUG_HELP }, |
2378 | }; | |
2379 | #define ndebopts (sizeof (debopts) / sizeof (debopts[0])) | |
3e2040c8 | 2380 | |
379d4ec4 UD |
2381 | /* Skip separating white spaces and commas. */ |
2382 | while (*dl_debug != '\0') | |
b5efde2f | 2383 | { |
379d4ec4 | 2384 | if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':') |
b5efde2f | 2385 | { |
3e2040c8 | 2386 | size_t cnt; |
379d4ec4 | 2387 | size_t len = 1; |
77aba05b | 2388 | |
379d4ec4 UD |
2389 | while (dl_debug[len] != '\0' && dl_debug[len] != ' ' |
2390 | && dl_debug[len] != ',' && dl_debug[len] != ':') | |
2391 | ++len; | |
14c44e2e | 2392 | |
3e2040c8 | 2393 | for (cnt = 0; cnt < ndebopts; ++cnt) |
379d4ec4 UD |
2394 | if (debopts[cnt].len == len |
2395 | && memcmp (dl_debug, debopts[cnt].name, len) == 0) | |
3e2040c8 | 2396 | { |
afdca0f2 | 2397 | GLRO(dl_debug_mask) |= debopts[cnt].mask; |
5688da55 | 2398 | any_debug = 1; |
3e2040c8 UD |
2399 | break; |
2400 | } | |
77aba05b | 2401 | |
3e2040c8 UD |
2402 | if (cnt == ndebopts) |
2403 | { | |
2404 | /* Display a warning and skip everything until next | |
2405 | separator. */ | |
2406 | char *copy = strndupa (dl_debug, len); | |
2407 | _dl_error_printf ("\ | |
2408 | warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy); | |
379d4ec4 UD |
2409 | } |
2410 | ||
2411 | dl_debug += len; | |
2412 | continue; | |
3e2040c8 | 2413 | } |
379d4ec4 UD |
2414 | |
2415 | ++dl_debug; | |
3e2040c8 | 2416 | } |
77aba05b | 2417 | |
afdca0f2 | 2418 | if (GLRO(dl_debug_mask) & DL_DEBUG_HELP) |
3e2040c8 UD |
2419 | { |
2420 | size_t cnt; | |
14c44e2e | 2421 | |
3e2040c8 UD |
2422 | _dl_printf ("\ |
2423 | Valid options for the LD_DEBUG environment variable are:\n\n"); | |
db276fa1 | 2424 | |
3e2040c8 | 2425 | for (cnt = 0; cnt < ndebopts; ++cnt) |
37d8b778 UD |
2426 | _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name, |
2427 | " " + debopts[cnt].len - 3, | |
3e2040c8 | 2428 | debopts[cnt].helptext); |
14c44e2e | 2429 | |
3e2040c8 UD |
2430 | _dl_printf ("\n\ |
2431 | To direct the debugging output into a file instead of standard output\n\ | |
2432 | a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n"); | |
2433 | _exit (0); | |
b5efde2f | 2434 | } |
b5efde2f UD |
2435 | } |
2436 | \f | |
9dcafc55 UD |
2437 | static void |
2438 | process_dl_audit (char *str) | |
2439 | { | |
2440 | /* The parameter is a colon separated list of DSO names. */ | |
2441 | char *p; | |
2442 | ||
2443 | while ((p = (strsep) (&str, ":")) != NULL) | |
2444 | if (p[0] != '\0' | |
2445 | && (__builtin_expect (! INTUSE(__libc_enable_secure), 1) | |
2446 | || strchr (p, '/') == NULL)) | |
2447 | { | |
2448 | /* This is using the local malloc, not the system malloc. The | |
2449 | memory can never be freed. */ | |
2450 | struct audit_list *newp = malloc (sizeof (*newp)); | |
2451 | newp->name = p; | |
2452 | ||
2453 | if (audit_list == NULL) | |
2454 | audit_list = newp->next = newp; | |
2455 | else | |
2456 | { | |
2457 | newp->next = audit_list->next; | |
2458 | audit_list = audit_list->next = newp; | |
2459 | } | |
2460 | } | |
2461 | } | |
2462 | \f | |
ea278354 UD |
2463 | /* Process all environments variables the dynamic linker must recognize. |
2464 | Since all of them start with `LD_' we are a bit smarter while finding | |
2465 | all the entries. */ | |
9360906d | 2466 | extern char **_environ attribute_hidden; |
67c94753 | 2467 | |
d6b5d570 | 2468 | |
ea278354 | 2469 | static void |
ba9fcb3f | 2470 | process_envvars (enum mode *modep) |
ea278354 | 2471 | { |
67c94753 | 2472 | char **runp = _environ; |
ea278354 UD |
2473 | char *envline; |
2474 | enum mode mode = normal; | |
7dea968e | 2475 | char *debug_output = NULL; |
ea278354 UD |
2476 | |
2477 | /* This is the default place for profiling data file. */ | |
afdca0f2 | 2478 | GLRO(dl_profile_output) |
e6caf4e1 | 2479 | = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0]; |
ea278354 UD |
2480 | |
2481 | while ((envline = _dl_next_ld_env_entry (&runp)) != NULL) | |
2482 | { | |
379d4ec4 UD |
2483 | size_t len = 0; |
2484 | ||
2485 | while (envline[len] != '\0' && envline[len] != '=') | |
2486 | ++len; | |
ea278354 | 2487 | |
75e8d1f5 UD |
2488 | if (envline[len] != '=') |
2489 | /* This is a "LD_" variable at the end of the string without | |
2490 | a '=' character. Ignore it since otherwise we will access | |
2491 | invalid memory below. */ | |
67c94753 | 2492 | continue; |
75e8d1f5 | 2493 | |
67c94753 | 2494 | switch (len) |
ea278354 | 2495 | { |
14c44e2e UD |
2496 | case 4: |
2497 | /* Warning level, verbose or not. */ | |
67c94753 | 2498 | if (memcmp (envline, "WARN", 4) == 0) |
afdca0f2 | 2499 | GLRO(dl_verbose) = envline[5] != '\0'; |
14c44e2e | 2500 | break; |
ea278354 | 2501 | |
14c44e2e UD |
2502 | case 5: |
2503 | /* Debugging of the dynamic linker? */ | |
67c94753 | 2504 | if (memcmp (envline, "DEBUG", 5) == 0) |
9dcafc55 UD |
2505 | { |
2506 | process_dl_debug (&envline[6]); | |
2507 | break; | |
2508 | } | |
2509 | if (memcmp (envline, "AUDIT", 5) == 0) | |
2510 | process_dl_audit (&envline[6]); | |
14c44e2e | 2511 | break; |
b5efde2f | 2512 | |
14c44e2e UD |
2513 | case 7: |
2514 | /* Print information about versions. */ | |
67c94753 | 2515 | if (memcmp (envline, "VERBOSE", 7) == 0) |
14c44e2e | 2516 | { |
67c94753 | 2517 | version_info = envline[8] != '\0'; |
14c44e2e UD |
2518 | break; |
2519 | } | |
7dea968e | 2520 | |
14c44e2e | 2521 | /* List of objects to be preloaded. */ |
67c94753 | 2522 | if (memcmp (envline, "PRELOAD", 7) == 0) |
14c44e2e | 2523 | { |
67c94753 | 2524 | preloadlist = &envline[8]; |
14c44e2e UD |
2525 | break; |
2526 | } | |
120b4c49 | 2527 | |
14c44e2e | 2528 | /* Which shared object shall be profiled. */ |
c95f3fd4 | 2529 | if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0') |
afdca0f2 | 2530 | GLRO(dl_profile) = &envline[8]; |
14c44e2e | 2531 | break; |
120b4c49 | 2532 | |
14c44e2e UD |
2533 | case 8: |
2534 | /* Do we bind early? */ | |
67c94753 | 2535 | if (memcmp (envline, "BIND_NOW", 8) == 0) |
f53c03c2 | 2536 | { |
afdca0f2 | 2537 | GLRO(dl_lazy) = envline[9] == '\0'; |
f53c03c2 UD |
2538 | break; |
2539 | } | |
67c94753 | 2540 | if (memcmp (envline, "BIND_NOT", 8) == 0) |
afdca0f2 | 2541 | GLRO(dl_bind_not) = envline[9] != '\0'; |
14c44e2e | 2542 | break; |
ea278354 | 2543 | |
14c44e2e UD |
2544 | case 9: |
2545 | /* Test whether we want to see the content of the auxiliary | |
2546 | array passed up from the kernel. */ | |
00a12162 UD |
2547 | if (!INTUSE(__libc_enable_secure) |
2548 | && memcmp (envline, "SHOW_AUXV", 9) == 0) | |
14c44e2e UD |
2549 | _dl_show_auxv (); |
2550 | break; | |
ea278354 | 2551 | |
12264bd7 | 2552 | case 10: |
3081378b | 2553 | /* Mask for the important hardware capabilities. */ |
67c94753 | 2554 | if (memcmp (envline, "HWCAP_MASK", 10) == 0) |
afdca0f2 UD |
2555 | GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, |
2556 | 0, 0); | |
12264bd7 UD |
2557 | break; |
2558 | ||
f787edde UD |
2559 | case 11: |
2560 | /* Path where the binary is found. */ | |
e6caf4e1 | 2561 | if (!INTUSE(__libc_enable_secure) |
67c94753 | 2562 | && memcmp (envline, "ORIGIN_PATH", 11) == 0) |
afdca0f2 | 2563 | GLRO(dl_origin_path) = &envline[12]; |
f787edde UD |
2564 | break; |
2565 | ||
14c44e2e | 2566 | case 12: |
dec126b4 | 2567 | /* The library search path. */ |
67c94753 | 2568 | if (memcmp (envline, "LIBRARY_PATH", 12) == 0) |
dec126b4 | 2569 | { |
67c94753 | 2570 | library_path = &envline[13]; |
dec126b4 UD |
2571 | break; |
2572 | } | |
2573 | ||
14c44e2e | 2574 | /* Where to place the profiling data file. */ |
67c94753 | 2575 | if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0) |
14c44e2e | 2576 | { |
67c94753 | 2577 | debug_output = &envline[13]; |
14c44e2e UD |
2578 | break; |
2579 | } | |
ea278354 | 2580 | |
00a12162 UD |
2581 | if (!INTUSE(__libc_enable_secure) |
2582 | && memcmp (envline, "DYNAMIC_WEAK", 12) == 0) | |
afdca0f2 | 2583 | GLRO(dl_dynamic_weak) = 1; |
14c44e2e | 2584 | break; |
ea278354 | 2585 | |
97fd3a30 UD |
2586 | case 13: |
2587 | /* We might have some extra environment variable with length 13 | |
2588 | to handle. */ | |
2589 | #ifdef EXTRA_LD_ENVVARS_13 | |
2590 | EXTRA_LD_ENVVARS_13 | |
2591 | #endif | |
2592 | if (!INTUSE(__libc_enable_secure) | |
2593 | && memcmp (envline, "USE_LOAD_BIAS", 13) == 0) | |
827b7087 UD |
2594 | { |
2595 | GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0; | |
2596 | break; | |
2597 | } | |
2598 | ||
2599 | if (memcmp (envline, "POINTER_GUARD", 13) == 0) | |
0a21e400 | 2600 | GLRO(dl_pointer_guard) = envline[14] != '0'; |
97fd3a30 UD |
2601 | break; |
2602 | ||
14c44e2e UD |
2603 | case 14: |
2604 | /* Where to place the profiling data file. */ | |
e6caf4e1 | 2605 | if (!INTUSE(__libc_enable_secure) |
3e2040c8 UD |
2606 | && memcmp (envline, "PROFILE_OUTPUT", 14) == 0 |
2607 | && envline[15] != '\0') | |
afdca0f2 | 2608 | GLRO(dl_profile_output) = &envline[15]; |
14c44e2e | 2609 | break; |
120b4c49 | 2610 | |
32e6df36 UD |
2611 | case 16: |
2612 | /* The mode of the dynamic linker can be set. */ | |
2613 | if (memcmp (envline, "TRACE_PRELINKING", 16) == 0) | |
2614 | { | |
2615 | mode = trace; | |
afdca0f2 UD |
2616 | GLRO(dl_verbose) = 1; |
2617 | GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK; | |
2618 | GLRO(dl_trace_prelink) = &envline[17]; | |
32e6df36 UD |
2619 | } |
2620 | break; | |
2621 | ||
14c44e2e UD |
2622 | case 20: |
2623 | /* The mode of the dynamic linker can be set. */ | |
67c94753 | 2624 | if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0) |
14c44e2e UD |
2625 | mode = trace; |
2626 | break; | |
e2102c14 UD |
2627 | |
2628 | /* We might have some extra environment variable to handle. This | |
2629 | is tricky due to the pre-processing of the length of the name | |
2630 | in the switch statement here. The code here assumes that added | |
2631 | environment variables have a different length. */ | |
2632 | #ifdef EXTRA_LD_ENVVARS | |
2633 | EXTRA_LD_ENVVARS | |
2634 | #endif | |
ea278354 UD |
2635 | } |
2636 | } | |
2637 | ||
3e2040c8 UD |
2638 | /* The caller wants this information. */ |
2639 | *modep = mode; | |
2640 | ||
4bae5567 UD |
2641 | /* Extra security for SUID binaries. Remove all dangerous environment |
2642 | variables. */ | |
e6caf4e1 | 2643 | if (__builtin_expect (INTUSE(__libc_enable_secure), 0)) |
4bae5567 | 2644 | { |
c95f3fd4 | 2645 | static const char unsecure_envvars[] = |
4bae5567 UD |
2646 | #ifdef EXTRA_UNSECURE_ENVVARS |
2647 | EXTRA_UNSECURE_ENVVARS | |
2648 | #endif | |
c95f3fd4 UD |
2649 | UNSECURE_ENVVARS; |
2650 | const char *nextp; | |
2651 | ||
2652 | nextp = unsecure_envvars; | |
2653 | do | |
2654 | { | |
2655 | unsetenv (nextp); | |
9710f75d UD |
2656 | /* We could use rawmemchr but this need not be fast. */ |
2657 | nextp = (char *) (strchr) (nextp, '\0') + 1; | |
c95f3fd4 UD |
2658 | } |
2659 | while (*nextp != '\0'); | |
74955460 UD |
2660 | |
2661 | if (__access ("/etc/suid-debug", F_OK) != 0) | |
00a12162 UD |
2662 | { |
2663 | unsetenv ("MALLOC_CHECK_"); | |
f57a3c94 | 2664 | GLRO(dl_debug_mask) = 0; |
00a12162 | 2665 | } |
f57a3c94 RM |
2666 | |
2667 | if (mode != normal) | |
2668 | _exit (5); | |
4bae5567 | 2669 | } |
7dea968e UD |
2670 | /* If we have to run the dynamic linker in debugging mode and the |
2671 | LD_DEBUG_OUTPUT environment variable is given, we write the debug | |
2672 | messages to this file. */ | |
3e2040c8 | 2673 | else if (any_debug && debug_output != NULL) |
7dea968e | 2674 | { |
5f2de337 UD |
2675 | #ifdef O_NOFOLLOW |
2676 | const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW; | |
2677 | #else | |
2678 | const int flags = O_WRONLY | O_APPEND | O_CREAT; | |
2679 | #endif | |
7a2fd787 UD |
2680 | size_t name_len = strlen (debug_output); |
2681 | char buf[name_len + 12]; | |
2682 | char *startp; | |
2683 | ||
2684 | buf[name_len + 11] = '\0'; | |
9710f75d | 2685 | startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0); |
7a2fd787 UD |
2686 | *--startp = '.'; |
2687 | startp = memcpy (startp - name_len, debug_output, name_len); | |
2688 | ||
dd70526e UD |
2689 | GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE); |
2690 | if (GLRO(dl_debug_fd) == -1) | |
7dea968e | 2691 | /* We use standard output if opening the file failed. */ |
dd70526e | 2692 | GLRO(dl_debug_fd) = STDOUT_FILENO; |
7dea968e | 2693 | } |
ea278354 | 2694 | } |
db276fa1 UD |
2695 | |
2696 | ||
2697 | /* Print the various times we collected. */ | |
2698 | static void | |
ee600e3f | 2699 | __attribute ((noinline)) |
392a6b52 | 2700 | print_statistics (hp_timing_t *rtld_total_timep) |
db276fa1 | 2701 | { |
8b07d6a8 | 2702 | #ifndef HP_TIMING_NONAVAIL |
f457369d | 2703 | char buf[200]; |
db276fa1 UD |
2704 | char *cp; |
2705 | char *wp; | |
2706 | ||
2707 | /* Total time rtld used. */ | |
2708 | if (HP_TIMING_AVAIL) | |
2709 | { | |
392a6b52 | 2710 | HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep); |
154d10bd UD |
2711 | _dl_debug_printf ("\nruntime linker statistics:\n" |
2712 | " total startup time in dynamic loader: %s\n", buf); | |
db276fa1 | 2713 | |
392a6b52 | 2714 | /* Print relocation statistics. */ |
35fc382a | 2715 | char pbuf[30]; |
db276fa1 | 2716 | HP_TIMING_PRINT (buf, sizeof (buf), relocate_time); |
392a6b52 | 2717 | cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep, |
9710f75d | 2718 | pbuf + sizeof (pbuf), 10, 0); |
35fc382a UD |
2719 | wp = pbuf; |
2720 | switch (pbuf + sizeof (pbuf) - cp) | |
db276fa1 UD |
2721 | { |
2722 | case 3: | |
2723 | *wp++ = *cp++; | |
2724 | case 2: | |
2725 | *wp++ = *cp++; | |
2726 | case 1: | |
2727 | *wp++ = '.'; | |
2728 | *wp++ = *cp++; | |
2729 | } | |
2730 | *wp = '\0'; | |
154d10bd UD |
2731 | _dl_debug_printf ("\ |
2732 | time needed for relocation: %s (%s%%)\n", buf, pbuf); | |
db276fa1 | 2733 | } |
1531e094 | 2734 | #endif |
a21a20a3 UD |
2735 | |
2736 | unsigned long int num_relative_relocations = 0; | |
c0f62c56 | 2737 | for (Lmid_t ns = 0; ns < DL_NNS; ++ns) |
a21a20a3 | 2738 | { |
c120d94d UD |
2739 | if (GL(dl_ns)[ns]._ns_loaded == NULL) |
2740 | continue; | |
2741 | ||
c0f62c56 | 2742 | struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist; |
a21a20a3 | 2743 | |
c0f62c56 UD |
2744 | for (unsigned int i = 0; i < scope->r_nlist; i++) |
2745 | { | |
2746 | struct link_map *l = scope->r_list [i]; | |
2747 | ||
c120d94d | 2748 | if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)]) |
c0f62c56 UD |
2749 | num_relative_relocations |
2750 | += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val; | |
c120d94d UD |
2751 | #ifndef ELF_MACHINE_REL_RELATIVE |
2752 | /* Relative relocations are processed on these architectures if | |
2753 | library is loaded to different address than p_vaddr or | |
2754 | if not prelinked. */ | |
2755 | if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)]) | |
2756 | && l->l_info[VERSYMIDX (DT_RELACOUNT)]) | |
2757 | #else | |
2758 | /* On e.g. IA-64 or Alpha, relative relocations are processed | |
2759 | only if library is loaded to different address than p_vaddr. */ | |
2760 | if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)]) | |
2761 | #endif | |
c0f62c56 UD |
2762 | num_relative_relocations |
2763 | += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val; | |
2764 | } | |
a21a20a3 UD |
2765 | } |
2766 | ||
42af49f8 UD |
2767 | _dl_debug_printf (" number of relocations: %lu\n" |
2768 | " number of relocations from cache: %lu\n" | |
2769 | " number of relative relocations: %lu\n", | |
2770 | GL(dl_num_relocations), | |
2771 | GL(dl_num_cache_relocations), | |
154d10bd | 2772 | num_relative_relocations); |
db276fa1 | 2773 | |
1531e094 | 2774 | #ifndef HP_TIMING_NONAVAIL |
db276fa1 UD |
2775 | /* Time spend while loading the object and the dependencies. */ |
2776 | if (HP_TIMING_AVAIL) | |
2777 | { | |
35fc382a | 2778 | char pbuf[30]; |
db276fa1 | 2779 | HP_TIMING_PRINT (buf, sizeof (buf), load_time); |
392a6b52 | 2780 | cp = _itoa ((1000ULL * load_time) / *rtld_total_timep, |
9710f75d | 2781 | pbuf + sizeof (pbuf), 10, 0); |
35fc382a UD |
2782 | wp = pbuf; |
2783 | switch (pbuf + sizeof (pbuf) - cp) | |
db276fa1 UD |
2784 | { |
2785 | case 3: | |
2786 | *wp++ = *cp++; | |
2787 | case 2: | |
2788 | *wp++ = *cp++; | |
2789 | case 1: | |
2790 | *wp++ = '.'; | |
2791 | *wp++ = *cp++; | |
2792 | } | |
2793 | *wp = '\0'; | |
154d10bd | 2794 | _dl_debug_printf ("\ |
7969407a | 2795 | time needed to load objects: %s (%s%%)\n", |
cff26a3e | 2796 | buf, pbuf); |
db276fa1 | 2797 | } |
1531e094 | 2798 | #endif |
db276fa1 | 2799 | } |