]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Run time dynamic linker. |
ed20b3d9 | 2 | Copyright (C) 1995-2002, 2003, 2004 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
d66e34cd | 19 | |
7d0b1164 | 20 | #include <errno.h> |
7dea968e | 21 | #include <fcntl.h> |
164a7164 | 22 | #include <stdbool.h> |
d66e34cd | 23 | #include <stdlib.h> |
f51d1dfd | 24 | #include <string.h> |
d66e34cd | 25 | #include <unistd.h> |
2064087b | 26 | #include <sys/mman.h> /* Check if MAP_ANON is defined. */ |
af8bf6bd | 27 | #include <sys/param.h> |
ba9fcb3f | 28 | #include <sys/stat.h> |
a42195db | 29 | #include <ldsodefs.h> |
ce37fa88 | 30 | #include <stdio-common/_itoa.h> |
f21acc89 | 31 | #include <entry.h> |
c94a8080 | 32 | #include <fpu_control.h> |
db276fa1 | 33 | #include <hp-timing.h> |
cf197e41 | 34 | #include <bits/libc-lock.h> |
f5348425 | 35 | #include "dynamic-link.h" |
e2102c14 | 36 | #include "dl-librecon.h" |
74955460 | 37 | #include <unsecvars.h> |
5688da55 UD |
38 | #include <dl-cache.h> |
39 | #include <dl-procinfo.h> | |
5f5843e3 | 40 | #include <tls.h> |
f5348425 | 41 | |
a853022c | 42 | #include <assert.h> |
f5348425 | 43 | |
6ce3881d RM |
44 | /* Avoid PLT use for our local calls at startup. */ |
45 | extern __typeof (__mempcpy) __mempcpy attribute_hidden; | |
46 | ||
47 | /* GCC has mental blocks about _exit. */ | |
48 | extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden; | |
49 | #define _exit exit_internal | |
50 | ||
fd26970f | 51 | /* Helper function to handle errors while resolving symbols. */ |
c84142e8 UD |
52 | static void print_unresolved (int errcode, const char *objname, |
53 | const char *errsting); | |
54 | ||
55 | /* Helper function to handle errors when a version is missing. */ | |
56 | static void print_missing_version (int errcode, const char *objname, | |
57 | const char *errsting); | |
fd26970f | 58 | |
db276fa1 | 59 | /* Print the various times we collected. */ |
392a6b52 | 60 | static void print_statistics (hp_timing_t *total_timep); |
ea278354 UD |
61 | |
62 | /* This is a list of all the modes the dynamic loader can be in. */ | |
63 | enum mode { normal, list, verify, trace }; | |
64 | ||
65 | /* Process all environments variables the dynamic linker must recognize. | |
66 | Since all of them start with `LD_' we are a bit smarter while finding | |
67 | all the entries. */ | |
ba9fcb3f | 68 | static void process_envvars (enum mode *modep); |
ea278354 | 69 | |
392a6b52 | 70 | int _dl_argc attribute_relro attribute_hidden; |
11986c68 UD |
71 | #ifdef DL_ARGV_NOT_RELRO |
72 | char **_dl_argv = NULL; | |
73 | #else | |
697afbe1 | 74 | char **_dl_argv attribute_relro = NULL; |
11986c68 | 75 | #endif |
e6caf4e1 | 76 | INTDEF(_dl_argv) |
5c82e15e UD |
77 | |
78 | /* Nonzero if we were run directly. */ | |
392a6b52 | 79 | unsigned int _dl_skip_args attribute_relro attribute_hidden; |
cf197e41 | 80 | |
39778c6c UD |
81 | /* Set nonzero during loading and initialization of executable and |
82 | libraries, cleared before the executable's entry point runs. This | |
83 | must not be initialized to nonzero, because the unused dynamic | |
84 | linker loaded in for libc.so's "ld.so.1" dep will provide the | |
85 | definition seen by libc.so's initializer; that value must be zero, | |
86 | and will be since that dynamic linker's _dl_start and dl_main will | |
87 | never be called. */ | |
e6caf4e1 UD |
88 | int _dl_starting_up = 0; |
89 | INTVARDEF(_dl_starting_up) | |
39778c6c | 90 | |
d6b5d570 UD |
91 | /* This is the structure which defines all variables global to ld.so |
92 | (except those which cannot be added for some reason). */ | |
5688da55 UD |
93 | struct rtld_global _rtld_global = |
94 | { | |
ccdf0cab UD |
95 | /* Get architecture specific initializer. */ |
96 | #include <dl-procinfo.c> | |
5688da55 | 97 | ._dl_debug_fd = STDERR_FILENO, |
ecdeaac0 RM |
98 | /* Default presumption without further information is executable stack. */ |
99 | ._dl_stack_flags = PF_R|PF_W|PF_X, | |
ffa8d2a0 | 100 | #ifdef _LIBC_REENTRANT |
d3c9f895 | 101 | ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER |
ffa8d2a0 | 102 | #endif |
5688da55 | 103 | }; |
27a754a9 UD |
104 | /* If we would use strong_alias here the compiler would see a |
105 | non-hidden definition. This would undo the effect of the previous | |
106 | declaration. So spell out was strong_alias does plus add the | |
107 | visibility attribute. */ | |
108 | extern struct rtld_global _rtld_local | |
109 | __attribute__ ((alias ("_rtld_global"), visibility ("hidden"))); | |
c0fb8a56 | 110 | |
afdca0f2 UD |
111 | |
112 | /* This variable is similar to _rtld_local, but all values are | |
113 | read-only after relocation. */ | |
114 | struct rtld_global_ro _rtld_global_ro attribute_relro = | |
115 | { | |
116 | #ifdef NEED_DL_SYSINFO | |
117 | ._dl_sysinfo = DL_SYSINFO_DEFAULT, | |
118 | #endif | |
119 | ._dl_use_load_bias = -2, | |
120 | ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID, | |
121 | ._dl_hwcap_mask = HWCAP_IMPORTANT, | |
122 | ._dl_lazy = 1, | |
123 | ._dl_fpu_control = _FPU_DEFAULT, | |
124 | }; | |
125 | /* If we would use strong_alias here the compiler would see a | |
126 | non-hidden definition. This would undo the effect of the previous | |
127 | declaration. So spell out was strong_alias does plus add the | |
128 | visibility attribute. */ | |
129 | extern struct rtld_global_ro _rtld_local_ro | |
130 | __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden"))); | |
131 | ||
132 | ||
67ddea92 | 133 | static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum, |
266180eb | 134 | ElfW(Addr) *user_entry); |
d66e34cd | 135 | |
392a6b52 | 136 | /* These two variables cannot be moved into .data.rel.ro. */ |
d6b5d570 UD |
137 | static struct libname_list _dl_rtld_libname; |
138 | static struct libname_list _dl_rtld_libname2; | |
86d2c878 | 139 | |
eaad82e0 UD |
140 | /* We expect less than a second for relocation. */ |
141 | #ifdef HP_SMALL_TIMING_AVAIL | |
142 | # undef HP_TIMING_AVAIL | |
143 | # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL | |
144 | #endif | |
145 | ||
db276fa1 | 146 | /* Variable for statistics. */ |
5732c4df | 147 | #ifndef HP_TIMING_NONAVAIL |
db276fa1 | 148 | static hp_timing_t relocate_time; |
392a6b52 UD |
149 | static hp_timing_t load_time attribute_relro; |
150 | static hp_timing_t start_time attribute_relro; | |
5732c4df | 151 | #endif |
db276fa1 | 152 | |
2a76f7ef UD |
153 | /* Additional definitions needed by TLS initialization. */ |
154 | #ifdef TLS_INIT_HELPER | |
155 | TLS_INIT_HELPER | |
5e289179 UD |
156 | #endif |
157 | ||
158 | /* Helper function for syscall implementation. */ | |
159 | #ifdef DL_SYSINFO_IMPLEMENTATION | |
160 | DL_SYSINFO_IMPLEMENTATION | |
2a76f7ef UD |
161 | #endif |
162 | ||
01d8e36d UD |
163 | /* Before ld.so is relocated we must not access variables which need |
164 | relocations. This means variables which are exported. Variables | |
165 | declared as static are fine. If we can mark a variable hidden this | |
27a754a9 | 166 | is fine, too. The latter is important here. We can avoid setting |
01d8e36d UD |
167 | up a temporary link map for ld.so if we can mark _rtld_global as |
168 | hidden. */ | |
9df89390 UD |
169 | #if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \ |
170 | && defined HAVE_VISIBILITY_ATTRIBUTE | |
01d8e36d UD |
171 | # define DONT_USE_BOOTSTRAP_MAP 1 |
172 | #endif | |
173 | ||
174 | #ifdef DONT_USE_BOOTSTRAP_MAP | |
175 | static ElfW(Addr) _dl_start_final (void *arg); | |
176 | #else | |
4874b009 RM |
177 | struct dl_start_final_info |
178 | { | |
179 | struct link_map l; | |
180 | #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE | |
181 | hp_timing_t start_time; | |
182 | #endif | |
183 | }; | |
01d8e36d | 184 | static ElfW(Addr) _dl_start_final (void *arg, |
4874b009 | 185 | struct dl_start_final_info *info); |
01d8e36d | 186 | #endif |
6a1db4ff | 187 | |
65da9563 RM |
188 | /* These defined magically in the linker script. */ |
189 | extern char _begin[] attribute_hidden; | |
190 | extern char _end[] attribute_hidden; | |
191 | ||
192 | ||
b1dbbaa4 RM |
193 | #ifdef RTLD_START |
194 | RTLD_START | |
195 | #else | |
eaad82e0 | 196 | # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START" |
b1dbbaa4 RM |
197 | #endif |
198 | ||
c2248c44 RM |
199 | #ifndef VALIDX |
200 | # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ | |
201 | + DT_EXTRANUM + DT_VALTAGIDX (tag)) | |
202 | #endif | |
203 | #ifndef ADDRIDX | |
204 | # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ | |
205 | + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag)) | |
206 | #endif | |
207 | ||
208 | /* This is the second half of _dl_start (below). It can be inlined safely | |
209 | under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT | |
210 | references. When the tools don't permit us to avoid using a GOT entry | |
211 | for _dl_rtld_global (no attribute_hidden support), we must make sure | |
212 | this function is not inlined (see below). */ | |
213 | ||
214 | #ifdef DONT_USE_BOOTSTRAP_MAP | |
215 | static inline ElfW(Addr) __attribute__ ((always_inline)) | |
216 | _dl_start_final (void *arg) | |
217 | #else | |
218 | static ElfW(Addr) __attribute__ ((noinline)) | |
4874b009 | 219 | _dl_start_final (void *arg, struct dl_start_final_info *info) |
c2248c44 RM |
220 | #endif |
221 | { | |
222 | ElfW(Addr) start_addr; | |
c2248c44 RM |
223 | |
224 | if (HP_TIMING_AVAIL) | |
225 | { | |
226 | /* If it hasn't happen yet record the startup time. */ | |
227 | if (! HP_TIMING_INLINE) | |
228 | HP_TIMING_NOW (start_time); | |
735d67f2 | 229 | #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL |
4874b009 RM |
230 | else |
231 | start_time = info->start_time; | |
232 | #endif | |
c2248c44 RM |
233 | |
234 | /* Initialize the timing functions. */ | |
235 | HP_TIMING_DIFF_INIT (); | |
236 | } | |
237 | ||
238 | /* Transfer data about ourselves to the permanent link_map structure. */ | |
239 | #ifndef DONT_USE_BOOTSTRAP_MAP | |
4874b009 RM |
240 | GL(dl_rtld_map).l_addr = info->l.l_addr; |
241 | GL(dl_rtld_map).l_ld = info->l.l_ld; | |
242 | memcpy (GL(dl_rtld_map).l_info, info->l.l_info, | |
c2248c44 | 243 | sizeof GL(dl_rtld_map).l_info); |
4874b009 | 244 | GL(dl_rtld_map).l_mach = info->l.l_mach; |
c2248c44 RM |
245 | #endif |
246 | _dl_setup_hash (&GL(dl_rtld_map)); | |
247 | GL(dl_rtld_map).l_opencount = 1; | |
248 | GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin; | |
249 | GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end; | |
250 | /* Copy the TLS related data if necessary. */ | |
251 | #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP | |
ce460d04 | 252 | # if USE___THREAD |
4874b009 | 253 | assert (info->l.l_tls_modid != 0); |
ce460d04 RM |
254 | GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize; |
255 | GL(dl_rtld_map).l_tls_align = info->l.l_tls_align; | |
99fe3b0e | 256 | GL(dl_rtld_map).l_tls_firstbyte_offset = info->l.l_tls_firstbyte_offset; |
ce460d04 RM |
257 | GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size; |
258 | GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage; | |
259 | GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset; | |
260 | GL(dl_rtld_map).l_tls_modid = 1; | |
c2248c44 | 261 | # else |
ce460d04 | 262 | assert (info->l.l_tls_modid == 0); |
299601a1 UD |
263 | # if NO_TLS_OFFSET != 0 |
264 | GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET; | |
265 | # endif | |
c2248c44 | 266 | # endif |
ce460d04 | 267 | |
c2248c44 RM |
268 | #endif |
269 | ||
270 | #if HP_TIMING_AVAIL | |
271 | HP_TIMING_NOW (GL(dl_cpuclock_offset)); | |
272 | #endif | |
273 | ||
ea4f25a7 UD |
274 | /* Initialize the stack end variable. */ |
275 | __libc_stack_end = __builtin_frame_address (0); | |
276 | ||
c2248c44 RM |
277 | /* Call the OS-dependent function to set up life so we can do things like |
278 | file access. It will call `dl_main' (below) to do all the real work | |
279 | of the dynamic linker, and then unwind our frame and run the user | |
280 | entry point on the same stack we entered on. */ | |
ecdeaac0 | 281 | start_addr = _dl_sysdep_start (arg, &dl_main); |
c2248c44 RM |
282 | |
283 | #ifndef HP_TIMING_NONAVAIL | |
392a6b52 | 284 | hp_timing_t rtld_total_time; |
c2248c44 RM |
285 | if (HP_TIMING_AVAIL) |
286 | { | |
287 | hp_timing_t end_time; | |
288 | ||
289 | /* Get the current time. */ | |
290 | HP_TIMING_NOW (end_time); | |
291 | ||
292 | /* Compute the difference. */ | |
293 | HP_TIMING_DIFF (rtld_total_time, start_time, end_time); | |
294 | } | |
295 | #endif | |
296 | ||
afdca0f2 | 297 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0)) |
39b04aa3 UD |
298 | { |
299 | #ifndef HP_TIMING_NONAVAIL | |
300 | print_statistics (&rtld_total_time); | |
301 | #else | |
302 | print_statistics (NULL); | |
303 | #endif | |
304 | } | |
c2248c44 RM |
305 | |
306 | return start_addr; | |
307 | } | |
308 | ||
50746436 | 309 | static ElfW(Addr) __attribute_used__ internal_function |
d66e34cd RM |
310 | _dl_start (void *arg) |
311 | { | |
01d8e36d UD |
312 | #ifdef DONT_USE_BOOTSTRAP_MAP |
313 | # define bootstrap_map GL(dl_rtld_map) | |
314 | #else | |
4874b009 RM |
315 | struct dl_start_final_info info; |
316 | # define bootstrap_map info.l | |
739d440d | 317 | #endif |
d66e34cd | 318 | |
b1dbbaa4 RM |
319 | /* This #define produces dynamic linking inline functions for |
320 | bootstrap relocation instead of general-purpose relocation. */ | |
321 | #define RTLD_BOOTSTRAP | |
c0282c06 UD |
322 | #define RESOLVE_MAP(sym, version, flags) \ |
323 | ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map) | |
50463d27 UD |
324 | #define RESOLVE(sym, version, flags) \ |
325 | ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr) | |
b1dbbaa4 RM |
326 | #include "dynamic-link.h" |
327 | ||
db276fa1 | 328 | if (HP_TIMING_INLINE && HP_TIMING_AVAIL) |
4874b009 | 329 | #ifdef DONT_USE_BOOTSTRAP_MAP |
db276fa1 | 330 | HP_TIMING_NOW (start_time); |
4874b009 RM |
331 | #else |
332 | HP_TIMING_NOW (info.start_time); | |
333 | #endif | |
db276fa1 | 334 | |
e66d0a4c UD |
335 | /* Partly clean the `bootstrap_map' structure up. Don't use |
336 | `memset' since it might not be built in or inlined and we cannot | |
337 | make function calls at this point. Use '__builtin_memset' if we | |
01d8e36d UD |
338 | know it is available. We do not have to clear the memory if we |
339 | do not have to use the temporary bootstrap_map. Global variables | |
340 | are initialized to zero by default. */ | |
341 | #ifndef DONT_USE_BOOTSTRAP_MAP | |
342 | # ifdef HAVE_BUILTIN_MEMSET | |
e66d0a4c | 343 | __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info)); |
01d8e36d | 344 | # else |
ce460d04 | 345 | for (size_t cnt = 0; |
264ec183 UD |
346 | cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]); |
347 | ++cnt) | |
348 | bootstrap_map.l_info[cnt] = 0; | |
01d8e36d | 349 | # endif |
e66d0a4c | 350 | #endif |
264ec183 | 351 | |
d66e34cd | 352 | /* Figure out the run-time load address of the dynamic linker itself. */ |
86d2c878 | 353 | bootstrap_map.l_addr = elf_machine_load_address (); |
d66e34cd | 354 | |
47707456 UD |
355 | /* Read our own dynamic section and fill in the info array. */ |
356 | bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); | |
479aa8ec | 357 | elf_get_dynamic_info (&bootstrap_map, NULL); |
d66e34cd | 358 | |
299601a1 UD |
359 | #if defined USE_TLS && NO_TLS_OFFSET != 0 |
360 | bootstrap_map.l_tls_offset = NO_TLS_OFFSET; | |
361 | #endif | |
362 | ||
65da9563 RM |
363 | /* Get the dynamic linker's own program header. First we need the ELF |
364 | file header. The `_begin' symbol created by the linker script points | |
365 | to it. When we have something like GOTOFF relocs, we can use a plain | |
366 | reference to find the runtime address. Without that, we have to rely | |
367 | on the `l_addr' value, which is not the value we want when prelinked. */ | |
ed20b3d9 | 368 | #if USE___THREAD |
5f5843e3 | 369 | dtv_t initdtv[3]; |
ce460d04 RM |
370 | ElfW(Ehdr) *ehdr |
371 | # ifdef DONT_USE_BOOTSTRAP_MAP | |
372 | = (ElfW(Ehdr) *) &_begin; | |
373 | # else | |
e8ed861d | 374 | # error This will not work with prelink. |
ce460d04 RM |
375 | = (ElfW(Ehdr) *) bootstrap_map.l_addr; |
376 | # endif | |
377 | ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff); | |
378 | size_t cnt = ehdr->e_phnum; /* PT_TLS is usually the last phdr. */ | |
379 | while (cnt-- > 0) | |
739d440d UD |
380 | if (phdr[cnt].p_type == PT_TLS) |
381 | { | |
382 | void *tlsblock; | |
383 | size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align); | |
f884de5b | 384 | char *p; |
739d440d UD |
385 | |
386 | bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz; | |
387 | bootstrap_map.l_tls_align = phdr[cnt].p_align; | |
99fe3b0e UD |
388 | if (phdr[cnt].p_align == 0) |
389 | bootstrap_map.l_tls_firstbyte_offset = 0; | |
390 | else | |
391 | bootstrap_map.l_tls_firstbyte_offset = (phdr[cnt].p_vaddr | |
392 | & (phdr[cnt].p_align - 1)); | |
739d440d UD |
393 | assert (bootstrap_map.l_tls_blocksize != 0); |
394 | bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz; | |
395 | bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr | |
a816b435 | 396 | + phdr[cnt].p_vaddr); |
739d440d UD |
397 | |
398 | /* We can now allocate the initial TLS block. This can happen | |
399 | on the stack. We'll get the final memory later when we | |
400 | know all about the various objects loaded at startup | |
401 | time. */ | |
402 | # if TLS_TCB_AT_TP | |
403 | tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize, | |
404 | TLS_INIT_TCB_ALIGN) | |
405 | + TLS_INIT_TCB_SIZE | |
406 | + max_align); | |
407 | # elif TLS_DTV_AT_TP | |
408 | tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE, | |
409 | bootstrap_map.l_tls_align) | |
410 | + bootstrap_map.l_tls_blocksize | |
411 | + max_align); | |
412 | # else | |
413 | /* In case a model with a different layout for the TCB and DTV | |
414 | is defined add another #elif here and in the following #ifs. */ | |
415 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
416 | # endif | |
417 | /* Align the TLS block. */ | |
418 | tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1) | |
419 | & ~(max_align - 1)); | |
420 | ||
421 | /* Initialize the dtv. [0] is the length, [1] the generation | |
422 | counter. */ | |
423 | initdtv[0].counter = 1; | |
424 | initdtv[1].counter = 0; | |
425 | ||
426 | /* Initialize the TLS block. */ | |
427 | # if TLS_TCB_AT_TP | |
428 | initdtv[2].pointer = tlsblock; | |
429 | # elif TLS_DTV_AT_TP | |
430 | bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE, | |
431 | bootstrap_map.l_tls_align); | |
432 | initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset; | |
433 | # else | |
434 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
435 | # endif | |
f884de5b UD |
436 | p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage, |
437 | bootstrap_map.l_tls_initimage_size); | |
438 | # ifdef HAVE_BUILTIN_MEMSET | |
439 | __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize | |
440 | - bootstrap_map.l_tls_initimage_size)); | |
441 | # else | |
442 | { | |
443 | size_t remaining = (bootstrap_map.l_tls_blocksize | |
444 | - bootstrap_map.l_tls_initimage_size); | |
445 | while (remaining-- > 0) | |
446 | *p++ = '\0'; | |
447 | } | |
448 | #endif | |
739d440d UD |
449 | |
450 | /* Install the pointer to the dtv. */ | |
451 | ||
452 | /* Initialize the thread pointer. */ | |
453 | # if TLS_TCB_AT_TP | |
454 | bootstrap_map.l_tls_offset | |
455 | = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN); | |
456 | ||
457 | INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset, | |
458 | initdtv); | |
459 | ||
fde89ad0 RM |
460 | const char *lossage = TLS_INIT_TP ((char *) tlsblock |
461 | + bootstrap_map.l_tls_offset, 0); | |
739d440d UD |
462 | # elif TLS_DTV_AT_TP |
463 | INSTALL_DTV (tlsblock, initdtv); | |
fde89ad0 | 464 | const char *lossage = TLS_INIT_TP (tlsblock, 0); |
739d440d UD |
465 | # else |
466 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" | |
467 | # endif | |
fde89ad0 RM |
468 | if (__builtin_expect (lossage != NULL, 0)) |
469 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", | |
470 | lossage); | |
739d440d UD |
471 | |
472 | /* So far this is module number one. */ | |
473 | bootstrap_map.l_tls_modid = 1; | |
e8ed861d UD |
474 | |
475 | /* There can only be one PT_TLS entry. */ | |
476 | break; | |
739d440d | 477 | } |
ce460d04 | 478 | #endif /* USE___THREAD */ |
739d440d | 479 | |
d66e34cd | 480 | #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC |
86d2c878 | 481 | ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info); |
d66e34cd RM |
482 | #endif |
483 | ||
32e6df36 UD |
484 | if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)]) |
485 | { | |
486 | /* Relocate ourselves so we can do normal function calls and | |
487 | data access using the global offset table. */ | |
488 | ||
489 | ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0); | |
490 | } | |
421f82e5 | 491 | |
ea7eb7e3 UD |
492 | /* Please note that we don't allow profiling of this object and |
493 | therefore need not test whether we have to allocate the array | |
494 | for the relocation results (as done in dl-reloc.c). */ | |
421f82e5 | 495 | |
d66e34cd RM |
496 | /* Now life is sane; we can call functions and access global data. |
497 | Set up to use the operating system facilities, and find out from | |
498 | the operating system's program loader where to find the program | |
6a1db4ff UD |
499 | header table in core. Put the rest of _dl_start into a separate |
500 | function, that way the compiler cannot put accesses to the GOT | |
501 | before ELF_DYNAMIC_RELOCATE. */ | |
c0282c06 | 502 | { |
01d8e36d UD |
503 | #ifdef DONT_USE_BOOTSTRAP_MAP |
504 | ElfW(Addr) entry = _dl_start_final (arg); | |
505 | #else | |
4874b009 | 506 | ElfW(Addr) entry = _dl_start_final (arg, &info); |
01d8e36d | 507 | #endif |
c0282c06 UD |
508 | |
509 | #ifndef ELF_MACHINE_START_ADDRESS | |
510 | # define ELF_MACHINE_START_ADDRESS(map, start) (start) | |
511 | #endif | |
512 | ||
d6b5d570 | 513 | return ELF_MACHINE_START_ADDRESS (GL(dl_loaded), entry); |
c0282c06 | 514 | } |
6a1db4ff UD |
515 | } |
516 | ||
517 | ||
d66e34cd | 518 | |
d66e34cd RM |
519 | /* Now life is peachy; we can do all normal operations. |
520 | On to the real work. */ | |
521 | ||
993b3242 UD |
522 | /* Some helper functions. */ |
523 | ||
524 | /* Arguments to relocate_doit. */ | |
525 | struct relocate_args | |
526 | { | |
527 | struct link_map *l; | |
528 | int lazy; | |
529 | }; | |
530 | ||
531 | struct map_args | |
532 | { | |
533 | /* Argument to map_doit. */ | |
534 | char *str; | |
f04b9a68 UD |
535 | struct link_map *loader; |
536 | int is_preloaded; | |
537 | int mode; | |
993b3242 | 538 | /* Return value of map_doit. */ |
f04b9a68 | 539 | struct link_map *map; |
993b3242 UD |
540 | }; |
541 | ||
542 | /* Arguments to version_check_doit. */ | |
543 | struct version_check_args | |
544 | { | |
993b3242 | 545 | int doexit; |
145b8413 | 546 | int dotrace; |
993b3242 UD |
547 | }; |
548 | ||
549 | static void | |
550 | relocate_doit (void *a) | |
551 | { | |
552 | struct relocate_args *args = (struct relocate_args *) a; | |
553 | ||
cff26a3e | 554 | INTUSE(_dl_relocate_object) (args->l, args->l->l_scope, args->lazy, 0); |
993b3242 UD |
555 | } |
556 | ||
557 | static void | |
558 | map_doit (void *a) | |
559 | { | |
be935610 | 560 | struct map_args *args = (struct map_args *) a; |
f04b9a68 UD |
561 | args->map = INTUSE(_dl_map_object) (args->loader, args->str, |
562 | args->is_preloaded, lt_library, 0, | |
563 | args->mode); | |
993b3242 UD |
564 | } |
565 | ||
566 | static void | |
567 | version_check_doit (void *a) | |
568 | { | |
be935610 | 569 | struct version_check_args *args = (struct version_check_args *) a; |
d6b5d570 | 570 | if (_dl_check_all_versions (GL(dl_loaded), 1, args->dotrace) && args->doexit) |
993b3242 UD |
571 | /* We cannot start the application. Abort now. */ |
572 | _exit (1); | |
573 | } | |
574 | ||
ce37fa88 UD |
575 | |
576 | static inline struct link_map * | |
577 | find_needed (const char *name) | |
578 | { | |
d6b5d570 | 579 | unsigned int n = GL(dl_loaded)->l_searchlist.r_nlist; |
ce37fa88 | 580 | |
be935610 | 581 | while (n-- > 0) |
d6b5d570 UD |
582 | if (_dl_name_match_p (name, GL(dl_loaded)->l_searchlist.r_list[n])) |
583 | return GL(dl_loaded)->l_searchlist.r_list[n]; | |
ce37fa88 UD |
584 | |
585 | /* Should never happen. */ | |
586 | return NULL; | |
587 | } | |
588 | ||
589 | static int | |
590 | match_version (const char *string, struct link_map *map) | |
591 | { | |
a42195db | 592 | const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
ce37fa88 UD |
593 | ElfW(Verdef) *def; |
594 | ||
b0982c4a | 595 | #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF)) |
ce37fa88 UD |
596 | if (map->l_info[VERDEFTAG] == NULL) |
597 | /* The file has no symbol versioning. */ | |
598 | return 0; | |
599 | ||
600 | def = (ElfW(Verdef) *) ((char *) map->l_addr | |
601 | + map->l_info[VERDEFTAG]->d_un.d_ptr); | |
602 | while (1) | |
603 | { | |
604 | ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux); | |
605 | ||
606 | /* Compare the version strings. */ | |
607 | if (strcmp (string, strtab + aux->vda_name) == 0) | |
608 | /* Bingo! */ | |
609 | return 1; | |
610 | ||
611 | /* If no more definitions we failed to find what we want. */ | |
612 | if (def->vd_next == 0) | |
613 | break; | |
614 | ||
615 | /* Next definition. */ | |
616 | def = (ElfW(Verdef) *) ((char *) def + def->vd_next); | |
617 | } | |
618 | ||
619 | return 0; | |
620 | } | |
621 | ||
bf2cc5fb | 622 | #ifdef _LIBC_REENTRANT |
ce460d04 RM |
623 | /* _dl_error_catch_tsd points to this for the single-threaded case. |
624 | It's reset by the thread library for multithreaded programs. */ | |
216455bc RM |
625 | void ** __attribute__ ((const)) |
626 | _dl_initial_error_catch_tsd (void) | |
ce460d04 RM |
627 | { |
628 | static void *data; | |
629 | return &data; | |
630 | } | |
bf2cc5fb | 631 | #endif |
ce460d04 | 632 | |
334fcf2a UD |
633 | #if defined SHARED && defined _LIBC_REENTRANT \ |
634 | && defined __rtld_lock_default_lock_recursive | |
635 | static void rtld_lock_default_lock_recursive (void *lock) | |
636 | { | |
637 | __rtld_lock_default_lock_recursive (lock); | |
638 | } | |
639 | ||
640 | static void rtld_lock_default_unlock_recursive (void *lock) | |
641 | { | |
642 | __rtld_lock_default_unlock_recursive (lock); | |
643 | } | |
644 | #endif | |
645 | ||
646 | ||
392a6b52 UD |
647 | /* The library search path. */ |
648 | static const char *library_path attribute_relro; | |
649 | /* The list preloaded objects. */ | |
650 | static const char *preloadlist attribute_relro; | |
651 | /* Nonzero if information about versions has to be printed. */ | |
652 | static int version_info attribute_relro; | |
a1a9d215 | 653 | |
d66e34cd | 654 | static void |
266180eb | 655 | dl_main (const ElfW(Phdr) *phdr, |
72f70279 | 656 | ElfW(Word) phnum, |
266180eb | 657 | ElfW(Addr) *user_entry) |
d66e34cd | 658 | { |
266180eb | 659 | const ElfW(Phdr) *ph; |
ea278354 | 660 | enum mode mode; |
2064087b RM |
661 | struct link_map **preloads; |
662 | unsigned int npreloads; | |
14bab8de UD |
663 | size_t file_size; |
664 | char *file; | |
164a7164 | 665 | bool has_interp = false; |
77aba05b | 666 | unsigned int i; |
164a7164 UD |
667 | bool prelinked = false; |
668 | bool rtld_is_main = false; | |
5732c4df | 669 | #ifndef HP_TIMING_NONAVAIL |
db276fa1 UD |
670 | hp_timing_t start; |
671 | hp_timing_t stop; | |
672 | hp_timing_t diff; | |
5732c4df | 673 | #endif |
a52d1562 UD |
674 | #ifdef USE_TLS |
675 | void *tcbp; | |
676 | #endif | |
d66e34cd | 677 | |
bf2cc5fb | 678 | #ifdef _LIBC_REENTRANT |
ce460d04 | 679 | /* Explicit initialization since the reloc would just be more work. */ |
216455bc | 680 | GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd; |
bf2cc5fb | 681 | #endif |
ce460d04 | 682 | |
adc12574 UD |
683 | #ifdef USE_TLS |
684 | GL(dl_init_static_tls) = &_dl_nothread_init_static_tls; | |
685 | #endif | |
686 | ||
334fcf2a UD |
687 | #if defined SHARED && defined _LIBC_REENTRANT \ |
688 | && defined __rtld_lock_default_lock_recursive | |
689 | GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive; | |
690 | GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive; | |
691 | #endif | |
692 | ||
c70ba488 RM |
693 | /* The explicit initialization here is cheaper than processing the reloc |
694 | in the _rtld_local definition's initializer. */ | |
695 | GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable; | |
696 | ||
ea278354 | 697 | /* Process the environment variable which control the behaviour. */ |
ba9fcb3f | 698 | process_envvars (&mode); |
3996f34b | 699 | |
46ec036d | 700 | /* Set up a flag which tells we are just starting. */ |
e6caf4e1 | 701 | INTUSE(_dl_starting_up) = 1; |
46ec036d | 702 | |
a16956f3 | 703 | if (*user_entry == (ElfW(Addr)) ENTRY_POINT) |
0200214b RM |
704 | { |
705 | /* Ho ho. We are not the program interpreter! We are the program | |
706 | itself! This means someone ran ld.so as a command. Well, that | |
707 | might be convenient to do sometimes. We support it by | |
708 | interpreting the args like this: | |
709 | ||
710 | ld.so PROGRAM ARGS... | |
711 | ||
712 | The first argument is the name of a file containing an ELF | |
713 | executable we will load and run with the following arguments. | |
714 | To simplify life here, PROGRAM is searched for using the | |
715 | normal rules for shared objects, rather than $PATH or anything | |
716 | like that. We just load it and use its entry point; we don't | |
717 | pay attention to its PT_INTERP command (we are the interpreter | |
718 | ourselves). This is an easy way to test a new ld.so before | |
719 | installing it. */ | |
164a7164 | 720 | rtld_is_main = true; |
421f82e5 | 721 | |
ffee1316 | 722 | /* Note the place where the dynamic linker actually came from. */ |
e6caf4e1 | 723 | GL(dl_rtld_map).l_name = rtld_progname; |
6a76c115 | 724 | |
fd26970f | 725 | while (_dl_argc > 1) |
e6caf4e1 | 726 | if (! strcmp (INTUSE(_dl_argv)[1], "--list")) |
fd26970f UD |
727 | { |
728 | mode = list; | |
afdca0f2 | 729 | GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */ |
61965e9b | 730 | |
fd26970f UD |
731 | ++_dl_skip_args; |
732 | --_dl_argc; | |
e6caf4e1 | 733 | ++INTUSE(_dl_argv); |
fd26970f | 734 | } |
e6caf4e1 | 735 | else if (! strcmp (INTUSE(_dl_argv)[1], "--verify")) |
fd26970f UD |
736 | { |
737 | mode = verify; | |
6a76c115 | 738 | |
fd26970f UD |
739 | ++_dl_skip_args; |
740 | --_dl_argc; | |
e6caf4e1 | 741 | ++INTUSE(_dl_argv); |
fd26970f | 742 | } |
e6caf4e1 UD |
743 | else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path") |
744 | && _dl_argc > 2) | |
880f421f | 745 | { |
e6caf4e1 | 746 | library_path = INTUSE(_dl_argv)[2]; |
880f421f | 747 | |
310930c1 UD |
748 | _dl_skip_args += 2; |
749 | _dl_argc -= 2; | |
e6caf4e1 | 750 | INTUSE(_dl_argv) += 2; |
310930c1 | 751 | } |
e6caf4e1 UD |
752 | else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath") |
753 | && _dl_argc > 2) | |
310930c1 | 754 | { |
afdca0f2 | 755 | GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2]; |
310930c1 | 756 | |
880f421f UD |
757 | _dl_skip_args += 2; |
758 | _dl_argc -= 2; | |
e6caf4e1 | 759 | INTUSE(_dl_argv) += 2; |
880f421f | 760 | } |
fd26970f UD |
761 | else |
762 | break; | |
d66e34cd | 763 | |
61eb22d3 UD |
764 | /* If we have no further argument the program was called incorrectly. |
765 | Grant the user some education. */ | |
766 | if (_dl_argc < 2) | |
35fc382a | 767 | _dl_fatal_printf ("\ |
2bcf29ba | 768 | Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ |
61eb22d3 UD |
769 | You have invoked `ld.so', the helper program for shared library executables.\n\ |
770 | This program usually lives in the file `/lib/ld.so', and special directives\n\ | |
771 | in executable files using ELF shared libraries tell the system's program\n\ | |
772 | loader to load the helper program from this file. This helper program loads\n\ | |
773 | the shared libraries needed by the program executable, prepares the program\n\ | |
774 | to run, and runs it. You may invoke this helper program directly from the\n\ | |
775 | command line to load and run an ELF executable file; this is like executing\n\ | |
776 | that file itself, but always uses this helper program from the file you\n\ | |
777 | specified, instead of the helper program file specified in the executable\n\ | |
778 | file you run. This is mostly of use for maintainers to test new versions\n\ | |
2bcf29ba UD |
779 | of this helper program; chances are you did not intend to run this program.\n\ |
780 | \n\ | |
b0a01055 UD |
781 | --list list all dependencies and how they are resolved\n\ |
782 | --verify verify that given object really is a dynamically linked\n\ | |
e8b1163e | 783 | object we can handle\n\ |
b0a01055 UD |
784 | --library-path PATH use given PATH instead of content of the environment\n\ |
785 | variable LD_LIBRARY_PATH\n\ | |
fcf70d41 | 786 | --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\ |
35fc382a | 787 | in LIST\n"); |
61eb22d3 | 788 | |
0200214b RM |
789 | ++_dl_skip_args; |
790 | --_dl_argc; | |
e6caf4e1 | 791 | ++INTUSE(_dl_argv); |
91f62ce6 | 792 | |
da832465 UD |
793 | /* Initialize the data structures for the search paths for shared |
794 | objects. */ | |
795 | _dl_init_paths (library_path); | |
796 | ||
c70ba488 RM |
797 | /* The initialization of _dl_stack_flags done below assumes the |
798 | executable's PT_GNU_STACK may have been honored by the kernel, and | |
799 | so a PT_GNU_STACK with PF_X set means the stack started out with | |
800 | execute permission. However, this is not really true if the | |
801 | dynamic linker is the executable the kernel loaded. For this | |
802 | case, we must reinitialize _dl_stack_flags to match the dynamic | |
803 | linker itself. If the dynamic linker was built with a | |
804 | PT_GNU_STACK, then the kernel may have loaded us with a | |
805 | nonexecutable stack that we will have to make executable when we | |
806 | load the program below unless it has a PT_GNU_STACK indicating | |
807 | nonexecutable stack is ok. */ | |
808 | ||
809 | for (ph = phdr; ph < &phdr[phnum]; ++ph) | |
810 | if (ph->p_type == PT_GNU_STACK) | |
811 | { | |
812 | GL(dl_stack_flags) = ph->p_flags; | |
813 | break; | |
814 | } | |
815 | ||
9a821cf9 | 816 | if (__builtin_expect (mode, normal) == verify) |
2de99474 | 817 | { |
8e17ea58 UD |
818 | const char *objname; |
819 | const char *err_str = NULL; | |
993b3242 | 820 | struct map_args args; |
2de99474 | 821 | |
e6caf4e1 | 822 | args.str = rtld_progname; |
f04b9a68 UD |
823 | args.loader = NULL; |
824 | args.is_preloaded = 0; | |
825 | args.mode = __RTLD_OPENEXEC; | |
cff26a3e | 826 | (void) INTUSE(_dl_catch_error) (&objname, &err_str, map_doit, &args); |
8e17ea58 | 827 | if (__builtin_expect (err_str != NULL, 0)) |
e6caf4e1 UD |
828 | /* We don't free the returned string, the programs stops |
829 | anyway. */ | |
830 | _exit (EXIT_FAILURE); | |
2de99474 UD |
831 | } |
832 | else | |
db276fa1 UD |
833 | { |
834 | HP_TIMING_NOW (start); | |
5a4b5076 UD |
835 | INTUSE(_dl_map_object) (NULL, rtld_progname, 0, lt_library, 0, |
836 | __RTLD_OPENEXEC); | |
db276fa1 | 837 | HP_TIMING_NOW (stop); |
61e0617a | 838 | |
db276fa1 UD |
839 | HP_TIMING_DIFF (load_time, start, stop); |
840 | } | |
2de99474 | 841 | |
d6b5d570 UD |
842 | phdr = GL(dl_loaded)->l_phdr; |
843 | phnum = GL(dl_loaded)->l_phnum; | |
143e2b96 UD |
844 | /* We overwrite here a pointer to a malloc()ed string. But since |
845 | the malloc() implementation used at this point is the dummy | |
846 | implementations which has no real free() function it does not | |
847 | makes sense to free the old string first. */ | |
d6b5d570 UD |
848 | GL(dl_loaded)->l_name = (char *) ""; |
849 | *user_entry = GL(dl_loaded)->l_entry; | |
0200214b RM |
850 | } |
851 | else | |
852 | { | |
853 | /* Create a link_map for the executable itself. | |
854 | This will be what dlopen on "" returns. */ | |
87c812c2 | 855 | _dl_new_object ((char *) "", "", lt_executable, NULL); |
d6b5d570 | 856 | if (GL(dl_loaded) == NULL) |
35fc382a | 857 | _dl_fatal_printf ("cannot allocate memory for link map\n"); |
d6b5d570 UD |
858 | GL(dl_loaded)->l_phdr = phdr; |
859 | GL(dl_loaded)->l_phnum = phnum; | |
860 | GL(dl_loaded)->l_entry = *user_entry; | |
da832465 | 861 | |
61e0617a UD |
862 | /* At this point we are in a bit of trouble. We would have to |
863 | fill in the values for l_dev and l_ino. But in general we | |
864 | do not know where the file is. We also do not handle AT_EXECFD | |
865 | even if it would be passed up. | |
866 | ||
867 | We leave the values here defined to 0. This is normally no | |
868 | problem as the program code itself is normally no shared | |
869 | object and therefore cannot be loaded dynamically. Nothing | |
870 | prevent the use of dynamic binaries and in these situations | |
871 | we might get problems. We might not be able to find out | |
872 | whether the object is already loaded. But since there is no | |
873 | easy way out and because the dynamic binary must also not | |
874 | have an SONAME we ignore this program for now. If it becomes | |
875 | a problem we can force people using SONAMEs. */ | |
876 | ||
97a51d8a UD |
877 | /* We delay initializing the path structure until we got the dynamic |
878 | information for the program. */ | |
0200214b RM |
879 | } |
880 | ||
d6b5d570 | 881 | GL(dl_loaded)->l_map_end = 0; |
052b6a6c | 882 | /* Perhaps the executable has no PT_LOAD header entries at all. */ |
d6b5d570 | 883 | GL(dl_loaded)->l_map_start = ~0; |
e7beef5f | 884 | /* We opened the file, account for it. */ |
d6b5d570 | 885 | ++GL(dl_loaded)->l_opencount; |
052b6a6c | 886 | |
0200214b | 887 | /* Scan the program header table for the dynamic section. */ |
72f70279 | 888 | for (ph = phdr; ph < &phdr[phnum]; ++ph) |
0200214b RM |
889 | switch (ph->p_type) |
890 | { | |
da832465 UD |
891 | case PT_PHDR: |
892 | /* Find out the load address. */ | |
d6b5d570 | 893 | GL(dl_loaded)->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr; |
da832465 | 894 | break; |
0200214b RM |
895 | case PT_DYNAMIC: |
896 | /* This tells us where to find the dynamic section, | |
897 | which tells us everything we need to do. */ | |
d6b5d570 | 898 | GL(dl_loaded)->l_ld = (void *) GL(dl_loaded)->l_addr + ph->p_vaddr; |
0200214b RM |
899 | break; |
900 | case PT_INTERP: | |
901 | /* This "interpreter segment" was used by the program loader to | |
902 | find the program interpreter, which is this program itself, the | |
903 | dynamic linker. We note what name finds us, so that a future | |
904 | dlopen call or DT_NEEDED entry, for something that wants to link | |
905 | against the dynamic linker as a shared library, will know that | |
906 | the shared object is already loaded. */ | |
d6b5d570 | 907 | _dl_rtld_libname.name = ((const char *) GL(dl_loaded)->l_addr |
be935610 | 908 | + ph->p_vaddr); |
752a2a50 | 909 | /* _dl_rtld_libname.next = NULL; Already zero. */ |
d6b5d570 | 910 | GL(dl_rtld_map).l_libname = &_dl_rtld_libname; |
f41c8091 UD |
911 | |
912 | /* Ordinarilly, we would get additional names for the loader from | |
913 | our DT_SONAME. This can't happen if we were actually linked as | |
914 | a static executable (detect this case when we have no DYNAMIC). | |
915 | If so, assume the filename component of the interpreter path to | |
916 | be our SONAME, and add it to our name list. */ | |
d6b5d570 | 917 | if (GL(dl_rtld_map).l_ld == NULL) |
f41c8091 | 918 | { |
88794e30 UD |
919 | const char *p = NULL; |
920 | const char *cp = _dl_rtld_libname.name; | |
921 | ||
922 | /* Find the filename part of the path. */ | |
923 | while (*cp != '\0') | |
924 | if (*cp++ == '/') | |
925 | p = cp; | |
926 | ||
927 | if (p != NULL) | |
f41c8091 | 928 | { |
88794e30 | 929 | _dl_rtld_libname2.name = p; |
752a2a50 | 930 | /* _dl_rtld_libname2.next = NULL; Already zero. */ |
f41c8091 UD |
931 | _dl_rtld_libname.next = &_dl_rtld_libname2; |
932 | } | |
933 | } | |
934 | ||
164a7164 | 935 | has_interp = true; |
0200214b | 936 | break; |
052b6a6c | 937 | case PT_LOAD: |
052b6a6c UD |
938 | { |
939 | ElfW(Addr) mapstart; | |
2373b30e UD |
940 | ElfW(Addr) allocend; |
941 | ||
942 | /* Remember where the main program starts in memory. */ | |
d6b5d570 UD |
943 | mapstart = (GL(dl_loaded)->l_addr |
944 | + (ph->p_vaddr & ~(ph->p_align - 1))); | |
945 | if (GL(dl_loaded)->l_map_start > mapstart) | |
946 | GL(dl_loaded)->l_map_start = mapstart; | |
2373b30e UD |
947 | |
948 | /* Also where it ends. */ | |
d6b5d570 UD |
949 | allocend = GL(dl_loaded)->l_addr + ph->p_vaddr + ph->p_memsz; |
950 | if (GL(dl_loaded)->l_map_end < allocend) | |
951 | GL(dl_loaded)->l_map_end = allocend; | |
052b6a6c UD |
952 | } |
953 | break; | |
a2f1f5cb UD |
954 | #ifdef USE_TLS |
955 | case PT_TLS: | |
aed283dd UD |
956 | if (ph->p_memsz > 0) |
957 | { | |
958 | /* Note that in the case the dynamic linker we duplicate work | |
959 | here since we read the PT_TLS entry already in | |
960 | _dl_start_final. But the result is repeatable so do not | |
961 | check for this special but unimportant case. */ | |
962 | GL(dl_loaded)->l_tls_blocksize = ph->p_memsz; | |
963 | GL(dl_loaded)->l_tls_align = ph->p_align; | |
99fe3b0e UD |
964 | if (ph->p_align == 0) |
965 | GL(dl_loaded)->l_tls_firstbyte_offset = 0; | |
966 | else | |
967 | GL(dl_loaded)->l_tls_firstbyte_offset = (ph->p_vaddr | |
968 | & (ph->p_align - 1)); | |
aed283dd UD |
969 | GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz; |
970 | GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr; | |
971 | ||
972 | /* This image gets the ID one. */ | |
973 | GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1; | |
974 | } | |
a2f1f5cb UD |
975 | break; |
976 | #endif | |
ecdeaac0 RM |
977 | case PT_GNU_STACK: |
978 | GL(dl_stack_flags) = ph->p_flags; | |
979 | break; | |
e8ed861d UD |
980 | |
981 | case PT_GNU_RELRO: | |
982 | GL(dl_loaded)->l_relro_addr = ph->p_vaddr; | |
983 | GL(dl_loaded)->l_relro_size = ph->p_memsz; | |
984 | break; | |
0200214b | 985 | } |
75aafc71 RM |
986 | #ifdef USE_TLS |
987 | /* Adjust the address of the TLS initialization image in case | |
988 | the executable is actually an ET_DYN object. */ | |
989 | if (GL(dl_loaded)->l_tls_initimage != NULL) | |
990 | GL(dl_loaded)->l_tls_initimage | |
991 | = (char *) GL(dl_loaded)->l_tls_initimage + GL(dl_loaded)->l_addr; | |
992 | #endif | |
d6b5d570 UD |
993 | if (! GL(dl_loaded)->l_map_end) |
994 | GL(dl_loaded)->l_map_end = ~0; | |
995 | if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name) | |
c84142e8 UD |
996 | { |
997 | /* We were invoked directly, so the program might not have a | |
998 | PT_INTERP. */ | |
d6b5d570 | 999 | _dl_rtld_libname.name = GL(dl_rtld_map).l_name; |
3fb55878 | 1000 | /* _dl_rtld_libname.next = NULL; Already zero. */ |
d6b5d570 | 1001 | GL(dl_rtld_map).l_libname = &_dl_rtld_libname; |
c84142e8 | 1002 | } |
ffee1316 | 1003 | else |
d6b5d570 | 1004 | assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */ |
0200214b | 1005 | |
9a51759b UD |
1006 | if (! rtld_is_main) |
1007 | { | |
1008 | /* Extract the contents of the dynamic section for easy access. */ | |
479aa8ec | 1009 | elf_get_dynamic_info (GL(dl_loaded), NULL); |
d6b5d570 | 1010 | if (GL(dl_loaded)->l_info[DT_HASH]) |
9a51759b | 1011 | /* Set up our cache of pointers into the hash table. */ |
d6b5d570 | 1012 | _dl_setup_hash (GL(dl_loaded)); |
9a51759b | 1013 | } |
0200214b | 1014 | |
9a821cf9 | 1015 | if (__builtin_expect (mode, normal) == verify) |
e2102c14 UD |
1016 | { |
1017 | /* We were called just to verify that this is a dynamic | |
1018 | executable using us as the program interpreter. Exit with an | |
1019 | error if we were not able to load the binary or no interpreter | |
1020 | is specified (i.e., this is no dynamically linked binary. */ | |
d6b5d570 | 1021 | if (GL(dl_loaded)->l_ld == NULL) |
e2102c14 | 1022 | _exit (1); |
e2102c14 UD |
1023 | |
1024 | /* We allow here some platform specific code. */ | |
1025 | #ifdef DISTINGUISH_LIB_VERSIONS | |
1026 | DISTINGUISH_LIB_VERSIONS; | |
1027 | #endif | |
eb406346 | 1028 | _exit (has_interp ? 0 : 2); |
e2102c14 UD |
1029 | } |
1030 | ||
9a51759b | 1031 | if (! rtld_is_main) |
97a51d8a UD |
1032 | /* Initialize the data structures for the search paths for shared |
1033 | objects. */ | |
120b4c49 | 1034 | _dl_init_paths (library_path); |
97a51d8a | 1035 | |
0200214b | 1036 | /* Put the link_map for ourselves on the chain so it can be found by |
ceb2d9aa | 1037 | name. Note that at this point the global chain of link maps contains |
d6b5d570 UD |
1038 | exactly one element, which is pointed to by dl_loaded. */ |
1039 | if (! GL(dl_rtld_map).l_name) | |
ffee1316 RM |
1040 | /* If not invoked directly, the dynamic linker shared object file was |
1041 | found by the PT_INTERP name. */ | |
d6b5d570 UD |
1042 | GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name; |
1043 | GL(dl_rtld_map).l_type = lt_library; | |
1044 | GL(dl_loaded)->l_next = &GL(dl_rtld_map); | |
1045 | GL(dl_rtld_map).l_prev = GL(dl_loaded); | |
1046 | ++GL(dl_nloaded); | |
e8ed861d | 1047 | ++GL(dl_load_adds); |
0200214b | 1048 | |
97fd3a30 UD |
1049 | /* If LD_USE_LOAD_BIAS env variable has not been seen, default |
1050 | to not using bias for non-prelinked PIEs and libraries | |
1051 | and using it for executables or prelinked PIEs or libraries. */ | |
afdca0f2 UD |
1052 | if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2) |
1053 | GLRO(dl_use_load_bias) = (GL(dl_loaded)->l_addr == 0) ? -1 : 0; | |
97fd3a30 | 1054 | |
553eca26 UD |
1055 | /* Set up the program header information for the dynamic linker |
1056 | itself. It is needed in the dl_iterate_phdr() callbacks. */ | |
4cfde896 | 1057 | ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start; |
e8ed861d UD |
1058 | ElfW(Phdr) *rtld_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start |
1059 | + rtld_ehdr->e_phoff); | |
1060 | GL(dl_rtld_map).l_phdr = rtld_phdr; | |
553eca26 UD |
1061 | GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum; |
1062 | ||
e8ed861d UD |
1063 | /* PT_GNU_RELRO is usually the last phdr. */ |
1064 | size_t cnt = rtld_ehdr->e_phnum; | |
1065 | while (cnt-- > 0) | |
1066 | if (rtld_phdr[cnt].p_type == PT_GNU_RELRO) | |
1067 | { | |
1068 | GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr; | |
1069 | GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz; | |
1070 | break; | |
1071 | } | |
1072 | ||
14bab8de | 1073 | /* We have two ways to specify objects to preload: via environment |
49c091e5 | 1074 | variable and via the file /etc/ld.so.preload. The latter can also |
14bab8de | 1075 | be used when security is enabled. */ |
2064087b RM |
1076 | preloads = NULL; |
1077 | npreloads = 0; | |
14bab8de | 1078 | |
db33f7d4 | 1079 | if (__builtin_expect (preloadlist != NULL, 0)) |
c4029823 | 1080 | { |
566efee2 UD |
1081 | /* The LD_PRELOAD environment variable gives list of libraries |
1082 | separated by white space or colons that are loaded before the | |
fd26970f UD |
1083 | executable's dependencies and prepended to the global scope |
1084 | list. If the binary is running setuid all elements | |
1085 | containing a '/' are ignored since it is insecure. */ | |
1086 | char *list = strdupa (preloadlist); | |
1087 | char *p; | |
db276fa1 UD |
1088 | |
1089 | HP_TIMING_NOW (start); | |
1090 | ||
9710f75d UD |
1091 | /* Prevent optimizing strsep. Speed is not important here. */ |
1092 | while ((p = (strsep) (&list, " :")) != NULL) | |
e2102c14 | 1093 | if (p[0] != '\0' |
e6caf4e1 | 1094 | && (__builtin_expect (! INTUSE(__libc_enable_secure), 1) |
db33f7d4 | 1095 | || strchr (p, '/') == NULL)) |
fd26970f | 1096 | { |
87837aac UD |
1097 | struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded), |
1098 | p, 1, | |
1099 | lt_library, | |
cff26a3e | 1100 | 0, 0); |
42c4f32a | 1101 | if (++new_map->l_opencount == 1) |
bd355af0 UD |
1102 | /* It is no duplicate. */ |
1103 | ++npreloads; | |
fd26970f | 1104 | } |
db276fa1 UD |
1105 | |
1106 | HP_TIMING_NOW (stop); | |
1107 | HP_TIMING_DIFF (diff, start, stop); | |
1108 | HP_TIMING_ACCUM_NT (load_time, diff); | |
c4029823 UD |
1109 | } |
1110 | ||
14bab8de | 1111 | /* Read the contents of the file. */ |
f04b9a68 UD |
1112 | const char preload_file[] = "/etc/ld.so.preload"; |
1113 | file = _dl_sysdep_read_whole_file (preload_file, &file_size, | |
14bab8de | 1114 | PROT_READ | PROT_WRITE); |
40b07f5b | 1115 | if (__builtin_expect (file != MAP_FAILED, 0)) |
14bab8de UD |
1116 | { |
1117 | /* Parse the file. It contains names of libraries to be loaded, | |
1118 | separated by white spaces or `:'. It may also contain | |
1119 | comments introduced by `#'. */ | |
1120 | char *problem; | |
1121 | char *runp; | |
1122 | size_t rest; | |
1123 | ||
1124 | /* Eliminate comments. */ | |
1125 | runp = file; | |
1126 | rest = file_size; | |
1127 | while (rest > 0) | |
1128 | { | |
1129 | char *comment = memchr (runp, '#', rest); | |
1130 | if (comment == NULL) | |
1131 | break; | |
1132 | ||
1133 | rest -= comment - runp; | |
1134 | do | |
1135 | *comment = ' '; | |
1136 | while (--rest > 0 && *++comment != '\n'); | |
1137 | } | |
1138 | ||
1139 | /* We have one problematic case: if we have a name at the end of | |
1140 | the file without a trailing terminating characters, we cannot | |
1141 | place the \0. Handle the case separately. */ | |
49891c10 UD |
1142 | if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t' |
1143 | && file[file_size - 1] != '\n' && file[file_size - 1] != ':') | |
14bab8de UD |
1144 | { |
1145 | problem = &file[file_size]; | |
1146 | while (problem > file && problem[-1] != ' ' && problem[-1] != '\t' | |
49891c10 | 1147 | && problem[-1] != '\n' && problem[-1] != ':') |
14bab8de UD |
1148 | --problem; |
1149 | ||
1150 | if (problem > file) | |
1151 | problem[-1] = '\0'; | |
1152 | } | |
1153 | else | |
49891c10 UD |
1154 | { |
1155 | problem = NULL; | |
1156 | file[file_size - 1] = '\0'; | |
1157 | } | |
14bab8de | 1158 | |
db276fa1 UD |
1159 | HP_TIMING_NOW (start); |
1160 | ||
14bab8de UD |
1161 | if (file != problem) |
1162 | { | |
1163 | char *p; | |
e2102c14 | 1164 | runp = file; |
14bab8de | 1165 | while ((p = strsep (&runp, ": \t\n")) != NULL) |
e2102c14 UD |
1166 | if (p[0] != '\0') |
1167 | { | |
f04b9a68 UD |
1168 | const char *objname; |
1169 | const char *err_str = NULL; | |
1170 | struct map_args args; | |
1171 | ||
1172 | args.str = p; | |
1173 | args.loader = GL(dl_loaded); | |
1174 | args.is_preloaded = 1; | |
1175 | args.mode = 0; | |
1176 | ||
1177 | (void) INTUSE(_dl_catch_error) (&objname, &err_str, map_doit, | |
1178 | &args); | |
1179 | if (__builtin_expect (err_str != NULL, 0)) | |
1180 | { | |
1181 | _dl_error_printf ("\ | |
1182 | ERROR: ld.so: object '%s' from %s cannot be preloaded: ignored.\n", | |
1183 | p, preload_file); | |
1184 | /* No need to call free, this is still before the libc's | |
1185 | malloc is used. */ | |
1186 | } | |
1187 | else if (++args.map->l_opencount == 1) | |
e2102c14 UD |
1188 | /* It is no duplicate. */ |
1189 | ++npreloads; | |
1190 | } | |
14bab8de UD |
1191 | } |
1192 | ||
1193 | if (problem != NULL) | |
1194 | { | |
1195 | char *p = strndupa (problem, file_size - (problem - file)); | |
87837aac UD |
1196 | struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded), p, |
1197 | 1, lt_library, | |
1198 | 0, 0); | |
42c4f32a | 1199 | if (++new_map->l_opencount == 1) |
bd355af0 UD |
1200 | /* It is no duplicate. */ |
1201 | ++npreloads; | |
14bab8de UD |
1202 | } |
1203 | ||
db276fa1 UD |
1204 | HP_TIMING_NOW (stop); |
1205 | HP_TIMING_DIFF (diff, start, stop); | |
1206 | HP_TIMING_ACCUM_NT (load_time, diff); | |
1207 | ||
14bab8de UD |
1208 | /* We don't need the file anymore. */ |
1209 | __munmap (file, file_size); | |
1210 | } | |
1211 | ||
db33f7d4 | 1212 | if (__builtin_expect (npreloads, 0) != 0) |
14bab8de UD |
1213 | { |
1214 | /* Set up PRELOADS with a vector of the preloaded libraries. */ | |
1215 | struct link_map *l; | |
14bab8de | 1216 | preloads = __alloca (npreloads * sizeof preloads[0]); |
d6b5d570 | 1217 | l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */ |
14bab8de UD |
1218 | i = 0; |
1219 | do | |
1220 | { | |
1221 | preloads[i++] = l; | |
1222 | l = l->l_next; | |
1223 | } while (l); | |
1224 | assert (i == npreloads); | |
1225 | } | |
1226 | ||
f866314b | 1227 | #ifdef NEED_DL_SYSINFO |
3b3ddb4f | 1228 | struct link_map *sysinfo_map = NULL; |
afdca0f2 | 1229 | if (GLRO(dl_sysinfo_dso) != NULL) |
f866314b | 1230 | { |
f866314b UD |
1231 | /* Do an abridged version of the work _dl_map_object_from_fd would do |
1232 | to map in the object. It's already mapped and prelinked (and | |
1233 | better be, since it's read-only and so we couldn't relocate it). | |
1234 | We just want our data structures to describe it as if we had just | |
1235 | mapped and relocated it normally. */ | |
1236 | struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL); | |
1237 | if (__builtin_expect (l != NULL, 1)) | |
1238 | { | |
f556dbfa | 1239 | static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT]; |
479aa8ec | 1240 | |
afdca0f2 UD |
1241 | l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso) |
1242 | + GLRO(dl_sysinfo_dso)->e_phoff); | |
1243 | l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum; | |
f866314b UD |
1244 | for (uint_fast16_t i = 0; i < l->l_phnum; ++i) |
1245 | { | |
1246 | const ElfW(Phdr) *const ph = &l->l_phdr[i]; | |
1247 | if (ph->p_type == PT_DYNAMIC) | |
1248 | { | |
1249 | l->l_ld = (void *) ph->p_vaddr; | |
1250 | l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); | |
f866314b | 1251 | } |
3b3ddb4f | 1252 | else if (ph->p_type == PT_LOAD) |
c776b3d7 | 1253 | { |
3b3ddb4f UD |
1254 | if (! l->l_addr) |
1255 | l->l_addr = ph->p_vaddr; | |
1256 | else if (ph->p_vaddr + ph->p_memsz >= l->l_map_end) | |
1257 | l->l_map_end = ph->p_vaddr + ph->p_memsz; | |
c776b3d7 | 1258 | } |
f866314b | 1259 | } |
afdca0f2 | 1260 | l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso); |
3b3ddb4f UD |
1261 | l->l_addr = l->l_map_start - l->l_addr; |
1262 | l->l_map_end += l->l_addr; | |
49ad572a | 1263 | l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr); |
479aa8ec | 1264 | elf_get_dynamic_info (l, dyn_temp); |
f866314b UD |
1265 | _dl_setup_hash (l); |
1266 | l->l_relocated = 1; | |
1267 | ||
1268 | /* Now that we have the info handy, use the DSO image's soname | |
b49329d9 RM |
1269 | so this object can be looked up by name. Note that we do not |
1270 | set l_name here. That field gives the file name of the DSO, | |
1271 | and this DSO is not associated with any file. */ | |
f866314b | 1272 | if (l->l_info[DT_SONAME] != NULL) |
f556dbfa UD |
1273 | { |
1274 | /* Work around a kernel problem. The kernel cannot handle | |
1275 | addresses in the vsyscall DSO pages in writev() calls. */ | |
1276 | const char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB]) | |
1277 | + l->l_info[DT_SONAME]->d_un.d_val); | |
1278 | size_t len = strlen (dsoname); | |
b49329d9 RM |
1279 | char *copy = malloc (len); |
1280 | if (copy == NULL) | |
f556dbfa | 1281 | _dl_fatal_printf ("out of memory\n"); |
b49329d9 | 1282 | l->l_libname->name = memcpy (copy, dsoname, len); |
f556dbfa | 1283 | } |
3b3ddb4f UD |
1284 | |
1285 | /* We have a prelinked DSO preloaded by the system. */ | |
afdca0f2 UD |
1286 | if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT) |
1287 | GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr; | |
3b3ddb4f | 1288 | sysinfo_map = l; |
f866314b UD |
1289 | } |
1290 | } | |
1291 | #endif | |
1292 | ||
2064087b RM |
1293 | /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD |
1294 | specified some libraries to load, these are inserted before the actual | |
1295 | dependencies in the executable's searchlist for symbol resolution. */ | |
db276fa1 | 1296 | HP_TIMING_NOW (start); |
87837aac UD |
1297 | INTUSE(_dl_map_object_deps) (GL(dl_loaded), preloads, npreloads, |
1298 | mode == trace, 0); | |
db276fa1 UD |
1299 | HP_TIMING_NOW (stop); |
1300 | HP_TIMING_DIFF (diff, start, stop); | |
1301 | HP_TIMING_ACCUM_NT (load_time, diff); | |
e3e35cfc | 1302 | |
42c4f32a UD |
1303 | /* Mark all objects as being in the global scope and set the open |
1304 | counter. */ | |
d6b5d570 | 1305 | for (i = GL(dl_loaded)->l_searchlist.r_nlist; i > 0; ) |
42c4f32a UD |
1306 | { |
1307 | --i; | |
d6b5d570 UD |
1308 | GL(dl_loaded)->l_searchlist.r_list[i]->l_global = 1; |
1309 | ++GL(dl_loaded)->l_searchlist.r_list[i]->l_opencount; | |
42c4f32a | 1310 | } |
d66e34cd | 1311 | |
2064087b | 1312 | #ifndef MAP_ANON |
f332db02 RM |
1313 | /* We are done mapping things, so close the zero-fill descriptor. */ |
1314 | __close (_dl_zerofd); | |
1315 | _dl_zerofd = -1; | |
2064087b | 1316 | #endif |
f332db02 | 1317 | |
f9496a7b | 1318 | /* Remove _dl_rtld_map from the chain. */ |
d6b5d570 UD |
1319 | GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next; |
1320 | if (GL(dl_rtld_map).l_next) | |
1321 | GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev; | |
f9496a7b | 1322 | |
d6b5d570 | 1323 | if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1)) |
0200214b | 1324 | { |
f9496a7b RM |
1325 | /* Some DT_NEEDED entry referred to the interpreter object itself, so |
1326 | put it back in the list of visible objects. We insert it into the | |
1327 | chain in symbol search order because gdb uses the chain's order as | |
1328 | its symbol search order. */ | |
77aba05b | 1329 | i = 1; |
d6b5d570 | 1330 | while (GL(dl_loaded)->l_searchlist.r_list[i] != &GL(dl_rtld_map)) |
f9496a7b | 1331 | ++i; |
d6b5d570 | 1332 | GL(dl_rtld_map).l_prev = GL(dl_loaded)->l_searchlist.r_list[i - 1]; |
b2bcd61a | 1333 | if (__builtin_expect (mode, normal) == normal) |
3b3ddb4f UD |
1334 | { |
1335 | GL(dl_rtld_map).l_next = (i + 1 < GL(dl_loaded)->l_searchlist.r_nlist | |
1336 | ? GL(dl_loaded)->l_searchlist.r_list[i + 1] | |
1337 | : NULL); | |
1338 | #ifdef NEED_DL_SYSINFO | |
1339 | if (sysinfo_map != NULL | |
1340 | && GL(dl_rtld_map).l_prev->l_next == sysinfo_map | |
1341 | && GL(dl_rtld_map).l_next != sysinfo_map) | |
1342 | GL(dl_rtld_map).l_prev = sysinfo_map; | |
1343 | #endif | |
1344 | } | |
b2bcd61a UD |
1345 | else |
1346 | /* In trace mode there might be an invisible object (which we | |
1347 | could not find) after the previous one in the search list. | |
1348 | In this case it doesn't matter much where we put the | |
1349 | interpreter object, so we just initialize the list pointer so | |
1350 | that the assertion below holds. */ | |
d6b5d570 | 1351 | GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next; |
b2bcd61a | 1352 | |
d6b5d570 UD |
1353 | assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next); |
1354 | GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map); | |
3fb55878 | 1355 | if (GL(dl_rtld_map).l_next != NULL) |
f9496a7b | 1356 | { |
d6b5d570 UD |
1357 | assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev); |
1358 | GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map); | |
f9496a7b | 1359 | } |
0200214b | 1360 | } |
d66e34cd | 1361 | |
c84142e8 UD |
1362 | /* Now let us see whether all libraries are available in the |
1363 | versions we need. */ | |
1364 | { | |
993b3242 UD |
1365 | struct version_check_args args; |
1366 | args.doexit = mode == normal; | |
145b8413 | 1367 | args.dotrace = mode == trace; |
993b3242 | 1368 | _dl_receive_error (print_missing_version, version_check_doit, &args); |
c84142e8 UD |
1369 | } |
1370 | ||
bff334e0 UD |
1371 | #ifdef USE_TLS |
1372 | /* Now it is time to determine the layout of the static TLS block | |
1373 | and allocate it for the initial thread. Note that we always | |
1374 | allocate the static block, we never defer it even if no | |
1375 | DF_STATIC_TLS bit is set. The reason is that we know glibc will | |
1376 | use the static model. First add the dynamic linker to the list | |
1377 | if it also uses TLS. */ | |
1378 | if (GL(dl_rtld_map).l_tls_blocksize != 0) | |
1379 | /* Assign a module ID. */ | |
1380 | GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid (); | |
1381 | ||
131fd126 UD |
1382 | # ifndef TLS_INIT_TP_EXPENSIVE |
1383 | # define TLS_INIT_TP_EXPENSIVE 0 | |
1384 | # endif | |
1385 | ||
2d148689 RM |
1386 | /* We do not initialize any of the TLS functionality unless any of the |
1387 | initial modules uses TLS. This makes dynamic loading of modules with | |
1388 | TLS impossible, but to support it requires either eagerly doing setup | |
1389 | now or lazily doing it later. Doing it now makes us incompatible with | |
1390 | an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever | |
1391 | used. Trying to do it lazily is too hairy to try when there could be | |
1392 | multiple threads (from a non-TLS-using libpthread). */ | |
bbe35eb5 | 1393 | if (!TLS_INIT_TP_EXPENSIVE || GL(dl_tls_max_dtv_idx) > 0) |
bff334e0 UD |
1394 | { |
1395 | struct link_map *l; | |
1396 | size_t nelem; | |
1397 | struct dtv_slotinfo *slotinfo; | |
1398 | ||
1399 | /* Number of elements in the static TLS block. */ | |
1400 | GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx); | |
1401 | ||
1402 | /* Allocate the array which contains the information about the | |
1403 | dtv slots. We allocate a few entries more than needed to | |
1404 | avoid the need for reallocation. */ | |
1405 | nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS; | |
1406 | ||
1407 | /* Allocate. */ | |
1408 | GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *) | |
1409 | malloc (sizeof (struct dtv_slotinfo_list) | |
1410 | + nelem * sizeof (struct dtv_slotinfo)); | |
1411 | /* No need to check the return value. If memory allocation failed | |
1412 | the program would have been terminated. */ | |
1413 | ||
1414 | slotinfo = memset (GL(dl_tls_dtv_slotinfo_list)->slotinfo, '\0', | |
1415 | nelem * sizeof (struct dtv_slotinfo)); | |
1416 | GL(dl_tls_dtv_slotinfo_list)->len = nelem; | |
1417 | GL(dl_tls_dtv_slotinfo_list)->next = NULL; | |
1418 | ||
1419 | /* Fill in the information from the loaded modules. */ | |
1420 | for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next) | |
1421 | if (l->l_tls_blocksize != 0) | |
1422 | /* This is a module with TLS data. Store the map reference. | |
1423 | The generation counter is zero. */ | |
1424 | slotinfo[++i].map = l; | |
1425 | assert (i == GL(dl_tls_max_dtv_idx)); | |
1426 | ||
216455bc | 1427 | /* Compute the TLS offsets for the various blocks. */ |
bff334e0 UD |
1428 | _dl_determine_tlsoffset (); |
1429 | ||
1430 | /* Construct the static TLS block and the dtv for the initial | |
1431 | thread. For some platforms this will include allocating memory | |
1432 | for the thread descriptor. The memory for the TLS block will | |
1433 | never be freed. It should be allocated accordingly. The dtv | |
1434 | array can be changed if dynamic loading requires it. */ | |
a816b435 | 1435 | tcbp = _dl_allocate_tls_storage (); |
bff334e0 UD |
1436 | if (tcbp == NULL) |
1437 | _dl_fatal_printf ("\ | |
935f95dd | 1438 | cannot allocate TLS data structures for initial thread"); |
08da0621 RM |
1439 | |
1440 | /* Store for detection of the special case by __tls_get_addr | |
1441 | so it knows not to pass this dtv to the normal realloc. */ | |
8784cc18 | 1442 | GL(dl_initial_dtv) = GET_DTV (tcbp); |
bff334e0 UD |
1443 | } |
1444 | #endif | |
1445 | ||
9a821cf9 | 1446 | if (__builtin_expect (mode, normal) != normal) |
0200214b RM |
1447 | { |
1448 | /* We were run just to list the shared libraries. It is | |
1449 | important that we do this before real relocation, because the | |
1450 | functions we call below for output may no longer work properly | |
1451 | after relocation. */ | |
81f3ac4c UD |
1452 | struct link_map *l; |
1453 | ||
afdca0f2 | 1454 | if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) |
ceb2d9aa | 1455 | { |
81f3ac4c | 1456 | struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist; |
ceb2d9aa | 1457 | |
81f3ac4c | 1458 | for (i = 0; i < scope->r_nlist; i++) |
32e6df36 | 1459 | { |
81f3ac4c UD |
1460 | l = scope->r_list [i]; |
1461 | if (l->l_faked) | |
32e6df36 | 1462 | { |
81f3ac4c UD |
1463 | _dl_printf ("\t%s => not found\n", l->l_libname->name); |
1464 | continue; | |
1465 | } | |
afdca0f2 UD |
1466 | if (_dl_name_match_p (GLRO(dl_trace_prelink), l)) |
1467 | GLRO(dl_trace_prelink_map) = l; | |
81f3ac4c UD |
1468 | _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)", |
1469 | l->l_libname->name[0] ? l->l_libname->name | |
1470 | : rtld_progname ?: "<main program>", | |
1471 | l->l_name[0] ? l->l_name | |
1472 | : rtld_progname ?: "<main program>", | |
d347a4ab UD |
1473 | (int) sizeof l->l_map_start * 2, |
1474 | (size_t) l->l_map_start, | |
1475 | (int) sizeof l->l_addr * 2, | |
1476 | (size_t) l->l_addr); | |
bff334e0 | 1477 | #ifdef USE_TLS |
81f3ac4c UD |
1478 | if (l->l_tls_modid) |
1479 | _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid, | |
1480 | (int) sizeof l->l_tls_offset * 2, | |
d347a4ab | 1481 | (size_t) l->l_tls_offset); |
81f3ac4c | 1482 | else |
bff334e0 | 1483 | #endif |
81f3ac4c | 1484 | _dl_printf ("\n"); |
32e6df36 | 1485 | } |
ceb2d9aa | 1486 | } |
81f3ac4c UD |
1487 | else if (! GL(dl_loaded)->l_info[DT_NEEDED]) |
1488 | _dl_printf ("\tstatically linked\n"); | |
1489 | else | |
1490 | { | |
1491 | for (l = GL(dl_loaded)->l_next; l; l = l->l_next) | |
1492 | if (l->l_faked) | |
1493 | /* The library was not found. */ | |
1494 | _dl_printf ("\t%s => not found\n", l->l_libname->name); | |
1495 | else | |
1496 | _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name, | |
1497 | l->l_name, (int) sizeof l->l_map_start * 2, | |
d347a4ab | 1498 | (size_t) l->l_map_start); |
81f3ac4c | 1499 | } |
1a3a58fd | 1500 | |
9a821cf9 | 1501 | if (__builtin_expect (mode, trace) != trace) |
5a47e7f2 | 1502 | for (i = 1; i < (unsigned int) _dl_argc; ++i) |
cddcfecf RM |
1503 | { |
1504 | const ElfW(Sym) *ref = NULL; | |
c0282c06 UD |
1505 | ElfW(Addr) loadbase; |
1506 | lookup_t result; | |
c0282c06 | 1507 | |
e6caf4e1 UD |
1508 | result = INTUSE(_dl_lookup_symbol) (INTUSE(_dl_argv)[i], |
1509 | GL(dl_loaded), | |
cff26a3e AJ |
1510 | &ref, GL(dl_loaded)->l_scope, |
1511 | ELF_RTYPE_CLASS_PLT, 1); | |
c0282c06 UD |
1512 | |
1513 | loadbase = LOOKUP_VALUE_ADDRESS (result); | |
1514 | ||
35fc382a | 1515 | _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n", |
e6caf4e1 | 1516 | INTUSE(_dl_argv)[i], |
d347a4ab UD |
1517 | (int) sizeof ref->st_value * 2, |
1518 | (size_t) ref->st_value, | |
1519 | (int) sizeof loadbase * 2, (size_t) loadbase); | |
cddcfecf | 1520 | } |
ce37fa88 | 1521 | else |
fd26970f | 1522 | { |
667b0577 | 1523 | /* If LD_WARN is set warn about undefined symbols. */ |
afdca0f2 | 1524 | if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose)) |
ce37fa88 UD |
1525 | { |
1526 | /* We have to do symbol dependency testing. */ | |
1527 | struct relocate_args args; | |
1528 | struct link_map *l; | |
993b3242 | 1529 | |
afdca0f2 | 1530 | args.lazy = GLRO(dl_lazy); |
fd26970f | 1531 | |
d6b5d570 | 1532 | l = GL(dl_loaded); |
ce37fa88 UD |
1533 | while (l->l_next) |
1534 | l = l->l_next; | |
1535 | do | |
1536 | { | |
d6b5d570 | 1537 | if (l != &GL(dl_rtld_map) && ! l->l_faked) |
ce37fa88 UD |
1538 | { |
1539 | args.l = l; | |
1540 | _dl_receive_error (print_unresolved, relocate_doit, | |
1541 | &args); | |
ce37fa88 UD |
1542 | } |
1543 | l = l->l_prev; | |
1544 | } while (l); | |
32e6df36 | 1545 | |
afdca0f2 | 1546 | if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) |
d6b5d570 | 1547 | && GL(dl_rtld_map).l_opencount > 1) |
cff26a3e AJ |
1548 | INTUSE(_dl_relocate_object) (&GL(dl_rtld_map), |
1549 | GL(dl_loaded)->l_scope, 0, 0); | |
ce37fa88 UD |
1550 | } |
1551 | ||
b0982c4a | 1552 | #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED)) |
120b4c49 | 1553 | if (version_info) |
fd26970f | 1554 | { |
ce37fa88 UD |
1555 | /* Print more information. This means here, print information |
1556 | about the versions needed. */ | |
1557 | int first = 1; | |
d6b5d570 | 1558 | struct link_map *map = GL(dl_loaded); |
ce37fa88 | 1559 | |
d6b5d570 | 1560 | for (map = GL(dl_loaded); map != NULL; map = map->l_next) |
fd26970f | 1561 | { |
f41c8091 | 1562 | const char *strtab; |
ce37fa88 | 1563 | ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG]; |
f41c8091 UD |
1564 | ElfW(Verneed) *ent; |
1565 | ||
1566 | if (dyn == NULL) | |
1567 | continue; | |
1568 | ||
a42195db | 1569 | strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
f41c8091 | 1570 | ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr); |
ce37fa88 | 1571 | |
f41c8091 | 1572 | if (first) |
ce37fa88 | 1573 | { |
35fc382a | 1574 | _dl_printf ("\n\tVersion information:\n"); |
f41c8091 UD |
1575 | first = 0; |
1576 | } | |
ce37fa88 | 1577 | |
35fc382a | 1578 | _dl_printf ("\t%s:\n", |
e6caf4e1 | 1579 | map->l_name[0] ? map->l_name : rtld_progname); |
f41c8091 UD |
1580 | |
1581 | while (1) | |
1582 | { | |
1583 | ElfW(Vernaux) *aux; | |
1584 | struct link_map *needed; | |
ce37fa88 | 1585 | |
f41c8091 UD |
1586 | needed = find_needed (strtab + ent->vn_file); |
1587 | aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux); | |
ce37fa88 UD |
1588 | |
1589 | while (1) | |
1590 | { | |
f41c8091 UD |
1591 | const char *fname = NULL; |
1592 | ||
f41c8091 | 1593 | if (needed != NULL |
ba9fcb3f UD |
1594 | && match_version (strtab + aux->vna_name, |
1595 | needed)) | |
f41c8091 UD |
1596 | fname = needed->l_name; |
1597 | ||
35fc382a UD |
1598 | _dl_printf ("\t\t%s (%s) %s=> %s\n", |
1599 | strtab + ent->vn_file, | |
1600 | strtab + aux->vna_name, | |
1601 | aux->vna_flags & VER_FLG_WEAK | |
1602 | ? "[WEAK] " : "", | |
1603 | fname ?: "not found"); | |
ce37fa88 | 1604 | |
f41c8091 UD |
1605 | if (aux->vna_next == 0) |
1606 | /* No more symbols. */ | |
ce37fa88 UD |
1607 | break; |
1608 | ||
f41c8091 UD |
1609 | /* Next symbol. */ |
1610 | aux = (ElfW(Vernaux) *) ((char *) aux | |
1611 | + aux->vna_next); | |
ce37fa88 | 1612 | } |
f41c8091 UD |
1613 | |
1614 | if (ent->vn_next == 0) | |
1615 | /* No more dependencies. */ | |
1616 | break; | |
1617 | ||
1618 | /* Next dependency. */ | |
1619 | ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next); | |
ce37fa88 | 1620 | } |
fd26970f | 1621 | } |
ce37fa88 | 1622 | } |
fd26970f | 1623 | } |
d66e34cd | 1624 | |
0200214b RM |
1625 | _exit (0); |
1626 | } | |
86d2c878 | 1627 | |
d6b5d570 | 1628 | if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)] |
afdca0f2 | 1629 | && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)) |
32e6df36 UD |
1630 | { |
1631 | ElfW(Lib) *liblist, *liblistend; | |
1632 | struct link_map **r_list, **r_listend, *l; | |
9710f75d UD |
1633 | const char *strtab = (const void *) D_PTR (GL(dl_loaded), |
1634 | l_info[DT_STRTAB]); | |
32e6df36 | 1635 | |
d6b5d570 | 1636 | assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL); |
32e6df36 | 1637 | liblist = (ElfW(Lib) *) |
d6b5d570 | 1638 | GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr; |
32e6df36 UD |
1639 | liblistend = (ElfW(Lib) *) |
1640 | ((char *) liblist | |
d6b5d570 UD |
1641 | + GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val); |
1642 | r_list = GL(dl_loaded)->l_searchlist.r_list; | |
1643 | r_listend = r_list + GL(dl_loaded)->l_searchlist.r_nlist; | |
32e6df36 UD |
1644 | |
1645 | for (; r_list < r_listend && liblist < liblistend; r_list++) | |
1646 | { | |
1647 | l = *r_list; | |
1648 | ||
d6b5d570 | 1649 | if (l == GL(dl_loaded)) |
32e6df36 UD |
1650 | continue; |
1651 | ||
1652 | /* If the library is not mapped where it should, fail. */ | |
1653 | if (l->l_addr) | |
1654 | break; | |
1655 | ||
1656 | /* Next, check if checksum matches. */ | |
1657 | if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL | |
1658 | || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val | |
1659 | != liblist->l_checksum) | |
1660 | break; | |
1661 | ||
1662 | if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL | |
1663 | || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val | |
1664 | != liblist->l_time_stamp) | |
1665 | break; | |
1666 | ||
1667 | if (! _dl_name_match_p (strtab + liblist->l_name, l)) | |
1668 | break; | |
1669 | ||
1670 | ++liblist; | |
1671 | } | |
1672 | ||
1673 | ||
1674 | if (r_list == r_listend && liblist == liblistend) | |
164a7164 | 1675 | prelinked = true; |
32e6df36 | 1676 | |
afdca0f2 | 1677 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)) |
32e6df36 UD |
1678 | _dl_printf ("\nprelink checking: %s\n", prelinked ? "ok" : "failed"); |
1679 | } | |
1680 | ||
ed20b3d9 UD |
1681 | |
1682 | /* Initialize _r_debug. */ | |
1683 | struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr); | |
1684 | { | |
1685 | struct link_map *l; | |
1686 | ||
1687 | l = GL(dl_loaded); | |
1688 | ||
1689 | #ifdef ELF_MACHINE_DEBUG_SETUP | |
1690 | ||
1691 | /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */ | |
1692 | ||
1693 | ELF_MACHINE_DEBUG_SETUP (l, r); | |
1694 | ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r); | |
1695 | ||
1696 | #else | |
1697 | ||
1698 | if (l->l_info[DT_DEBUG] != NULL) | |
1699 | /* There is a DT_DEBUG entry in the dynamic section. Fill it in | |
1700 | with the run-time address of the r_debug structure */ | |
1701 | l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | |
1702 | ||
1703 | /* Fill in the pointer in the dynamic linker's own dynamic section, in | |
1704 | case you run gdb on the dynamic linker directly. */ | |
1705 | if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL) | |
1706 | GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | |
1707 | #endif | |
1708 | } | |
1709 | ||
32e6df36 UD |
1710 | if (prelinked) |
1711 | { | |
d89ae1d5 RM |
1712 | struct link_map *l; |
1713 | ||
d6b5d570 | 1714 | if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL) |
32e6df36 UD |
1715 | { |
1716 | ElfW(Rela) *conflict, *conflictend; | |
1717 | #ifndef HP_TIMING_NONAVAIL | |
1718 | hp_timing_t start; | |
1719 | hp_timing_t stop; | |
1720 | #endif | |
1721 | ||
1722 | HP_TIMING_NOW (start); | |
d6b5d570 | 1723 | assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL); |
32e6df36 | 1724 | conflict = (ElfW(Rela) *) |
d89ae1d5 | 1725 | GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr; |
32e6df36 | 1726 | conflictend = (ElfW(Rela) *) |
d89ae1d5 RM |
1727 | ((char *) conflict |
1728 | + GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val); | |
d6b5d570 | 1729 | _dl_resolve_conflicts (GL(dl_loaded), conflict, conflictend); |
32e6df36 UD |
1730 | HP_TIMING_NOW (stop); |
1731 | HP_TIMING_DIFF (relocate_time, start, stop); | |
1732 | } | |
1733 | ||
d89ae1d5 RM |
1734 | |
1735 | /* Mark all the objects so we know they have been already relocated. */ | |
1736 | for (l = GL(dl_loaded); l != NULL; l = l->l_next) | |
e8648a5a UD |
1737 | { |
1738 | l->l_relocated = 1; | |
1739 | if (l->l_relro_size) | |
1740 | _dl_protect_relro (l); | |
1741 | } | |
d89ae1d5 | 1742 | |
32e6df36 UD |
1743 | _dl_sysdep_start_cleanup (); |
1744 | } | |
1745 | else | |
164a7164 UD |
1746 | { |
1747 | /* Now we have all the objects loaded. Relocate them all except for | |
1748 | the dynamic linker itself. We do this in reverse order so that copy | |
1749 | relocs of earlier objects overwrite the data written by later | |
1750 | objects. We do not re-relocate the dynamic linker itself in this | |
1751 | loop because that could result in the GOT entries for functions we | |
1752 | call being changed, and that would break us. It is safe to relocate | |
1753 | the dynamic linker out of order because it has no copy relocs (we | |
1754 | know that because it is self-contained). */ | |
1755 | ||
1756 | struct link_map *l; | |
afdca0f2 | 1757 | int consider_profiling = GLRO(dl_profile) != NULL; |
8b07d6a8 | 1758 | #ifndef HP_TIMING_NONAVAIL |
164a7164 UD |
1759 | hp_timing_t start; |
1760 | hp_timing_t stop; | |
1761 | hp_timing_t add; | |
8b07d6a8 | 1762 | #endif |
c0fb8a56 | 1763 | |
164a7164 | 1764 | /* If we are profiling we also must do lazy reloaction. */ |
afdca0f2 | 1765 | GLRO(dl_lazy) |= consider_profiling; |
c0fb8a56 | 1766 | |
164a7164 UD |
1767 | l = GL(dl_loaded); |
1768 | while (l->l_next) | |
1769 | l = l->l_next; | |
db276fa1 | 1770 | |
164a7164 UD |
1771 | HP_TIMING_NOW (start); |
1772 | do | |
1773 | { | |
1774 | /* While we are at it, help the memory handling a bit. We have to | |
1775 | mark some data structures as allocated with the fake malloc() | |
1776 | implementation in ld.so. */ | |
1777 | struct libname_list *lnp = l->l_libname->next; | |
752a2a50 | 1778 | |
164a7164 UD |
1779 | while (__builtin_expect (lnp != NULL, 0)) |
1780 | { | |
1781 | lnp->dont_free = 1; | |
1782 | lnp = lnp->next; | |
1783 | } | |
752a2a50 | 1784 | |
164a7164 | 1785 | if (l != &GL(dl_rtld_map)) |
afdca0f2 | 1786 | INTUSE(_dl_relocate_object) (l, l->l_scope, GLRO(dl_lazy), |
cff26a3e | 1787 | consider_profiling); |
be935610 | 1788 | |
164a7164 UD |
1789 | l = l->l_prev; |
1790 | } | |
1791 | while (l); | |
1792 | HP_TIMING_NOW (stop); | |
1793 | ||
1794 | HP_TIMING_DIFF (relocate_time, start, stop); | |
1795 | ||
1796 | /* Do any necessary cleanups for the startup OS interface code. | |
1797 | We do these now so that no calls are made after rtld re-relocation | |
1798 | which might be resolved to different functions than we expect. | |
1799 | We cannot do this before relocating the other objects because | |
1800 | _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */ | |
1801 | _dl_sysdep_start_cleanup (); | |
1802 | ||
1803 | /* Now enable profiling if needed. Like the previous call, | |
1804 | this has to go here because the calls it makes should use the | |
1805 | rtld versions of the functions (particularly calloc()), but it | |
1806 | needs to have _dl_profile_map set up by the relocator. */ | |
1807 | if (__builtin_expect (GL(dl_profile_map) != NULL, 0)) | |
1808 | /* We must prepare the profiling. */ | |
afdca0f2 UD |
1809 | INTUSE(_dl_start_profile) (GL(dl_profile_map), |
1810 | GLRO(dl_profile_output)); | |
164a7164 UD |
1811 | |
1812 | if (GL(dl_rtld_map).l_opencount > 1) | |
1813 | { | |
1814 | /* There was an explicit ref to the dynamic linker as a shared lib. | |
1815 | Re-relocate ourselves with user-controlled symbol definitions. */ | |
1816 | HP_TIMING_NOW (start); | |
cff26a3e AJ |
1817 | INTUSE(_dl_relocate_object) (&GL(dl_rtld_map), GL(dl_loaded)->l_scope, |
1818 | 0, 0); | |
164a7164 UD |
1819 | HP_TIMING_NOW (stop); |
1820 | HP_TIMING_DIFF (add, start, stop); | |
1821 | HP_TIMING_ACCUM_NT (relocate_time, add); | |
1822 | } | |
1823 | } | |
ac16e905 | 1824 | |
be935610 | 1825 | /* Now set up the variable which helps the assembler startup code. */ |
d6b5d570 UD |
1826 | GL(dl_main_searchlist) = &GL(dl_loaded)->l_searchlist; |
1827 | GL(dl_global_scope)[0] = &GL(dl_loaded)->l_searchlist; | |
be935610 | 1828 | |
32e6df36 | 1829 | /* Save the information about the original global scope list since |
604510f7 | 1830 | we need it in the memory handling later. */ |
d6b5d570 | 1831 | GL(dl_initial_searchlist) = *GL(dl_main_searchlist); |
604510f7 | 1832 | |
131fd126 UD |
1833 | #ifndef NONTLS_INIT_TP |
1834 | # define NONTLS_INIT_TP do { } while (0) | |
1835 | #endif | |
1836 | ||
a816b435 | 1837 | #ifdef USE_TLS |
131fd126 | 1838 | if (GL(dl_tls_max_dtv_idx) > 0 || USE___THREAD || !TLS_INIT_TP_EXPENSIVE) |
a816b435 RM |
1839 | { |
1840 | /* Now that we have completed relocation, the initializer data | |
1841 | for the TLS blocks has its final values and we can copy them | |
1842 | into the main thread's TLS area, which we allocated above. */ | |
1843 | _dl_allocate_tls_init (tcbp); | |
1844 | ||
ce460d04 RM |
1845 | /* And finally install it for the main thread. If ld.so itself uses |
1846 | TLS we know the thread pointer was initialized earlier. */ | |
fde89ad0 RM |
1847 | const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD); |
1848 | if (__builtin_expect (lossage != NULL, 0)) | |
1849 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage); | |
a816b435 | 1850 | } |
131fd126 | 1851 | else |
a816b435 | 1852 | #endif |
131fd126 | 1853 | NONTLS_INIT_TP; |
a816b435 | 1854 | |
ed20b3d9 UD |
1855 | /* Notify the debugger that all objects are now mapped in. */ |
1856 | r->r_state = RT_ADD; | |
1857 | INTUSE(_dl_debug_state) (); | |
0200214b | 1858 | |
08cac4ac UD |
1859 | #ifndef MAP_COPY |
1860 | /* We must munmap() the cache file. */ | |
cff26a3e | 1861 | INTUSE(_dl_unload_cache) (); |
08cac4ac UD |
1862 | #endif |
1863 | ||
d66e34cd RM |
1864 | /* Once we return, _dl_sysdep_start will invoke |
1865 | the DT_INIT functions and then *USER_ENTRY. */ | |
1866 | } | |
fd26970f UD |
1867 | \f |
1868 | /* This is a little helper function for resolving symbols while | |
1869 | tracing the binary. */ | |
1870 | static void | |
c84142e8 UD |
1871 | print_unresolved (int errcode __attribute__ ((unused)), const char *objname, |
1872 | const char *errstring) | |
fd26970f | 1873 | { |
3996f34b | 1874 | if (objname[0] == '\0') |
e6caf4e1 | 1875 | objname = rtld_progname ?: "<main program>"; |
35fc382a | 1876 | _dl_error_printf ("%s (%s)\n", errstring, objname); |
fd26970f | 1877 | } |
c84142e8 UD |
1878 | \f |
1879 | /* This is a little helper function for resolving symbols while | |
1880 | tracing the binary. */ | |
1881 | static void | |
1882 | print_missing_version (int errcode __attribute__ ((unused)), | |
1883 | const char *objname, const char *errstring) | |
1884 | { | |
e6caf4e1 | 1885 | _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>", |
35fc382a | 1886 | objname, errstring); |
c84142e8 | 1887 | } |
ea278354 | 1888 | \f |
7dea968e | 1889 | /* Nonzero if any of the debugging options is enabled. */ |
392a6b52 | 1890 | static int any_debug attribute_relro; |
7dea968e | 1891 | |
b5efde2f UD |
1892 | /* Process the string given as the parameter which explains which debugging |
1893 | options are enabled. */ | |
1894 | static void | |
14c44e2e | 1895 | process_dl_debug (const char *dl_debug) |
b5efde2f | 1896 | { |
3e2040c8 UD |
1897 | /* When adding new entries make sure that the maximal length of a name |
1898 | is correctly handled in the LD_DEBUG_HELP code below. */ | |
1899 | static const struct | |
1900 | { | |
379d4ec4 UD |
1901 | unsigned char len; |
1902 | const char name[10]; | |
3e2040c8 UD |
1903 | const char helptext[41]; |
1904 | unsigned short int mask; | |
1905 | } debopts[] = | |
1906 | { | |
379d4ec4 UD |
1907 | #define LEN_AND_STR(str) sizeof (str) - 1, str |
1908 | { LEN_AND_STR ("libs"), "display library search paths", | |
3e2040c8 | 1909 | DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 1910 | { LEN_AND_STR ("reloc"), "display relocation processing", |
3e2040c8 | 1911 | DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 1912 | { LEN_AND_STR ("files"), "display progress for input file", |
3e2040c8 | 1913 | DL_DEBUG_FILES | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 1914 | { LEN_AND_STR ("symbols"), "display symbol table processing", |
3e2040c8 | 1915 | DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 1916 | { LEN_AND_STR ("bindings"), "display information about symbol binding", |
3e2040c8 | 1917 | DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 1918 | { LEN_AND_STR ("versions"), "display version dependencies", |
3e2040c8 | 1919 | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 1920 | { LEN_AND_STR ("all"), "all previous options combined", |
3e2040c8 UD |
1921 | DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS |
1922 | | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS }, | |
379d4ec4 | 1923 | { LEN_AND_STR ("statistics"), "display relocation statistics", |
3e2040c8 | 1924 | DL_DEBUG_STATISTICS }, |
379d4ec4 | 1925 | { LEN_AND_STR ("help"), "display this help message and exit", |
3e2040c8 UD |
1926 | DL_DEBUG_HELP }, |
1927 | }; | |
1928 | #define ndebopts (sizeof (debopts) / sizeof (debopts[0])) | |
3e2040c8 | 1929 | |
379d4ec4 UD |
1930 | /* Skip separating white spaces and commas. */ |
1931 | while (*dl_debug != '\0') | |
b5efde2f | 1932 | { |
379d4ec4 | 1933 | if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':') |
b5efde2f | 1934 | { |
3e2040c8 | 1935 | size_t cnt; |
379d4ec4 | 1936 | size_t len = 1; |
77aba05b | 1937 | |
379d4ec4 UD |
1938 | while (dl_debug[len] != '\0' && dl_debug[len] != ' ' |
1939 | && dl_debug[len] != ',' && dl_debug[len] != ':') | |
1940 | ++len; | |
14c44e2e | 1941 | |
3e2040c8 | 1942 | for (cnt = 0; cnt < ndebopts; ++cnt) |
379d4ec4 UD |
1943 | if (debopts[cnt].len == len |
1944 | && memcmp (dl_debug, debopts[cnt].name, len) == 0) | |
3e2040c8 | 1945 | { |
afdca0f2 | 1946 | GLRO(dl_debug_mask) |= debopts[cnt].mask; |
5688da55 | 1947 | any_debug = 1; |
3e2040c8 UD |
1948 | break; |
1949 | } | |
77aba05b | 1950 | |
3e2040c8 UD |
1951 | if (cnt == ndebopts) |
1952 | { | |
1953 | /* Display a warning and skip everything until next | |
1954 | separator. */ | |
1955 | char *copy = strndupa (dl_debug, len); | |
1956 | _dl_error_printf ("\ | |
1957 | warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy); | |
379d4ec4 UD |
1958 | } |
1959 | ||
1960 | dl_debug += len; | |
1961 | continue; | |
3e2040c8 | 1962 | } |
379d4ec4 UD |
1963 | |
1964 | ++dl_debug; | |
3e2040c8 | 1965 | } |
77aba05b | 1966 | |
afdca0f2 | 1967 | if (GLRO(dl_debug_mask) & DL_DEBUG_HELP) |
3e2040c8 UD |
1968 | { |
1969 | size_t cnt; | |
14c44e2e | 1970 | |
3e2040c8 UD |
1971 | _dl_printf ("\ |
1972 | Valid options for the LD_DEBUG environment variable are:\n\n"); | |
db276fa1 | 1973 | |
3e2040c8 | 1974 | for (cnt = 0; cnt < ndebopts; ++cnt) |
37d8b778 UD |
1975 | _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name, |
1976 | " " + debopts[cnt].len - 3, | |
3e2040c8 | 1977 | debopts[cnt].helptext); |
14c44e2e | 1978 | |
3e2040c8 UD |
1979 | _dl_printf ("\n\ |
1980 | To direct the debugging output into a file instead of standard output\n\ | |
1981 | a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n"); | |
1982 | _exit (0); | |
b5efde2f | 1983 | } |
b5efde2f UD |
1984 | } |
1985 | \f | |
ea278354 UD |
1986 | /* Process all environments variables the dynamic linker must recognize. |
1987 | Since all of them start with `LD_' we are a bit smarter while finding | |
1988 | all the entries. */ | |
9360906d | 1989 | extern char **_environ attribute_hidden; |
67c94753 | 1990 | |
d6b5d570 | 1991 | |
ea278354 | 1992 | static void |
ba9fcb3f | 1993 | process_envvars (enum mode *modep) |
ea278354 | 1994 | { |
67c94753 | 1995 | char **runp = _environ; |
ea278354 UD |
1996 | char *envline; |
1997 | enum mode mode = normal; | |
7dea968e | 1998 | char *debug_output = NULL; |
ea278354 UD |
1999 | |
2000 | /* This is the default place for profiling data file. */ | |
afdca0f2 | 2001 | GLRO(dl_profile_output) |
e6caf4e1 | 2002 | = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0]; |
ea278354 UD |
2003 | |
2004 | while ((envline = _dl_next_ld_env_entry (&runp)) != NULL) | |
2005 | { | |
379d4ec4 UD |
2006 | size_t len = 0; |
2007 | ||
2008 | while (envline[len] != '\0' && envline[len] != '=') | |
2009 | ++len; | |
ea278354 | 2010 | |
75e8d1f5 UD |
2011 | if (envline[len] != '=') |
2012 | /* This is a "LD_" variable at the end of the string without | |
2013 | a '=' character. Ignore it since otherwise we will access | |
2014 | invalid memory below. */ | |
67c94753 | 2015 | continue; |
75e8d1f5 | 2016 | |
67c94753 | 2017 | switch (len) |
ea278354 | 2018 | { |
14c44e2e UD |
2019 | case 4: |
2020 | /* Warning level, verbose or not. */ | |
67c94753 | 2021 | if (memcmp (envline, "WARN", 4) == 0) |
afdca0f2 | 2022 | GLRO(dl_verbose) = envline[5] != '\0'; |
14c44e2e | 2023 | break; |
ea278354 | 2024 | |
14c44e2e UD |
2025 | case 5: |
2026 | /* Debugging of the dynamic linker? */ | |
67c94753 UD |
2027 | if (memcmp (envline, "DEBUG", 5) == 0) |
2028 | process_dl_debug (&envline[6]); | |
14c44e2e | 2029 | break; |
b5efde2f | 2030 | |
14c44e2e UD |
2031 | case 7: |
2032 | /* Print information about versions. */ | |
67c94753 | 2033 | if (memcmp (envline, "VERBOSE", 7) == 0) |
14c44e2e | 2034 | { |
67c94753 | 2035 | version_info = envline[8] != '\0'; |
14c44e2e UD |
2036 | break; |
2037 | } | |
7dea968e | 2038 | |
14c44e2e | 2039 | /* List of objects to be preloaded. */ |
67c94753 | 2040 | if (memcmp (envline, "PRELOAD", 7) == 0) |
14c44e2e | 2041 | { |
67c94753 | 2042 | preloadlist = &envline[8]; |
14c44e2e UD |
2043 | break; |
2044 | } | |
120b4c49 | 2045 | |
14c44e2e | 2046 | /* Which shared object shall be profiled. */ |
c95f3fd4 | 2047 | if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0') |
afdca0f2 | 2048 | GLRO(dl_profile) = &envline[8]; |
14c44e2e | 2049 | break; |
120b4c49 | 2050 | |
14c44e2e UD |
2051 | case 8: |
2052 | /* Do we bind early? */ | |
67c94753 | 2053 | if (memcmp (envline, "BIND_NOW", 8) == 0) |
f53c03c2 | 2054 | { |
afdca0f2 | 2055 | GLRO(dl_lazy) = envline[9] == '\0'; |
f53c03c2 UD |
2056 | break; |
2057 | } | |
67c94753 | 2058 | if (memcmp (envline, "BIND_NOT", 8) == 0) |
afdca0f2 | 2059 | GLRO(dl_bind_not) = envline[9] != '\0'; |
14c44e2e | 2060 | break; |
ea278354 | 2061 | |
14c44e2e UD |
2062 | case 9: |
2063 | /* Test whether we want to see the content of the auxiliary | |
2064 | array passed up from the kernel. */ | |
67c94753 | 2065 | if (memcmp (envline, "SHOW_AUXV", 9) == 0) |
14c44e2e UD |
2066 | _dl_show_auxv (); |
2067 | break; | |
ea278354 | 2068 | |
12264bd7 | 2069 | case 10: |
3081378b | 2070 | /* Mask for the important hardware capabilities. */ |
67c94753 | 2071 | if (memcmp (envline, "HWCAP_MASK", 10) == 0) |
afdca0f2 UD |
2072 | GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, |
2073 | 0, 0); | |
12264bd7 UD |
2074 | break; |
2075 | ||
f787edde UD |
2076 | case 11: |
2077 | /* Path where the binary is found. */ | |
e6caf4e1 | 2078 | if (!INTUSE(__libc_enable_secure) |
67c94753 | 2079 | && memcmp (envline, "ORIGIN_PATH", 11) == 0) |
afdca0f2 | 2080 | GLRO(dl_origin_path) = &envline[12]; |
f787edde UD |
2081 | break; |
2082 | ||
14c44e2e | 2083 | case 12: |
dec126b4 | 2084 | /* The library search path. */ |
67c94753 | 2085 | if (memcmp (envline, "LIBRARY_PATH", 12) == 0) |
dec126b4 | 2086 | { |
67c94753 | 2087 | library_path = &envline[13]; |
dec126b4 UD |
2088 | break; |
2089 | } | |
2090 | ||
14c44e2e | 2091 | /* Where to place the profiling data file. */ |
67c94753 | 2092 | if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0) |
14c44e2e | 2093 | { |
67c94753 | 2094 | debug_output = &envline[13]; |
14c44e2e UD |
2095 | break; |
2096 | } | |
ea278354 | 2097 | |
67c94753 | 2098 | if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0) |
afdca0f2 | 2099 | GLRO(dl_dynamic_weak) = 1; |
14c44e2e | 2100 | break; |
ea278354 | 2101 | |
97fd3a30 UD |
2102 | case 13: |
2103 | /* We might have some extra environment variable with length 13 | |
2104 | to handle. */ | |
2105 | #ifdef EXTRA_LD_ENVVARS_13 | |
2106 | EXTRA_LD_ENVVARS_13 | |
2107 | #endif | |
2108 | if (!INTUSE(__libc_enable_secure) | |
2109 | && memcmp (envline, "USE_LOAD_BIAS", 13) == 0) | |
afdca0f2 | 2110 | GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0; |
97fd3a30 UD |
2111 | break; |
2112 | ||
14c44e2e UD |
2113 | case 14: |
2114 | /* Where to place the profiling data file. */ | |
e6caf4e1 | 2115 | if (!INTUSE(__libc_enable_secure) |
3e2040c8 UD |
2116 | && memcmp (envline, "PROFILE_OUTPUT", 14) == 0 |
2117 | && envline[15] != '\0') | |
afdca0f2 | 2118 | GLRO(dl_profile_output) = &envline[15]; |
14c44e2e | 2119 | break; |
120b4c49 | 2120 | |
32e6df36 UD |
2121 | case 16: |
2122 | /* The mode of the dynamic linker can be set. */ | |
2123 | if (memcmp (envline, "TRACE_PRELINKING", 16) == 0) | |
2124 | { | |
2125 | mode = trace; | |
afdca0f2 UD |
2126 | GLRO(dl_verbose) = 1; |
2127 | GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK; | |
2128 | GLRO(dl_trace_prelink) = &envline[17]; | |
32e6df36 UD |
2129 | } |
2130 | break; | |
2131 | ||
14c44e2e UD |
2132 | case 20: |
2133 | /* The mode of the dynamic linker can be set. */ | |
67c94753 | 2134 | if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0) |
14c44e2e UD |
2135 | mode = trace; |
2136 | break; | |
e2102c14 UD |
2137 | |
2138 | /* We might have some extra environment variable to handle. This | |
2139 | is tricky due to the pre-processing of the length of the name | |
2140 | in the switch statement here. The code here assumes that added | |
2141 | environment variables have a different length. */ | |
2142 | #ifdef EXTRA_LD_ENVVARS | |
2143 | EXTRA_LD_ENVVARS | |
2144 | #endif | |
ea278354 UD |
2145 | } |
2146 | } | |
2147 | ||
3e2040c8 UD |
2148 | /* The caller wants this information. */ |
2149 | *modep = mode; | |
2150 | ||
4bae5567 UD |
2151 | /* Extra security for SUID binaries. Remove all dangerous environment |
2152 | variables. */ | |
e6caf4e1 | 2153 | if (__builtin_expect (INTUSE(__libc_enable_secure), 0)) |
4bae5567 | 2154 | { |
c95f3fd4 | 2155 | static const char unsecure_envvars[] = |
4bae5567 UD |
2156 | #ifdef EXTRA_UNSECURE_ENVVARS |
2157 | EXTRA_UNSECURE_ENVVARS | |
2158 | #endif | |
c95f3fd4 UD |
2159 | UNSECURE_ENVVARS; |
2160 | const char *nextp; | |
2161 | ||
2162 | nextp = unsecure_envvars; | |
2163 | do | |
2164 | { | |
2165 | unsetenv (nextp); | |
9710f75d UD |
2166 | /* We could use rawmemchr but this need not be fast. */ |
2167 | nextp = (char *) (strchr) (nextp, '\0') + 1; | |
c95f3fd4 UD |
2168 | } |
2169 | while (*nextp != '\0'); | |
74955460 UD |
2170 | |
2171 | if (__access ("/etc/suid-debug", F_OK) != 0) | |
2172 | unsetenv ("MALLOC_CHECK_"); | |
4bae5567 | 2173 | } |
7dea968e UD |
2174 | /* If we have to run the dynamic linker in debugging mode and the |
2175 | LD_DEBUG_OUTPUT environment variable is given, we write the debug | |
2176 | messages to this file. */ | |
3e2040c8 | 2177 | else if (any_debug && debug_output != NULL) |
7dea968e | 2178 | { |
5f2de337 UD |
2179 | #ifdef O_NOFOLLOW |
2180 | const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW; | |
2181 | #else | |
2182 | const int flags = O_WRONLY | O_APPEND | O_CREAT; | |
2183 | #endif | |
7a2fd787 UD |
2184 | size_t name_len = strlen (debug_output); |
2185 | char buf[name_len + 12]; | |
2186 | char *startp; | |
2187 | ||
2188 | buf[name_len + 11] = '\0'; | |
9710f75d | 2189 | startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0); |
7a2fd787 UD |
2190 | *--startp = '.'; |
2191 | startp = memcpy (startp - name_len, debug_output, name_len); | |
2192 | ||
5688da55 UD |
2193 | GL(dl_debug_fd) = __open (startp, flags, DEFFILEMODE); |
2194 | if (GL(dl_debug_fd) == -1) | |
7dea968e | 2195 | /* We use standard output if opening the file failed. */ |
5688da55 | 2196 | GL(dl_debug_fd) = STDOUT_FILENO; |
7dea968e | 2197 | } |
ea278354 | 2198 | } |
db276fa1 UD |
2199 | |
2200 | ||
2201 | /* Print the various times we collected. */ | |
2202 | static void | |
392a6b52 | 2203 | print_statistics (hp_timing_t *rtld_total_timep) |
db276fa1 | 2204 | { |
8b07d6a8 | 2205 | #ifndef HP_TIMING_NONAVAIL |
f457369d | 2206 | char buf[200]; |
db276fa1 UD |
2207 | char *cp; |
2208 | char *wp; | |
2209 | ||
2210 | /* Total time rtld used. */ | |
2211 | if (HP_TIMING_AVAIL) | |
2212 | { | |
392a6b52 | 2213 | HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep); |
cff26a3e AJ |
2214 | INTUSE(_dl_debug_printf) ("\nruntime linker statistics:\n" |
2215 | " total startup time in dynamic loader: %s\n", | |
2216 | buf); | |
db276fa1 | 2217 | |
392a6b52 | 2218 | /* Print relocation statistics. */ |
35fc382a | 2219 | char pbuf[30]; |
db276fa1 | 2220 | HP_TIMING_PRINT (buf, sizeof (buf), relocate_time); |
392a6b52 | 2221 | cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep, |
9710f75d | 2222 | pbuf + sizeof (pbuf), 10, 0); |
35fc382a UD |
2223 | wp = pbuf; |
2224 | switch (pbuf + sizeof (pbuf) - cp) | |
db276fa1 UD |
2225 | { |
2226 | case 3: | |
2227 | *wp++ = *cp++; | |
2228 | case 2: | |
2229 | *wp++ = *cp++; | |
2230 | case 1: | |
2231 | *wp++ = '.'; | |
2232 | *wp++ = *cp++; | |
2233 | } | |
2234 | *wp = '\0'; | |
cff26a3e | 2235 | INTUSE(_dl_debug_printf) ("\ |
7969407a | 2236 | time needed for relocation: %s (%s%%)\n", |
cff26a3e | 2237 | buf, pbuf); |
db276fa1 | 2238 | } |
1531e094 | 2239 | #endif |
a21a20a3 UD |
2240 | |
2241 | unsigned long int num_relative_relocations = 0; | |
2242 | struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist; | |
2243 | unsigned int i; | |
2244 | ||
2245 | for (i = 0; i < scope->r_nlist; i++) | |
2246 | { | |
2247 | struct link_map *l = scope->r_list [i]; | |
2248 | ||
2249 | if (!l->l_addr) | |
2250 | continue; | |
2251 | ||
2252 | if (l->l_info[VERSYMIDX (DT_RELCOUNT)]) | |
2253 | num_relative_relocations += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val; | |
2254 | if (l->l_info[VERSYMIDX (DT_RELACOUNT)]) | |
2255 | num_relative_relocations += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val; | |
2256 | } | |
2257 | ||
cff26a3e AJ |
2258 | INTUSE(_dl_debug_printf) (" number of relocations: %lu\n", |
2259 | GL(dl_num_relocations)); | |
2260 | INTUSE(_dl_debug_printf) (" number of relocations from cache: %lu\n", | |
2261 | GL(dl_num_cache_relocations)); | |
a21a20a3 UD |
2262 | INTUSE(_dl_debug_printf) (" number of relative relocations: %lu\n", |
2263 | num_relative_relocations); | |
db276fa1 | 2264 | |
1531e094 | 2265 | #ifndef HP_TIMING_NONAVAIL |
db276fa1 UD |
2266 | /* Time spend while loading the object and the dependencies. */ |
2267 | if (HP_TIMING_AVAIL) | |
2268 | { | |
35fc382a | 2269 | char pbuf[30]; |
db276fa1 | 2270 | HP_TIMING_PRINT (buf, sizeof (buf), load_time); |
392a6b52 | 2271 | cp = _itoa ((1000ULL * load_time) / *rtld_total_timep, |
9710f75d | 2272 | pbuf + sizeof (pbuf), 10, 0); |
35fc382a UD |
2273 | wp = pbuf; |
2274 | switch (pbuf + sizeof (pbuf) - cp) | |
db276fa1 UD |
2275 | { |
2276 | case 3: | |
2277 | *wp++ = *cp++; | |
2278 | case 2: | |
2279 | *wp++ = *cp++; | |
2280 | case 1: | |
2281 | *wp++ = '.'; | |
2282 | *wp++ = *cp++; | |
2283 | } | |
2284 | *wp = '\0'; | |
cff26a3e | 2285 | INTUSE(_dl_debug_printf) ("\ |
7969407a | 2286 | time needed to load objects: %s (%s%%)\n", |
cff26a3e | 2287 | buf, pbuf); |
db276fa1 | 2288 | } |
1531e094 | 2289 | #endif |
db276fa1 | 2290 | } |