1 /* Support for dynamic linking code in static libc.
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* This file defines some things that for the dynamic linker are defined in
20 rtld.c and dl-sysdep.c in ways appropriate to bootstrap dynamic linking. */
26 #include <sys/param.h>
29 #include <dl-machine.h>
30 #include <bits/libc-lock.h>
32 #include <dl-librecon.h>
33 #include <dl-procinfo.h>
34 #include <dl-hwcap2.h>
35 #include <unsecvars.h>
36 #include <hp-timing.h>
37 #include <stackinfo.h>
39 extern char *__progname
;
40 char **_dl_argv
= &__progname
; /* This is checked for some error messages. */
42 /* Name of the architecture. */
43 const char *_dl_platform
;
44 size_t _dl_platformlen
;
48 ElfW(Addr
) _dl_use_load_bias
= -2;
51 /* If nonzero print warnings about problematic situations. */
54 /* We never do profiling. */
55 const char *_dl_profile
;
56 const char *_dl_profile_output
;
58 /* Names of shared object for which the RUNPATHs and RPATHs should be
60 const char *_dl_inhibit_rpath
;
62 /* The map for the object we will profile. */
63 struct link_map
*_dl_profile_map
;
65 /* This is the address of the last stack address ever used. */
66 void *__libc_stack_end
;
68 /* Path where the binary is found. */
69 const char *_dl_origin_path
;
71 /* Nonzero if runtime lookup should not update the .got/.plt. */
74 /* Namespace information. */
75 struct link_namespaces _dl_ns
[DL_NNS
];
78 /* Incremented whenever something may have been added to dl_loaded. */
79 unsigned long long _dl_load_adds
;
81 /* Fake scope. In dynamically linked binaries this is the scope of the
82 main application but here we don't have something like this. So
83 create a fake scope containing nothing. */
84 struct r_scope_elem _dl_initial_searchlist
;
86 #ifndef HAVE_INLINED_SYSCALLS
87 /* Nonzero during startup. */
88 int _dl_starting_up
= 1;
91 /* Random data provided by the kernel. */
94 /* Get architecture specific initializer. */
95 #include <dl-procinfo.c>
97 /* We expect less than a second for relocation. */
98 #ifdef HP_SMALL_TIMING_AVAIL
99 # undef HP_TIMING_AVAIL
100 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
103 /* Initial value of the CPU clock. */
104 #ifndef HP_TIMING_NONAVAIL
105 hp_timing_t _dl_cpuclock_offset
;
108 void (*_dl_init_static_tls
) (struct link_map
*) = &_dl_nothread_init_static_tls
;
110 size_t _dl_pagesize
= EXEC_PAGESIZE
;
112 int _dl_inhibit_cache
;
114 unsigned int _dl_osversion
;
116 /* All known directories in sorted order. */
117 struct r_search_path_elem
*_dl_all_dirs
;
119 /* All directories after startup. */
120 struct r_search_path_elem
*_dl_init_all_dirs
;
122 /* The object to be initialized first. */
123 struct link_map
*_dl_initfirst
;
125 /* Descriptor to write debug messages to. */
126 int _dl_debug_fd
= STDERR_FILENO
;
128 int _dl_correct_cache_id
= _DL_CACHE_DEFAULT_ID
;
130 ElfW(auxv_t
) *_dl_auxv
;
131 const ElfW(Phdr
) *_dl_phdr
;
133 uint64_t _dl_hwcap
__attribute__ ((nocommon
));
136 uint64_t _dl_hwcap2
__attribute__ ((nocommon
));
139 /* This is not initialized to HWCAP_IMPORTANT, matching the definition
140 of _dl_important_hwcaps, below, where no hwcap strings are ever
141 used. This mask is still used to mediate the lookups in the cache
142 file. Since there is no way to set this nonzero (we don't grok the
143 LD_HWCAP_MASK environment variable here), there is no real point in
144 setting _dl_hwcap nonzero below, but we do anyway. */
145 uint64_t _dl_hwcap_mask
__attribute__ ((nocommon
));
147 /* Prevailing state of the stack. Generally this includes PF_X, indicating it's
148 * executable but this isn't true for all platforms. */
149 ElfW(Word
) _dl_stack_flags
= DEFAULT_STACK_PERMS
;
151 /* If loading a shared object requires that we make the stack executable
152 when it was not, we do it by calling this function.
153 It returns an errno code or zero on success. */
154 int (*_dl_make_stack_executable_hook
) (void **) internal_function
155 = _dl_make_stack_executable
;
158 /* Function in libpthread to wait for termination of lookups. */
159 void (*_dl_wait_lookup_done
) (void);
161 struct dl_scope_free_list
*_dl_scope_free_list
;
163 #ifdef NEED_DL_SYSINFO
164 /* Needed for improved syscall handling on at least x86/Linux. */
165 uintptr_t _dl_sysinfo
= DL_SYSINFO_DEFAULT
;
167 #ifdef NEED_DL_SYSINFO_DSO
168 /* Address of the ELF headers in the vsyscall page. */
169 const ElfW(Ehdr
) *_dl_sysinfo_dso
;
171 struct link_map
*_dl_sysinfo_map
;
173 # include "get-dynamic-info.h"
175 #include "setup-vdso.h"
177 /* During the program run we must not modify the global data of
178 loaded shared object simultanously in two threads. Therefore we
179 protect `_dl_open' and `_dl_close' in dl-close.c.
181 This must be a recursive lock since the initializer function of
182 the loaded object might as well require a call to this function.
183 At this time it is not anymore a problem to modify the tables. */
184 __rtld_lock_define_initialized_recursive (, _dl_load_lock
)
185 /* This lock is used to keep __dl_iterate_phdr from inspecting the
186 list of loaded objects while an object is added to or removed from
188 __rtld_lock_define_initialized_recursive (, _dl_load_write_lock
)
191 #ifdef HAVE_AUX_VECTOR
196 _dl_aux_init (ElfW(auxv_t
) *av
)
203 for (; av
->a_type
!= AT_NULL
; ++av
)
207 if (av
->a_un
.a_val
!= 0)
208 GLRO(dl_pagesize
) = av
->a_un
.a_val
;
211 GLRO(dl_clktck
) = av
->a_un
.a_val
;
214 GL(dl_phdr
) = (const void *) av
->a_un
.a_val
;
217 GL(dl_phnum
) = av
->a_un
.a_val
;
220 GLRO(dl_hwcap
) = (unsigned long int) av
->a_un
.a_val
;
224 GLRO(dl_hwcap2
) = (unsigned long int) av
->a_un
.a_val
;
227 #ifdef NEED_DL_SYSINFO
229 GL(dl_sysinfo
) = av
->a_un
.a_val
;
232 #ifdef NEED_DL_SYSINFO_DSO
233 case AT_SYSINFO_EHDR
:
234 GL(dl_sysinfo_dso
) = (void *) av
->a_un
.a_val
;
238 uid
^= av
->a_un
.a_val
;
242 uid
^= av
->a_un
.a_val
;
246 gid
^= av
->a_un
.a_val
;
250 gid
^= av
->a_un
.a_val
;
255 __libc_enable_secure
= av
->a_un
.a_val
;
256 __libc_enable_secure_decided
= 1;
259 _dl_random
= (void *) av
->a_un
.a_val
;
261 # ifdef DL_PLATFORM_AUXV
267 __libc_enable_secure
= uid
!= 0 || gid
!= 0;
268 __libc_enable_secure_decided
= 1;
276 _dl_non_dynamic_init (void)
279 HP_TIMING_NOW (_dl_cpuclock_offset
);
281 _dl_verbose
= *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
283 /* Set up the data structures for the system-supplied DSO early,
284 so they can influence _dl_init_paths. */
285 setup_vdso (NULL
, NULL
);
287 /* Initialize the data structures for the search paths for shared
289 _dl_init_paths (getenv ("LD_LIBRARY_PATH"));
291 /* Remember the last search directory added at startup. */
292 _dl_init_all_dirs
= GL(dl_all_dirs
);
294 _dl_lazy
= *(getenv ("LD_BIND_NOW") ?: "") == '\0';
296 _dl_bind_not
= *(getenv ("LD_BIND_NOT") ?: "") != '\0';
298 _dl_dynamic_weak
= *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
300 _dl_profile_output
= getenv ("LD_PROFILE_OUTPUT");
301 if (_dl_profile_output
== NULL
|| _dl_profile_output
[0] == '\0')
303 = &"/var/tmp\0/var/profile"[__libc_enable_secure
? 9 : 0];
305 if (__libc_enable_secure
)
307 static const char unsecure_envvars
[] =
309 #ifdef EXTRA_UNSECURE_ENVVARS
310 EXTRA_UNSECURE_ENVVARS
313 const char *cp
= unsecure_envvars
;
315 while (cp
< unsecure_envvars
+ sizeof (unsecure_envvars
))
318 cp
= (const char *) __rawmemchr (cp
, '\0') + 1;
321 if (__access ("/etc/suid-debug", F_OK
) != 0)
322 __unsetenv ("MALLOC_CHECK_");
325 #ifdef DL_PLATFORM_INIT
329 #ifdef DL_OSVERSION_INIT
333 /* Now determine the length of the platform string. */
334 if (_dl_platform
!= NULL
)
335 _dl_platformlen
= strlen (_dl_platform
);
337 /* Scan for a program header telling us the stack is nonexecutable. */
338 if (_dl_phdr
!= NULL
)
339 for (uint_fast16_t i
= 0; i
< _dl_phnum
; ++i
)
340 if (_dl_phdr
[i
].p_type
== PT_GNU_STACK
)
342 _dl_stack_flags
= _dl_phdr
[i
].p_flags
;
347 #ifdef DL_SYSINFO_IMPLEMENTATION
348 DL_SYSINFO_IMPLEMENTATION