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