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