1 /* Run-time dynamic linker data structures for loaded ELF shared objects.
2 Copyright (C) 1995, 1996 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. */
28 #define __ELF_WORDSIZE 32 /* XXX */
30 /* We use this macro to refer to ELF types independent of the native wordsize.
31 `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */
32 #define ElfW(type) _ElfW (Elf, __ELF_WORDSIZE, type)
33 #define ELFW(type) _ElfW (ELF, __ELF_WORDSIZE, type)
34 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
35 #define _ElfW_1(e,w,t) e##w##t
38 /* Rendezvous structure used by the run-time dynamic linker to communicate
39 details of shared object loading to the debugger. If the executable's
40 dynamic section has a DT_DEBUG element, the run-time linker sets that
41 element's value to the address where this structure can be found. */
45 int r_version; /* Version number for this protocol. */
47 struct link_map *r_map; /* Head of the chain of loaded objects. */
49 /* This is the address of a function internal to the run-time linker,
50 that will always be called when the linker begins to map in a
51 library or unmap it, and again when the mapping change is complete.
52 The debugger can set a breakpoint at this address if it wants to
53 notice shared object mapping changes. */
57 /* This state value describes the mapping change taking place when
58 the `r_brk' address is called. */
59 RT_CONSISTENT, /* Mapping change is complete. */
60 RT_ADD, /* Beginning to add a new object. */
61 RT_DELETE, /* Beginning to remove an object mapping. */
64 ElfW(Addr) r_ldbase; /* Base address the linker is loaded at. */
67 /* This symbol refers to the "dynamic structure" in the `.dynamic' section
68 of whatever module refers to `_DYNAMIC'. So, to find its own
69 `struct r_debug', a program could do:
70 for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL)
71 if (dyn->d_tag == DT_DEBUG) r_debug = (struct r_debug) dyn->d_un.d_ptr;
74 extern ElfW(Dyn) _DYNAMIC[];
77 /* Structure describing a loaded shared object. The `l_next' and `l_prev'
78 members form a chain of all the shared objects loaded at startup.
80 These data structures exist in space used by the run-time dynamic linker;
81 modifying them may have disastrous results. */
85 /* These first few members are part of the protocol with the debugger.
86 This is the same format used in SVR4. */
88 ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
89 char *l_name; /* Absolute file name object was found in. */
90 ElfW(Dyn) *l_ld; /* Dynamic section of the shared object. */
91 struct link_map *l_next, *l_prev; /* Chain of loaded objects. */
93 /* All following members are internal to the dynamic linker.
94 They may change without notice. */
96 const char *l_libname; /* Name requested (before search). */
97 /* Indexed pointers to dynamic section.
98 [0,DT_NUM) are indexed by the processor-independent tags.
99 [DT_NUM,DT_NUM+DT_PROCNUM] are indexed by the tag minus DT_LOPROC. */
100 ElfW(Dyn) *l_info[DT_NUM + DT_PROCNUM];
101 const ElfW(Phdr) *l_phdr; /* Pointer to program header table in core. */
102 ElfW(Addr) l_entry; /* Entry point location. */
103 ElfW(Half) l_phnum; /* Number of program header entries. */
105 /* Array of DT_NEEDED dependencies and their dependencies, in
106 dependency order for symbol lookup. This is null before the
107 dependencies have been loaded. */
108 struct link_map **l_searchlist;
109 unsigned int l_nsearchlist;
111 /* Symbol hash table. */
112 ElfW(Word) l_nbuckets;
113 const ElfW(Word) *l_buckets, *l_chain;
115 unsigned int l_opencount; /* Reference count for dlopen/dlclose. */
116 enum /* Where this object came from. */
118 lt_executable, /* The main executable program. */
119 lt_interpreter, /* The interpreter: the dynamic linker. */
120 lt_library, /* Library needed by main executable. */
121 lt_loaded, /* Extra run-time loaded shared object. */
123 unsigned int l_relocated:1; /* Nonzero if object's relocations done. */
124 unsigned int l_init_called:1; /* Nonzero if DT_INIT function called. */
125 unsigned int l_init_running:1; /* Nonzero while DT_INIT function runs. */
126 unsigned int l_reserved:3; /* Reserved for internal use. */
129 /* Internal functions of the run-time dynamic linker.
130 These can be accessed if you link again the dynamic linker
131 as a shared library, as in `-lld' or `/lib/ld.so' explicitly;
132 but are not normally of interest to user programs.
134 The `-ldl' library functions in <dlfcn.h> provide a simple
135 user interface to run-time dynamic linking. */
138 /* Cached value of `getpagesize ()'. */
139 extern size_t _dl_pagesize;
141 /* File descriptor referring to the zero-fill device. */
142 extern int _dl_zerofd;
144 /* OS-dependent function to open the zero-fill device. */
145 extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */
147 /* OS-dependent function to write a message on the standard output.
148 All arguments are `const char *'; args until a null pointer
149 are concatenated to form the message to print. */
150 extern void _dl_sysdep_message (const char *string, ...);
152 /* OS-dependent function to give a fatal error message and exit
153 when the dynamic linker fails before the program is fully linked.
154 All arguments are `const char *'; args until a null pointer
155 are concatenated to form the message to print. */
156 extern void _dl_sysdep_fatal (const char *string, ...)
157 __attribute__ ((__noreturn__));
159 /* Nonzero if the program should be "secure" (i.e. it's setuid or somesuch).
160 This tells the dynamic linker to ignore environment variables. */
161 extern int _dl_secure;
163 /* This function is called by all the internal dynamic linker functions
164 when they encounter an error. ERRCODE is either an `errno' code or
165 zero; OBJECT is the name of the problematical shared object, or null if
166 it is a general problem; ERRSTRING is a string describing the specific
169 extern void _dl_signal_error (int errcode,
171 const char *errstring)
172 __attribute__ ((__noreturn__));
174 /* Call OPERATE, catching errors from `dl_signal_error'. If there is no
175 error, *ERRSTRING is set to null. If there is an error, *ERRSTRING and
176 *OBJECT are set to the strings passed to _dl_signal_error, and the error
177 code passed is the return value. */
178 extern int _dl_catch_error (const char **errstring,
180 void (*operate) (void));
183 /* Helper function for <dlfcn.h> functions. Runs the OPERATE function via
184 _dl_catch_error. Returns zero for success, nonzero for failure; and
185 arranges for `dlerror' to return the error details. */
186 extern int _dlerror_run (void (*operate) (void));
189 /* Open the shared object NAME and map in its segments.
190 LOADER's DT_RPATH is used in searching for NAME.
191 If the object is already opened, returns its existing map. */
192 extern struct link_map *_dl_map_object (struct link_map *loader,
195 /* Similar, but file found at REALNAME and opened on FD.
196 REALNAME must malloc'd storage and is used in internal data structures. */
197 extern struct link_map *_dl_map_object_from_fd (const char *name,
198 int fd, char *realname);
200 /* Call _dl_map_object on the dependencies of MAP, and
201 set up MAP->l_searchlist. */
202 extern void _dl_map_object_deps (struct link_map *map);
204 /* Cache the locations of MAP's hash table. */
205 extern void _dl_setup_hash (struct link_map *map);
208 /* Open the shared object NAME, relocate it, and run its initializer if it
209 hasn't already been run. LOADER's DT_RPATH is used in searching for
210 NAME. MODE is as for `dlopen' (see <dlfcn.h>). If the object is
211 already opened, returns its existing map. */
212 extern struct link_map *_dl_open (struct link_map *loader,
213 const char *name, int mode);
217 /* Search loaded objects' symbol tables for a definition of the symbol
218 referred to by UNDEF. *SYM is the symbol table entry containing the
219 reference; it is replaced with the defining symbol, and the base load
220 address of the defining object is returned. Each of SYMBOL_SCOPE[0] and
221 SYMBOL_SCOPE[1] that is not null and their dependencies are searched to
222 resolve the name. REFERENCE_NAME should name the object containing the
223 reference; it is used in error messages. RELOC_ADDR is the address
224 being fixed up and the chosen symbol cannot be one with this value. If
225 NOPLT is nonzero, then the reference must not be resolved to a PLT
227 extern ElfW(Addr) _dl_lookup_symbol (const char *undef,
228 const ElfW(Sym) **sym,
229 struct link_map *symbol_scope[2],
230 const char *reference_name,
231 ElfW(Addr) reloc_addr,
234 /* Look up symbol NAME in MAP's scope and return its run-time address. */
235 extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name);
238 /* Structure describing the dynamic linker itself. */
239 extern struct link_map _dl_rtld_map;
241 /* List of objects currently loaded. */
242 extern struct link_map *_dl_loaded;
244 /* Tail of that list which were loaded at startup. */
245 extern struct link_map *_dl_startup_loaded;
247 /* Allocate a `struct link_map' for a new object being loaded,
248 and enter it into the _dl_loaded list. */
249 extern struct link_map *_dl_new_object (char *realname, const char *libname,
252 /* Relocate the given object (if it hasn't already been).
253 If LAZY is nonzero, don't relocate its PLT. */
254 extern void _dl_relocate_object (struct link_map *map, int lazy);
256 /* Return the address of the next initializer function for MAP or one of
257 its dependencies that has not yet been run. When there are no more
258 initializers to be run, this returns zero. The functions are returned
259 in the order they should be called. */
260 extern ElfW(Addr) _dl_init_next (struct link_map *map);
262 /* Call the finalizer functions of all shared objects whose
263 initializer functions have completed. */
264 extern void _dl_fini (void);
266 /* The dynamic linker calls this function before and having changing
267 any shared object mappings. The `r_state' member of `struct r_debug'
268 says what change is taking place. This function's address is
269 the value of the `r_brk' member. */
270 extern void _dl_r_debug_state (void);