1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
26 #include "../stdio-common/_itoa.h"
28 #include "dynamic-link.h"
31 /* System-specific function to do initial startup for the dynamic linker.
32 After this, file access calls and getenv must work. This is responsible
33 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
34 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
35 extern ElfW(Addr
) _dl_sysdep_start (void **start_argptr
,
36 void (*dl_main
) (const ElfW(Phdr
) *phdr
,
38 ElfW(Addr
) *user_entry
));
39 extern void _dl_sysdep_start_cleanup (void);
41 /* System-dependent function to read a file's whole contents
42 in the most convenient manner available. */
43 extern void *_dl_sysdep_read_whole_file (const char *filename
,
47 /* Helper function to handle errors while resolving symbols. */
48 static void print_unresolved (const char *errstring
, const char *objname
);
53 const char *_dl_rpath
;
55 /* Set nonzero during loading and initialization of executable and
56 libraries, cleared before the executable's entry point runs. This
57 must not be initialized to nonzero, because the unused dynamic
58 linker loaded in for libc.so's "ld.so.1" dep will provide the
59 definition seen by libc.so's initializer; that value must be zero,
60 and will be since that dynamic linker's _dl_start and dl_main will
64 static void dl_main (const ElfW(Phdr
) *phdr
,
66 ElfW(Addr
) *user_entry
);
68 struct link_map _dl_rtld_map
;
73 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
79 struct link_map bootstrap_map
;
81 /* This #define produces dynamic linking inline functions for
82 bootstrap relocation instead of general-purpose relocation. */
83 #define RTLD_BOOTSTRAP
84 #define RESOLVE(sym, flags) bootstrap_map.l_addr
85 #include "dynamic-link.h"
87 /* Figure out the run-time load address of the dynamic linker itself. */
88 bootstrap_map
.l_addr
= elf_machine_load_address ();
90 /* Read our own dynamic section and fill in the info array. */
91 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
92 elf_get_dynamic_info (bootstrap_map
.l_ld
, bootstrap_map
.l_info
);
94 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
95 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
98 /* Relocate ourselves so we can do normal function calls and
99 data access using the global offset table. */
101 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0);
104 /* Now life is sane; we can call functions and access global data.
105 Set up to use the operating system facilities, and find out from
106 the operating system's program loader where to find the program
107 header table in core. */
110 /* Transfer data about ourselves to the permanent link_map structure. */
111 _dl_rtld_map
.l_addr
= bootstrap_map
.l_addr
;
112 _dl_rtld_map
.l_ld
= bootstrap_map
.l_ld
;
113 memcpy (_dl_rtld_map
.l_info
, bootstrap_map
.l_info
,
114 sizeof _dl_rtld_map
.l_info
);
115 _dl_setup_hash (&_dl_rtld_map
);
117 /* Cache the DT_RPATH stored in ld.so itself; this will be
118 the default search path. */
119 _dl_rpath
= (void *) (_dl_rtld_map
.l_addr
+
120 _dl_rtld_map
.l_info
[DT_STRTAB
]->d_un
.d_ptr
+
121 _dl_rtld_map
.l_info
[DT_RPATH
]->d_un
.d_val
);
123 /* Call the OS-dependent function to set up life so we can do things like
124 file access. It will call `dl_main' (below) to do all the real work
125 of the dynamic linker, and then unwind our frame and run the user
126 entry point on the same stack we entered on. */
127 return _dl_sysdep_start (arg
, &dl_main
);
131 /* Now life is peachy; we can do all normal operations.
132 On to the real work. */
136 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
139 dl_main (const ElfW(Phdr
) *phdr
,
141 ElfW(Addr
) *user_entry
)
143 const ElfW(Phdr
) *ph
;
146 enum { normal
, list
, verify
, trace
} mode
;
147 struct link_map
**preloads
;
148 unsigned int npreloads
;
149 const char *preloadlist
;
153 mode
= getenv ("LD_TRACE_LOADED_OBJECTS") != NULL
? trace
: normal
;
155 /* LAZY is determined by the parameters --datadeps and --function-deps
156 if we trace the binary. */
160 lazy
= !__libc_enable_secure
&& *(getenv ("LD_BIND_NOW") ?: "") == '\0';
162 /* Set up a flag which tells we are just starting. */
165 if (*user_entry
== (ElfW(Addr
)) &_start
)
167 /* Ho ho. We are not the program interpreter! We are the program
168 itself! This means someone ran ld.so as a command. Well, that
169 might be convenient to do sometimes. We support it by
170 interpreting the args like this:
172 ld.so PROGRAM ARGS...
174 The first argument is the name of a file containing an ELF
175 executable we will load and run with the following arguments.
176 To simplify life here, PROGRAM is searched for using the
177 normal rules for shared objects, rather than $PATH or anything
178 like that. We just load it and use its entry point; we don't
179 pay attention to its PT_INTERP command (we are the interpreter
180 ourselves). This is an easy way to test a new ld.so before
184 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
185 You have invoked `ld.so', the helper program for shared library executables.\n\
186 This program usually lives in the file `/lib/ld.so', and special directives\n\
187 in executable files using ELF shared libraries tell the system's program\n\
188 loader to load the helper program from this file. This helper program loads\n\
189 the shared libraries needed by the program executable, prepares the program\n\
190 to run, and runs it. You may invoke this helper program directly from the\n\
191 command line to load and run an ELF executable file; this is like executing\n\
192 that file itself, but always uses this helper program from the file you\n\
193 specified, instead of the helper program file specified in the executable\n\
194 file you run. This is mostly of use for maintainers to test new versions\n\
195 of this helper program; chances are you did not intend to run this program.\n",
198 /* Note the place where the dynamic linker actually came from. */
199 _dl_rtld_map
.l_name
= _dl_argv
[0];
202 if (! strcmp (_dl_argv
[1], "--list"))
205 lazy
= -1; /* This means do no dependency analysis. */
211 else if (! strcmp (_dl_argv
[1], "--verify"))
219 else if (! strcmp (_dl_argv
[1], "--data-relocs"))
222 lazy
= 1; /* This means do only data relocation analysis. */
228 else if (! strcmp (_dl_argv
[1], "--function-relocs"))
231 lazy
= 0; /* This means do also function relocation analysis. */
248 l
= _dl_map_object (NULL
, _dl_argv
[0], lt_library
, 0);
250 char *err_str
= NULL
;
251 const char *obj_name
__attribute__ ((unused
));
253 (void) _dl_catch_error (&err_str
, &obj_name
, doit
);
257 _exit (EXIT_FAILURE
);
261 l
= _dl_map_object (NULL
, _dl_argv
[0], lt_library
, 0);
265 l
->l_name
= (char *) "";
266 *user_entry
= l
->l_entry
;
270 /* Create a link_map for the executable itself.
271 This will be what dlopen on "" returns. */
272 l
= _dl_new_object ((char *) "", "", lt_executable
);
274 _dl_sysdep_fatal ("cannot allocate memory for link map", NULL
);
277 l
->l_entry
= *user_entry
;
282 /* GDB assumes that the first element on the chain is the
283 link_map for the executable itself, and always skips it.
284 Make sure the first one is indeed that one. */
285 l
->l_prev
->l_next
= l
->l_next
;
287 l
->l_next
->l_prev
= l
->l_prev
;
289 l
->l_next
= _dl_loaded
;
290 _dl_loaded
->l_prev
= l
;
294 /* Scan the program header table for the dynamic section. */
295 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
299 /* This tells us where to find the dynamic section,
300 which tells us everything we need to do. */
301 l
->l_ld
= (void *) l
->l_addr
+ ph
->p_vaddr
;
304 /* This "interpreter segment" was used by the program loader to
305 find the program interpreter, which is this program itself, the
306 dynamic linker. We note what name finds us, so that a future
307 dlopen call or DT_NEEDED entry, for something that wants to link
308 against the dynamic linker as a shared library, will know that
309 the shared object is already loaded. */
310 _dl_rtld_map
.l_libname
= (const char *) l
->l_addr
+ ph
->p_vaddr
;
313 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
314 /* We were invoked directly, so the program might not have a PT_INTERP. */
315 _dl_rtld_map
.l_libname
= _dl_rtld_map
.l_name
;
317 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
320 /* We were called just to verify that this is a dynamic executable
321 using us as the program interpreter. */
322 _exit (l
->l_ld
== NULL
? EXIT_FAILURE
: EXIT_SUCCESS
);
324 /* Extract the contents of the dynamic section for easy access. */
325 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
326 if (l
->l_info
[DT_HASH
])
327 /* Set up our cache of pointers into the hash table. */
330 /* Put the link_map for ourselves on the chain so it can be found by
332 if (! _dl_rtld_map
.l_name
)
333 /* If not invoked directly, the dynamic linker shared object file was
334 found by the PT_INTERP name. */
335 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
;
336 _dl_rtld_map
.l_type
= lt_library
;
339 l
->l_next
= &_dl_rtld_map
;
340 _dl_rtld_map
.l_prev
= l
;
342 /* We have two ways to specify objects to preload: via environment
343 variable and via the file /etc/ld.so.preload. The later can also
344 be used when security is enabled. */
348 preloadlist
= getenv ("LD_PRELOAD");
351 /* The LD_PRELOAD environment variable gives list of libraries
352 separated by white space or colons that are loaded before the
353 executable's dependencies and prepended to the global scope
354 list. If the binary is running setuid all elements
355 containing a '/' are ignored since it is insecure. */
356 char *list
= strdupa (preloadlist
);
358 while ((p
= strsep (&list
, " :")) != NULL
)
359 if (! __libc_enable_secure
|| strchr (p
, '/') == NULL
)
361 struct link_map
*new_map
= _dl_map_object (NULL
, p
, lt_library
, 0);
362 if (new_map
->l_opencount
== 1)
363 /* It is no duplicate. */
368 /* Read the contents of the file. */
369 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
370 PROT_READ
| PROT_WRITE
);
373 /* Parse the file. It contains names of libraries to be loaded,
374 separated by white spaces or `:'. It may also contain
375 comments introduced by `#'. */
380 /* Eliminate comments. */
385 char *comment
= memchr (runp
, '#', rest
);
389 rest
-= comment
- runp
;
392 while (--rest
> 0 && *++comment
!= '\n');
395 /* We have one problematic case: if we have a name at the end of
396 the file without a trailing terminating characters, we cannot
397 place the \0. Handle the case separately. */
398 if (file_size
> 0 && file
[file_size
- 1] != ' '
399 && file
[file_size
- 1] != '\t' && file
[file_size
- 1] != '\n'
400 && file
[file_size
- 1] != ':')
402 problem
= &file
[file_size
];
403 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
404 && problem
[-1] != '\n' && problem
[-1] != ':')
414 file
[file_size
- 1] = '\0';
417 if (file_size
> 0 && file
!= problem
)
420 runp
= file
+ strspn (file
, ": \t\n");
421 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
423 struct link_map
*new_map
= _dl_map_object (NULL
, p
,
425 if (new_map
->l_opencount
== 1)
426 /* It is no duplicate. */
430 runp
+= strspn (runp
, ": \t\n");
436 char *p
= strndupa (problem
, file_size
- (problem
- file
));
437 struct link_map
*new_map
= _dl_map_object (NULL
, p
, lt_library
, 0);
438 if (new_map
->l_opencount
== 1)
439 /* It is no duplicate. */
443 /* We don't need the file anymore. */
444 __munmap (file
, file_size
);
449 /* Set up PRELOADS with a vector of the preloaded libraries. */
452 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
453 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
460 assert (i
== npreloads
);
463 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
464 specified some libraries to load, these are inserted before the actual
465 dependencies in the executable's searchlist for symbol resolution. */
466 _dl_map_object_deps (l
, preloads
, npreloads
, mode
== trace
);
469 /* We are done mapping things, so close the zero-fill descriptor. */
470 __close (_dl_zerofd
);
474 /* Remove _dl_rtld_map from the chain. */
475 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
476 if (_dl_rtld_map
.l_next
)
477 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
479 if (_dl_rtld_map
.l_opencount
)
481 /* Some DT_NEEDED entry referred to the interpreter object itself, so
482 put it back in the list of visible objects. We insert it into the
483 chain in symbol search order because gdb uses the chain's order as
484 its symbol search order. */
486 while (l
->l_searchlist
[i
] != &_dl_rtld_map
)
488 _dl_rtld_map
.l_prev
= l
->l_searchlist
[i
- 1];
489 _dl_rtld_map
.l_next
= (i
+ 1 < l
->l_nsearchlist
?
490 l
->l_searchlist
[i
+ 1] : NULL
);
491 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
492 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
493 if (_dl_rtld_map
.l_next
)
495 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
496 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
502 /* We were run just to list the shared libraries. It is
503 important that we do this before real relocation, because the
504 functions we call below for output may no longer work properly
509 if (! _dl_loaded
->l_info
[DT_NEEDED
])
510 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
512 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
513 if (l
->l_opencount
== 0)
514 /* The library was not found. */
515 _dl_sysdep_message ("\t", l
->l_libname
, " => not found\n", NULL
);
519 buf
[sizeof buf
- 1] = '\0';
520 bp
= _itoa (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
521 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
522 < sizeof l
->l_addr
* 2)
524 _dl_sysdep_message ("\t", l
->l_libname
, " => ", l
->l_name
,
525 " (0x", bp
, ")\n", NULL
);
529 for (i
= 1; i
< _dl_argc
; ++i
)
531 const ElfW(Sym
) *ref
= NULL
;
532 ElfW(Addr
) loadbase
= _dl_lookup_symbol (_dl_argv
[i
], &ref
,
533 &_dl_default_scope
[2],
537 buf
[sizeof buf
- 1] = '\0';
538 bp
= _itoa (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
539 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
541 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
542 buf
[sizeof buf
- 1] = '\0';
543 bp
= _itoa (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
544 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
546 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
550 /* We have to do symbol dependency testing. */
553 _dl_relocate_object (l
, _dl_object_relocation_scope (l
), lazy
);
561 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
563 _dl_receive_error (print_unresolved
, doit
);
564 *_dl_global_scope_end
= NULL
;
574 /* Now we have all the objects loaded. Relocate them all except for
575 the dynamic linker itself. We do this in reverse order so that copy
576 relocs of earlier objects overwrite the data written by later
577 objects. We do not re-relocate the dynamic linker itself in this
578 loop because that could result in the GOT entries for functions we
579 call being changed, and that would break us. It is safe to relocate
580 the dynamic linker out of order because it has no copy relocs (we
581 know that because it is self-contained). */
588 if (l
!= &_dl_rtld_map
)
590 _dl_relocate_object (l
, _dl_object_relocation_scope (l
), lazy
);
591 *_dl_global_scope_end
= NULL
;
596 /* Do any necessary cleanups for the startup OS interface code.
597 We do these now so that no calls are made after rtld re-relocation
598 which might be resolved to different functions than we expect.
599 We cannot do this before relocating the other objects because
600 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
601 _dl_sysdep_start_cleanup ();
603 if (_dl_rtld_map
.l_opencount
> 0)
604 /* There was an explicit ref to the dynamic linker as a shared lib.
605 Re-relocate ourselves with user-controlled symbol definitions. */
606 _dl_relocate_object (&_dl_rtld_map
, &_dl_default_scope
[2], 0);
610 /* Initialize _r_debug. */
611 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
615 #ifdef ELF_MACHINE_DEBUG_SETUP
617 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
619 ELF_MACHINE_DEBUG_SETUP (l
, r
);
620 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
624 if (l
->l_info
[DT_DEBUG
])
625 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
626 with the run-time address of the r_debug structure */
627 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
629 /* Fill in the pointer in the dynamic linker's own dynamic section, in
630 case you run gdb on the dynamic linker directly. */
631 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
632 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
636 /* Notify the debugger that all objects are now mapped in. */
641 /* Once we return, _dl_sysdep_start will invoke
642 the DT_INIT functions and then *USER_ENTRY. */
645 /* This is a little helper function for resolving symbols while
646 tracing the binary. */
648 print_unresolved (const char *errstring
, const char *objname
)
650 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);