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