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