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