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