]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/link.h
update from main archive 961019
[thirdparty/glibc.git] / elf / link.h
CommitLineData
d66e34cd 1/* Run-time dynamic linker data structures for loaded ELF shared objects.
a2e1b046 2Copyright (C) 1995, 1996 Free Software Foundation, Inc.
d66e34cd
RM
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA. */
19
20#ifndef _LINK_H
21#define _LINK_H 1
22
266180eb
RM
23#define __need_size_t
24#include <stddef.h>
25
d66e34cd
RM
26#include <elf.h>
27
266180eb
RM
28/* We use this macro to refer to ELF types independent of the native wordsize.
29 `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */
567c63af
RM
30#define ElfW(type) _ElfW (Elf, __ELF_NATIVE_CLASS, type)
31#define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type)
266180eb
RM
32#define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
33#define _ElfW_1(e,w,t) e##w##t
567c63af 34#include <elfclass.h> /* Defines __ELF_NATIVE_CLASS. */
d66e34cd
RM
35
36/* Rendezvous structure used by the run-time dynamic linker to communicate
37 details of shared object loading to the debugger. If the executable's
38 dynamic section has a DT_DEBUG element, the run-time linker sets that
39 element's value to the address where this structure can be found. */
40
41struct r_debug
42 {
43 int r_version; /* Version number for this protocol. */
44
45 struct link_map *r_map; /* Head of the chain of loaded objects. */
46
47 /* This is the address of a function internal to the run-time linker,
48 that will always be called when the linker begins to map in a
49 library or unmap it, and again when the mapping change is complete.
50 The debugger can set a breakpoint at this address if it wants to
51 notice shared object mapping changes. */
266180eb 52 ElfW(Addr) r_brk;
d66e34cd
RM
53 enum
54 {
55 /* This state value describes the mapping change taking place when
56 the `r_brk' address is called. */
57 RT_CONSISTENT, /* Mapping change is complete. */
58 RT_ADD, /* Beginning to add a new object. */
59 RT_DELETE, /* Beginning to remove an object mapping. */
60 } r_state;
61
266180eb 62 ElfW(Addr) r_ldbase; /* Base address the linker is loaded at. */
d66e34cd
RM
63 };
64
4d6acc61
RM
65/* This is the instance of that structure used by the dynamic linker. */
66extern struct r_debug _r_debug;
67
d66e34cd
RM
68/* This symbol refers to the "dynamic structure" in the `.dynamic' section
69 of whatever module refers to `_DYNAMIC'. So, to find its own
70 `struct r_debug', a program could do:
71 for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL)
72 if (dyn->d_tag == DT_DEBUG) r_debug = (struct r_debug) dyn->d_un.d_ptr;
73 */
74
266180eb 75extern ElfW(Dyn) _DYNAMIC[];
d66e34cd
RM
76
77
78/* Structure describing a loaded shared object. The `l_next' and `l_prev'
79 members form a chain of all the shared objects loaded at startup.
80
81 These data structures exist in space used by the run-time dynamic linker;
82 modifying them may have disastrous results. */
83
84struct link_map
85 {
86 /* These first few members are part of the protocol with the debugger.
87 This is the same format used in SVR4. */
88
266180eb 89 ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
d66e34cd 90 char *l_name; /* Absolute file name object was found in. */
266180eb 91 ElfW(Dyn) *l_ld; /* Dynamic section of the shared object. */
d66e34cd
RM
92 struct link_map *l_next, *l_prev; /* Chain of loaded objects. */
93
94 /* All following members are internal to the dynamic linker.
95 They may change without notice. */
96
97 const char *l_libname; /* Name requested (before search). */
a2e1b046
RM
98 /* Indexed pointers to dynamic section.
99 [0,DT_NUM) are indexed by the processor-independent tags.
100 [DT_NUM,DT_NUM+DT_PROCNUM] are indexed by the tag minus DT_LOPROC. */
266180eb
RM
101 ElfW(Dyn) *l_info[DT_NUM + DT_PROCNUM];
102 const ElfW(Phdr) *l_phdr; /* Pointer to program header table in core. */
103 ElfW(Addr) l_entry; /* Entry point location. */
104 ElfW(Half) l_phnum; /* Number of program header entries. */
d66e34cd 105
efec1d0c
RM
106 /* Array of DT_NEEDED dependencies and their dependencies, in
107 dependency order for symbol lookup. This is null before the
108 dependencies have been loaded. */
109 struct link_map **l_searchlist;
110 unsigned int l_nsearchlist;
111
ba79d61b
RM
112 /* Dependent object that first caused this object to be loaded. */
113 struct link_map *l_loader;
114
d66e34cd 115 /* Symbol hash table. */
8d6468d0
RM
116 ElfW(Symndx) l_nbuckets;
117 const ElfW(Symndx) *l_buckets, *l_chain;
d66e34cd
RM
118
119 unsigned int l_opencount; /* Reference count for dlopen/dlclose. */
120 enum /* Where this object came from. */
121 {
122 lt_executable, /* The main executable program. */
d66e34cd
RM
123 lt_library, /* Library needed by main executable. */
124 lt_loaded, /* Extra run-time loaded shared object. */
125 } l_type:2;
d66e34cd
RM
126 unsigned int l_relocated:1; /* Nonzero if object's relocations done. */
127 unsigned int l_init_called:1; /* Nonzero if DT_INIT function called. */
128 unsigned int l_init_running:1; /* Nonzero while DT_INIT function runs. */
ba79d61b
RM
129 unsigned int l_global:1; /* Nonzero if object in _dl_global_scope. */
130 unsigned int l_reserved:2; /* Reserved for internal use. */
d66e34cd
RM
131 };
132\f
133/* Internal functions of the run-time dynamic linker.
134 These can be accessed if you link again the dynamic linker
135 as a shared library, as in `-lld' or `/lib/ld.so' explicitly;
136 but are not normally of interest to user programs.
137
138 The `-ldl' library functions in <dlfcn.h> provide a simple
139 user interface to run-time dynamic linking. */
140
141
266180eb
RM
142/* Cached value of `getpagesize ()'. */
143extern size_t _dl_pagesize;
144
d66e34cd
RM
145/* File descriptor referring to the zero-fill device. */
146extern int _dl_zerofd;
147
148/* OS-dependent function to open the zero-fill device. */
149extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */
150
6a76c115
RM
151/* OS-dependent function to write a message on the standard output.
152 All arguments are `const char *'; args until a null pointer
153 are concatenated to form the message to print. */
154extern void _dl_sysdep_message (const char *string, ...);
155
d66e34cd
RM
156/* OS-dependent function to give a fatal error message and exit
157 when the dynamic linker fails before the program is fully linked.
158 All arguments are `const char *'; args until a null pointer
159 are concatenated to form the message to print. */
160extern void _dl_sysdep_fatal (const char *string, ...)
161 __attribute__ ((__noreturn__));
162
163/* Nonzero if the program should be "secure" (i.e. it's setuid or somesuch).
164 This tells the dynamic linker to ignore environment variables. */
165extern int _dl_secure;
166
167/* This function is called by all the internal dynamic linker functions
421f82e5
RM
168 when they encounter an error. ERRCODE is either an `errno' code or
169 zero; OBJECT is the name of the problematical shared object, or null if
170 it is a general problem; ERRSTRING is a string describing the specific
171 problem. */
a2e1b046 172
421f82e5
RM
173extern void _dl_signal_error (int errcode,
174 const char *object,
175 const char *errstring)
d66e34cd
RM
176 __attribute__ ((__noreturn__));
177
178/* Call OPERATE, catching errors from `dl_signal_error'. If there is no
421f82e5
RM
179 error, *ERRSTRING is set to null. If there is an error, *ERRSTRING and
180 *OBJECT are set to the strings passed to _dl_signal_error, and the error
14bab8de
UD
181 code passed is the return value. ERRSTRING if nonzero points to a
182 malloc'ed string which the caller has to free after use. */
183extern int _dl_catch_error (char **errstring,
421f82e5
RM
184 const char **object,
185 void (*operate) (void));
d66e34cd
RM
186
187
188/* Helper function for <dlfcn.h> functions. Runs the OPERATE function via
189 _dl_catch_error. Returns zero for success, nonzero for failure; and
190 arranges for `dlerror' to return the error details. */
191extern int _dlerror_run (void (*operate) (void));
192
193
194/* Open the shared object NAME and map in its segments.
195 LOADER's DT_RPATH is used in searching for NAME.
d66e34cd
RM
196 If the object is already opened, returns its existing map. */
197extern struct link_map *_dl_map_object (struct link_map *loader,
ba79d61b 198 const char *name, int type);
ea03559a 199
2064087b
RM
200/* Call _dl_map_object on the dependencies of MAP, and set up
201 MAP->l_searchlist. PRELOADS points to a vector of NPRELOADS previously
202 loaded objects that will be inserted into MAP->l_searchlist after MAP
203 but before its dependencies. */
204extern void _dl_map_object_deps (struct link_map *map,
205 struct link_map **preloads,
206 unsigned int npreloads);
efec1d0c 207
d66e34cd
RM
208/* Cache the locations of MAP's hash table. */
209extern void _dl_setup_hash (struct link_map *map);
210
211
efec1d0c 212/* Open the shared object NAME, relocate it, and run its initializer if it
a23db8e4
RM
213 hasn't already been run. MODE is as for `dlopen' (see <dlfcn.h>). If
214 the object is already opened, returns its existing map. */
215extern struct link_map *_dl_open (const char *name, int mode);
efec1d0c 216
ba79d61b
RM
217/* Close an object previously opened by _dl_open. */
218extern void _dl_close (struct link_map *map);
efec1d0c
RM
219
220
d66e34cd
RM
221/* Search loaded objects' symbol tables for a definition of the symbol
222 referred to by UNDEF. *SYM is the symbol table entry containing the
223 reference; it is replaced with the defining symbol, and the base load
ba79d61b
RM
224 address of the defining object is returned. SYMBOL_SCOPE is a
225 null-terminated list of object scopes to search; each object's
226 l_searchlist (i.e. the segment of the dependency tree starting at that
227 object) is searched in turn. REFERENCE_NAME should name the object
706074a5
UD
228 containing the reference; it is used in error messages. FLAGS is a
229 set of flags: */
230#define DL_LOOKUP_NOEXEC 1 /* Don't search the executable for a
231 definition; this is used for copy
232 relocs. */
233#define DL_LOOKUP_NOPLT 2 /* The reference must not be resolved
234 to a PLT entry. */
266180eb
RM
235extern ElfW(Addr) _dl_lookup_symbol (const char *undef,
236 const ElfW(Sym) **sym,
ba79d61b 237 struct link_map *symbol_scope[],
f2b0f935 238 const char *reference_name,
706074a5 239 int flags);
d66e34cd 240
ee188d55 241/* Look up symbol NAME in MAP's scope and return its run-time address. */
266180eb 242extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name);
ee188d55
RM
243
244
245/* Structure describing the dynamic linker itself. */
246extern struct link_map _dl_rtld_map;
d66e34cd 247
ba79d61b
RM
248/* The list of objects currently loaded is the third element of the
249 `_dl_default_scope' array, and the fourth element is always null.
250 This leaves two slots before it that are used when resolving
251 DT_SYMBOLIC objects' references one after it for normal references
252 (see below). */
253#define _dl_loaded (_dl_default_scope[2])
254extern struct link_map *_dl_default_scope[5];
255
256/* Null-terminated list of objects in the dynamic `global scope'. The
257 list starts at [2]; i.e. &_dl_global_scope[2] is the argument
258 passed to _dl_lookup_symbol to search the global scope. To search
259 a specific object and its dependencies in preference to the global
260 scope, fill in the [1] slot and pass its address; for two specific
261 object scopes, fill [0] and [1]. The list is double-terminated; to
262 search the global scope and then a specific object and its
263 dependencies, set *_dl_global_scope_end. This variable initially
264 points to _dl_default_scope, and _dl_loaded is always kept in [2]
265 of this list. A new list is malloc'd when new objects are loaded
266 with RTLD_GLOBAL. */
267extern struct link_map **_dl_global_scope, **_dl_global_scope_end;
268extern size_t _dl_global_scope_alloc; /* Number of slots malloc'd. */
269
270/* Hack _dl_global_scope[0] and [1] as necessary, and return a pointer into
271 _dl_global_scope that should be passed to _dl_lookup_symbol for symbol
272 references made in the object MAP's relocations. */
273extern struct link_map **_dl_object_relocation_scope (struct link_map *map);
d66e34cd 274
d66e34cd
RM
275
276/* Allocate a `struct link_map' for a new object being loaded,
277 and enter it into the _dl_loaded list. */
278extern struct link_map *_dl_new_object (char *realname, const char *libname,
279 int type);
280
281/* Relocate the given object (if it hasn't already been).
ba79d61b 282 SCOPE is passed to _dl_lookup_symbol in symbol lookups.
d66e34cd 283 If LAZY is nonzero, don't relocate its PLT. */
ba79d61b
RM
284extern void _dl_relocate_object (struct link_map *map,
285 struct link_map *scope[],
286 int lazy);
d66e34cd 287
f68b86cc
RM
288/* Return the address of the next initializer function for MAP or one of
289 its dependencies that has not yet been run. When there are no more
290 initializers to be run, this returns zero. The functions are returned
291 in the order they should be called. */
266180eb 292extern ElfW(Addr) _dl_init_next (struct link_map *map);
d66e34cd
RM
293
294/* Call the finalizer functions of all shared objects whose
295 initializer functions have completed. */
296extern void _dl_fini (void);
297
298/* The dynamic linker calls this function before and having changing
299 any shared object mappings. The `r_state' member of `struct r_debug'
300 says what change is taking place. This function's address is
301 the value of the `r_brk' member. */
4d6acc61
RM
302extern void _dl_debug_state (void);
303
304/* Initialize `struct r_debug' if it has not already been done. The
305 argument is the run-time load address of the dynamic linker, to be put
306 in the `r_ldbase' member. Returns the address of the structure. */
307extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase);
d66e34cd
RM
308
309
310#endif /* link.h */