]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Run-time dynamic linker data structures for loaded ELF shared objects. |
df4ef2ab | 2 | Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
54d79e99 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
54d79e99 UD |
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. | |
d66e34cd | 9 | |
54d79e99 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 | |
13 | Library General Public License for more details. | |
d66e34cd | 14 | |
54d79e99 UD |
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. */ | |
d66e34cd RM |
19 | |
20 | #ifndef _LINK_H | |
f166d865 | 21 | |
d66e34cd | 22 | #define _LINK_H 1 |
f166d865 | 23 | #include <features.h> |
d66e34cd | 24 | |
266180eb | 25 | #define __need_size_t |
f166d865 | 26 | #define __need_NULL |
266180eb RM |
27 | #include <stddef.h> |
28 | ||
d66e34cd RM |
29 | #include <elf.h> |
30 | ||
1fb05e3d UD |
31 | __BEGIN_DECLS |
32 | ||
266180eb RM |
33 | /* We use this macro to refer to ELF types independent of the native wordsize. |
34 | `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */ | |
567c63af RM |
35 | #define ElfW(type) _ElfW (Elf, __ELF_NATIVE_CLASS, type) |
36 | #define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type) | |
266180eb RM |
37 | #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t) |
38 | #define _ElfW_1(e,w,t) e##w##t | |
567c63af | 39 | #include <elfclass.h> /* Defines __ELF_NATIVE_CLASS. */ |
d66e34cd RM |
40 | |
41 | /* Rendezvous structure used by the run-time dynamic linker to communicate | |
42 | details of shared object loading to the debugger. If the executable's | |
43 | dynamic section has a DT_DEBUG element, the run-time linker sets that | |
44 | element's value to the address where this structure can be found. */ | |
45 | ||
46 | struct r_debug | |
47 | { | |
48 | int r_version; /* Version number for this protocol. */ | |
49 | ||
50 | struct link_map *r_map; /* Head of the chain of loaded objects. */ | |
51 | ||
52 | /* This is the address of a function internal to the run-time linker, | |
53 | that will always be called when the linker begins to map in a | |
54 | library or unmap it, and again when the mapping change is complete. | |
55 | The debugger can set a breakpoint at this address if it wants to | |
56 | notice shared object mapping changes. */ | |
266180eb | 57 | ElfW(Addr) r_brk; |
d66e34cd RM |
58 | enum |
59 | { | |
60 | /* This state value describes the mapping change taking place when | |
61 | the `r_brk' address is called. */ | |
62 | RT_CONSISTENT, /* Mapping change is complete. */ | |
63 | RT_ADD, /* Beginning to add a new object. */ | |
64 | RT_DELETE, /* Beginning to remove an object mapping. */ | |
65 | } r_state; | |
66 | ||
266180eb | 67 | ElfW(Addr) r_ldbase; /* Base address the linker is loaded at. */ |
d66e34cd RM |
68 | }; |
69 | ||
4d6acc61 RM |
70 | /* This is the instance of that structure used by the dynamic linker. */ |
71 | extern struct r_debug _r_debug; | |
72 | ||
d66e34cd RM |
73 | /* This symbol refers to the "dynamic structure" in the `.dynamic' section |
74 | of whatever module refers to `_DYNAMIC'. So, to find its own | |
75 | `struct r_debug', a program could do: | |
76 | for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL) | |
77 | if (dyn->d_tag == DT_DEBUG) r_debug = (struct r_debug) dyn->d_un.d_ptr; | |
78 | */ | |
79 | ||
266180eb | 80 | extern ElfW(Dyn) _DYNAMIC[]; |
d66e34cd | 81 | |
c84142e8 UD |
82 | /* For the version handling we need an array with only names and their |
83 | hash values. */ | |
1fb05e3d UD |
84 | struct r_found_version |
85 | { | |
86 | const char *name; | |
87 | ElfW(Word) hash; | |
88 | ||
89 | const char *filename; | |
90 | }; | |
d66e34cd RM |
91 | |
92 | /* Structure describing a loaded shared object. The `l_next' and `l_prev' | |
93 | members form a chain of all the shared objects loaded at startup. | |
94 | ||
95 | These data structures exist in space used by the run-time dynamic linker; | |
96 | modifying them may have disastrous results. */ | |
97 | ||
98 | struct link_map | |
99 | { | |
100 | /* These first few members are part of the protocol with the debugger. | |
101 | This is the same format used in SVR4. */ | |
102 | ||
266180eb | 103 | ElfW(Addr) l_addr; /* Base address shared object is loaded at. */ |
d66e34cd | 104 | char *l_name; /* Absolute file name object was found in. */ |
266180eb | 105 | ElfW(Dyn) *l_ld; /* Dynamic section of the shared object. */ |
d66e34cd RM |
106 | struct link_map *l_next, *l_prev; /* Chain of loaded objects. */ |
107 | ||
108 | /* All following members are internal to the dynamic linker. | |
109 | They may change without notice. */ | |
110 | ||
c84142e8 UD |
111 | struct libname_list |
112 | { | |
113 | const char *name; /* Name requested (before search). */ | |
114 | struct libname_list *next; /* Link to next name for this object. */ | |
115 | } *l_libname; | |
a2e1b046 RM |
116 | /* Indexed pointers to dynamic section. |
117 | [0,DT_NUM) are indexed by the processor-independent tags. | |
df4ef2ab UD |
118 | [DT_NUM,DT_NUM+DT_PROCNUM) are indexed by the tag minus DT_LOPROC. |
119 | [DT_NUM+DT_PROCNUM,DT_NUM+DT_PROCNUM+DT_EXTRANUM) are indexed | |
1228ed5c UD |
120 | by DT_EXTRATAGIDX(tagvalue) and |
121 | [DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM, | |
122 | DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM) | |
123 | are indexed by DT_EXTRATAGIDX(tagvalue) (see <elf.h>). */ | |
df4ef2ab | 124 | |
1228ed5c | 125 | ElfW(Dyn) *l_info[DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM]; |
266180eb RM |
126 | const ElfW(Phdr) *l_phdr; /* Pointer to program header table in core. */ |
127 | ElfW(Addr) l_entry; /* Entry point location. */ | |
128 | ElfW(Half) l_phnum; /* Number of program header entries. */ | |
d66e34cd | 129 | |
efec1d0c RM |
130 | /* Array of DT_NEEDED dependencies and their dependencies, in |
131 | dependency order for symbol lookup. This is null before the | |
132 | dependencies have been loaded. */ | |
133 | struct link_map **l_searchlist; | |
134 | unsigned int l_nsearchlist; | |
135 | ||
84384f5b UD |
136 | /* We keep another list in which we keep duplicates. This is |
137 | needed in _dl_lookup_symbol_skip to implemented RTLD_NEXT. */ | |
138 | struct link_map **l_dupsearchlist; | |
139 | unsigned int l_ndupsearchlist; | |
140 | ||
ba79d61b RM |
141 | /* Dependent object that first caused this object to be loaded. */ |
142 | struct link_map *l_loader; | |
143 | ||
d66e34cd | 144 | /* Symbol hash table. */ |
8d6468d0 RM |
145 | ElfW(Symndx) l_nbuckets; |
146 | const ElfW(Symndx) *l_buckets, *l_chain; | |
d66e34cd RM |
147 | |
148 | unsigned int l_opencount; /* Reference count for dlopen/dlclose. */ | |
149 | enum /* Where this object came from. */ | |
150 | { | |
151 | lt_executable, /* The main executable program. */ | |
d66e34cd RM |
152 | lt_library, /* Library needed by main executable. */ |
153 | lt_loaded, /* Extra run-time loaded shared object. */ | |
154 | } l_type:2; | |
d66e34cd RM |
155 | unsigned int l_relocated:1; /* Nonzero if object's relocations done. */ |
156 | unsigned int l_init_called:1; /* Nonzero if DT_INIT function called. */ | |
157 | unsigned int l_init_running:1; /* Nonzero while DT_INIT function runs. */ | |
ba79d61b RM |
158 | unsigned int l_global:1; /* Nonzero if object in _dl_global_scope. */ |
159 | unsigned int l_reserved:2; /* Reserved for internal use. */ | |
c84142e8 UD |
160 | |
161 | /* Array with version names. */ | |
162 | unsigned int l_nversions; | |
1fb05e3d | 163 | struct r_found_version *l_versions; |
d66e34cd | 164 | }; |
fd26970f UD |
165 | |
166 | ||
c84142e8 UD |
167 | /* Test whether given NAME matches any of the names of the given object. */ |
168 | static inline int | |
169 | __attribute__ ((unused)) | |
1fb05e3d | 170 | _dl_name_match_p (const char *__name, struct link_map *__map) |
c84142e8 UD |
171 | { |
172 | int __found = strcmp (__name, __map->l_name) == 0; | |
173 | struct libname_list *__runp = __map->l_libname; | |
174 | ||
175 | while (! __found && __runp != NULL) | |
176 | if (strcmp (__name, __runp->name) == 0) | |
177 | __found = 1; | |
178 | else | |
179 | __runp = __runp->next; | |
180 | ||
181 | return __found; | |
182 | } | |
183 | ||
fd26970f | 184 | /* Function used as argument for `_dl_receive_error' function. The |
c84142e8 UD |
185 | arguments are the error code, error string, and the objname the |
186 | error occurred in. */ | |
187 | typedef void (*receiver_fct) (int, const char *, const char *); | |
d66e34cd RM |
188 | \f |
189 | /* Internal functions of the run-time dynamic linker. | |
190 | These can be accessed if you link again the dynamic linker | |
191 | as a shared library, as in `-lld' or `/lib/ld.so' explicitly; | |
192 | but are not normally of interest to user programs. | |
193 | ||
194 | The `-ldl' library functions in <dlfcn.h> provide a simple | |
195 | user interface to run-time dynamic linking. */ | |
196 | ||
197 | ||
266180eb RM |
198 | /* Cached value of `getpagesize ()'. */ |
199 | extern size_t _dl_pagesize; | |
200 | ||
d66e34cd RM |
201 | /* File descriptor referring to the zero-fill device. */ |
202 | extern int _dl_zerofd; | |
203 | ||
204 | /* OS-dependent function to open the zero-fill device. */ | |
205 | extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */ | |
206 | ||
6a76c115 RM |
207 | /* OS-dependent function to write a message on the standard output. |
208 | All arguments are `const char *'; args until a null pointer | |
209 | are concatenated to form the message to print. */ | |
210 | extern void _dl_sysdep_message (const char *string, ...); | |
211 | ||
fd26970f UD |
212 | /* OS-dependent function to write a message on the standard error. |
213 | All arguments are `const char *'; args until a null pointer | |
214 | are concatenated to form the message to print. */ | |
215 | extern void _dl_sysdep_error (const char *string, ...); | |
216 | ||
d66e34cd RM |
217 | /* OS-dependent function to give a fatal error message and exit |
218 | when the dynamic linker fails before the program is fully linked. | |
219 | All arguments are `const char *'; args until a null pointer | |
220 | are concatenated to form the message to print. */ | |
221 | extern void _dl_sysdep_fatal (const char *string, ...) | |
222 | __attribute__ ((__noreturn__)); | |
223 | ||
224 | /* Nonzero if the program should be "secure" (i.e. it's setuid or somesuch). | |
225 | This tells the dynamic linker to ignore environment variables. */ | |
226 | extern int _dl_secure; | |
227 | ||
228 | /* This function is called by all the internal dynamic linker functions | |
421f82e5 RM |
229 | when they encounter an error. ERRCODE is either an `errno' code or |
230 | zero; OBJECT is the name of the problematical shared object, or null if | |
231 | it is a general problem; ERRSTRING is a string describing the specific | |
232 | problem. */ | |
421f82e5 RM |
233 | extern void _dl_signal_error (int errcode, |
234 | const char *object, | |
fd26970f | 235 | const char *errstring); |
d66e34cd RM |
236 | |
237 | /* Call OPERATE, catching errors from `dl_signal_error'. If there is no | |
421f82e5 RM |
238 | error, *ERRSTRING is set to null. If there is an error, *ERRSTRING and |
239 | *OBJECT are set to the strings passed to _dl_signal_error, and the error | |
14bab8de UD |
240 | code passed is the return value. ERRSTRING if nonzero points to a |
241 | malloc'ed string which the caller has to free after use. */ | |
242 | extern int _dl_catch_error (char **errstring, | |
421f82e5 RM |
243 | const char **object, |
244 | void (*operate) (void)); | |
d66e34cd | 245 | |
fd26970f UD |
246 | /* Call OPERATE, receiving errors from `dl_signal_error'. Unlike |
247 | `_dl_catch_error' the operation is resumed after the OPERATE | |
248 | function returns. */ | |
249 | extern void _dl_receive_error (receiver_fct fct, void (*operate) (void)); | |
250 | ||
d66e34cd RM |
251 | |
252 | /* Helper function for <dlfcn.h> functions. Runs the OPERATE function via | |
253 | _dl_catch_error. Returns zero for success, nonzero for failure; and | |
254 | arranges for `dlerror' to return the error details. */ | |
255 | extern int _dlerror_run (void (*operate) (void)); | |
256 | ||
257 | ||
258 | /* Open the shared object NAME and map in its segments. | |
259 | LOADER's DT_RPATH is used in searching for NAME. | |
d66e34cd RM |
260 | If the object is already opened, returns its existing map. */ |
261 | extern struct link_map *_dl_map_object (struct link_map *loader, | |
46ec036d UD |
262 | const char *name, int type, |
263 | int trace_mode); | |
ea03559a | 264 | |
2064087b RM |
265 | /* Call _dl_map_object on the dependencies of MAP, and set up |
266 | MAP->l_searchlist. PRELOADS points to a vector of NPRELOADS previously | |
267 | loaded objects that will be inserted into MAP->l_searchlist after MAP | |
268 | but before its dependencies. */ | |
269 | extern void _dl_map_object_deps (struct link_map *map, | |
270 | struct link_map **preloads, | |
46ec036d | 271 | unsigned int npreloads, int trace_mode); |
efec1d0c | 272 | |
d66e34cd RM |
273 | /* Cache the locations of MAP's hash table. */ |
274 | extern void _dl_setup_hash (struct link_map *map); | |
275 | ||
276 | ||
efec1d0c | 277 | /* Open the shared object NAME, relocate it, and run its initializer if it |
a23db8e4 RM |
278 | hasn't already been run. MODE is as for `dlopen' (see <dlfcn.h>). If |
279 | the object is already opened, returns its existing map. */ | |
280 | extern struct link_map *_dl_open (const char *name, int mode); | |
efec1d0c | 281 | |
ba79d61b RM |
282 | /* Close an object previously opened by _dl_open. */ |
283 | extern void _dl_close (struct link_map *map); | |
efec1d0c RM |
284 | |
285 | ||
d66e34cd RM |
286 | /* Search loaded objects' symbol tables for a definition of the symbol |
287 | referred to by UNDEF. *SYM is the symbol table entry containing the | |
288 | reference; it is replaced with the defining symbol, and the base load | |
ba79d61b RM |
289 | address of the defining object is returned. SYMBOL_SCOPE is a |
290 | null-terminated list of object scopes to search; each object's | |
291 | l_searchlist (i.e. the segment of the dependency tree starting at that | |
292 | object) is searched in turn. REFERENCE_NAME should name the object | |
bc9f6000 UD |
293 | containing the reference; it is used in error messages. |
294 | RELOC_TYPE is a machine-dependent reloc type, which is passed to | |
295 | the `elf_machine_lookup_*_p' macros in dl-machine.h to affect which | |
296 | symbols can be chosen. */ | |
266180eb RM |
297 | extern ElfW(Addr) _dl_lookup_symbol (const char *undef, |
298 | const ElfW(Sym) **sym, | |
ba79d61b | 299 | struct link_map *symbol_scope[], |
f2b0f935 | 300 | const char *reference_name, |
bc9f6000 | 301 | int reloc_type); |
d66e34cd | 302 | |
c84142e8 UD |
303 | /* Lookup versioned symbol. */ |
304 | extern ElfW(Addr) _dl_lookup_versioned_symbol (const char *undef, | |
305 | const ElfW(Sym) **sym, | |
306 | struct link_map *symbol_scope[], | |
307 | const char *reference_name, | |
1fb05e3d | 308 | const struct r_found_version *version, |
bc9f6000 | 309 | int reloc_type); |
c84142e8 | 310 | |
84384f5b UD |
311 | /* For handling RTLD_NEXT we must be able to skip shared objects. */ |
312 | extern ElfW(Addr) _dl_lookup_symbol_skip (const char *undef, | |
313 | const ElfW(Sym) **sym, | |
314 | struct link_map *symbol_scope[], | |
315 | const char *reference_name, | |
bc9f6000 | 316 | struct link_map *skip_this); |
84384f5b | 317 | |
c84142e8 UD |
318 | /* For handling RTLD_NEXT with versioned symbols we must be able to |
319 | skip shared objects. */ | |
320 | extern ElfW(Addr) _dl_lookup_versioned_symbol_skip (const char *undef, | |
321 | const ElfW(Sym) **sym, | |
322 | struct link_map *symbol_scope[], | |
323 | const char *reference_name, | |
1fb05e3d | 324 | const struct r_found_version *version, |
bc9f6000 | 325 | struct link_map *skip_this); |
c84142e8 | 326 | |
ee188d55 | 327 | /* Look up symbol NAME in MAP's scope and return its run-time address. */ |
266180eb | 328 | extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name); |
ee188d55 RM |
329 | |
330 | ||
331 | /* Structure describing the dynamic linker itself. */ | |
332 | extern struct link_map _dl_rtld_map; | |
d66e34cd | 333 | |
ba79d61b RM |
334 | /* The list of objects currently loaded is the third element of the |
335 | `_dl_default_scope' array, and the fourth element is always null. | |
336 | This leaves two slots before it that are used when resolving | |
337 | DT_SYMBOLIC objects' references one after it for normal references | |
338 | (see below). */ | |
339 | #define _dl_loaded (_dl_default_scope[2]) | |
340 | extern struct link_map *_dl_default_scope[5]; | |
341 | ||
342 | /* Null-terminated list of objects in the dynamic `global scope'. The | |
343 | list starts at [2]; i.e. &_dl_global_scope[2] is the argument | |
344 | passed to _dl_lookup_symbol to search the global scope. To search | |
345 | a specific object and its dependencies in preference to the global | |
346 | scope, fill in the [1] slot and pass its address; for two specific | |
347 | object scopes, fill [0] and [1]. The list is double-terminated; to | |
348 | search the global scope and then a specific object and its | |
349 | dependencies, set *_dl_global_scope_end. This variable initially | |
350 | points to _dl_default_scope, and _dl_loaded is always kept in [2] | |
351 | of this list. A new list is malloc'd when new objects are loaded | |
352 | with RTLD_GLOBAL. */ | |
353 | extern struct link_map **_dl_global_scope, **_dl_global_scope_end; | |
354 | extern size_t _dl_global_scope_alloc; /* Number of slots malloc'd. */ | |
355 | ||
356 | /* Hack _dl_global_scope[0] and [1] as necessary, and return a pointer into | |
357 | _dl_global_scope that should be passed to _dl_lookup_symbol for symbol | |
358 | references made in the object MAP's relocations. */ | |
359 | extern struct link_map **_dl_object_relocation_scope (struct link_map *map); | |
d66e34cd | 360 | |
d66e34cd RM |
361 | |
362 | /* Allocate a `struct link_map' for a new object being loaded, | |
363 | and enter it into the _dl_loaded list. */ | |
364 | extern struct link_map *_dl_new_object (char *realname, const char *libname, | |
365 | int type); | |
366 | ||
367 | /* Relocate the given object (if it hasn't already been). | |
ba79d61b | 368 | SCOPE is passed to _dl_lookup_symbol in symbol lookups. |
d66e34cd | 369 | If LAZY is nonzero, don't relocate its PLT. */ |
ba79d61b RM |
370 | extern void _dl_relocate_object (struct link_map *map, |
371 | struct link_map *scope[], | |
372 | int lazy); | |
d66e34cd | 373 | |
c84142e8 UD |
374 | /* Check the version dependencies of all objects available through |
375 | MAP. If VERBOSE print some more diagnostics. */ | |
376 | extern int _dl_check_all_versions (struct link_map *map, int verbose); | |
377 | ||
378 | /* Check the version dependencies for MAP. If VERBOSE print some more | |
379 | diagnostics. */ | |
380 | extern int _dl_check_map_versions (struct link_map *map, int verbose); | |
381 | ||
f68b86cc RM |
382 | /* Return the address of the next initializer function for MAP or one of |
383 | its dependencies that has not yet been run. When there are no more | |
384 | initializers to be run, this returns zero. The functions are returned | |
385 | in the order they should be called. */ | |
266180eb | 386 | extern ElfW(Addr) _dl_init_next (struct link_map *map); |
d66e34cd RM |
387 | |
388 | /* Call the finalizer functions of all shared objects whose | |
389 | initializer functions have completed. */ | |
390 | extern void _dl_fini (void); | |
391 | ||
392 | /* The dynamic linker calls this function before and having changing | |
393 | any shared object mappings. The `r_state' member of `struct r_debug' | |
394 | says what change is taking place. This function's address is | |
395 | the value of the `r_brk' member. */ | |
4d6acc61 RM |
396 | extern void _dl_debug_state (void); |
397 | ||
398 | /* Initialize `struct r_debug' if it has not already been done. The | |
399 | argument is the run-time load address of the dynamic linker, to be put | |
400 | in the `r_ldbase' member. Returns the address of the structure. */ | |
401 | extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase); | |
d66e34cd | 402 | |
1fb05e3d | 403 | __END_DECLS |
d66e34cd RM |
404 | |
405 | #endif /* link.h */ |