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 static ElfW(Addr
) _dl_start_final (void *arg
,
150 struct link_map
*bootstrap_map_p
);
153 /* These defined magically in the linker script. */
154 extern char _begin
[] attribute_hidden
;
155 extern char _end
[] attribute_hidden
;
161 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
165 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
166 + DT_EXTRANUM + DT_VALTAGIDX (tag))
169 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
170 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
173 /* This is the second half of _dl_start (below). It can be inlined safely
174 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
175 references. When the tools don't permit us to avoid using a GOT entry
176 for _dl_rtld_global (no attribute_hidden support), we must make sure
177 this function is not inlined (see below). */
179 #ifdef DONT_USE_BOOTSTRAP_MAP
180 static inline ElfW(Addr
) __attribute__ ((always_inline
))
181 _dl_start_final (void *arg
)
183 static ElfW(Addr
) __attribute__ ((noinline
))
184 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
)
187 ElfW(Addr
) start_addr
;
191 /* If it hasn't happen yet record the startup time. */
192 if (! HP_TIMING_INLINE
)
193 HP_TIMING_NOW (start_time
);
195 /* Initialize the timing functions. */
196 HP_TIMING_DIFF_INIT ();
199 /* Transfer data about ourselves to the permanent link_map structure. */
200 #ifndef DONT_USE_BOOTSTRAP_MAP
201 GL(dl_rtld_map
).l_addr
= bootstrap_map_p
->l_addr
;
202 GL(dl_rtld_map
).l_ld
= bootstrap_map_p
->l_ld
;
203 memcpy (GL(dl_rtld_map
).l_info
, bootstrap_map_p
->l_info
,
204 sizeof GL(dl_rtld_map
).l_info
);
205 GL(dl_rtld_map
).l_mach
= bootstrap_map_p
->l_mach
;
207 _dl_setup_hash (&GL(dl_rtld_map
));
208 GL(dl_rtld_map
).l_opencount
= 1;
209 GL(dl_rtld_map
).l_map_start
= (ElfW(Addr
)) _begin
;
210 GL(dl_rtld_map
).l_map_end
= (ElfW(Addr
)) _end
;
211 /* Copy the TLS related data if necessary. */
212 #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
213 # ifdef HAVE___THREAD
214 assert (bootstrap_map_p
->l_tls_modid
!= 0);
216 if (bootstrap_map_p
->l_tls_modid
!= 0)
219 GL(dl_rtld_map
).l_tls_blocksize
= bootstrap_map_p
->l_tls_blocksize
;
220 GL(dl_rtld_map
).l_tls_align
= bootstrap_map_p
->l_tls_align
;
221 GL(dl_rtld_map
).l_tls_initimage_size
222 = bootstrap_map_p
->l_tls_initimage_size
;
223 GL(dl_rtld_map
).l_tls_initimage
= bootstrap_map_p
->l_tls_initimage
;
224 GL(dl_rtld_map
).l_tls_offset
= bootstrap_map_p
->l_tls_offset
;
225 GL(dl_rtld_map
).l_tls_modid
= 1;
226 GL(dl_rtld_map
).l_tls_tp_initialized
227 = bootstrap_map_p
->l_tls_tp_initialized
;
232 HP_TIMING_NOW (GL(dl_cpuclock_offset
));
235 /* Call the OS-dependent function to set up life so we can do things like
236 file access. It will call `dl_main' (below) to do all the real work
237 of the dynamic linker, and then unwind our frame and run the user
238 entry point on the same stack we entered on. */
239 start_addr
= _dl_sysdep_start (arg
, &dl_main
);
241 #ifndef HP_TIMING_NONAVAIL
244 hp_timing_t end_time
;
246 /* Get the current time. */
247 HP_TIMING_NOW (end_time
);
249 /* Compute the difference. */
250 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
254 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_STATISTICS
, 0))
260 static ElfW(Addr
) __attribute_used__ internal_function
261 _dl_start (void *arg
)
263 #ifdef DONT_USE_BOOTSTRAP_MAP
264 # define bootstrap_map GL(dl_rtld_map)
266 struct link_map bootstrap_map
;
268 #if !defined HAVE_BUILTIN_MEMSET || defined USE_TLS
277 /* This #define produces dynamic linking inline functions for
278 bootstrap relocation instead of general-purpose relocation. */
279 #define RTLD_BOOTSTRAP
280 #define RESOLVE_MAP(sym, version, flags) \
281 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
282 #define RESOLVE(sym, version, flags) \
283 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
284 #include "dynamic-link.h"
286 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
287 HP_TIMING_NOW (start_time
);
289 /* Partly clean the `bootstrap_map' structure up. Don't use
290 `memset' since it might not be built in or inlined and we cannot
291 make function calls at this point. Use '__builtin_memset' if we
292 know it is available. We do not have to clear the memory if we
293 do not have to use the temporary bootstrap_map. Global variables
294 are initialized to zero by default. */
295 #ifndef DONT_USE_BOOTSTRAP_MAP
296 # ifdef HAVE_BUILTIN_MEMSET
297 __builtin_memset (bootstrap_map
.l_info
, '\0', sizeof (bootstrap_map
.l_info
));
300 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
302 bootstrap_map
.l_info
[cnt
] = 0;
306 /* Figure out the run-time load address of the dynamic linker itself. */
307 bootstrap_map
.l_addr
= elf_machine_load_address ();
309 /* Read our own dynamic section and fill in the info array. */
310 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
311 elf_get_dynamic_info (&bootstrap_map
);
314 # if !defined HAVE___THREAD && !defined DONT_USE_BOOTSTRAP_MAP
315 /* Signal that we have not found TLS data so far. */
316 bootstrap_map
.l_tls_modid
= 0;
319 /* Get the dynamic linker's own program header. First we need the ELF
320 file header. The `_begin' symbol created by the linker script points
321 to it. When we have something like GOTOFF relocs, we can use a plain
322 reference to find the runtime address. Without that, we have to rely
323 on the `l_addr' value, which is not the value we want when prelinked. */
324 #ifdef DONT_USE_BOOTSTRAP_MAP
325 ehdr
= (ElfW(Ehdr
) *) &_begin
;
327 ehdr
= (ElfW(Ehdr
) *) bootstrap_map
.l_addr
;
329 phdr
= (ElfW(Phdr
) *) ((ElfW(Addr
)) ehdr
+ ehdr
->e_phoff
);
330 for (cnt
= 0; cnt
< ehdr
->e_phnum
; ++cnt
)
331 if (phdr
[cnt
].p_type
== PT_TLS
)
334 size_t max_align
= MAX (TLS_INIT_TCB_ALIGN
, phdr
[cnt
].p_align
);
337 bootstrap_map
.l_tls_blocksize
= phdr
[cnt
].p_memsz
;
338 bootstrap_map
.l_tls_align
= phdr
[cnt
].p_align
;
339 assert (bootstrap_map
.l_tls_blocksize
!= 0);
340 bootstrap_map
.l_tls_initimage_size
= phdr
[cnt
].p_filesz
;
341 bootstrap_map
.l_tls_initimage
= (void *) (bootstrap_map
.l_addr
342 + phdr
[cnt
].p_vaddr
);
344 /* We can now allocate the initial TLS block. This can happen
345 on the stack. We'll get the final memory later when we
346 know all about the various objects loaded at startup
349 tlsblock
= alloca (roundup (bootstrap_map
.l_tls_blocksize
,
354 tlsblock
= alloca (roundup (TLS_INIT_TCB_SIZE
,
355 bootstrap_map
.l_tls_align
)
356 + bootstrap_map
.l_tls_blocksize
359 /* In case a model with a different layout for the TCB and DTV
360 is defined add another #elif here and in the following #ifs. */
361 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
363 /* Align the TLS block. */
364 tlsblock
= (void *) (((uintptr_t) tlsblock
+ max_align
- 1)
367 /* Initialize the dtv. [0] is the length, [1] the generation
369 initdtv
[0].counter
= 1;
370 initdtv
[1].counter
= 0;
372 /* Initialize the TLS block. */
374 initdtv
[2].pointer
= tlsblock
;
376 bootstrap_map
.l_tls_offset
= roundup (TLS_INIT_TCB_SIZE
,
377 bootstrap_map
.l_tls_align
);
378 initdtv
[2].pointer
= (char *) tlsblock
+ bootstrap_map
.l_tls_offset
;
380 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
382 p
= __mempcpy (initdtv
[2].pointer
, bootstrap_map
.l_tls_initimage
,
383 bootstrap_map
.l_tls_initimage_size
);
384 # ifdef HAVE_BUILTIN_MEMSET
385 __builtin_memset (p
, '\0', (bootstrap_map
.l_tls_blocksize
386 - bootstrap_map
.l_tls_initimage_size
));
389 size_t remaining
= (bootstrap_map
.l_tls_blocksize
390 - bootstrap_map
.l_tls_initimage_size
);
391 while (remaining
-- > 0)
396 /* Install the pointer to the dtv. */
398 /* Initialize the thread pointer. */
400 bootstrap_map
.l_tls_offset
401 = roundup (bootstrap_map
.l_tls_blocksize
, TLS_INIT_TCB_ALIGN
);
403 INSTALL_DTV ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
,
406 if (TLS_INIT_TP ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
, 0)
408 _dl_fatal_printf ("cannot setup thread-local storage\n");
410 INSTALL_DTV (tlsblock
, initdtv
);
411 if (TLS_INIT_TP (tlsblock
, 0) != 0)
412 _dl_fatal_printf ("cannot setup thread-local storage\n");
414 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
417 /* So far this is module number one. */
418 bootstrap_map
.l_tls_modid
= 1;
419 /* The TP got initialized. */
420 bootstrap_map
.l_tls_tp_initialized
= 1;
422 /* There can only be one PT_TLS entry. */
427 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
428 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
431 if (bootstrap_map
.l_addr
|| ! bootstrap_map
.l_info
[VALIDX(DT_GNU_PRELINKED
)])
433 /* Relocate ourselves so we can do normal function calls and
434 data access using the global offset table. */
436 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
439 /* Please note that we don't allow profiling of this object and
440 therefore need not test whether we have to allocate the array
441 for the relocation results (as done in dl-reloc.c). */
443 /* Now life is sane; we can call functions and access global data.
444 Set up to use the operating system facilities, and find out from
445 the operating system's program loader where to find the program
446 header table in core. Put the rest of _dl_start into a separate
447 function, that way the compiler cannot put accesses to the GOT
448 before ELF_DYNAMIC_RELOCATE. */
450 #ifdef DONT_USE_BOOTSTRAP_MAP
451 ElfW(Addr
) entry
= _dl_start_final (arg
);
453 ElfW(Addr
) entry
= _dl_start_final (arg
, &bootstrap_map
);
456 #ifndef ELF_MACHINE_START_ADDRESS
457 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
460 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded
), entry
);
466 /* Now life is peachy; we can do all normal operations.
467 On to the real work. */
469 /* Some helper functions. */
471 /* Arguments to relocate_doit. */
480 /* Argument to map_doit. */
482 /* Return value of map_doit. */
483 struct link_map
*main_map
;
486 /* Arguments to version_check_doit. */
487 struct version_check_args
494 relocate_doit (void *a
)
496 struct relocate_args
*args
= (struct relocate_args
*) a
;
498 INTUSE(_dl_relocate_object
) (args
->l
, args
->l
->l_scope
, args
->lazy
, 0);
504 struct map_args
*args
= (struct map_args
*) a
;
505 args
->main_map
= INTUSE(_dl_map_object
) (NULL
, args
->str
, 0, lt_library
, 0, 0);
509 version_check_doit (void *a
)
511 struct version_check_args
*args
= (struct version_check_args
*) a
;
512 if (_dl_check_all_versions (GL(dl_loaded
), 1, args
->dotrace
) && args
->doexit
)
513 /* We cannot start the application. Abort now. */
518 static inline struct link_map
*
519 find_needed (const char *name
)
521 unsigned int n
= GL(dl_loaded
)->l_searchlist
.r_nlist
;
524 if (_dl_name_match_p (name
, GL(dl_loaded
)->l_searchlist
.r_list
[n
]))
525 return GL(dl_loaded
)->l_searchlist
.r_list
[n
];
527 /* Should never happen. */
532 match_version (const char *string
, struct link_map
*map
)
534 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
537 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
538 if (map
->l_info
[VERDEFTAG
] == NULL
)
539 /* The file has no symbol versioning. */
542 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
543 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
546 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
548 /* Compare the version strings. */
549 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
553 /* If no more definitions we failed to find what we want. */
554 if (def
->vd_next
== 0)
557 /* Next definition. */
558 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
564 static const char *library_path
; /* The library search path. */
565 static const char *preloadlist
; /* The list preloaded objects. */
566 static int version_info
; /* Nonzero if information about
567 versions has to be printed. */
570 dl_main (const ElfW(Phdr
) *phdr
,
572 ElfW(Addr
) *user_entry
)
574 const ElfW(Phdr
) *ph
;
576 struct link_map
**preloads
;
577 unsigned int npreloads
;
580 bool has_interp
= false;
582 bool prelinked
= false;
583 bool rtld_is_main
= false;
584 #ifndef HP_TIMING_NONAVAIL
593 /* Process the environment variable which control the behaviour. */
594 process_envvars (&mode
);
596 /* Set up a flag which tells we are just starting. */
597 INTUSE(_dl_starting_up
) = 1;
599 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
601 /* Ho ho. We are not the program interpreter! We are the program
602 itself! This means someone ran ld.so as a command. Well, that
603 might be convenient to do sometimes. We support it by
604 interpreting the args like this:
606 ld.so PROGRAM ARGS...
608 The first argument is the name of a file containing an ELF
609 executable we will load and run with the following arguments.
610 To simplify life here, PROGRAM is searched for using the
611 normal rules for shared objects, rather than $PATH or anything
612 like that. We just load it and use its entry point; we don't
613 pay attention to its PT_INTERP command (we are the interpreter
614 ourselves). This is an easy way to test a new ld.so before
618 /* Note the place where the dynamic linker actually came from. */
619 GL(dl_rtld_map
).l_name
= rtld_progname
;
622 if (! strcmp (INTUSE(_dl_argv
)[1], "--list"))
625 GL(dl_lazy
) = -1; /* This means do no dependency analysis. */
631 else if (! strcmp (INTUSE(_dl_argv
)[1], "--verify"))
639 else if (! strcmp (INTUSE(_dl_argv
)[1], "--library-path")
642 library_path
= INTUSE(_dl_argv
)[2];
646 INTUSE(_dl_argv
) += 2;
648 else if (! strcmp (INTUSE(_dl_argv
)[1], "--inhibit-rpath")
651 GL(dl_inhibit_rpath
) = INTUSE(_dl_argv
)[2];
655 INTUSE(_dl_argv
) += 2;
660 /* If we have no further argument the program was called incorrectly.
661 Grant the user some education. */
664 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
665 You have invoked `ld.so', the helper program for shared library executables.\n\
666 This program usually lives in the file `/lib/ld.so', and special directives\n\
667 in executable files using ELF shared libraries tell the system's program\n\
668 loader to load the helper program from this file. This helper program loads\n\
669 the shared libraries needed by the program executable, prepares the program\n\
670 to run, and runs it. You may invoke this helper program directly from the\n\
671 command line to load and run an ELF executable file; this is like executing\n\
672 that file itself, but always uses this helper program from the file you\n\
673 specified, instead of the helper program file specified in the executable\n\
674 file you run. This is mostly of use for maintainers to test new versions\n\
675 of this helper program; chances are you did not intend to run this program.\n\
677 --list list all dependencies and how they are resolved\n\
678 --verify verify that given object really is a dynamically linked\n\
679 object we can handle\n\
680 --library-path PATH use given PATH instead of content of the environment\n\
681 variable LD_LIBRARY_PATH\n\
682 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
689 /* Initialize the data structures for the search paths for shared
691 _dl_init_paths (library_path
);
693 if (__builtin_expect (mode
, normal
) == verify
)
696 const char *err_str
= NULL
;
697 struct map_args args
;
699 args
.str
= rtld_progname
;
700 (void) INTUSE(_dl_catch_error
) (&objname
, &err_str
, map_doit
, &args
);
701 if (__builtin_expect (err_str
!= NULL
, 0))
702 /* We don't free the returned string, the programs stops
704 _exit (EXIT_FAILURE
);
708 HP_TIMING_NOW (start
);
709 INTUSE(_dl_map_object
) (NULL
, rtld_progname
, 0, lt_library
, 0, 0);
710 HP_TIMING_NOW (stop
);
712 HP_TIMING_DIFF (load_time
, start
, stop
);
715 phdr
= GL(dl_loaded
)->l_phdr
;
716 phnum
= GL(dl_loaded
)->l_phnum
;
717 /* We overwrite here a pointer to a malloc()ed string. But since
718 the malloc() implementation used at this point is the dummy
719 implementations which has no real free() function it does not
720 makes sense to free the old string first. */
721 GL(dl_loaded
)->l_name
= (char *) "";
722 *user_entry
= GL(dl_loaded
)->l_entry
;
726 /* Create a link_map for the executable itself.
727 This will be what dlopen on "" returns. */
728 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
729 if (GL(dl_loaded
) == NULL
)
730 _dl_fatal_printf ("cannot allocate memory for link map\n");
731 GL(dl_loaded
)->l_phdr
= phdr
;
732 GL(dl_loaded
)->l_phnum
= phnum
;
733 GL(dl_loaded
)->l_entry
= *user_entry
;
735 /* At this point we are in a bit of trouble. We would have to
736 fill in the values for l_dev and l_ino. But in general we
737 do not know where the file is. We also do not handle AT_EXECFD
738 even if it would be passed up.
740 We leave the values here defined to 0. This is normally no
741 problem as the program code itself is normally no shared
742 object and therefore cannot be loaded dynamically. Nothing
743 prevent the use of dynamic binaries and in these situations
744 we might get problems. We might not be able to find out
745 whether the object is already loaded. But since there is no
746 easy way out and because the dynamic binary must also not
747 have an SONAME we ignore this program for now. If it becomes
748 a problem we can force people using SONAMEs. */
750 /* We delay initializing the path structure until we got the dynamic
751 information for the program. */
754 GL(dl_loaded
)->l_map_end
= 0;
755 /* Perhaps the executable has no PT_LOAD header entries at all. */
756 GL(dl_loaded
)->l_map_start
= ~0;
757 /* We opened the file, account for it. */
758 ++GL(dl_loaded
)->l_opencount
;
760 /* Scan the program header table for the dynamic section. */
761 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
765 /* Find out the load address. */
766 GL(dl_loaded
)->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
769 /* This tells us where to find the dynamic section,
770 which tells us everything we need to do. */
771 GL(dl_loaded
)->l_ld
= (void *) GL(dl_loaded
)->l_addr
+ ph
->p_vaddr
;
774 /* This "interpreter segment" was used by the program loader to
775 find the program interpreter, which is this program itself, the
776 dynamic linker. We note what name finds us, so that a future
777 dlopen call or DT_NEEDED entry, for something that wants to link
778 against the dynamic linker as a shared library, will know that
779 the shared object is already loaded. */
780 _dl_rtld_libname
.name
= ((const char *) GL(dl_loaded
)->l_addr
782 /* _dl_rtld_libname.next = NULL; Already zero. */
783 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
785 /* Ordinarilly, we would get additional names for the loader from
786 our DT_SONAME. This can't happen if we were actually linked as
787 a static executable (detect this case when we have no DYNAMIC).
788 If so, assume the filename component of the interpreter path to
789 be our SONAME, and add it to our name list. */
790 if (GL(dl_rtld_map
).l_ld
== NULL
)
792 const char *p
= NULL
;
793 const char *cp
= _dl_rtld_libname
.name
;
795 /* Find the filename part of the path. */
802 _dl_rtld_libname2
.name
= p
;
803 /* _dl_rtld_libname2.next = NULL; Already zero. */
804 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
815 /* Remember where the main program starts in memory. */
816 mapstart
= (GL(dl_loaded
)->l_addr
817 + (ph
->p_vaddr
& ~(ph
->p_align
- 1)));
818 if (GL(dl_loaded
)->l_map_start
> mapstart
)
819 GL(dl_loaded
)->l_map_start
= mapstart
;
821 /* Also where it ends. */
822 allocend
= GL(dl_loaded
)->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
823 if (GL(dl_loaded
)->l_map_end
< allocend
)
824 GL(dl_loaded
)->l_map_end
= allocend
;
831 /* Note that in the case the dynamic linker we duplicate work
832 here since we read the PT_TLS entry already in
833 _dl_start_final. But the result is repeatable so do not
834 check for this special but unimportant case. */
835 GL(dl_loaded
)->l_tls_blocksize
= ph
->p_memsz
;
836 GL(dl_loaded
)->l_tls_align
= ph
->p_align
;
837 GL(dl_loaded
)->l_tls_initimage_size
= ph
->p_filesz
;
838 GL(dl_loaded
)->l_tls_initimage
= (void *) ph
->p_vaddr
;
840 /* This image gets the ID one. */
841 GL(dl_tls_max_dtv_idx
) = GL(dl_loaded
)->l_tls_modid
= 1;
847 /* Adjust the address of the TLS initialization image in case
848 the executable is actually an ET_DYN object. */
849 if (GL(dl_loaded
)->l_tls_initimage
!= NULL
)
850 GL(dl_loaded
)->l_tls_initimage
851 = (char *) GL(dl_loaded
)->l_tls_initimage
+ GL(dl_loaded
)->l_addr
;
853 if (! GL(dl_loaded
)->l_map_end
)
854 GL(dl_loaded
)->l_map_end
= ~0;
855 if (! GL(dl_rtld_map
).l_libname
&& GL(dl_rtld_map
).l_name
)
857 /* We were invoked directly, so the program might not have a
859 _dl_rtld_libname
.name
= GL(dl_rtld_map
).l_name
;
860 /* _dl_rtld_libname.next = NULL; Already zero. */
861 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
864 assert (GL(dl_rtld_map
).l_libname
); /* How else did we get here? */
868 /* Extract the contents of the dynamic section for easy access. */
869 elf_get_dynamic_info (GL(dl_loaded
));
870 if (GL(dl_loaded
)->l_info
[DT_HASH
])
871 /* Set up our cache of pointers into the hash table. */
872 _dl_setup_hash (GL(dl_loaded
));
875 if (__builtin_expect (mode
, normal
) == verify
)
877 /* We were called just to verify that this is a dynamic
878 executable using us as the program interpreter. Exit with an
879 error if we were not able to load the binary or no interpreter
880 is specified (i.e., this is no dynamically linked binary. */
881 if (GL(dl_loaded
)->l_ld
== NULL
)
884 /* We allow here some platform specific code. */
885 #ifdef DISTINGUISH_LIB_VERSIONS
886 DISTINGUISH_LIB_VERSIONS
;
888 _exit (has_interp
? 0 : 2);
892 /* Initialize the data structures for the search paths for shared
894 _dl_init_paths (library_path
);
896 /* Put the link_map for ourselves on the chain so it can be found by
897 name. Note that at this point the global chain of link maps contains
898 exactly one element, which is pointed to by dl_loaded. */
899 if (! GL(dl_rtld_map
).l_name
)
900 /* If not invoked directly, the dynamic linker shared object file was
901 found by the PT_INTERP name. */
902 GL(dl_rtld_map
).l_name
= (char *) GL(dl_rtld_map
).l_libname
->name
;
903 GL(dl_rtld_map
).l_type
= lt_library
;
904 GL(dl_loaded
)->l_next
= &GL(dl_rtld_map
);
905 GL(dl_rtld_map
).l_prev
= GL(dl_loaded
);
908 /* We have two ways to specify objects to preload: via environment
909 variable and via the file /etc/ld.so.preload. The latter can also
910 be used when security is enabled. */
914 if (__builtin_expect (preloadlist
!= NULL
, 0))
916 /* The LD_PRELOAD environment variable gives list of libraries
917 separated by white space or colons that are loaded before the
918 executable's dependencies and prepended to the global scope
919 list. If the binary is running setuid all elements
920 containing a '/' are ignored since it is insecure. */
921 char *list
= strdupa (preloadlist
);
924 HP_TIMING_NOW (start
);
926 /* Prevent optimizing strsep. Speed is not important here. */
927 while ((p
= (strsep
) (&list
, " :")) != NULL
)
929 && (__builtin_expect (! INTUSE(__libc_enable_secure
), 1)
930 || strchr (p
, '/') == NULL
))
932 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
),
936 if (++new_map
->l_opencount
== 1)
937 /* It is no duplicate. */
941 HP_TIMING_NOW (stop
);
942 HP_TIMING_DIFF (diff
, start
, stop
);
943 HP_TIMING_ACCUM_NT (load_time
, diff
);
946 /* Read the contents of the file. */
947 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
948 PROT_READ
| PROT_WRITE
);
949 if (__builtin_expect (file
!= MAP_FAILED
, 0))
951 /* Parse the file. It contains names of libraries to be loaded,
952 separated by white spaces or `:'. It may also contain
953 comments introduced by `#'. */
958 /* Eliminate comments. */
963 char *comment
= memchr (runp
, '#', rest
);
967 rest
-= comment
- runp
;
970 while (--rest
> 0 && *++comment
!= '\n');
973 /* We have one problematic case: if we have a name at the end of
974 the file without a trailing terminating characters, we cannot
975 place the \0. Handle the case separately. */
976 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
977 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
979 problem
= &file
[file_size
];
980 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
981 && problem
[-1] != '\n' && problem
[-1] != ':')
990 file
[file_size
- 1] = '\0';
993 HP_TIMING_NOW (start
);
999 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
1002 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
),
1006 if (++new_map
->l_opencount
== 1)
1007 /* It is no duplicate. */
1012 if (problem
!= NULL
)
1014 char *p
= strndupa (problem
, file_size
- (problem
- file
));
1015 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
), p
,
1018 if (++new_map
->l_opencount
== 1)
1019 /* It is no duplicate. */
1023 HP_TIMING_NOW (stop
);
1024 HP_TIMING_DIFF (diff
, start
, stop
);
1025 HP_TIMING_ACCUM_NT (load_time
, diff
);
1027 /* We don't need the file anymore. */
1028 __munmap (file
, file_size
);
1031 if (__builtin_expect (npreloads
, 0) != 0)
1033 /* Set up PRELOADS with a vector of the preloaded libraries. */
1035 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
1036 l
= GL(dl_rtld_map
).l_next
; /* End of the chain before preloads. */
1043 assert (i
== npreloads
);
1046 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1047 specified some libraries to load, these are inserted before the actual
1048 dependencies in the executable's searchlist for symbol resolution. */
1049 HP_TIMING_NOW (start
);
1050 INTUSE(_dl_map_object_deps
) (GL(dl_loaded
), preloads
, npreloads
,
1052 HP_TIMING_NOW (stop
);
1053 HP_TIMING_DIFF (diff
, start
, stop
);
1054 HP_TIMING_ACCUM_NT (load_time
, diff
);
1056 /* Mark all objects as being in the global scope and set the open
1058 for (i
= GL(dl_loaded
)->l_searchlist
.r_nlist
; i
> 0; )
1061 GL(dl_loaded
)->l_searchlist
.r_list
[i
]->l_global
= 1;
1062 ++GL(dl_loaded
)->l_searchlist
.r_list
[i
]->l_opencount
;
1066 /* We are done mapping things, so close the zero-fill descriptor. */
1067 __close (_dl_zerofd
);
1071 /* Remove _dl_rtld_map from the chain. */
1072 GL(dl_rtld_map
).l_prev
->l_next
= GL(dl_rtld_map
).l_next
;
1073 if (GL(dl_rtld_map
).l_next
)
1074 GL(dl_rtld_map
).l_next
->l_prev
= GL(dl_rtld_map
).l_prev
;
1076 if (__builtin_expect (GL(dl_rtld_map
).l_opencount
> 1, 1))
1078 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1079 put it back in the list of visible objects. We insert it into the
1080 chain in symbol search order because gdb uses the chain's order as
1081 its symbol search order. */
1083 while (GL(dl_loaded
)->l_searchlist
.r_list
[i
] != &GL(dl_rtld_map
))
1085 GL(dl_rtld_map
).l_prev
= GL(dl_loaded
)->l_searchlist
.r_list
[i
- 1];
1086 if (__builtin_expect (mode
, normal
) == normal
)
1087 GL(dl_rtld_map
).l_next
= (i
+ 1 < GL(dl_loaded
)->l_searchlist
.r_nlist
1088 ? GL(dl_loaded
)->l_searchlist
.r_list
[i
+ 1]
1091 /* In trace mode there might be an invisible object (which we
1092 could not find) after the previous one in the search list.
1093 In this case it doesn't matter much where we put the
1094 interpreter object, so we just initialize the list pointer so
1095 that the assertion below holds. */
1096 GL(dl_rtld_map
).l_next
= GL(dl_rtld_map
).l_prev
->l_next
;
1098 assert (GL(dl_rtld_map
).l_prev
->l_next
== GL(dl_rtld_map
).l_next
);
1099 GL(dl_rtld_map
).l_prev
->l_next
= &GL(dl_rtld_map
);
1100 if (GL(dl_rtld_map
).l_next
!= NULL
)
1102 assert (GL(dl_rtld_map
).l_next
->l_prev
== GL(dl_rtld_map
).l_prev
);
1103 GL(dl_rtld_map
).l_next
->l_prev
= &GL(dl_rtld_map
);
1107 /* Now let us see whether all libraries are available in the
1108 versions we need. */
1110 struct version_check_args args
;
1111 args
.doexit
= mode
== normal
;
1112 args
.dotrace
= mode
== trace
;
1113 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
1117 /* Now it is time to determine the layout of the static TLS block
1118 and allocate it for the initial thread. Note that we always
1119 allocate the static block, we never defer it even if no
1120 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1121 use the static model. First add the dynamic linker to the list
1122 if it also uses TLS. */
1123 if (GL(dl_rtld_map
).l_tls_blocksize
!= 0)
1124 /* Assign a module ID. */
1125 GL(dl_rtld_map
).l_tls_modid
= _dl_next_tls_modid ();
1128 /* If dynamic loading of modules with TLS is impossible we do not
1129 have to initialize any of the TLS functionality unless any of the
1130 initial modules uses TLS. */
1131 if (GL(dl_tls_max_dtv_idx
) > 0)
1136 struct dtv_slotinfo
*slotinfo
;
1138 /* Number of elements in the static TLS block. */
1139 GL(dl_tls_static_nelem
) = GL(dl_tls_max_dtv_idx
);
1141 /* Allocate the array which contains the information about the
1142 dtv slots. We allocate a few entries more than needed to
1143 avoid the need for reallocation. */
1144 nelem
= GL(dl_tls_max_dtv_idx
) + 1 + TLS_SLOTINFO_SURPLUS
;
1147 GL(dl_tls_dtv_slotinfo_list
) = (struct dtv_slotinfo_list
*)
1148 malloc (sizeof (struct dtv_slotinfo_list
)
1149 + nelem
* sizeof (struct dtv_slotinfo
));
1150 /* No need to check the return value. If memory allocation failed
1151 the program would have been terminated. */
1153 slotinfo
= memset (GL(dl_tls_dtv_slotinfo_list
)->slotinfo
, '\0',
1154 nelem
* sizeof (struct dtv_slotinfo
));
1155 GL(dl_tls_dtv_slotinfo_list
)->len
= nelem
;
1156 GL(dl_tls_dtv_slotinfo_list
)->next
= NULL
;
1158 /* Fill in the information from the loaded modules. */
1159 for (l
= GL(dl_loaded
), i
= 0; l
!= NULL
; l
= l
->l_next
)
1160 if (l
->l_tls_blocksize
!= 0)
1161 /* This is a module with TLS data. Store the map reference.
1162 The generation counter is zero. */
1163 slotinfo
[++i
].map
= l
;
1164 assert (i
== GL(dl_tls_max_dtv_idx
));
1166 /* Compute the TLS offsets for the various blocks. We call this
1167 function even if none of the modules available at startup time
1168 uses TLS to initialize some variables. */
1169 _dl_determine_tlsoffset ();
1171 /* Construct the static TLS block and the dtv for the initial
1172 thread. For some platforms this will include allocating memory
1173 for the thread descriptor. The memory for the TLS block will
1174 never be freed. It should be allocated accordingly. The dtv
1175 array can be changed if dynamic loading requires it. */
1176 tcbp
= _dl_allocate_tls_storage ();
1178 _dl_fatal_printf ("\
1179 cannot allocate TLS data structures for initial thread");
1181 /* Store for detection of the special case by __tls_get_addr
1182 so it knows not to pass this dtv to the normal realloc. */
1183 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
1187 if (__builtin_expect (mode
, normal
) != normal
)
1189 /* We were run just to list the shared libraries. It is
1190 important that we do this before real relocation, because the
1191 functions we call below for output may no longer work properly
1192 after relocation. */
1193 if (! GL(dl_loaded
)->l_info
[DT_NEEDED
])
1194 _dl_printf ("\tstatically linked\n");
1199 if (GL(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1201 struct r_scope_elem
*scope
= &GL(dl_loaded
)->l_searchlist
;
1203 for (i
= 0; i
< scope
->r_nlist
; i
++)
1205 l
= scope
->r_list
[i
];
1208 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1211 if (_dl_name_match_p (GL(dl_trace_prelink
), l
))
1212 GL(dl_trace_prelink_map
) = l
;
1213 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1214 l
->l_libname
->name
[0] ? l
->l_libname
->name
1215 : rtld_progname
?: "<main program>",
1216 l
->l_name
[0] ? l
->l_name
1217 : rtld_progname
?: "<main program>",
1218 (int) sizeof l
->l_map_start
* 2,
1220 (int) sizeof l
->l_addr
* 2,
1224 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l
->l_tls_modid
,
1225 (int) sizeof l
->l_tls_offset
* 2,
1234 for (l
= GL(dl_loaded
)->l_next
; l
; l
= l
->l_next
)
1236 /* The library was not found. */
1237 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1239 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l
->l_libname
->name
,
1240 l
->l_name
, (int) sizeof l
->l_map_start
* 2,
1245 if (__builtin_expect (mode
, trace
) != trace
)
1246 for (i
= 1; i
< (unsigned int) _dl_argc
; ++i
)
1248 const ElfW(Sym
) *ref
= NULL
;
1249 ElfW(Addr
) loadbase
;
1252 result
= INTUSE(_dl_lookup_symbol
) (INTUSE(_dl_argv
)[i
],
1254 &ref
, GL(dl_loaded
)->l_scope
,
1255 ELF_RTYPE_CLASS_PLT
, 1);
1257 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
1259 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1260 INTUSE(_dl_argv
)[i
],
1261 (int) sizeof ref
->st_value
* 2, ref
->st_value
,
1262 (int) sizeof loadbase
* 2, loadbase
);
1266 /* If LD_WARN is set warn about undefined symbols. */
1267 if (GL(dl_lazy
) >= 0 && GL(dl_verbose
))
1269 /* We have to do symbol dependency testing. */
1270 struct relocate_args args
;
1273 args
.lazy
= GL(dl_lazy
);
1280 if (l
!= &GL(dl_rtld_map
) && ! l
->l_faked
)
1283 _dl_receive_error (print_unresolved
, relocate_doit
,
1289 if ((GL(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1290 && GL(dl_rtld_map
).l_opencount
> 1)
1291 INTUSE(_dl_relocate_object
) (&GL(dl_rtld_map
),
1292 GL(dl_loaded
)->l_scope
, 0, 0);
1295 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1298 /* Print more information. This means here, print information
1299 about the versions needed. */
1301 struct link_map
*map
= GL(dl_loaded
);
1303 for (map
= GL(dl_loaded
); map
!= NULL
; map
= map
->l_next
)
1306 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
1312 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
1313 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
1317 _dl_printf ("\n\tVersion information:\n");
1321 _dl_printf ("\t%s:\n",
1322 map
->l_name
[0] ? map
->l_name
: rtld_progname
);
1327 struct link_map
*needed
;
1329 needed
= find_needed (strtab
+ ent
->vn_file
);
1330 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
1334 const char *fname
= NULL
;
1337 && match_version (strtab
+ aux
->vna_name
,
1339 fname
= needed
->l_name
;
1341 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1342 strtab
+ ent
->vn_file
,
1343 strtab
+ aux
->vna_name
,
1344 aux
->vna_flags
& VER_FLG_WEAK
1346 fname
?: "not found");
1348 if (aux
->vna_next
== 0)
1349 /* No more symbols. */
1353 aux
= (ElfW(Vernaux
) *) ((char *) aux
1357 if (ent
->vn_next
== 0)
1358 /* No more dependencies. */
1361 /* Next dependency. */
1362 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1371 if (GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]
1372 && ! __builtin_expect (GL(dl_profile
) != NULL
, 0))
1374 ElfW(Lib
) *liblist
, *liblistend
;
1375 struct link_map
**r_list
, **r_listend
, *l
;
1376 const char *strtab
= (const void *) D_PTR (GL(dl_loaded
),
1379 assert (GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
);
1380 liblist
= (ElfW(Lib
) *)
1381 GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]->d_un
.d_ptr
;
1382 liblistend
= (ElfW(Lib
) *)
1384 + GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)]->d_un
.d_val
);
1385 r_list
= GL(dl_loaded
)->l_searchlist
.r_list
;
1386 r_listend
= r_list
+ GL(dl_loaded
)->l_searchlist
.r_nlist
;
1388 for (; r_list
< r_listend
&& liblist
< liblistend
; r_list
++)
1392 if (l
== GL(dl_loaded
))
1395 /* If the library is not mapped where it should, fail. */
1399 /* Next, check if checksum matches. */
1400 if (l
->l_info
[VALIDX(DT_CHECKSUM
)] == NULL
1401 || l
->l_info
[VALIDX(DT_CHECKSUM
)]->d_un
.d_val
1402 != liblist
->l_checksum
)
1405 if (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] == NULL
1406 || l
->l_info
[VALIDX(DT_GNU_PRELINKED
)]->d_un
.d_val
1407 != liblist
->l_time_stamp
)
1410 if (! _dl_name_match_p (strtab
+ liblist
->l_name
, l
))
1417 if (r_list
== r_listend
&& liblist
== liblistend
)
1420 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1421 _dl_printf ("\nprelink checking: %s\n", prelinked
? "ok" : "failed");
1428 if (GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_CONFLICT
)] != NULL
)
1430 ElfW(Rela
) *conflict
, *conflictend
;
1431 #ifndef HP_TIMING_NONAVAIL
1436 HP_TIMING_NOW (start
);
1437 assert (GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)] != NULL
);
1438 conflict
= (ElfW(Rela
) *)
1439 GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_CONFLICT
)]->d_un
.d_ptr
;
1440 conflictend
= (ElfW(Rela
) *)
1442 + GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)]->d_un
.d_val
);
1443 _dl_resolve_conflicts (GL(dl_loaded
), conflict
, conflictend
);
1444 HP_TIMING_NOW (stop
);
1445 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1449 /* Mark all the objects so we know they have been already relocated. */
1450 for (l
= GL(dl_loaded
); l
!= NULL
; l
= l
->l_next
)
1453 _dl_sysdep_start_cleanup ();
1457 /* Now we have all the objects loaded. Relocate them all except for
1458 the dynamic linker itself. We do this in reverse order so that copy
1459 relocs of earlier objects overwrite the data written by later
1460 objects. We do not re-relocate the dynamic linker itself in this
1461 loop because that could result in the GOT entries for functions we
1462 call being changed, and that would break us. It is safe to relocate
1463 the dynamic linker out of order because it has no copy relocs (we
1464 know that because it is self-contained). */
1467 int consider_profiling
= GL(dl_profile
) != NULL
;
1468 #ifndef HP_TIMING_NONAVAIL
1474 /* If we are profiling we also must do lazy reloaction. */
1475 GL(dl_lazy
) |= consider_profiling
;
1481 HP_TIMING_NOW (start
);
1484 /* While we are at it, help the memory handling a bit. We have to
1485 mark some data structures as allocated with the fake malloc()
1486 implementation in ld.so. */
1487 struct libname_list
*lnp
= l
->l_libname
->next
;
1489 while (__builtin_expect (lnp
!= NULL
, 0))
1495 if (l
!= &GL(dl_rtld_map
))
1496 INTUSE(_dl_relocate_object
) (l
, l
->l_scope
, GL(dl_lazy
),
1497 consider_profiling
);
1502 HP_TIMING_NOW (stop
);
1504 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1506 /* Do any necessary cleanups for the startup OS interface code.
1507 We do these now so that no calls are made after rtld re-relocation
1508 which might be resolved to different functions than we expect.
1509 We cannot do this before relocating the other objects because
1510 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1511 _dl_sysdep_start_cleanup ();
1513 /* Now enable profiling if needed. Like the previous call,
1514 this has to go here because the calls it makes should use the
1515 rtld versions of the functions (particularly calloc()), but it
1516 needs to have _dl_profile_map set up by the relocator. */
1517 if (__builtin_expect (GL(dl_profile_map
) != NULL
, 0))
1518 /* We must prepare the profiling. */
1519 INTUSE(_dl_start_profile
) (GL(dl_profile_map
), GL(dl_profile_output
));
1521 if (GL(dl_rtld_map
).l_opencount
> 1)
1523 /* There was an explicit ref to the dynamic linker as a shared lib.
1524 Re-relocate ourselves with user-controlled symbol definitions. */
1525 HP_TIMING_NOW (start
);
1526 INTUSE(_dl_relocate_object
) (&GL(dl_rtld_map
), GL(dl_loaded
)->l_scope
,
1528 HP_TIMING_NOW (stop
);
1529 HP_TIMING_DIFF (add
, start
, stop
);
1530 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1534 /* Now set up the variable which helps the assembler startup code. */
1535 GL(dl_main_searchlist
) = &GL(dl_loaded
)->l_searchlist
;
1536 GL(dl_global_scope
)[0] = &GL(dl_loaded
)->l_searchlist
;
1538 /* Save the information about the original global scope list since
1539 we need it in the memory handling later. */
1540 GL(dl_initial_searchlist
) = *GL(dl_main_searchlist
);
1544 if (GL(dl_tls_max_dtv_idx
) > 0)
1547 /* Now that we have completed relocation, the initializer data
1548 for the TLS blocks has its final values and we can copy them
1549 into the main thread's TLS area, which we allocated above. */
1550 _dl_allocate_tls_init (tcbp
);
1552 /* And finally install it for the main thread. */
1553 # ifndef HAVE___THREAD
1554 TLS_INIT_TP (tcbp
, GL(dl_rtld_map
).l_tls_tp_initialized
);
1556 /* If the compiler supports the __thread keyword we know that
1557 at least ld.so itself uses TLS and therefore the thread
1558 pointer was initialized earlier. */
1559 assert (GL(dl_rtld_map
).l_tls_tp_initialized
!= 0);
1560 TLS_INIT_TP (tcbp
, 1);
1566 /* Initialize _r_debug. */
1567 struct r_debug
*r
= _dl_debug_initialize (GL(dl_rtld_map
).l_addr
);
1572 #ifdef ELF_MACHINE_DEBUG_SETUP
1574 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1576 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1577 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map
), r
);
1581 if (l
->l_info
[DT_DEBUG
] != NULL
)
1582 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1583 with the run-time address of the r_debug structure */
1584 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1586 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1587 case you run gdb on the dynamic linker directly. */
1588 if (GL(dl_rtld_map
).l_info
[DT_DEBUG
] != NULL
)
1589 GL(dl_rtld_map
).l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1593 /* Notify the debugger that all objects are now mapped in. */
1594 r
->r_state
= RT_ADD
;
1595 INTUSE(_dl_debug_state
) ();
1599 /* We must munmap() the cache file. */
1600 INTUSE(_dl_unload_cache
) ();
1603 /* Once we return, _dl_sysdep_start will invoke
1604 the DT_INIT functions and then *USER_ENTRY. */
1607 /* This is a little helper function for resolving symbols while
1608 tracing the binary. */
1610 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1611 const char *errstring
)
1613 if (objname
[0] == '\0')
1614 objname
= rtld_progname
?: "<main program>";
1615 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
1618 /* This is a little helper function for resolving symbols while
1619 tracing the binary. */
1621 print_missing_version (int errcode
__attribute__ ((unused
)),
1622 const char *objname
, const char *errstring
)
1624 _dl_error_printf ("%s: %s: %s\n", rtld_progname
?: "<program name unknown>",
1625 objname
, errstring
);
1628 /* Nonzero if any of the debugging options is enabled. */
1629 static int any_debug
;
1631 /* Process the string given as the parameter which explains which debugging
1632 options are enabled. */
1634 process_dl_debug (const char *dl_debug
)
1636 /* When adding new entries make sure that the maximal length of a name
1637 is correctly handled in the LD_DEBUG_HELP code below. */
1641 const char name
[10];
1642 const char helptext
[41];
1643 unsigned short int mask
;
1646 #define LEN_AND_STR(str) sizeof (str) - 1, str
1647 { LEN_AND_STR ("libs"), "display library search paths",
1648 DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
},
1649 { LEN_AND_STR ("reloc"), "display relocation processing",
1650 DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
},
1651 { LEN_AND_STR ("files"), "display progress for input file",
1652 DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
},
1653 { LEN_AND_STR ("symbols"), "display symbol table processing",
1654 DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
},
1655 { LEN_AND_STR ("bindings"), "display information about symbol binding",
1656 DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
},
1657 { LEN_AND_STR ("versions"), "display version dependencies",
1658 DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
1659 { LEN_AND_STR ("all"), "all previous options combined",
1660 DL_DEBUG_LIBS
| DL_DEBUG_RELOC
| DL_DEBUG_FILES
| DL_DEBUG_SYMBOLS
1661 | DL_DEBUG_BINDINGS
| DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
1662 { LEN_AND_STR ("statistics"), "display relocation statistics",
1663 DL_DEBUG_STATISTICS
},
1664 { LEN_AND_STR ("help"), "display this help message and exit",
1667 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1669 /* Skip separating white spaces and commas. */
1670 while (*dl_debug
!= '\0')
1672 if (*dl_debug
!= ' ' && *dl_debug
!= ',' && *dl_debug
!= ':')
1677 while (dl_debug
[len
] != '\0' && dl_debug
[len
] != ' '
1678 && dl_debug
[len
] != ',' && dl_debug
[len
] != ':')
1681 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
1682 if (debopts
[cnt
].len
== len
1683 && memcmp (dl_debug
, debopts
[cnt
].name
, len
) == 0)
1685 GL(dl_debug_mask
) |= debopts
[cnt
].mask
;
1690 if (cnt
== ndebopts
)
1692 /* Display a warning and skip everything until next
1694 char *copy
= strndupa (dl_debug
, len
);
1695 _dl_error_printf ("\
1696 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy
);
1706 if (GL(dl_debug_mask
) & DL_DEBUG_HELP
)
1711 Valid options for the LD_DEBUG environment variable are:\n\n");
1713 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
1714 _dl_printf (" %.*s%s%s\n", debopts
[cnt
].len
, debopts
[cnt
].name
,
1715 " " + debopts
[cnt
].len
- 3,
1716 debopts
[cnt
].helptext
);
1719 To direct the debugging output into a file instead of standard output\n\
1720 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1725 /* Process all environments variables the dynamic linker must recognize.
1726 Since all of them start with `LD_' we are a bit smarter while finding
1728 extern char **_environ attribute_hidden
;
1732 process_envvars (enum mode
*modep
)
1734 char **runp
= _environ
;
1736 enum mode mode
= normal
;
1737 char *debug_output
= NULL
;
1739 /* This is the default place for profiling data file. */
1740 GL(dl_profile_output
)
1741 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure
) ? 9 : 0];
1743 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1747 while (envline
[len
] != '\0' && envline
[len
] != '=')
1750 if (envline
[len
] != '=')
1751 /* This is a "LD_" variable at the end of the string without
1752 a '=' character. Ignore it since otherwise we will access
1753 invalid memory below. */
1759 /* Warning level, verbose or not. */
1760 if (memcmp (envline
, "WARN", 4) == 0)
1761 GL(dl_verbose
) = envline
[5] != '\0';
1765 /* Debugging of the dynamic linker? */
1766 if (memcmp (envline
, "DEBUG", 5) == 0)
1767 process_dl_debug (&envline
[6]);
1771 /* Print information about versions. */
1772 if (memcmp (envline
, "VERBOSE", 7) == 0)
1774 version_info
= envline
[8] != '\0';
1778 /* List of objects to be preloaded. */
1779 if (memcmp (envline
, "PRELOAD", 7) == 0)
1781 preloadlist
= &envline
[8];
1785 /* Which shared object shall be profiled. */
1786 if (memcmp (envline
, "PROFILE", 7) == 0 && envline
[8] != '\0')
1787 GL(dl_profile
) = &envline
[8];
1791 /* Do we bind early? */
1792 if (memcmp (envline
, "BIND_NOW", 8) == 0)
1794 GL(dl_lazy
) = envline
[9] == '\0';
1797 if (memcmp (envline
, "BIND_NOT", 8) == 0)
1798 GL(dl_bind_not
) = envline
[9] != '\0';
1802 /* Test whether we want to see the content of the auxiliary
1803 array passed up from the kernel. */
1804 if (memcmp (envline
, "SHOW_AUXV", 9) == 0)
1809 /* Mask for the important hardware capabilities. */
1810 if (memcmp (envline
, "HWCAP_MASK", 10) == 0)
1811 GL(dl_hwcap_mask
) = __strtoul_internal (&envline
[11], NULL
, 0, 0);
1815 /* Path where the binary is found. */
1816 if (!INTUSE(__libc_enable_secure
)
1817 && memcmp (envline
, "ORIGIN_PATH", 11) == 0)
1818 GL(dl_origin_path
) = &envline
[12];
1822 /* The library search path. */
1823 if (memcmp (envline
, "LIBRARY_PATH", 12) == 0)
1825 library_path
= &envline
[13];
1829 /* Where to place the profiling data file. */
1830 if (memcmp (envline
, "DEBUG_OUTPUT", 12) == 0)
1832 debug_output
= &envline
[13];
1836 if (memcmp (envline
, "DYNAMIC_WEAK", 12) == 0)
1837 GL(dl_dynamic_weak
) = 1;
1841 /* Where to place the profiling data file. */
1842 if (!INTUSE(__libc_enable_secure
)
1843 && memcmp (envline
, "PROFILE_OUTPUT", 14) == 0
1844 && envline
[15] != '\0')
1845 GL(dl_profile_output
) = &envline
[15];
1849 /* The mode of the dynamic linker can be set. */
1850 if (memcmp (envline
, "TRACE_PRELINKING", 16) == 0)
1854 GL(dl_debug_mask
) |= DL_DEBUG_PRELINK
;
1855 GL(dl_trace_prelink
) = &envline
[17];
1860 /* The mode of the dynamic linker can be set. */
1861 if (memcmp (envline
, "TRACE_LOADED_OBJECTS", 20) == 0)
1865 /* We might have some extra environment variable to handle. This
1866 is tricky due to the pre-processing of the length of the name
1867 in the switch statement here. The code here assumes that added
1868 environment variables have a different length. */
1869 #ifdef EXTRA_LD_ENVVARS
1875 /* The caller wants this information. */
1878 /* Extra security for SUID binaries. Remove all dangerous environment
1880 if (__builtin_expect (INTUSE(__libc_enable_secure
), 0))
1882 static const char unsecure_envvars
[] =
1883 #ifdef EXTRA_UNSECURE_ENVVARS
1884 EXTRA_UNSECURE_ENVVARS
1889 nextp
= unsecure_envvars
;
1893 /* We could use rawmemchr but this need not be fast. */
1894 nextp
= (char *) (strchr
) (nextp
, '\0') + 1;
1896 while (*nextp
!= '\0');
1898 if (__access ("/etc/suid-debug", F_OK
) != 0)
1899 unsetenv ("MALLOC_CHECK_");
1901 /* If we have to run the dynamic linker in debugging mode and the
1902 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1903 messages to this file. */
1904 else if (any_debug
&& debug_output
!= NULL
)
1907 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
1909 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
1911 size_t name_len
= strlen (debug_output
);
1912 char buf
[name_len
+ 12];
1915 buf
[name_len
+ 11] = '\0';
1916 startp
= _itoa (__getpid (), &buf
[name_len
+ 11], 10, 0);
1918 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1920 GL(dl_debug_fd
) = __open (startp
, flags
, DEFFILEMODE
);
1921 if (GL(dl_debug_fd
) == -1)
1922 /* We use standard output if opening the file failed. */
1923 GL(dl_debug_fd
) = STDOUT_FILENO
;
1928 /* Print the various times we collected. */
1930 print_statistics (void)
1932 #ifndef HP_TIMING_NONAVAIL
1937 /* Total time rtld used. */
1938 if (HP_TIMING_AVAIL
)
1940 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1941 INTUSE(_dl_debug_printf
) ("\nruntime linker statistics:\n"
1942 " total startup time in dynamic loader: %s\n",
1946 /* Print relocation statistics. */
1947 if (HP_TIMING_AVAIL
)
1950 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1951 cp
= _itoa ((1000ULL * relocate_time
) / rtld_total_time
,
1952 pbuf
+ sizeof (pbuf
), 10, 0);
1954 switch (pbuf
+ sizeof (pbuf
) - cp
)
1965 INTUSE(_dl_debug_printf
) ("\
1966 time needed for relocation: %s (%s%%)\n",
1970 INTUSE(_dl_debug_printf
) (" number of relocations: %lu\n",
1971 GL(dl_num_relocations
));
1972 INTUSE(_dl_debug_printf
) (" number of relocations from cache: %lu\n",
1973 GL(dl_num_cache_relocations
));
1975 #ifndef HP_TIMING_NONAVAIL
1976 /* Time spend while loading the object and the dependencies. */
1977 if (HP_TIMING_AVAIL
)
1980 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1981 cp
= _itoa ((1000ULL * load_time
) / rtld_total_time
,
1982 pbuf
+ sizeof (pbuf
), 10, 0);
1984 switch (pbuf
+ sizeof (pbuf
) - cp
)
1995 INTUSE(_dl_debug_printf
) ("\
1996 time needed to load objects: %s (%s%%)\n",