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