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