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