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