]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/rtld.c
3d207a3fa072cb9de62b7061ef7a37e1a06a1e12
[thirdparty/glibc.git] / elf / rtld.c
1 /* Run time dynamic linker.
2 Copyright (C) 1995-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <dlfcn.h>
21 #include <fcntl.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <ldsodefs.h>
30 #include <_itoa.h>
31 #include <entry.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>
38 #include <dl-cache.h>
39 #include <dl-osinfo.h>
40 #include <dl-procinfo.h>
41 #include <tls.h>
42 #include <stap-probe.h>
43 #include <stackinfo.h>
44
45 #include <assert.h>
46
47 /* Avoid PLT use for our local calls at startup. */
48 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
49
50 /* GCC has mental blocks about _exit. */
51 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
52 #define _exit exit_internal
53
54 /* Helper function to handle errors while resolving symbols. */
55 static void print_unresolved (int errcode, const char *objname,
56 const char *errsting);
57
58 /* Helper function to handle errors when a version is missing. */
59 static void print_missing_version (int errcode, const char *objname,
60 const char *errsting);
61
62 /* Print the various times we collected. */
63 static void print_statistics (hp_timing_t *total_timep);
64
65 /* Add audit objects. */
66 static void process_dl_audit (char *str);
67
68 /* This is a list of all the modes the dynamic loader can be in. */
69 enum mode { normal, list, verify, trace };
70
71 /* Process all environments variables the dynamic linker must recognize.
72 Since all of them start with `LD_' we are a bit smarter while finding
73 all the entries. */
74 static void process_envvars (enum mode *modep);
75
76 #ifdef DL_ARGV_NOT_RELRO
77 int _dl_argc attribute_hidden;
78 char **_dl_argv = NULL;
79 /* Nonzero if we were run directly. */
80 unsigned int _dl_skip_args attribute_hidden;
81 #else
82 int _dl_argc attribute_relro attribute_hidden;
83 char **_dl_argv attribute_relro = NULL;
84 unsigned int _dl_skip_args attribute_relro attribute_hidden;
85 #endif
86 INTDEF(_dl_argv)
87
88 #ifndef THREAD_SET_STACK_GUARD
89 /* Only exported for architectures that don't store the stack guard canary
90 in thread local area. */
91 uintptr_t __stack_chk_guard attribute_relro;
92 #endif
93
94 /* Only exported for architectures that don't store the pointer guard
95 value in thread local area. */
96 uintptr_t __pointer_chk_guard_local
97 attribute_relro attribute_hidden __attribute__ ((nocommon));
98 #ifndef THREAD_SET_POINTER_GUARD
99 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
100 #endif
101
102
103 /* List of auditing DSOs. */
104 static struct audit_list
105 {
106 const char *name;
107 struct audit_list *next;
108 } *audit_list;
109
110 #ifndef HAVE_INLINED_SYSCALLS
111 /* Set nonzero during loading and initialization of executable and
112 libraries, cleared before the executable's entry point runs. This
113 must not be initialized to nonzero, because the unused dynamic
114 linker loaded in for libc.so's "ld.so.1" dep will provide the
115 definition seen by libc.so's initializer; that value must be zero,
116 and will be since that dynamic linker's _dl_start and dl_main will
117 never be called. */
118 int _dl_starting_up = 0;
119 INTVARDEF(_dl_starting_up)
120 #endif
121
122 /* This is the structure which defines all variables global to ld.so
123 (except those which cannot be added for some reason). */
124 struct rtld_global _rtld_global =
125 {
126 /* Generally the default presumption without further information is an
127 * executable stack but this is not true for all platforms. */
128 ._dl_stack_flags = DEFAULT_STACK_PERMS,
129 #ifdef _LIBC_REENTRANT
130 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
131 ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
132 #endif
133 ._dl_nns = 1,
134 ._dl_ns =
135 {
136 #ifdef _LIBC_REENTRANT
137 [LM_ID_BASE] = { ._ns_unique_sym_table
138 = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } }
139 #endif
140 }
141 };
142 /* If we would use strong_alias here the compiler would see a
143 non-hidden definition. This would undo the effect of the previous
144 declaration. So spell out was strong_alias does plus add the
145 visibility attribute. */
146 extern struct rtld_global _rtld_local
147 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
148
149
150 /* This variable is similar to _rtld_local, but all values are
151 read-only after relocation. */
152 struct rtld_global_ro _rtld_global_ro attribute_relro =
153 {
154 /* Get architecture specific initializer. */
155 #include <dl-procinfo.c>
156 #ifdef NEED_DL_SYSINFO
157 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
158 #endif
159 ._dl_debug_fd = STDERR_FILENO,
160 ._dl_use_load_bias = -2,
161 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
162 ._dl_hwcap_mask = HWCAP_IMPORTANT,
163 ._dl_lazy = 1,
164 ._dl_fpu_control = _FPU_DEFAULT,
165 ._dl_pointer_guard = 1,
166 ._dl_pagesize = EXEC_PAGESIZE,
167 ._dl_inhibit_cache = 0,
168
169 /* Function pointers. */
170 ._dl_debug_printf = _dl_debug_printf,
171 ._dl_catch_error = _dl_catch_error,
172 ._dl_signal_error = _dl_signal_error,
173 ._dl_mcount = _dl_mcount_internal,
174 ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
175 ._dl_check_caller = _dl_check_caller,
176 ._dl_open = _dl_open,
177 ._dl_close = _dl_close,
178 ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
179 #ifdef HAVE_DL_DISCOVER_OSVERSION
180 ._dl_discover_osversion = _dl_discover_osversion
181 #endif
182 };
183 /* If we would use strong_alias here the compiler would see a
184 non-hidden definition. This would undo the effect of the previous
185 declaration. So spell out was strong_alias does plus add the
186 visibility attribute. */
187 extern struct rtld_global_ro _rtld_local_ro
188 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
189
190
191 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
192 ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv);
193
194 /* These two variables cannot be moved into .data.rel.ro. */
195 static struct libname_list _dl_rtld_libname;
196 static struct libname_list _dl_rtld_libname2;
197
198 /* We expect less than a second for relocation. */
199 #ifdef HP_SMALL_TIMING_AVAIL
200 # undef HP_TIMING_AVAIL
201 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
202 #endif
203
204 /* Variable for statistics. */
205 #ifndef HP_TIMING_NONAVAIL
206 static hp_timing_t relocate_time;
207 static hp_timing_t load_time attribute_relro;
208 static hp_timing_t start_time attribute_relro;
209 #endif
210
211 /* Additional definitions needed by TLS initialization. */
212 #ifdef TLS_INIT_HELPER
213 TLS_INIT_HELPER
214 #endif
215
216 /* Helper function for syscall implementation. */
217 #ifdef DL_SYSINFO_IMPLEMENTATION
218 DL_SYSINFO_IMPLEMENTATION
219 #endif
220
221 /* Before ld.so is relocated we must not access variables which need
222 relocations. This means variables which are exported. Variables
223 declared as static are fine. If we can mark a variable hidden this
224 is fine, too. The latter is important here. We can avoid setting
225 up a temporary link map for ld.so if we can mark _rtld_global as
226 hidden. */
227 #ifdef PI_STATIC_AND_HIDDEN
228 # define DONT_USE_BOOTSTRAP_MAP 1
229 #endif
230
231 #ifdef DONT_USE_BOOTSTRAP_MAP
232 static ElfW(Addr) _dl_start_final (void *arg);
233 #else
234 struct dl_start_final_info
235 {
236 struct link_map l;
237 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
238 hp_timing_t start_time;
239 #endif
240 };
241 static ElfW(Addr) _dl_start_final (void *arg,
242 struct dl_start_final_info *info);
243 #endif
244
245 /* These defined magically in the linker script. */
246 extern char _begin[] attribute_hidden;
247 extern char _etext[] attribute_hidden;
248 extern char _end[] attribute_hidden;
249
250
251 #ifdef RTLD_START
252 RTLD_START
253 #else
254 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
255 #endif
256
257 /* This is the second half of _dl_start (below). It can be inlined safely
258 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
259 references. When the tools don't permit us to avoid using a GOT entry
260 for _dl_rtld_global (no attribute_hidden support), we must make sure
261 this function is not inlined (see below). */
262
263 #ifdef DONT_USE_BOOTSTRAP_MAP
264 static inline ElfW(Addr) __attribute__ ((always_inline))
265 _dl_start_final (void *arg)
266 #else
267 static ElfW(Addr) __attribute__ ((noinline))
268 _dl_start_final (void *arg, struct dl_start_final_info *info)
269 #endif
270 {
271 ElfW(Addr) start_addr;
272
273 if (HP_TIMING_AVAIL)
274 {
275 /* If it hasn't happen yet record the startup time. */
276 if (! HP_TIMING_INLINE)
277 HP_TIMING_NOW (start_time);
278 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
279 else
280 start_time = info->start_time;
281 #endif
282
283 /* Initialize the timing functions. */
284 HP_TIMING_DIFF_INIT ();
285 }
286
287 /* Transfer data about ourselves to the permanent link_map structure. */
288 #ifndef DONT_USE_BOOTSTRAP_MAP
289 GL(dl_rtld_map).l_addr = info->l.l_addr;
290 GL(dl_rtld_map).l_ld = info->l.l_ld;
291 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
292 sizeof GL(dl_rtld_map).l_info);
293 GL(dl_rtld_map).l_mach = info->l.l_mach;
294 GL(dl_rtld_map).l_relocated = 1;
295 #endif
296 _dl_setup_hash (&GL(dl_rtld_map));
297 GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
298 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
299 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
300 GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
301 /* Copy the TLS related data if necessary. */
302 #ifndef DONT_USE_BOOTSTRAP_MAP
303 # if USE___THREAD
304 assert (info->l.l_tls_modid != 0);
305 GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize;
306 GL(dl_rtld_map).l_tls_align = info->l.l_tls_align;
307 GL(dl_rtld_map).l_tls_firstbyte_offset = info->l.l_tls_firstbyte_offset;
308 GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size;
309 GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage;
310 GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset;
311 GL(dl_rtld_map).l_tls_modid = 1;
312 # else
313 # if NO_TLS_OFFSET != 0
314 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
315 # endif
316 # endif
317
318 #endif
319
320 #if HP_TIMING_AVAIL
321 HP_TIMING_NOW (GL(dl_cpuclock_offset));
322 #endif
323
324 /* Initialize the stack end variable. */
325 __libc_stack_end = __builtin_frame_address (0);
326
327 /* Call the OS-dependent function to set up life so we can do things like
328 file access. It will call `dl_main' (below) to do all the real work
329 of the dynamic linker, and then unwind our frame and run the user
330 entry point on the same stack we entered on. */
331 start_addr = _dl_sysdep_start (arg, &dl_main);
332
333 #ifndef HP_TIMING_NONAVAIL
334 hp_timing_t rtld_total_time;
335 if (HP_TIMING_AVAIL)
336 {
337 hp_timing_t end_time;
338
339 /* Get the current time. */
340 HP_TIMING_NOW (end_time);
341
342 /* Compute the difference. */
343 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
344 }
345 #endif
346
347 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
348 {
349 #ifndef HP_TIMING_NONAVAIL
350 print_statistics (&rtld_total_time);
351 #else
352 print_statistics (NULL);
353 #endif
354 }
355
356 return start_addr;
357 }
358
359 static ElfW(Addr) __attribute_used__ internal_function
360 _dl_start (void *arg)
361 {
362 #ifdef DONT_USE_BOOTSTRAP_MAP
363 # define bootstrap_map GL(dl_rtld_map)
364 #else
365 struct dl_start_final_info info;
366 # define bootstrap_map info.l
367 #endif
368
369 /* This #define produces dynamic linking inline functions for
370 bootstrap relocation instead of general-purpose relocation.
371 Since ld.so must not have any undefined symbols the result
372 is trivial: always the map of ld.so itself. */
373 #define RTLD_BOOTSTRAP
374 #define RESOLVE_MAP(sym, version, flags) (&bootstrap_map)
375 #include "dynamic-link.h"
376
377 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
378 #ifdef DONT_USE_BOOTSTRAP_MAP
379 HP_TIMING_NOW (start_time);
380 #else
381 HP_TIMING_NOW (info.start_time);
382 #endif
383
384 /* Partly clean the `bootstrap_map' structure up. Don't use
385 `memset' since it might not be built in or inlined and we cannot
386 make function calls at this point. Use '__builtin_memset' if we
387 know it is available. We do not have to clear the memory if we
388 do not have to use the temporary bootstrap_map. Global variables
389 are initialized to zero by default. */
390 #ifndef DONT_USE_BOOTSTRAP_MAP
391 # ifdef HAVE_BUILTIN_MEMSET
392 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
393 # else
394 for (size_t cnt = 0;
395 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
396 ++cnt)
397 bootstrap_map.l_info[cnt] = 0;
398 # endif
399 # if USE___THREAD
400 bootstrap_map.l_tls_modid = 0;
401 # endif
402 #endif
403
404 /* Figure out the run-time load address of the dynamic linker itself. */
405 bootstrap_map.l_addr = elf_machine_load_address ();
406
407 /* Read our own dynamic section and fill in the info array. */
408 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
409 elf_get_dynamic_info (&bootstrap_map, NULL);
410
411 #if NO_TLS_OFFSET != 0
412 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
413 #endif
414
415 /* Get the dynamic linker's own program header. First we need the ELF
416 file header. The `_begin' symbol created by the linker script points
417 to it. When we have something like GOTOFF relocs, we can use a plain
418 reference to find the runtime address. Without that, we have to rely
419 on the `l_addr' value, which is not the value we want when prelinked. */
420 #if USE___THREAD
421 dtv_t initdtv[3];
422 ElfW(Ehdr) *ehdr
423 # ifdef DONT_USE_BOOTSTRAP_MAP
424 = (ElfW(Ehdr) *) &_begin;
425 # else
426 # error This will not work with prelink.
427 = (ElfW(Ehdr) *) bootstrap_map.l_addr;
428 # endif
429 ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff);
430 size_t cnt = ehdr->e_phnum; /* PT_TLS is usually the last phdr. */
431 while (cnt-- > 0)
432 if (phdr[cnt].p_type == PT_TLS)
433 {
434 void *tlsblock;
435 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
436 char *p;
437
438 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
439 bootstrap_map.l_tls_align = phdr[cnt].p_align;
440 if (phdr[cnt].p_align == 0)
441 bootstrap_map.l_tls_firstbyte_offset = 0;
442 else
443 bootstrap_map.l_tls_firstbyte_offset = (phdr[cnt].p_vaddr
444 & (phdr[cnt].p_align - 1));
445 assert (bootstrap_map.l_tls_blocksize != 0);
446 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
447 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
448 + phdr[cnt].p_vaddr);
449
450 /* We can now allocate the initial TLS block. This can happen
451 on the stack. We'll get the final memory later when we
452 know all about the various objects loaded at startup
453 time. */
454 # if TLS_TCB_AT_TP
455 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
456 TLS_INIT_TCB_ALIGN)
457 + TLS_INIT_TCB_SIZE
458 + max_align);
459 # elif TLS_DTV_AT_TP
460 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
461 bootstrap_map.l_tls_align)
462 + bootstrap_map.l_tls_blocksize
463 + max_align);
464 # else
465 /* In case a model with a different layout for the TCB and DTV
466 is defined add another #elif here and in the following #ifs. */
467 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
468 # endif
469 /* Align the TLS block. */
470 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
471 & ~(max_align - 1));
472
473 /* Initialize the dtv. [0] is the length, [1] the generation
474 counter. */
475 initdtv[0].counter = 1;
476 initdtv[1].counter = 0;
477
478 /* Initialize the TLS block. */
479 # if TLS_TCB_AT_TP
480 initdtv[2].pointer = tlsblock;
481 # elif TLS_DTV_AT_TP
482 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
483 bootstrap_map.l_tls_align);
484 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
485 # else
486 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
487 # endif
488 p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
489 bootstrap_map.l_tls_initimage_size);
490 # ifdef HAVE_BUILTIN_MEMSET
491 __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
492 - bootstrap_map.l_tls_initimage_size));
493 # else
494 {
495 size_t remaining = (bootstrap_map.l_tls_blocksize
496 - bootstrap_map.l_tls_initimage_size);
497 while (remaining-- > 0)
498 *p++ = '\0';
499 }
500 # endif
501
502 /* Install the pointer to the dtv. */
503
504 /* Initialize the thread pointer. */
505 # if TLS_TCB_AT_TP
506 bootstrap_map.l_tls_offset
507 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
508
509 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
510 initdtv);
511
512 const char *lossage = TLS_INIT_TP ((char *) tlsblock
513 + bootstrap_map.l_tls_offset, 0);
514 # elif TLS_DTV_AT_TP
515 INSTALL_DTV (tlsblock, initdtv);
516 const char *lossage = TLS_INIT_TP (tlsblock, 0);
517 # else
518 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
519 # endif
520 if (__builtin_expect (lossage != NULL, 0))
521 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
522 lossage);
523
524 /* So far this is module number one. */
525 bootstrap_map.l_tls_modid = 1;
526
527 /* There can only be one PT_TLS entry. */
528 break;
529 }
530 #endif /* USE___THREAD */
531
532 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
533 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
534 #endif
535
536 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
537 {
538 /* Relocate ourselves so we can do normal function calls and
539 data access using the global offset table. */
540
541 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0, 0);
542 }
543 bootstrap_map.l_relocated = 1;
544
545 /* Please note that we don't allow profiling of this object and
546 therefore need not test whether we have to allocate the array
547 for the relocation results (as done in dl-reloc.c). */
548
549 /* Now life is sane; we can call functions and access global data.
550 Set up to use the operating system facilities, and find out from
551 the operating system's program loader where to find the program
552 header table in core. Put the rest of _dl_start into a separate
553 function, that way the compiler cannot put accesses to the GOT
554 before ELF_DYNAMIC_RELOCATE. */
555 {
556 #ifdef DONT_USE_BOOTSTRAP_MAP
557 ElfW(Addr) entry = _dl_start_final (arg);
558 #else
559 ElfW(Addr) entry = _dl_start_final (arg, &info);
560 #endif
561
562 #ifndef ELF_MACHINE_START_ADDRESS
563 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
564 #endif
565
566 return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
567 }
568 }
569
570
571
572 /* Now life is peachy; we can do all normal operations.
573 On to the real work. */
574
575 /* Some helper functions. */
576
577 /* Arguments to relocate_doit. */
578 struct relocate_args
579 {
580 struct link_map *l;
581 int reloc_mode;
582 };
583
584 struct map_args
585 {
586 /* Argument to map_doit. */
587 char *str;
588 struct link_map *loader;
589 int mode;
590 /* Return value of map_doit. */
591 struct link_map *map;
592 };
593
594 struct dlmopen_args
595 {
596 const char *fname;
597 struct link_map *map;
598 };
599
600 struct lookup_args
601 {
602 const char *name;
603 struct link_map *map;
604 void *result;
605 };
606
607 /* Arguments to version_check_doit. */
608 struct version_check_args
609 {
610 int doexit;
611 int dotrace;
612 };
613
614 static void
615 relocate_doit (void *a)
616 {
617 struct relocate_args *args = (struct relocate_args *) a;
618
619 _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0);
620 }
621
622 static void
623 map_doit (void *a)
624 {
625 struct map_args *args = (struct map_args *) a;
626 args->map = _dl_map_object (args->loader, args->str, lt_library, 0,
627 args->mode, LM_ID_BASE);
628 }
629
630 static void
631 dlmopen_doit (void *a)
632 {
633 struct dlmopen_args *args = (struct dlmopen_args *) a;
634 args->map = _dl_open (args->fname,
635 (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT
636 | __RTLD_SECURE),
637 dl_main, LM_ID_NEWLM, _dl_argc, INTUSE(_dl_argv),
638 __environ);
639 }
640
641 static void
642 lookup_doit (void *a)
643 {
644 struct lookup_args *args = (struct lookup_args *) a;
645 const ElfW(Sym) *ref = NULL;
646 args->result = NULL;
647 lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
648 args->map->l_local_scope, NULL, 0,
649 DL_LOOKUP_RETURN_NEWEST, NULL);
650 if (ref != NULL)
651 args->result = DL_SYMBOL_ADDRESS (l, ref);
652 }
653
654 static void
655 version_check_doit (void *a)
656 {
657 struct version_check_args *args = (struct version_check_args *) a;
658 if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
659 args->dotrace) && args->doexit)
660 /* We cannot start the application. Abort now. */
661 _exit (1);
662 }
663
664
665 static inline struct link_map *
666 find_needed (const char *name)
667 {
668 struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
669 unsigned int n = scope->r_nlist;
670
671 while (n-- > 0)
672 if (_dl_name_match_p (name, scope->r_list[n]))
673 return scope->r_list[n];
674
675 /* Should never happen. */
676 return NULL;
677 }
678
679 static int
680 match_version (const char *string, struct link_map *map)
681 {
682 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
683 ElfW(Verdef) *def;
684
685 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
686 if (map->l_info[VERDEFTAG] == NULL)
687 /* The file has no symbol versioning. */
688 return 0;
689
690 def = (ElfW(Verdef) *) ((char *) map->l_addr
691 + map->l_info[VERDEFTAG]->d_un.d_ptr);
692 while (1)
693 {
694 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
695
696 /* Compare the version strings. */
697 if (strcmp (string, strtab + aux->vda_name) == 0)
698 /* Bingo! */
699 return 1;
700
701 /* If no more definitions we failed to find what we want. */
702 if (def->vd_next == 0)
703 break;
704
705 /* Next definition. */
706 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
707 }
708
709 return 0;
710 }
711
712 static bool tls_init_tp_called;
713
714 static void *
715 init_tls (void)
716 {
717 /* Number of elements in the static TLS block. */
718 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
719
720 /* Do not do this twice. The audit interface might have required
721 the DTV interfaces to be set up early. */
722 if (GL(dl_initial_dtv) != NULL)
723 return NULL;
724
725 /* Allocate the array which contains the information about the
726 dtv slots. We allocate a few entries more than needed to
727 avoid the need for reallocation. */
728 size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
729
730 /* Allocate. */
731 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
732 calloc (sizeof (struct dtv_slotinfo_list)
733 + nelem * sizeof (struct dtv_slotinfo), 1);
734 /* No need to check the return value. If memory allocation failed
735 the program would have been terminated. */
736
737 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
738 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
739 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
740
741 /* Fill in the information from the loaded modules. No namespace
742 but the base one can be filled at this time. */
743 assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
744 int i = 0;
745 for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
746 l = l->l_next)
747 if (l->l_tls_blocksize != 0)
748 {
749 /* This is a module with TLS data. Store the map reference.
750 The generation counter is zero. */
751 slotinfo[i].map = l;
752 /* slotinfo[i].gen = 0; */
753 ++i;
754 }
755 assert (i == GL(dl_tls_max_dtv_idx));
756
757 /* Compute the TLS offsets for the various blocks. */
758 _dl_determine_tlsoffset ();
759
760 /* Construct the static TLS block and the dtv for the initial
761 thread. For some platforms this will include allocating memory
762 for the thread descriptor. The memory for the TLS block will
763 never be freed. It should be allocated accordingly. The dtv
764 array can be changed if dynamic loading requires it. */
765 void *tcbp = _dl_allocate_tls_storage ();
766 if (tcbp == NULL)
767 _dl_fatal_printf ("\
768 cannot allocate TLS data structures for initial thread");
769
770 /* Store for detection of the special case by __tls_get_addr
771 so it knows not to pass this dtv to the normal realloc. */
772 GL(dl_initial_dtv) = GET_DTV (tcbp);
773
774 /* And finally install it for the main thread. If ld.so itself uses
775 TLS we know the thread pointer was initialized earlier. */
776 const char *lossage
777 #ifdef USE___THREAD
778 = TLS_INIT_TP (tcbp, USE___THREAD);
779 #else
780 = TLS_INIT_TP (tcbp, 0);
781 #endif
782 if (__builtin_expect (lossage != NULL, 0))
783 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
784 tls_init_tp_called = true;
785
786 return tcbp;
787 }
788
789 #ifdef _LIBC_REENTRANT
790 /* _dl_error_catch_tsd points to this for the single-threaded case.
791 It's reset by the thread library for multithreaded programs. */
792 void ** __attribute__ ((const))
793 _dl_initial_error_catch_tsd (void)
794 {
795 static void *data;
796 return &data;
797 }
798 #endif
799
800
801 static unsigned int
802 do_preload (char *fname, struct link_map *main_map, const char *where)
803 {
804 const char *objname;
805 const char *err_str = NULL;
806 struct map_args args;
807 bool malloced;
808
809 args.str = fname;
810 args.loader = main_map;
811 args.mode = __RTLD_SECURE;
812
813 unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
814
815 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
816 if (__builtin_expect (err_str != NULL, 0))
817 {
818 _dl_error_printf ("\
819 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
820 fname, where, err_str);
821 /* No need to call free, this is still before
822 the libc's malloc is used. */
823 }
824 else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
825 /* It is no duplicate. */
826 return 1;
827
828 /* Nothing loaded. */
829 return 0;
830 }
831
832 #if defined SHARED && defined _LIBC_REENTRANT \
833 && defined __rtld_lock_default_lock_recursive
834 static void
835 rtld_lock_default_lock_recursive (void *lock)
836 {
837 __rtld_lock_default_lock_recursive (lock);
838 }
839
840 static void
841 rtld_lock_default_unlock_recursive (void *lock)
842 {
843 __rtld_lock_default_unlock_recursive (lock);
844 }
845 #endif
846
847
848 static void
849 security_init (void)
850 {
851 /* Set up the stack checker's canary. */
852 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
853 #ifdef THREAD_SET_STACK_GUARD
854 THREAD_SET_STACK_GUARD (stack_chk_guard);
855 #else
856 __stack_chk_guard = stack_chk_guard;
857 #endif
858
859 /* Set up the pointer guard as well, if necessary. */
860 if (GLRO(dl_pointer_guard))
861 {
862 uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
863 stack_chk_guard);
864 #ifdef THREAD_SET_POINTER_GUARD
865 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
866 #endif
867 __pointer_chk_guard_local = pointer_chk_guard;
868 }
869
870 /* We do not need the _dl_random value anymore. The less
871 information we leave behind, the better, so clear the
872 variable. */
873 _dl_random = NULL;
874 }
875
876 #include "setup-vdso.h"
877
878 /* The library search path. */
879 static const char *library_path attribute_relro;
880 /* The list preloaded objects. */
881 static const char *preloadlist attribute_relro;
882 /* Nonzero if information about versions has to be printed. */
883 static int version_info attribute_relro;
884
885 static void
886 dl_main (const ElfW(Phdr) *phdr,
887 ElfW(Word) phnum,
888 ElfW(Addr) *user_entry,
889 ElfW(auxv_t) *auxv)
890 {
891 const ElfW(Phdr) *ph;
892 enum mode mode;
893 struct link_map *main_map;
894 size_t file_size;
895 char *file;
896 bool has_interp = false;
897 unsigned int i;
898 bool prelinked = false;
899 bool rtld_is_main = false;
900 #ifndef HP_TIMING_NONAVAIL
901 hp_timing_t start;
902 hp_timing_t stop;
903 hp_timing_t diff;
904 #endif
905 void *tcbp = NULL;
906
907 #ifdef _LIBC_REENTRANT
908 /* Explicit initialization since the reloc would just be more work. */
909 GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
910 #endif
911
912 GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
913
914 #if defined SHARED && defined _LIBC_REENTRANT \
915 && defined __rtld_lock_default_lock_recursive
916 GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
917 GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
918 #endif
919
920 /* The explicit initialization here is cheaper than processing the reloc
921 in the _rtld_local definition's initializer. */
922 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
923
924 /* Process the environment variable which control the behaviour. */
925 process_envvars (&mode);
926
927 #ifndef HAVE_INLINED_SYSCALLS
928 /* Set up a flag which tells we are just starting. */
929 INTUSE(_dl_starting_up) = 1;
930 #endif
931
932 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
933 {
934 /* Ho ho. We are not the program interpreter! We are the program
935 itself! This means someone ran ld.so as a command. Well, that
936 might be convenient to do sometimes. We support it by
937 interpreting the args like this:
938
939 ld.so PROGRAM ARGS...
940
941 The first argument is the name of a file containing an ELF
942 executable we will load and run with the following arguments.
943 To simplify life here, PROGRAM is searched for using the
944 normal rules for shared objects, rather than $PATH or anything
945 like that. We just load it and use its entry point; we don't
946 pay attention to its PT_INTERP command (we are the interpreter
947 ourselves). This is an easy way to test a new ld.so before
948 installing it. */
949 rtld_is_main = true;
950
951 /* Note the place where the dynamic linker actually came from. */
952 GL(dl_rtld_map).l_name = rtld_progname;
953
954 while (_dl_argc > 1)
955 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
956 {
957 mode = list;
958 GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */
959
960 ++_dl_skip_args;
961 --_dl_argc;
962 ++INTUSE(_dl_argv);
963 }
964 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
965 {
966 mode = verify;
967
968 ++_dl_skip_args;
969 --_dl_argc;
970 ++INTUSE(_dl_argv);
971 }
972 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-cache"))
973 {
974 GLRO(dl_inhibit_cache) = 1;
975 ++_dl_skip_args;
976 --_dl_argc;
977 ++INTUSE(_dl_argv);
978 }
979 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
980 && _dl_argc > 2)
981 {
982 library_path = INTUSE(_dl_argv)[2];
983
984 _dl_skip_args += 2;
985 _dl_argc -= 2;
986 INTUSE(_dl_argv) += 2;
987 }
988 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
989 && _dl_argc > 2)
990 {
991 GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
992
993 _dl_skip_args += 2;
994 _dl_argc -= 2;
995 INTUSE(_dl_argv) += 2;
996 }
997 else if (! strcmp (INTUSE(_dl_argv)[1], "--audit") && _dl_argc > 2)
998 {
999 process_dl_audit (INTUSE(_dl_argv)[2]);
1000
1001 _dl_skip_args += 2;
1002 _dl_argc -= 2;
1003 INTUSE(_dl_argv) += 2;
1004 }
1005 else
1006 break;
1007
1008 /* If we have no further argument the program was called incorrectly.
1009 Grant the user some education. */
1010 if (_dl_argc < 2)
1011 _dl_fatal_printf ("\
1012 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
1013 You have invoked `ld.so', the helper program for shared library executables.\n\
1014 This program usually lives in the file `/lib/ld.so', and special directives\n\
1015 in executable files using ELF shared libraries tell the system's program\n\
1016 loader to load the helper program from this file. This helper program loads\n\
1017 the shared libraries needed by the program executable, prepares the program\n\
1018 to run, and runs it. You may invoke this helper program directly from the\n\
1019 command line to load and run an ELF executable file; this is like executing\n\
1020 that file itself, but always uses this helper program from the file you\n\
1021 specified, instead of the helper program file specified in the executable\n\
1022 file you run. This is mostly of use for maintainers to test new versions\n\
1023 of this helper program; chances are you did not intend to run this program.\n\
1024 \n\
1025 --list list all dependencies and how they are resolved\n\
1026 --verify verify that given object really is a dynamically linked\n\
1027 object we can handle\n\
1028 --inhibit-cache Do not use " LD_SO_CACHE "\n\
1029 --library-path PATH use given PATH instead of content of the environment\n\
1030 variable LD_LIBRARY_PATH\n\
1031 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
1032 in LIST\n\
1033 --audit LIST use objects named in LIST as auditors\n");
1034
1035 ++_dl_skip_args;
1036 --_dl_argc;
1037 ++INTUSE(_dl_argv);
1038
1039 /* The initialization of _dl_stack_flags done below assumes the
1040 executable's PT_GNU_STACK may have been honored by the kernel, and
1041 so a PT_GNU_STACK with PF_X set means the stack started out with
1042 execute permission. However, this is not really true if the
1043 dynamic linker is the executable the kernel loaded. For this
1044 case, we must reinitialize _dl_stack_flags to match the dynamic
1045 linker itself. If the dynamic linker was built with a
1046 PT_GNU_STACK, then the kernel may have loaded us with a
1047 nonexecutable stack that we will have to make executable when we
1048 load the program below unless it has a PT_GNU_STACK indicating
1049 nonexecutable stack is ok. */
1050
1051 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1052 if (ph->p_type == PT_GNU_STACK)
1053 {
1054 GL(dl_stack_flags) = ph->p_flags;
1055 break;
1056 }
1057
1058 if (__builtin_expect (mode, normal) == verify)
1059 {
1060 const char *objname;
1061 const char *err_str = NULL;
1062 struct map_args args;
1063 bool malloced;
1064
1065 args.str = rtld_progname;
1066 args.loader = NULL;
1067 args.mode = __RTLD_OPENEXEC;
1068 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
1069 &args);
1070 if (__builtin_expect (err_str != NULL, 0))
1071 /* We don't free the returned string, the programs stops
1072 anyway. */
1073 _exit (EXIT_FAILURE);
1074 }
1075 else
1076 {
1077 HP_TIMING_NOW (start);
1078 _dl_map_object (NULL, rtld_progname, lt_library, 0,
1079 __RTLD_OPENEXEC, LM_ID_BASE);
1080 HP_TIMING_NOW (stop);
1081
1082 HP_TIMING_DIFF (load_time, start, stop);
1083 }
1084
1085 /* Now the map for the main executable is available. */
1086 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1087
1088 if (__builtin_expect (mode, normal) == normal
1089 && GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1090 && main_map->l_info[DT_SONAME] != NULL
1091 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1092 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
1093 (const char *) D_PTR (main_map, l_info[DT_STRTAB])
1094 + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
1095 _dl_fatal_printf ("loader cannot load itself\n");
1096
1097 phdr = main_map->l_phdr;
1098 phnum = main_map->l_phnum;
1099 /* We overwrite here a pointer to a malloc()ed string. But since
1100 the malloc() implementation used at this point is the dummy
1101 implementations which has no real free() function it does not
1102 makes sense to free the old string first. */
1103 main_map->l_name = (char *) "";
1104 *user_entry = main_map->l_entry;
1105
1106 #ifdef HAVE_AUX_VECTOR
1107 /* Adjust the on-stack auxiliary vector so that it looks like the
1108 binary was executed directly. */
1109 for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
1110 switch (av->a_type)
1111 {
1112 case AT_PHDR:
1113 av->a_un.a_val = (uintptr_t) phdr;
1114 break;
1115 case AT_PHNUM:
1116 av->a_un.a_val = phnum;
1117 break;
1118 case AT_ENTRY:
1119 av->a_un.a_val = *user_entry;
1120 break;
1121 case AT_EXECFN:
1122 av->a_un.a_val = (uintptr_t) _dl_argv[0];
1123 break;
1124 }
1125 #endif
1126 }
1127 else
1128 {
1129 /* Create a link_map for the executable itself.
1130 This will be what dlopen on "" returns. */
1131 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
1132 __RTLD_OPENEXEC, LM_ID_BASE);
1133 assert (main_map != NULL);
1134 main_map->l_phdr = phdr;
1135 main_map->l_phnum = phnum;
1136 main_map->l_entry = *user_entry;
1137
1138 /* Even though the link map is not yet fully initialized we can add
1139 it to the map list since there are no possible users running yet. */
1140 _dl_add_to_namespace_list (main_map, LM_ID_BASE);
1141 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1142
1143 /* At this point we are in a bit of trouble. We would have to
1144 fill in the values for l_dev and l_ino. But in general we
1145 do not know where the file is. We also do not handle AT_EXECFD
1146 even if it would be passed up.
1147
1148 We leave the values here defined to 0. This is normally no
1149 problem as the program code itself is normally no shared
1150 object and therefore cannot be loaded dynamically. Nothing
1151 prevent the use of dynamic binaries and in these situations
1152 we might get problems. We might not be able to find out
1153 whether the object is already loaded. But since there is no
1154 easy way out and because the dynamic binary must also not
1155 have an SONAME we ignore this program for now. If it becomes
1156 a problem we can force people using SONAMEs. */
1157
1158 /* We delay initializing the path structure until we got the dynamic
1159 information for the program. */
1160 }
1161
1162 main_map->l_map_end = 0;
1163 main_map->l_text_end = 0;
1164 /* Perhaps the executable has no PT_LOAD header entries at all. */
1165 main_map->l_map_start = ~0;
1166 /* And it was opened directly. */
1167 ++main_map->l_direct_opencount;
1168
1169 /* Scan the program header table for the dynamic section. */
1170 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1171 switch (ph->p_type)
1172 {
1173 case PT_PHDR:
1174 /* Find out the load address. */
1175 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1176 break;
1177 case PT_DYNAMIC:
1178 /* This tells us where to find the dynamic section,
1179 which tells us everything we need to do. */
1180 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1181 break;
1182 case PT_INTERP:
1183 /* This "interpreter segment" was used by the program loader to
1184 find the program interpreter, which is this program itself, the
1185 dynamic linker. We note what name finds us, so that a future
1186 dlopen call or DT_NEEDED entry, for something that wants to link
1187 against the dynamic linker as a shared library, will know that
1188 the shared object is already loaded. */
1189 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1190 + ph->p_vaddr);
1191 /* _dl_rtld_libname.next = NULL; Already zero. */
1192 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1193
1194 /* Ordinarilly, we would get additional names for the loader from
1195 our DT_SONAME. This can't happen if we were actually linked as
1196 a static executable (detect this case when we have no DYNAMIC).
1197 If so, assume the filename component of the interpreter path to
1198 be our SONAME, and add it to our name list. */
1199 if (GL(dl_rtld_map).l_ld == NULL)
1200 {
1201 const char *p = NULL;
1202 const char *cp = _dl_rtld_libname.name;
1203
1204 /* Find the filename part of the path. */
1205 while (*cp != '\0')
1206 if (*cp++ == '/')
1207 p = cp;
1208
1209 if (p != NULL)
1210 {
1211 _dl_rtld_libname2.name = p;
1212 /* _dl_rtld_libname2.next = NULL; Already zero. */
1213 _dl_rtld_libname.next = &_dl_rtld_libname2;
1214 }
1215 }
1216
1217 has_interp = true;
1218 break;
1219 case PT_LOAD:
1220 {
1221 ElfW(Addr) mapstart;
1222 ElfW(Addr) allocend;
1223
1224 /* Remember where the main program starts in memory. */
1225 mapstart = (main_map->l_addr
1226 + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1227 if (main_map->l_map_start > mapstart)
1228 main_map->l_map_start = mapstart;
1229
1230 /* Also where it ends. */
1231 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1232 if (main_map->l_map_end < allocend)
1233 main_map->l_map_end = allocend;
1234 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1235 main_map->l_text_end = allocend;
1236 }
1237 break;
1238
1239 case PT_TLS:
1240 if (ph->p_memsz > 0)
1241 {
1242 /* Note that in the case the dynamic linker we duplicate work
1243 here since we read the PT_TLS entry already in
1244 _dl_start_final. But the result is repeatable so do not
1245 check for this special but unimportant case. */
1246 main_map->l_tls_blocksize = ph->p_memsz;
1247 main_map->l_tls_align = ph->p_align;
1248 if (ph->p_align == 0)
1249 main_map->l_tls_firstbyte_offset = 0;
1250 else
1251 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1252 & (ph->p_align - 1));
1253 main_map->l_tls_initimage_size = ph->p_filesz;
1254 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1255
1256 /* This image gets the ID one. */
1257 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1258 }
1259 break;
1260
1261 case PT_GNU_STACK:
1262 GL(dl_stack_flags) = ph->p_flags;
1263 break;
1264
1265 case PT_GNU_RELRO:
1266 main_map->l_relro_addr = ph->p_vaddr;
1267 main_map->l_relro_size = ph->p_memsz;
1268 break;
1269 }
1270
1271 /* Adjust the address of the TLS initialization image in case
1272 the executable is actually an ET_DYN object. */
1273 if (main_map->l_tls_initimage != NULL)
1274 main_map->l_tls_initimage
1275 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1276 if (! main_map->l_map_end)
1277 main_map->l_map_end = ~0;
1278 if (! main_map->l_text_end)
1279 main_map->l_text_end = ~0;
1280 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1281 {
1282 /* We were invoked directly, so the program might not have a
1283 PT_INTERP. */
1284 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1285 /* _dl_rtld_libname.next = NULL; Already zero. */
1286 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1287 }
1288 else
1289 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1290
1291 /* If the current libname is different from the SONAME, add the
1292 latter as well. */
1293 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1294 && strcmp (GL(dl_rtld_map).l_libname->name,
1295 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1296 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1297 {
1298 static struct libname_list newname;
1299 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1300 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1301 newname.next = NULL;
1302 newname.dont_free = 1;
1303
1304 assert (GL(dl_rtld_map).l_libname->next == NULL);
1305 GL(dl_rtld_map).l_libname->next = &newname;
1306 }
1307 /* The ld.so must be relocated since otherwise loading audit modules
1308 will fail since they reuse the very same ld.so. */
1309 assert (GL(dl_rtld_map).l_relocated);
1310
1311 if (! rtld_is_main)
1312 {
1313 /* Extract the contents of the dynamic section for easy access. */
1314 elf_get_dynamic_info (main_map, NULL);
1315 /* Set up our cache of pointers into the hash table. */
1316 _dl_setup_hash (main_map);
1317 }
1318
1319 if (__builtin_expect (mode, normal) == verify)
1320 {
1321 /* We were called just to verify that this is a dynamic
1322 executable using us as the program interpreter. Exit with an
1323 error if we were not able to load the binary or no interpreter
1324 is specified (i.e., this is no dynamically linked binary. */
1325 if (main_map->l_ld == NULL)
1326 _exit (1);
1327
1328 /* We allow here some platform specific code. */
1329 #ifdef DISTINGUISH_LIB_VERSIONS
1330 DISTINGUISH_LIB_VERSIONS;
1331 #endif
1332 _exit (has_interp ? 0 : 2);
1333 }
1334
1335 struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1336 /* Set up the data structures for the system-supplied DSO early,
1337 so they can influence _dl_init_paths. */
1338 setup_vdso (main_map, &first_preload);
1339
1340 #ifdef DL_SYSDEP_OSCHECK
1341 DL_SYSDEP_OSCHECK (_dl_fatal_printf);
1342 #endif
1343
1344 /* Initialize the data structures for the search paths for shared
1345 objects. */
1346 _dl_init_paths (library_path);
1347
1348 /* Initialize _r_debug. */
1349 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1350 LM_ID_BASE);
1351 r->r_state = RT_CONSISTENT;
1352
1353 /* Put the link_map for ourselves on the chain so it can be found by
1354 name. Note that at this point the global chain of link maps contains
1355 exactly one element, which is pointed to by dl_loaded. */
1356 if (! GL(dl_rtld_map).l_name)
1357 /* If not invoked directly, the dynamic linker shared object file was
1358 found by the PT_INTERP name. */
1359 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1360 GL(dl_rtld_map).l_type = lt_library;
1361 main_map->l_next = &GL(dl_rtld_map);
1362 GL(dl_rtld_map).l_prev = main_map;
1363 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1364 ++GL(dl_load_adds);
1365
1366 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1367 to not using bias for non-prelinked PIEs and libraries
1368 and using it for executables or prelinked PIEs or libraries. */
1369 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1370 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1371
1372 /* Set up the program header information for the dynamic linker
1373 itself. It is needed in the dl_iterate_phdr() callbacks. */
1374 ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start;
1375 ElfW(Phdr) *rtld_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start
1376 + rtld_ehdr->e_phoff);
1377 GL(dl_rtld_map).l_phdr = rtld_phdr;
1378 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1379
1380
1381 /* PT_GNU_RELRO is usually the last phdr. */
1382 size_t cnt = rtld_ehdr->e_phnum;
1383 while (cnt-- > 0)
1384 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1385 {
1386 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1387 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1388 break;
1389 }
1390
1391 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1392 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1393 /* Assign a module ID. Do this before loading any audit modules. */
1394 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1395
1396 /* If we have auditing DSOs to load, do it now. */
1397 if (__builtin_expect (audit_list != NULL, 0))
1398 {
1399 /* Iterate over all entries in the list. The order is important. */
1400 struct audit_ifaces *last_audit = NULL;
1401 struct audit_list *al = audit_list->next;
1402
1403 /* Since we start using the auditing DSOs right away we need to
1404 initialize the data structures now. */
1405 tcbp = init_tls ();
1406
1407 /* Initialize security features. We need to do it this early
1408 since otherwise the constructors of the audit libraries will
1409 use different values (especially the pointer guard) and will
1410 fail later on. */
1411 security_init ();
1412
1413 do
1414 {
1415 int tls_idx = GL(dl_tls_max_dtv_idx);
1416
1417 /* Now it is time to determine the layout of the static TLS
1418 block and allocate it for the initial thread. Note that we
1419 always allocate the static block, we never defer it even if
1420 no DF_STATIC_TLS bit is set. The reason is that we know
1421 glibc will use the static model. */
1422 struct dlmopen_args dlmargs;
1423 dlmargs.fname = al->name;
1424 dlmargs.map = NULL;
1425
1426 const char *objname;
1427 const char *err_str = NULL;
1428 bool malloced;
1429 (void) _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit,
1430 &dlmargs);
1431 if (__builtin_expect (err_str != NULL, 0))
1432 {
1433 not_loaded:
1434 _dl_error_printf ("\
1435 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
1436 al->name, err_str);
1437 if (malloced)
1438 free ((char *) err_str);
1439 }
1440 else
1441 {
1442 struct lookup_args largs;
1443 largs.name = "la_version";
1444 largs.map = dlmargs.map;
1445
1446 /* Check whether the interface version matches. */
1447 (void) _dl_catch_error (&objname, &err_str, &malloced,
1448 lookup_doit, &largs);
1449
1450 unsigned int (*laversion) (unsigned int);
1451 unsigned int lav;
1452 if (err_str == NULL
1453 && (laversion = largs.result) != NULL
1454 && (lav = laversion (LAV_CURRENT)) > 0
1455 && lav <= LAV_CURRENT)
1456 {
1457 /* Allocate structure for the callback function pointers.
1458 This call can never fail. */
1459 union
1460 {
1461 struct audit_ifaces ifaces;
1462 #define naudit_ifaces 8
1463 void (*fptr[naudit_ifaces]) (void);
1464 } *newp = malloc (sizeof (*newp));
1465
1466 /* Names of the auditing interfaces. All in one
1467 long string. */
1468 static const char audit_iface_names[] =
1469 "la_activity\0"
1470 "la_objsearch\0"
1471 "la_objopen\0"
1472 "la_preinit\0"
1473 #if __ELF_NATIVE_CLASS == 32
1474 "la_symbind32\0"
1475 #elif __ELF_NATIVE_CLASS == 64
1476 "la_symbind64\0"
1477 #else
1478 # error "__ELF_NATIVE_CLASS must be defined"
1479 #endif
1480 #define STRING(s) __STRING (s)
1481 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1482 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1483 "la_objclose\0";
1484 unsigned int cnt = 0;
1485 const char *cp = audit_iface_names;
1486 do
1487 {
1488 largs.name = cp;
1489 (void) _dl_catch_error (&objname, &err_str, &malloced,
1490 lookup_doit, &largs);
1491
1492 /* Store the pointer. */
1493 if (err_str == NULL && largs.result != NULL)
1494 {
1495 newp->fptr[cnt] = largs.result;
1496
1497 /* The dynamic linker link map is statically
1498 allocated, initialize the data now. */
1499 GL(dl_rtld_map).l_audit[cnt].cookie
1500 = (intptr_t) &GL(dl_rtld_map);
1501 }
1502 else
1503 newp->fptr[cnt] = NULL;
1504 ++cnt;
1505
1506 cp = (char *) rawmemchr (cp, '\0') + 1;
1507 }
1508 while (*cp != '\0');
1509 assert (cnt == naudit_ifaces);
1510
1511 /* Now append the new auditing interface to the list. */
1512 newp->ifaces.next = NULL;
1513 if (last_audit == NULL)
1514 last_audit = GLRO(dl_audit) = &newp->ifaces;
1515 else
1516 last_audit = last_audit->next = &newp->ifaces;
1517 ++GLRO(dl_naudit);
1518
1519 /* Mark the DSO as being used for auditing. */
1520 dlmargs.map->l_auditing = 1;
1521 }
1522 else
1523 {
1524 /* We cannot use the DSO, it does not have the
1525 appropriate interfaces or it expects something
1526 more recent. */
1527 #ifndef NDEBUG
1528 Lmid_t ns = dlmargs.map->l_ns;
1529 #endif
1530 _dl_close (dlmargs.map);
1531
1532 /* Make sure the namespace has been cleared entirely. */
1533 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
1534 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
1535
1536 GL(dl_tls_max_dtv_idx) = tls_idx;
1537 goto not_loaded;
1538 }
1539 }
1540
1541 al = al->next;
1542 }
1543 while (al != audit_list->next);
1544
1545 /* If we have any auditing modules, announce that we already
1546 have two objects loaded. */
1547 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
1548 {
1549 struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) };
1550
1551 for (unsigned int outer = 0; outer < 2; ++outer)
1552 {
1553 struct audit_ifaces *afct = GLRO(dl_audit);
1554 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1555 {
1556 if (afct->objopen != NULL)
1557 {
1558 ls[outer]->l_audit[cnt].bindflags
1559 = afct->objopen (ls[outer], LM_ID_BASE,
1560 &ls[outer]->l_audit[cnt].cookie);
1561
1562 ls[outer]->l_audit_any_plt
1563 |= ls[outer]->l_audit[cnt].bindflags != 0;
1564 }
1565
1566 afct = afct->next;
1567 }
1568 }
1569 }
1570 }
1571
1572 /* Set up debugging before the debugger is notified for the first time. */
1573 #ifdef ELF_MACHINE_DEBUG_SETUP
1574 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1575 ELF_MACHINE_DEBUG_SETUP (main_map, r);
1576 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1577 #else
1578 if (main_map->l_info[DT_DEBUG] != NULL)
1579 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1580 with the run-time address of the r_debug structure */
1581 main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1582
1583 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1584 case you run gdb on the dynamic linker directly. */
1585 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1586 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1587 #endif
1588
1589 /* We start adding objects. */
1590 r->r_state = RT_ADD;
1591 _dl_debug_state ();
1592 LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
1593
1594 /* Auditing checkpoint: we are ready to signal that the initial map
1595 is being constructed. */
1596 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
1597 {
1598 struct audit_ifaces *afct = GLRO(dl_audit);
1599 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1600 {
1601 if (afct->activity != NULL)
1602 afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD);
1603
1604 afct = afct->next;
1605 }
1606 }
1607
1608 /* We have two ways to specify objects to preload: via environment
1609 variable and via the file /etc/ld.so.preload. The latter can also
1610 be used when security is enabled. */
1611 assert (*first_preload == NULL);
1612 struct link_map **preloads = NULL;
1613 unsigned int npreloads = 0;
1614
1615 if (__builtin_expect (preloadlist != NULL, 0))
1616 {
1617 /* The LD_PRELOAD environment variable gives list of libraries
1618 separated by white space or colons that are loaded before the
1619 executable's dependencies and prepended to the global scope
1620 list. If the binary is running setuid all elements
1621 containing a '/' are ignored since it is insecure. */
1622 char *list = strdupa (preloadlist);
1623 char *p;
1624
1625 HP_TIMING_NOW (start);
1626
1627 /* Prevent optimizing strsep. Speed is not important here. */
1628 while ((p = (strsep) (&list, " :")) != NULL)
1629 if (p[0] != '\0'
1630 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
1631 || strchr (p, '/') == NULL))
1632 npreloads += do_preload (p, main_map, "LD_PRELOAD");
1633
1634 HP_TIMING_NOW (stop);
1635 HP_TIMING_DIFF (diff, start, stop);
1636 HP_TIMING_ACCUM_NT (load_time, diff);
1637 }
1638
1639 /* There usually is no ld.so.preload file, it should only be used
1640 for emergencies and testing. So the open call etc should usually
1641 fail. Using access() on a non-existing file is faster than using
1642 open(). So we do this first. If it succeeds we do almost twice
1643 the work but this does not matter, since it is not for production
1644 use. */
1645 static const char preload_file[] = "/etc/ld.so.preload";
1646 if (__builtin_expect (__access (preload_file, R_OK) == 0, 0))
1647 {
1648 /* Read the contents of the file. */
1649 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1650 PROT_READ | PROT_WRITE);
1651 if (__builtin_expect (file != MAP_FAILED, 0))
1652 {
1653 /* Parse the file. It contains names of libraries to be loaded,
1654 separated by white spaces or `:'. It may also contain
1655 comments introduced by `#'. */
1656 char *problem;
1657 char *runp;
1658 size_t rest;
1659
1660 /* Eliminate comments. */
1661 runp = file;
1662 rest = file_size;
1663 while (rest > 0)
1664 {
1665 char *comment = memchr (runp, '#', rest);
1666 if (comment == NULL)
1667 break;
1668
1669 rest -= comment - runp;
1670 do
1671 *comment = ' ';
1672 while (--rest > 0 && *++comment != '\n');
1673 }
1674
1675 /* We have one problematic case: if we have a name at the end of
1676 the file without a trailing terminating characters, we cannot
1677 place the \0. Handle the case separately. */
1678 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1679 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1680 {
1681 problem = &file[file_size];
1682 while (problem > file && problem[-1] != ' '
1683 && problem[-1] != '\t'
1684 && problem[-1] != '\n' && problem[-1] != ':')
1685 --problem;
1686
1687 if (problem > file)
1688 problem[-1] = '\0';
1689 }
1690 else
1691 {
1692 problem = NULL;
1693 file[file_size - 1] = '\0';
1694 }
1695
1696 HP_TIMING_NOW (start);
1697
1698 if (file != problem)
1699 {
1700 char *p;
1701 runp = file;
1702 while ((p = strsep (&runp, ": \t\n")) != NULL)
1703 if (p[0] != '\0')
1704 npreloads += do_preload (p, main_map, preload_file);
1705 }
1706
1707 if (problem != NULL)
1708 {
1709 char *p = strndupa (problem, file_size - (problem - file));
1710
1711 npreloads += do_preload (p, main_map, preload_file);
1712 }
1713
1714 HP_TIMING_NOW (stop);
1715 HP_TIMING_DIFF (diff, start, stop);
1716 HP_TIMING_ACCUM_NT (load_time, diff);
1717
1718 /* We don't need the file anymore. */
1719 __munmap (file, file_size);
1720 }
1721 }
1722
1723 if (__builtin_expect (*first_preload != NULL, 0))
1724 {
1725 /* Set up PRELOADS with a vector of the preloaded libraries. */
1726 struct link_map *l = *first_preload;
1727 preloads = __alloca (npreloads * sizeof preloads[0]);
1728 i = 0;
1729 do
1730 {
1731 preloads[i++] = l;
1732 l = l->l_next;
1733 } while (l);
1734 assert (i == npreloads);
1735 }
1736
1737 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1738 specified some libraries to load, these are inserted before the actual
1739 dependencies in the executable's searchlist for symbol resolution. */
1740 HP_TIMING_NOW (start);
1741 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
1742 HP_TIMING_NOW (stop);
1743 HP_TIMING_DIFF (diff, start, stop);
1744 HP_TIMING_ACCUM_NT (load_time, diff);
1745
1746 /* Mark all objects as being in the global scope. */
1747 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1748 main_map->l_searchlist.r_list[--i]->l_global = 1;
1749
1750 /* Remove _dl_rtld_map from the chain. */
1751 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1752 if (GL(dl_rtld_map).l_next != NULL)
1753 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1754
1755 for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1756 if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1757 break;
1758
1759 bool rtld_multiple_ref = false;
1760 if (__builtin_expect (i < main_map->l_searchlist.r_nlist, 1))
1761 {
1762 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1763 put it back in the list of visible objects. We insert it into the
1764 chain in symbol search order because gdb uses the chain's order as
1765 its symbol search order. */
1766 rtld_multiple_ref = true;
1767
1768 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1769 if (__builtin_expect (mode, normal) == normal)
1770 {
1771 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1772 ? main_map->l_searchlist.r_list[i + 1]
1773 : NULL);
1774 #ifdef NEED_DL_SYSINFO_DSO
1775 if (GLRO(dl_sysinfo_map) != NULL
1776 && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
1777 && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
1778 GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
1779 #endif
1780 }
1781 else
1782 /* In trace mode there might be an invisible object (which we
1783 could not find) after the previous one in the search list.
1784 In this case it doesn't matter much where we put the
1785 interpreter object, so we just initialize the list pointer so
1786 that the assertion below holds. */
1787 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1788
1789 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1790 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1791 if (GL(dl_rtld_map).l_next != NULL)
1792 {
1793 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1794 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1795 }
1796 }
1797
1798 /* Now let us see whether all libraries are available in the
1799 versions we need. */
1800 {
1801 struct version_check_args args;
1802 args.doexit = mode == normal;
1803 args.dotrace = mode == trace;
1804 _dl_receive_error (print_missing_version, version_check_doit, &args);
1805 }
1806
1807 /* We do not initialize any of the TLS functionality unless any of the
1808 initial modules uses TLS. This makes dynamic loading of modules with
1809 TLS impossible, but to support it requires either eagerly doing setup
1810 now or lazily doing it later. Doing it now makes us incompatible with
1811 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1812 used. Trying to do it lazily is too hairy to try when there could be
1813 multiple threads (from a non-TLS-using libpthread). */
1814 bool was_tls_init_tp_called = tls_init_tp_called;
1815 if (tcbp == NULL)
1816 tcbp = init_tls ();
1817
1818 if (__builtin_expect (audit_list == NULL, 1))
1819 /* Initialize security features. But only if we have not done it
1820 earlier. */
1821 security_init ();
1822
1823 if (__builtin_expect (mode, normal) != normal)
1824 {
1825 /* We were run just to list the shared libraries. It is
1826 important that we do this before real relocation, because the
1827 functions we call below for output may no longer work properly
1828 after relocation. */
1829 struct link_map *l;
1830
1831 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1832 {
1833 struct r_scope_elem *scope = &main_map->l_searchlist;
1834
1835 for (i = 0; i < scope->r_nlist; i++)
1836 {
1837 l = scope->r_list [i];
1838 if (l->l_faked)
1839 {
1840 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1841 continue;
1842 }
1843 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
1844 GLRO(dl_trace_prelink_map) = l;
1845 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1846 DSO_FILENAME (l->l_libname->name),
1847 DSO_FILENAME (l->l_name),
1848 (int) sizeof l->l_map_start * 2,
1849 (size_t) l->l_map_start,
1850 (int) sizeof l->l_addr * 2,
1851 (size_t) l->l_addr);
1852
1853 if (l->l_tls_modid)
1854 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1855 (int) sizeof l->l_tls_offset * 2,
1856 (size_t) l->l_tls_offset);
1857 else
1858 _dl_printf ("\n");
1859 }
1860 }
1861 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
1862 {
1863 /* Look through the dependencies of the main executable
1864 and determine which of them is not actually
1865 required. */
1866 struct link_map *l = main_map;
1867
1868 /* Relocate the main executable. */
1869 struct relocate_args args = { .l = l,
1870 .reloc_mode = ((GLRO(dl_lazy)
1871 ? RTLD_LAZY : 0)
1872 | __RTLD_NOIFUNC) };
1873 _dl_receive_error (print_unresolved, relocate_doit, &args);
1874
1875 /* This loop depends on the dependencies of the executable to
1876 correspond in number and order to the DT_NEEDED entries. */
1877 ElfW(Dyn) *dyn = main_map->l_ld;
1878 bool first = true;
1879 while (dyn->d_tag != DT_NULL)
1880 {
1881 if (dyn->d_tag == DT_NEEDED)
1882 {
1883 l = l->l_next;
1884 #ifdef NEED_DL_SYSINFO_DSO
1885 /* Skip the VDSO since it's not part of the list
1886 of objects we brought in via DT_NEEDED entries. */
1887 if (l == GLRO(dl_sysinfo_map))
1888 l = l->l_next;
1889 #endif
1890 if (!l->l_used)
1891 {
1892 if (first)
1893 {
1894 _dl_printf ("Unused direct dependencies:\n");
1895 first = false;
1896 }
1897
1898 _dl_printf ("\t%s\n", l->l_name);
1899 }
1900 }
1901
1902 ++dyn;
1903 }
1904
1905 _exit (first != true);
1906 }
1907 else if (! main_map->l_info[DT_NEEDED])
1908 _dl_printf ("\tstatically linked\n");
1909 else
1910 {
1911 for (l = main_map->l_next; l; l = l->l_next)
1912 if (l->l_faked)
1913 /* The library was not found. */
1914 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1915 else if (strcmp (l->l_libname->name, l->l_name) == 0)
1916 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
1917 (int) sizeof l->l_map_start * 2,
1918 (size_t) l->l_map_start);
1919 else
1920 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1921 l->l_name, (int) sizeof l->l_map_start * 2,
1922 (size_t) l->l_map_start);
1923 }
1924
1925 if (__builtin_expect (mode, trace) != trace)
1926 for (i = 1; i < (unsigned int) _dl_argc; ++i)
1927 {
1928 const ElfW(Sym) *ref = NULL;
1929 ElfW(Addr) loadbase;
1930 lookup_t result;
1931
1932 result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map,
1933 &ref, main_map->l_scope,
1934 NULL, ELF_RTYPE_CLASS_PLT,
1935 DL_LOOKUP_ADD_DEPENDENCY, NULL);
1936
1937 loadbase = LOOKUP_VALUE_ADDRESS (result);
1938
1939 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1940 INTUSE(_dl_argv)[i],
1941 (int) sizeof ref->st_value * 2,
1942 (size_t) ref->st_value,
1943 (int) sizeof loadbase * 2, (size_t) loadbase);
1944 }
1945 else
1946 {
1947 /* If LD_WARN is set, warn about undefined symbols. */
1948 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
1949 {
1950 /* We have to do symbol dependency testing. */
1951 struct relocate_args args;
1952 unsigned int i;
1953
1954 args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
1955 | __RTLD_NOIFUNC);
1956
1957 i = main_map->l_searchlist.r_nlist;
1958 while (i-- > 0)
1959 {
1960 struct link_map *l = main_map->l_initfini[i];
1961 if (l != &GL(dl_rtld_map) && ! l->l_faked)
1962 {
1963 args.l = l;
1964 _dl_receive_error (print_unresolved, relocate_doit,
1965 &args);
1966 }
1967 }
1968
1969 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1970 && rtld_multiple_ref)
1971 {
1972 /* Mark the link map as not yet relocated again. */
1973 GL(dl_rtld_map).l_relocated = 0;
1974 _dl_relocate_object (&GL(dl_rtld_map),
1975 main_map->l_scope, __RTLD_NOIFUNC, 0);
1976 }
1977 }
1978 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1979 if (version_info)
1980 {
1981 /* Print more information. This means here, print information
1982 about the versions needed. */
1983 int first = 1;
1984 struct link_map *map;
1985
1986 for (map = main_map; map != NULL; map = map->l_next)
1987 {
1988 const char *strtab;
1989 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1990 ElfW(Verneed) *ent;
1991
1992 if (dyn == NULL)
1993 continue;
1994
1995 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1996 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1997
1998 if (first)
1999 {
2000 _dl_printf ("\n\tVersion information:\n");
2001 first = 0;
2002 }
2003
2004 _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name));
2005
2006 while (1)
2007 {
2008 ElfW(Vernaux) *aux;
2009 struct link_map *needed;
2010
2011 needed = find_needed (strtab + ent->vn_file);
2012 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
2013
2014 while (1)
2015 {
2016 const char *fname = NULL;
2017
2018 if (needed != NULL
2019 && match_version (strtab + aux->vna_name,
2020 needed))
2021 fname = needed->l_name;
2022
2023 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2024 strtab + ent->vn_file,
2025 strtab + aux->vna_name,
2026 aux->vna_flags & VER_FLG_WEAK
2027 ? "[WEAK] " : "",
2028 fname ?: "not found");
2029
2030 if (aux->vna_next == 0)
2031 /* No more symbols. */
2032 break;
2033
2034 /* Next symbol. */
2035 aux = (ElfW(Vernaux) *) ((char *) aux
2036 + aux->vna_next);
2037 }
2038
2039 if (ent->vn_next == 0)
2040 /* No more dependencies. */
2041 break;
2042
2043 /* Next dependency. */
2044 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
2045 }
2046 }
2047 }
2048 }
2049
2050 _exit (0);
2051 }
2052
2053 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
2054 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
2055 && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
2056 {
2057 ElfW(Lib) *liblist, *liblistend;
2058 struct link_map **r_list, **r_listend, *l;
2059 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
2060
2061 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
2062 liblist = (ElfW(Lib) *)
2063 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
2064 liblistend = (ElfW(Lib) *)
2065 ((char *) liblist +
2066 main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
2067 r_list = main_map->l_searchlist.r_list;
2068 r_listend = r_list + main_map->l_searchlist.r_nlist;
2069
2070 for (; r_list < r_listend && liblist < liblistend; r_list++)
2071 {
2072 l = *r_list;
2073
2074 if (l == main_map)
2075 continue;
2076
2077 /* If the library is not mapped where it should, fail. */
2078 if (l->l_addr)
2079 break;
2080
2081 /* Next, check if checksum matches. */
2082 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
2083 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
2084 != liblist->l_checksum)
2085 break;
2086
2087 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
2088 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
2089 != liblist->l_time_stamp)
2090 break;
2091
2092 if (! _dl_name_match_p (strtab + liblist->l_name, l))
2093 break;
2094
2095 ++liblist;
2096 }
2097
2098
2099 if (r_list == r_listend && liblist == liblistend)
2100 prelinked = true;
2101
2102 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2103 _dl_debug_printf ("\nprelink checking: %s\n",
2104 prelinked ? "ok" : "failed");
2105 }
2106
2107
2108 /* Now set up the variable which helps the assembler startup code. */
2109 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
2110
2111 /* Save the information about the original global scope list since
2112 we need it in the memory handling later. */
2113 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
2114
2115 /* Remember the last search directory added at startup, now that
2116 malloc will no longer be the one from dl-minimal.c. */
2117 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
2118
2119 /* Print scope information. */
2120 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
2121 {
2122 _dl_debug_printf ("\nInitial object scopes\n");
2123
2124 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2125 _dl_show_scope (l, 0);
2126 }
2127
2128 if (prelinked)
2129 {
2130 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2131 {
2132 ElfW(Rela) *conflict, *conflictend;
2133 #ifndef HP_TIMING_NONAVAIL
2134 hp_timing_t start;
2135 hp_timing_t stop;
2136 #endif
2137
2138 HP_TIMING_NOW (start);
2139 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2140 conflict = (ElfW(Rela) *)
2141 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2142 conflictend = (ElfW(Rela) *)
2143 ((char *) conflict
2144 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2145 _dl_resolve_conflicts (main_map, conflict, conflictend);
2146 HP_TIMING_NOW (stop);
2147 HP_TIMING_DIFF (relocate_time, start, stop);
2148 }
2149
2150
2151 /* Mark all the objects so we know they have been already relocated. */
2152 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2153 {
2154 l->l_relocated = 1;
2155 if (l->l_relro_size)
2156 _dl_protect_relro (l);
2157
2158 /* Add object to slot information data if necessasy. */
2159 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2160 _dl_add_to_slotinfo (l);
2161 }
2162 }
2163 else
2164 {
2165 /* Now we have all the objects loaded. Relocate them all except for
2166 the dynamic linker itself. We do this in reverse order so that copy
2167 relocs of earlier objects overwrite the data written by later
2168 objects. We do not re-relocate the dynamic linker itself in this
2169 loop because that could result in the GOT entries for functions we
2170 call being changed, and that would break us. It is safe to relocate
2171 the dynamic linker out of order because it has no copy relocs (we
2172 know that because it is self-contained). */
2173
2174 int consider_profiling = GLRO(dl_profile) != NULL;
2175 #ifndef HP_TIMING_NONAVAIL
2176 hp_timing_t start;
2177 hp_timing_t stop;
2178 #endif
2179
2180 /* If we are profiling we also must do lazy reloaction. */
2181 GLRO(dl_lazy) |= consider_profiling;
2182
2183 HP_TIMING_NOW (start);
2184 unsigned i = main_map->l_searchlist.r_nlist;
2185 while (i-- > 0)
2186 {
2187 struct link_map *l = main_map->l_initfini[i];
2188
2189 /* While we are at it, help the memory handling a bit. We have to
2190 mark some data structures as allocated with the fake malloc()
2191 implementation in ld.so. */
2192 struct libname_list *lnp = l->l_libname->next;
2193
2194 while (__builtin_expect (lnp != NULL, 0))
2195 {
2196 lnp->dont_free = 1;
2197 lnp = lnp->next;
2198 }
2199 /* Also allocated with the fake malloc(). */
2200 l->l_free_initfini = 0;
2201
2202 if (l != &GL(dl_rtld_map))
2203 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2204 consider_profiling);
2205
2206 /* Add object to slot information data if necessasy. */
2207 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2208 _dl_add_to_slotinfo (l);
2209 }
2210 HP_TIMING_NOW (stop);
2211
2212 HP_TIMING_DIFF (relocate_time, start, stop);
2213
2214 /* Now enable profiling if needed. Like the previous call,
2215 this has to go here because the calls it makes should use the
2216 rtld versions of the functions (particularly calloc()), but it
2217 needs to have _dl_profile_map set up by the relocator. */
2218 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
2219 /* We must prepare the profiling. */
2220 _dl_start_profile ();
2221 }
2222
2223 if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2224 ++GL(dl_tls_generation);
2225
2226 /* Now that we have completed relocation, the initializer data
2227 for the TLS blocks has its final values and we can copy them
2228 into the main thread's TLS area, which we allocated above. */
2229 _dl_allocate_tls_init (tcbp);
2230
2231 /* And finally install it for the main thread. If ld.so itself uses
2232 TLS we know the thread pointer was initialized earlier. */
2233 if (! tls_init_tp_called)
2234 {
2235 const char *lossage
2236 #ifdef USE___THREAD
2237 = TLS_INIT_TP (tcbp, USE___THREAD);
2238 #else
2239 = TLS_INIT_TP (tcbp, 0);
2240 #endif
2241 if (__builtin_expect (lossage != NULL, 0))
2242 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2243 lossage);
2244 }
2245
2246 /* Make sure no new search directories have been added. */
2247 assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2248
2249 if (! prelinked && rtld_multiple_ref)
2250 {
2251 /* There was an explicit ref to the dynamic linker as a shared lib.
2252 Re-relocate ourselves with user-controlled symbol definitions.
2253
2254 We must do this after TLS initialization in case after this
2255 re-relocation, we might call a user-supplied function
2256 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2257
2258 #ifndef HP_TIMING_NONAVAIL
2259 hp_timing_t start;
2260 hp_timing_t stop;
2261 hp_timing_t add;
2262 #endif
2263
2264 HP_TIMING_NOW (start);
2265 /* Mark the link map as not yet relocated again. */
2266 GL(dl_rtld_map).l_relocated = 0;
2267 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2268 HP_TIMING_NOW (stop);
2269 HP_TIMING_DIFF (add, start, stop);
2270 HP_TIMING_ACCUM_NT (relocate_time, add);
2271 }
2272
2273 /* Do any necessary cleanups for the startup OS interface code.
2274 We do these now so that no calls are made after rtld re-relocation
2275 which might be resolved to different functions than we expect.
2276 We cannot do this before relocating the other objects because
2277 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2278 _dl_sysdep_start_cleanup ();
2279
2280 #ifdef SHARED
2281 /* Auditing checkpoint: we have added all objects. */
2282 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
2283 {
2284 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2285 /* Do not call the functions for any auditing object. */
2286 if (head->l_auditing == 0)
2287 {
2288 struct audit_ifaces *afct = GLRO(dl_audit);
2289 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2290 {
2291 if (afct->activity != NULL)
2292 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
2293
2294 afct = afct->next;
2295 }
2296 }
2297 }
2298 #endif
2299
2300 /* Notify the debugger all new objects are now ready to go. We must re-get
2301 the address since by now the variable might be in another object. */
2302 r = _dl_debug_initialize (0, LM_ID_BASE);
2303 r->r_state = RT_CONSISTENT;
2304 _dl_debug_state ();
2305 LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
2306
2307 #if defined USE_LDCONFIG && !defined MAP_COPY
2308 /* We must munmap() the cache file. */
2309 _dl_unload_cache ();
2310 #endif
2311
2312 /* Once we return, _dl_sysdep_start will invoke
2313 the DT_INIT functions and then *USER_ENTRY. */
2314 }
2315 \f
2316 /* This is a little helper function for resolving symbols while
2317 tracing the binary. */
2318 static void
2319 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2320 const char *errstring)
2321 {
2322 if (objname[0] == '\0')
2323 objname = RTLD_PROGNAME;
2324 _dl_error_printf ("%s (%s)\n", errstring, objname);
2325 }
2326 \f
2327 /* This is a little helper function for resolving symbols while
2328 tracing the binary. */
2329 static void
2330 print_missing_version (int errcode __attribute__ ((unused)),
2331 const char *objname, const char *errstring)
2332 {
2333 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME,
2334 objname, errstring);
2335 }
2336 \f
2337 /* Nonzero if any of the debugging options is enabled. */
2338 static int any_debug attribute_relro;
2339
2340 /* Process the string given as the parameter which explains which debugging
2341 options are enabled. */
2342 static void
2343 process_dl_debug (const char *dl_debug)
2344 {
2345 /* When adding new entries make sure that the maximal length of a name
2346 is correctly handled in the LD_DEBUG_HELP code below. */
2347 static const struct
2348 {
2349 unsigned char len;
2350 const char name[10];
2351 const char helptext[41];
2352 unsigned short int mask;
2353 } debopts[] =
2354 {
2355 #define LEN_AND_STR(str) sizeof (str) - 1, str
2356 { LEN_AND_STR ("libs"), "display library search paths",
2357 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2358 { LEN_AND_STR ("reloc"), "display relocation processing",
2359 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2360 { LEN_AND_STR ("files"), "display progress for input file",
2361 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2362 { LEN_AND_STR ("symbols"), "display symbol table processing",
2363 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2364 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2365 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2366 { LEN_AND_STR ("versions"), "display version dependencies",
2367 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2368 { LEN_AND_STR ("scopes"), "display scope information",
2369 DL_DEBUG_SCOPES },
2370 { LEN_AND_STR ("all"), "all previous options combined",
2371 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2372 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2373 | DL_DEBUG_SCOPES },
2374 { LEN_AND_STR ("statistics"), "display relocation statistics",
2375 DL_DEBUG_STATISTICS },
2376 { LEN_AND_STR ("unused"), "determined unused DSOs",
2377 DL_DEBUG_UNUSED },
2378 { LEN_AND_STR ("help"), "display this help message and exit",
2379 DL_DEBUG_HELP },
2380 };
2381 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2382
2383 /* Skip separating white spaces and commas. */
2384 while (*dl_debug != '\0')
2385 {
2386 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2387 {
2388 size_t cnt;
2389 size_t len = 1;
2390
2391 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2392 && dl_debug[len] != ',' && dl_debug[len] != ':')
2393 ++len;
2394
2395 for (cnt = 0; cnt < ndebopts; ++cnt)
2396 if (debopts[cnt].len == len
2397 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2398 {
2399 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2400 any_debug = 1;
2401 break;
2402 }
2403
2404 if (cnt == ndebopts)
2405 {
2406 /* Display a warning and skip everything until next
2407 separator. */
2408 char *copy = strndupa (dl_debug, len);
2409 _dl_error_printf ("\
2410 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2411 }
2412
2413 dl_debug += len;
2414 continue;
2415 }
2416
2417 ++dl_debug;
2418 }
2419
2420 if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2421 {
2422 /* In order to get an accurate picture of whether a particular
2423 DT_NEEDED entry is actually used we have to process both
2424 the PLT and non-PLT relocation entries. */
2425 GLRO(dl_lazy) = 0;
2426 }
2427
2428 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2429 {
2430 size_t cnt;
2431
2432 _dl_printf ("\
2433 Valid options for the LD_DEBUG environment variable are:\n\n");
2434
2435 for (cnt = 0; cnt < ndebopts; ++cnt)
2436 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2437 " " + debopts[cnt].len - 3,
2438 debopts[cnt].helptext);
2439
2440 _dl_printf ("\n\
2441 To direct the debugging output into a file instead of standard output\n\
2442 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2443 _exit (0);
2444 }
2445 }
2446 \f
2447 static void
2448 process_dl_audit (char *str)
2449 {
2450 /* The parameter is a colon separated list of DSO names. */
2451 char *p;
2452
2453 while ((p = (strsep) (&str, ":")) != NULL)
2454 if (p[0] != '\0'
2455 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
2456 || strchr (p, '/') == NULL))
2457 {
2458 /* This is using the local malloc, not the system malloc. The
2459 memory can never be freed. */
2460 struct audit_list *newp = malloc (sizeof (*newp));
2461 newp->name = p;
2462
2463 if (audit_list == NULL)
2464 audit_list = newp->next = newp;
2465 else
2466 {
2467 newp->next = audit_list->next;
2468 audit_list = audit_list->next = newp;
2469 }
2470 }
2471 }
2472 \f
2473 /* Process all environments variables the dynamic linker must recognize.
2474 Since all of them start with `LD_' we are a bit smarter while finding
2475 all the entries. */
2476 extern char **_environ attribute_hidden;
2477
2478
2479 static void
2480 process_envvars (enum mode *modep)
2481 {
2482 char **runp = _environ;
2483 char *envline;
2484 enum mode mode = normal;
2485 char *debug_output = NULL;
2486
2487 /* This is the default place for profiling data file. */
2488 GLRO(dl_profile_output)
2489 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
2490
2491 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2492 {
2493 size_t len = 0;
2494
2495 while (envline[len] != '\0' && envline[len] != '=')
2496 ++len;
2497
2498 if (envline[len] != '=')
2499 /* This is a "LD_" variable at the end of the string without
2500 a '=' character. Ignore it since otherwise we will access
2501 invalid memory below. */
2502 continue;
2503
2504 switch (len)
2505 {
2506 case 4:
2507 /* Warning level, verbose or not. */
2508 if (memcmp (envline, "WARN", 4) == 0)
2509 GLRO(dl_verbose) = envline[5] != '\0';
2510 break;
2511
2512 case 5:
2513 /* Debugging of the dynamic linker? */
2514 if (memcmp (envline, "DEBUG", 5) == 0)
2515 {
2516 process_dl_debug (&envline[6]);
2517 break;
2518 }
2519 if (memcmp (envline, "AUDIT", 5) == 0)
2520 process_dl_audit (&envline[6]);
2521 break;
2522
2523 case 7:
2524 /* Print information about versions. */
2525 if (memcmp (envline, "VERBOSE", 7) == 0)
2526 {
2527 version_info = envline[8] != '\0';
2528 break;
2529 }
2530
2531 /* List of objects to be preloaded. */
2532 if (memcmp (envline, "PRELOAD", 7) == 0)
2533 {
2534 preloadlist = &envline[8];
2535 break;
2536 }
2537
2538 /* Which shared object shall be profiled. */
2539 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2540 GLRO(dl_profile) = &envline[8];
2541 break;
2542
2543 case 8:
2544 /* Do we bind early? */
2545 if (memcmp (envline, "BIND_NOW", 8) == 0)
2546 {
2547 GLRO(dl_lazy) = envline[9] == '\0';
2548 break;
2549 }
2550 if (memcmp (envline, "BIND_NOT", 8) == 0)
2551 GLRO(dl_bind_not) = envline[9] != '\0';
2552 break;
2553
2554 case 9:
2555 /* Test whether we want to see the content of the auxiliary
2556 array passed up from the kernel. */
2557 if (!INTUSE(__libc_enable_secure)
2558 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2559 _dl_show_auxv ();
2560 break;
2561
2562 case 10:
2563 /* Mask for the important hardware capabilities. */
2564 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
2565 GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL,
2566 0, 0);
2567 break;
2568
2569 case 11:
2570 /* Path where the binary is found. */
2571 if (!INTUSE(__libc_enable_secure)
2572 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2573 GLRO(dl_origin_path) = &envline[12];
2574 break;
2575
2576 case 12:
2577 /* The library search path. */
2578 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
2579 {
2580 library_path = &envline[13];
2581 break;
2582 }
2583
2584 /* Where to place the profiling data file. */
2585 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2586 {
2587 debug_output = &envline[13];
2588 break;
2589 }
2590
2591 if (!INTUSE(__libc_enable_secure)
2592 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2593 GLRO(dl_dynamic_weak) = 1;
2594 break;
2595
2596 case 13:
2597 /* We might have some extra environment variable with length 13
2598 to handle. */
2599 #ifdef EXTRA_LD_ENVVARS_13
2600 EXTRA_LD_ENVVARS_13
2601 #endif
2602 if (!INTUSE(__libc_enable_secure)
2603 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2604 {
2605 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2606 break;
2607 }
2608
2609 if (memcmp (envline, "POINTER_GUARD", 13) == 0)
2610 GLRO(dl_pointer_guard) = envline[14] != '0';
2611 break;
2612
2613 case 14:
2614 /* Where to place the profiling data file. */
2615 if (!INTUSE(__libc_enable_secure)
2616 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2617 && envline[15] != '\0')
2618 GLRO(dl_profile_output) = &envline[15];
2619 break;
2620
2621 case 16:
2622 /* The mode of the dynamic linker can be set. */
2623 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2624 {
2625 mode = trace;
2626 GLRO(dl_verbose) = 1;
2627 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2628 GLRO(dl_trace_prelink) = &envline[17];
2629 }
2630 break;
2631
2632 case 20:
2633 /* The mode of the dynamic linker can be set. */
2634 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2635 mode = trace;
2636 break;
2637
2638 /* We might have some extra environment variable to handle. This
2639 is tricky due to the pre-processing of the length of the name
2640 in the switch statement here. The code here assumes that added
2641 environment variables have a different length. */
2642 #ifdef EXTRA_LD_ENVVARS
2643 EXTRA_LD_ENVVARS
2644 #endif
2645 }
2646 }
2647
2648 /* The caller wants this information. */
2649 *modep = mode;
2650
2651 /* Extra security for SUID binaries. Remove all dangerous environment
2652 variables. */
2653 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
2654 {
2655 static const char unsecure_envvars[] =
2656 #ifdef EXTRA_UNSECURE_ENVVARS
2657 EXTRA_UNSECURE_ENVVARS
2658 #endif
2659 UNSECURE_ENVVARS;
2660 const char *nextp;
2661
2662 nextp = unsecure_envvars;
2663 do
2664 {
2665 unsetenv (nextp);
2666 /* We could use rawmemchr but this need not be fast. */
2667 nextp = (char *) (strchr) (nextp, '\0') + 1;
2668 }
2669 while (*nextp != '\0');
2670
2671 if (__access ("/etc/suid-debug", F_OK) != 0)
2672 {
2673 unsetenv ("MALLOC_CHECK_");
2674 GLRO(dl_debug_mask) = 0;
2675 }
2676
2677 if (mode != normal)
2678 _exit (5);
2679 }
2680 /* If we have to run the dynamic linker in debugging mode and the
2681 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2682 messages to this file. */
2683 else if (any_debug && debug_output != NULL)
2684 {
2685 #ifdef O_NOFOLLOW
2686 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2687 #else
2688 const int flags = O_WRONLY | O_APPEND | O_CREAT;
2689 #endif
2690 size_t name_len = strlen (debug_output);
2691 char buf[name_len + 12];
2692 char *startp;
2693
2694 buf[name_len + 11] = '\0';
2695 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2696 *--startp = '.';
2697 startp = memcpy (startp - name_len, debug_output, name_len);
2698
2699 GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
2700 if (GLRO(dl_debug_fd) == -1)
2701 /* We use standard output if opening the file failed. */
2702 GLRO(dl_debug_fd) = STDOUT_FILENO;
2703 }
2704 }
2705
2706
2707 /* Print the various times we collected. */
2708 static void
2709 __attribute ((noinline))
2710 print_statistics (hp_timing_t *rtld_total_timep)
2711 {
2712 #ifndef HP_TIMING_NONAVAIL
2713 char buf[200];
2714 char *cp;
2715 char *wp;
2716
2717 /* Total time rtld used. */
2718 if (HP_TIMING_AVAIL)
2719 {
2720 HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
2721 _dl_debug_printf ("\nruntime linker statistics:\n"
2722 " total startup time in dynamic loader: %s\n", buf);
2723
2724 /* Print relocation statistics. */
2725 char pbuf[30];
2726 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
2727 cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
2728 pbuf + sizeof (pbuf), 10, 0);
2729 wp = pbuf;
2730 switch (pbuf + sizeof (pbuf) - cp)
2731 {
2732 case 3:
2733 *wp++ = *cp++;
2734 case 2:
2735 *wp++ = *cp++;
2736 case 1:
2737 *wp++ = '.';
2738 *wp++ = *cp++;
2739 }
2740 *wp = '\0';
2741 _dl_debug_printf ("\
2742 time needed for relocation: %s (%s%%)\n", buf, pbuf);
2743 }
2744 #endif
2745
2746 unsigned long int num_relative_relocations = 0;
2747 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2748 {
2749 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2750 continue;
2751
2752 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2753
2754 for (unsigned int i = 0; i < scope->r_nlist; i++)
2755 {
2756 struct link_map *l = scope->r_list [i];
2757
2758 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2759 num_relative_relocations
2760 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2761 #ifndef ELF_MACHINE_REL_RELATIVE
2762 /* Relative relocations are processed on these architectures if
2763 library is loaded to different address than p_vaddr or
2764 if not prelinked. */
2765 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2766 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2767 #else
2768 /* On e.g. IA-64 or Alpha, relative relocations are processed
2769 only if library is loaded to different address than p_vaddr. */
2770 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2771 #endif
2772 num_relative_relocations
2773 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2774 }
2775 }
2776
2777 _dl_debug_printf (" number of relocations: %lu\n"
2778 " number of relocations from cache: %lu\n"
2779 " number of relative relocations: %lu\n",
2780 GL(dl_num_relocations),
2781 GL(dl_num_cache_relocations),
2782 num_relative_relocations);
2783
2784 #ifndef HP_TIMING_NONAVAIL
2785 /* Time spend while loading the object and the dependencies. */
2786 if (HP_TIMING_AVAIL)
2787 {
2788 char pbuf[30];
2789 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
2790 cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
2791 pbuf + sizeof (pbuf), 10, 0);
2792 wp = pbuf;
2793 switch (pbuf + sizeof (pbuf) - cp)
2794 {
2795 case 3:
2796 *wp++ = *cp++;
2797 case 2:
2798 *wp++ = *cp++;
2799 case 1:
2800 *wp++ = '.';
2801 *wp++ = *cp++;
2802 }
2803 *wp = '\0';
2804 _dl_debug_printf ("\
2805 time needed to load objects: %s (%s%%)\n",
2806 buf, pbuf);
2807 }
2808 #endif
2809 }