Update.
[glibc.git] / elf / link.h
blobc4b0a4fd9c164d7376b77fab1e9933396e1cca36
1 /* Run-time dynamic linker data structures for loaded ELF shared objects.
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. */
20 #ifndef _LINK_H
21 #define _LINK_H 1
23 #include <features.h>
25 #define __need_size_t
26 #define __need_NULL
27 #include <stddef.h>
29 #include <elf.h>
30 #include <dlfcn.h>
32 __BEGIN_DECLS
34 /* We use this macro to refer to ELF types independent of the native wordsize.
35 `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */
36 #define ElfW(type) _ElfW (Elf, __ELF_NATIVE_CLASS, type)
37 #define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type)
38 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
39 #define _ElfW_1(e,w,t) e##w##t
41 #include <bits/elfclass.h> /* Defines __ELF_NATIVE_CLASS. */
43 /* Rendezvous structure used by the run-time dynamic linker to communicate
44 details of shared object loading to the debugger. If the executable's
45 dynamic section has a DT_DEBUG element, the run-time linker sets that
46 element's value to the address where this structure can be found. */
48 struct r_debug
50 int r_version; /* Version number for this protocol. */
52 struct link_map *r_map; /* Head of the chain of loaded objects. */
54 /* This is the address of a function internal to the run-time linker,
55 that will always be called when the linker begins to map in a
56 library or unmap it, and again when the mapping change is complete.
57 The debugger can set a breakpoint at this address if it wants to
58 notice shared object mapping changes. */
59 ElfW(Addr) r_brk;
60 enum
62 /* This state value describes the mapping change taking place when
63 the `r_brk' address is called. */
64 RT_CONSISTENT, /* Mapping change is complete. */
65 RT_ADD, /* Beginning to add a new object. */
66 RT_DELETE /* Beginning to remove an object mapping. */
67 } r_state;
69 ElfW(Addr) r_ldbase; /* Base address the linker is loaded at. */
72 /* This is the instance of that structure used by the dynamic linker. */
73 extern struct r_debug _r_debug;
75 /* This symbol refers to the "dynamic structure" in the `.dynamic' section
76 of whatever module refers to `_DYNAMIC'. So, to find its own
77 `struct r_debug', a program could do:
78 for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL)
79 if (dyn->d_tag == DT_DEBUG) r_debug = (struct r_debug) dyn->d_un.d_ptr;
82 extern ElfW(Dyn) _DYNAMIC[];
84 /* For the version handling we need an array with only names and their
85 hash values. */
86 struct r_found_version
88 const char *name;
89 ElfW(Word) hash;
91 int hidden;
92 const char *filename;
95 /* We want to cache information about the searches for shared objects. */
97 enum r_dir_status { unknown, nonexisting, existing };
99 struct r_search_path_elem
101 const char *dirname;
103 size_t dirnamelen;
104 enum r_dir_status dirstatus;
106 size_t machdirnamelen;
107 enum r_dir_status machdirstatus;
109 /* This link is only used in the `all_dirs' member of `r_search_path'. */
110 struct r_search_path_elem *next;
114 /* Structure describing a loaded shared object. The `l_next' and `l_prev'
115 members form a chain of all the shared objects loaded at startup.
117 These data structures exist in space used by the run-time dynamic linker;
118 modifying them may have disastrous results. */
120 struct link_map
122 /* These first few members are part of the protocol with the debugger.
123 This is the same format used in SVR4. */
125 ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
126 char *l_name; /* Absolute file name object was found in. */
127 ElfW(Dyn) *l_ld; /* Dynamic section of the shared object. */
128 struct link_map *l_next, *l_prev; /* Chain of loaded objects. */
130 /* All following members are internal to the dynamic linker.
131 They may change without notice. */
133 struct libname_list
135 const char *name; /* Name requested (before search). */
136 struct libname_list *next; /* Link to next name for this object. */
137 } *l_libname;
138 /* Indexed pointers to dynamic section.
139 [0,DT_NUM) are indexed by the processor-independent tags.
140 [DT_NUM,DT_NUM+DT_PROCNUM) are indexed by the tag minus DT_LOPROC.
141 [DT_NUM+DT_PROCNUM,DT_NUM+DT_PROCNUM+DT_EXTRANUM) are indexed
142 by DT_EXTRATAGIDX(tagvalue) and
143 [DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM,
144 DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM)
145 are indexed by DT_EXTRATAGIDX(tagvalue) (see <elf.h>). */
147 ElfW(Dyn) *l_info[DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM];
148 const ElfW(Phdr) *l_phdr; /* Pointer to program header table in core. */
149 ElfW(Addr) l_entry; /* Entry point location. */
150 ElfW(Half) l_phnum; /* Number of program header entries. */
152 /* Array of DT_NEEDED dependencies and their dependencies, in
153 dependency order for symbol lookup. This is null before the
154 dependencies have been loaded. */
155 struct link_map **l_searchlist;
156 unsigned int l_nsearchlist;
158 /* We keep another list in which we keep duplicates. This is
159 needed in _dl_lookup_symbol_skip to implemented RTLD_NEXT. */
160 struct link_map **l_dupsearchlist;
161 unsigned int l_ndupsearchlist;
163 /* Dependent object that first caused this object to be loaded. */
164 struct link_map *l_loader;
166 /* Symbol hash table. */
167 ElfW(Symndx) l_nbuckets;
168 const ElfW(Symndx) *l_buckets, *l_chain;
170 unsigned int l_opencount; /* Reference count for dlopen/dlclose. */
171 enum /* Where this object came from. */
173 lt_executable, /* The main executable program. */
174 lt_library, /* Library needed by main executable. */
175 lt_loaded /* Extra run-time loaded shared object. */
176 } l_type:2;
177 unsigned int l_relocated:1; /* Nonzero if object's relocations done. */
178 unsigned int l_init_called:1; /* Nonzero if DT_INIT function called. */
179 unsigned int l_init_running:1; /* Nonzero while DT_INIT function runs. */
180 unsigned int l_global:1; /* Nonzero if object in _dl_global_scope. */
181 unsigned int l_reserved:2; /* Reserved for internal use. */
183 /* Array with version names. */
184 unsigned int l_nversions;
185 struct r_found_version *l_versions;
187 /* Collected information about own RPATH directories. */
188 struct r_search_path_elem **l_rpath_dirs;
192 /* Test whether given NAME matches any of the names of the given object. */
193 static __inline int
194 __attribute__ ((unused))
195 _dl_name_match_p (const char *__name, struct link_map *__map)
197 int __found = strcmp (__name, __map->l_name) == 0;
198 struct libname_list *__runp = __map->l_libname;
200 while (! __found && __runp != NULL)
201 if (strcmp (__name, __runp->name) == 0)
202 __found = 1;
203 else
204 __runp = __runp->next;
206 return __found;
209 /* Function used as argument for `_dl_receive_error' function. The
210 arguments are the error code, error string, and the objname the
211 error occurred in. */
212 typedef void (*receiver_fct) (int, const char *, const char *);
214 /* Internal functions of the run-time dynamic linker.
215 These can be accessed if you link again the dynamic linker
216 as a shared library, as in `-lld' or `/lib/ld.so' explicitly;
217 but are not normally of interest to user programs.
219 The `-ldl' library functions in <dlfcn.h> provide a simple
220 user interface to run-time dynamic linking. */
223 /* Cached value of `getpagesize ()'. */
224 extern size_t _dl_pagesize;
226 /* File descriptor referring to the zero-fill device. */
227 extern int _dl_zerofd;
229 /* Name of the shared object to be profiled (if any). */
230 extern const char *_dl_profile;
231 /* Map of shared object to be profiled. */
232 extern struct link_map *_dl_profile_map;
234 /* OS-dependent function to open the zero-fill device. */
235 extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */
237 /* OS-dependent function to write a message on the standard output.
238 All arguments are `const char *'; args until a null pointer
239 are concatenated to form the message to print. */
240 extern void _dl_sysdep_message (const char *string, ...);
242 /* OS-dependent function to write a message on the standard error.
243 All arguments are `const char *'; args until a null pointer
244 are concatenated to form the message to print. */
245 extern void _dl_sysdep_error (const char *string, ...);
247 /* OS-dependent function to give a fatal error message and exit
248 when the dynamic linker fails before the program is fully linked.
249 All arguments are `const char *'; args until a null pointer
250 are concatenated to form the message to print. */
251 extern void _dl_sysdep_fatal (const char *string, ...)
252 __attribute__ ((__noreturn__));
254 /* Nonzero if the program should be "secure" (i.e. it's setuid or somesuch).
255 This tells the dynamic linker to ignore environment variables. */
256 extern int _dl_secure;
258 /* This function is called by all the internal dynamic linker functions
259 when they encounter an error. ERRCODE is either an `errno' code or
260 zero; OBJECT is the name of the problematical shared object, or null if
261 it is a general problem; ERRSTRING is a string describing the specific
262 problem. */
263 extern void _dl_signal_error (int errcode,
264 const char *object,
265 const char *errstring);
267 /* Call OPERATE, catching errors from `dl_signal_error'. If there is no
268 error, *ERRSTRING is set to null. If there is an error, *ERRSTRING and
269 *OBJECT are set to the strings passed to _dl_signal_error, and the error
270 code passed is the return value. ERRSTRING if nonzero points to a
271 malloc'ed string which the caller has to free after use.
272 ARGS is passed as argument to OPERATE. */
273 extern int _dl_catch_error (char **errstring,
274 const char **object,
275 void (*operate) (void *),
276 void *args);
278 /* Call OPERATE, receiving errors from `dl_signal_error'. Unlike
279 `_dl_catch_error' the operation is resumed after the OPERATE
280 function returns.
281 ARGS is passed as argument to OPERATE. */
282 extern void _dl_receive_error (receiver_fct fct, void (*operate) (void *),
283 void *args);
286 /* Helper function for <dlfcn.h> functions. Runs the OPERATE function via
287 _dl_catch_error. Returns zero for success, nonzero for failure; and
288 arranges for `dlerror' to return the error details.
289 ARGS is passed as argument to OPERATE. */
290 extern int _dlerror_run (void (*operate) (void *), void *args);
293 /* Open the shared object NAME and map in its segments.
294 LOADER's DT_RPATH is used in searching for NAME.
295 If the object is already opened, returns its existing map. */
296 extern struct link_map *_dl_map_object (struct link_map *loader,
297 const char *name, int type,
298 int trace_mode);
300 /* Call _dl_map_object on the dependencies of MAP, and set up
301 MAP->l_searchlist. PRELOADS points to a vector of NPRELOADS previously
302 loaded objects that will be inserted into MAP->l_searchlist after MAP
303 but before its dependencies. */
304 extern void _dl_map_object_deps (struct link_map *map,
305 struct link_map **preloads,
306 unsigned int npreloads, int trace_mode);
308 /* Cache the locations of MAP's hash table. */
309 extern void _dl_setup_hash (struct link_map *map);
312 /* Open the shared object NAME, relocate it, and run its initializer if it
313 hasn't already been run. MODE is as for `dlopen' (see <dlfcn.h>). If
314 the object is already opened, returns its existing map. */
315 extern struct link_map *_dl_open (const char *name, int mode);
317 /* Close an object previously opened by _dl_open. */
318 extern void _dl_close (struct link_map *map);
321 /* Search loaded objects' symbol tables for a definition of the symbol
322 referred to by UNDEF. *SYM is the symbol table entry containing the
323 reference; it is replaced with the defining symbol, and the base load
324 address of the defining object is returned. SYMBOL_SCOPE is a
325 null-terminated list of object scopes to search; each object's
326 l_searchlist (i.e. the segment of the dependency tree starting at that
327 object) is searched in turn. REFERENCE_NAME should name the object
328 containing the reference; it is used in error messages.
329 RELOC_TYPE is a machine-dependent reloc type, which is passed to
330 the `elf_machine_lookup_*_p' macros in dl-machine.h to affect which
331 symbols can be chosen. */
332 extern ElfW(Addr) _dl_lookup_symbol (const char *undef,
333 const ElfW(Sym) **sym,
334 struct link_map *symbol_scope[],
335 const char *reference_name,
336 int reloc_type);
338 /* Lookup versioned symbol. */
339 extern ElfW(Addr) _dl_lookup_versioned_symbol (const char *undef,
340 const ElfW(Sym) **sym,
341 struct link_map *symbol_scope[],
342 const char *reference_name,
343 const struct r_found_version *version,
344 int reloc_type);
346 /* For handling RTLD_NEXT we must be able to skip shared objects. */
347 extern ElfW(Addr) _dl_lookup_symbol_skip (const char *undef,
348 const ElfW(Sym) **sym,
349 struct link_map *symbol_scope[],
350 const char *reference_name,
351 struct link_map *skip_this);
353 /* For handling RTLD_NEXT with versioned symbols we must be able to
354 skip shared objects. */
355 extern ElfW(Addr) _dl_lookup_versioned_symbol_skip (const char *undef,
356 const ElfW(Sym) **sym,
357 struct link_map *symbol_scope[],
358 const char *reference_name,
359 const struct r_found_version *version,
360 struct link_map *skip_this);
362 /* Locate shared object containing the given address. */
363 extern int _dl_addr (const void *address, Dl_info *info);
365 /* Look up symbol NAME in MAP's scope and return its run-time address. */
366 extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name);
369 /* Structure describing the dynamic linker itself. */
370 extern struct link_map _dl_rtld_map;
372 /* The list of objects currently loaded is the third element of the
373 `_dl_default_scope' array, and the fourth element is always null.
374 This leaves two slots before it that are used when resolving
375 DT_SYMBOLIC objects' references one after it for normal references
376 (see below). */
377 #define _dl_loaded (_dl_default_scope[2])
378 extern struct link_map *_dl_default_scope[5];
380 /* Null-terminated list of objects in the dynamic `global scope'. The
381 list starts at [2]; i.e. &_dl_global_scope[2] is the argument
382 passed to _dl_lookup_symbol to search the global scope. To search
383 a specific object and its dependencies in preference to the global
384 scope, fill in the [1] slot and pass its address; for two specific
385 object scopes, fill [0] and [1]. The list is double-terminated; to
386 search the global scope and then a specific object and its
387 dependencies, set *_dl_global_scope_end. This variable initially
388 points to _dl_default_scope, and _dl_loaded is always kept in [2]
389 of this list. A new list is malloc'd when new objects are loaded
390 with RTLD_GLOBAL. */
391 extern struct link_map **_dl_global_scope, **_dl_global_scope_end;
392 extern size_t _dl_global_scope_alloc; /* Number of slots malloc'd. */
394 /* Hack _dl_global_scope[0] and [1] as necessary, and return a pointer into
395 _dl_global_scope that should be passed to _dl_lookup_symbol for symbol
396 references made in the object MAP's relocations. */
397 extern struct link_map **_dl_object_relocation_scope (struct link_map *map);
400 /* Allocate a `struct link_map' for a new object being loaded,
401 and enter it into the _dl_loaded list. */
402 extern struct link_map *_dl_new_object (char *realname, const char *libname,
403 int type);
405 /* Relocate the given object (if it hasn't already been).
406 SCOPE is passed to _dl_lookup_symbol in symbol lookups.
407 If LAZY is nonzero, don't relocate its PLT. */
408 extern void _dl_relocate_object (struct link_map *map,
409 struct link_map *scope[],
410 int lazy);
412 /* Check the version dependencies of all objects available through
413 MAP. If VERBOSE print some more diagnostics. */
414 extern int _dl_check_all_versions (struct link_map *map, int verbose);
416 /* Check the version dependencies for MAP. If VERBOSE print some more
417 diagnostics. */
418 extern int _dl_check_map_versions (struct link_map *map, int verbose);
420 /* Return the address of the next initializer function for MAP or one of
421 its dependencies that has not yet been run. When there are no more
422 initializers to be run, this returns zero. The functions are returned
423 in the order they should be called. */
424 extern ElfW(Addr) _dl_init_next (struct link_map *map);
426 /* Call the finalizer functions of all shared objects whose
427 initializer functions have completed. */
428 extern void _dl_fini (void);
430 /* The dynamic linker calls this function before and having changing
431 any shared object mappings. The `r_state' member of `struct r_debug'
432 says what change is taking place. This function's address is
433 the value of the `r_brk' member. */
434 extern void _dl_debug_state (void);
436 /* Initialize `struct r_debug' if it has not already been done. The
437 argument is the run-time load address of the dynamic linker, to be put
438 in the `r_ldbase' member. Returns the address of the structure. */
439 extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase);
441 /* Initialize the basic data structure for the search paths. */
442 extern void _dl_init_paths (void);
444 /* Gather the information needed to install the profiling tables and start
445 the timers. */
446 extern void _dl_start_profile (struct link_map *map, const char *output_dir);
448 /* The actual functions used to keep book on the calls. */
449 extern void _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc);
452 /* Show the member of the auxiliry aray passed up from the kernel. */
453 extern void _dl_show_auxv (void);
455 __END_DECLS
457 #endif /* link.h */