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