1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996, 1997 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 (int errcode
, const char *objname
,
49 const char *errsting
);
51 /* Helper function to handle errors when a version is missing. */
52 static void print_missing_version (int errcode
, const char *objname
,
53 const char *errsting
);
58 const char *_dl_rpath
;
61 /* Set nonzero during loading and initialization of executable and
62 libraries, cleared before the executable's entry point runs. This
63 must not be initialized to nonzero, because the unused dynamic
64 linker loaded in for libc.so's "ld.so.1" dep will provide the
65 definition seen by libc.so's initializer; that value must be zero,
66 and will be since that dynamic linker's _dl_start and dl_main will
70 static void dl_main (const ElfW(Phdr
) *phdr
,
72 ElfW(Addr
) *user_entry
);
74 struct link_map _dl_rtld_map
;
75 struct libname_list _dl_rtld_libname
;
80 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
86 struct link_map bootstrap_map
;
88 /* This #define produces dynamic linking inline functions for
89 bootstrap relocation instead of general-purpose relocation. */
90 #define RTLD_BOOTSTRAP
91 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
92 #include "dynamic-link.h"
94 /* Figure out the run-time load address of the dynamic linker itself. */
95 bootstrap_map
.l_addr
= elf_machine_load_address ();
97 /* Read our own dynamic section and fill in the info array. */
98 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
99 elf_get_dynamic_info (bootstrap_map
.l_ld
, bootstrap_map
.l_info
);
101 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
102 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
105 /* Relocate ourselves so we can do normal function calls and
106 data access using the global offset table. */
108 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0);
110 /* Now life is sane; we can call functions and access global data.
111 Set up to use the operating system facilities, and find out from
112 the operating system's program loader where to find the program
113 header table in core. */
116 /* Transfer data about ourselves to the permanent link_map structure. */
117 _dl_rtld_map
.l_addr
= bootstrap_map
.l_addr
;
118 _dl_rtld_map
.l_ld
= bootstrap_map
.l_ld
;
119 memcpy (_dl_rtld_map
.l_info
, bootstrap_map
.l_info
,
120 sizeof _dl_rtld_map
.l_info
);
121 _dl_setup_hash (&_dl_rtld_map
);
123 /* Cache the DT_RPATH stored in ld.so itself; this will be
124 the default search path. */
125 _dl_rpath
= (void *) (_dl_rtld_map
.l_addr
+
126 _dl_rtld_map
.l_info
[DT_STRTAB
]->d_un
.d_ptr
+
127 _dl_rtld_map
.l_info
[DT_RPATH
]->d_un
.d_val
);
129 /* Call the OS-dependent function to set up life so we can do things like
130 file access. It will call `dl_main' (below) to do all the real work
131 of the dynamic linker, and then unwind our frame and run the user
132 entry point on the same stack we entered on. */
133 return _dl_sysdep_start (arg
, &dl_main
);
137 /* Now life is peachy; we can do all normal operations.
138 On to the real work. */
142 /* Some helper functions. */
144 /* Arguments to relocate_doit. */
153 /* Argument to map_doit. */
155 /* Return value of map_doit. */
156 struct link_map
*main_map
;
159 /* Arguments to version_check_doit. */
160 struct version_check_args
162 struct link_map
*main_map
;
167 relocate_doit (void *a
)
169 struct relocate_args
*args
= (struct relocate_args
*) a
;
171 _dl_relocate_object (args
->l
, _dl_object_relocation_scope (args
->l
),
178 struct map_args
*args
= (struct map_args
*)a
;
179 args
->main_map
= _dl_map_object (NULL
, args
->str
, lt_library
, 0);
183 version_check_doit (void *a
)
185 struct version_check_args
*args
= (struct version_check_args
*)a
;
186 if (_dl_check_all_versions (args
->main_map
, 1) && args
->doexit
)
187 /* We cannot start the application. Abort now. */
191 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
194 dl_main (const ElfW(Phdr
) *phdr
,
196 ElfW(Addr
) *user_entry
)
198 const ElfW(Phdr
) *ph
;
199 struct link_map
*main_map
;
201 enum { normal
, list
, verify
, trace
} mode
;
202 struct link_map
**preloads
;
203 unsigned int npreloads
;
204 const char *preloadlist
;
209 mode
= getenv ("LD_TRACE_LOADED_OBJECTS") != NULL
? trace
: normal
;
210 _dl_verbose
= *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
212 /* LAZY is determined by the environment variable LD_WARN and
213 LD_BIND_NOW if we trace the binary. */
216 ? (*(getenv ("LD_BIND_NOW") ?: "") == '\0' ? 1 : 0) : -1);
218 lazy
= !__libc_enable_secure
&& *(getenv ("LD_BIND_NOW") ?: "") == '\0';
220 /* Set up a flag which tells we are just starting. */
223 if (*user_entry
== (ElfW(Addr
)) &_start
)
225 /* Ho ho. We are not the program interpreter! We are the program
226 itself! This means someone ran ld.so as a command. Well, that
227 might be convenient to do sometimes. We support it by
228 interpreting the args like this:
230 ld.so PROGRAM ARGS...
232 The first argument is the name of a file containing an ELF
233 executable we will load and run with the following arguments.
234 To simplify life here, PROGRAM is searched for using the
235 normal rules for shared objects, rather than $PATH or anything
236 like that. We just load it and use its entry point; we don't
237 pay attention to its PT_INTERP command (we are the interpreter
238 ourselves). This is an easy way to test a new ld.so before
242 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
243 You have invoked `ld.so', the helper program for shared library executables.\n\
244 This program usually lives in the file `/lib/ld.so', and special directives\n\
245 in executable files using ELF shared libraries tell the system's program\n\
246 loader to load the helper program from this file. This helper program loads\n\
247 the shared libraries needed by the program executable, prepares the program\n\
248 to run, and runs it. You may invoke this helper program directly from the\n\
249 command line to load and run an ELF executable file; this is like executing\n\
250 that file itself, but always uses this helper program from the file you\n\
251 specified, instead of the helper program file specified in the executable\n\
252 file you run. This is mostly of use for maintainers to test new versions\n\
253 of this helper program; chances are you did not intend to run this program.\n",
256 /* Note the place where the dynamic linker actually came from. */
257 _dl_rtld_map
.l_name
= _dl_argv
[0];
260 if (! strcmp (_dl_argv
[1], "--list"))
263 lazy
= -1; /* This means do no dependency analysis. */
269 else if (! strcmp (_dl_argv
[1], "--verify"))
286 char *err_str
= NULL
;
287 const char *obj_name
__attribute__ ((unused
));
288 struct map_args args
;
290 args
.str
= _dl_argv
[0];
291 (void) _dl_catch_error (&err_str
, &obj_name
, map_doit
, &args
);
292 main_map
= args
.main_map
;
296 _exit (EXIT_FAILURE
);
300 main_map
= _dl_map_object (NULL
, _dl_argv
[0], lt_library
, 0);
302 phdr
= main_map
->l_phdr
;
303 phent
= main_map
->l_phnum
;
304 main_map
->l_name
= (char *) "";
305 *user_entry
= main_map
->l_entry
;
309 /* Create a link_map for the executable itself.
310 This will be what dlopen on "" returns. */
311 main_map
= _dl_new_object ((char *) "", "", lt_executable
);
312 if (main_map
== NULL
)
313 _dl_sysdep_fatal ("cannot allocate memory for link map", NULL
);
314 main_map
->l_phdr
= phdr
;
315 main_map
->l_phnum
= phent
;
316 main_map
->l_entry
= *user_entry
;
317 main_map
->l_opencount
= 1;
320 /* Scan the program header table for the dynamic section. */
321 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
325 /* This tells us where to find the dynamic section,
326 which tells us everything we need to do. */
327 main_map
->l_ld
= (void *) main_map
->l_addr
+ ph
->p_vaddr
;
330 /* This "interpreter segment" was used by the program loader to
331 find the program interpreter, which is this program itself, the
332 dynamic linker. We note what name finds us, so that a future
333 dlopen call or DT_NEEDED entry, for something that wants to link
334 against the dynamic linker as a shared library, will know that
335 the shared object is already loaded. */
336 _dl_rtld_libname
.name
= (const char *) main_map
->l_addr
+ ph
->p_vaddr
;
337 _dl_rtld_libname
.next
= NULL
;
338 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
342 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
344 /* We were invoked directly, so the program might not have a
346 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
347 _dl_rtld_libname
.next
= NULL
;
348 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
351 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
354 /* We were called just to verify that this is a dynamic executable
355 using us as the program interpreter. */
356 _exit (main_map
->l_ld
== NULL
? 1 : has_interp
? 0 : 2);
358 /* Extract the contents of the dynamic section for easy access. */
359 elf_get_dynamic_info (main_map
->l_ld
, main_map
->l_info
);
360 if (main_map
->l_info
[DT_HASH
])
361 /* Set up our cache of pointers into the hash table. */
362 _dl_setup_hash (main_map
);
364 /* Put the link_map for ourselves on the chain so it can be found by
365 name. Note that at this point the global chain of link maps contains
366 exactly one element, which is pointed to by main_map. */
367 if (! _dl_rtld_map
.l_name
)
368 /* If not invoked directly, the dynamic linker shared object file was
369 found by the PT_INTERP name. */
370 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
371 _dl_rtld_map
.l_type
= lt_library
;
372 main_map
->l_next
= &_dl_rtld_map
;
373 _dl_rtld_map
.l_prev
= main_map
;
375 /* We have two ways to specify objects to preload: via environment
376 variable and via the file /etc/ld.so.preload. The later can also
377 be used when security is enabled. */
381 preloadlist
= getenv ("LD_PRELOAD");
384 /* The LD_PRELOAD environment variable gives a white space
385 separated list of libraries that are loaded before the
386 executable's dependencies and prepended to the global scope
387 list. If the binary is running setuid all elements
388 containing a '/' are ignored since it is insecure. */
389 char *list
= strdupa (preloadlist
);
391 while ((p
= strsep (&list
, " ")) != NULL
)
392 if (! __libc_enable_secure
|| strchr (p
, '/') == NULL
)
394 (void) _dl_map_object (NULL
, p
, lt_library
, 0);
399 /* Read the contents of the file. */
400 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
401 PROT_READ
| PROT_WRITE
);
404 /* Parse the file. It contains names of libraries to be loaded,
405 separated by white spaces or `:'. It may also contain
406 comments introduced by `#'. */
411 /* Eliminate comments. */
416 char *comment
= memchr (runp
, '#', rest
);
420 rest
-= comment
- runp
;
423 while (--rest
> 0 && *++comment
!= '\n');
426 /* We have one problematic case: if we have a name at the end of
427 the file without a trailing terminating characters, we cannot
428 place the \0. Handle the case separately. */
429 if (file
[file_size
- 1] != ' ' && file
[file_size
] != '\t'
430 && file
[file_size
] != '\n')
432 problem
= &file
[file_size
];
433 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
434 && problem
[-1] != '\n')
447 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
449 (void) _dl_map_object (NULL
, p
, lt_library
, 0);
456 char *p
= strndupa (problem
, file_size
- (problem
- file
));
457 (void) _dl_map_object (NULL
, p
, lt_library
, 0);
460 /* We don't need the file anymore. */
461 __munmap (file
, file_size
);
466 /* Set up PRELOADS with a vector of the preloaded libraries. */
469 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
470 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
477 assert (i
== npreloads
);
480 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
481 specified some libraries to load, these are inserted before the actual
482 dependencies in the executable's searchlist for symbol resolution. */
483 _dl_map_object_deps (main_map
, preloads
, npreloads
, mode
== trace
);
486 /* We are done mapping things, so close the zero-fill descriptor. */
487 __close (_dl_zerofd
);
491 /* Remove _dl_rtld_map from the chain. */
492 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
493 if (_dl_rtld_map
.l_next
)
494 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
496 if (_dl_rtld_map
.l_opencount
)
498 /* Some DT_NEEDED entry referred to the interpreter object itself, so
499 put it back in the list of visible objects. We insert it into the
500 chain in symbol search order because gdb uses the chain's order as
501 its symbol search order. */
503 while (main_map
->l_searchlist
[i
] != &_dl_rtld_map
)
505 _dl_rtld_map
.l_prev
= main_map
->l_searchlist
[i
- 1];
506 _dl_rtld_map
.l_next
= (i
+ 1 < main_map
->l_nsearchlist
?
507 main_map
->l_searchlist
[i
+ 1] : NULL
);
508 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
509 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
510 if (_dl_rtld_map
.l_next
)
512 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
513 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
517 /* Now let us see whether all libraries are available in the
520 struct version_check_args args
;
521 args
.doexit
= mode
== normal
;
522 args
.main_map
= main_map
;
523 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
528 /* We were run just to list the shared libraries. It is
529 important that we do this before real relocation, because the
530 functions we call below for output may no longer work properly
535 if (! _dl_loaded
->l_info
[DT_NEEDED
])
536 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
541 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
542 if (l
->l_opencount
== 0)
543 /* The library was not found. */
544 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
549 buf
[sizeof buf
- 1] = '\0';
550 bp
= _itoa (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
551 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
552 < sizeof l
->l_addr
* 2)
554 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
555 l
->l_name
, " (0x", bp
, ")\n", NULL
);
560 for (i
= 1; i
< _dl_argc
; ++i
)
562 const ElfW(Sym
) *ref
= NULL
;
563 ElfW(Addr
) loadbase
= _dl_lookup_symbol (_dl_argv
[i
], &ref
,
564 &_dl_default_scope
[2],
566 ELF_MACHINE_RELOC_NOPLT
);
568 buf
[sizeof buf
- 1] = '\0';
569 bp
= _itoa (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
570 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
572 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
573 buf
[sizeof buf
- 1] = '\0';
574 bp
= _itoa (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
575 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
577 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
581 /* We have to do symbol dependency testing. */
582 struct relocate_args args
;
592 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
595 _dl_receive_error (print_unresolved
, relocate_doit
, &args
);
596 *_dl_global_scope_end
= NULL
;
606 /* Now we have all the objects loaded. Relocate them all except for
607 the dynamic linker itself. We do this in reverse order so that copy
608 relocs of earlier objects overwrite the data written by later
609 objects. We do not re-relocate the dynamic linker itself in this
610 loop because that could result in the GOT entries for functions we
611 call being changed, and that would break us. It is safe to relocate
612 the dynamic linker out of order because it has no copy relocs (we
613 know that because it is self-contained). */
621 if (l
!= &_dl_rtld_map
)
623 _dl_relocate_object (l
, _dl_object_relocation_scope (l
), lazy
);
624 *_dl_global_scope_end
= NULL
;
629 /* Do any necessary cleanups for the startup OS interface code.
630 We do these now so that no calls are made after rtld re-relocation
631 which might be resolved to different functions than we expect.
632 We cannot do this before relocating the other objects because
633 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
634 _dl_sysdep_start_cleanup ();
636 if (_dl_rtld_map
.l_opencount
> 0)
637 /* There was an explicit ref to the dynamic linker as a shared lib.
638 Re-relocate ourselves with user-controlled symbol definitions. */
639 _dl_relocate_object (&_dl_rtld_map
, &_dl_default_scope
[2], 0);
643 /* Initialize _r_debug. */
644 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
649 #ifdef ELF_MACHINE_DEBUG_SETUP
651 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
653 ELF_MACHINE_DEBUG_SETUP (l
, r
);
654 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
658 if (l
->l_info
[DT_DEBUG
])
659 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
660 with the run-time address of the r_debug structure */
661 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
663 /* Fill in the pointer in the dynamic linker's own dynamic section, in
664 case you run gdb on the dynamic linker directly. */
665 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
666 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
670 /* Notify the debugger that all objects are now mapped in. */
675 /* Once we return, _dl_sysdep_start will invoke
676 the DT_INIT functions and then *USER_ENTRY. */
679 /* This is a little helper function for resolving symbols while
680 tracing the binary. */
682 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
683 const char *errstring
)
685 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
688 /* This is a little helper function for resolving symbols while
689 tracing the binary. */
691 print_missing_version (int errcode
__attribute__ ((unused
)),
692 const char *objname
, const char *errstring
)
694 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
695 objname
, ": ", errstring
, "\n", NULL
);