1 /* Run time dynamic linker.
2 Copyright (C) 1995-1999, 2000, 2001, 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
27 #include <sys/param.h>
30 #include <stdio-common/_itoa.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <bits/libc-lock.h>
35 #include "dynamic-link.h"
36 #include "dl-librecon.h"
37 #include <unsecvars.h>
39 #include <dl-procinfo.h>
43 /* Avoid PLT use for our local calls at startup. */
44 extern __typeof (__mempcpy
) __mempcpy attribute_hidden
;
46 /* GCC has mental blocks about _exit. */
47 extern __typeof (_exit
) exit_internal
asm ("_exit") attribute_hidden
;
48 #define _exit exit_internal
50 /* Helper function to handle errors while resolving symbols. */
51 static void print_unresolved (int errcode
, const char *objname
,
52 const char *errsting
);
54 /* Helper function to handle errors when a version is missing. */
55 static void print_missing_version (int errcode
, const char *objname
,
56 const char *errsting
);
58 /* Print the various times we collected. */
59 static void print_statistics (void);
61 /* This is a list of all the modes the dynamic loader can be in. */
62 enum mode
{ normal
, list
, verify
, trace
};
64 /* Process all environments variables the dynamic linker must recognize.
65 Since all of them start with `LD_' we are a bit smarter while finding
67 static void process_envvars (enum mode
*modep
);
69 int _dl_argc attribute_hidden
;
70 char **_dl_argv
= NULL
;
73 /* Nonzero if we were run directly. */
74 unsigned int _dl_skip_args attribute_hidden
;
76 /* Set nonzero during loading and initialization of executable and
77 libraries, cleared before the executable's entry point runs. This
78 must not be initialized to nonzero, because the unused dynamic
79 linker loaded in for libc.so's "ld.so.1" dep will provide the
80 definition seen by libc.so's initializer; that value must be zero,
81 and will be since that dynamic linker's _dl_start and dl_main will
83 int _dl_starting_up
= 0;
84 INTVARDEF(_dl_starting_up
)
86 /* This is the structure which defines all variables global to ld.so
87 (except those which cannot be added for some reason). */
88 struct rtld_global _rtld_global
=
90 /* Get architecture specific initializer. */
91 #include <dl-procinfo.c>
92 ._dl_debug_fd
= STDERR_FILENO
,
94 /* XXX I know about at least one case where we depend on the old
95 weak behavior (it has to do with librt). Until we get DSO
96 groups implemented we have to make this the default.
98 ._dl_dynamic_weak
= 1,
101 ._dl_fpu_control
= _FPU_DEFAULT
,
102 ._dl_correct_cache_id
= _DL_CACHE_DEFAULT_ID
,
103 ._dl_hwcap_mask
= HWCAP_IMPORTANT
,
104 #ifdef _LIBC_REENTRANT
105 ._dl_load_lock
= _LIBC_LOCK_RECURSIVE_INITIALIZER
108 strong_alias (_rtld_global
, _rtld_local
);
110 static void dl_main (const ElfW(Phdr
) *phdr
, ElfW(Word
) phnum
,
111 ElfW(Addr
) *user_entry
);
113 static struct libname_list _dl_rtld_libname
;
114 static struct libname_list _dl_rtld_libname2
;
116 /* We expect less than a second for relocation. */
117 #ifdef HP_SMALL_TIMING_AVAIL
118 # undef HP_TIMING_AVAIL
119 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
122 /* Variable for statistics. */
123 #ifndef HP_TIMING_NONAVAIL
124 static hp_timing_t rtld_total_time
;
125 static hp_timing_t relocate_time
;
126 static hp_timing_t load_time
;
127 static hp_timing_t start_time
;
130 /* Additional definitions needed by TLS initialization. */
131 #ifdef TLS_INIT_HELPER
135 /* Before ld.so is relocated we must not access variables which need
136 relocations. This means variables which are exported. Variables
137 declared as static are fine. If we can mark a variable hidden this
138 is fine, too. The latter is impotant here. We can avoid setting
139 up a temporary link map for ld.so if we can mark _rtld_global as
141 #if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \
142 && defined HAVE_VISIBILITY_ATTRIBUTE
143 # define DONT_USE_BOOTSTRAP_MAP 1
146 #ifdef DONT_USE_BOOTSTRAP_MAP
147 static ElfW(Addr
) _dl_start_final (void *arg
);
149 struct dl_start_final_info
152 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
153 hp_timing_t start_time
;
156 static ElfW(Addr
) _dl_start_final (void *arg
,
157 struct dl_start_final_info
*info
);
160 /* These defined magically in the linker script. */
161 extern char _begin
[] attribute_hidden
;
162 extern char _end
[] attribute_hidden
;
168 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
172 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
173 + DT_EXTRANUM + DT_VALTAGIDX (tag))
176 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
177 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
180 /* This is the second half of _dl_start (below). It can be inlined safely
181 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
182 references. When the tools don't permit us to avoid using a GOT entry
183 for _dl_rtld_global (no attribute_hidden support), we must make sure
184 this function is not inlined (see below). */
186 #ifdef DONT_USE_BOOTSTRAP_MAP
187 static inline ElfW(Addr
) __attribute__ ((always_inline
))
188 _dl_start_final (void *arg
)
190 static ElfW(Addr
) __attribute__ ((noinline
))
191 _dl_start_final (void *arg
, struct dl_start_final_info
*info
)
194 ElfW(Addr
) start_addr
;
198 /* If it hasn't happen yet record the startup time. */
199 if (! HP_TIMING_INLINE
)
200 HP_TIMING_NOW (start_time
);
201 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
203 start_time
= info
->start_time
;
206 /* Initialize the timing functions. */
207 HP_TIMING_DIFF_INIT ();
210 /* Transfer data about ourselves to the permanent link_map structure. */
211 #ifndef DONT_USE_BOOTSTRAP_MAP
212 GL(dl_rtld_map
).l_addr
= info
->l
.l_addr
;
213 GL(dl_rtld_map
).l_ld
= info
->l
.l_ld
;
214 memcpy (GL(dl_rtld_map
).l_info
, info
->l
.l_info
,
215 sizeof GL(dl_rtld_map
).l_info
);
216 GL(dl_rtld_map
).l_mach
= info
->l
.l_mach
;
218 _dl_setup_hash (&GL(dl_rtld_map
));
219 GL(dl_rtld_map
).l_opencount
= 1;
220 GL(dl_rtld_map
).l_map_start
= (ElfW(Addr
)) _begin
;
221 GL(dl_rtld_map
).l_map_end
= (ElfW(Addr
)) _end
;
222 /* Copy the TLS related data if necessary. */
223 #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
224 # ifdef HAVE___THREAD
225 assert (info
->l
.l_tls_modid
!= 0);
227 if (info
->l
.l_tls_modid
!= 0)
230 GL(dl_rtld_map
).l_tls_blocksize
= info
->l
.l_tls_blocksize
;
231 GL(dl_rtld_map
).l_tls_align
= info
->l
.l_tls_align
;
232 GL(dl_rtld_map
).l_tls_initimage_size
= info
->l
.l_tls_initimage_size
;
233 GL(dl_rtld_map
).l_tls_initimage
= info
->l
.l_tls_initimage
;
234 GL(dl_rtld_map
).l_tls_offset
= info
->l
.l_tls_offset
;
235 GL(dl_rtld_map
).l_tls_modid
= 1;
236 GL(dl_rtld_map
).l_tls_tp_initialized
237 = info
->l
.l_tls_tp_initialized
;
242 HP_TIMING_NOW (GL(dl_cpuclock_offset
));
245 /* Call the OS-dependent function to set up life so we can do things like
246 file access. It will call `dl_main' (below) to do all the real work
247 of the dynamic linker, and then unwind our frame and run the user
248 entry point on the same stack we entered on. */
249 start_addr
= _dl_sysdep_start (arg
, &dl_main
);
251 #ifndef HP_TIMING_NONAVAIL
254 hp_timing_t end_time
;
256 /* Get the current time. */
257 HP_TIMING_NOW (end_time
);
259 /* Compute the difference. */
260 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
264 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_STATISTICS
, 0))
270 static ElfW(Addr
) __attribute_used__ internal_function
271 _dl_start (void *arg
)
273 #ifdef DONT_USE_BOOTSTRAP_MAP
274 # define bootstrap_map GL(dl_rtld_map)
276 struct dl_start_final_info info
;
277 # define bootstrap_map info.l
279 #if !defined HAVE_BUILTIN_MEMSET || defined USE_TLS
288 /* This #define produces dynamic linking inline functions for
289 bootstrap relocation instead of general-purpose relocation. */
290 #define RTLD_BOOTSTRAP
291 #define RESOLVE_MAP(sym, version, flags) \
292 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
293 #define RESOLVE(sym, version, flags) \
294 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
295 #include "dynamic-link.h"
297 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
298 #ifdef DONT_USE_BOOTSTRAP_MAP
299 HP_TIMING_NOW (start_time
);
301 HP_TIMING_NOW (info
.start_time
);
304 /* Partly clean the `bootstrap_map' structure up. Don't use
305 `memset' since it might not be built in or inlined and we cannot
306 make function calls at this point. Use '__builtin_memset' if we
307 know it is available. We do not have to clear the memory if we
308 do not have to use the temporary bootstrap_map. Global variables
309 are initialized to zero by default. */
310 #ifndef DONT_USE_BOOTSTRAP_MAP
311 # ifdef HAVE_BUILTIN_MEMSET
312 __builtin_memset (bootstrap_map
.l_info
, '\0', sizeof (bootstrap_map
.l_info
));
315 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
317 bootstrap_map
.l_info
[cnt
] = 0;
321 /* Figure out the run-time load address of the dynamic linker itself. */
322 bootstrap_map
.l_addr
= elf_machine_load_address ();
324 /* Read our own dynamic section and fill in the info array. */
325 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
326 elf_get_dynamic_info (&bootstrap_map
);
329 # if !defined HAVE___THREAD && !defined DONT_USE_BOOTSTRAP_MAP
330 /* Signal that we have not found TLS data so far. */
331 bootstrap_map
.l_tls_modid
= 0;
334 /* Get the dynamic linker's own program header. First we need the ELF
335 file header. The `_begin' symbol created by the linker script points
336 to it. When we have something like GOTOFF relocs, we can use a plain
337 reference to find the runtime address. Without that, we have to rely
338 on the `l_addr' value, which is not the value we want when prelinked. */
339 #ifdef DONT_USE_BOOTSTRAP_MAP
340 ehdr
= (ElfW(Ehdr
) *) &_begin
;
342 ehdr
= (ElfW(Ehdr
) *) bootstrap_map
.l_addr
;
344 phdr
= (ElfW(Phdr
) *) ((ElfW(Addr
)) ehdr
+ ehdr
->e_phoff
);
345 for (cnt
= 0; cnt
< ehdr
->e_phnum
; ++cnt
)
346 if (phdr
[cnt
].p_type
== PT_TLS
)
349 size_t max_align
= MAX (TLS_INIT_TCB_ALIGN
, phdr
[cnt
].p_align
);
352 bootstrap_map
.l_tls_blocksize
= phdr
[cnt
].p_memsz
;
353 bootstrap_map
.l_tls_align
= phdr
[cnt
].p_align
;
354 assert (bootstrap_map
.l_tls_blocksize
!= 0);
355 bootstrap_map
.l_tls_initimage_size
= phdr
[cnt
].p_filesz
;
356 bootstrap_map
.l_tls_initimage
= (void *) (bootstrap_map
.l_addr
357 + phdr
[cnt
].p_vaddr
);
359 /* We can now allocate the initial TLS block. This can happen
360 on the stack. We'll get the final memory later when we
361 know all about the various objects loaded at startup
364 tlsblock
= alloca (roundup (bootstrap_map
.l_tls_blocksize
,
369 tlsblock
= alloca (roundup (TLS_INIT_TCB_SIZE
,
370 bootstrap_map
.l_tls_align
)
371 + bootstrap_map
.l_tls_blocksize
374 /* In case a model with a different layout for the TCB and DTV
375 is defined add another #elif here and in the following #ifs. */
376 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
378 /* Align the TLS block. */
379 tlsblock
= (void *) (((uintptr_t) tlsblock
+ max_align
- 1)
382 /* Initialize the dtv. [0] is the length, [1] the generation
384 initdtv
[0].counter
= 1;
385 initdtv
[1].counter
= 0;
387 /* Initialize the TLS block. */
389 initdtv
[2].pointer
= tlsblock
;
391 bootstrap_map
.l_tls_offset
= roundup (TLS_INIT_TCB_SIZE
,
392 bootstrap_map
.l_tls_align
);
393 initdtv
[2].pointer
= (char *) tlsblock
+ bootstrap_map
.l_tls_offset
;
395 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
397 p
= __mempcpy (initdtv
[2].pointer
, bootstrap_map
.l_tls_initimage
,
398 bootstrap_map
.l_tls_initimage_size
);
399 # ifdef HAVE_BUILTIN_MEMSET
400 __builtin_memset (p
, '\0', (bootstrap_map
.l_tls_blocksize
401 - bootstrap_map
.l_tls_initimage_size
));
404 size_t remaining
= (bootstrap_map
.l_tls_blocksize
405 - bootstrap_map
.l_tls_initimage_size
);
406 while (remaining
-- > 0)
411 /* Install the pointer to the dtv. */
413 /* Initialize the thread pointer. */
415 bootstrap_map
.l_tls_offset
416 = roundup (bootstrap_map
.l_tls_blocksize
, TLS_INIT_TCB_ALIGN
);
418 INSTALL_DTV ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
,
421 if (TLS_INIT_TP ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
, 0)
423 _dl_fatal_printf ("cannot setup thread-local storage\n");
425 INSTALL_DTV (tlsblock
, initdtv
);
426 if (TLS_INIT_TP (tlsblock
, 0) != 0)
427 _dl_fatal_printf ("cannot setup thread-local storage\n");
429 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
432 /* So far this is module number one. */
433 bootstrap_map
.l_tls_modid
= 1;
434 /* The TP got initialized. */
435 bootstrap_map
.l_tls_tp_initialized
= 1;
437 /* There can only be one PT_TLS entry. */
442 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
443 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
446 if (bootstrap_map
.l_addr
|| ! bootstrap_map
.l_info
[VALIDX(DT_GNU_PRELINKED
)])
448 /* Relocate ourselves so we can do normal function calls and
449 data access using the global offset table. */
451 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
454 /* Please note that we don't allow profiling of this object and
455 therefore need not test whether we have to allocate the array
456 for the relocation results (as done in dl-reloc.c). */
458 /* Now life is sane; we can call functions and access global data.
459 Set up to use the operating system facilities, and find out from
460 the operating system's program loader where to find the program
461 header table in core. Put the rest of _dl_start into a separate
462 function, that way the compiler cannot put accesses to the GOT
463 before ELF_DYNAMIC_RELOCATE. */
465 #ifdef DONT_USE_BOOTSTRAP_MAP
466 ElfW(Addr
) entry
= _dl_start_final (arg
);
468 ElfW(Addr
) entry
= _dl_start_final (arg
, &info
);
471 #ifndef ELF_MACHINE_START_ADDRESS
472 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
475 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded
), entry
);
481 /* Now life is peachy; we can do all normal operations.
482 On to the real work. */
484 /* Some helper functions. */
486 /* Arguments to relocate_doit. */
495 /* Argument to map_doit. */
497 /* Return value of map_doit. */
498 struct link_map
*main_map
;
501 /* Arguments to version_check_doit. */
502 struct version_check_args
509 relocate_doit (void *a
)
511 struct relocate_args
*args
= (struct relocate_args
*) a
;
513 INTUSE(_dl_relocate_object
) (args
->l
, args
->l
->l_scope
, args
->lazy
, 0);
519 struct map_args
*args
= (struct map_args
*) a
;
520 args
->main_map
= INTUSE(_dl_map_object
) (NULL
, args
->str
, 0, lt_library
, 0, 0);
524 version_check_doit (void *a
)
526 struct version_check_args
*args
= (struct version_check_args
*) a
;
527 if (_dl_check_all_versions (GL(dl_loaded
), 1, args
->dotrace
) && args
->doexit
)
528 /* We cannot start the application. Abort now. */
533 static inline struct link_map
*
534 find_needed (const char *name
)
536 unsigned int n
= GL(dl_loaded
)->l_searchlist
.r_nlist
;
539 if (_dl_name_match_p (name
, GL(dl_loaded
)->l_searchlist
.r_list
[n
]))
540 return GL(dl_loaded
)->l_searchlist
.r_list
[n
];
542 /* Should never happen. */
547 match_version (const char *string
, struct link_map
*map
)
549 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
552 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
553 if (map
->l_info
[VERDEFTAG
] == NULL
)
554 /* The file has no symbol versioning. */
557 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
558 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
561 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
563 /* Compare the version strings. */
564 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
568 /* If no more definitions we failed to find what we want. */
569 if (def
->vd_next
== 0)
572 /* Next definition. */
573 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
579 static const char *library_path
; /* The library search path. */
580 static const char *preloadlist
; /* The list preloaded objects. */
581 static int version_info
; /* Nonzero if information about
582 versions has to be printed. */
585 dl_main (const ElfW(Phdr
) *phdr
,
587 ElfW(Addr
) *user_entry
)
589 const ElfW(Phdr
) *ph
;
591 struct link_map
**preloads
;
592 unsigned int npreloads
;
595 bool has_interp
= false;
597 bool prelinked
= false;
598 bool rtld_is_main
= false;
599 #ifndef HP_TIMING_NONAVAIL
608 /* Process the environment variable which control the behaviour. */
609 process_envvars (&mode
);
611 /* Set up a flag which tells we are just starting. */
612 INTUSE(_dl_starting_up
) = 1;
614 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
616 /* Ho ho. We are not the program interpreter! We are the program
617 itself! This means someone ran ld.so as a command. Well, that
618 might be convenient to do sometimes. We support it by
619 interpreting the args like this:
621 ld.so PROGRAM ARGS...
623 The first argument is the name of a file containing an ELF
624 executable we will load and run with the following arguments.
625 To simplify life here, PROGRAM is searched for using the
626 normal rules for shared objects, rather than $PATH or anything
627 like that. We just load it and use its entry point; we don't
628 pay attention to its PT_INTERP command (we are the interpreter
629 ourselves). This is an easy way to test a new ld.so before
633 /* Note the place where the dynamic linker actually came from. */
634 GL(dl_rtld_map
).l_name
= rtld_progname
;
637 if (! strcmp (INTUSE(_dl_argv
)[1], "--list"))
640 GL(dl_lazy
) = -1; /* This means do no dependency analysis. */
646 else if (! strcmp (INTUSE(_dl_argv
)[1], "--verify"))
654 else if (! strcmp (INTUSE(_dl_argv
)[1], "--library-path")
657 library_path
= INTUSE(_dl_argv
)[2];
661 INTUSE(_dl_argv
) += 2;
663 else if (! strcmp (INTUSE(_dl_argv
)[1], "--inhibit-rpath")
666 GL(dl_inhibit_rpath
) = INTUSE(_dl_argv
)[2];
670 INTUSE(_dl_argv
) += 2;
675 /* If we have no further argument the program was called incorrectly.
676 Grant the user some education. */
679 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
680 You have invoked `ld.so', the helper program for shared library executables.\n\
681 This program usually lives in the file `/lib/ld.so', and special directives\n\
682 in executable files using ELF shared libraries tell the system's program\n\
683 loader to load the helper program from this file. This helper program loads\n\
684 the shared libraries needed by the program executable, prepares the program\n\
685 to run, and runs it. You may invoke this helper program directly from the\n\
686 command line to load and run an ELF executable file; this is like executing\n\
687 that file itself, but always uses this helper program from the file you\n\
688 specified, instead of the helper program file specified in the executable\n\
689 file you run. This is mostly of use for maintainers to test new versions\n\
690 of this helper program; chances are you did not intend to run this program.\n\
692 --list list all dependencies and how they are resolved\n\
693 --verify verify that given object really is a dynamically linked\n\
694 object we can handle\n\
695 --library-path PATH use given PATH instead of content of the environment\n\
696 variable LD_LIBRARY_PATH\n\
697 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
704 /* Initialize the data structures for the search paths for shared
706 _dl_init_paths (library_path
);
708 if (__builtin_expect (mode
, normal
) == verify
)
711 const char *err_str
= NULL
;
712 struct map_args args
;
714 args
.str
= rtld_progname
;
715 (void) INTUSE(_dl_catch_error
) (&objname
, &err_str
, map_doit
, &args
);
716 if (__builtin_expect (err_str
!= NULL
, 0))
717 /* We don't free the returned string, the programs stops
719 _exit (EXIT_FAILURE
);
723 HP_TIMING_NOW (start
);
724 INTUSE(_dl_map_object
) (NULL
, rtld_progname
, 0, lt_library
, 0, 0);
725 HP_TIMING_NOW (stop
);
727 HP_TIMING_DIFF (load_time
, start
, stop
);
730 phdr
= GL(dl_loaded
)->l_phdr
;
731 phnum
= GL(dl_loaded
)->l_phnum
;
732 /* We overwrite here a pointer to a malloc()ed string. But since
733 the malloc() implementation used at this point is the dummy
734 implementations which has no real free() function it does not
735 makes sense to free the old string first. */
736 GL(dl_loaded
)->l_name
= (char *) "";
737 *user_entry
= GL(dl_loaded
)->l_entry
;
741 /* Create a link_map for the executable itself.
742 This will be what dlopen on "" returns. */
743 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
744 if (GL(dl_loaded
) == NULL
)
745 _dl_fatal_printf ("cannot allocate memory for link map\n");
746 GL(dl_loaded
)->l_phdr
= phdr
;
747 GL(dl_loaded
)->l_phnum
= phnum
;
748 GL(dl_loaded
)->l_entry
= *user_entry
;
750 /* At this point we are in a bit of trouble. We would have to
751 fill in the values for l_dev and l_ino. But in general we
752 do not know where the file is. We also do not handle AT_EXECFD
753 even if it would be passed up.
755 We leave the values here defined to 0. This is normally no
756 problem as the program code itself is normally no shared
757 object and therefore cannot be loaded dynamically. Nothing
758 prevent the use of dynamic binaries and in these situations
759 we might get problems. We might not be able to find out
760 whether the object is already loaded. But since there is no
761 easy way out and because the dynamic binary must also not
762 have an SONAME we ignore this program for now. If it becomes
763 a problem we can force people using SONAMEs. */
765 /* We delay initializing the path structure until we got the dynamic
766 information for the program. */
769 GL(dl_loaded
)->l_map_end
= 0;
770 /* Perhaps the executable has no PT_LOAD header entries at all. */
771 GL(dl_loaded
)->l_map_start
= ~0;
772 /* We opened the file, account for it. */
773 ++GL(dl_loaded
)->l_opencount
;
775 /* Scan the program header table for the dynamic section. */
776 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
780 /* Find out the load address. */
781 GL(dl_loaded
)->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
784 /* This tells us where to find the dynamic section,
785 which tells us everything we need to do. */
786 GL(dl_loaded
)->l_ld
= (void *) GL(dl_loaded
)->l_addr
+ ph
->p_vaddr
;
789 /* This "interpreter segment" was used by the program loader to
790 find the program interpreter, which is this program itself, the
791 dynamic linker. We note what name finds us, so that a future
792 dlopen call or DT_NEEDED entry, for something that wants to link
793 against the dynamic linker as a shared library, will know that
794 the shared object is already loaded. */
795 _dl_rtld_libname
.name
= ((const char *) GL(dl_loaded
)->l_addr
797 /* _dl_rtld_libname.next = NULL; Already zero. */
798 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
800 /* Ordinarilly, we would get additional names for the loader from
801 our DT_SONAME. This can't happen if we were actually linked as
802 a static executable (detect this case when we have no DYNAMIC).
803 If so, assume the filename component of the interpreter path to
804 be our SONAME, and add it to our name list. */
805 if (GL(dl_rtld_map
).l_ld
== NULL
)
807 const char *p
= NULL
;
808 const char *cp
= _dl_rtld_libname
.name
;
810 /* Find the filename part of the path. */
817 _dl_rtld_libname2
.name
= p
;
818 /* _dl_rtld_libname2.next = NULL; Already zero. */
819 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
830 /* Remember where the main program starts in memory. */
831 mapstart
= (GL(dl_loaded
)->l_addr
832 + (ph
->p_vaddr
& ~(ph
->p_align
- 1)));
833 if (GL(dl_loaded
)->l_map_start
> mapstart
)
834 GL(dl_loaded
)->l_map_start
= mapstart
;
836 /* Also where it ends. */
837 allocend
= GL(dl_loaded
)->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
838 if (GL(dl_loaded
)->l_map_end
< allocend
)
839 GL(dl_loaded
)->l_map_end
= allocend
;
846 /* Note that in the case the dynamic linker we duplicate work
847 here since we read the PT_TLS entry already in
848 _dl_start_final. But the result is repeatable so do not
849 check for this special but unimportant case. */
850 GL(dl_loaded
)->l_tls_blocksize
= ph
->p_memsz
;
851 GL(dl_loaded
)->l_tls_align
= ph
->p_align
;
852 GL(dl_loaded
)->l_tls_initimage_size
= ph
->p_filesz
;
853 GL(dl_loaded
)->l_tls_initimage
= (void *) ph
->p_vaddr
;
855 /* This image gets the ID one. */
856 GL(dl_tls_max_dtv_idx
) = GL(dl_loaded
)->l_tls_modid
= 1;
862 /* Adjust the address of the TLS initialization image in case
863 the executable is actually an ET_DYN object. */
864 if (GL(dl_loaded
)->l_tls_initimage
!= NULL
)
865 GL(dl_loaded
)->l_tls_initimage
866 = (char *) GL(dl_loaded
)->l_tls_initimage
+ GL(dl_loaded
)->l_addr
;
868 if (! GL(dl_loaded
)->l_map_end
)
869 GL(dl_loaded
)->l_map_end
= ~0;
870 if (! GL(dl_rtld_map
).l_libname
&& GL(dl_rtld_map
).l_name
)
872 /* We were invoked directly, so the program might not have a
874 _dl_rtld_libname
.name
= GL(dl_rtld_map
).l_name
;
875 /* _dl_rtld_libname.next = NULL; Already zero. */
876 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
879 assert (GL(dl_rtld_map
).l_libname
); /* How else did we get here? */
883 /* Extract the contents of the dynamic section for easy access. */
884 elf_get_dynamic_info (GL(dl_loaded
));
885 if (GL(dl_loaded
)->l_info
[DT_HASH
])
886 /* Set up our cache of pointers into the hash table. */
887 _dl_setup_hash (GL(dl_loaded
));
890 if (__builtin_expect (mode
, normal
) == verify
)
892 /* We were called just to verify that this is a dynamic
893 executable using us as the program interpreter. Exit with an
894 error if we were not able to load the binary or no interpreter
895 is specified (i.e., this is no dynamically linked binary. */
896 if (GL(dl_loaded
)->l_ld
== NULL
)
899 /* We allow here some platform specific code. */
900 #ifdef DISTINGUISH_LIB_VERSIONS
901 DISTINGUISH_LIB_VERSIONS
;
903 _exit (has_interp
? 0 : 2);
907 /* Initialize the data structures for the search paths for shared
909 _dl_init_paths (library_path
);
911 /* Put the link_map for ourselves on the chain so it can be found by
912 name. Note that at this point the global chain of link maps contains
913 exactly one element, which is pointed to by dl_loaded. */
914 if (! GL(dl_rtld_map
).l_name
)
915 /* If not invoked directly, the dynamic linker shared object file was
916 found by the PT_INTERP name. */
917 GL(dl_rtld_map
).l_name
= (char *) GL(dl_rtld_map
).l_libname
->name
;
918 GL(dl_rtld_map
).l_type
= lt_library
;
919 GL(dl_loaded
)->l_next
= &GL(dl_rtld_map
);
920 GL(dl_rtld_map
).l_prev
= GL(dl_loaded
);
923 /* We have two ways to specify objects to preload: via environment
924 variable and via the file /etc/ld.so.preload. The latter can also
925 be used when security is enabled. */
929 if (__builtin_expect (preloadlist
!= NULL
, 0))
931 /* The LD_PRELOAD environment variable gives list of libraries
932 separated by white space or colons that are loaded before the
933 executable's dependencies and prepended to the global scope
934 list. If the binary is running setuid all elements
935 containing a '/' are ignored since it is insecure. */
936 char *list
= strdupa (preloadlist
);
939 HP_TIMING_NOW (start
);
941 /* Prevent optimizing strsep. Speed is not important here. */
942 while ((p
= (strsep
) (&list
, " :")) != NULL
)
944 && (__builtin_expect (! INTUSE(__libc_enable_secure
), 1)
945 || strchr (p
, '/') == NULL
))
947 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
),
951 if (++new_map
->l_opencount
== 1)
952 /* It is no duplicate. */
956 HP_TIMING_NOW (stop
);
957 HP_TIMING_DIFF (diff
, start
, stop
);
958 HP_TIMING_ACCUM_NT (load_time
, diff
);
961 /* Read the contents of the file. */
962 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
963 PROT_READ
| PROT_WRITE
);
964 if (__builtin_expect (file
!= MAP_FAILED
, 0))
966 /* Parse the file. It contains names of libraries to be loaded,
967 separated by white spaces or `:'. It may also contain
968 comments introduced by `#'. */
973 /* Eliminate comments. */
978 char *comment
= memchr (runp
, '#', rest
);
982 rest
-= comment
- runp
;
985 while (--rest
> 0 && *++comment
!= '\n');
988 /* We have one problematic case: if we have a name at the end of
989 the file without a trailing terminating characters, we cannot
990 place the \0. Handle the case separately. */
991 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
992 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
994 problem
= &file
[file_size
];
995 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
996 && problem
[-1] != '\n' && problem
[-1] != ':')
1005 file
[file_size
- 1] = '\0';
1008 HP_TIMING_NOW (start
);
1010 if (file
!= problem
)
1014 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
1017 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
),
1021 if (++new_map
->l_opencount
== 1)
1022 /* It is no duplicate. */
1027 if (problem
!= NULL
)
1029 char *p
= strndupa (problem
, file_size
- (problem
- file
));
1030 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
), p
,
1033 if (++new_map
->l_opencount
== 1)
1034 /* It is no duplicate. */
1038 HP_TIMING_NOW (stop
);
1039 HP_TIMING_DIFF (diff
, start
, stop
);
1040 HP_TIMING_ACCUM_NT (load_time
, diff
);
1042 /* We don't need the file anymore. */
1043 __munmap (file
, file_size
);
1046 if (__builtin_expect (npreloads
, 0) != 0)
1048 /* Set up PRELOADS with a vector of the preloaded libraries. */
1050 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
1051 l
= GL(dl_rtld_map
).l_next
; /* End of the chain before preloads. */
1058 assert (i
== npreloads
);
1061 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1062 specified some libraries to load, these are inserted before the actual
1063 dependencies in the executable's searchlist for symbol resolution. */
1064 HP_TIMING_NOW (start
);
1065 INTUSE(_dl_map_object_deps
) (GL(dl_loaded
), preloads
, npreloads
,
1067 HP_TIMING_NOW (stop
);
1068 HP_TIMING_DIFF (diff
, start
, stop
);
1069 HP_TIMING_ACCUM_NT (load_time
, diff
);
1071 /* Mark all objects as being in the global scope and set the open
1073 for (i
= GL(dl_loaded
)->l_searchlist
.r_nlist
; i
> 0; )
1076 GL(dl_loaded
)->l_searchlist
.r_list
[i
]->l_global
= 1;
1077 ++GL(dl_loaded
)->l_searchlist
.r_list
[i
]->l_opencount
;
1081 /* We are done mapping things, so close the zero-fill descriptor. */
1082 __close (_dl_zerofd
);
1086 /* Remove _dl_rtld_map from the chain. */
1087 GL(dl_rtld_map
).l_prev
->l_next
= GL(dl_rtld_map
).l_next
;
1088 if (GL(dl_rtld_map
).l_next
)
1089 GL(dl_rtld_map
).l_next
->l_prev
= GL(dl_rtld_map
).l_prev
;
1091 if (__builtin_expect (GL(dl_rtld_map
).l_opencount
> 1, 1))
1093 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1094 put it back in the list of visible objects. We insert it into the
1095 chain in symbol search order because gdb uses the chain's order as
1096 its symbol search order. */
1098 while (GL(dl_loaded
)->l_searchlist
.r_list
[i
] != &GL(dl_rtld_map
))
1100 GL(dl_rtld_map
).l_prev
= GL(dl_loaded
)->l_searchlist
.r_list
[i
- 1];
1101 if (__builtin_expect (mode
, normal
) == normal
)
1102 GL(dl_rtld_map
).l_next
= (i
+ 1 < GL(dl_loaded
)->l_searchlist
.r_nlist
1103 ? GL(dl_loaded
)->l_searchlist
.r_list
[i
+ 1]
1106 /* In trace mode there might be an invisible object (which we
1107 could not find) after the previous one in the search list.
1108 In this case it doesn't matter much where we put the
1109 interpreter object, so we just initialize the list pointer so
1110 that the assertion below holds. */
1111 GL(dl_rtld_map
).l_next
= GL(dl_rtld_map
).l_prev
->l_next
;
1113 assert (GL(dl_rtld_map
).l_prev
->l_next
== GL(dl_rtld_map
).l_next
);
1114 GL(dl_rtld_map
).l_prev
->l_next
= &GL(dl_rtld_map
);
1115 if (GL(dl_rtld_map
).l_next
!= NULL
)
1117 assert (GL(dl_rtld_map
).l_next
->l_prev
== GL(dl_rtld_map
).l_prev
);
1118 GL(dl_rtld_map
).l_next
->l_prev
= &GL(dl_rtld_map
);
1122 /* Now let us see whether all libraries are available in the
1123 versions we need. */
1125 struct version_check_args args
;
1126 args
.doexit
= mode
== normal
;
1127 args
.dotrace
= mode
== trace
;
1128 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
1132 /* Now it is time to determine the layout of the static TLS block
1133 and allocate it for the initial thread. Note that we always
1134 allocate the static block, we never defer it even if no
1135 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1136 use the static model. First add the dynamic linker to the list
1137 if it also uses TLS. */
1138 if (GL(dl_rtld_map
).l_tls_blocksize
!= 0)
1139 /* Assign a module ID. */
1140 GL(dl_rtld_map
).l_tls_modid
= _dl_next_tls_modid ();
1143 /* If dynamic loading of modules with TLS is impossible we do not
1144 have to initialize any of the TLS functionality unless any of the
1145 initial modules uses TLS. */
1146 if (GL(dl_tls_max_dtv_idx
) > 0)
1151 struct dtv_slotinfo
*slotinfo
;
1153 /* Number of elements in the static TLS block. */
1154 GL(dl_tls_static_nelem
) = GL(dl_tls_max_dtv_idx
);
1156 /* Allocate the array which contains the information about the
1157 dtv slots. We allocate a few entries more than needed to
1158 avoid the need for reallocation. */
1159 nelem
= GL(dl_tls_max_dtv_idx
) + 1 + TLS_SLOTINFO_SURPLUS
;
1162 GL(dl_tls_dtv_slotinfo_list
) = (struct dtv_slotinfo_list
*)
1163 malloc (sizeof (struct dtv_slotinfo_list
)
1164 + nelem
* sizeof (struct dtv_slotinfo
));
1165 /* No need to check the return value. If memory allocation failed
1166 the program would have been terminated. */
1168 slotinfo
= memset (GL(dl_tls_dtv_slotinfo_list
)->slotinfo
, '\0',
1169 nelem
* sizeof (struct dtv_slotinfo
));
1170 GL(dl_tls_dtv_slotinfo_list
)->len
= nelem
;
1171 GL(dl_tls_dtv_slotinfo_list
)->next
= NULL
;
1173 /* Fill in the information from the loaded modules. */
1174 for (l
= GL(dl_loaded
), i
= 0; l
!= NULL
; l
= l
->l_next
)
1175 if (l
->l_tls_blocksize
!= 0)
1176 /* This is a module with TLS data. Store the map reference.
1177 The generation counter is zero. */
1178 slotinfo
[++i
].map
= l
;
1179 assert (i
== GL(dl_tls_max_dtv_idx
));
1181 /* Compute the TLS offsets for the various blocks. We call this
1182 function even if none of the modules available at startup time
1183 uses TLS to initialize some variables. */
1184 _dl_determine_tlsoffset ();
1186 /* Construct the static TLS block and the dtv for the initial
1187 thread. For some platforms this will include allocating memory
1188 for the thread descriptor. The memory for the TLS block will
1189 never be freed. It should be allocated accordingly. The dtv
1190 array can be changed if dynamic loading requires it. */
1191 tcbp
= _dl_allocate_tls_storage ();
1193 _dl_fatal_printf ("\
1194 cannot allocate TLS data structures for initial thread");
1196 /* Store for detection of the special case by __tls_get_addr
1197 so it knows not to pass this dtv to the normal realloc. */
1198 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
1202 if (__builtin_expect (mode
, normal
) != normal
)
1204 /* We were run just to list the shared libraries. It is
1205 important that we do this before real relocation, because the
1206 functions we call below for output may no longer work properly
1207 after relocation. */
1208 if (! GL(dl_loaded
)->l_info
[DT_NEEDED
])
1209 _dl_printf ("\tstatically linked\n");
1214 if (GL(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1216 struct r_scope_elem
*scope
= &GL(dl_loaded
)->l_searchlist
;
1218 for (i
= 0; i
< scope
->r_nlist
; i
++)
1220 l
= scope
->r_list
[i
];
1223 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1226 if (_dl_name_match_p (GL(dl_trace_prelink
), l
))
1227 GL(dl_trace_prelink_map
) = l
;
1228 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1229 l
->l_libname
->name
[0] ? l
->l_libname
->name
1230 : rtld_progname
?: "<main program>",
1231 l
->l_name
[0] ? l
->l_name
1232 : rtld_progname
?: "<main program>",
1233 (int) sizeof l
->l_map_start
* 2,
1235 (int) sizeof l
->l_addr
* 2,
1239 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l
->l_tls_modid
,
1240 (int) sizeof l
->l_tls_offset
* 2,
1249 for (l
= GL(dl_loaded
)->l_next
; l
; l
= l
->l_next
)
1251 /* The library was not found. */
1252 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1254 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l
->l_libname
->name
,
1255 l
->l_name
, (int) sizeof l
->l_map_start
* 2,
1260 if (__builtin_expect (mode
, trace
) != trace
)
1261 for (i
= 1; i
< (unsigned int) _dl_argc
; ++i
)
1263 const ElfW(Sym
) *ref
= NULL
;
1264 ElfW(Addr
) loadbase
;
1267 result
= INTUSE(_dl_lookup_symbol
) (INTUSE(_dl_argv
)[i
],
1269 &ref
, GL(dl_loaded
)->l_scope
,
1270 ELF_RTYPE_CLASS_PLT
, 1);
1272 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
1274 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1275 INTUSE(_dl_argv
)[i
],
1276 (int) sizeof ref
->st_value
* 2, ref
->st_value
,
1277 (int) sizeof loadbase
* 2, loadbase
);
1281 /* If LD_WARN is set warn about undefined symbols. */
1282 if (GL(dl_lazy
) >= 0 && GL(dl_verbose
))
1284 /* We have to do symbol dependency testing. */
1285 struct relocate_args args
;
1288 args
.lazy
= GL(dl_lazy
);
1295 if (l
!= &GL(dl_rtld_map
) && ! l
->l_faked
)
1298 _dl_receive_error (print_unresolved
, relocate_doit
,
1304 if ((GL(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1305 && GL(dl_rtld_map
).l_opencount
> 1)
1306 INTUSE(_dl_relocate_object
) (&GL(dl_rtld_map
),
1307 GL(dl_loaded
)->l_scope
, 0, 0);
1310 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1313 /* Print more information. This means here, print information
1314 about the versions needed. */
1316 struct link_map
*map
= GL(dl_loaded
);
1318 for (map
= GL(dl_loaded
); map
!= NULL
; map
= map
->l_next
)
1321 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
1327 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
1328 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
1332 _dl_printf ("\n\tVersion information:\n");
1336 _dl_printf ("\t%s:\n",
1337 map
->l_name
[0] ? map
->l_name
: rtld_progname
);
1342 struct link_map
*needed
;
1344 needed
= find_needed (strtab
+ ent
->vn_file
);
1345 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
1349 const char *fname
= NULL
;
1352 && match_version (strtab
+ aux
->vna_name
,
1354 fname
= needed
->l_name
;
1356 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1357 strtab
+ ent
->vn_file
,
1358 strtab
+ aux
->vna_name
,
1359 aux
->vna_flags
& VER_FLG_WEAK
1361 fname
?: "not found");
1363 if (aux
->vna_next
== 0)
1364 /* No more symbols. */
1368 aux
= (ElfW(Vernaux
) *) ((char *) aux
1372 if (ent
->vn_next
== 0)
1373 /* No more dependencies. */
1376 /* Next dependency. */
1377 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1386 if (GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]
1387 && ! __builtin_expect (GL(dl_profile
) != NULL
, 0))
1389 ElfW(Lib
) *liblist
, *liblistend
;
1390 struct link_map
**r_list
, **r_listend
, *l
;
1391 const char *strtab
= (const void *) D_PTR (GL(dl_loaded
),
1394 assert (GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
);
1395 liblist
= (ElfW(Lib
) *)
1396 GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]->d_un
.d_ptr
;
1397 liblistend
= (ElfW(Lib
) *)
1399 + GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)]->d_un
.d_val
);
1400 r_list
= GL(dl_loaded
)->l_searchlist
.r_list
;
1401 r_listend
= r_list
+ GL(dl_loaded
)->l_searchlist
.r_nlist
;
1403 for (; r_list
< r_listend
&& liblist
< liblistend
; r_list
++)
1407 if (l
== GL(dl_loaded
))
1410 /* If the library is not mapped where it should, fail. */
1414 /* Next, check if checksum matches. */
1415 if (l
->l_info
[VALIDX(DT_CHECKSUM
)] == NULL
1416 || l
->l_info
[VALIDX(DT_CHECKSUM
)]->d_un
.d_val
1417 != liblist
->l_checksum
)
1420 if (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] == NULL
1421 || l
->l_info
[VALIDX(DT_GNU_PRELINKED
)]->d_un
.d_val
1422 != liblist
->l_time_stamp
)
1425 if (! _dl_name_match_p (strtab
+ liblist
->l_name
, l
))
1432 if (r_list
== r_listend
&& liblist
== liblistend
)
1435 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1436 _dl_printf ("\nprelink checking: %s\n", prelinked
? "ok" : "failed");
1443 if (GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_CONFLICT
)] != NULL
)
1445 ElfW(Rela
) *conflict
, *conflictend
;
1446 #ifndef HP_TIMING_NONAVAIL
1451 HP_TIMING_NOW (start
);
1452 assert (GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)] != NULL
);
1453 conflict
= (ElfW(Rela
) *)
1454 GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_CONFLICT
)]->d_un
.d_ptr
;
1455 conflictend
= (ElfW(Rela
) *)
1457 + GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)]->d_un
.d_val
);
1458 _dl_resolve_conflicts (GL(dl_loaded
), conflict
, conflictend
);
1459 HP_TIMING_NOW (stop
);
1460 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1464 /* Mark all the objects so we know they have been already relocated. */
1465 for (l
= GL(dl_loaded
); l
!= NULL
; l
= l
->l_next
)
1468 _dl_sysdep_start_cleanup ();
1472 /* Now we have all the objects loaded. Relocate them all except for
1473 the dynamic linker itself. We do this in reverse order so that copy
1474 relocs of earlier objects overwrite the data written by later
1475 objects. We do not re-relocate the dynamic linker itself in this
1476 loop because that could result in the GOT entries for functions we
1477 call being changed, and that would break us. It is safe to relocate
1478 the dynamic linker out of order because it has no copy relocs (we
1479 know that because it is self-contained). */
1482 int consider_profiling
= GL(dl_profile
) != NULL
;
1483 #ifndef HP_TIMING_NONAVAIL
1489 /* If we are profiling we also must do lazy reloaction. */
1490 GL(dl_lazy
) |= consider_profiling
;
1496 HP_TIMING_NOW (start
);
1499 /* While we are at it, help the memory handling a bit. We have to
1500 mark some data structures as allocated with the fake malloc()
1501 implementation in ld.so. */
1502 struct libname_list
*lnp
= l
->l_libname
->next
;
1504 while (__builtin_expect (lnp
!= NULL
, 0))
1510 if (l
!= &GL(dl_rtld_map
))
1511 INTUSE(_dl_relocate_object
) (l
, l
->l_scope
, GL(dl_lazy
),
1512 consider_profiling
);
1517 HP_TIMING_NOW (stop
);
1519 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1521 /* Do any necessary cleanups for the startup OS interface code.
1522 We do these now so that no calls are made after rtld re-relocation
1523 which might be resolved to different functions than we expect.
1524 We cannot do this before relocating the other objects because
1525 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1526 _dl_sysdep_start_cleanup ();
1528 /* Now enable profiling if needed. Like the previous call,
1529 this has to go here because the calls it makes should use the
1530 rtld versions of the functions (particularly calloc()), but it
1531 needs to have _dl_profile_map set up by the relocator. */
1532 if (__builtin_expect (GL(dl_profile_map
) != NULL
, 0))
1533 /* We must prepare the profiling. */
1534 INTUSE(_dl_start_profile
) (GL(dl_profile_map
), GL(dl_profile_output
));
1536 if (GL(dl_rtld_map
).l_opencount
> 1)
1538 /* There was an explicit ref to the dynamic linker as a shared lib.
1539 Re-relocate ourselves with user-controlled symbol definitions. */
1540 HP_TIMING_NOW (start
);
1541 INTUSE(_dl_relocate_object
) (&GL(dl_rtld_map
), GL(dl_loaded
)->l_scope
,
1543 HP_TIMING_NOW (stop
);
1544 HP_TIMING_DIFF (add
, start
, stop
);
1545 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1549 /* Now set up the variable which helps the assembler startup code. */
1550 GL(dl_main_searchlist
) = &GL(dl_loaded
)->l_searchlist
;
1551 GL(dl_global_scope
)[0] = &GL(dl_loaded
)->l_searchlist
;
1553 /* Save the information about the original global scope list since
1554 we need it in the memory handling later. */
1555 GL(dl_initial_searchlist
) = *GL(dl_main_searchlist
);
1559 if (GL(dl_tls_max_dtv_idx
) > 0)
1562 /* Now that we have completed relocation, the initializer data
1563 for the TLS blocks has its final values and we can copy them
1564 into the main thread's TLS area, which we allocated above. */
1565 _dl_allocate_tls_init (tcbp
);
1567 /* And finally install it for the main thread. */
1568 # ifndef HAVE___THREAD
1569 TLS_INIT_TP (tcbp
, GL(dl_rtld_map
).l_tls_tp_initialized
);
1571 /* If the compiler supports the __thread keyword we know that
1572 at least ld.so itself uses TLS and therefore the thread
1573 pointer was initialized earlier. */
1574 assert (GL(dl_rtld_map
).l_tls_tp_initialized
!= 0);
1575 TLS_INIT_TP (tcbp
, 1);
1581 /* Initialize _r_debug. */
1582 struct r_debug
*r
= _dl_debug_initialize (GL(dl_rtld_map
).l_addr
);
1587 #ifdef ELF_MACHINE_DEBUG_SETUP
1589 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1591 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1592 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map
), r
);
1596 if (l
->l_info
[DT_DEBUG
] != NULL
)
1597 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1598 with the run-time address of the r_debug structure */
1599 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1601 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1602 case you run gdb on the dynamic linker directly. */
1603 if (GL(dl_rtld_map
).l_info
[DT_DEBUG
] != NULL
)
1604 GL(dl_rtld_map
).l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1608 /* Notify the debugger that all objects are now mapped in. */
1609 r
->r_state
= RT_ADD
;
1610 INTUSE(_dl_debug_state
) ();
1614 /* We must munmap() the cache file. */
1615 INTUSE(_dl_unload_cache
) ();
1618 /* Once we return, _dl_sysdep_start will invoke
1619 the DT_INIT functions and then *USER_ENTRY. */
1622 /* This is a little helper function for resolving symbols while
1623 tracing the binary. */
1625 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1626 const char *errstring
)
1628 if (objname
[0] == '\0')
1629 objname
= rtld_progname
?: "<main program>";
1630 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
1633 /* This is a little helper function for resolving symbols while
1634 tracing the binary. */
1636 print_missing_version (int errcode
__attribute__ ((unused
)),
1637 const char *objname
, const char *errstring
)
1639 _dl_error_printf ("%s: %s: %s\n", rtld_progname
?: "<program name unknown>",
1640 objname
, errstring
);
1643 /* Nonzero if any of the debugging options is enabled. */
1644 static int any_debug
;
1646 /* Process the string given as the parameter which explains which debugging
1647 options are enabled. */
1649 process_dl_debug (const char *dl_debug
)
1651 /* When adding new entries make sure that the maximal length of a name
1652 is correctly handled in the LD_DEBUG_HELP code below. */
1656 const char name
[10];
1657 const char helptext
[41];
1658 unsigned short int mask
;
1661 #define LEN_AND_STR(str) sizeof (str) - 1, str
1662 { LEN_AND_STR ("libs"), "display library search paths",
1663 DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
},
1664 { LEN_AND_STR ("reloc"), "display relocation processing",
1665 DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
},
1666 { LEN_AND_STR ("files"), "display progress for input file",
1667 DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
},
1668 { LEN_AND_STR ("symbols"), "display symbol table processing",
1669 DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
},
1670 { LEN_AND_STR ("bindings"), "display information about symbol binding",
1671 DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
},
1672 { LEN_AND_STR ("versions"), "display version dependencies",
1673 DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
1674 { LEN_AND_STR ("all"), "all previous options combined",
1675 DL_DEBUG_LIBS
| DL_DEBUG_RELOC
| DL_DEBUG_FILES
| DL_DEBUG_SYMBOLS
1676 | DL_DEBUG_BINDINGS
| DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
1677 { LEN_AND_STR ("statistics"), "display relocation statistics",
1678 DL_DEBUG_STATISTICS
},
1679 { LEN_AND_STR ("help"), "display this help message and exit",
1682 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1684 /* Skip separating white spaces and commas. */
1685 while (*dl_debug
!= '\0')
1687 if (*dl_debug
!= ' ' && *dl_debug
!= ',' && *dl_debug
!= ':')
1692 while (dl_debug
[len
] != '\0' && dl_debug
[len
] != ' '
1693 && dl_debug
[len
] != ',' && dl_debug
[len
] != ':')
1696 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
1697 if (debopts
[cnt
].len
== len
1698 && memcmp (dl_debug
, debopts
[cnt
].name
, len
) == 0)
1700 GL(dl_debug_mask
) |= debopts
[cnt
].mask
;
1705 if (cnt
== ndebopts
)
1707 /* Display a warning and skip everything until next
1709 char *copy
= strndupa (dl_debug
, len
);
1710 _dl_error_printf ("\
1711 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy
);
1721 if (GL(dl_debug_mask
) & DL_DEBUG_HELP
)
1726 Valid options for the LD_DEBUG environment variable are:\n\n");
1728 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
1729 _dl_printf (" %.*s%s%s\n", debopts
[cnt
].len
, debopts
[cnt
].name
,
1730 " " + debopts
[cnt
].len
- 3,
1731 debopts
[cnt
].helptext
);
1734 To direct the debugging output into a file instead of standard output\n\
1735 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1740 /* Process all environments variables the dynamic linker must recognize.
1741 Since all of them start with `LD_' we are a bit smarter while finding
1743 extern char **_environ attribute_hidden
;
1747 process_envvars (enum mode
*modep
)
1749 char **runp
= _environ
;
1751 enum mode mode
= normal
;
1752 char *debug_output
= NULL
;
1754 /* This is the default place for profiling data file. */
1755 GL(dl_profile_output
)
1756 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure
) ? 9 : 0];
1758 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1762 while (envline
[len
] != '\0' && envline
[len
] != '=')
1765 if (envline
[len
] != '=')
1766 /* This is a "LD_" variable at the end of the string without
1767 a '=' character. Ignore it since otherwise we will access
1768 invalid memory below. */
1774 /* Warning level, verbose or not. */
1775 if (memcmp (envline
, "WARN", 4) == 0)
1776 GL(dl_verbose
) = envline
[5] != '\0';
1780 /* Debugging of the dynamic linker? */
1781 if (memcmp (envline
, "DEBUG", 5) == 0)
1782 process_dl_debug (&envline
[6]);
1786 /* Print information about versions. */
1787 if (memcmp (envline
, "VERBOSE", 7) == 0)
1789 version_info
= envline
[8] != '\0';
1793 /* List of objects to be preloaded. */
1794 if (memcmp (envline
, "PRELOAD", 7) == 0)
1796 preloadlist
= &envline
[8];
1800 /* Which shared object shall be profiled. */
1801 if (memcmp (envline
, "PROFILE", 7) == 0 && envline
[8] != '\0')
1802 GL(dl_profile
) = &envline
[8];
1806 /* Do we bind early? */
1807 if (memcmp (envline
, "BIND_NOW", 8) == 0)
1809 GL(dl_lazy
) = envline
[9] == '\0';
1812 if (memcmp (envline
, "BIND_NOT", 8) == 0)
1813 GL(dl_bind_not
) = envline
[9] != '\0';
1817 /* Test whether we want to see the content of the auxiliary
1818 array passed up from the kernel. */
1819 if (memcmp (envline
, "SHOW_AUXV", 9) == 0)
1824 /* Mask for the important hardware capabilities. */
1825 if (memcmp (envline
, "HWCAP_MASK", 10) == 0)
1826 GL(dl_hwcap_mask
) = __strtoul_internal (&envline
[11], NULL
, 0, 0);
1830 /* Path where the binary is found. */
1831 if (!INTUSE(__libc_enable_secure
)
1832 && memcmp (envline
, "ORIGIN_PATH", 11) == 0)
1833 GL(dl_origin_path
) = &envline
[12];
1837 /* The library search path. */
1838 if (memcmp (envline
, "LIBRARY_PATH", 12) == 0)
1840 library_path
= &envline
[13];
1844 /* Where to place the profiling data file. */
1845 if (memcmp (envline
, "DEBUG_OUTPUT", 12) == 0)
1847 debug_output
= &envline
[13];
1851 if (memcmp (envline
, "DYNAMIC_WEAK", 12) == 0)
1852 GL(dl_dynamic_weak
) = 1;
1856 /* Where to place the profiling data file. */
1857 if (!INTUSE(__libc_enable_secure
)
1858 && memcmp (envline
, "PROFILE_OUTPUT", 14) == 0
1859 && envline
[15] != '\0')
1860 GL(dl_profile_output
) = &envline
[15];
1864 /* The mode of the dynamic linker can be set. */
1865 if (memcmp (envline
, "TRACE_PRELINKING", 16) == 0)
1869 GL(dl_debug_mask
) |= DL_DEBUG_PRELINK
;
1870 GL(dl_trace_prelink
) = &envline
[17];
1875 /* The mode of the dynamic linker can be set. */
1876 if (memcmp (envline
, "TRACE_LOADED_OBJECTS", 20) == 0)
1880 /* We might have some extra environment variable to handle. This
1881 is tricky due to the pre-processing of the length of the name
1882 in the switch statement here. The code here assumes that added
1883 environment variables have a different length. */
1884 #ifdef EXTRA_LD_ENVVARS
1890 /* The caller wants this information. */
1893 /* Extra security for SUID binaries. Remove all dangerous environment
1895 if (__builtin_expect (INTUSE(__libc_enable_secure
), 0))
1897 static const char unsecure_envvars
[] =
1898 #ifdef EXTRA_UNSECURE_ENVVARS
1899 EXTRA_UNSECURE_ENVVARS
1904 nextp
= unsecure_envvars
;
1908 /* We could use rawmemchr but this need not be fast. */
1909 nextp
= (char *) (strchr
) (nextp
, '\0') + 1;
1911 while (*nextp
!= '\0');
1913 if (__access ("/etc/suid-debug", F_OK
) != 0)
1914 unsetenv ("MALLOC_CHECK_");
1916 /* If we have to run the dynamic linker in debugging mode and the
1917 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1918 messages to this file. */
1919 else if (any_debug
&& debug_output
!= NULL
)
1922 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
1924 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
1926 size_t name_len
= strlen (debug_output
);
1927 char buf
[name_len
+ 12];
1930 buf
[name_len
+ 11] = '\0';
1931 startp
= _itoa (__getpid (), &buf
[name_len
+ 11], 10, 0);
1933 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1935 GL(dl_debug_fd
) = __open (startp
, flags
, DEFFILEMODE
);
1936 if (GL(dl_debug_fd
) == -1)
1937 /* We use standard output if opening the file failed. */
1938 GL(dl_debug_fd
) = STDOUT_FILENO
;
1943 /* Print the various times we collected. */
1945 print_statistics (void)
1947 #ifndef HP_TIMING_NONAVAIL
1952 /* Total time rtld used. */
1953 if (HP_TIMING_AVAIL
)
1955 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1956 INTUSE(_dl_debug_printf
) ("\nruntime linker statistics:\n"
1957 " total startup time in dynamic loader: %s\n",
1961 /* Print relocation statistics. */
1962 if (HP_TIMING_AVAIL
)
1965 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1966 cp
= _itoa ((1000ULL * relocate_time
) / rtld_total_time
,
1967 pbuf
+ sizeof (pbuf
), 10, 0);
1969 switch (pbuf
+ sizeof (pbuf
) - cp
)
1980 INTUSE(_dl_debug_printf
) ("\
1981 time needed for relocation: %s (%s%%)\n",
1985 INTUSE(_dl_debug_printf
) (" number of relocations: %lu\n",
1986 GL(dl_num_relocations
));
1987 INTUSE(_dl_debug_printf
) (" number of relocations from cache: %lu\n",
1988 GL(dl_num_cache_relocations
));
1990 #ifndef HP_TIMING_NONAVAIL
1991 /* Time spend while loading the object and the dependencies. */
1992 if (HP_TIMING_AVAIL
)
1995 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1996 cp
= _itoa ((1000ULL * load_time
) / rtld_total_time
,
1997 pbuf
+ sizeof (pbuf
), 10, 0);
1999 switch (pbuf
+ sizeof (pbuf
) - cp
)
2010 INTUSE(_dl_debug_printf
) ("\
2011 time needed to load objects: %s (%s%%)\n",