]>
sourceware.org Git - glibc.git/blob - elf/rtld.c
1 /* Run time dynamic linker.
2 Copyright (C) 1995 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
21 #include "dynamic-link.h"
30 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
33 /* System-specific function to do initial startup for the dynamic linker.
34 After this, file access calls and getenv must work. This is responsible
35 for setting _dl_secure if we need to be secure (e.g. setuid),
36 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
37 extern Elf32_Addr
_dl_sysdep_start (void **start_argptr
,
38 void (*dl_main
) (const Elf32_Phdr
*phdr
,
40 Elf32_Addr
*user_entry
));
46 struct r_debug dl_r_debug
;
48 static void dl_main (const Elf32_Phdr
*phdr
,
50 Elf32_Addr
*user_entry
);
55 struct link_map rtld_map
;
57 /* Figure out the run-time load address of the dynamic linker itself. */
58 rtld_map
.l_addr
= elf_machine_load_address ();
60 /* Read our own dynamic section and fill in the info array.
61 Conveniently, the first element of the GOT contains the
62 offset of _DYNAMIC relative to the run-time load address. */
63 rtld_map
.l_ld
= (void *) rtld_map
.l_addr
+ *elf_machine_got ();
64 elf_get_dynamic_info (rtld_map
.l_ld
, rtld_map
.l_info
);
66 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
67 ELF_MACHINE_BEFORE_RTLD_RELOC (rtld_map
.l_info
);
70 /* Relocate ourselves so we can do normal function calls and
71 data access using the global offset table. */
73 ELF_DYNAMIC_RELOCATE (&rtld_map
, 0, NULL
);
76 /* Now life is sane; we can call functions and access global data.
77 Set up to use the operating system facilities, and find out from
78 the operating system's program loader where to find the program
79 header table in core. */
81 dl_r_debug
.r_ldbase
= rtld_map
.l_addr
; /* Record our load address. */
83 /* Call the OS-dependent function to set up life so we can do things like
84 file access. It will call `dl_main' (below) to do all the real work
85 of the dynamic linker, and then unwind our frame and run the user
86 entry point on the same stack we entered on. */
87 return _dl_sysdep_start (&arg
, &dl_main
);
91 /* Now life is peachy; we can do all normal operations.
92 On to the real work. */
97 dl_main (const Elf32_Phdr
*phdr
,
99 Elf32_Addr
*user_entry
)
103 const Elf32_Phdr
*ph
;
105 const char *interpreter_name
;
108 if (*user_entry
== (Elf32_Addr
) &_start
)
110 /* Ho ho. We are not the program interpreter! We are the program
111 itself! This means someone ran ld.so as a command. Well, that
112 might be convenient to do sometimes. We support it by
113 interpreting the args like this:
115 ld.so PROGRAM ARGS...
117 The first argument is the name of a file containing an ELF
118 executable we will load and run with the following arguments.
119 To simplify life here, PROGRAM is searched for using the
120 normal rules for shared objects, rather than $PATH or anything
121 like that. We just load it and use its entry point; we don't
122 pay attention to its PT_INTERP command (we are the interpreter
123 ourselves). This is an easy way to test a new ld.so before
127 Usage: ld.so EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
128 You have invoked `ld.so', the helper program for shared library executables.\n\
129 This program usually lives in the file `/lib/ld.so', and special directives\n\
130 in executable files using ELF shared libraries tell the system's program\n\
131 loader to load the helper program from this file. This helper program loads\n\
132 the shared libraries needed by the program executable, prepares the program\n\
133 to run, and runs it. You may invoke this helper program directly from the\n\
134 command line to load and run an ELF executable file; this is like executing\n\
135 that file itself, but always uses this helper program from the file you\n\
136 specified, instead of the helper program file specified in the executable\n\
137 file you run. This is mostly of use for maintainers to test new versions\n\
138 of this helper program; chances are you did not intend to run this program.\n"
141 interpreter_name
= _dl_argv
[0];
144 l
= _dl_map_object (NULL
, _dl_argv
[0], user_entry
);
147 l
->l_type
= lt_executable
;
148 l
->l_libname
= (char *) "";
152 /* Create a link_map for the executable itself.
153 This will be what dlopen on "" returns. */
154 l
= _dl_new_object ((char *) "", "", lt_executable
);
157 interpreter_name
= 0;
160 /* Scan the program header table for the dynamic section. */
161 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
165 /* This tells us where to find the dynamic section,
166 which tells us everything we need to do. */
167 l
->l_ld
= (void *) ph
->p_vaddr
;
170 /* This "interpreter segment" was used by the program loader to
171 find the program interpreter, which is this program itself, the
172 dynamic linker. We note what name finds us, so that a future
173 dlopen call or DT_NEEDED entry, for something that wants to link
174 against the dynamic linker as a shared library, will know that
175 the shared object is already loaded. */
176 interpreter_name
= (void *) ph
->p_vaddr
;
179 assert (interpreter_name
); /* How else did we get here? */
181 /* Extract the contents of the dynamic section for easy access. */
182 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
183 /* Set up our cache of pointers into the hash table. */
186 if (l
->l_info
[DT_DEBUG
])
187 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
188 with the run-time address of the r_debug structure, which we
189 will set up later to communicate with the debugger. */
190 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (Elf32_Addr
) &dl_r_debug
;
192 l
= _dl_new_object ((char *) interpreter_name
, interpreter_name
,
195 /* Now process all the DT_NEEDED entries and map in the objects.
196 Each new link_map will go on the end of the chain, so we will
197 come across it later in the loop to map in its dependencies. */
198 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
200 if (l
->l_info
[DT_NEEDED
])
203 = (void *) l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
;
205 for (d
= l
->l_ld
; d
->d_tag
!= DT_NULL
; ++d
)
206 if (d
->d_tag
== DT_NEEDED
)
207 _dl_map_object (l
, strtab
+ d
->d_un
.d_val
, NULL
);
209 l
->l_deps_loaded
= 1;
212 l
= _dl_loaded
->l_next
;
213 assert (l
->l_type
== lt_interpreter
);
214 if (l
->l_opencount
== 0)
216 /* No DT_NEEDED entry referred to the interpreter object itself.
217 Remove it from the maps we will use for symbol resolution. */
218 l
->l_prev
->l_next
= l
->l_next
;
220 l
->l_next
->l_prev
= l
->l_prev
;
223 lazy
= _dl_secure
|| *(getenv ("LD_BIND_NOW") ?: "");
225 /* Now we have all the objects loaded. Relocate them all.
226 We do this in reverse order so that copy relocs of earlier
227 objects overwrite the data written by later objects. */
233 _dl_relocate_object (l
, lazy
);
237 /* Tell the debugger where to find the map of loaded objects. */
238 dl_r_debug
.r_version
= 1 /* R_DEBUG_VERSION XXX */;
239 dl_r_debug
.r_map
= _dl_loaded
;
240 dl_r_debug
.r_brk
= (Elf32_Addr
) &_dl_r_debug_state
;
242 const char *errstring
;
246 err
= _dl_catch_error (&errstring
, &errobj
, &doit
);
248 _dl_sysdep_fatal (_dl_argv
[0] ?: "<program name unknown>",
249 ": error in loading shared libraries\n",
250 errobj
?: "", errobj
? ": " : "",
251 errstring
, err
? ": " : NULL
,
252 err
? strerror (err
) : NULL
, NULL
);
254 /* Once we return, _dl_sysdep_start will invoke
255 the DT_INIT functions and then *USER_ENTRY. */
258 /* This function exists solely to have a breakpoint set on it by the
261 _dl_r_debug_state (void)
This page took 0.051639 seconds and 6 git commands to generate.