1 /* Run time dynamic linker.
2 Copyright (C) 1995-2019 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 "dynamic-link.h"
36 #include <dl-librecon.h>
37 #include <unsecvars.h>
39 #include <dl-osinfo.h>
40 #include <dl-procinfo.h>
43 #include <stap-probe.h>
44 #include <stackinfo.h>
45 #include <not-cancel.h>
49 /* Only enables rtld profiling for architectures which provides non generic
50 hp-timing support. The generic support requires either syscall
51 (clock_gettime), which will incur in extra overhead on loading time.
52 Using vDSO is also an option, but it will require extra support on loader
53 to setup the vDSO pointer before its usage. */
55 # define RLTD_TIMING_DECLARE(var, classifier,...) \
56 classifier hp_timing_t var __VA_ARGS__
57 # define RTLD_TIMING_VAR(var) RLTD_TIMING_DECLARE (var, )
58 # define RTLD_TIMING_SET(var, value) (var) = (value)
59 # define RTLD_TIMING_REF(var) &(var)
62 rtld_timer_start (hp_timing_t
*var
)
68 rtld_timer_stop (hp_timing_t
*var
, hp_timing_t start
)
72 HP_TIMING_DIFF (*var
, start
, stop
);
76 rtld_timer_accum (hp_timing_t
*sum
, hp_timing_t start
)
79 rtld_timer_stop (&stop
, start
);
80 HP_TIMING_ACCUM_NT(*sum
, stop
);
83 # define RLTD_TIMING_DECLARE(var, classifier...)
84 # define RTLD_TIMING_SET(var, value)
85 # define RTLD_TIMING_VAR(var)
86 # define RTLD_TIMING_REF(var) 0
87 # define rtld_timer_start(var)
88 # define rtld_timer_stop(var, start)
89 # define rtld_timer_accum(sum, start)
92 /* Avoid PLT use for our local calls at startup. */
93 extern __typeof (__mempcpy
) __mempcpy attribute_hidden
;
95 /* GCC has mental blocks about _exit. */
96 extern __typeof (_exit
) exit_internal
asm ("_exit") attribute_hidden
;
97 #define _exit exit_internal
99 /* Helper function to handle errors while resolving symbols. */
100 static void print_unresolved (int errcode
, const char *objname
,
101 const char *errsting
);
103 /* Helper function to handle errors when a version is missing. */
104 static void print_missing_version (int errcode
, const char *objname
,
105 const char *errsting
);
107 /* Print the various times we collected. */
108 static void print_statistics (const hp_timing_t
*total_timep
);
110 /* Add audit objects. */
111 static void process_dl_audit (char *str
);
113 /* This is a list of all the modes the dynamic loader can be in. */
114 enum mode
{ normal
, list
, verify
, trace
};
116 /* Process all environments variables the dynamic linker must recognize.
117 Since all of them start with `LD_' we are a bit smarter while finding
119 static void process_envvars (enum mode
*modep
);
121 #ifdef DL_ARGV_NOT_RELRO
122 int _dl_argc attribute_hidden
;
123 char **_dl_argv
= NULL
;
124 /* Nonzero if we were run directly. */
125 unsigned int _dl_skip_args attribute_hidden
;
127 int _dl_argc attribute_relro attribute_hidden
;
128 char **_dl_argv attribute_relro
= NULL
;
129 unsigned int _dl_skip_args attribute_relro attribute_hidden
;
131 rtld_hidden_data_def (_dl_argv
)
133 #ifndef THREAD_SET_STACK_GUARD
134 /* Only exported for architectures that don't store the stack guard canary
135 in thread local area. */
136 uintptr_t __stack_chk_guard attribute_relro
;
139 /* Only exported for architectures that don't store the pointer guard
140 value in thread local area. */
141 uintptr_t __pointer_chk_guard_local
142 attribute_relro attribute_hidden
__attribute__ ((nocommon
));
143 #ifndef THREAD_SET_POINTER_GUARD
144 strong_alias (__pointer_chk_guard_local
, __pointer_chk_guard
)
147 /* Length limits for names and paths, to protect the dynamic linker,
148 particularly when __libc_enable_secure is active. */
150 # define SECURE_NAME_LIMIT NAME_MAX
152 # define SECURE_NAME_LIMIT 255
155 # define SECURE_PATH_LIMIT PATH_MAX
157 # define SECURE_PATH_LIMIT 1024
160 /* Check that AT_SECURE=0, or that the passed name does not contain
161 directories and is not overly long. Reject empty names
164 dso_name_valid_for_suid (const char *p
)
166 if (__glibc_unlikely (__libc_enable_secure
))
168 /* Ignore pathnames with directories for AT_SECURE=1
169 programs, and also skip overlong names. */
170 size_t len
= strlen (p
);
171 if (len
>= SECURE_NAME_LIMIT
|| memchr (p
, '/', len
) != NULL
)
177 /* LD_AUDIT variable contents. Must be processed before the
179 const char *audit_list_string
;
181 /* Cyclic list of auditing DSOs. audit_list->next is the first
183 static struct audit_list
186 struct audit_list
*next
;
189 /* Iterator for audit_list_string followed by audit_list. */
190 struct audit_list_iter
192 /* Tail of audit_list_string still needing processing, or NULL. */
193 const char *audit_list_tail
;
195 /* The list element returned in the previous iteration. NULL before
196 the first element. */
197 struct audit_list
*previous
;
199 /* Scratch buffer for returning a name which is part of
200 audit_list_string. */
201 char fname
[SECURE_NAME_LIMIT
];
204 /* Initialize an audit list iterator. */
206 audit_list_iter_init (struct audit_list_iter
*iter
)
208 iter
->audit_list_tail
= audit_list_string
;
209 iter
->previous
= NULL
;
212 /* Iterate through both audit_list_string and audit_list. */
214 audit_list_iter_next (struct audit_list_iter
*iter
)
216 if (iter
->audit_list_tail
!= NULL
)
218 /* First iterate over audit_list_string. */
219 while (*iter
->audit_list_tail
!= '\0')
221 /* Split audit list at colon. */
222 size_t len
= strcspn (iter
->audit_list_tail
, ":");
223 if (len
> 0 && len
< sizeof (iter
->fname
))
225 memcpy (iter
->fname
, iter
->audit_list_tail
, len
);
226 iter
->fname
[len
] = '\0';
229 /* Do not return this name to the caller. */
230 iter
->fname
[0] = '\0';
232 /* Skip over the substring and the following delimiter. */
233 iter
->audit_list_tail
+= len
;
234 if (*iter
->audit_list_tail
== ':')
235 ++iter
->audit_list_tail
;
237 /* If the name is valid, return it. */
238 if (dso_name_valid_for_suid (iter
->fname
))
240 /* Otherwise, wrap around and try the next name. */
242 /* Fall through to the procesing of audit_list. */
245 if (iter
->previous
== NULL
)
247 if (audit_list
== NULL
)
248 /* No pre-parsed audit list. */
250 /* Start of audit list. The first list element is at
251 audit_list->next (cyclic list). */
252 iter
->previous
= audit_list
->next
;
253 return iter
->previous
->name
;
255 if (iter
->previous
== audit_list
)
256 /* Cyclic list wrap-around. */
258 iter
->previous
= iter
->previous
->next
;
259 return iter
->previous
->name
;
262 #ifndef HAVE_INLINED_SYSCALLS
263 /* Set nonzero during loading and initialization of executable and
264 libraries, cleared before the executable's entry point runs. This
265 must not be initialized to nonzero, because the unused dynamic
266 linker loaded in for libc.so's "ld.so.1" dep will provide the
267 definition seen by libc.so's initializer; that value must be zero,
268 and will be since that dynamic linker's _dl_start and dl_main will
270 int _dl_starting_up
= 0;
271 rtld_hidden_def (_dl_starting_up
)
274 /* This is the structure which defines all variables global to ld.so
275 (except those which cannot be added for some reason). */
276 struct rtld_global _rtld_global
=
278 /* Generally the default presumption without further information is an
279 * executable stack but this is not true for all platforms. */
280 ._dl_stack_flags
= DEFAULT_STACK_PERMS
,
281 #ifdef _LIBC_REENTRANT
282 ._dl_load_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
283 ._dl_load_write_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
288 #ifdef _LIBC_REENTRANT
289 [LM_ID_BASE
] = { ._ns_unique_sym_table
290 = { .lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
} }
294 /* If we would use strong_alias here the compiler would see a
295 non-hidden definition. This would undo the effect of the previous
296 declaration. So spell out was strong_alias does plus add the
297 visibility attribute. */
298 extern struct rtld_global _rtld_local
299 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
302 /* This variable is similar to _rtld_local, but all values are
303 read-only after relocation. */
304 struct rtld_global_ro _rtld_global_ro attribute_relro
=
306 /* Get architecture specific initializer. */
307 #include <dl-procinfo.c>
308 #ifdef NEED_DL_SYSINFO
309 ._dl_sysinfo
= DL_SYSINFO_DEFAULT
,
311 ._dl_debug_fd
= STDERR_FILENO
,
312 ._dl_use_load_bias
= -2,
313 ._dl_correct_cache_id
= _DL_CACHE_DEFAULT_ID
,
315 ._dl_hwcap_mask
= HWCAP_IMPORTANT
,
318 ._dl_fpu_control
= _FPU_DEFAULT
,
319 ._dl_pagesize
= EXEC_PAGESIZE
,
320 ._dl_inhibit_cache
= 0,
322 /* Function pointers. */
323 ._dl_debug_printf
= _dl_debug_printf
,
324 ._dl_mcount
= _dl_mcount
,
325 ._dl_lookup_symbol_x
= _dl_lookup_symbol_x
,
326 ._dl_open
= _dl_open
,
327 ._dl_close
= _dl_close
,
328 ._dl_tls_get_addr_soft
= _dl_tls_get_addr_soft
,
329 #ifdef HAVE_DL_DISCOVER_OSVERSION
330 ._dl_discover_osversion
= _dl_discover_osversion
333 /* If we would use strong_alias here the compiler would see a
334 non-hidden definition. This would undo the effect of the previous
335 declaration. So spell out was strong_alias does plus add the
336 visibility attribute. */
337 extern struct rtld_global_ro _rtld_local_ro
338 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
341 static void dl_main (const ElfW(Phdr
) *phdr
, ElfW(Word
) phnum
,
342 ElfW(Addr
) *user_entry
, ElfW(auxv_t
) *auxv
);
344 /* These two variables cannot be moved into .data.rel.ro. */
345 static struct libname_list _dl_rtld_libname
;
346 static struct libname_list _dl_rtld_libname2
;
348 /* Variable for statistics. */
349 RLTD_TIMING_DECLARE (relocate_time
, static);
350 RLTD_TIMING_DECLARE (load_time
, static, attribute_relro
);
351 RLTD_TIMING_DECLARE (start_time
, static, attribute_relro
);
353 /* Additional definitions needed by TLS initialization. */
354 #ifdef TLS_INIT_HELPER
358 /* Helper function for syscall implementation. */
359 #ifdef DL_SYSINFO_IMPLEMENTATION
360 DL_SYSINFO_IMPLEMENTATION
363 /* Before ld.so is relocated we must not access variables which need
364 relocations. This means variables which are exported. Variables
365 declared as static are fine. If we can mark a variable hidden this
366 is fine, too. The latter is important here. We can avoid setting
367 up a temporary link map for ld.so if we can mark _rtld_global as
369 #ifdef PI_STATIC_AND_HIDDEN
370 # define DONT_USE_BOOTSTRAP_MAP 1
373 #ifdef DONT_USE_BOOTSTRAP_MAP
374 static ElfW(Addr
) _dl_start_final (void *arg
);
376 struct dl_start_final_info
379 RTLD_TIMING_VAR (start_time
);
381 static ElfW(Addr
) _dl_start_final (void *arg
,
382 struct dl_start_final_info
*info
);
385 /* These defined magically in the linker script. */
386 extern char _begin
[] attribute_hidden
;
387 extern char _etext
[] attribute_hidden
;
388 extern char _end
[] attribute_hidden
;
394 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
397 /* This is the second half of _dl_start (below). It can be inlined safely
398 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
399 references. When the tools don't permit us to avoid using a GOT entry
400 for _dl_rtld_global (no attribute_hidden support), we must make sure
401 this function is not inlined (see below). */
403 #ifdef DONT_USE_BOOTSTRAP_MAP
404 static inline ElfW(Addr
) __attribute__ ((always_inline
))
405 _dl_start_final (void *arg
)
407 static ElfW(Addr
) __attribute__ ((noinline
))
408 _dl_start_final (void *arg
, struct dl_start_final_info
*info
)
411 ElfW(Addr
) start_addr
;
413 /* If it hasn't happen yet record the startup time. */
414 rtld_timer_start (&start_time
);
415 #if !defined DONT_USE_BOOTSTRAP_MAP
416 RTLD_TIMING_SET (start_time
, info
->start_time
);
419 /* Transfer data about ourselves to the permanent link_map structure. */
420 #ifndef DONT_USE_BOOTSTRAP_MAP
421 GL(dl_rtld_map
).l_addr
= info
->l
.l_addr
;
422 GL(dl_rtld_map
).l_ld
= info
->l
.l_ld
;
423 memcpy (GL(dl_rtld_map
).l_info
, info
->l
.l_info
,
424 sizeof GL(dl_rtld_map
).l_info
);
425 GL(dl_rtld_map
).l_mach
= info
->l
.l_mach
;
426 GL(dl_rtld_map
).l_relocated
= 1;
428 _dl_setup_hash (&GL(dl_rtld_map
));
429 GL(dl_rtld_map
).l_real
= &GL(dl_rtld_map
);
430 GL(dl_rtld_map
).l_map_start
= (ElfW(Addr
)) _begin
;
431 GL(dl_rtld_map
).l_map_end
= (ElfW(Addr
)) _end
;
432 GL(dl_rtld_map
).l_text_end
= (ElfW(Addr
)) _etext
;
433 /* Copy the TLS related data if necessary. */
434 #ifndef DONT_USE_BOOTSTRAP_MAP
435 # if NO_TLS_OFFSET != 0
436 GL(dl_rtld_map
).l_tls_offset
= NO_TLS_OFFSET
;
440 /* Initialize the stack end variable. */
441 __libc_stack_end
= __builtin_frame_address (0);
443 /* Call the OS-dependent function to set up life so we can do things like
444 file access. It will call `dl_main' (below) to do all the real work
445 of the dynamic linker, and then unwind our frame and run the user
446 entry point on the same stack we entered on. */
447 start_addr
= _dl_sysdep_start (arg
, &dl_main
);
449 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_STATISTICS
))
451 RTLD_TIMING_VAR (rtld_total_time
);
452 rtld_timer_stop (&rtld_total_time
, start_time
);
453 print_statistics (RTLD_TIMING_REF(rtld_total_time
));
459 static ElfW(Addr
) __attribute_used__
460 _dl_start (void *arg
)
462 #ifdef DONT_USE_BOOTSTRAP_MAP
463 # define bootstrap_map GL(dl_rtld_map)
465 struct dl_start_final_info info
;
466 # define bootstrap_map info.l
469 /* This #define produces dynamic linking inline functions for
470 bootstrap relocation instead of general-purpose relocation.
471 Since ld.so must not have any undefined symbols the result
472 is trivial: always the map of ld.so itself. */
473 #define RTLD_BOOTSTRAP
474 #define BOOTSTRAP_MAP (&bootstrap_map)
475 #define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP
476 #include "dynamic-link.h"
478 #ifdef DONT_USE_BOOTSTRAP_MAP
479 rtld_timer_start (&start_time
);
481 rtld_timer_start (&info
.start_time
);
484 /* Partly clean the `bootstrap_map' structure up. Don't use
485 `memset' since it might not be built in or inlined and we cannot
486 make function calls at this point. Use '__builtin_memset' if we
487 know it is available. We do not have to clear the memory if we
488 do not have to use the temporary bootstrap_map. Global variables
489 are initialized to zero by default. */
490 #ifndef DONT_USE_BOOTSTRAP_MAP
491 # ifdef HAVE_BUILTIN_MEMSET
492 __builtin_memset (bootstrap_map
.l_info
, '\0', sizeof (bootstrap_map
.l_info
));
495 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
497 bootstrap_map
.l_info
[cnt
] = 0;
501 /* Figure out the run-time load address of the dynamic linker itself. */
502 bootstrap_map
.l_addr
= elf_machine_load_address ();
504 /* Read our own dynamic section and fill in the info array. */
505 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
506 elf_get_dynamic_info (&bootstrap_map
, NULL
);
508 #if NO_TLS_OFFSET != 0
509 bootstrap_map
.l_tls_offset
= NO_TLS_OFFSET
;
512 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
513 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
516 if (bootstrap_map
.l_addr
|| ! bootstrap_map
.l_info
[VALIDX(DT_GNU_PRELINKED
)])
518 /* Relocate ourselves so we can do normal function calls and
519 data access using the global offset table. */
521 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0, 0);
523 bootstrap_map
.l_relocated
= 1;
525 /* Please note that we don't allow profiling of this object and
526 therefore need not test whether we have to allocate the array
527 for the relocation results (as done in dl-reloc.c). */
529 /* Now life is sane; we can call functions and access global data.
530 Set up to use the operating system facilities, and find out from
531 the operating system's program loader where to find the program
532 header table in core. Put the rest of _dl_start into a separate
533 function, that way the compiler cannot put accesses to the GOT
534 before ELF_DYNAMIC_RELOCATE. */
536 #ifdef DONT_USE_BOOTSTRAP_MAP
537 ElfW(Addr
) entry
= _dl_start_final (arg
);
539 ElfW(Addr
) entry
= _dl_start_final (arg
, &info
);
542 #ifndef ELF_MACHINE_START_ADDRESS
543 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
546 return ELF_MACHINE_START_ADDRESS (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, entry
);
552 /* Now life is peachy; we can do all normal operations.
553 On to the real work. */
555 /* Some helper functions. */
557 /* Arguments to relocate_doit. */
566 /* Argument to map_doit. */
568 struct link_map
*loader
;
570 /* Return value of map_doit. */
571 struct link_map
*map
;
577 struct link_map
*map
;
583 struct link_map
*map
;
587 /* Arguments to version_check_doit. */
588 struct version_check_args
595 relocate_doit (void *a
)
597 struct relocate_args
*args
= (struct relocate_args
*) a
;
599 _dl_relocate_object (args
->l
, args
->l
->l_scope
, args
->reloc_mode
, 0);
605 struct map_args
*args
= (struct map_args
*) a
;
606 int type
= (args
->mode
== __RTLD_OPENEXEC
) ? lt_executable
: lt_library
;
607 args
->map
= _dl_map_object (args
->loader
, args
->str
, type
, 0,
608 args
->mode
, LM_ID_BASE
);
612 dlmopen_doit (void *a
)
614 struct dlmopen_args
*args
= (struct dlmopen_args
*) a
;
615 args
->map
= _dl_open (args
->fname
,
616 (RTLD_LAZY
| __RTLD_DLOPEN
| __RTLD_AUDIT
618 dl_main
, LM_ID_NEWLM
, _dl_argc
, _dl_argv
,
623 lookup_doit (void *a
)
625 struct lookup_args
*args
= (struct lookup_args
*) a
;
626 const ElfW(Sym
) *ref
= NULL
;
628 lookup_t l
= _dl_lookup_symbol_x (args
->name
, args
->map
, &ref
,
629 args
->map
->l_local_scope
, NULL
, 0,
630 DL_LOOKUP_RETURN_NEWEST
, NULL
);
632 args
->result
= DL_SYMBOL_ADDRESS (l
, ref
);
636 version_check_doit (void *a
)
638 struct version_check_args
*args
= (struct version_check_args
*) a
;
639 if (_dl_check_all_versions (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, 1,
640 args
->dotrace
) && args
->doexit
)
641 /* We cannot start the application. Abort now. */
646 static inline struct link_map
*
647 find_needed (const char *name
)
649 struct r_scope_elem
*scope
= &GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
->l_searchlist
;
650 unsigned int n
= scope
->r_nlist
;
653 if (_dl_name_match_p (name
, scope
->r_list
[n
]))
654 return scope
->r_list
[n
];
656 /* Should never happen. */
661 match_version (const char *string
, struct link_map
*map
)
663 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
666 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
667 if (map
->l_info
[VERDEFTAG
] == NULL
)
668 /* The file has no symbol versioning. */
671 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
672 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
675 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
677 /* Compare the version strings. */
678 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
682 /* If no more definitions we failed to find what we want. */
683 if (def
->vd_next
== 0)
686 /* Next definition. */
687 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
693 static bool tls_init_tp_called
;
698 /* Number of elements in the static TLS block. */
699 GL(dl_tls_static_nelem
) = GL(dl_tls_max_dtv_idx
);
701 /* Do not do this twice. The audit interface might have required
702 the DTV interfaces to be set up early. */
703 if (GL(dl_initial_dtv
) != NULL
)
706 /* Allocate the array which contains the information about the
707 dtv slots. We allocate a few entries more than needed to
708 avoid the need for reallocation. */
709 size_t nelem
= GL(dl_tls_max_dtv_idx
) + 1 + TLS_SLOTINFO_SURPLUS
;
712 GL(dl_tls_dtv_slotinfo_list
) = (struct dtv_slotinfo_list
*)
713 calloc (sizeof (struct dtv_slotinfo_list
)
714 + nelem
* sizeof (struct dtv_slotinfo
), 1);
715 /* No need to check the return value. If memory allocation failed
716 the program would have been terminated. */
718 struct dtv_slotinfo
*slotinfo
= GL(dl_tls_dtv_slotinfo_list
)->slotinfo
;
719 GL(dl_tls_dtv_slotinfo_list
)->len
= nelem
;
720 GL(dl_tls_dtv_slotinfo_list
)->next
= NULL
;
722 /* Fill in the information from the loaded modules. No namespace
723 but the base one can be filled at this time. */
724 assert (GL(dl_ns
)[LM_ID_BASE
+ 1]._ns_loaded
== NULL
);
726 for (struct link_map
*l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
; l
!= NULL
;
728 if (l
->l_tls_blocksize
!= 0)
730 /* This is a module with TLS data. Store the map reference.
731 The generation counter is zero. */
733 /* slotinfo[i].gen = 0; */
736 assert (i
== GL(dl_tls_max_dtv_idx
));
738 /* Compute the TLS offsets for the various blocks. */
739 _dl_determine_tlsoffset ();
741 /* Construct the static TLS block and the dtv for the initial
742 thread. For some platforms this will include allocating memory
743 for the thread descriptor. The memory for the TLS block will
744 never be freed. It should be allocated accordingly. The dtv
745 array can be changed if dynamic loading requires it. */
746 void *tcbp
= _dl_allocate_tls_storage ();
749 cannot allocate TLS data structures for initial thread\n");
751 /* Store for detection of the special case by __tls_get_addr
752 so it knows not to pass this dtv to the normal realloc. */
753 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
755 /* And finally install it for the main thread. */
756 const char *lossage
= TLS_INIT_TP (tcbp
);
757 if (__glibc_unlikely (lossage
!= NULL
))
758 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage
);
759 tls_init_tp_called
= true;
765 do_preload (const char *fname
, struct link_map
*main_map
, const char *where
)
768 const char *err_str
= NULL
;
769 struct map_args args
;
773 args
.loader
= main_map
;
774 args
.mode
= __RTLD_SECURE
;
776 unsigned int old_nloaded
= GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
;
778 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, map_doit
, &args
);
779 if (__glibc_unlikely (err_str
!= NULL
))
782 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
783 fname
, where
, err_str
);
784 /* No need to call free, this is still before
785 the libc's malloc is used. */
787 else if (GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
!= old_nloaded
)
788 /* It is no duplicate. */
791 /* Nothing loaded. */
795 #if defined SHARED && defined _LIBC_REENTRANT \
796 && defined __rtld_lock_default_lock_recursive
798 rtld_lock_default_lock_recursive (void *lock
)
800 __rtld_lock_default_lock_recursive (lock
);
804 rtld_lock_default_unlock_recursive (void *lock
)
806 __rtld_lock_default_unlock_recursive (lock
);
814 /* Set up the stack checker's canary. */
815 uintptr_t stack_chk_guard
= _dl_setup_stack_chk_guard (_dl_random
);
816 #ifdef THREAD_SET_STACK_GUARD
817 THREAD_SET_STACK_GUARD (stack_chk_guard
);
819 __stack_chk_guard
= stack_chk_guard
;
822 /* Set up the pointer guard as well, if necessary. */
823 uintptr_t pointer_chk_guard
824 = _dl_setup_pointer_guard (_dl_random
, stack_chk_guard
);
825 #ifdef THREAD_SET_POINTER_GUARD
826 THREAD_SET_POINTER_GUARD (pointer_chk_guard
);
828 __pointer_chk_guard_local
= pointer_chk_guard
;
830 /* We do not need the _dl_random value anymore. The less
831 information we leave behind, the better, so clear the
836 #include "setup-vdso.h"
838 /* The library search path. */
839 static const char *library_path attribute_relro
;
840 /* The list preloaded objects. */
841 static const char *preloadlist attribute_relro
;
842 /* Nonzero if information about versions has to be printed. */
843 static int version_info attribute_relro
;
844 /* The preload list passed as a command argument. */
845 static const char *preloadarg attribute_relro
;
847 /* The LD_PRELOAD environment variable gives list of libraries
848 separated by white space or colons that are loaded before the
849 executable's dependencies and prepended to the global scope list.
850 (If the binary is running setuid all elements containing a '/' are
851 ignored since it is insecure.) Return the number of preloads
852 performed. Ditto for --preload command argument. */
854 handle_preload_list (const char *preloadlist
, struct link_map
*main_map
,
857 unsigned int npreloads
= 0;
858 const char *p
= preloadlist
;
859 char fname
[SECURE_PATH_LIMIT
];
863 /* Split preload list at space/colon. */
864 size_t len
= strcspn (p
, " :");
865 if (len
> 0 && len
< sizeof (fname
))
867 memcpy (fname
, p
, len
);
873 /* Skip over the substring and the following delimiter. */
878 if (dso_name_valid_for_suid (fname
))
879 npreloads
+= do_preload (fname
, main_map
, where
);
884 /* Called if the audit DSO cannot be used: if it does not have the
885 appropriate interfaces, or it expects a more recent version library
886 version than what the dynamic linker provides. */
888 unload_audit_module (struct link_map
*map
, int original_tls_idx
)
891 Lmid_t ns
= map
->l_ns
;
895 /* Make sure the namespace has been cleared entirely. */
896 assert (GL(dl_ns
)[ns
]._ns_loaded
== NULL
);
897 assert (GL(dl_ns
)[ns
]._ns_nloaded
== 0);
899 GL(dl_tls_max_dtv_idx
) = original_tls_idx
;
902 /* Called to print an error message if loading of an audit module
905 report_audit_module_load_error (const char *name
, const char *err_str
,
909 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
912 free ((char *) err_str
);
915 /* Load one audit module. */
917 load_audit_module (const char *name
, struct audit_ifaces
**last_audit
)
919 int original_tls_idx
= GL(dl_tls_max_dtv_idx
);
921 struct dlmopen_args dlmargs
;
922 dlmargs
.fname
= name
;
926 const char *err_str
= NULL
;
928 _dl_catch_error (&objname
, &err_str
, &malloced
, dlmopen_doit
, &dlmargs
);
929 if (__glibc_unlikely (err_str
!= NULL
))
931 report_audit_module_load_error (name
, err_str
, malloced
);
935 struct lookup_args largs
;
936 largs
.name
= "la_version";
937 largs
.map
= dlmargs
.map
;
938 _dl_catch_error (&objname
, &err_str
, &malloced
, lookup_doit
, &largs
);
939 if (__glibc_likely (err_str
!= NULL
))
941 unload_audit_module (dlmargs
.map
, original_tls_idx
);
942 report_audit_module_load_error (name
, err_str
, malloced
);
946 unsigned int (*laversion
) (unsigned int) = largs
.result
;
948 /* A null symbol indicates that something is very wrong with the
949 loaded object because defined symbols are supposed to have a
950 valid, non-null address. */
951 assert (laversion
!= NULL
);
953 unsigned int lav
= laversion (LAV_CURRENT
);
956 /* Only print an error message if debugging because this can
957 happen deliberately. */
958 if (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
)
960 file=%s [%lu]; audit interface function la_version returned zero; ignored.\n",
961 dlmargs
.map
->l_name
, dlmargs
.map
->l_ns
);
962 unload_audit_module (dlmargs
.map
, original_tls_idx
);
966 if (lav
> LAV_CURRENT
)
969 ERROR: audit interface '%s' requires version %d (maximum supported version %d); ignored.\n",
970 name
, lav
, LAV_CURRENT
);
971 unload_audit_module (dlmargs
.map
, original_tls_idx
);
975 enum { naudit_ifaces
= 8 };
978 struct audit_ifaces ifaces
;
979 void (*fptr
[naudit_ifaces
]) (void);
980 } *newp
= malloc (sizeof (*newp
));
982 _dl_fatal_printf ("Out of memory while loading audit modules\n");
984 /* Names of the auditing interfaces. All in one
986 static const char audit_iface_names
[] =
991 #if __ELF_NATIVE_CLASS == 32
993 #elif __ELF_NATIVE_CLASS == 64
996 # error "__ELF_NATIVE_CLASS must be defined"
998 #define STRING(s) __STRING (s)
999 "la_" STRING (ARCH_LA_PLTENTER
) "\0"
1000 "la_" STRING (ARCH_LA_PLTEXIT
) "\0"
1002 unsigned int cnt
= 0;
1003 const char *cp
= audit_iface_names
;
1007 _dl_catch_error (&objname
, &err_str
, &malloced
, lookup_doit
, &largs
);
1009 /* Store the pointer. */
1010 if (err_str
== NULL
&& largs
.result
!= NULL
)
1011 newp
->fptr
[cnt
] = largs
.result
;
1013 newp
->fptr
[cnt
] = NULL
;
1016 cp
= rawmemchr (cp
, '\0') + 1;
1018 while (*cp
!= '\0');
1019 assert (cnt
== naudit_ifaces
);
1021 /* Now append the new auditing interface to the list. */
1022 newp
->ifaces
.next
= NULL
;
1023 if (*last_audit
== NULL
)
1024 *last_audit
= GLRO(dl_audit
) = &newp
->ifaces
;
1026 *last_audit
= (*last_audit
)->next
= &newp
->ifaces
;
1028 /* The dynamic linker link map is statically allocated, so the
1029 cookie in _dl_new_object has not happened. */
1030 link_map_audit_state (&GL (dl_rtld_map
), GLRO (dl_naudit
))->cookie
1031 = (intptr_t) &GL (dl_rtld_map
);
1035 /* Mark the DSO as being used for auditing. */
1036 dlmargs
.map
->l_auditing
= 1;
1039 /* Notify the the audit modules that the object MAP has already been
1042 notify_audit_modules_of_loaded_object (struct link_map
*map
)
1044 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1045 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1047 if (afct
->objopen
!= NULL
)
1049 struct auditstate
*state
= link_map_audit_state (map
, cnt
);
1050 state
->bindflags
= afct
->objopen (map
, LM_ID_BASE
, &state
->cookie
);
1051 map
->l_audit_any_plt
|= state
->bindflags
!= 0;
1058 /* Load all audit modules. */
1060 load_audit_modules (struct link_map
*main_map
)
1062 struct audit_ifaces
*last_audit
= NULL
;
1063 struct audit_list_iter al_iter
;
1064 audit_list_iter_init (&al_iter
);
1068 const char *name
= audit_list_iter_next (&al_iter
);
1071 load_audit_module (name
, &last_audit
);
1074 /* Notify audit modules of the initially loaded modules (the main
1075 program and the dynamic linker itself). */
1076 if (GLRO(dl_naudit
) > 0)
1078 notify_audit_modules_of_loaded_object (main_map
);
1079 notify_audit_modules_of_loaded_object (&GL(dl_rtld_map
));
1084 dl_main (const ElfW(Phdr
) *phdr
,
1086 ElfW(Addr
) *user_entry
,
1089 const ElfW(Phdr
) *ph
;
1091 struct link_map
*main_map
;
1094 bool has_interp
= false;
1096 bool prelinked
= false;
1097 bool rtld_is_main
= false;
1100 GL(dl_init_static_tls
) = &_dl_nothread_init_static_tls
;
1102 #if defined SHARED && defined _LIBC_REENTRANT \
1103 && defined __rtld_lock_default_lock_recursive
1104 GL(dl_rtld_lock_recursive
) = rtld_lock_default_lock_recursive
;
1105 GL(dl_rtld_unlock_recursive
) = rtld_lock_default_unlock_recursive
;
1108 /* The explicit initialization here is cheaper than processing the reloc
1109 in the _rtld_local definition's initializer. */
1110 GL(dl_make_stack_executable_hook
) = &_dl_make_stack_executable
;
1112 /* Process the environment variable which control the behaviour. */
1113 process_envvars (&mode
);
1115 #ifndef HAVE_INLINED_SYSCALLS
1116 /* Set up a flag which tells we are just starting. */
1117 _dl_starting_up
= 1;
1120 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
1122 /* Ho ho. We are not the program interpreter! We are the program
1123 itself! This means someone ran ld.so as a command. Well, that
1124 might be convenient to do sometimes. We support it by
1125 interpreting the args like this:
1127 ld.so PROGRAM ARGS...
1129 The first argument is the name of a file containing an ELF
1130 executable we will load and run with the following arguments.
1131 To simplify life here, PROGRAM is searched for using the
1132 normal rules for shared objects, rather than $PATH or anything
1133 like that. We just load it and use its entry point; we don't
1134 pay attention to its PT_INTERP command (we are the interpreter
1135 ourselves). This is an easy way to test a new ld.so before
1137 rtld_is_main
= true;
1139 /* Note the place where the dynamic linker actually came from. */
1140 GL(dl_rtld_map
).l_name
= rtld_progname
;
1142 while (_dl_argc
> 1)
1143 if (! strcmp (_dl_argv
[1], "--list"))
1146 GLRO(dl_lazy
) = -1; /* This means do no dependency analysis. */
1152 else if (! strcmp (_dl_argv
[1], "--verify"))
1160 else if (! strcmp (_dl_argv
[1], "--inhibit-cache"))
1162 GLRO(dl_inhibit_cache
) = 1;
1167 else if (! strcmp (_dl_argv
[1], "--library-path")
1170 library_path
= _dl_argv
[2];
1176 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath")
1179 GLRO(dl_inhibit_rpath
) = _dl_argv
[2];
1185 else if (! strcmp (_dl_argv
[1], "--audit") && _dl_argc
> 2)
1187 process_dl_audit (_dl_argv
[2]);
1193 else if (! strcmp (_dl_argv
[1], "--preload") && _dl_argc
> 2)
1195 preloadarg
= _dl_argv
[2];
1203 /* If we have no further argument the program was called incorrectly.
1204 Grant the user some education. */
1206 _dl_fatal_printf ("\
1207 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
1208 You have invoked `ld.so', the helper program for shared library executables.\n\
1209 This program usually lives in the file `/lib/ld.so', and special directives\n\
1210 in executable files using ELF shared libraries tell the system's program\n\
1211 loader to load the helper program from this file. This helper program loads\n\
1212 the shared libraries needed by the program executable, prepares the program\n\
1213 to run, and runs it. You may invoke this helper program directly from the\n\
1214 command line to load and run an ELF executable file; this is like executing\n\
1215 that file itself, but always uses this helper program from the file you\n\
1216 specified, instead of the helper program file specified in the executable\n\
1217 file you run. This is mostly of use for maintainers to test new versions\n\
1218 of this helper program; chances are you did not intend to run this program.\n\
1220 --list list all dependencies and how they are resolved\n\
1221 --verify verify that given object really is a dynamically linked\n\
1222 object we can handle\n\
1223 --inhibit-cache Do not use " LD_SO_CACHE
"\n\
1224 --library-path PATH use given PATH instead of content of the environment\n\
1225 variable LD_LIBRARY_PATH\n\
1226 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
1228 --audit LIST use objects named in LIST as auditors\n\
1229 --preload LIST preload objects named in LIST\n");
1235 /* The initialization of _dl_stack_flags done below assumes the
1236 executable's PT_GNU_STACK may have been honored by the kernel, and
1237 so a PT_GNU_STACK with PF_X set means the stack started out with
1238 execute permission. However, this is not really true if the
1239 dynamic linker is the executable the kernel loaded. For this
1240 case, we must reinitialize _dl_stack_flags to match the dynamic
1241 linker itself. If the dynamic linker was built with a
1242 PT_GNU_STACK, then the kernel may have loaded us with a
1243 nonexecutable stack that we will have to make executable when we
1244 load the program below unless it has a PT_GNU_STACK indicating
1245 nonexecutable stack is ok. */
1247 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
1248 if (ph
->p_type
== PT_GNU_STACK
)
1250 GL(dl_stack_flags
) = ph
->p_flags
;
1254 if (__builtin_expect (mode
, normal
) == verify
)
1256 const char *objname
;
1257 const char *err_str
= NULL
;
1258 struct map_args args
;
1261 args
.str
= rtld_progname
;
1263 args
.mode
= __RTLD_OPENEXEC
;
1264 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, map_doit
,
1266 if (__glibc_unlikely (err_str
!= NULL
))
1267 /* We don't free the returned string, the programs stops
1269 _exit (EXIT_FAILURE
);
1273 RTLD_TIMING_VAR (start
);
1274 rtld_timer_start (&start
);
1275 _dl_map_object (NULL
, rtld_progname
, lt_executable
, 0,
1276 __RTLD_OPENEXEC
, LM_ID_BASE
);
1277 rtld_timer_stop (&load_time
, start
);
1280 /* Now the map for the main executable is available. */
1281 main_map
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
1283 if (__builtin_expect (mode
, normal
) == normal
1284 && GL(dl_rtld_map
).l_info
[DT_SONAME
] != NULL
1285 && main_map
->l_info
[DT_SONAME
] != NULL
1286 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1287 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_val
,
1288 (const char *) D_PTR (main_map
, l_info
[DT_STRTAB
])
1289 + main_map
->l_info
[DT_SONAME
]->d_un
.d_val
) == 0)
1290 _dl_fatal_printf ("loader cannot load itself\n");
1292 phdr
= main_map
->l_phdr
;
1293 phnum
= main_map
->l_phnum
;
1294 /* We overwrite here a pointer to a malloc()ed string. But since
1295 the malloc() implementation used at this point is the dummy
1296 implementations which has no real free() function it does not
1297 makes sense to free the old string first. */
1298 main_map
->l_name
= (char *) "";
1299 *user_entry
= main_map
->l_entry
;
1301 #ifdef HAVE_AUX_VECTOR
1302 /* Adjust the on-stack auxiliary vector so that it looks like the
1303 binary was executed directly. */
1304 for (ElfW(auxv_t
) *av
= auxv
; av
->a_type
!= AT_NULL
; av
++)
1308 av
->a_un
.a_val
= (uintptr_t) phdr
;
1311 av
->a_un
.a_val
= phnum
;
1314 av
->a_un
.a_val
= *user_entry
;
1317 av
->a_un
.a_val
= (uintptr_t) _dl_argv
[0];
1324 /* Create a link_map for the executable itself.
1325 This will be what dlopen on "" returns. */
1326 main_map
= _dl_new_object ((char *) "", "", lt_executable
, NULL
,
1327 __RTLD_OPENEXEC
, LM_ID_BASE
);
1328 assert (main_map
!= NULL
);
1329 main_map
->l_phdr
= phdr
;
1330 main_map
->l_phnum
= phnum
;
1331 main_map
->l_entry
= *user_entry
;
1333 /* Even though the link map is not yet fully initialized we can add
1334 it to the map list since there are no possible users running yet. */
1335 _dl_add_to_namespace_list (main_map
, LM_ID_BASE
);
1336 assert (main_map
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
);
1338 /* At this point we are in a bit of trouble. We would have to
1339 fill in the values for l_dev and l_ino. But in general we
1340 do not know where the file is. We also do not handle AT_EXECFD
1341 even if it would be passed up.
1343 We leave the values here defined to 0. This is normally no
1344 problem as the program code itself is normally no shared
1345 object and therefore cannot be loaded dynamically. Nothing
1346 prevent the use of dynamic binaries and in these situations
1347 we might get problems. We might not be able to find out
1348 whether the object is already loaded. But since there is no
1349 easy way out and because the dynamic binary must also not
1350 have an SONAME we ignore this program for now. If it becomes
1351 a problem we can force people using SONAMEs. */
1353 /* We delay initializing the path structure until we got the dynamic
1354 information for the program. */
1357 main_map
->l_map_end
= 0;
1358 main_map
->l_text_end
= 0;
1359 /* Perhaps the executable has no PT_LOAD header entries at all. */
1360 main_map
->l_map_start
= ~0;
1361 /* And it was opened directly. */
1362 ++main_map
->l_direct_opencount
;
1364 /* Scan the program header table for the dynamic section. */
1365 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
1369 /* Find out the load address. */
1370 main_map
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
1373 /* This tells us where to find the dynamic section,
1374 which tells us everything we need to do. */
1375 main_map
->l_ld
= (void *) main_map
->l_addr
+ ph
->p_vaddr
;
1378 /* This "interpreter segment" was used by the program loader to
1379 find the program interpreter, which is this program itself, the
1380 dynamic linker. We note what name finds us, so that a future
1381 dlopen call or DT_NEEDED entry, for something that wants to link
1382 against the dynamic linker as a shared library, will know that
1383 the shared object is already loaded. */
1384 _dl_rtld_libname
.name
= ((const char *) main_map
->l_addr
1386 /* _dl_rtld_libname.next = NULL; Already zero. */
1387 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
1389 /* Ordinarilly, we would get additional names for the loader from
1390 our DT_SONAME. This can't happen if we were actually linked as
1391 a static executable (detect this case when we have no DYNAMIC).
1392 If so, assume the filename component of the interpreter path to
1393 be our SONAME, and add it to our name list. */
1394 if (GL(dl_rtld_map
).l_ld
== NULL
)
1396 const char *p
= NULL
;
1397 const char *cp
= _dl_rtld_libname
.name
;
1399 /* Find the filename part of the path. */
1406 _dl_rtld_libname2
.name
= p
;
1407 /* _dl_rtld_libname2.next = NULL; Already zero. */
1408 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
1416 ElfW(Addr
) mapstart
;
1417 ElfW(Addr
) allocend
;
1419 /* Remember where the main program starts in memory. */
1420 mapstart
= (main_map
->l_addr
1421 + (ph
->p_vaddr
& ~(GLRO(dl_pagesize
) - 1)));
1422 if (main_map
->l_map_start
> mapstart
)
1423 main_map
->l_map_start
= mapstart
;
1425 /* Also where it ends. */
1426 allocend
= main_map
->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
1427 if (main_map
->l_map_end
< allocend
)
1428 main_map
->l_map_end
= allocend
;
1429 if ((ph
->p_flags
& PF_X
) && allocend
> main_map
->l_text_end
)
1430 main_map
->l_text_end
= allocend
;
1435 if (ph
->p_memsz
> 0)
1437 /* Note that in the case the dynamic linker we duplicate work
1438 here since we read the PT_TLS entry already in
1439 _dl_start_final. But the result is repeatable so do not
1440 check for this special but unimportant case. */
1441 main_map
->l_tls_blocksize
= ph
->p_memsz
;
1442 main_map
->l_tls_align
= ph
->p_align
;
1443 if (ph
->p_align
== 0)
1444 main_map
->l_tls_firstbyte_offset
= 0;
1446 main_map
->l_tls_firstbyte_offset
= (ph
->p_vaddr
1447 & (ph
->p_align
- 1));
1448 main_map
->l_tls_initimage_size
= ph
->p_filesz
;
1449 main_map
->l_tls_initimage
= (void *) ph
->p_vaddr
;
1451 /* This image gets the ID one. */
1452 GL(dl_tls_max_dtv_idx
) = main_map
->l_tls_modid
= 1;
1457 GL(dl_stack_flags
) = ph
->p_flags
;
1461 main_map
->l_relro_addr
= ph
->p_vaddr
;
1462 main_map
->l_relro_size
= ph
->p_memsz
;
1466 if (_rtld_process_pt_note (main_map
, ph
))
1467 _dl_error_printf ("\
1468 ERROR: '%s': cannot process note segment.\n", _dl_argv
[0]);
1472 /* Adjust the address of the TLS initialization image in case
1473 the executable is actually an ET_DYN object. */
1474 if (main_map
->l_tls_initimage
!= NULL
)
1475 main_map
->l_tls_initimage
1476 = (char *) main_map
->l_tls_initimage
+ main_map
->l_addr
;
1477 if (! main_map
->l_map_end
)
1478 main_map
->l_map_end
= ~0;
1479 if (! main_map
->l_text_end
)
1480 main_map
->l_text_end
= ~0;
1481 if (! GL(dl_rtld_map
).l_libname
&& GL(dl_rtld_map
).l_name
)
1483 /* We were invoked directly, so the program might not have a
1485 _dl_rtld_libname
.name
= GL(dl_rtld_map
).l_name
;
1486 /* _dl_rtld_libname.next = NULL; Already zero. */
1487 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
1490 assert (GL(dl_rtld_map
).l_libname
); /* How else did we get here? */
1492 /* If the current libname is different from the SONAME, add the
1494 if (GL(dl_rtld_map
).l_info
[DT_SONAME
] != NULL
1495 && strcmp (GL(dl_rtld_map
).l_libname
->name
,
1496 (const char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1497 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_val
) != 0)
1499 static struct libname_list newname
;
1500 newname
.name
= ((char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1501 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_ptr
);
1502 newname
.next
= NULL
;
1503 newname
.dont_free
= 1;
1505 assert (GL(dl_rtld_map
).l_libname
->next
== NULL
);
1506 GL(dl_rtld_map
).l_libname
->next
= &newname
;
1508 /* The ld.so must be relocated since otherwise loading audit modules
1509 will fail since they reuse the very same ld.so. */
1510 assert (GL(dl_rtld_map
).l_relocated
);
1514 /* Extract the contents of the dynamic section for easy access. */
1515 elf_get_dynamic_info (main_map
, NULL
);
1516 /* Set up our cache of pointers into the hash table. */
1517 _dl_setup_hash (main_map
);
1520 if (__builtin_expect (mode
, normal
) == verify
)
1522 /* We were called just to verify that this is a dynamic
1523 executable using us as the program interpreter. Exit with an
1524 error if we were not able to load the binary or no interpreter
1525 is specified (i.e., this is no dynamically linked binary. */
1526 if (main_map
->l_ld
== NULL
)
1529 /* We allow here some platform specific code. */
1530 #ifdef DISTINGUISH_LIB_VERSIONS
1531 DISTINGUISH_LIB_VERSIONS
;
1533 _exit (has_interp
? 0 : 2);
1536 struct link_map
**first_preload
= &GL(dl_rtld_map
).l_next
;
1537 /* Set up the data structures for the system-supplied DSO early,
1538 so they can influence _dl_init_paths. */
1539 setup_vdso (main_map
, &first_preload
);
1541 #ifdef DL_SYSDEP_OSCHECK
1542 DL_SYSDEP_OSCHECK (_dl_fatal_printf
);
1545 /* Initialize the data structures for the search paths for shared
1547 _dl_init_paths (library_path
);
1549 /* Initialize _r_debug. */
1550 struct r_debug
*r
= _dl_debug_initialize (GL(dl_rtld_map
).l_addr
,
1552 r
->r_state
= RT_CONSISTENT
;
1554 /* Put the link_map for ourselves on the chain so it can be found by
1555 name. Note that at this point the global chain of link maps contains
1556 exactly one element, which is pointed to by dl_loaded. */
1557 if (! GL(dl_rtld_map
).l_name
)
1558 /* If not invoked directly, the dynamic linker shared object file was
1559 found by the PT_INTERP name. */
1560 GL(dl_rtld_map
).l_name
= (char *) GL(dl_rtld_map
).l_libname
->name
;
1561 GL(dl_rtld_map
).l_type
= lt_library
;
1562 main_map
->l_next
= &GL(dl_rtld_map
);
1563 GL(dl_rtld_map
).l_prev
= main_map
;
1564 ++GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
;
1567 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1568 to not using bias for non-prelinked PIEs and libraries
1569 and using it for executables or prelinked PIEs or libraries. */
1570 if (GLRO(dl_use_load_bias
) == (ElfW(Addr
)) -2)
1571 GLRO(dl_use_load_bias
) = main_map
->l_addr
== 0 ? -1 : 0;
1573 /* Set up the program header information for the dynamic linker
1574 itself. It is needed in the dl_iterate_phdr callbacks. */
1575 const ElfW(Ehdr
) *rtld_ehdr
;
1577 /* Starting from binutils-2.23, the linker will define the magic symbol
1578 __ehdr_start to point to our own ELF header if it is visible in a
1579 segment that also includes the phdrs. If that's not available, we use
1580 the old method that assumes the beginning of the file is part of the
1581 lowest-addressed PT_LOAD segment. */
1582 #ifdef HAVE_EHDR_START
1583 extern const ElfW(Ehdr
) __ehdr_start
__attribute__ ((visibility ("hidden")));
1584 rtld_ehdr
= &__ehdr_start
;
1586 rtld_ehdr
= (void *) GL(dl_rtld_map
).l_map_start
;
1588 assert (rtld_ehdr
->e_ehsize
== sizeof *rtld_ehdr
);
1589 assert (rtld_ehdr
->e_phentsize
== sizeof (ElfW(Phdr
)));
1591 const ElfW(Phdr
) *rtld_phdr
= (const void *) rtld_ehdr
+ rtld_ehdr
->e_phoff
;
1593 GL(dl_rtld_map
).l_phdr
= rtld_phdr
;
1594 GL(dl_rtld_map
).l_phnum
= rtld_ehdr
->e_phnum
;
1597 /* PT_GNU_RELRO is usually the last phdr. */
1598 size_t cnt
= rtld_ehdr
->e_phnum
;
1600 if (rtld_phdr
[cnt
].p_type
== PT_GNU_RELRO
)
1602 GL(dl_rtld_map
).l_relro_addr
= rtld_phdr
[cnt
].p_vaddr
;
1603 GL(dl_rtld_map
).l_relro_size
= rtld_phdr
[cnt
].p_memsz
;
1607 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1608 if (GL(dl_rtld_map
).l_tls_blocksize
!= 0)
1609 /* Assign a module ID. Do this before loading any audit modules. */
1610 GL(dl_rtld_map
).l_tls_modid
= _dl_next_tls_modid ();
1612 /* If we have auditing DSOs to load, do it now. */
1613 bool need_security_init
= true;
1614 if (__glibc_unlikely (audit_list
!= NULL
)
1615 || __glibc_unlikely (audit_list_string
!= NULL
))
1617 /* Since we start using the auditing DSOs right away we need to
1618 initialize the data structures now. */
1621 /* Initialize security features. We need to do it this early
1622 since otherwise the constructors of the audit libraries will
1623 use different values (especially the pointer guard) and will
1626 need_security_init
= false;
1628 load_audit_modules (main_map
);
1631 /* Keep track of the currently loaded modules to count how many
1632 non-audit modules which use TLS are loaded. */
1633 size_t count_modids
= _dl_count_modids ();
1635 /* Set up debugging before the debugger is notified for the first time. */
1636 #ifdef ELF_MACHINE_DEBUG_SETUP
1637 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1638 ELF_MACHINE_DEBUG_SETUP (main_map
, r
);
1639 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map
), r
);
1641 if (main_map
->l_info
[DT_DEBUG
] != NULL
)
1642 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1643 with the run-time address of the r_debug structure */
1644 main_map
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1646 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1647 case you run gdb on the dynamic linker directly. */
1648 if (GL(dl_rtld_map
).l_info
[DT_DEBUG
] != NULL
)
1649 GL(dl_rtld_map
).l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1652 /* We start adding objects. */
1653 r
->r_state
= RT_ADD
;
1655 LIBC_PROBE (init_start
, 2, LM_ID_BASE
, r
);
1657 /* Auditing checkpoint: we are ready to signal that the initial map
1658 is being constructed. */
1659 if (__glibc_unlikely (GLRO(dl_naudit
) > 0))
1661 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1662 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1664 if (afct
->activity
!= NULL
)
1665 afct
->activity (&link_map_audit_state (main_map
, cnt
)->cookie
,
1672 /* We have two ways to specify objects to preload: via environment
1673 variable and via the file /etc/ld.so.preload. The latter can also
1674 be used when security is enabled. */
1675 assert (*first_preload
== NULL
);
1676 struct link_map
**preloads
= NULL
;
1677 unsigned int npreloads
= 0;
1679 if (__glibc_unlikely (preloadlist
!= NULL
))
1681 RTLD_TIMING_VAR (start
);
1682 rtld_timer_start (&start
);
1683 npreloads
+= handle_preload_list (preloadlist
, main_map
, "LD_PRELOAD");
1684 rtld_timer_accum (&load_time
, start
);
1687 if (__glibc_unlikely (preloadarg
!= NULL
))
1689 RTLD_TIMING_VAR (start
);
1690 rtld_timer_start (&start
);
1691 npreloads
+= handle_preload_list (preloadarg
, main_map
, "--preload");
1692 rtld_timer_accum (&load_time
, start
);
1695 /* There usually is no ld.so.preload file, it should only be used
1696 for emergencies and testing. So the open call etc should usually
1697 fail. Using access() on a non-existing file is faster than using
1698 open(). So we do this first. If it succeeds we do almost twice
1699 the work but this does not matter, since it is not for production
1701 static const char preload_file
[] = "/etc/ld.so.preload";
1702 if (__glibc_unlikely (__access (preload_file
, R_OK
) == 0))
1704 /* Read the contents of the file. */
1705 file
= _dl_sysdep_read_whole_file (preload_file
, &file_size
,
1706 PROT_READ
| PROT_WRITE
);
1707 if (__glibc_unlikely (file
!= MAP_FAILED
))
1709 /* Parse the file. It contains names of libraries to be loaded,
1710 separated by white spaces or `:'. It may also contain
1711 comments introduced by `#'. */
1716 /* Eliminate comments. */
1721 char *comment
= memchr (runp
, '#', rest
);
1722 if (comment
== NULL
)
1725 rest
-= comment
- runp
;
1728 while (--rest
> 0 && *++comment
!= '\n');
1731 /* We have one problematic case: if we have a name at the end of
1732 the file without a trailing terminating characters, we cannot
1733 place the \0. Handle the case separately. */
1734 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
1735 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
1737 problem
= &file
[file_size
];
1738 while (problem
> file
&& problem
[-1] != ' '
1739 && problem
[-1] != '\t'
1740 && problem
[-1] != '\n' && problem
[-1] != ':')
1749 file
[file_size
- 1] = '\0';
1752 RTLD_TIMING_VAR (start
);
1753 rtld_timer_start (&start
);
1755 if (file
!= problem
)
1759 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
1761 npreloads
+= do_preload (p
, main_map
, preload_file
);
1764 if (problem
!= NULL
)
1766 char *p
= strndupa (problem
, file_size
- (problem
- file
));
1768 npreloads
+= do_preload (p
, main_map
, preload_file
);
1771 rtld_timer_accum (&load_time
, start
);
1773 /* We don't need the file anymore. */
1774 __munmap (file
, file_size
);
1778 if (__glibc_unlikely (*first_preload
!= NULL
))
1780 /* Set up PRELOADS with a vector of the preloaded libraries. */
1781 struct link_map
*l
= *first_preload
;
1782 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
1789 assert (i
== npreloads
);
1792 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1793 specified some libraries to load, these are inserted before the actual
1794 dependencies in the executable's searchlist for symbol resolution. */
1796 RTLD_TIMING_VAR (start
);
1797 rtld_timer_start (&start
);
1798 _dl_map_object_deps (main_map
, preloads
, npreloads
, mode
== trace
, 0);
1799 rtld_timer_accum (&load_time
, start
);
1802 /* Mark all objects as being in the global scope. */
1803 for (i
= main_map
->l_searchlist
.r_nlist
; i
> 0; )
1804 main_map
->l_searchlist
.r_list
[--i
]->l_global
= 1;
1806 /* Remove _dl_rtld_map from the chain. */
1807 GL(dl_rtld_map
).l_prev
->l_next
= GL(dl_rtld_map
).l_next
;
1808 if (GL(dl_rtld_map
).l_next
!= NULL
)
1809 GL(dl_rtld_map
).l_next
->l_prev
= GL(dl_rtld_map
).l_prev
;
1811 for (i
= 1; i
< main_map
->l_searchlist
.r_nlist
; ++i
)
1812 if (main_map
->l_searchlist
.r_list
[i
] == &GL(dl_rtld_map
))
1815 bool rtld_multiple_ref
= false;
1816 if (__glibc_likely (i
< main_map
->l_searchlist
.r_nlist
))
1818 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1819 put it back in the list of visible objects. We insert it into the
1820 chain in symbol search order because gdb uses the chain's order as
1821 its symbol search order. */
1822 rtld_multiple_ref
= true;
1824 GL(dl_rtld_map
).l_prev
= main_map
->l_searchlist
.r_list
[i
- 1];
1825 if (__builtin_expect (mode
, normal
) == normal
)
1827 GL(dl_rtld_map
).l_next
= (i
+ 1 < main_map
->l_searchlist
.r_nlist
1828 ? main_map
->l_searchlist
.r_list
[i
+ 1]
1830 #ifdef NEED_DL_SYSINFO_DSO
1831 if (GLRO(dl_sysinfo_map
) != NULL
1832 && GL(dl_rtld_map
).l_prev
->l_next
== GLRO(dl_sysinfo_map
)
1833 && GL(dl_rtld_map
).l_next
!= GLRO(dl_sysinfo_map
))
1834 GL(dl_rtld_map
).l_prev
= GLRO(dl_sysinfo_map
);
1838 /* In trace mode there might be an invisible object (which we
1839 could not find) after the previous one in the search list.
1840 In this case it doesn't matter much where we put the
1841 interpreter object, so we just initialize the list pointer so
1842 that the assertion below holds. */
1843 GL(dl_rtld_map
).l_next
= GL(dl_rtld_map
).l_prev
->l_next
;
1845 assert (GL(dl_rtld_map
).l_prev
->l_next
== GL(dl_rtld_map
).l_next
);
1846 GL(dl_rtld_map
).l_prev
->l_next
= &GL(dl_rtld_map
);
1847 if (GL(dl_rtld_map
).l_next
!= NULL
)
1849 assert (GL(dl_rtld_map
).l_next
->l_prev
== GL(dl_rtld_map
).l_prev
);
1850 GL(dl_rtld_map
).l_next
->l_prev
= &GL(dl_rtld_map
);
1854 /* Now let us see whether all libraries are available in the
1855 versions we need. */
1857 struct version_check_args args
;
1858 args
.doexit
= mode
== normal
;
1859 args
.dotrace
= mode
== trace
;
1860 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
1863 /* We do not initialize any of the TLS functionality unless any of the
1864 initial modules uses TLS. This makes dynamic loading of modules with
1865 TLS impossible, but to support it requires either eagerly doing setup
1866 now or lazily doing it later. Doing it now makes us incompatible with
1867 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1868 used. Trying to do it lazily is too hairy to try when there could be
1869 multiple threads (from a non-TLS-using libpthread). */
1870 bool was_tls_init_tp_called
= tls_init_tp_called
;
1874 if (__glibc_likely (need_security_init
))
1875 /* Initialize security features. But only if we have not done it
1879 if (__builtin_expect (mode
, normal
) != normal
)
1881 /* We were run just to list the shared libraries. It is
1882 important that we do this before real relocation, because the
1883 functions we call below for output may no longer work properly
1884 after relocation. */
1887 if (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1889 struct r_scope_elem
*scope
= &main_map
->l_searchlist
;
1891 for (i
= 0; i
< scope
->r_nlist
; i
++)
1893 l
= scope
->r_list
[i
];
1896 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1899 if (_dl_name_match_p (GLRO(dl_trace_prelink
), l
))
1900 GLRO(dl_trace_prelink_map
) = l
;
1901 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1902 DSO_FILENAME (l
->l_libname
->name
),
1903 DSO_FILENAME (l
->l_name
),
1904 (int) sizeof l
->l_map_start
* 2,
1905 (size_t) l
->l_map_start
,
1906 (int) sizeof l
->l_addr
* 2,
1907 (size_t) l
->l_addr
);
1910 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l
->l_tls_modid
,
1911 (int) sizeof l
->l_tls_offset
* 2,
1912 (size_t) l
->l_tls_offset
);
1917 else if (GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
)
1919 /* Look through the dependencies of the main executable
1920 and determine which of them is not actually
1922 struct link_map
*l
= main_map
;
1924 /* Relocate the main executable. */
1925 struct relocate_args args
= { .l
= l
,
1926 .reloc_mode
= ((GLRO(dl_lazy
)
1928 | __RTLD_NOIFUNC
) };
1929 _dl_receive_error (print_unresolved
, relocate_doit
, &args
);
1931 /* This loop depends on the dependencies of the executable to
1932 correspond in number and order to the DT_NEEDED entries. */
1933 ElfW(Dyn
) *dyn
= main_map
->l_ld
;
1935 while (dyn
->d_tag
!= DT_NULL
)
1937 if (dyn
->d_tag
== DT_NEEDED
)
1940 #ifdef NEED_DL_SYSINFO_DSO
1941 /* Skip the VDSO since it's not part of the list
1942 of objects we brought in via DT_NEEDED entries. */
1943 if (l
== GLRO(dl_sysinfo_map
))
1950 _dl_printf ("Unused direct dependencies:\n");
1954 _dl_printf ("\t%s\n", l
->l_name
);
1961 _exit (first
!= true);
1963 else if (! main_map
->l_info
[DT_NEEDED
])
1964 _dl_printf ("\tstatically linked\n");
1967 for (l
= main_map
->l_next
; l
; l
= l
->l_next
)
1969 /* The library was not found. */
1970 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1971 else if (strcmp (l
->l_libname
->name
, l
->l_name
) == 0)
1972 _dl_printf ("\t%s (0x%0*Zx)\n", l
->l_libname
->name
,
1973 (int) sizeof l
->l_map_start
* 2,
1974 (size_t) l
->l_map_start
);
1976 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l
->l_libname
->name
,
1977 l
->l_name
, (int) sizeof l
->l_map_start
* 2,
1978 (size_t) l
->l_map_start
);
1981 if (__builtin_expect (mode
, trace
) != trace
)
1982 for (i
= 1; i
< (unsigned int) _dl_argc
; ++i
)
1984 const ElfW(Sym
) *ref
= NULL
;
1985 ElfW(Addr
) loadbase
;
1988 result
= _dl_lookup_symbol_x (_dl_argv
[i
], main_map
,
1989 &ref
, main_map
->l_scope
,
1990 NULL
, ELF_RTYPE_CLASS_PLT
,
1991 DL_LOOKUP_ADD_DEPENDENCY
, NULL
);
1993 loadbase
= LOOKUP_VALUE_ADDRESS (result
, false);
1995 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1997 (int) sizeof ref
->st_value
* 2,
1998 (size_t) ref
->st_value
,
1999 (int) sizeof loadbase
* 2, (size_t) loadbase
);
2003 /* If LD_WARN is set, warn about undefined symbols. */
2004 if (GLRO(dl_lazy
) >= 0 && GLRO(dl_verbose
))
2006 /* We have to do symbol dependency testing. */
2007 struct relocate_args args
;
2010 args
.reloc_mode
= ((GLRO(dl_lazy
) ? RTLD_LAZY
: 0)
2013 i
= main_map
->l_searchlist
.r_nlist
;
2016 struct link_map
*l
= main_map
->l_initfini
[i
];
2017 if (l
!= &GL(dl_rtld_map
) && ! l
->l_faked
)
2020 _dl_receive_error (print_unresolved
, relocate_doit
,
2025 if ((GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
2026 && rtld_multiple_ref
)
2028 /* Mark the link map as not yet relocated again. */
2029 GL(dl_rtld_map
).l_relocated
= 0;
2030 _dl_relocate_object (&GL(dl_rtld_map
),
2031 main_map
->l_scope
, __RTLD_NOIFUNC
, 0);
2034 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2037 /* Print more information. This means here, print information
2038 about the versions needed. */
2040 struct link_map
*map
;
2042 for (map
= main_map
; map
!= NULL
; map
= map
->l_next
)
2045 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
2051 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
2052 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
2056 _dl_printf ("\n\tVersion information:\n");
2060 _dl_printf ("\t%s:\n", DSO_FILENAME (map
->l_name
));
2065 struct link_map
*needed
;
2067 needed
= find_needed (strtab
+ ent
->vn_file
);
2068 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
2072 const char *fname
= NULL
;
2075 && match_version (strtab
+ aux
->vna_name
,
2077 fname
= needed
->l_name
;
2079 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2080 strtab
+ ent
->vn_file
,
2081 strtab
+ aux
->vna_name
,
2082 aux
->vna_flags
& VER_FLG_WEAK
2084 fname
?: "not found");
2086 if (aux
->vna_next
== 0)
2087 /* No more symbols. */
2091 aux
= (ElfW(Vernaux
) *) ((char *) aux
2095 if (ent
->vn_next
== 0)
2096 /* No more dependencies. */
2099 /* Next dependency. */
2100 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
2109 if (main_map
->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]
2110 && ! __builtin_expect (GLRO(dl_profile
) != NULL
, 0)
2111 && ! __builtin_expect (GLRO(dl_dynamic_weak
), 0))
2113 ElfW(Lib
) *liblist
, *liblistend
;
2114 struct link_map
**r_list
, **r_listend
, *l
;
2115 const char *strtab
= (const void *) D_PTR (main_map
, l_info
[DT_STRTAB
]);
2117 assert (main_map
->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
);
2118 liblist
= (ElfW(Lib
) *)
2119 main_map
->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]->d_un
.d_ptr
;
2120 liblistend
= (ElfW(Lib
) *)
2122 + main_map
->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)]->d_un
.d_val
);
2123 r_list
= main_map
->l_searchlist
.r_list
;
2124 r_listend
= r_list
+ main_map
->l_searchlist
.r_nlist
;
2126 for (; r_list
< r_listend
&& liblist
< liblistend
; r_list
++)
2133 /* If the library is not mapped where it should, fail. */
2137 /* Next, check if checksum matches. */
2138 if (l
->l_info
[VALIDX(DT_CHECKSUM
)] == NULL
2139 || l
->l_info
[VALIDX(DT_CHECKSUM
)]->d_un
.d_val
2140 != liblist
->l_checksum
)
2143 if (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] == NULL
2144 || l
->l_info
[VALIDX(DT_GNU_PRELINKED
)]->d_un
.d_val
2145 != liblist
->l_time_stamp
)
2148 if (! _dl_name_match_p (strtab
+ liblist
->l_name
, l
))
2155 if (r_list
== r_listend
&& liblist
== liblistend
)
2158 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
2159 _dl_debug_printf ("\nprelink checking: %s\n",
2160 prelinked
? "ok" : "failed");
2164 /* Now set up the variable which helps the assembler startup code. */
2165 GL(dl_ns
)[LM_ID_BASE
]._ns_main_searchlist
= &main_map
->l_searchlist
;
2167 /* Save the information about the original global scope list since
2168 we need it in the memory handling later. */
2169 GLRO(dl_initial_searchlist
) = *GL(dl_ns
)[LM_ID_BASE
]._ns_main_searchlist
;
2171 /* Remember the last search directory added at startup, now that
2172 malloc will no longer be the one from dl-minimal.c. As a side
2173 effect, this marks ld.so as initialized, so that the rtld_active
2174 function returns true from now on. */
2175 GLRO(dl_init_all_dirs
) = GL(dl_all_dirs
);
2177 /* Print scope information. */
2178 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_SCOPES
))
2180 _dl_debug_printf ("\nInitial object scopes\n");
2182 for (struct link_map
*l
= main_map
; l
!= NULL
; l
= l
->l_next
)
2183 _dl_show_scope (l
, 0);
2186 _rtld_main_check (main_map
, _dl_argv
[0]);
2190 if (main_map
->l_info
[ADDRIDX (DT_GNU_CONFLICT
)] != NULL
)
2192 ElfW(Rela
) *conflict
, *conflictend
;
2194 RTLD_TIMING_VAR (start
);
2195 rtld_timer_start (&start
);
2197 assert (main_map
->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)] != NULL
);
2198 conflict
= (ElfW(Rela
) *)
2199 main_map
->l_info
[ADDRIDX (DT_GNU_CONFLICT
)]->d_un
.d_ptr
;
2200 conflictend
= (ElfW(Rela
) *)
2202 + main_map
->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)]->d_un
.d_val
);
2203 _dl_resolve_conflicts (main_map
, conflict
, conflictend
);
2205 rtld_timer_stop (&relocate_time
, start
);
2209 /* Mark all the objects so we know they have been already relocated. */
2210 for (struct link_map
*l
= main_map
; l
!= NULL
; l
= l
->l_next
)
2213 if (l
->l_relro_size
)
2214 _dl_protect_relro (l
);
2216 /* Add object to slot information data if necessasy. */
2217 if (l
->l_tls_blocksize
!= 0 && tls_init_tp_called
)
2218 _dl_add_to_slotinfo (l
);
2223 /* Now we have all the objects loaded. Relocate them all except for
2224 the dynamic linker itself. We do this in reverse order so that copy
2225 relocs of earlier objects overwrite the data written by later
2226 objects. We do not re-relocate the dynamic linker itself in this
2227 loop because that could result in the GOT entries for functions we
2228 call being changed, and that would break us. It is safe to relocate
2229 the dynamic linker out of order because it has no copy relocs (we
2230 know that because it is self-contained). */
2232 int consider_profiling
= GLRO(dl_profile
) != NULL
;
2234 /* If we are profiling we also must do lazy reloaction. */
2235 GLRO(dl_lazy
) |= consider_profiling
;
2237 RTLD_TIMING_VAR (start
);
2238 rtld_timer_start (&start
);
2239 unsigned i
= main_map
->l_searchlist
.r_nlist
;
2242 struct link_map
*l
= main_map
->l_initfini
[i
];
2244 /* While we are at it, help the memory handling a bit. We have to
2245 mark some data structures as allocated with the fake malloc()
2246 implementation in ld.so. */
2247 struct libname_list
*lnp
= l
->l_libname
->next
;
2249 while (__builtin_expect (lnp
!= NULL
, 0))
2254 /* Also allocated with the fake malloc(). */
2255 l
->l_free_initfini
= 0;
2257 if (l
!= &GL(dl_rtld_map
))
2258 _dl_relocate_object (l
, l
->l_scope
, GLRO(dl_lazy
) ? RTLD_LAZY
: 0,
2259 consider_profiling
);
2261 /* Add object to slot information data if necessasy. */
2262 if (l
->l_tls_blocksize
!= 0 && tls_init_tp_called
)
2263 _dl_add_to_slotinfo (l
);
2265 rtld_timer_stop (&relocate_time
, start
);
2267 /* Now enable profiling if needed. Like the previous call,
2268 this has to go here because the calls it makes should use the
2269 rtld versions of the functions (particularly calloc()), but it
2270 needs to have _dl_profile_map set up by the relocator. */
2271 if (__glibc_unlikely (GL(dl_profile_map
) != NULL
))
2272 /* We must prepare the profiling. */
2273 _dl_start_profile ();
2276 if ((!was_tls_init_tp_called
&& GL(dl_tls_max_dtv_idx
) > 0)
2277 || count_modids
!= _dl_count_modids ())
2278 ++GL(dl_tls_generation
);
2280 /* Now that we have completed relocation, the initializer data
2281 for the TLS blocks has its final values and we can copy them
2282 into the main thread's TLS area, which we allocated above.
2283 Note: thread-local variables must only be accessed after completing
2285 _dl_allocate_tls_init (tcbp
);
2287 /* And finally install it for the main thread. */
2288 if (! tls_init_tp_called
)
2290 const char *lossage
= TLS_INIT_TP (tcbp
);
2291 if (__glibc_unlikely (lossage
!= NULL
))
2292 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2296 /* Make sure no new search directories have been added. */
2297 assert (GLRO(dl_init_all_dirs
) == GL(dl_all_dirs
));
2299 if (! prelinked
&& rtld_multiple_ref
)
2301 /* There was an explicit ref to the dynamic linker as a shared lib.
2302 Re-relocate ourselves with user-controlled symbol definitions.
2304 We must do this after TLS initialization in case after this
2305 re-relocation, we might call a user-supplied function
2306 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2308 RTLD_TIMING_VAR (start
);
2309 rtld_timer_start (&start
);
2311 /* Mark the link map as not yet relocated again. */
2312 GL(dl_rtld_map
).l_relocated
= 0;
2313 _dl_relocate_object (&GL(dl_rtld_map
), main_map
->l_scope
, 0, 0);
2315 rtld_timer_accum (&relocate_time
, start
);
2318 /* Do any necessary cleanups for the startup OS interface code.
2319 We do these now so that no calls are made after rtld re-relocation
2320 which might be resolved to different functions than we expect.
2321 We cannot do this before relocating the other objects because
2322 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2323 _dl_sysdep_start_cleanup ();
2326 /* Auditing checkpoint: we have added all objects. */
2327 if (__glibc_unlikely (GLRO(dl_naudit
) > 0))
2329 struct link_map
*head
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2330 /* Do not call the functions for any auditing object. */
2331 if (head
->l_auditing
== 0)
2333 struct audit_ifaces
*afct
= GLRO(dl_audit
);
2334 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
2336 if (afct
->activity
!= NULL
)
2337 afct
->activity (&link_map_audit_state (head
, cnt
)->cookie
,
2346 /* Notify the debugger all new objects are now ready to go. We must re-get
2347 the address since by now the variable might be in another object. */
2348 r
= _dl_debug_initialize (0, LM_ID_BASE
);
2349 r
->r_state
= RT_CONSISTENT
;
2351 LIBC_PROBE (init_complete
, 2, LM_ID_BASE
, r
);
2353 #if defined USE_LDCONFIG && !defined MAP_COPY
2354 /* We must munmap() the cache file. */
2355 _dl_unload_cache ();
2358 /* Once we return, _dl_sysdep_start will invoke
2359 the DT_INIT functions and then *USER_ENTRY. */
2362 /* This is a little helper function for resolving symbols while
2363 tracing the binary. */
2365 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
2366 const char *errstring
)
2368 if (objname
[0] == '\0')
2369 objname
= RTLD_PROGNAME
;
2370 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
2373 /* This is a little helper function for resolving symbols while
2374 tracing the binary. */
2376 print_missing_version (int errcode
__attribute__ ((unused
)),
2377 const char *objname
, const char *errstring
)
2379 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME
,
2380 objname
, errstring
);
2383 /* Nonzero if any of the debugging options is enabled. */
2384 static int any_debug attribute_relro
;
2386 /* Process the string given as the parameter which explains which debugging
2387 options are enabled. */
2389 process_dl_debug (const char *dl_debug
)
2391 /* When adding new entries make sure that the maximal length of a name
2392 is correctly handled in the LD_DEBUG_HELP code below. */
2396 const char name
[10];
2397 const char helptext
[41];
2398 unsigned short int mask
;
2401 #define LEN_AND_STR(str) sizeof (str) - 1, str
2402 { LEN_AND_STR ("libs"), "display library search paths",
2403 DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
},
2404 { LEN_AND_STR ("reloc"), "display relocation processing",
2405 DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
},
2406 { LEN_AND_STR ("files"), "display progress for input file",
2407 DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
},
2408 { LEN_AND_STR ("symbols"), "display symbol table processing",
2409 DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
},
2410 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2411 DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
},
2412 { LEN_AND_STR ("versions"), "display version dependencies",
2413 DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
2414 { LEN_AND_STR ("scopes"), "display scope information",
2416 { LEN_AND_STR ("all"), "all previous options combined",
2417 DL_DEBUG_LIBS
| DL_DEBUG_RELOC
| DL_DEBUG_FILES
| DL_DEBUG_SYMBOLS
2418 | DL_DEBUG_BINDINGS
| DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
2419 | DL_DEBUG_SCOPES
},
2420 { LEN_AND_STR ("statistics"), "display relocation statistics",
2421 DL_DEBUG_STATISTICS
},
2422 { LEN_AND_STR ("unused"), "determined unused DSOs",
2424 { LEN_AND_STR ("help"), "display this help message and exit",
2427 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2429 /* Skip separating white spaces and commas. */
2430 while (*dl_debug
!= '\0')
2432 if (*dl_debug
!= ' ' && *dl_debug
!= ',' && *dl_debug
!= ':')
2437 while (dl_debug
[len
] != '\0' && dl_debug
[len
] != ' '
2438 && dl_debug
[len
] != ',' && dl_debug
[len
] != ':')
2441 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
2442 if (debopts
[cnt
].len
== len
2443 && memcmp (dl_debug
, debopts
[cnt
].name
, len
) == 0)
2445 GLRO(dl_debug_mask
) |= debopts
[cnt
].mask
;
2450 if (cnt
== ndebopts
)
2452 /* Display a warning and skip everything until next
2454 char *copy
= strndupa (dl_debug
, len
);
2455 _dl_error_printf ("\
2456 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy
);
2466 if (GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
)
2468 /* In order to get an accurate picture of whether a particular
2469 DT_NEEDED entry is actually used we have to process both
2470 the PLT and non-PLT relocation entries. */
2474 if (GLRO(dl_debug_mask
) & DL_DEBUG_HELP
)
2479 Valid options for the LD_DEBUG environment variable are:\n\n");
2481 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
2482 _dl_printf (" %.*s%s%s\n", debopts
[cnt
].len
, debopts
[cnt
].name
,
2483 " " + debopts
[cnt
].len
- 3,
2484 debopts
[cnt
].helptext
);
2487 To direct the debugging output into a file instead of standard output\n\
2488 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2494 process_dl_audit (char *str
)
2496 /* The parameter is a colon separated list of DSO names. */
2499 while ((p
= (strsep
) (&str
, ":")) != NULL
)
2500 if (dso_name_valid_for_suid (p
))
2502 /* This is using the local malloc, not the system malloc. The
2503 memory can never be freed. */
2504 struct audit_list
*newp
= malloc (sizeof (*newp
));
2507 if (audit_list
== NULL
)
2508 audit_list
= newp
->next
= newp
;
2511 newp
->next
= audit_list
->next
;
2512 audit_list
= audit_list
->next
= newp
;
2517 /* Process all environments variables the dynamic linker must recognize.
2518 Since all of them start with `LD_' we are a bit smarter while finding
2520 extern char **_environ attribute_hidden
;
2524 process_envvars (enum mode
*modep
)
2526 char **runp
= _environ
;
2528 enum mode mode
= normal
;
2529 char *debug_output
= NULL
;
2531 /* This is the default place for profiling data file. */
2532 GLRO(dl_profile_output
)
2533 = &"/var/tmp\0/var/profile"[__libc_enable_secure
? 9 : 0];
2535 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
2539 while (envline
[len
] != '\0' && envline
[len
] != '=')
2542 if (envline
[len
] != '=')
2543 /* This is a "LD_" variable at the end of the string without
2544 a '=' character. Ignore it since otherwise we will access
2545 invalid memory below. */
2551 /* Warning level, verbose or not. */
2552 if (memcmp (envline
, "WARN", 4) == 0)
2553 GLRO(dl_verbose
) = envline
[5] != '\0';
2557 /* Debugging of the dynamic linker? */
2558 if (memcmp (envline
, "DEBUG", 5) == 0)
2560 process_dl_debug (&envline
[6]);
2563 if (memcmp (envline
, "AUDIT", 5) == 0)
2564 audit_list_string
= &envline
[6];
2568 /* Print information about versions. */
2569 if (memcmp (envline
, "VERBOSE", 7) == 0)
2571 version_info
= envline
[8] != '\0';
2575 /* List of objects to be preloaded. */
2576 if (memcmp (envline
, "PRELOAD", 7) == 0)
2578 preloadlist
= &envline
[8];
2582 /* Which shared object shall be profiled. */
2583 if (memcmp (envline
, "PROFILE", 7) == 0 && envline
[8] != '\0')
2584 GLRO(dl_profile
) = &envline
[8];
2588 /* Do we bind early? */
2589 if (memcmp (envline
, "BIND_NOW", 8) == 0)
2591 GLRO(dl_lazy
) = envline
[9] == '\0';
2594 if (memcmp (envline
, "BIND_NOT", 8) == 0)
2595 GLRO(dl_bind_not
) = envline
[9] != '\0';
2599 /* Test whether we want to see the content of the auxiliary
2600 array passed up from the kernel. */
2601 if (!__libc_enable_secure
2602 && memcmp (envline
, "SHOW_AUXV", 9) == 0)
2608 /* Mask for the important hardware capabilities. */
2609 if (!__libc_enable_secure
2610 && memcmp (envline
, "HWCAP_MASK", 10) == 0)
2611 GLRO(dl_hwcap_mask
) = _dl_strtoul (&envline
[11], NULL
);
2616 /* Path where the binary is found. */
2617 if (!__libc_enable_secure
2618 && memcmp (envline
, "ORIGIN_PATH", 11) == 0)
2619 GLRO(dl_origin_path
) = &envline
[12];
2623 /* The library search path. */
2624 if (!__libc_enable_secure
2625 && memcmp (envline
, "LIBRARY_PATH", 12) == 0)
2627 library_path
= &envline
[13];
2631 /* Where to place the profiling data file. */
2632 if (memcmp (envline
, "DEBUG_OUTPUT", 12) == 0)
2634 debug_output
= &envline
[13];
2638 if (!__libc_enable_secure
2639 && memcmp (envline
, "DYNAMIC_WEAK", 12) == 0)
2640 GLRO(dl_dynamic_weak
) = 1;
2644 /* We might have some extra environment variable with length 13
2646 #ifdef EXTRA_LD_ENVVARS_13
2649 if (!__libc_enable_secure
2650 && memcmp (envline
, "USE_LOAD_BIAS", 13) == 0)
2652 GLRO(dl_use_load_bias
) = envline
[14] == '1' ? -1 : 0;
2658 /* Where to place the profiling data file. */
2659 if (!__libc_enable_secure
2660 && memcmp (envline
, "PROFILE_OUTPUT", 14) == 0
2661 && envline
[15] != '\0')
2662 GLRO(dl_profile_output
) = &envline
[15];
2666 /* The mode of the dynamic linker can be set. */
2667 if (memcmp (envline
, "TRACE_PRELINKING", 16) == 0)
2670 GLRO(dl_verbose
) = 1;
2671 GLRO(dl_debug_mask
) |= DL_DEBUG_PRELINK
;
2672 GLRO(dl_trace_prelink
) = &envline
[17];
2677 /* The mode of the dynamic linker can be set. */
2678 if (memcmp (envline
, "TRACE_LOADED_OBJECTS", 20) == 0)
2682 /* We might have some extra environment variable to handle. This
2683 is tricky due to the pre-processing of the length of the name
2684 in the switch statement here. The code here assumes that added
2685 environment variables have a different length. */
2686 #ifdef EXTRA_LD_ENVVARS
2692 /* The caller wants this information. */
2695 /* Extra security for SUID binaries. Remove all dangerous environment
2697 if (__builtin_expect (__libc_enable_secure
, 0))
2699 static const char unsecure_envvars
[] =
2700 #ifdef EXTRA_UNSECURE_ENVVARS
2701 EXTRA_UNSECURE_ENVVARS
2706 nextp
= unsecure_envvars
;
2710 /* We could use rawmemchr but this need not be fast. */
2711 nextp
= (char *) (strchr
) (nextp
, '\0') + 1;
2713 while (*nextp
!= '\0');
2715 if (__access ("/etc/suid-debug", F_OK
) != 0)
2718 unsetenv ("MALLOC_CHECK_");
2720 GLRO(dl_debug_mask
) = 0;
2726 /* If we have to run the dynamic linker in debugging mode and the
2727 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2728 messages to this file. */
2729 else if (any_debug
&& debug_output
!= NULL
)
2731 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
2732 size_t name_len
= strlen (debug_output
);
2733 char buf
[name_len
+ 12];
2736 buf
[name_len
+ 11] = '\0';
2737 startp
= _itoa (__getpid (), &buf
[name_len
+ 11], 10, 0);
2739 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
2741 GLRO(dl_debug_fd
) = __open64_nocancel (startp
, flags
, DEFFILEMODE
);
2742 if (GLRO(dl_debug_fd
) == -1)
2743 /* We use standard output if opening the file failed. */
2744 GLRO(dl_debug_fd
) = STDOUT_FILENO
;
2748 #if HP_TIMING_INLINE
2750 print_statistics_item (const char *title
, hp_timing_t time
,
2753 char cycles
[HP_TIMING_PRINT_SIZE
];
2754 HP_TIMING_PRINT (cycles
, sizeof (cycles
), time
);
2756 char relative
[3 * sizeof (hp_timing_t
) + 2];
2757 char *cp
= _itoa ((1000ULL * time
) / total
, relative
+ sizeof (relative
),
2759 /* Sets the decimal point. */
2760 char *wp
= relative
;
2761 switch (relative
+ sizeof (relative
) - cp
)
2774 _dl_debug_printf ("%s: %s cycles (%s%%)\n", title
, cycles
, relative
);
2778 /* Print the various times we collected. */
2780 __attribute ((noinline
))
2781 print_statistics (const hp_timing_t
*rtld_total_timep
)
2783 #if HP_TIMING_INLINE
2785 char cycles
[HP_TIMING_PRINT_SIZE
];
2786 HP_TIMING_PRINT (cycles
, sizeof (cycles
), *rtld_total_timep
);
2787 _dl_debug_printf ("\nruntime linker statistics:\n"
2788 " total startup time in dynamic loader: %s cycles\n",
2790 print_statistics_item (" time needed for relocation",
2791 relocate_time
, *rtld_total_timep
);
2795 unsigned long int num_relative_relocations
= 0;
2796 for (Lmid_t ns
= 0; ns
< GL(dl_nns
); ++ns
)
2798 if (GL(dl_ns
)[ns
]._ns_loaded
== NULL
)
2801 struct r_scope_elem
*scope
= &GL(dl_ns
)[ns
]._ns_loaded
->l_searchlist
;
2803 for (unsigned int i
= 0; i
< scope
->r_nlist
; i
++)
2805 struct link_map
*l
= scope
->r_list
[i
];
2807 if (l
->l_addr
!= 0 && l
->l_info
[VERSYMIDX (DT_RELCOUNT
)])
2808 num_relative_relocations
2809 += l
->l_info
[VERSYMIDX (DT_RELCOUNT
)]->d_un
.d_val
;
2810 #ifndef ELF_MACHINE_REL_RELATIVE
2811 /* Relative relocations are processed on these architectures if
2812 library is loaded to different address than p_vaddr or
2813 if not prelinked. */
2814 if ((l
->l_addr
!= 0 || !l
->l_info
[VALIDX(DT_GNU_PRELINKED
)])
2815 && l
->l_info
[VERSYMIDX (DT_RELACOUNT
)])
2817 /* On e.g. IA-64 or Alpha, relative relocations are processed
2818 only if library is loaded to different address than p_vaddr. */
2819 if (l
->l_addr
!= 0 && l
->l_info
[VERSYMIDX (DT_RELACOUNT
)])
2821 num_relative_relocations
2822 += l
->l_info
[VERSYMIDX (DT_RELACOUNT
)]->d_un
.d_val
;
2826 _dl_debug_printf (" number of relocations: %lu\n"
2827 " number of relocations from cache: %lu\n"
2828 " number of relative relocations: %lu\n",
2829 GL(dl_num_relocations
),
2830 GL(dl_num_cache_relocations
),
2831 num_relative_relocations
);
2833 #if HP_TIMING_INLINE
2834 print_statistics_item (" time needed to load objects",
2835 load_time
, *rtld_total_timep
);