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
);
156 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
160 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
161 + DT_EXTRANUM + DT_VALTAGIDX (tag))
164 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
165 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
168 /* This is the second half of _dl_start (below). It can be inlined safely
169 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
170 references. When the tools don't permit us to avoid using a GOT entry
171 for _dl_rtld_global (no attribute_hidden support), we must make sure
172 this function is not inlined (see below). */
174 #ifdef DONT_USE_BOOTSTRAP_MAP
175 static inline ElfW(Addr
) __attribute__ ((always_inline
))
176 _dl_start_final (void *arg
)
178 static ElfW(Addr
) __attribute__ ((noinline
))
179 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
)
182 ElfW(Addr
) start_addr
;
183 extern char _begin
[] attribute_hidden
;
184 extern char _end
[] attribute_hidden
;
188 /* If it hasn't happen yet record the startup time. */
189 if (! HP_TIMING_INLINE
)
190 HP_TIMING_NOW (start_time
);
192 /* Initialize the timing functions. */
193 HP_TIMING_DIFF_INIT ();
196 /* Transfer data about ourselves to the permanent link_map structure. */
197 #ifndef DONT_USE_BOOTSTRAP_MAP
198 GL(dl_rtld_map
).l_addr
= bootstrap_map_p
->l_addr
;
199 GL(dl_rtld_map
).l_ld
= bootstrap_map_p
->l_ld
;
200 memcpy (GL(dl_rtld_map
).l_info
, bootstrap_map_p
->l_info
,
201 sizeof GL(dl_rtld_map
).l_info
);
202 GL(dl_rtld_map
).l_mach
= bootstrap_map_p
->l_mach
;
204 _dl_setup_hash (&GL(dl_rtld_map
));
205 GL(dl_rtld_map
).l_opencount
= 1;
206 GL(dl_rtld_map
).l_map_start
= (ElfW(Addr
)) _begin
;
207 GL(dl_rtld_map
).l_map_end
= (ElfW(Addr
)) _end
;
208 /* Copy the TLS related data if necessary. */
209 #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
210 # ifdef HAVE___THREAD
211 assert (bootstrap_map_p
->l_tls_modid
!= 0);
213 if (bootstrap_map_p
->l_tls_modid
!= 0)
216 GL(dl_rtld_map
).l_tls_blocksize
= bootstrap_map_p
->l_tls_blocksize
;
217 GL(dl_rtld_map
).l_tls_align
= bootstrap_map_p
->l_tls_align
;
218 GL(dl_rtld_map
).l_tls_initimage_size
219 = bootstrap_map_p
->l_tls_initimage_size
;
220 GL(dl_rtld_map
).l_tls_initimage
= bootstrap_map_p
->l_tls_initimage
;
221 GL(dl_rtld_map
).l_tls_offset
= bootstrap_map_p
->l_tls_offset
;
222 GL(dl_rtld_map
).l_tls_modid
= 1;
223 GL(dl_rtld_map
).l_tls_tp_initialized
224 = bootstrap_map_p
->l_tls_tp_initialized
;
229 HP_TIMING_NOW (GL(dl_cpuclock_offset
));
232 /* Call the OS-dependent function to set up life so we can do things like
233 file access. It will call `dl_main' (below) to do all the real work
234 of the dynamic linker, and then unwind our frame and run the user
235 entry point on the same stack we entered on. */
236 start_addr
= _dl_sysdep_start (arg
, &dl_main
);
238 #ifndef HP_TIMING_NONAVAIL
241 hp_timing_t end_time
;
243 /* Get the current time. */
244 HP_TIMING_NOW (end_time
);
246 /* Compute the difference. */
247 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
251 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_STATISTICS
, 0))
257 static ElfW(Addr
) __attribute_used__ internal_function
258 _dl_start (void *arg
)
260 #ifdef DONT_USE_BOOTSTRAP_MAP
261 # define bootstrap_map GL(dl_rtld_map)
263 struct link_map bootstrap_map
;
265 #if !defined HAVE_BUILTIN_MEMSET || defined USE_TLS
274 /* This #define produces dynamic linking inline functions for
275 bootstrap relocation instead of general-purpose relocation. */
276 #define RTLD_BOOTSTRAP
277 #define RESOLVE_MAP(sym, version, flags) \
278 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
279 #define RESOLVE(sym, version, flags) \
280 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
281 #include "dynamic-link.h"
283 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
284 HP_TIMING_NOW (start_time
);
286 /* Partly clean the `bootstrap_map' structure up. Don't use
287 `memset' since it might not be built in or inlined and we cannot
288 make function calls at this point. Use '__builtin_memset' if we
289 know it is available. We do not have to clear the memory if we
290 do not have to use the temporary bootstrap_map. Global variables
291 are initialized to zero by default. */
292 #ifndef DONT_USE_BOOTSTRAP_MAP
293 # ifdef HAVE_BUILTIN_MEMSET
294 __builtin_memset (bootstrap_map
.l_info
, '\0', sizeof (bootstrap_map
.l_info
));
297 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
299 bootstrap_map
.l_info
[cnt
] = 0;
303 /* Figure out the run-time load address of the dynamic linker itself. */
304 bootstrap_map
.l_addr
= elf_machine_load_address ();
306 /* Read our own dynamic section and fill in the info array. */
307 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
308 elf_get_dynamic_info (&bootstrap_map
);
311 # if !defined HAVE___THREAD && !defined DONT_USE_BOOTSTRAP_MAP
312 /* Signal that we have not found TLS data so far. */
313 bootstrap_map
.l_tls_modid
= 0;
316 /* Get the dynamic linkers program header. */
317 ehdr
= (ElfW(Ehdr
) *) bootstrap_map
.l_addr
;
318 phdr
= (ElfW(Phdr
) *) (bootstrap_map
.l_addr
+ ehdr
->e_phoff
);
319 for (cnt
= 0; cnt
< ehdr
->e_phnum
; ++cnt
)
320 if (phdr
[cnt
].p_type
== PT_TLS
)
323 size_t max_align
= MAX (TLS_INIT_TCB_ALIGN
, phdr
[cnt
].p_align
);
326 bootstrap_map
.l_tls_blocksize
= phdr
[cnt
].p_memsz
;
327 bootstrap_map
.l_tls_align
= phdr
[cnt
].p_align
;
328 assert (bootstrap_map
.l_tls_blocksize
!= 0);
329 bootstrap_map
.l_tls_initimage_size
= phdr
[cnt
].p_filesz
;
330 bootstrap_map
.l_tls_initimage
= (void *) (bootstrap_map
.l_addr
331 + phdr
[cnt
].p_vaddr
);
333 /* We can now allocate the initial TLS block. This can happen
334 on the stack. We'll get the final memory later when we
335 know all about the various objects loaded at startup
338 tlsblock
= alloca (roundup (bootstrap_map
.l_tls_blocksize
,
343 tlsblock
= alloca (roundup (TLS_INIT_TCB_SIZE
,
344 bootstrap_map
.l_tls_align
)
345 + bootstrap_map
.l_tls_blocksize
348 /* In case a model with a different layout for the TCB and DTV
349 is defined add another #elif here and in the following #ifs. */
350 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
352 /* Align the TLS block. */
353 tlsblock
= (void *) (((uintptr_t) tlsblock
+ max_align
- 1)
356 /* Initialize the dtv. [0] is the length, [1] the generation
358 initdtv
[0].counter
= 1;
359 initdtv
[1].counter
= 0;
361 /* Initialize the TLS block. */
363 initdtv
[2].pointer
= tlsblock
;
365 bootstrap_map
.l_tls_offset
= roundup (TLS_INIT_TCB_SIZE
,
366 bootstrap_map
.l_tls_align
);
367 initdtv
[2].pointer
= (char *) tlsblock
+ bootstrap_map
.l_tls_offset
;
369 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
371 p
= __mempcpy (initdtv
[2].pointer
, bootstrap_map
.l_tls_initimage
,
372 bootstrap_map
.l_tls_initimage_size
);
373 # ifdef HAVE_BUILTIN_MEMSET
374 __builtin_memset (p
, '\0', (bootstrap_map
.l_tls_blocksize
375 - bootstrap_map
.l_tls_initimage_size
));
378 size_t remaining
= (bootstrap_map
.l_tls_blocksize
379 - bootstrap_map
.l_tls_initimage_size
);
380 while (remaining
-- > 0)
385 /* Install the pointer to the dtv. */
387 /* Initialize the thread pointer. */
389 bootstrap_map
.l_tls_offset
390 = roundup (bootstrap_map
.l_tls_blocksize
, TLS_INIT_TCB_ALIGN
);
392 INSTALL_DTV ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
,
395 if (TLS_INIT_TP ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
, 0)
397 _dl_fatal_printf ("cannot setup thread-local storage\n");
399 INSTALL_DTV (tlsblock
, initdtv
);
400 if (TLS_INIT_TP (tlsblock
, 0) != 0)
401 _dl_fatal_printf ("cannot setup thread-local storage\n");
403 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
406 /* So far this is module number one. */
407 bootstrap_map
.l_tls_modid
= 1;
408 /* The TP got initialized. */
409 bootstrap_map
.l_tls_tp_initialized
= 1;
411 /* There can only be one PT_TLS entry. */
416 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
417 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
420 if (bootstrap_map
.l_addr
|| ! bootstrap_map
.l_info
[VALIDX(DT_GNU_PRELINKED
)])
422 /* Relocate ourselves so we can do normal function calls and
423 data access using the global offset table. */
425 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
428 /* Please note that we don't allow profiling of this object and
429 therefore need not test whether we have to allocate the array
430 for the relocation results (as done in dl-reloc.c). */
432 /* Now life is sane; we can call functions and access global data.
433 Set up to use the operating system facilities, and find out from
434 the operating system's program loader where to find the program
435 header table in core. Put the rest of _dl_start into a separate
436 function, that way the compiler cannot put accesses to the GOT
437 before ELF_DYNAMIC_RELOCATE. */
439 #ifdef DONT_USE_BOOTSTRAP_MAP
440 ElfW(Addr
) entry
= _dl_start_final (arg
);
442 ElfW(Addr
) entry
= _dl_start_final (arg
, &bootstrap_map
);
445 #ifndef ELF_MACHINE_START_ADDRESS
446 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
449 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded
), entry
);
455 /* Now life is peachy; we can do all normal operations.
456 On to the real work. */
458 /* Some helper functions. */
460 /* Arguments to relocate_doit. */
469 /* Argument to map_doit. */
471 /* Return value of map_doit. */
472 struct link_map
*main_map
;
475 /* Arguments to version_check_doit. */
476 struct version_check_args
483 relocate_doit (void *a
)
485 struct relocate_args
*args
= (struct relocate_args
*) a
;
487 INTUSE(_dl_relocate_object
) (args
->l
, args
->l
->l_scope
, args
->lazy
, 0);
493 struct map_args
*args
= (struct map_args
*) a
;
494 args
->main_map
= INTUSE(_dl_map_object
) (NULL
, args
->str
, 0, lt_library
, 0, 0);
498 version_check_doit (void *a
)
500 struct version_check_args
*args
= (struct version_check_args
*) a
;
501 if (_dl_check_all_versions (GL(dl_loaded
), 1, args
->dotrace
) && args
->doexit
)
502 /* We cannot start the application. Abort now. */
507 static inline struct link_map
*
508 find_needed (const char *name
)
510 unsigned int n
= GL(dl_loaded
)->l_searchlist
.r_nlist
;
513 if (_dl_name_match_p (name
, GL(dl_loaded
)->l_searchlist
.r_list
[n
]))
514 return GL(dl_loaded
)->l_searchlist
.r_list
[n
];
516 /* Should never happen. */
521 match_version (const char *string
, struct link_map
*map
)
523 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
526 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
527 if (map
->l_info
[VERDEFTAG
] == NULL
)
528 /* The file has no symbol versioning. */
531 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
532 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
535 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
537 /* Compare the version strings. */
538 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
542 /* If no more definitions we failed to find what we want. */
543 if (def
->vd_next
== 0)
546 /* Next definition. */
547 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
553 static const char *library_path
; /* The library search path. */
554 static const char *preloadlist
; /* The list preloaded objects. */
555 static int version_info
; /* Nonzero if information about
556 versions has to be printed. */
559 dl_main (const ElfW(Phdr
) *phdr
,
561 ElfW(Addr
) *user_entry
)
563 const ElfW(Phdr
) *ph
;
565 struct link_map
**preloads
;
566 unsigned int npreloads
;
569 bool has_interp
= false;
571 bool prelinked
= false;
572 bool rtld_is_main
= false;
573 #ifndef HP_TIMING_NONAVAIL
582 /* Process the environment variable which control the behaviour. */
583 process_envvars (&mode
);
585 /* Set up a flag which tells we are just starting. */
586 INTUSE(_dl_starting_up
) = 1;
588 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
590 /* Ho ho. We are not the program interpreter! We are the program
591 itself! This means someone ran ld.so as a command. Well, that
592 might be convenient to do sometimes. We support it by
593 interpreting the args like this:
595 ld.so PROGRAM ARGS...
597 The first argument is the name of a file containing an ELF
598 executable we will load and run with the following arguments.
599 To simplify life here, PROGRAM is searched for using the
600 normal rules for shared objects, rather than $PATH or anything
601 like that. We just load it and use its entry point; we don't
602 pay attention to its PT_INTERP command (we are the interpreter
603 ourselves). This is an easy way to test a new ld.so before
607 /* Note the place where the dynamic linker actually came from. */
608 GL(dl_rtld_map
).l_name
= rtld_progname
;
611 if (! strcmp (INTUSE(_dl_argv
)[1], "--list"))
614 GL(dl_lazy
) = -1; /* This means do no dependency analysis. */
620 else if (! strcmp (INTUSE(_dl_argv
)[1], "--verify"))
628 else if (! strcmp (INTUSE(_dl_argv
)[1], "--library-path")
631 library_path
= INTUSE(_dl_argv
)[2];
635 INTUSE(_dl_argv
) += 2;
637 else if (! strcmp (INTUSE(_dl_argv
)[1], "--inhibit-rpath")
640 GL(dl_inhibit_rpath
) = INTUSE(_dl_argv
)[2];
644 INTUSE(_dl_argv
) += 2;
649 /* If we have no further argument the program was called incorrectly.
650 Grant the user some education. */
653 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
654 You have invoked `ld.so', the helper program for shared library executables.\n\
655 This program usually lives in the file `/lib/ld.so', and special directives\n\
656 in executable files using ELF shared libraries tell the system's program\n\
657 loader to load the helper program from this file. This helper program loads\n\
658 the shared libraries needed by the program executable, prepares the program\n\
659 to run, and runs it. You may invoke this helper program directly from the\n\
660 command line to load and run an ELF executable file; this is like executing\n\
661 that file itself, but always uses this helper program from the file you\n\
662 specified, instead of the helper program file specified in the executable\n\
663 file you run. This is mostly of use for maintainers to test new versions\n\
664 of this helper program; chances are you did not intend to run this program.\n\
666 --list list all dependencies and how they are resolved\n\
667 --verify verify that given object really is a dynamically linked\n\
668 object we can handle\n\
669 --library-path PATH use given PATH instead of content of the environment\n\
670 variable LD_LIBRARY_PATH\n\
671 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
678 /* Initialize the data structures for the search paths for shared
680 _dl_init_paths (library_path
);
682 if (__builtin_expect (mode
, normal
) == verify
)
685 const char *err_str
= NULL
;
686 struct map_args args
;
688 args
.str
= rtld_progname
;
689 (void) INTUSE(_dl_catch_error
) (&objname
, &err_str
, map_doit
, &args
);
690 if (__builtin_expect (err_str
!= NULL
, 0))
691 /* We don't free the returned string, the programs stops
693 _exit (EXIT_FAILURE
);
697 HP_TIMING_NOW (start
);
698 INTUSE(_dl_map_object
) (NULL
, rtld_progname
, 0, lt_library
, 0, 0);
699 HP_TIMING_NOW (stop
);
701 HP_TIMING_DIFF (load_time
, start
, stop
);
704 phdr
= GL(dl_loaded
)->l_phdr
;
705 phnum
= GL(dl_loaded
)->l_phnum
;
706 /* We overwrite here a pointer to a malloc()ed string. But since
707 the malloc() implementation used at this point is the dummy
708 implementations which has no real free() function it does not
709 makes sense to free the old string first. */
710 GL(dl_loaded
)->l_name
= (char *) "";
711 *user_entry
= GL(dl_loaded
)->l_entry
;
715 /* Create a link_map for the executable itself.
716 This will be what dlopen on "" returns. */
717 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
718 if (GL(dl_loaded
) == NULL
)
719 _dl_fatal_printf ("cannot allocate memory for link map\n");
720 GL(dl_loaded
)->l_phdr
= phdr
;
721 GL(dl_loaded
)->l_phnum
= phnum
;
722 GL(dl_loaded
)->l_entry
= *user_entry
;
724 /* At this point we are in a bit of trouble. We would have to
725 fill in the values for l_dev and l_ino. But in general we
726 do not know where the file is. We also do not handle AT_EXECFD
727 even if it would be passed up.
729 We leave the values here defined to 0. This is normally no
730 problem as the program code itself is normally no shared
731 object and therefore cannot be loaded dynamically. Nothing
732 prevent the use of dynamic binaries and in these situations
733 we might get problems. We might not be able to find out
734 whether the object is already loaded. But since there is no
735 easy way out and because the dynamic binary must also not
736 have an SONAME we ignore this program for now. If it becomes
737 a problem we can force people using SONAMEs. */
739 /* We delay initializing the path structure until we got the dynamic
740 information for the program. */
743 GL(dl_loaded
)->l_map_end
= 0;
744 /* Perhaps the executable has no PT_LOAD header entries at all. */
745 GL(dl_loaded
)->l_map_start
= ~0;
746 /* We opened the file, account for it. */
747 ++GL(dl_loaded
)->l_opencount
;
749 /* Scan the program header table for the dynamic section. */
750 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
754 /* Find out the load address. */
755 GL(dl_loaded
)->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
758 /* This tells us where to find the dynamic section,
759 which tells us everything we need to do. */
760 GL(dl_loaded
)->l_ld
= (void *) GL(dl_loaded
)->l_addr
+ ph
->p_vaddr
;
763 /* This "interpreter segment" was used by the program loader to
764 find the program interpreter, which is this program itself, the
765 dynamic linker. We note what name finds us, so that a future
766 dlopen call or DT_NEEDED entry, for something that wants to link
767 against the dynamic linker as a shared library, will know that
768 the shared object is already loaded. */
769 _dl_rtld_libname
.name
= ((const char *) GL(dl_loaded
)->l_addr
771 /* _dl_rtld_libname.next = NULL; Already zero. */
772 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
774 /* Ordinarilly, we would get additional names for the loader from
775 our DT_SONAME. This can't happen if we were actually linked as
776 a static executable (detect this case when we have no DYNAMIC).
777 If so, assume the filename component of the interpreter path to
778 be our SONAME, and add it to our name list. */
779 if (GL(dl_rtld_map
).l_ld
== NULL
)
781 const char *p
= NULL
;
782 const char *cp
= _dl_rtld_libname
.name
;
784 /* Find the filename part of the path. */
791 _dl_rtld_libname2
.name
= p
;
792 /* _dl_rtld_libname2.next = NULL; Already zero. */
793 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
804 /* Remember where the main program starts in memory. */
805 mapstart
= (GL(dl_loaded
)->l_addr
806 + (ph
->p_vaddr
& ~(ph
->p_align
- 1)));
807 if (GL(dl_loaded
)->l_map_start
> mapstart
)
808 GL(dl_loaded
)->l_map_start
= mapstart
;
810 /* Also where it ends. */
811 allocend
= GL(dl_loaded
)->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
812 if (GL(dl_loaded
)->l_map_end
< allocend
)
813 GL(dl_loaded
)->l_map_end
= allocend
;
820 /* Note that in the case the dynamic linker we duplicate work
821 here since we read the PT_TLS entry already in
822 _dl_start_final. But the result is repeatable so do not
823 check for this special but unimportant case. */
824 GL(dl_loaded
)->l_tls_blocksize
= ph
->p_memsz
;
825 GL(dl_loaded
)->l_tls_align
= ph
->p_align
;
826 GL(dl_loaded
)->l_tls_initimage_size
= ph
->p_filesz
;
827 GL(dl_loaded
)->l_tls_initimage
= (void *) ph
->p_vaddr
;
829 /* This image gets the ID one. */
830 GL(dl_tls_max_dtv_idx
) = GL(dl_loaded
)->l_tls_modid
= 1;
835 if (! GL(dl_loaded
)->l_map_end
)
836 GL(dl_loaded
)->l_map_end
= ~0;
837 if (! GL(dl_rtld_map
).l_libname
&& GL(dl_rtld_map
).l_name
)
839 /* We were invoked directly, so the program might not have a
841 _dl_rtld_libname
.name
= GL(dl_rtld_map
).l_name
;
842 /* _dl_rtld_libname.next = NULL; Already zero. */
843 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
846 assert (GL(dl_rtld_map
).l_libname
); /* How else did we get here? */
850 /* Extract the contents of the dynamic section for easy access. */
851 elf_get_dynamic_info (GL(dl_loaded
));
852 if (GL(dl_loaded
)->l_info
[DT_HASH
])
853 /* Set up our cache of pointers into the hash table. */
854 _dl_setup_hash (GL(dl_loaded
));
857 if (__builtin_expect (mode
, normal
) == verify
)
859 /* We were called just to verify that this is a dynamic
860 executable using us as the program interpreter. Exit with an
861 error if we were not able to load the binary or no interpreter
862 is specified (i.e., this is no dynamically linked binary. */
863 if (GL(dl_loaded
)->l_ld
== NULL
)
866 /* We allow here some platform specific code. */
867 #ifdef DISTINGUISH_LIB_VERSIONS
868 DISTINGUISH_LIB_VERSIONS
;
870 _exit (has_interp
? 0 : 2);
874 /* Initialize the data structures for the search paths for shared
876 _dl_init_paths (library_path
);
878 /* Put the link_map for ourselves on the chain so it can be found by
879 name. Note that at this point the global chain of link maps contains
880 exactly one element, which is pointed to by dl_loaded. */
881 if (! GL(dl_rtld_map
).l_name
)
882 /* If not invoked directly, the dynamic linker shared object file was
883 found by the PT_INTERP name. */
884 GL(dl_rtld_map
).l_name
= (char *) GL(dl_rtld_map
).l_libname
->name
;
885 GL(dl_rtld_map
).l_type
= lt_library
;
886 GL(dl_loaded
)->l_next
= &GL(dl_rtld_map
);
887 GL(dl_rtld_map
).l_prev
= GL(dl_loaded
);
890 /* We have two ways to specify objects to preload: via environment
891 variable and via the file /etc/ld.so.preload. The latter can also
892 be used when security is enabled. */
896 if (__builtin_expect (preloadlist
!= NULL
, 0))
898 /* The LD_PRELOAD environment variable gives list of libraries
899 separated by white space or colons that are loaded before the
900 executable's dependencies and prepended to the global scope
901 list. If the binary is running setuid all elements
902 containing a '/' are ignored since it is insecure. */
903 char *list
= strdupa (preloadlist
);
906 HP_TIMING_NOW (start
);
908 /* Prevent optimizing strsep. Speed is not important here. */
909 while ((p
= (strsep
) (&list
, " :")) != NULL
)
911 && (__builtin_expect (! INTUSE(__libc_enable_secure
), 1)
912 || strchr (p
, '/') == NULL
))
914 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
),
918 if (++new_map
->l_opencount
== 1)
919 /* It is no duplicate. */
923 HP_TIMING_NOW (stop
);
924 HP_TIMING_DIFF (diff
, start
, stop
);
925 HP_TIMING_ACCUM_NT (load_time
, diff
);
928 /* Read the contents of the file. */
929 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
930 PROT_READ
| PROT_WRITE
);
931 if (__builtin_expect (file
!= MAP_FAILED
, 0))
933 /* Parse the file. It contains names of libraries to be loaded,
934 separated by white spaces or `:'. It may also contain
935 comments introduced by `#'. */
940 /* Eliminate comments. */
945 char *comment
= memchr (runp
, '#', rest
);
949 rest
-= comment
- runp
;
952 while (--rest
> 0 && *++comment
!= '\n');
955 /* We have one problematic case: if we have a name at the end of
956 the file without a trailing terminating characters, we cannot
957 place the \0. Handle the case separately. */
958 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
959 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
961 problem
= &file
[file_size
];
962 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
963 && problem
[-1] != '\n' && problem
[-1] != ':')
972 file
[file_size
- 1] = '\0';
975 HP_TIMING_NOW (start
);
981 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
984 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
),
988 if (++new_map
->l_opencount
== 1)
989 /* It is no duplicate. */
996 char *p
= strndupa (problem
, file_size
- (problem
- file
));
997 struct link_map
*new_map
= INTUSE(_dl_map_object
) (GL(dl_loaded
), p
,
1000 if (++new_map
->l_opencount
== 1)
1001 /* It is no duplicate. */
1005 HP_TIMING_NOW (stop
);
1006 HP_TIMING_DIFF (diff
, start
, stop
);
1007 HP_TIMING_ACCUM_NT (load_time
, diff
);
1009 /* We don't need the file anymore. */
1010 __munmap (file
, file_size
);
1013 if (__builtin_expect (npreloads
, 0) != 0)
1015 /* Set up PRELOADS with a vector of the preloaded libraries. */
1017 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
1018 l
= GL(dl_rtld_map
).l_next
; /* End of the chain before preloads. */
1025 assert (i
== npreloads
);
1028 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1029 specified some libraries to load, these are inserted before the actual
1030 dependencies in the executable's searchlist for symbol resolution. */
1031 HP_TIMING_NOW (start
);
1032 INTUSE(_dl_map_object_deps
) (GL(dl_loaded
), preloads
, npreloads
,
1034 HP_TIMING_NOW (stop
);
1035 HP_TIMING_DIFF (diff
, start
, stop
);
1036 HP_TIMING_ACCUM_NT (load_time
, diff
);
1038 /* Mark all objects as being in the global scope and set the open
1040 for (i
= GL(dl_loaded
)->l_searchlist
.r_nlist
; i
> 0; )
1043 GL(dl_loaded
)->l_searchlist
.r_list
[i
]->l_global
= 1;
1044 ++GL(dl_loaded
)->l_searchlist
.r_list
[i
]->l_opencount
;
1048 /* We are done mapping things, so close the zero-fill descriptor. */
1049 __close (_dl_zerofd
);
1053 /* Remove _dl_rtld_map from the chain. */
1054 GL(dl_rtld_map
).l_prev
->l_next
= GL(dl_rtld_map
).l_next
;
1055 if (GL(dl_rtld_map
).l_next
)
1056 GL(dl_rtld_map
).l_next
->l_prev
= GL(dl_rtld_map
).l_prev
;
1058 if (__builtin_expect (GL(dl_rtld_map
).l_opencount
> 1, 1))
1060 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1061 put it back in the list of visible objects. We insert it into the
1062 chain in symbol search order because gdb uses the chain's order as
1063 its symbol search order. */
1065 while (GL(dl_loaded
)->l_searchlist
.r_list
[i
] != &GL(dl_rtld_map
))
1067 GL(dl_rtld_map
).l_prev
= GL(dl_loaded
)->l_searchlist
.r_list
[i
- 1];
1068 if (__builtin_expect (mode
, normal
) == normal
)
1069 GL(dl_rtld_map
).l_next
= (i
+ 1 < GL(dl_loaded
)->l_searchlist
.r_nlist
1070 ? GL(dl_loaded
)->l_searchlist
.r_list
[i
+ 1]
1073 /* In trace mode there might be an invisible object (which we
1074 could not find) after the previous one in the search list.
1075 In this case it doesn't matter much where we put the
1076 interpreter object, so we just initialize the list pointer so
1077 that the assertion below holds. */
1078 GL(dl_rtld_map
).l_next
= GL(dl_rtld_map
).l_prev
->l_next
;
1080 assert (GL(dl_rtld_map
).l_prev
->l_next
== GL(dl_rtld_map
).l_next
);
1081 GL(dl_rtld_map
).l_prev
->l_next
= &GL(dl_rtld_map
);
1082 if (GL(dl_rtld_map
).l_next
!= NULL
)
1084 assert (GL(dl_rtld_map
).l_next
->l_prev
== GL(dl_rtld_map
).l_prev
);
1085 GL(dl_rtld_map
).l_next
->l_prev
= &GL(dl_rtld_map
);
1089 /* Now let us see whether all libraries are available in the
1090 versions we need. */
1092 struct version_check_args args
;
1093 args
.doexit
= mode
== normal
;
1094 args
.dotrace
= mode
== trace
;
1095 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
1099 /* Now it is time to determine the layout of the static TLS block
1100 and allocate it for the initial thread. Note that we always
1101 allocate the static block, we never defer it even if no
1102 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1103 use the static model. First add the dynamic linker to the list
1104 if it also uses TLS. */
1105 if (GL(dl_rtld_map
).l_tls_blocksize
!= 0)
1106 /* Assign a module ID. */
1107 GL(dl_rtld_map
).l_tls_modid
= _dl_next_tls_modid ();
1110 /* If dynamic loading of modules with TLS is impossible we do not
1111 have to initialize any of the TLS functionality unless any of the
1112 initial modules uses TLS. */
1113 if (GL(dl_tls_max_dtv_idx
) > 0)
1118 struct dtv_slotinfo
*slotinfo
;
1120 /* Number of elements in the static TLS block. */
1121 GL(dl_tls_static_nelem
) = GL(dl_tls_max_dtv_idx
);
1123 /* Allocate the array which contains the information about the
1124 dtv slots. We allocate a few entries more than needed to
1125 avoid the need for reallocation. */
1126 nelem
= GL(dl_tls_max_dtv_idx
) + 1 + TLS_SLOTINFO_SURPLUS
;
1129 GL(dl_tls_dtv_slotinfo_list
) = (struct dtv_slotinfo_list
*)
1130 malloc (sizeof (struct dtv_slotinfo_list
)
1131 + nelem
* sizeof (struct dtv_slotinfo
));
1132 /* No need to check the return value. If memory allocation failed
1133 the program would have been terminated. */
1135 slotinfo
= memset (GL(dl_tls_dtv_slotinfo_list
)->slotinfo
, '\0',
1136 nelem
* sizeof (struct dtv_slotinfo
));
1137 GL(dl_tls_dtv_slotinfo_list
)->len
= nelem
;
1138 GL(dl_tls_dtv_slotinfo_list
)->next
= NULL
;
1140 /* Fill in the information from the loaded modules. */
1141 for (l
= GL(dl_loaded
), i
= 0; l
!= NULL
; l
= l
->l_next
)
1142 if (l
->l_tls_blocksize
!= 0)
1143 /* This is a module with TLS data. Store the map reference.
1144 The generation counter is zero. */
1145 slotinfo
[++i
].map
= l
;
1146 assert (i
== GL(dl_tls_max_dtv_idx
));
1148 /* Compute the TLS offsets for the various blocks. We call this
1149 function even if none of the modules available at startup time
1150 uses TLS to initialize some variables. */
1151 _dl_determine_tlsoffset ();
1153 /* Construct the static TLS block and the dtv for the initial
1154 thread. For some platforms this will include allocating memory
1155 for the thread descriptor. The memory for the TLS block will
1156 never be freed. It should be allocated accordingly. The dtv
1157 array can be changed if dynamic loading requires it. */
1158 tcbp
= _dl_allocate_tls_storage ();
1160 _dl_fatal_printf ("\
1161 cannot allocate TLS data structures for initial thread");
1163 /* Store for detection of the special case by __tls_get_addr
1164 so it knows not to pass this dtv to the normal realloc. */
1165 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
1169 if (__builtin_expect (mode
, normal
) != normal
)
1171 /* We were run just to list the shared libraries. It is
1172 important that we do this before real relocation, because the
1173 functions we call below for output may no longer work properly
1174 after relocation. */
1175 if (! GL(dl_loaded
)->l_info
[DT_NEEDED
])
1176 _dl_printf ("\tstatically linked\n");
1181 if (GL(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1183 struct r_scope_elem
*scope
= &GL(dl_loaded
)->l_searchlist
;
1185 for (i
= 0; i
< scope
->r_nlist
; i
++)
1187 l
= scope
->r_list
[i
];
1190 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1193 if (_dl_name_match_p (GL(dl_trace_prelink
), l
))
1194 GL(dl_trace_prelink_map
) = l
;
1195 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1196 l
->l_libname
->name
[0] ? l
->l_libname
->name
1197 : rtld_progname
?: "<main program>",
1198 l
->l_name
[0] ? l
->l_name
1199 : rtld_progname
?: "<main program>",
1200 (int) sizeof l
->l_map_start
* 2,
1202 (int) sizeof l
->l_addr
* 2,
1206 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l
->l_tls_modid
,
1207 (int) sizeof l
->l_tls_offset
* 2,
1216 for (l
= GL(dl_loaded
)->l_next
; l
; l
= l
->l_next
)
1218 /* The library was not found. */
1219 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1221 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l
->l_libname
->name
,
1222 l
->l_name
, (int) sizeof l
->l_map_start
* 2,
1227 if (__builtin_expect (mode
, trace
) != trace
)
1228 for (i
= 1; i
< (unsigned int) _dl_argc
; ++i
)
1230 const ElfW(Sym
) *ref
= NULL
;
1231 ElfW(Addr
) loadbase
;
1234 result
= INTUSE(_dl_lookup_symbol
) (INTUSE(_dl_argv
)[i
],
1236 &ref
, GL(dl_loaded
)->l_scope
,
1237 ELF_RTYPE_CLASS_PLT
, 1);
1239 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
1241 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1242 INTUSE(_dl_argv
)[i
],
1243 (int) sizeof ref
->st_value
* 2, ref
->st_value
,
1244 (int) sizeof loadbase
* 2, loadbase
);
1248 /* If LD_WARN is set warn about undefined symbols. */
1249 if (GL(dl_lazy
) >= 0 && GL(dl_verbose
))
1251 /* We have to do symbol dependency testing. */
1252 struct relocate_args args
;
1255 args
.lazy
= GL(dl_lazy
);
1262 if (l
!= &GL(dl_rtld_map
) && ! l
->l_faked
)
1265 _dl_receive_error (print_unresolved
, relocate_doit
,
1271 if ((GL(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1272 && GL(dl_rtld_map
).l_opencount
> 1)
1273 INTUSE(_dl_relocate_object
) (&GL(dl_rtld_map
),
1274 GL(dl_loaded
)->l_scope
, 0, 0);
1277 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1280 /* Print more information. This means here, print information
1281 about the versions needed. */
1283 struct link_map
*map
= GL(dl_loaded
);
1285 for (map
= GL(dl_loaded
); map
!= NULL
; map
= map
->l_next
)
1288 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
1294 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
1295 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
1299 _dl_printf ("\n\tVersion information:\n");
1303 _dl_printf ("\t%s:\n",
1304 map
->l_name
[0] ? map
->l_name
: rtld_progname
);
1309 struct link_map
*needed
;
1311 needed
= find_needed (strtab
+ ent
->vn_file
);
1312 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
1316 const char *fname
= NULL
;
1319 && match_version (strtab
+ aux
->vna_name
,
1321 fname
= needed
->l_name
;
1323 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1324 strtab
+ ent
->vn_file
,
1325 strtab
+ aux
->vna_name
,
1326 aux
->vna_flags
& VER_FLG_WEAK
1328 fname
?: "not found");
1330 if (aux
->vna_next
== 0)
1331 /* No more symbols. */
1335 aux
= (ElfW(Vernaux
) *) ((char *) aux
1339 if (ent
->vn_next
== 0)
1340 /* No more dependencies. */
1343 /* Next dependency. */
1344 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1353 if (GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]
1354 && ! __builtin_expect (GL(dl_profile
) != NULL
, 0))
1356 ElfW(Lib
) *liblist
, *liblistend
;
1357 struct link_map
**r_list
, **r_listend
, *l
;
1358 const char *strtab
= (const void *) D_PTR (GL(dl_loaded
),
1361 assert (GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
);
1362 liblist
= (ElfW(Lib
) *)
1363 GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]->d_un
.d_ptr
;
1364 liblistend
= (ElfW(Lib
) *)
1366 + GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)]->d_un
.d_val
);
1367 r_list
= GL(dl_loaded
)->l_searchlist
.r_list
;
1368 r_listend
= r_list
+ GL(dl_loaded
)->l_searchlist
.r_nlist
;
1370 for (; r_list
< r_listend
&& liblist
< liblistend
; r_list
++)
1374 if (l
== GL(dl_loaded
))
1377 /* If the library is not mapped where it should, fail. */
1381 /* Next, check if checksum matches. */
1382 if (l
->l_info
[VALIDX(DT_CHECKSUM
)] == NULL
1383 || l
->l_info
[VALIDX(DT_CHECKSUM
)]->d_un
.d_val
1384 != liblist
->l_checksum
)
1387 if (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] == NULL
1388 || l
->l_info
[VALIDX(DT_GNU_PRELINKED
)]->d_un
.d_val
1389 != liblist
->l_time_stamp
)
1392 if (! _dl_name_match_p (strtab
+ liblist
->l_name
, l
))
1399 if (r_list
== r_listend
&& liblist
== liblistend
)
1402 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1403 _dl_printf ("\nprelink checking: %s\n", prelinked
? "ok" : "failed");
1408 if (GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_CONFLICT
)] != NULL
)
1410 ElfW(Rela
) *conflict
, *conflictend
;
1411 #ifndef HP_TIMING_NONAVAIL
1416 HP_TIMING_NOW (start
);
1417 assert (GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)] != NULL
);
1418 conflict
= (ElfW(Rela
) *)
1419 GL(dl_loaded
)->l_info
[ADDRIDX (DT_GNU_CONFLICT
)]->d_un
.d_ptr
;
1420 conflictend
= (ElfW(Rela
) *)
1422 + GL(dl_loaded
)->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)]->d_un
.d_val
);
1423 _dl_resolve_conflicts (GL(dl_loaded
), conflict
, conflictend
);
1424 HP_TIMING_NOW (stop
);
1425 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1428 _dl_sysdep_start_cleanup ();
1432 /* Now we have all the objects loaded. Relocate them all except for
1433 the dynamic linker itself. We do this in reverse order so that copy
1434 relocs of earlier objects overwrite the data written by later
1435 objects. We do not re-relocate the dynamic linker itself in this
1436 loop because that could result in the GOT entries for functions we
1437 call being changed, and that would break us. It is safe to relocate
1438 the dynamic linker out of order because it has no copy relocs (we
1439 know that because it is self-contained). */
1442 int consider_profiling
= GL(dl_profile
) != NULL
;
1443 #ifndef HP_TIMING_NONAVAIL
1449 /* If we are profiling we also must do lazy reloaction. */
1450 GL(dl_lazy
) |= consider_profiling
;
1456 HP_TIMING_NOW (start
);
1459 /* While we are at it, help the memory handling a bit. We have to
1460 mark some data structures as allocated with the fake malloc()
1461 implementation in ld.so. */
1462 struct libname_list
*lnp
= l
->l_libname
->next
;
1464 while (__builtin_expect (lnp
!= NULL
, 0))
1470 if (l
!= &GL(dl_rtld_map
))
1471 INTUSE(_dl_relocate_object
) (l
, l
->l_scope
, GL(dl_lazy
),
1472 consider_profiling
);
1477 HP_TIMING_NOW (stop
);
1479 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1481 /* Do any necessary cleanups for the startup OS interface code.
1482 We do these now so that no calls are made after rtld re-relocation
1483 which might be resolved to different functions than we expect.
1484 We cannot do this before relocating the other objects because
1485 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1486 _dl_sysdep_start_cleanup ();
1488 /* Now enable profiling if needed. Like the previous call,
1489 this has to go here because the calls it makes should use the
1490 rtld versions of the functions (particularly calloc()), but it
1491 needs to have _dl_profile_map set up by the relocator. */
1492 if (__builtin_expect (GL(dl_profile_map
) != NULL
, 0))
1493 /* We must prepare the profiling. */
1494 INTUSE(_dl_start_profile
) (GL(dl_profile_map
), GL(dl_profile_output
));
1496 if (GL(dl_rtld_map
).l_opencount
> 1)
1498 /* There was an explicit ref to the dynamic linker as a shared lib.
1499 Re-relocate ourselves with user-controlled symbol definitions. */
1500 HP_TIMING_NOW (start
);
1501 INTUSE(_dl_relocate_object
) (&GL(dl_rtld_map
), GL(dl_loaded
)->l_scope
,
1503 HP_TIMING_NOW (stop
);
1504 HP_TIMING_DIFF (add
, start
, stop
);
1505 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1509 /* Now set up the variable which helps the assembler startup code. */
1510 GL(dl_main_searchlist
) = &GL(dl_loaded
)->l_searchlist
;
1511 GL(dl_global_scope
)[0] = &GL(dl_loaded
)->l_searchlist
;
1513 /* Save the information about the original global scope list since
1514 we need it in the memory handling later. */
1515 GL(dl_initial_searchlist
) = *GL(dl_main_searchlist
);
1519 if (GL(dl_tls_max_dtv_idx
) > 0)
1522 /* Now that we have completed relocation, the initializer data
1523 for the TLS blocks has its final values and we can copy them
1524 into the main thread's TLS area, which we allocated above. */
1525 _dl_allocate_tls_init (tcbp
);
1527 /* And finally install it for the main thread. */
1528 # ifndef HAVE___THREAD
1529 TLS_INIT_TP (tcbp
, GL(dl_rtld_map
).l_tls_tp_initialized
);
1531 /* If the compiler supports the __thread keyword we know that
1532 at least ld.so itself uses TLS and therefore the thread
1533 pointer was initialized earlier. */
1534 assert (GL(dl_rtld_map
).l_tls_tp_initialized
!= 0);
1535 TLS_INIT_TP (tcbp
, 1);
1541 /* Initialize _r_debug. */
1542 struct r_debug
*r
= _dl_debug_initialize (GL(dl_rtld_map
).l_addr
);
1547 #ifdef ELF_MACHINE_DEBUG_SETUP
1549 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1551 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1552 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map
), r
);
1556 if (l
->l_info
[DT_DEBUG
] != NULL
)
1557 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1558 with the run-time address of the r_debug structure */
1559 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1561 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1562 case you run gdb on the dynamic linker directly. */
1563 if (GL(dl_rtld_map
).l_info
[DT_DEBUG
] != NULL
)
1564 GL(dl_rtld_map
).l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1568 /* Notify the debugger that all objects are now mapped in. */
1569 r
->r_state
= RT_ADD
;
1570 INTUSE(_dl_debug_state
) ();
1574 /* We must munmap() the cache file. */
1575 INTUSE(_dl_unload_cache
) ();
1578 /* Once we return, _dl_sysdep_start will invoke
1579 the DT_INIT functions and then *USER_ENTRY. */
1582 /* This is a little helper function for resolving symbols while
1583 tracing the binary. */
1585 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1586 const char *errstring
)
1588 if (objname
[0] == '\0')
1589 objname
= rtld_progname
?: "<main program>";
1590 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
1593 /* This is a little helper function for resolving symbols while
1594 tracing the binary. */
1596 print_missing_version (int errcode
__attribute__ ((unused
)),
1597 const char *objname
, const char *errstring
)
1599 _dl_error_printf ("%s: %s: %s\n", rtld_progname
?: "<program name unknown>",
1600 objname
, errstring
);
1603 /* Nonzero if any of the debugging options is enabled. */
1604 static int any_debug
;
1606 /* Process the string given as the parameter which explains which debugging
1607 options are enabled. */
1609 process_dl_debug (const char *dl_debug
)
1611 /* When adding new entries make sure that the maximal length of a name
1612 is correctly handled in the LD_DEBUG_HELP code below. */
1616 const char name
[10];
1617 const char helptext
[41];
1618 unsigned short int mask
;
1621 #define LEN_AND_STR(str) sizeof (str) - 1, str
1622 { LEN_AND_STR ("libs"), "display library search paths",
1623 DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
},
1624 { LEN_AND_STR ("reloc"), "display relocation processing",
1625 DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
},
1626 { LEN_AND_STR ("files"), "display progress for input file",
1627 DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
},
1628 { LEN_AND_STR ("symbols"), "display symbol table processing",
1629 DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
},
1630 { LEN_AND_STR ("bindings"), "display information about symbol binding",
1631 DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
},
1632 { LEN_AND_STR ("versions"), "display version dependencies",
1633 DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
1634 { LEN_AND_STR ("all"), "all previous options combined",
1635 DL_DEBUG_LIBS
| DL_DEBUG_RELOC
| DL_DEBUG_FILES
| DL_DEBUG_SYMBOLS
1636 | DL_DEBUG_BINDINGS
| DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
1637 { LEN_AND_STR ("statistics"), "display relocation statistics",
1638 DL_DEBUG_STATISTICS
},
1639 { LEN_AND_STR ("help"), "display this help message and exit",
1642 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1644 /* Skip separating white spaces and commas. */
1645 while (*dl_debug
!= '\0')
1647 if (*dl_debug
!= ' ' && *dl_debug
!= ',' && *dl_debug
!= ':')
1652 while (dl_debug
[len
] != '\0' && dl_debug
[len
] != ' '
1653 && dl_debug
[len
] != ',' && dl_debug
[len
] != ':')
1656 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
1657 if (debopts
[cnt
].len
== len
1658 && memcmp (dl_debug
, debopts
[cnt
].name
, len
) == 0)
1660 GL(dl_debug_mask
) |= debopts
[cnt
].mask
;
1665 if (cnt
== ndebopts
)
1667 /* Display a warning and skip everything until next
1669 char *copy
= strndupa (dl_debug
, len
);
1670 _dl_error_printf ("\
1671 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy
);
1681 if (GL(dl_debug_mask
) & DL_DEBUG_HELP
)
1686 Valid options for the LD_DEBUG environment variable are:\n\n");
1688 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
1689 _dl_printf (" %.*s%s%s\n", debopts
[cnt
].len
, debopts
[cnt
].name
,
1690 " " + debopts
[cnt
].len
- 3,
1691 debopts
[cnt
].helptext
);
1694 To direct the debugging output into a file instead of standard output\n\
1695 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1700 /* Process all environments variables the dynamic linker must recognize.
1701 Since all of them start with `LD_' we are a bit smarter while finding
1703 extern char **_environ attribute_hidden
;
1707 process_envvars (enum mode
*modep
)
1709 char **runp
= _environ
;
1711 enum mode mode
= normal
;
1712 char *debug_output
= NULL
;
1714 /* This is the default place for profiling data file. */
1715 GL(dl_profile_output
)
1716 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure
) ? 9 : 0];
1718 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1722 while (envline
[len
] != '\0' && envline
[len
] != '=')
1725 if (envline
[len
] != '=')
1726 /* This is a "LD_" variable at the end of the string without
1727 a '=' character. Ignore it since otherwise we will access
1728 invalid memory below. */
1734 /* Warning level, verbose or not. */
1735 if (memcmp (envline
, "WARN", 4) == 0)
1736 GL(dl_verbose
) = envline
[5] != '\0';
1740 /* Debugging of the dynamic linker? */
1741 if (memcmp (envline
, "DEBUG", 5) == 0)
1742 process_dl_debug (&envline
[6]);
1746 /* Print information about versions. */
1747 if (memcmp (envline
, "VERBOSE", 7) == 0)
1749 version_info
= envline
[8] != '\0';
1753 /* List of objects to be preloaded. */
1754 if (memcmp (envline
, "PRELOAD", 7) == 0)
1756 preloadlist
= &envline
[8];
1760 /* Which shared object shall be profiled. */
1761 if (memcmp (envline
, "PROFILE", 7) == 0 && envline
[8] != '\0')
1762 GL(dl_profile
) = &envline
[8];
1766 /* Do we bind early? */
1767 if (memcmp (envline
, "BIND_NOW", 8) == 0)
1769 GL(dl_lazy
) = envline
[9] == '\0';
1772 if (memcmp (envline
, "BIND_NOT", 8) == 0)
1773 GL(dl_bind_not
) = envline
[9] != '\0';
1777 /* Test whether we want to see the content of the auxiliary
1778 array passed up from the kernel. */
1779 if (memcmp (envline
, "SHOW_AUXV", 9) == 0)
1784 /* Mask for the important hardware capabilities. */
1785 if (memcmp (envline
, "HWCAP_MASK", 10) == 0)
1786 GL(dl_hwcap_mask
) = __strtoul_internal (&envline
[11], NULL
, 0, 0);
1790 /* Path where the binary is found. */
1791 if (!INTUSE(__libc_enable_secure
)
1792 && memcmp (envline
, "ORIGIN_PATH", 11) == 0)
1793 GL(dl_origin_path
) = &envline
[12];
1797 /* The library search path. */
1798 if (memcmp (envline
, "LIBRARY_PATH", 12) == 0)
1800 library_path
= &envline
[13];
1804 /* Where to place the profiling data file. */
1805 if (memcmp (envline
, "DEBUG_OUTPUT", 12) == 0)
1807 debug_output
= &envline
[13];
1811 if (memcmp (envline
, "DYNAMIC_WEAK", 12) == 0)
1812 GL(dl_dynamic_weak
) = 1;
1816 /* Where to place the profiling data file. */
1817 if (!INTUSE(__libc_enable_secure
)
1818 && memcmp (envline
, "PROFILE_OUTPUT", 14) == 0
1819 && envline
[15] != '\0')
1820 GL(dl_profile_output
) = &envline
[15];
1824 /* The mode of the dynamic linker can be set. */
1825 if (memcmp (envline
, "TRACE_PRELINKING", 16) == 0)
1829 GL(dl_debug_mask
) |= DL_DEBUG_PRELINK
;
1830 GL(dl_trace_prelink
) = &envline
[17];
1835 /* The mode of the dynamic linker can be set. */
1836 if (memcmp (envline
, "TRACE_LOADED_OBJECTS", 20) == 0)
1840 /* We might have some extra environment variable to handle. This
1841 is tricky due to the pre-processing of the length of the name
1842 in the switch statement here. The code here assumes that added
1843 environment variables have a different length. */
1844 #ifdef EXTRA_LD_ENVVARS
1850 /* The caller wants this information. */
1853 /* Extra security for SUID binaries. Remove all dangerous environment
1855 if (__builtin_expect (INTUSE(__libc_enable_secure
), 0))
1857 static const char unsecure_envvars
[] =
1858 #ifdef EXTRA_UNSECURE_ENVVARS
1859 EXTRA_UNSECURE_ENVVARS
1864 nextp
= unsecure_envvars
;
1868 /* We could use rawmemchr but this need not be fast. */
1869 nextp
= (char *) (strchr
) (nextp
, '\0') + 1;
1871 while (*nextp
!= '\0');
1873 if (__access ("/etc/suid-debug", F_OK
) != 0)
1874 unsetenv ("MALLOC_CHECK_");
1876 /* If we have to run the dynamic linker in debugging mode and the
1877 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1878 messages to this file. */
1879 else if (any_debug
&& debug_output
!= NULL
)
1882 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
1884 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
1886 size_t name_len
= strlen (debug_output
);
1887 char buf
[name_len
+ 12];
1890 buf
[name_len
+ 11] = '\0';
1891 startp
= _itoa (__getpid (), &buf
[name_len
+ 11], 10, 0);
1893 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1895 GL(dl_debug_fd
) = __open (startp
, flags
, DEFFILEMODE
);
1896 if (GL(dl_debug_fd
) == -1)
1897 /* We use standard output if opening the file failed. */
1898 GL(dl_debug_fd
) = STDOUT_FILENO
;
1903 /* Print the various times we collected. */
1905 print_statistics (void)
1907 #ifndef HP_TIMING_NONAVAIL
1912 /* Total time rtld used. */
1913 if (HP_TIMING_AVAIL
)
1915 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1916 INTUSE(_dl_debug_printf
) ("\nruntime linker statistics:\n"
1917 " total startup time in dynamic loader: %s\n",
1921 /* Print relocation statistics. */
1922 if (HP_TIMING_AVAIL
)
1925 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1926 cp
= _itoa ((1000ULL * relocate_time
) / rtld_total_time
,
1927 pbuf
+ sizeof (pbuf
), 10, 0);
1929 switch (pbuf
+ sizeof (pbuf
) - cp
)
1940 INTUSE(_dl_debug_printf
) ("\
1941 time needed for relocation: %s (%s%%)\n",
1945 INTUSE(_dl_debug_printf
) (" number of relocations: %lu\n",
1946 GL(dl_num_relocations
));
1947 INTUSE(_dl_debug_printf
) (" number of relocations from cache: %lu\n",
1948 GL(dl_num_cache_relocations
));
1950 #ifndef HP_TIMING_NONAVAIL
1951 /* Time spend while loading the object and the dependencies. */
1952 if (HP_TIMING_AVAIL
)
1955 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1956 cp
= _itoa ((1000ULL * load_time
) / rtld_total_time
,
1957 pbuf
+ sizeof (pbuf
), 10, 0);
1959 switch (pbuf
+ sizeof (pbuf
) - cp
)
1970 INTUSE(_dl_debug_printf
) ("\
1971 time needed to load objects: %s (%s%%)\n",