1 /* Run time dynamic linker.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
27 #include <sys/param.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <libc-lock.h>
35 #include <unsecvars.h>
37 #include <dl-osinfo.h>
38 #include <dl-procinfo.h>
41 #include <dl-vdso-setup.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>
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>
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"
67 /* Must include after <dl-machine.h> for DT_MIPS definition. */
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. */
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)
83 rtld_timer_start (hp_timing_t
*var
)
89 rtld_timer_stop (hp_timing_t
*var
, hp_timing_t start
)
93 HP_TIMING_DIFF (*var
, start
, stop
);
97 rtld_timer_accum (hp_timing_t
*sum
, hp_timing_t start
)
100 rtld_timer_stop (&stop
, start
);
101 HP_TIMING_ACCUM_NT(*sum
, stop
);
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)
113 /* Avoid PLT use for our local calls at startup. */
114 extern __typeof (__mempcpy
) __mempcpy attribute_hidden
;
116 /* GCC has mental blocks about _exit. */
117 extern __typeof (_exit
) exit_internal
asm ("_exit") attribute_hidden
;
118 #define _exit exit_internal
120 /* Helper function to handle errors while resolving symbols. */
121 static void print_unresolved (int errcode
, const char *objname
,
122 const char *errsting
);
124 /* Helper function to handle errors when a version is missing. */
125 static void print_missing_version (int errcode
, const char *objname
,
126 const char *errsting
);
128 /* Print the various times we collected. */
129 static void print_statistics (const hp_timing_t
*total_timep
);
131 /* Creates an empty audit list. */
132 static void audit_list_init (struct audit_list
*);
134 /* Add a string to the end of the audit list, for later parsing. Must
135 not be called after audit_list_next. */
136 static void audit_list_add_string (struct audit_list
*, const char *);
138 /* Add the audit strings from the link map, found in the dynamic
139 segment at TG (either DT_AUDIT and DT_DEPAUDIT). Must be called
140 before audit_list_next. */
141 static void audit_list_add_dynamic_tag (struct audit_list
*,
145 /* Extract the next audit module from the audit list. Only modules
146 for which dso_name_valid_for_suid is true are returned. Must be
147 called after all the audit_list_add_string,
148 audit_list_add_dynamic_tags calls. */
149 static const char *audit_list_next (struct audit_list
*);
151 /* Initialize *STATE with the defaults. */
152 static void dl_main_state_init (struct dl_main_state
*state
);
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
157 extern char **_environ attribute_hidden
;
158 static void process_envvars (struct dl_main_state
*state
);
160 int _dl_argc attribute_relro attribute_hidden
;
161 char **_dl_argv attribute_relro
= NULL
;
162 rtld_hidden_data_def (_dl_argv
)
164 #ifndef THREAD_SET_STACK_GUARD
165 /* Only exported for architectures that don't store the stack guard canary
166 in thread local area. */
167 uintptr_t __stack_chk_guard attribute_relro
;
170 /* Only exported for architectures that don't store the pointer guard
171 value in thread local area. */
172 uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden
;
173 #ifndef THREAD_SET_POINTER_GUARD
174 strong_alias (__pointer_chk_guard_local
, __pointer_chk_guard
)
177 /* Check that AT_SECURE=0, or that the passed name does not contain
178 directories and is not overly long. Reject empty names
181 dso_name_valid_for_suid (const char *p
)
183 if (__glibc_unlikely (__libc_enable_secure
))
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
)
195 audit_list_init (struct audit_list
*list
)
198 list
->current_index
= 0;
199 list
->current_tail
= NULL
;
203 audit_list_add_string (struct audit_list
*list
, const char *string
)
205 /* Empty strings do not load anything. */
209 if (list
->length
== array_length (list
->audit_strings
))
210 _dl_fatal_printf ("Fatal glibc error: Too many audit modules requested\n");
212 list
->audit_strings
[list
->length
++] = string
;
214 /* Initialize processing of the first string for
216 if (list
->length
== 1)
217 list
->current_tail
= string
;
221 audit_list_add_dynamic_tag (struct audit_list
*list
, struct link_map
*main_map
,
224 ElfW(Dyn
) *info
= main_map
->l_info
[ADDRIDX (tag
)];
225 const char *strtab
= (const char *) D_PTR (main_map
, l_info
[DT_STRTAB
]);
227 audit_list_add_string (list
, strtab
+ info
->d_un
.d_val
);
231 audit_list_next (struct audit_list
*list
)
233 if (list
->current_tail
== NULL
)
238 /* Advance to the next string in audit_strings if the current
239 string has been exhausted. */
240 while (*list
->current_tail
== '\0')
242 ++list
->current_index
;
243 if (list
->current_index
== list
->length
)
245 list
->current_tail
= NULL
;
248 list
->current_tail
= list
->audit_strings
[list
->current_index
];
251 /* Split the in-string audit list at the next colon colon. */
252 size_t len
= strcspn (list
->current_tail
, ":");
253 if (len
> 0 && len
< sizeof (list
->fname
))
255 memcpy (list
->fname
, list
->current_tail
, len
);
256 list
->fname
[len
] = '\0';
259 /* Mark the name as unusable for dso_name_valid_for_suid. */
260 list
->fname
[0] = '\0';
262 /* Skip over the substring and the following delimiter. */
263 list
->current_tail
+= len
;
264 if (*list
->current_tail
== ':')
265 ++list
->current_tail
;
267 /* If the name is valid, return it. */
268 if (dso_name_valid_for_suid (list
->fname
))
271 /* Otherwise wrap around to find the next list element. . */
275 /* Count audit modules before they are loaded so GLRO(dl_naudit)
276 is not yet usable. */
278 audit_list_count (struct audit_list
*list
)
280 /* Restore the audit_list iterator state at the end. */
281 const char *saved_tail
= list
->current_tail
;
284 assert (list
->current_index
== 0);
285 while (audit_list_next (list
) != NULL
)
287 list
->current_tail
= saved_tail
;
288 list
->current_index
= 0;
293 dl_main_state_init (struct dl_main_state
*state
)
295 audit_list_init (&state
->audit_list
);
296 state
->library_path
= NULL
;
297 state
->library_path_source
= NULL
;
298 state
->preloadlist
= NULL
;
299 state
->preloadarg
= NULL
;
300 state
->glibc_hwcaps_prepend
= NULL
;
301 state
->glibc_hwcaps_mask
= NULL
;
302 state
->mode
= rtld_mode_normal
;
303 state
->any_debug
= false;
304 state
->version_info
= false;
307 #ifndef HAVE_INLINED_SYSCALLS
308 /* Set nonzero during loading and initialization of executable and
309 libraries, cleared before the executable's entry point runs. This
310 must not be initialized to nonzero, because the unused dynamic
311 linker loaded in for libc.so's "ld.so.1" dep will provide the
312 definition seen by libc.so's initializer; that value must be zero,
313 and will be since that dynamic linker's _dl_start and dl_main will
315 int _dl_starting_up
= 0;
316 rtld_hidden_def (_dl_starting_up
)
319 /* This is the structure which defines all variables global to ld.so
320 (except those which cannot be added for some reason). */
321 struct rtld_global _rtld_global
=
323 /* Get architecture specific initializer. */
324 #include <dl-procruntime.c>
325 /* Generally the default presumption without further information is an
326 * executable stack but this is not true for all platforms. */
327 ._dl_stack_flags
= DEFAULT_STACK_PERMS
,
328 #ifdef _LIBC_REENTRANT
329 ._dl_load_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
330 ._dl_load_write_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
331 ._dl_load_tls_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
336 #ifdef _LIBC_REENTRANT
337 [LM_ID_BASE
] = { ._ns_unique_sym_table
338 = { .lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
} }
342 /* If we would use strong_alias here the compiler would see a
343 non-hidden definition. This would undo the effect of the previous
344 declaration. So spell out what strong_alias does plus add the
345 visibility attribute. */
346 extern struct rtld_global _rtld_local
347 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
350 /* This variable is similar to _rtld_local, but all values are
351 read-only after relocation. */
352 struct rtld_global_ro _rtld_global_ro attribute_relro
=
354 /* Get architecture specific initializer. */
355 #include <dl-procinfo.c>
356 #ifdef NEED_DL_SYSINFO
357 ._dl_sysinfo
= DL_SYSINFO_DEFAULT
,
359 ._dl_debug_fd
= STDERR_FILENO
,
361 ._dl_fpu_control
= _FPU_DEFAULT
,
362 ._dl_pagesize
= EXEC_PAGESIZE
,
363 ._dl_inhibit_cache
= 0,
364 ._dl_profile_output
= "/var/tmp",
366 /* Function pointers. */
367 ._dl_debug_printf
= _dl_debug_printf
,
368 ._dl_mcount
= _dl_mcount
,
369 ._dl_lookup_symbol_x
= _dl_lookup_symbol_x
,
370 ._dl_open
= _dl_open
,
371 ._dl_close
= _dl_close
,
372 ._dl_catch_error
= _dl_catch_error
,
373 ._dl_error_free
= _dl_error_free
,
374 ._dl_tls_get_addr_soft
= _dl_tls_get_addr_soft
,
375 ._dl_libc_freeres
= __rtld_libc_freeres
,
377 /* If we would use strong_alias here the compiler would see a
378 non-hidden definition. This would undo the effect of the previous
379 declaration. So spell out was strong_alias does plus add the
380 visibility attribute. */
381 extern struct rtld_global_ro _rtld_local_ro
382 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
385 static void dl_main (const ElfW(Phdr
) *phdr
, ElfW(Word
) phnum
,
386 ElfW(Addr
) *user_entry
, ElfW(auxv_t
) *auxv
);
388 /* These two variables cannot be moved into .data.rel.ro. */
389 static struct libname_list _dl_rtld_libname
;
390 static struct libname_list _dl_rtld_libname2
;
392 /* Variable for statistics. */
393 RLTD_TIMING_DECLARE (relocate_time
, static);
394 RLTD_TIMING_DECLARE (load_time
, static, attribute_relro
);
395 RLTD_TIMING_DECLARE (start_time
, static, attribute_relro
);
397 /* Additional definitions needed by TLS initialization. */
398 #ifdef TLS_INIT_HELPER
402 /* Helper function for syscall implementation. */
403 #ifdef DL_SYSINFO_IMPLEMENTATION
404 DL_SYSINFO_IMPLEMENTATION
407 /* Before ld.so is relocated we must not access variables which need
408 relocations. This means variables which are exported. Variables
409 declared as static are fine. If we can mark a variable hidden this
410 is fine, too. The latter is important here. We can avoid setting
411 up a temporary link map for ld.so if we can mark _rtld_global as
413 #ifndef HIDDEN_VAR_NEEDS_DYNAMIC_RELOC
414 # define DONT_USE_BOOTSTRAP_MAP 1
417 #ifdef DONT_USE_BOOTSTRAP_MAP
418 static ElfW(Addr
) _dl_start_final (void *arg
);
420 struct dl_start_final_info
423 RTLD_TIMING_VAR (start_time
);
425 static ElfW(Addr
) _dl_start_final (void *arg
,
426 struct dl_start_final_info
*info
);
429 /* These are defined magically by the linker. */
430 extern const ElfW(Ehdr
) __ehdr_start attribute_hidden
;
431 extern char _etext
[] attribute_hidden
;
432 extern char _end
[] attribute_hidden
;
438 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
441 /* This is the second half of _dl_start (below). It can be inlined safely
442 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
443 references. When the tools don't permit us to avoid using a GOT entry
444 for _dl_rtld_global (no attribute_hidden support), we must make sure
445 this function is not inlined (see below). */
447 #ifdef DONT_USE_BOOTSTRAP_MAP
448 static inline ElfW(Addr
) __attribute__ ((always_inline
))
449 _dl_start_final (void *arg
)
451 static ElfW(Addr
) __attribute__ ((noinline
))
452 _dl_start_final (void *arg
, struct dl_start_final_info
*info
)
455 ElfW(Addr
) start_addr
;
457 /* Do not use an initializer for these members because it would
458 interfere with __rtld_static_init. */
459 GLRO (dl_find_object
) = &_dl_find_object
;
461 /* If it hasn't happen yet record the startup time. */
462 rtld_timer_start (&start_time
);
463 #if !defined DONT_USE_BOOTSTRAP_MAP
464 RTLD_TIMING_SET (start_time
, info
->start_time
);
467 /* Transfer data about ourselves to the permanent link_map structure. */
468 #ifndef DONT_USE_BOOTSTRAP_MAP
469 GL(dl_rtld_map
).l_addr
= info
->l
.l_addr
;
470 GL(dl_rtld_map
).l_ld
= info
->l
.l_ld
;
471 GL(dl_rtld_map
).l_ld_readonly
= info
->l
.l_ld_readonly
;
472 memcpy (GL(dl_rtld_map
).l_info
, info
->l
.l_info
,
473 sizeof GL(dl_rtld_map
).l_info
);
474 GL(dl_rtld_map
).l_mach
= info
->l
.l_mach
;
475 GL(dl_rtld_map
).l_relocated
= 1;
477 _dl_setup_hash (&GL(dl_rtld_map
));
478 GL(dl_rtld_map
).l_real
= &GL(dl_rtld_map
);
479 GL(dl_rtld_map
).l_map_start
= (ElfW(Addr
)) &__ehdr_start
;
480 GL(dl_rtld_map
).l_map_end
= (ElfW(Addr
)) _end
;
481 /* Copy the TLS related data if necessary. */
482 #ifndef DONT_USE_BOOTSTRAP_MAP
483 # if NO_TLS_OFFSET != 0
484 GL(dl_rtld_map
).l_tls_offset
= NO_TLS_OFFSET
;
488 /* Initialize the stack end variable. */
489 __libc_stack_end
= __builtin_frame_address (0);
491 /* Call the OS-dependent function to set up life so we can do things like
492 file access. It will call `dl_main' (below) to do all the real work
493 of the dynamic linker, and then unwind our frame and run the user
494 entry point on the same stack we entered on. */
495 start_addr
= _dl_sysdep_start (arg
, &dl_main
);
497 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_STATISTICS
))
499 RTLD_TIMING_VAR (rtld_total_time
);
500 rtld_timer_stop (&rtld_total_time
, start_time
);
501 print_statistics (RTLD_TIMING_REF(rtld_total_time
));
504 #ifndef ELF_MACHINE_START_ADDRESS
505 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
507 return ELF_MACHINE_START_ADDRESS (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, start_addr
);
510 #ifdef DONT_USE_BOOTSTRAP_MAP
511 # define bootstrap_map GL(dl_rtld_map)
513 # define bootstrap_map info.l
516 static ElfW(Addr
) __attribute_used__
517 _dl_start (void *arg
)
519 #ifdef DONT_USE_BOOTSTRAP_MAP
520 rtld_timer_start (&start_time
);
522 struct dl_start_final_info info
;
523 rtld_timer_start (&info
.start_time
);
526 /* Partly clean the `bootstrap_map' structure up. Don't use
527 `memset' since it might not be built in or inlined and we cannot
528 make function calls at this point. Use '__builtin_memset' if we
529 know it is available. We do not have to clear the memory if we
530 do not have to use the temporary bootstrap_map. Global variables
531 are initialized to zero by default. */
532 #ifndef DONT_USE_BOOTSTRAP_MAP
533 # ifdef HAVE_BUILTIN_MEMSET
534 __builtin_memset (bootstrap_map
.l_info
, '\0', sizeof (bootstrap_map
.l_info
));
537 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
539 bootstrap_map
.l_info
[cnt
] = 0;
543 /* Figure out the run-time load address of the dynamic linker itself. */
544 bootstrap_map
.l_addr
= elf_machine_load_address ();
546 /* Read our own dynamic section and fill in the info array. */
547 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
548 bootstrap_map
.l_ld_readonly
= DL_RO_DYN_SECTION
;
549 elf_get_dynamic_info (&bootstrap_map
, true, false);
551 #if NO_TLS_OFFSET != 0
552 bootstrap_map
.l_tls_offset
= NO_TLS_OFFSET
;
555 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
556 ELF_MACHINE_BEFORE_RTLD_RELOC (&bootstrap_map
, bootstrap_map
.l_info
);
559 if (bootstrap_map
.l_addr
)
561 /* Relocate ourselves so we can do normal function calls and
562 data access using the global offset table. */
564 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, NULL
, 0, 0, 0);
566 bootstrap_map
.l_relocated
= 1;
568 /* Please note that we don't allow profiling of this object and
569 therefore need not test whether we have to allocate the array
570 for the relocation results (as done in dl-reloc.c). */
572 /* Now life is sane; we can call functions and access global data.
573 Set up to use the operating system facilities, and find out from
574 the operating system's program loader where to find the program
575 header table in core. Put the rest of _dl_start into a separate
576 function, that way the compiler cannot put accesses to the GOT
577 before ELF_DYNAMIC_RELOCATE. */
579 __rtld_malloc_init_stubs ();
581 #ifdef DONT_USE_BOOTSTRAP_MAP
582 return _dl_start_final (arg
);
584 return _dl_start_final (arg
, &info
);
590 /* Now life is peachy; we can do all normal operations.
591 On to the real work. */
593 /* Some helper functions. */
595 /* Arguments to relocate_doit. */
604 /* Argument to map_doit. */
606 struct link_map
*loader
;
608 /* Return value of map_doit. */
609 struct link_map
*map
;
615 struct link_map
*map
;
621 struct link_map
*map
;
625 /* Arguments to version_check_doit. */
626 struct version_check_args
633 relocate_doit (void *a
)
635 struct relocate_args
*args
= (struct relocate_args
*) a
;
637 _dl_relocate_object (args
->l
, args
->l
->l_scope
, args
->reloc_mode
, 0);
643 struct map_args
*args
= (struct map_args
*) a
;
644 int type
= (args
->mode
== __RTLD_OPENEXEC
) ? lt_executable
: lt_library
;
645 args
->map
= _dl_map_object (args
->loader
, args
->str
, type
, 0,
646 args
->mode
, LM_ID_BASE
);
650 dlmopen_doit (void *a
)
652 struct dlmopen_args
*args
= (struct dlmopen_args
*) a
;
653 args
->map
= _dl_open (args
->fname
,
654 (RTLD_LAZY
| __RTLD_DLOPEN
| __RTLD_AUDIT
656 dl_main
, LM_ID_NEWLM
, _dl_argc
, _dl_argv
,
661 lookup_doit (void *a
)
663 struct lookup_args
*args
= (struct lookup_args
*) a
;
664 const ElfW(Sym
) *ref
= NULL
;
666 lookup_t l
= _dl_lookup_symbol_x (args
->name
, args
->map
, &ref
,
667 args
->map
->l_local_scope
, NULL
, 0,
668 DL_LOOKUP_RETURN_NEWEST
, NULL
);
670 args
->result
= DL_SYMBOL_ADDRESS (l
, ref
);
674 version_check_doit (void *a
)
676 struct version_check_args
*args
= (struct version_check_args
*) a
;
677 if (_dl_check_all_versions (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, 1,
678 args
->dotrace
) && args
->doexit
)
679 /* We cannot start the application. Abort now. */
684 static inline struct link_map
*
685 find_needed (const char *name
)
687 struct r_scope_elem
*scope
= &GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
->l_searchlist
;
688 unsigned int n
= scope
->r_nlist
;
691 if (_dl_name_match_p (name
, scope
->r_list
[n
]))
692 return scope
->r_list
[n
];
694 /* Should never happen. */
699 match_version (const char *string
, struct link_map
*map
)
701 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
704 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
705 if (map
->l_info
[VERDEFTAG
] == NULL
)
706 /* The file has no symbol versioning. */
709 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
710 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
713 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
715 /* Compare the version strings. */
716 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
720 /* If no more definitions we failed to find what we want. */
721 if (def
->vd_next
== 0)
724 /* Next definition. */
725 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
731 bool __rtld_tls_init_tp_called
;
734 init_tls (size_t naudit
)
736 /* Number of elements in the static TLS block. */
737 GL(dl_tls_static_nelem
) = GL(dl_tls_max_dtv_idx
);
739 /* Do not do this twice. The audit interface might have required
740 the DTV interfaces to be set up early. */
741 if (GL(dl_initial_dtv
) != NULL
)
744 /* Allocate the array which contains the information about the
745 dtv slots. We allocate a few entries more than needed to
746 avoid the need for reallocation. */
747 size_t nelem
= GL(dl_tls_max_dtv_idx
) + 1 + TLS_SLOTINFO_SURPLUS
;
750 GL(dl_tls_dtv_slotinfo_list
) = (struct dtv_slotinfo_list
*)
751 calloc (sizeof (struct dtv_slotinfo_list
)
752 + nelem
* sizeof (struct dtv_slotinfo
), 1);
753 /* No need to check the return value. If memory allocation failed
754 the program would have been terminated. */
756 struct dtv_slotinfo
*slotinfo
= GL(dl_tls_dtv_slotinfo_list
)->slotinfo
;
757 GL(dl_tls_dtv_slotinfo_list
)->len
= nelem
;
758 GL(dl_tls_dtv_slotinfo_list
)->next
= NULL
;
760 /* Fill in the information from the loaded modules. No namespace
761 but the base one can be filled at this time. */
762 assert (GL(dl_ns
)[LM_ID_BASE
+ 1]._ns_loaded
== NULL
);
764 for (struct link_map
*l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
; l
!= NULL
;
766 if (l
->l_tls_blocksize
!= 0)
768 /* This is a module with TLS data. Store the map reference.
769 The generation counter is zero. */
771 /* slotinfo[i].gen = 0; */
774 assert (i
== GL(dl_tls_max_dtv_idx
));
776 /* Calculate the size of the static TLS surplus. */
777 _dl_tls_static_surplus_init (naudit
);
779 /* Compute the TLS offsets for the various blocks. */
780 _dl_determine_tlsoffset ();
782 /* Construct the static TLS block and the dtv for the initial
783 thread. For some platforms this will include allocating memory
784 for the thread descriptor. The memory for the TLS block will
785 never be freed. It should be allocated accordingly. The dtv
786 array can be changed if dynamic loading requires it. */
787 void *tcbp
= _dl_allocate_tls_storage ();
790 cannot allocate TLS data structures for initial thread\n");
792 /* Store for detection of the special case by __tls_get_addr
793 so it knows not to pass this dtv to the normal realloc. */
794 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
796 /* And finally install it for the main thread. */
797 call_tls_init_tp (tcbp
);
798 __rtld_tls_init_tp_called
= true;
804 do_preload (const char *fname
, struct link_map
*main_map
, const char *where
)
807 const char *err_str
= NULL
;
808 struct map_args args
;
812 args
.loader
= main_map
;
813 args
.mode
= __RTLD_SECURE
;
815 unsigned int old_nloaded
= GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
;
817 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, map_doit
, &args
);
818 if (__glibc_unlikely (err_str
!= NULL
))
821 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
822 fname
, where
, err_str
);
823 /* No need to call free, this is still before
824 the libc's malloc is used. */
826 else if (GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
!= old_nloaded
)
827 /* It is no duplicate. */
830 /* Nothing loaded. */
837 /* Set up the stack checker's canary. */
838 uintptr_t stack_chk_guard
= _dl_setup_stack_chk_guard (_dl_random
);
839 #ifdef THREAD_SET_STACK_GUARD
840 THREAD_SET_STACK_GUARD (stack_chk_guard
);
842 __stack_chk_guard
= stack_chk_guard
;
845 /* Set up the pointer guard as well, if necessary. */
846 uintptr_t pointer_chk_guard
847 = _dl_setup_pointer_guard (_dl_random
, stack_chk_guard
);
848 #ifdef THREAD_SET_POINTER_GUARD
849 THREAD_SET_POINTER_GUARD (pointer_chk_guard
);
851 __pointer_chk_guard_local
= pointer_chk_guard
;
853 /* We do not need the _dl_random value anymore. The less
854 information we leave behind, the better, so clear the
859 #include <setup-vdso.h>
861 /* The LD_PRELOAD environment variable gives list of libraries
862 separated by white space or colons that are loaded before the
863 executable's dependencies and prepended to the global scope list.
864 (If the binary is running setuid all elements containing a '/' are
865 ignored since it is insecure.) Return the number of preloads
866 performed. Ditto for --preload command argument. */
868 handle_preload_list (const char *preloadlist
, struct link_map
*main_map
,
871 unsigned int npreloads
= 0;
872 const char *p
= preloadlist
;
873 char fname
[SECURE_PATH_LIMIT
];
877 /* Split preload list at space/colon. */
878 size_t len
= strcspn (p
, " :");
879 if (len
> 0 && len
< sizeof (fname
))
881 memcpy (fname
, p
, len
);
887 /* Skip over the substring and the following delimiter. */
892 if (dso_name_valid_for_suid (fname
))
893 npreloads
+= do_preload (fname
, main_map
, where
);
898 /* Called if the audit DSO cannot be used: if it does not have the
899 appropriate interfaces, or it expects a more recent version library
900 version than what the dynamic linker provides. */
902 unload_audit_module (struct link_map
*map
, int original_tls_idx
)
905 Lmid_t ns
= map
->l_ns
;
909 /* Make sure the namespace has been cleared entirely. */
910 assert (GL(dl_ns
)[ns
]._ns_loaded
== NULL
);
911 assert (GL(dl_ns
)[ns
]._ns_nloaded
== 0);
913 GL(dl_tls_max_dtv_idx
) = original_tls_idx
;
916 /* Called to print an error message if loading of an audit module
919 report_audit_module_load_error (const char *name
, const char *err_str
,
923 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
926 free ((char *) err_str
);
929 /* Load one audit module. */
931 load_audit_module (const char *name
, struct audit_ifaces
**last_audit
)
933 int original_tls_idx
= GL(dl_tls_max_dtv_idx
);
935 struct dlmopen_args dlmargs
;
936 dlmargs
.fname
= name
;
940 const char *err_str
= NULL
;
942 _dl_catch_error (&objname
, &err_str
, &malloced
, dlmopen_doit
, &dlmargs
);
943 if (__glibc_unlikely (err_str
!= NULL
))
945 report_audit_module_load_error (name
, err_str
, malloced
);
949 struct lookup_args largs
;
950 largs
.name
= "la_version";
951 largs
.map
= dlmargs
.map
;
952 _dl_catch_error (&objname
, &err_str
, &malloced
, lookup_doit
, &largs
);
953 if (__glibc_likely (err_str
!= NULL
))
955 unload_audit_module (dlmargs
.map
, original_tls_idx
);
956 report_audit_module_load_error (name
, err_str
, malloced
);
960 unsigned int (*laversion
) (unsigned int) = largs
.result
;
962 /* A null symbol indicates that something is very wrong with the
963 loaded object because defined symbols are supposed to have a
964 valid, non-null address. */
965 assert (laversion
!= NULL
);
967 unsigned int lav
= laversion (LAV_CURRENT
);
970 /* Only print an error message if debugging because this can
971 happen deliberately. */
972 if (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
)
974 file=%s [%lu]; audit interface function la_version returned zero; ignored.\n",
975 dlmargs
.map
->l_name
, dlmargs
.map
->l_ns
);
976 unload_audit_module (dlmargs
.map
, original_tls_idx
);
980 if (!_dl_audit_check_version (lav
))
983 ERROR: audit interface '%s' requires version %d (maximum supported version %d); ignored.\n",
984 name
, lav
, LAV_CURRENT
);
985 unload_audit_module (dlmargs
.map
, original_tls_idx
);
989 enum { naudit_ifaces
= 8 };
992 struct audit_ifaces ifaces
;
993 void (*fptr
[naudit_ifaces
]) (void);
994 } *newp
= malloc (sizeof (*newp
));
996 _dl_fatal_printf ("Out of memory while loading audit modules\n");
998 /* Names of the auditing interfaces. All in one
1000 static const char audit_iface_names
[] =
1006 #define STRING(s) __STRING (s)
1007 "la_" STRING (ARCH_LA_PLTENTER
) "\0"
1008 "la_" STRING (ARCH_LA_PLTEXIT
) "\0"
1010 unsigned int cnt
= 0;
1011 const char *cp
= audit_iface_names
;
1015 _dl_catch_error (&objname
, &err_str
, &malloced
, lookup_doit
, &largs
);
1017 /* Store the pointer. */
1018 if (err_str
== NULL
&& largs
.result
!= NULL
)
1019 newp
->fptr
[cnt
] = largs
.result
;
1021 newp
->fptr
[cnt
] = NULL
;
1024 cp
= strchr (cp
, '\0') + 1;
1026 while (*cp
!= '\0');
1027 assert (cnt
== naudit_ifaces
);
1029 /* Now append the new auditing interface to the list. */
1030 newp
->ifaces
.next
= NULL
;
1031 if (*last_audit
== NULL
)
1032 *last_audit
= GLRO(dl_audit
) = &newp
->ifaces
;
1034 *last_audit
= (*last_audit
)->next
= &newp
->ifaces
;
1036 /* The dynamic linker link map is statically allocated, so the
1037 cookie in _dl_new_object has not happened. */
1038 link_map_audit_state (&GL (dl_rtld_map
), GLRO (dl_naudit
))->cookie
1039 = (intptr_t) &GL (dl_rtld_map
);
1043 /* Mark the DSO as being used for auditing. */
1044 dlmargs
.map
->l_auditing
= 1;
1047 /* Load all audit modules. */
1049 load_audit_modules (struct link_map
*main_map
, struct audit_list
*audit_list
)
1051 struct audit_ifaces
*last_audit
= NULL
;
1055 const char *name
= audit_list_next (audit_list
);
1058 load_audit_module (name
, &last_audit
);
1061 /* Notify audit modules of the initially loaded modules (the main
1062 program and the dynamic linker itself). */
1063 if (GLRO(dl_naudit
) > 0)
1065 _dl_audit_objopen (main_map
, LM_ID_BASE
);
1066 _dl_audit_objopen (&GL(dl_rtld_map
), LM_ID_BASE
);
1070 /* Check if the executable is not actually dynamically linked, and
1071 invoke it directly in that case. */
1073 rtld_chain_load (struct link_map
*main_map
, char *argv0
)
1075 /* The dynamic loader run against itself. */
1076 const char *rtld_soname
1077 = ((const char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1078 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_val
);
1079 if (main_map
->l_info
[DT_SONAME
] != NULL
1080 && strcmp (rtld_soname
,
1081 ((const char *) D_PTR (main_map
, l_info
[DT_STRTAB
])
1082 + main_map
->l_info
[DT_SONAME
]->d_un
.d_val
)) == 0)
1083 _dl_fatal_printf ("%s: loader cannot load itself\n", rtld_soname
);
1085 /* With DT_NEEDED dependencies, the executable is dynamically
1087 if (__glibc_unlikely (main_map
->l_info
[DT_NEEDED
] != NULL
))
1090 /* If the executable has program interpreter, it is dynamically
1092 for (size_t i
= 0; i
< main_map
->l_phnum
; ++i
)
1093 if (main_map
->l_phdr
[i
].p_type
== PT_INTERP
)
1096 const char *pathname
= _dl_argv
[0];
1098 _dl_argv
[0] = argv0
;
1099 int errcode
= __rtld_execve (pathname
, _dl_argv
, _environ
);
1100 const char *errname
= strerrorname_np (errcode
);
1101 if (errname
!= NULL
)
1102 _dl_fatal_printf("%s: cannot execute %s: %s\n",
1103 rtld_soname
, pathname
, errname
);
1105 _dl_fatal_printf("%s: cannot execute %s: %d\n",
1106 rtld_soname
, pathname
, errcode
);
1109 /* Called to complete the initialization of the link map for the main
1110 executable. Returns true if there is a PT_INTERP segment. */
1112 rtld_setup_main_map (struct link_map
*main_map
)
1114 /* This have already been filled in right after _dl_new_object, or
1115 as part of _dl_map_object. */
1116 const ElfW(Phdr
) *phdr
= main_map
->l_phdr
;
1117 ElfW(Word
) phnum
= main_map
->l_phnum
;
1119 bool has_interp
= false;
1121 main_map
->l_map_end
= 0;
1122 /* Perhaps the executable has no PT_LOAD header entries at all. */
1123 main_map
->l_map_start
= ~0;
1124 /* And it was opened directly. */
1125 ++main_map
->l_direct_opencount
;
1126 main_map
->l_contiguous
= 1;
1128 /* A PT_LOAD segment at an unexpected address will clear the
1129 l_contiguous flag. The ELF specification says that PT_LOAD
1130 segments need to be sorted in in increasing order, but perhaps
1131 not all executables follow this requirement. Having l_contiguous
1132 equal to 1 is just an optimization, so the code below does not
1133 try to sort the segments in case they are unordered.
1135 There is one corner case in which l_contiguous is not set to 1,
1136 but where it could be set: If a PIE (ET_DYN) binary is loaded by
1137 glibc itself (not the kernel), it is always contiguous due to the
1138 way the glibc loader works. However, the kernel loader may still
1139 create holes in this case, and the code here still uses 0
1140 conservatively for the glibc-loaded case, too. */
1141 ElfW(Addr
) expected_load_address
= 0;
1143 /* Scan the program header table for the dynamic section. */
1144 for (const ElfW(Phdr
) *ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
1148 /* Find out the load address. */
1149 main_map
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
1152 /* This tells us where to find the dynamic section,
1153 which tells us everything we need to do. */
1154 main_map
->l_ld
= (void *) main_map
->l_addr
+ ph
->p_vaddr
;
1155 main_map
->l_ld_readonly
= (ph
->p_flags
& PF_W
) == 0;
1158 /* This "interpreter segment" was used by the program loader to
1159 find the program interpreter, which is this program itself, the
1160 dynamic linker. We note what name finds us, so that a future
1161 dlopen call or DT_NEEDED entry, for something that wants to link
1162 against the dynamic linker as a shared library, will know that
1163 the shared object is already loaded. */
1164 _dl_rtld_libname
.name
= ((const char *) main_map
->l_addr
1166 /* _dl_rtld_libname.next = NULL; Already zero. */
1167 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
1169 /* Ordinarily, we would get additional names for the loader from
1170 our DT_SONAME. This can't happen if we were actually linked as
1171 a static executable (detect this case when we have no DYNAMIC).
1172 If so, assume the filename component of the interpreter path to
1173 be our SONAME, and add it to our name list. */
1174 if (GL(dl_rtld_map
).l_ld
== NULL
)
1176 const char *p
= NULL
;
1177 const char *cp
= _dl_rtld_libname
.name
;
1179 /* Find the filename part of the path. */
1186 _dl_rtld_libname2
.name
= p
;
1187 /* _dl_rtld_libname2.next = NULL; Already zero. */
1188 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
1196 ElfW(Addr
) mapstart
;
1197 ElfW(Addr
) allocend
;
1199 /* Remember where the main program starts in memory. */
1200 mapstart
= (main_map
->l_addr
1201 + (ph
->p_vaddr
& ~(GLRO(dl_pagesize
) - 1)));
1202 if (main_map
->l_map_start
> mapstart
)
1203 main_map
->l_map_start
= mapstart
;
1205 if (main_map
->l_contiguous
&& expected_load_address
!= 0
1206 && expected_load_address
!= mapstart
)
1207 main_map
->l_contiguous
= 0;
1209 /* Also where it ends. */
1210 allocend
= main_map
->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
1211 if (main_map
->l_map_end
< allocend
)
1212 main_map
->l_map_end
= allocend
;
1214 /* The next expected address is the page following this load
1216 expected_load_address
= ((allocend
+ GLRO(dl_pagesize
) - 1)
1217 & ~(GLRO(dl_pagesize
) - 1));
1222 if (ph
->p_memsz
> 0)
1224 /* Note that in the case the dynamic linker we duplicate work
1225 here since we read the PT_TLS entry already in
1226 _dl_start_final. But the result is repeatable so do not
1227 check for this special but unimportant case. */
1228 main_map
->l_tls_blocksize
= ph
->p_memsz
;
1229 main_map
->l_tls_align
= ph
->p_align
;
1230 if (ph
->p_align
== 0)
1231 main_map
->l_tls_firstbyte_offset
= 0;
1233 main_map
->l_tls_firstbyte_offset
= (ph
->p_vaddr
1234 & (ph
->p_align
- 1));
1235 main_map
->l_tls_initimage_size
= ph
->p_filesz
;
1236 main_map
->l_tls_initimage
= (void *) ph
->p_vaddr
;
1238 /* This image gets the ID one. */
1239 GL(dl_tls_max_dtv_idx
) = main_map
->l_tls_modid
= 1;
1244 GL(dl_stack_flags
) = ph
->p_flags
;
1248 main_map
->l_relro_addr
= ph
->p_vaddr
;
1249 main_map
->l_relro_size
= ph
->p_memsz
;
1252 /* Process program headers again, but scan them backwards so
1253 that PT_NOTE can be skipped if PT_GNU_PROPERTY exits. */
1254 for (const ElfW(Phdr
) *ph
= &phdr
[phnum
]; ph
!= phdr
; --ph
)
1255 switch (ph
[-1].p_type
)
1258 _dl_process_pt_note (main_map
, -1, &ph
[-1]);
1260 case PT_GNU_PROPERTY
:
1261 _dl_process_pt_gnu_property (main_map
, -1, &ph
[-1]);
1265 /* Adjust the address of the TLS initialization image in case
1266 the executable is actually an ET_DYN object. */
1267 if (main_map
->l_tls_initimage
!= NULL
)
1268 main_map
->l_tls_initimage
1269 = (char *) main_map
->l_tls_initimage
+ main_map
->l_addr
;
1270 if (! main_map
->l_map_end
)
1271 main_map
->l_map_end
= ~0;
1272 if (! GL(dl_rtld_map
).l_libname
&& GL(dl_rtld_map
).l_name
)
1274 /* We were invoked directly, so the program might not have a
1276 _dl_rtld_libname
.name
= GL(dl_rtld_map
).l_name
;
1277 /* _dl_rtld_libname.next = NULL; Already zero. */
1278 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
1281 assert (GL(dl_rtld_map
).l_libname
); /* How else did we get here? */
1286 /* Adjusts the contents of the stack and related globals for the user
1287 entry point. The ld.so processed skip_args arguments and bumped
1288 _dl_argv and _dl_argc accordingly. Those arguments are removed from
1291 _dl_start_args_adjust (int skip_args
)
1293 void **sp
= (void **) (_dl_argv
- skip_args
- 1);
1294 void **p
= sp
+ skip_args
;
1300 intptr_t argc
__attribute__ ((unused
)) = (intptr_t) sp
[0] - skip_args
;
1301 assert (argc
== _dl_argc
);
1303 /* Adjust argc on stack. */
1304 sp
[0] = (void *) (intptr_t) _dl_argc
;
1306 /* Update globals in rtld. */
1307 _dl_argv
-= skip_args
;
1308 _environ
-= skip_args
;
1310 /* Shuffle argv down. */
1315 assert (_environ
== (char **) (sp
+ 1));
1317 /* Shuffle envp down. */
1322 #ifdef HAVE_AUX_VECTOR
1323 void **auxv
= (void **) GLRO(dl_auxv
) - skip_args
;
1324 GLRO(dl_auxv
) = (ElfW(auxv_t
) *) auxv
; /* Aliasing violation. */
1325 assert (auxv
== sp
+ 1);
1327 /* Shuffle auxv down. */
1329 char *oldp
= (char *) (p
+ 1);
1330 char *newp
= (char *) (sp
+ 1);
1333 memcpy (&ax
, oldp
, sizeof (ax
));
1334 memcpy (newp
, &ax
, sizeof (ax
));
1335 oldp
+= sizeof (ax
);
1336 newp
+= sizeof (ax
);
1338 while (ax
.a_type
!= AT_NULL
);
1343 dl_main (const ElfW(Phdr
) *phdr
,
1345 ElfW(Addr
) *user_entry
,
1348 struct link_map
*main_map
;
1352 bool rtld_is_main
= false;
1355 struct dl_main_state state
;
1356 dl_main_state_init (&state
);
1358 __tls_pre_init_tp ();
1360 #if !PTHREAD_IN_LIBC
1361 /* The explicit initialization here is cheaper than processing the reloc
1362 in the _rtld_local definition's initializer. */
1363 GL(dl_make_stack_executable_hook
) = &_dl_make_stack_executable
;
1366 /* Process the environment variable which control the behaviour. */
1367 process_envvars (&state
);
1369 #ifndef HAVE_INLINED_SYSCALLS
1370 /* Set up a flag which tells we are just starting. */
1371 _dl_starting_up
= 1;
1374 const char *ld_so_name
= _dl_argv
[0];
1375 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
1377 /* Ho ho. We are not the program interpreter! We are the program
1378 itself! This means someone ran ld.so as a command. Well, that
1379 might be convenient to do sometimes. We support it by
1380 interpreting the args like this:
1382 ld.so PROGRAM ARGS...
1384 The first argument is the name of a file containing an ELF
1385 executable we will load and run with the following arguments.
1386 To simplify life here, PROGRAM is searched for using the
1387 normal rules for shared objects, rather than $PATH or anything
1388 like that. We just load it and use its entry point; we don't
1389 pay attention to its PT_INTERP command (we are the interpreter
1390 ourselves). This is an easy way to test a new ld.so before
1392 rtld_is_main
= true;
1395 char **orig_argv
= _dl_argv
;
1397 /* Note the place where the dynamic linker actually came from. */
1398 GL(dl_rtld_map
).l_name
= rtld_progname
;
1400 while (_dl_argc
> 1)
1401 if (! strcmp (_dl_argv
[1], "--list"))
1403 if (state
.mode
!= rtld_mode_help
)
1405 state
.mode
= rtld_mode_list
;
1406 /* This means do no dependency analysis. */
1413 else if (! strcmp (_dl_argv
[1], "--verify"))
1415 if (state
.mode
!= rtld_mode_help
)
1416 state
.mode
= rtld_mode_verify
;
1421 else if (! strcmp (_dl_argv
[1], "--inhibit-cache"))
1423 GLRO(dl_inhibit_cache
) = 1;
1427 else if (! strcmp (_dl_argv
[1], "--library-path")
1430 state
.library_path
= _dl_argv
[2];
1431 state
.library_path_source
= "--library-path";
1436 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath")
1439 GLRO(dl_inhibit_rpath
) = _dl_argv
[2];
1444 else if (! strcmp (_dl_argv
[1], "--audit") && _dl_argc
> 2)
1446 audit_list_add_string (&state
.audit_list
, _dl_argv
[2]);
1451 else if (! strcmp (_dl_argv
[1], "--preload") && _dl_argc
> 2)
1453 state
.preloadarg
= _dl_argv
[2];
1457 else if (! strcmp (_dl_argv
[1], "--argv0") && _dl_argc
> 2)
1459 argv0
= _dl_argv
[2];
1464 else if (strcmp (_dl_argv
[1], "--glibc-hwcaps-prepend") == 0
1467 state
.glibc_hwcaps_prepend
= _dl_argv
[2];
1471 else if (strcmp (_dl_argv
[1], "--glibc-hwcaps-mask") == 0
1474 state
.glibc_hwcaps_mask
= _dl_argv
[2];
1478 else if (! strcmp (_dl_argv
[1], "--list-tunables"))
1480 state
.mode
= rtld_mode_list_tunables
;
1485 else if (! strcmp (_dl_argv
[1], "--list-diagnostics"))
1487 state
.mode
= rtld_mode_list_diagnostics
;
1492 else if (strcmp (_dl_argv
[1], "--help") == 0)
1494 state
.mode
= rtld_mode_help
;
1498 else if (strcmp (_dl_argv
[1], "--version") == 0)
1500 else if (_dl_argv
[1][0] == '-' && _dl_argv
[1][1] == '-')
1502 if (_dl_argv
[1][1] == '\0')
1503 /* End of option list. */
1506 /* Unrecognized option. */
1507 _dl_usage (ld_so_name
, _dl_argv
[1]);
1512 if (__glibc_unlikely (state
.mode
== rtld_mode_list_tunables
))
1514 __tunables_print ();
1518 if (state
.mode
== rtld_mode_list_diagnostics
)
1519 _dl_print_diagnostics (_environ
);
1521 /* If we have no further argument the program was called incorrectly.
1522 Grant the user some education. */
1525 if (state
.mode
== rtld_mode_help
)
1526 /* --help without an executable is not an error. */
1527 _dl_help (ld_so_name
, &state
);
1529 _dl_usage (ld_so_name
, NULL
);
1535 /* The initialization of _dl_stack_flags done below assumes the
1536 executable's PT_GNU_STACK may have been honored by the kernel, and
1537 so a PT_GNU_STACK with PF_X set means the stack started out with
1538 execute permission. However, this is not really true if the
1539 dynamic linker is the executable the kernel loaded. For this
1540 case, we must reinitialize _dl_stack_flags to match the dynamic
1541 linker itself. If the dynamic linker was built with a
1542 PT_GNU_STACK, then the kernel may have loaded us with a
1543 nonexecutable stack that we will have to make executable when we
1544 load the program below unless it has a PT_GNU_STACK indicating
1545 nonexecutable stack is ok. */
1547 for (const ElfW(Phdr
) *ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
1548 if (ph
->p_type
== PT_GNU_STACK
)
1550 GL(dl_stack_flags
) = ph
->p_flags
;
1554 if (__glibc_unlikely (state
.mode
== rtld_mode_verify
1555 || state
.mode
== rtld_mode_help
))
1557 const char *objname
;
1558 const char *err_str
= NULL
;
1559 struct map_args args
;
1562 args
.str
= rtld_progname
;
1564 args
.mode
= __RTLD_OPENEXEC
;
1565 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, map_doit
,
1567 if (__glibc_unlikely (err_str
!= NULL
))
1569 /* We don't free the returned string, the programs stops
1571 if (state
.mode
== rtld_mode_help
)
1572 /* Mask the failure to load the main object. The help
1573 message contains less information in this case. */
1574 _dl_help (ld_so_name
, &state
);
1576 _exit (EXIT_FAILURE
);
1581 RTLD_TIMING_VAR (start
);
1582 rtld_timer_start (&start
);
1583 _dl_map_object (NULL
, rtld_progname
, lt_executable
, 0,
1584 __RTLD_OPENEXEC
, LM_ID_BASE
);
1585 rtld_timer_stop (&load_time
, start
);
1588 /* Now the map for the main executable is available. */
1589 main_map
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
1591 if (__glibc_likely (state
.mode
== rtld_mode_normal
))
1592 rtld_chain_load (main_map
, argv0
);
1594 phdr
= main_map
->l_phdr
;
1595 phnum
= main_map
->l_phnum
;
1596 /* We overwrite here a pointer to a malloc()ed string. But since
1597 the malloc() implementation used at this point is the dummy
1598 implementations which has no real free() function it does not
1599 makes sense to free the old string first. */
1600 main_map
->l_name
= (char *) "";
1601 *user_entry
= main_map
->l_entry
;
1603 /* Set bit indicating this is the main program map. */
1604 main_map
->l_main_map
= 1;
1606 #ifdef HAVE_AUX_VECTOR
1607 /* Adjust the on-stack auxiliary vector so that it looks like the
1608 binary was executed directly. */
1609 for (ElfW(auxv_t
) *av
= auxv
; av
->a_type
!= AT_NULL
; av
++)
1613 av
->a_un
.a_val
= (uintptr_t) phdr
;
1616 av
->a_un
.a_val
= phnum
;
1619 av
->a_un
.a_val
= *user_entry
;
1622 av
->a_un
.a_val
= (uintptr_t) _dl_argv
[0];
1627 /* Set the argv[0] string now that we've processed the executable. */
1629 _dl_argv
[0] = argv0
;
1631 /* Adjust arguments for the application entry point. */
1632 _dl_start_args_adjust (_dl_argv
- orig_argv
);
1636 /* Create a link_map for the executable itself.
1637 This will be what dlopen on "" returns. */
1638 main_map
= _dl_new_object ((char *) "", "", lt_executable
, NULL
,
1639 __RTLD_OPENEXEC
, LM_ID_BASE
);
1640 assert (main_map
!= NULL
);
1641 main_map
->l_phdr
= phdr
;
1642 main_map
->l_phnum
= phnum
;
1643 main_map
->l_entry
= *user_entry
;
1645 /* Even though the link map is not yet fully initialized we can add
1646 it to the map list since there are no possible users running yet. */
1647 _dl_add_to_namespace_list (main_map
, LM_ID_BASE
);
1648 assert (main_map
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
);
1650 /* At this point we are in a bit of trouble. We would have to
1651 fill in the values for l_dev and l_ino. But in general we
1652 do not know where the file is. We also do not handle AT_EXECFD
1653 even if it would be passed up.
1655 We leave the values here defined to 0. This is normally no
1656 problem as the program code itself is normally no shared
1657 object and therefore cannot be loaded dynamically. Nothing
1658 prevent the use of dynamic binaries and in these situations
1659 we might get problems. We might not be able to find out
1660 whether the object is already loaded. But since there is no
1661 easy way out and because the dynamic binary must also not
1662 have an SONAME we ignore this program for now. If it becomes
1663 a problem we can force people using SONAMEs. */
1665 /* We delay initializing the path structure until we got the dynamic
1666 information for the program. */
1669 bool has_interp
= rtld_setup_main_map (main_map
);
1671 /* If the current libname is different from the SONAME, add the
1673 if (GL(dl_rtld_map
).l_info
[DT_SONAME
] != NULL
1674 && strcmp (GL(dl_rtld_map
).l_libname
->name
,
1675 (const char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1676 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_val
) != 0)
1678 static struct libname_list newname
;
1679 newname
.name
= ((char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1680 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_ptr
);
1681 newname
.next
= NULL
;
1682 newname
.dont_free
= 1;
1684 assert (GL(dl_rtld_map
).l_libname
->next
== NULL
);
1685 GL(dl_rtld_map
).l_libname
->next
= &newname
;
1687 /* The ld.so must be relocated since otherwise loading audit modules
1688 will fail since they reuse the very same ld.so. */
1689 assert (GL(dl_rtld_map
).l_relocated
);
1693 /* Extract the contents of the dynamic section for easy access. */
1694 elf_get_dynamic_info (main_map
, false, false);
1696 /* If the main map is libc.so, update the base namespace to
1697 refer to this map. If libc.so is loaded later, this happens
1698 in _dl_map_object_from_fd. */
1699 if (main_map
->l_info
[DT_SONAME
] != NULL
1700 && (strcmp (((const char *) D_PTR (main_map
, l_info
[DT_STRTAB
])
1701 + main_map
->l_info
[DT_SONAME
]->d_un
.d_val
), LIBC_SO
)
1703 GL(dl_ns
)[LM_ID_BASE
].libc_map
= main_map
;
1705 /* Set up our cache of pointers into the hash table. */
1706 _dl_setup_hash (main_map
);
1709 if (__glibc_unlikely (state
.mode
== rtld_mode_verify
))
1711 /* We were called just to verify that this is a dynamic
1712 executable using us as the program interpreter. Exit with an
1713 error if we were not able to load the binary or no interpreter
1714 is specified (i.e., this is no dynamically linked binary. */
1715 if (main_map
->l_ld
== NULL
)
1718 _exit (has_interp
? 0 : 2);
1721 struct link_map
**first_preload
= &GL(dl_rtld_map
).l_next
;
1722 /* Set up the data structures for the system-supplied DSO early,
1723 so they can influence _dl_init_paths. */
1724 setup_vdso (main_map
, &first_preload
);
1726 /* With vDSO setup we can initialize the function pointers. */
1727 setup_vdso_pointers ();
1729 /* Initialize the data structures for the search paths for shared
1731 call_init_paths (&state
);
1733 /* Initialize _r_debug_extended. */
1734 struct r_debug
*r
= _dl_debug_initialize (GL(dl_rtld_map
).l_addr
,
1736 r
->r_state
= RT_CONSISTENT
;
1738 /* Put the link_map for ourselves on the chain so it can be found by
1739 name. Note that at this point the global chain of link maps contains
1740 exactly one element, which is pointed to by dl_loaded. */
1741 if (! GL(dl_rtld_map
).l_name
)
1742 /* If not invoked directly, the dynamic linker shared object file was
1743 found by the PT_INTERP name. */
1744 GL(dl_rtld_map
).l_name
= (char *) GL(dl_rtld_map
).l_libname
->name
;
1745 GL(dl_rtld_map
).l_type
= lt_library
;
1746 main_map
->l_next
= &GL(dl_rtld_map
);
1747 GL(dl_rtld_map
).l_prev
= main_map
;
1748 ++GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
;
1751 /* Starting from binutils-2.23, the linker will define the magic symbol
1752 __ehdr_start to point to our own ELF header if it is visible in a
1753 segment that also includes the phdrs. If that's not available, we use
1754 the old method that assumes the beginning of the file is part of the
1755 lowest-addressed PT_LOAD segment. */
1757 /* Set up the program header information for the dynamic linker
1758 itself. It is needed in the dl_iterate_phdr callbacks. */
1759 const ElfW(Ehdr
) *rtld_ehdr
= &__ehdr_start
;
1760 assert (rtld_ehdr
->e_ehsize
== sizeof *rtld_ehdr
);
1761 assert (rtld_ehdr
->e_phentsize
== sizeof (ElfW(Phdr
)));
1763 const ElfW(Phdr
) *rtld_phdr
= (const void *) rtld_ehdr
+ rtld_ehdr
->e_phoff
;
1765 GL(dl_rtld_map
).l_phdr
= rtld_phdr
;
1766 GL(dl_rtld_map
).l_phnum
= rtld_ehdr
->e_phnum
;
1769 /* PT_GNU_RELRO is usually the last phdr. */
1770 size_t cnt
= rtld_ehdr
->e_phnum
;
1772 if (rtld_phdr
[cnt
].p_type
== PT_GNU_RELRO
)
1774 GL(dl_rtld_map
).l_relro_addr
= rtld_phdr
[cnt
].p_vaddr
;
1775 GL(dl_rtld_map
).l_relro_size
= rtld_phdr
[cnt
].p_memsz
;
1779 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1780 if (GL(dl_rtld_map
).l_tls_blocksize
!= 0)
1781 /* Assign a module ID. Do this before loading any audit modules. */
1782 _dl_assign_tls_modid (&GL(dl_rtld_map
));
1784 audit_list_add_dynamic_tag (&state
.audit_list
, main_map
, DT_AUDIT
);
1785 audit_list_add_dynamic_tag (&state
.audit_list
, main_map
, DT_DEPAUDIT
);
1787 /* At this point, all data has been obtained that is included in the
1789 if (__glibc_unlikely (state
.mode
== rtld_mode_help
))
1790 _dl_help (ld_so_name
, &state
);
1792 /* If we have auditing DSOs to load, do it now. */
1793 bool need_security_init
= true;
1794 if (state
.audit_list
.length
> 0)
1796 size_t naudit
= audit_list_count (&state
.audit_list
);
1798 /* Since we start using the auditing DSOs right away we need to
1799 initialize the data structures now. */
1800 tcbp
= init_tls (naudit
);
1802 /* Initialize security features. We need to do it this early
1803 since otherwise the constructors of the audit libraries will
1804 use different values (especially the pointer guard) and will
1807 need_security_init
= false;
1809 load_audit_modules (main_map
, &state
.audit_list
);
1811 /* The count based on audit strings may overestimate the number
1812 of audit modules that got loaded, but not underestimate. */
1813 assert (GLRO(dl_naudit
) <= naudit
);
1816 /* Keep track of the currently loaded modules to count how many
1817 non-audit modules which use TLS are loaded. */
1818 size_t count_modids
= _dl_count_modids ();
1820 /* Set up debugging before the debugger is notified for the first time. */
1821 elf_setup_debug_entry (main_map
, r
);
1823 /* We start adding objects. */
1824 r
->r_state
= RT_ADD
;
1826 LIBC_PROBE (init_start
, 2, LM_ID_BASE
, r
);
1828 /* Auditing checkpoint: we are ready to signal that the initial map
1829 is being constructed. */
1830 _dl_audit_activity_map (main_map
, LA_ACT_ADD
);
1832 /* We have two ways to specify objects to preload: via environment
1833 variable and via the file /etc/ld.so.preload. The latter can also
1834 be used when security is enabled. */
1835 assert (*first_preload
== NULL
);
1836 struct link_map
**preloads
= NULL
;
1837 unsigned int npreloads
= 0;
1839 if (__glibc_unlikely (state
.preloadlist
!= NULL
))
1841 RTLD_TIMING_VAR (start
);
1842 rtld_timer_start (&start
);
1843 npreloads
+= handle_preload_list (state
.preloadlist
, main_map
,
1845 rtld_timer_accum (&load_time
, start
);
1848 if (__glibc_unlikely (state
.preloadarg
!= NULL
))
1850 RTLD_TIMING_VAR (start
);
1851 rtld_timer_start (&start
);
1852 npreloads
+= handle_preload_list (state
.preloadarg
, main_map
,
1854 rtld_timer_accum (&load_time
, start
);
1857 /* There usually is no ld.so.preload file, it should only be used
1858 for emergencies and testing. So the open call etc should usually
1859 fail. Using access() on a non-existing file is faster than using
1860 open(). So we do this first. If it succeeds we do almost twice
1861 the work but this does not matter, since it is not for production
1863 static const char preload_file
[] = "/etc/ld.so.preload";
1864 if (__glibc_unlikely (__access (preload_file
, R_OK
) == 0))
1866 /* Read the contents of the file. */
1867 file
= _dl_sysdep_read_whole_file (preload_file
, &file_size
,
1868 PROT_READ
| PROT_WRITE
);
1869 if (__glibc_unlikely (file
!= MAP_FAILED
))
1871 /* Parse the file. It contains names of libraries to be loaded,
1872 separated by white spaces or `:'. It may also contain
1873 comments introduced by `#'. */
1878 /* Eliminate comments. */
1883 char *comment
= memchr (runp
, '#', rest
);
1884 if (comment
== NULL
)
1887 rest
-= comment
- runp
;
1890 while (--rest
> 0 && *++comment
!= '\n');
1893 /* We have one problematic case: if we have a name at the end of
1894 the file without a trailing terminating characters, we cannot
1895 place the \0. Handle the case separately. */
1896 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
1897 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
1899 problem
= &file
[file_size
];
1900 while (problem
> file
&& problem
[-1] != ' '
1901 && problem
[-1] != '\t'
1902 && problem
[-1] != '\n' && problem
[-1] != ':')
1911 file
[file_size
- 1] = '\0';
1914 RTLD_TIMING_VAR (start
);
1915 rtld_timer_start (&start
);
1917 if (file
!= problem
)
1921 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
1923 npreloads
+= do_preload (p
, main_map
, preload_file
);
1926 if (problem
!= NULL
)
1928 char *p
= strndupa (problem
, file_size
- (problem
- file
));
1930 npreloads
+= do_preload (p
, main_map
, preload_file
);
1933 rtld_timer_accum (&load_time
, start
);
1935 /* We don't need the file anymore. */
1936 __munmap (file
, file_size
);
1940 if (__glibc_unlikely (*first_preload
!= NULL
))
1942 /* Set up PRELOADS with a vector of the preloaded libraries. */
1943 struct link_map
*l
= *first_preload
;
1944 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
1951 assert (i
== npreloads
);
1954 #ifdef NEED_DL_SYSINFO_DSO
1955 /* Now that the audit modules are opened, call la_objopen for the vDSO. */
1956 if (GLRO(dl_sysinfo_map
) != NULL
)
1957 _dl_audit_objopen (GLRO(dl_sysinfo_map
), LM_ID_BASE
);
1960 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1961 specified some libraries to load, these are inserted before the actual
1962 dependencies in the executable's searchlist for symbol resolution. */
1964 RTLD_TIMING_VAR (start
);
1965 rtld_timer_start (&start
);
1966 _dl_map_object_deps (main_map
, preloads
, npreloads
,
1967 state
.mode
== rtld_mode_trace
, 0);
1968 rtld_timer_accum (&load_time
, start
);
1971 /* Mark all objects as being in the global scope. */
1972 for (i
= main_map
->l_searchlist
.r_nlist
; i
> 0; )
1973 main_map
->l_searchlist
.r_list
[--i
]->l_global
= 1;
1975 /* Remove _dl_rtld_map from the chain. */
1976 GL(dl_rtld_map
).l_prev
->l_next
= GL(dl_rtld_map
).l_next
;
1977 if (GL(dl_rtld_map
).l_next
!= NULL
)
1978 GL(dl_rtld_map
).l_next
->l_prev
= GL(dl_rtld_map
).l_prev
;
1980 for (i
= 1; i
< main_map
->l_searchlist
.r_nlist
; ++i
)
1981 if (main_map
->l_searchlist
.r_list
[i
] == &GL(dl_rtld_map
))
1984 bool rtld_multiple_ref
= false;
1985 if (__glibc_likely (i
< main_map
->l_searchlist
.r_nlist
))
1987 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1988 put it back in the list of visible objects. We insert it into the
1989 chain in symbol search order because gdb uses the chain's order as
1990 its symbol search order. */
1991 rtld_multiple_ref
= true;
1993 GL(dl_rtld_map
).l_prev
= main_map
->l_searchlist
.r_list
[i
- 1];
1994 if (__glibc_likely (state
.mode
== rtld_mode_normal
))
1996 GL(dl_rtld_map
).l_next
= (i
+ 1 < main_map
->l_searchlist
.r_nlist
1997 ? main_map
->l_searchlist
.r_list
[i
+ 1]
1999 #ifdef NEED_DL_SYSINFO_DSO
2000 if (GLRO(dl_sysinfo_map
) != NULL
2001 && GL(dl_rtld_map
).l_prev
->l_next
== GLRO(dl_sysinfo_map
)
2002 && GL(dl_rtld_map
).l_next
!= GLRO(dl_sysinfo_map
))
2003 GL(dl_rtld_map
).l_prev
= GLRO(dl_sysinfo_map
);
2007 /* In trace mode there might be an invisible object (which we
2008 could not find) after the previous one in the search list.
2009 In this case it doesn't matter much where we put the
2010 interpreter object, so we just initialize the list pointer so
2011 that the assertion below holds. */
2012 GL(dl_rtld_map
).l_next
= GL(dl_rtld_map
).l_prev
->l_next
;
2014 assert (GL(dl_rtld_map
).l_prev
->l_next
== GL(dl_rtld_map
).l_next
);
2015 GL(dl_rtld_map
).l_prev
->l_next
= &GL(dl_rtld_map
);
2016 if (GL(dl_rtld_map
).l_next
!= NULL
)
2018 assert (GL(dl_rtld_map
).l_next
->l_prev
== GL(dl_rtld_map
).l_prev
);
2019 GL(dl_rtld_map
).l_next
->l_prev
= &GL(dl_rtld_map
);
2023 /* Now let us see whether all libraries are available in the
2024 versions we need. */
2026 struct version_check_args args
;
2027 args
.doexit
= state
.mode
== rtld_mode_normal
;
2028 args
.dotrace
= state
.mode
== rtld_mode_trace
;
2029 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
2032 /* We do not initialize any of the TLS functionality unless any of the
2033 initial modules uses TLS. This makes dynamic loading of modules with
2034 TLS impossible, but to support it requires either eagerly doing setup
2035 now or lazily doing it later. Doing it now makes us incompatible with
2036 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
2037 used. Trying to do it lazily is too hairy to try when there could be
2038 multiple threads (from a non-TLS-using libpthread). */
2039 bool was_tls_init_tp_called
= __rtld_tls_init_tp_called
;
2041 tcbp
= init_tls (0);
2043 if (__glibc_likely (need_security_init
))
2044 /* Initialize security features. But only if we have not done it
2048 if (__glibc_unlikely (state
.mode
!= rtld_mode_normal
))
2050 /* We were run just to list the shared libraries. It is
2051 important that we do this before real relocation, because the
2052 functions we call below for output may no longer work properly
2053 after relocation. */
2056 if (GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
)
2058 /* Look through the dependencies of the main executable
2059 and determine which of them is not actually
2061 struct link_map
*l
= main_map
;
2063 /* Relocate the main executable. */
2064 struct relocate_args args
= { .l
= l
,
2065 .reloc_mode
= ((GLRO(dl_lazy
)
2067 | __RTLD_NOIFUNC
) };
2068 _dl_receive_error (print_unresolved
, relocate_doit
, &args
);
2070 /* This loop depends on the dependencies of the executable to
2071 correspond in number and order to the DT_NEEDED entries. */
2072 ElfW(Dyn
) *dyn
= main_map
->l_ld
;
2074 while (dyn
->d_tag
!= DT_NULL
)
2076 if (dyn
->d_tag
== DT_NEEDED
)
2079 #ifdef NEED_DL_SYSINFO_DSO
2080 /* Skip the VDSO since it's not part of the list
2081 of objects we brought in via DT_NEEDED entries. */
2082 if (l
== GLRO(dl_sysinfo_map
))
2089 _dl_printf ("Unused direct dependencies:\n");
2093 _dl_printf ("\t%s\n", l
->l_name
);
2100 _exit (first
!= true);
2102 else if (! main_map
->l_info
[DT_NEEDED
])
2103 _dl_printf ("\tstatically linked\n");
2106 for (l
= state
.mode_trace_program
? main_map
: main_map
->l_next
;
2109 /* The library was not found. */
2110 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
2111 else if (strcmp (l
->l_libname
->name
, l
->l_name
) == 0)
2112 /* Print vDSO like libraries without duplicate name. Some
2113 consumers depend of this format. */
2114 _dl_printf ("\t%s (0x%0*zx)\n", l
->l_libname
->name
,
2115 (int) sizeof l
->l_map_start
* 2,
2116 (size_t) l
->l_map_start
);
2118 _dl_printf ("\t%s => %s (0x%0*zx)\n",
2119 DSO_FILENAME (l
->l_libname
->name
),
2120 DSO_FILENAME (l
->l_name
),
2121 (int) sizeof l
->l_map_start
* 2,
2122 (size_t) l
->l_map_start
);
2126 if (__glibc_unlikely (state
.mode
!= rtld_mode_trace
))
2127 for (i
= 1; i
< (unsigned int) _dl_argc
; ++i
)
2129 const ElfW(Sym
) *ref
= NULL
;
2130 ElfW(Addr
) loadbase
;
2133 result
= _dl_lookup_symbol_x (_dl_argv
[i
], main_map
,
2134 &ref
, main_map
->l_scope
,
2135 NULL
, ELF_RTYPE_CLASS_PLT
,
2136 DL_LOOKUP_ADD_DEPENDENCY
, NULL
);
2138 loadbase
= LOOKUP_VALUE_ADDRESS (result
, false);
2140 _dl_printf ("%s found at 0x%0*zd in object at 0x%0*zd\n",
2142 (int) sizeof ref
->st_value
* 2,
2143 (size_t) ref
->st_value
,
2144 (int) sizeof loadbase
* 2, (size_t) loadbase
);
2148 /* If LD_WARN is set, warn about undefined symbols. */
2149 if (GLRO(dl_lazy
) >= 0 && GLRO(dl_verbose
))
2151 /* We have to do symbol dependency testing. */
2152 struct relocate_args args
;
2155 args
.reloc_mode
= ((GLRO(dl_lazy
) ? RTLD_LAZY
: 0)
2158 i
= main_map
->l_searchlist
.r_nlist
;
2161 struct link_map
*l
= main_map
->l_initfini
[i
];
2162 if (l
!= &GL(dl_rtld_map
) && ! l
->l_faked
)
2165 _dl_receive_error (print_unresolved
, relocate_doit
,
2171 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2172 if (state
.version_info
)
2174 /* Print more information. This means here, print information
2175 about the versions needed. */
2177 struct link_map
*map
;
2179 for (map
= main_map
; map
!= NULL
; map
= map
->l_next
)
2182 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
2188 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
2189 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
2193 _dl_printf ("\n\tVersion information:\n");
2197 _dl_printf ("\t%s:\n", DSO_FILENAME (map
->l_name
));
2202 struct link_map
*needed
;
2204 needed
= find_needed (strtab
+ ent
->vn_file
);
2205 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
2209 const char *fname
= NULL
;
2212 && match_version (strtab
+ aux
->vna_name
,
2214 fname
= needed
->l_name
;
2216 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2217 strtab
+ ent
->vn_file
,
2218 strtab
+ aux
->vna_name
,
2219 aux
->vna_flags
& VER_FLG_WEAK
2221 fname
?: "not found");
2223 if (aux
->vna_next
== 0)
2224 /* No more symbols. */
2228 aux
= (ElfW(Vernaux
) *) ((char *) aux
2232 if (ent
->vn_next
== 0)
2233 /* No more dependencies. */
2236 /* Next dependency. */
2237 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
2246 /* Now set up the variable which helps the assembler startup code. */
2247 GL(dl_ns
)[LM_ID_BASE
]._ns_main_searchlist
= &main_map
->l_searchlist
;
2249 /* Save the information about the original global scope list since
2250 we need it in the memory handling later. */
2251 GLRO(dl_initial_searchlist
) = *GL(dl_ns
)[LM_ID_BASE
]._ns_main_searchlist
;
2253 /* Remember the last search directory added at startup, now that
2254 malloc will no longer be the one from dl-minimal.c. As a side
2255 effect, this marks ld.so as initialized, so that the rtld_active
2256 function returns true from now on. */
2257 GLRO(dl_init_all_dirs
) = GL(dl_all_dirs
);
2259 /* Print scope information. */
2260 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_SCOPES
))
2262 _dl_debug_printf ("\nInitial object scopes\n");
2264 for (struct link_map
*l
= main_map
; l
!= NULL
; l
= l
->l_next
)
2265 _dl_show_scope (l
, 0);
2268 _rtld_main_check (main_map
, _dl_argv
[0]);
2270 /* Now we have all the objects loaded. Relocate them all except for
2271 the dynamic linker itself. We do this in reverse order so that copy
2272 relocs of earlier objects overwrite the data written by later
2273 objects. We do not re-relocate the dynamic linker itself in this
2274 loop because that could result in the GOT entries for functions we
2275 call being changed, and that would break us. It is safe to relocate
2276 the dynamic linker out of order because it has no copy relocs (we
2277 know that because it is self-contained). */
2279 int consider_profiling
= GLRO(dl_profile
) != NULL
;
2281 /* If we are profiling we also must do lazy reloaction. */
2282 GLRO(dl_lazy
) |= consider_profiling
;
2284 RTLD_TIMING_VAR (start
);
2285 rtld_timer_start (&start
);
2287 unsigned i
= main_map
->l_searchlist
.r_nlist
;
2290 struct link_map
*l
= main_map
->l_initfini
[i
];
2292 /* While we are at it, help the memory handling a bit. We have to
2293 mark some data structures as allocated with the fake malloc()
2294 implementation in ld.so. */
2295 struct libname_list
*lnp
= l
->l_libname
->next
;
2297 while (__builtin_expect (lnp
!= NULL
, 0))
2302 /* Also allocated with the fake malloc(). */
2303 l
->l_free_initfini
= 0;
2305 if (l
!= &GL(dl_rtld_map
))
2306 _dl_relocate_object (l
, l
->l_scope
, GLRO(dl_lazy
) ? RTLD_LAZY
: 0,
2307 consider_profiling
);
2309 /* Add object to slot information data if necessasy. */
2310 if (l
->l_tls_blocksize
!= 0 && __rtld_tls_init_tp_called
)
2311 _dl_add_to_slotinfo (l
, true);
2314 rtld_timer_stop (&relocate_time
, start
);
2316 /* Now enable profiling if needed. Like the previous call,
2317 this has to go here because the calls it makes should use the
2318 rtld versions of the functions (particularly calloc()), but it
2319 needs to have _dl_profile_map set up by the relocator. */
2320 if (__glibc_unlikely (GL(dl_profile_map
) != NULL
))
2321 /* We must prepare the profiling. */
2322 _dl_start_profile ();
2324 if ((!was_tls_init_tp_called
&& GL(dl_tls_max_dtv_idx
) > 0)
2325 || count_modids
!= _dl_count_modids ())
2326 ++GL(dl_tls_generation
);
2328 /* Now that we have completed relocation, the initializer data
2329 for the TLS blocks has its final values and we can copy them
2330 into the main thread's TLS area, which we allocated above.
2331 Note: thread-local variables must only be accessed after completing
2333 _dl_allocate_tls_init (tcbp
, false);
2335 /* And finally install it for the main thread. */
2336 if (! __rtld_tls_init_tp_called
)
2337 call_tls_init_tp (tcbp
);
2339 /* Make sure no new search directories have been added. */
2340 assert (GLRO(dl_init_all_dirs
) == GL(dl_all_dirs
));
2342 if (rtld_multiple_ref
)
2344 /* There was an explicit ref to the dynamic linker as a shared lib.
2345 Re-relocate ourselves with user-controlled symbol definitions.
2347 We must do this after TLS initialization in case after this
2348 re-relocation, we might call a user-supplied function
2349 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2351 /* Set up the object lookup structures. */
2352 _dl_find_object_init ();
2354 /* The malloc implementation has been relocated, so resolving
2355 its symbols (and potentially calling IFUNC resolvers) is safe
2357 __rtld_malloc_init_real (main_map
);
2359 /* Likewise for the locking implementation. */
2360 __rtld_mutex_init ();
2362 RTLD_TIMING_VAR (start
);
2363 rtld_timer_start (&start
);
2365 /* Mark the link map as not yet relocated again. */
2366 GL(dl_rtld_map
).l_relocated
= 0;
2367 _dl_relocate_object (&GL(dl_rtld_map
), main_map
->l_scope
, 0, 0);
2369 rtld_timer_accum (&relocate_time
, start
);
2372 /* Relocation is complete. Perform early libc initialization. This
2373 is the initial libc, even if audit modules have been loaded with
2375 _dl_call_libc_early_init (GL(dl_ns
)[LM_ID_BASE
].libc_map
, true);
2377 /* Do any necessary cleanups for the startup OS interface code.
2378 We do these now so that no calls are made after rtld re-relocation
2379 which might be resolved to different functions than we expect.
2380 We cannot do this before relocating the other objects because
2381 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2382 _dl_sysdep_start_cleanup ();
2384 /* Auditing checkpoint: we have added all objects. */
2385 _dl_audit_activity_nsid (LM_ID_BASE
, LA_ACT_CONSISTENT
);
2387 /* Notify the debugger all new objects are now ready to go. We must re-get
2388 the address since by now the variable might be in another object. */
2389 r
= _dl_debug_update (LM_ID_BASE
);
2390 r
->r_state
= RT_CONSISTENT
;
2392 LIBC_PROBE (init_complete
, 2, LM_ID_BASE
, r
);
2394 #if defined USE_LDCONFIG && !defined MAP_COPY
2395 /* We must munmap() the cache file. */
2396 _dl_unload_cache ();
2399 /* Once we return, _dl_sysdep_start will invoke
2400 the DT_INIT functions and then *USER_ENTRY. */
2403 /* This is a little helper function for resolving symbols while
2404 tracing the binary. */
2406 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
2407 const char *errstring
)
2409 if (objname
[0] == '\0')
2410 objname
= RTLD_PROGNAME
;
2411 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
2414 /* This is a little helper function for resolving symbols while
2415 tracing the binary. */
2417 print_missing_version (int errcode
__attribute__ ((unused
)),
2418 const char *objname
, const char *errstring
)
2420 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME
,
2421 objname
, errstring
);
2424 /* Process the string given as the parameter which explains which debugging
2425 options are enabled. */
2427 process_dl_debug (struct dl_main_state
*state
, const char *dl_debug
)
2429 /* When adding new entries make sure that the maximal length of a name
2430 is correctly handled in the LD_DEBUG_HELP code below. */
2434 const char name
[10];
2435 const char helptext
[41];
2436 unsigned short int mask
;
2439 #define LEN_AND_STR(str) sizeof (str) - 1, str
2440 { LEN_AND_STR ("libs"), "display library search paths",
2441 DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
},
2442 { LEN_AND_STR ("reloc"), "display relocation processing",
2443 DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
},
2444 { LEN_AND_STR ("files"), "display progress for input file",
2445 DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
},
2446 { LEN_AND_STR ("symbols"), "display symbol table processing",
2447 DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
},
2448 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2449 DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
},
2450 { LEN_AND_STR ("versions"), "display version dependencies",
2451 DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
2452 { LEN_AND_STR ("scopes"), "display scope information",
2454 { LEN_AND_STR ("all"), "all previous options combined",
2455 DL_DEBUG_LIBS
| DL_DEBUG_RELOC
| DL_DEBUG_FILES
| DL_DEBUG_SYMBOLS
2456 | DL_DEBUG_BINDINGS
| DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
2457 | DL_DEBUG_SCOPES
},
2458 { LEN_AND_STR ("statistics"), "display relocation statistics",
2459 DL_DEBUG_STATISTICS
},
2460 { LEN_AND_STR ("unused"), "determined unused DSOs",
2462 { LEN_AND_STR ("help"), "display this help message and exit",
2465 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2467 /* Skip separating white spaces and commas. */
2468 while (*dl_debug
!= '\0')
2470 if (*dl_debug
!= ' ' && *dl_debug
!= ',' && *dl_debug
!= ':')
2475 while (dl_debug
[len
] != '\0' && dl_debug
[len
] != ' '
2476 && dl_debug
[len
] != ',' && dl_debug
[len
] != ':')
2479 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
2480 if (debopts
[cnt
].len
== len
2481 && memcmp (dl_debug
, debopts
[cnt
].name
, len
) == 0)
2483 GLRO(dl_debug_mask
) |= debopts
[cnt
].mask
;
2484 state
->any_debug
= true;
2488 if (cnt
== ndebopts
)
2490 /* Display a warning and skip everything until next
2492 char *copy
= strndupa (dl_debug
, len
);
2493 _dl_error_printf ("\
2494 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy
);
2504 if (GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
)
2506 /* In order to get an accurate picture of whether a particular
2507 DT_NEEDED entry is actually used we have to process both
2508 the PLT and non-PLT relocation entries. */
2512 if (GLRO(dl_debug_mask
) & DL_DEBUG_HELP
)
2517 Valid options for the LD_DEBUG environment variable are:\n\n");
2519 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
2520 _dl_printf (" %.*s%s%s\n", debopts
[cnt
].len
, debopts
[cnt
].name
,
2521 " " + debopts
[cnt
].len
- 3,
2522 debopts
[cnt
].helptext
);
2525 To direct the debugging output into a file instead of standard output\n\
2526 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2532 process_envvars (struct dl_main_state
*state
)
2534 char **runp
= _environ
;
2536 char *debug_output
= NULL
;
2538 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
2542 while (envline
[len
] != '\0' && envline
[len
] != '=')
2545 if (envline
[len
] != '=')
2546 /* This is a "LD_" variable at the end of the string without
2547 a '=' character. Ignore it since otherwise we will access
2548 invalid memory below. */
2554 /* Warning level, verbose or not. */
2555 if (memcmp (envline
, "WARN", 4) == 0)
2556 GLRO(dl_verbose
) = envline
[5] != '\0';
2560 /* Debugging of the dynamic linker? */
2561 if (memcmp (envline
, "DEBUG", 5) == 0)
2563 process_dl_debug (state
, &envline
[6]);
2566 if (memcmp (envline
, "AUDIT", 5) == 0)
2567 audit_list_add_string (&state
->audit_list
, &envline
[6]);
2571 /* Print information about versions. */
2572 if (memcmp (envline
, "VERBOSE", 7) == 0)
2574 state
->version_info
= envline
[8] != '\0';
2578 /* List of objects to be preloaded. */
2579 if (memcmp (envline
, "PRELOAD", 7) == 0)
2581 state
->preloadlist
= &envline
[8];
2585 /* Which shared object shall be profiled. */
2586 if (!__libc_enable_secure
2587 && memcmp (envline
, "PROFILE", 7) == 0 && envline
[8] != '\0')
2588 GLRO(dl_profile
) = &envline
[8];
2592 /* Do we bind early? */
2593 if (memcmp (envline
, "BIND_NOW", 8) == 0)
2595 GLRO(dl_lazy
) = envline
[9] == '\0';
2598 if (memcmp (envline
, "BIND_NOT", 8) == 0)
2599 GLRO(dl_bind_not
) = envline
[9] != '\0';
2603 /* Test whether we want to see the content of the auxiliary
2604 array passed up from the kernel. */
2605 if (!__libc_enable_secure
2606 && memcmp (envline
, "SHOW_AUXV", 9) == 0)
2611 /* Path where the binary is found. */
2612 if (!__libc_enable_secure
2613 && memcmp (envline
, "ORIGIN_PATH", 11) == 0)
2614 GLRO(dl_origin_path
) = &envline
[12];
2618 /* The library search path. */
2619 if (!__libc_enable_secure
2620 && memcmp (envline
, "LIBRARY_PATH", 12) == 0)
2622 state
->library_path
= &envline
[13];
2623 state
->library_path_source
= "LD_LIBRARY_PATH";
2627 /* Where to place the profiling data file. */
2628 if (memcmp (envline
, "DEBUG_OUTPUT", 12) == 0)
2630 debug_output
= &envline
[13];
2634 if (!__libc_enable_secure
2635 && memcmp (envline
, "DYNAMIC_WEAK", 12) == 0)
2636 GLRO(dl_dynamic_weak
) = 1;
2640 /* Where to place the profiling data file. */
2641 if (!__libc_enable_secure
2642 && memcmp (envline
, "PROFILE_OUTPUT", 14) == 0
2643 && envline
[15] != '\0')
2644 GLRO(dl_profile_output
) = &envline
[15];
2648 /* The mode of the dynamic linker can be set. */
2649 if (memcmp (envline
, "TRACE_LOADED_OBJECTS", 20) == 0)
2651 state
->mode
= rtld_mode_trace
;
2652 state
->mode_trace_program
2653 = _dl_strtoul (&envline
[21], NULL
) > 1;
2659 /* Extra security for SUID binaries. Remove all dangerous environment
2661 if (__glibc_unlikely (__libc_enable_secure
))
2663 const char *nextp
= UNSECURE_ENVVARS
;
2667 nextp
= strchr (nextp
, '\0') + 1;
2669 while (*nextp
!= '\0');
2671 GLRO(dl_debug_mask
) = 0;
2673 if (state
->mode
!= rtld_mode_normal
)
2676 /* If we have to run the dynamic linker in debugging mode and the
2677 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2678 messages to this file. */
2679 else if (state
->any_debug
&& debug_output
!= NULL
)
2681 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
2682 size_t name_len
= strlen (debug_output
);
2683 char buf
[name_len
+ 12];
2686 buf
[name_len
+ 11] = '\0';
2687 startp
= _itoa (__getpid (), &buf
[name_len
+ 11], 10, 0);
2689 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
2691 GLRO(dl_debug_fd
) = __open64_nocancel (startp
, flags
, DEFFILEMODE
);
2692 if (GLRO(dl_debug_fd
) == -1)
2693 /* We use standard output if opening the file failed. */
2694 GLRO(dl_debug_fd
) = STDOUT_FILENO
;
2698 #if HP_TIMING_INLINE
2700 print_statistics_item (const char *title
, hp_timing_t time
,
2703 char cycles
[HP_TIMING_PRINT_SIZE
];
2704 HP_TIMING_PRINT (cycles
, sizeof (cycles
), time
);
2706 char relative
[3 * sizeof (hp_timing_t
) + 2];
2707 char *cp
= _itoa ((1000ULL * time
) / total
, relative
+ sizeof (relative
),
2709 /* Sets the decimal point. */
2710 char *wp
= relative
;
2711 switch (relative
+ sizeof (relative
) - cp
)
2724 _dl_debug_printf ("%s: %s cycles (%s%%)\n", title
, cycles
, relative
);
2728 /* Print the various times we collected. */
2730 __attribute ((noinline
))
2731 print_statistics (const hp_timing_t
*rtld_total_timep
)
2733 #if HP_TIMING_INLINE
2735 char cycles
[HP_TIMING_PRINT_SIZE
];
2736 HP_TIMING_PRINT (cycles
, sizeof (cycles
), *rtld_total_timep
);
2737 _dl_debug_printf ("\nruntime linker statistics:\n"
2738 " total startup time in dynamic loader: %s cycles\n",
2740 print_statistics_item (" time needed for relocation",
2741 relocate_time
, *rtld_total_timep
);
2745 unsigned long int num_relative_relocations
= 0;
2746 for (Lmid_t ns
= 0; ns
< GL(dl_nns
); ++ns
)
2748 if (GL(dl_ns
)[ns
]._ns_loaded
== NULL
)
2751 struct r_scope_elem
*scope
= &GL(dl_ns
)[ns
]._ns_loaded
->l_searchlist
;
2753 for (unsigned int i
= 0; i
< scope
->r_nlist
; i
++)
2755 struct link_map
*l
= scope
->r_list
[i
];
2757 if (l
->l_addr
!= 0 && l
->l_info
[VERSYMIDX (DT_RELCOUNT
)])
2758 num_relative_relocations
2759 += l
->l_info
[VERSYMIDX (DT_RELCOUNT
)]->d_un
.d_val
;
2760 #ifndef ELF_MACHINE_REL_RELATIVE
2761 /* Relative relocations are processed on these architectures if
2762 library is loaded to different address than p_vaddr. */
2763 if ((l
->l_addr
!= 0)
2764 && l
->l_info
[VERSYMIDX (DT_RELACOUNT
)])
2766 /* On e.g. IA-64 or Alpha, relative relocations are processed
2767 only if library is loaded to different address than p_vaddr. */
2768 if (l
->l_addr
!= 0 && l
->l_info
[VERSYMIDX (DT_RELACOUNT
)])
2770 num_relative_relocations
2771 += l
->l_info
[VERSYMIDX (DT_RELACOUNT
)]->d_un
.d_val
;
2775 _dl_debug_printf (" number of relocations: %lu\n"
2776 " number of relocations from cache: %lu\n"
2777 " number of relative relocations: %lu\n",
2778 GL(dl_num_relocations
),
2779 GL(dl_num_cache_relocations
),
2780 num_relative_relocations
);
2782 #if HP_TIMING_INLINE
2783 print_statistics_item (" time needed to load objects",
2784 load_time
, *rtld_total_timep
);