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

source code of glibc/elf/rtld.c