1 /* Run-time dynamic linker data structures for loaded ELF shared objects.
2 Copyright (C) 1995-1999, 2000, 2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 #include <dl-lookupcfg.h>
34 #include <bits/libc-lock.h>
38 /* We use this macro to refer to ELF types independent of the native wordsize.
39 `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */
40 #define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type)
42 /* All references to the value of l_info[DT_PLTGOT],
43 l_info[DT_STRTAB], l_info[DT_SYMTAB], l_info[DT_RELA],
44 l_info[DT_REL], l_info[DT_JMPREL], and l_info[VERSYMIDX (DT_VERSYM)]
45 have to be accessed via the D_PTR macro. The macro is needed since for
46 most architectures the entry is already relocated - but for some not
47 and we need to relocate at access time. */
48 #ifdef DL_RO_DYN_SECTION
49 # define D_PTR(map,i) (map->i->d_un.d_ptr + map->l_addr)
51 # define D_PTR(map,i) map->i->d_un.d_ptr
54 /* On some platforms more information than just the address of the symbol
55 is needed from the lookup functions. In this case we return the whole
57 #ifdef DL_LOOKUP_RETURNS_MAP
58 typedef struct link_map *lookup_t;
59 # define LOOKUP_VALUE(map) map
60 # define LOOKUP_VALUE_ADDRESS(map) (map ? map->l_addr : 0)
62 typedef ElfW(Addr) lookup_t;
63 # define LOOKUP_VALUE(map) map->l_addr
64 # define LOOKUP_VALUE_ADDRESS(address) address
67 /* on some architectures a pointer to a function is not just a pointer
68 to the actual code of the function but rather an architecture
69 specific descriptor. */
70 #ifndef ELF_FUNCTION_PTR_IS_SPECIAL
71 # define DL_SYMBOL_ADDRESS(map, ref) \
72 (void *) (LOOKUP_VALUE_ADDRESS (map) + ref->st_value)
73 # define DL_LOOKUP_ADDRESS(addr) ((ElfW(Addr)) (addr))
74 # define DL_DT_INIT_ADDRESS(map, start) (start)
75 # define DL_DT_FINI_ADDRESS(map, start) (start)
78 /* Unmap a loaded object, called by _dl_close (). */
79 #ifndef DL_UNMAP_IS_SPECIAL
80 # define DL_UNMAP(map) \
81 __munmap ((void *) (map)->l_map_start, \
82 (map)->l_map_end - (map)->l_map_start)
85 /* By default we do not need special support to initialize DSOs loaded
86 by statically linked binaries. */
87 #ifndef DL_STATIC_INIT
88 # define DL_STATIC_INIT(map)
91 /* Reloc type classes as returned by elf_machine_type_class().
92 ELF_RTYPE_CLASS_PLT means this reloc should not be satisfied by
93 some PLT symbol, ELF_RTYPE_CLASS_COPY means this reloc should not be
94 satisfied by any symbol in the executable. */
95 #define ELF_RTYPE_CLASS_PLT 1
96 #define ELF_RTYPE_CLASS_COPY 2
98 /* For the version handling we need an array with only names and their
100 struct r_found_version
106 const char *filename;
109 /* We want to cache information about the searches for shared objects. */
111 enum r_dir_status { unknown, nonexisting, existing };
113 struct r_search_path_elem
115 /* This link is only used in the `all_dirs' member of `r_search_path'. */
116 struct r_search_path_elem *next;
118 /* Strings saying where the definition came from. */
122 /* Basename for this search path element. The string must end with
123 a slash character. */
127 enum r_dir_status status[0];
137 /* A data structure for a simple single linked list of strings. */
140 const char *name; /* Name requested (before search). */
141 struct libname_list *next; /* Link to next name for this object. */
142 int dont_free; /* Flag whether this element should be freed
143 if the object is not entirely unloaded. */
147 /* Test whether given NAME matches any of the names of the given object. */
149 __attribute__ ((unused))
150 _dl_name_match_p (const char *__name, struct link_map *__map)
152 int __found = strcmp (__name, __map->l_name) == 0;
153 struct libname_list *__runp = __map->l_libname;
155 while (! __found && __runp != NULL)
156 if (strcmp (__name, __runp->name) == 0)
159 __runp = __runp->next;
164 /* Function used as argument for `_dl_receive_error' function. The
165 arguments are the error code, error string, and the objname the
166 error occurred in. */
167 typedef void (*receiver_fct) (int, const char *, const char *);
169 /* Internal functions of the run-time dynamic linker.
170 These can be accessed if you link again the dynamic linker
171 as a shared library, as in `-lld' or `/lib/ld.so' explicitly;
172 but are not normally of interest to user programs.
174 The `-ldl' library functions in <dlfcn.h> provide a simple
175 user interface to run-time dynamic linking. */
178 /* Parameters passed to the dynamic linker. */
179 extern char **_dl_argv;
181 /* Cached value of `getpagesize ()'. */
182 extern size_t _dl_pagesize;
185 extern unsigned int _dl_osversion;
187 /* File descriptor referring to the zero-fill device. */
188 extern int _dl_zerofd;
190 /* Name of the shared object to be profiled (if any). */
191 extern const char *_dl_profile;
192 /* Map of shared object to be profiled. */
193 extern struct link_map *_dl_profile_map;
194 /* Filename of the output file. */
195 extern const char *_dl_profile_output;
197 /* If nonzero the appropriate debug information is printed. */
198 extern int _dl_debug_mask;
199 #define DL_DEBUG_LIBS (1 << 0)
200 #define DL_DEBUG_IMPCALLS (1 << 1)
201 #define DL_DEBUG_BINDINGS (1 << 2)
202 #define DL_DEBUG_SYMBOLS (1 << 3)
203 #define DL_DEBUG_VERSIONS (1 << 4)
204 #define DL_DEBUG_RELOC (1 << 5)
205 #define DL_DEBUG_FILES (1 << 6)
206 #define DL_DEBUG_STATISTICS (1 << 7)
208 /* Expect cache ID. */
209 extern int _dl_correct_cache_id;
211 /* Mask for hardware capabilities that are available. */
212 extern unsigned long int _dl_hwcap;
214 /* Mask for important hardware capabilities we honour. */
215 extern unsigned long int _dl_hwcap_mask;
217 /* File descriptor to write debug messages to. */
218 extern int _dl_debug_fd;
220 /* Names of shared object for which the RPATH should be ignored. */
221 extern const char *_dl_inhibit_rpath;
223 /* Nonzero if references should be treated as weak during runtime linking. */
224 extern int _dl_dynamic_weak;
226 /* The array with message we print as a last resort. */
227 extern const char _dl_out_of_memory[];
229 /* Nonzero if runtime lookups should not update the .got/.plt. */
230 extern int _dl_bind_not;
232 /* List of search directories. */
233 extern struct r_search_path_elem *_dl_all_dirs;
234 extern struct r_search_path_elem *_dl_init_all_dirs;
236 /* OS-dependent function to open the zero-fill device. */
237 extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */
240 /* During the program run we must not modify the global data of
241 loaded shared object simultanously in two threads. Therefore we
242 protect `_dl_open' and `_dl_close' in dl-close.c.
244 This must be a recursive lock since the initializer function of
245 the loaded object might as well require a call to this function.
246 At this time it is not anymore a problem to modify the tables. */
247 __libc_lock_define_recursive (extern, _dl_load_lock)
250 /* Write message on the debug file descriptor. The parameters are
251 interpreted as for a `printf' call. All the lines start with a
252 tag showing the PID. */
253 extern void _dl_debug_printf (const char *fmt, ...)
254 __attribute__ ((__format__ (__printf__, 1, 2)));
256 /* Write message on the debug file descriptor. The parameters are
257 interpreted as for a `printf' call. All the lines buf the first
258 start with a tag showing the PID. */
259 extern void _dl_debug_printf_c (const char *fmt, ...)
260 __attribute__ ((__format__ (__printf__, 1, 2)));
263 /* Write a message on the specified descriptor FD. The parameters are
264 interpreted as for a `printf' call. */
265 extern void _dl_dprintf (int fd, const char *fmt, ...)
266 __attribute__ ((__format__ (__printf__, 2, 3)));
268 /* Write a message on the specified descriptor standard output. The
269 parameters are interpreted as for a `printf' call. */
270 #define _dl_printf(fmt, args...) \
271 _dl_dprintf (STDOUT_FILENO, fmt, ##args)
273 /* Write a message on the specified descriptor standard error. The
274 parameters are interpreted as for a `printf' call. */
275 #define _dl_error_printf(fmt, args...) \
276 _dl_dprintf (STDERR_FILENO, fmt, ##args)
278 /* Write a message on the specified descriptor standard error and exit
279 the program. The parameters are interpreted as for a `printf' call. */
280 #define _dl_fatal_printf(fmt, args...) \
283 _dl_dprintf (STDERR_FILENO, fmt, ##args); \
289 /* This function is called by all the internal dynamic linker functions
290 when they encounter an error. ERRCODE is either an `errno' code or
291 zero; OBJECT is the name of the problematical shared object, or null if
292 it is a general problem; ERRSTRING is a string describing the specific
294 extern void _dl_signal_error (int errcode, const char *object,
295 const char *occurred, const char *errstring)
297 __attribute__ ((__noreturn__));
299 /* Like _dl_signal_error, but may return when called in the context of
300 _dl_receive_error. */
301 extern void _dl_signal_cerror (int errcode, const char *object,
302 const char *occation, const char *errstring)
305 /* Call OPERATE, receiving errors from `dl_signal_cerror'. Unlike
306 `_dl_catch_error' the operation is resumed after the OPERATE
308 ARGS is passed as argument to OPERATE. */
309 extern void _dl_receive_error (receiver_fct fct, void (*operate) (void *),
314 /* Open the shared object NAME and map in its segments.
315 LOADER's DT_RPATH is used in searching for NAME.
316 If the object is already opened, returns its existing map.
317 For preloaded shared objects PRELOADED is set to a non-zero
318 value to allow additional security checks. */
319 extern struct link_map *_dl_map_object (struct link_map *loader,
320 const char *name, int preloaded,
321 int type, int trace_mode, int mode)
324 /* Call _dl_map_object on the dependencies of MAP, and set up
325 MAP->l_searchlist. PRELOADS points to a vector of NPRELOADS previously
326 loaded objects that will be inserted into MAP->l_searchlist after MAP
327 but before its dependencies. */
328 extern void _dl_map_object_deps (struct link_map *map,
329 struct link_map **preloads,
330 unsigned int npreloads, int trace_mode)
333 /* Cache the locations of MAP's hash table. */
334 extern void _dl_setup_hash (struct link_map *map) internal_function;
337 /* Search loaded objects' symbol tables for a definition of the symbol
338 referred to by UNDEF. *SYM is the symbol table entry containing the
339 reference; it is replaced with the defining symbol, and the base load
340 address of the defining object is returned. SYMBOL_SCOPE is a
341 null-terminated list of object scopes to search; each object's
342 l_searchlist (i.e. the segment of the dependency tree starting at that
343 object) is searched in turn. REFERENCE_NAME should name the object
344 containing the reference; it is used in error messages.
345 TYPE_CLASS describes the type of symbol we are looking for. */
346 extern lookup_t _dl_lookup_symbol (const char *undef,
347 struct link_map *undef_map,
348 const ElfW(Sym) **sym,
349 struct r_scope_elem *symbol_scope[],
350 int type_class, int explicit)
353 /* Lookup versioned symbol. */
354 extern lookup_t _dl_lookup_versioned_symbol (const char *undef,
355 struct link_map *undef_map,
356 const ElfW(Sym) **sym,
357 struct r_scope_elem *symbol_scope[],
358 const struct r_found_version *version,
359 int type_class, int explicit)
362 /* For handling RTLD_NEXT we must be able to skip shared objects. */
363 extern lookup_t _dl_lookup_symbol_skip (const char *undef,
364 struct link_map *undef_map,
365 const ElfW(Sym) **sym,
366 struct r_scope_elem *symbol_scope[],
367 struct link_map *skip_this)
370 /* For handling RTLD_NEXT with versioned symbols we must be able to
371 skip shared objects. */
372 extern lookup_t _dl_lookup_versioned_symbol_skip (const char *undef,
373 struct link_map *undef_map,
374 const ElfW(Sym) **sym,
375 struct r_scope_elem *symbol_scope[],
376 const struct r_found_version *version,
377 struct link_map *skip_this)
380 /* Look up symbol NAME in MAP's scope and return its run-time address. */
381 extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name)
385 /* Structure describing the dynamic linker itself. */
386 extern struct link_map _dl_rtld_map;
387 /* And a pointer to the map for the main map. */
388 extern struct link_map *_dl_loaded;
389 /* Number of object in the _dl_loaded list. */
390 extern unsigned int _dl_nloaded;
391 /* Array representing global scope. */
392 extern struct r_scope_elem *_dl_global_scope[2];
393 /* Direct pointer to the searchlist of the main object. */
394 extern struct r_scope_elem *_dl_main_searchlist;
395 /* Copy of the content of `_dl_main_searchlist'. */
396 extern struct r_scope_elem _dl_initial_searchlist;
397 /* This is zero at program start to signal that the global scope map is
398 allocated by rtld. Later it keeps the size of the map. It might be
399 reset if in _dl_close if the last global object is removed. */
400 extern size_t _dl_global_scope_alloc;
402 /* Allocate a `struct link_map' for a new object being loaded,
403 and enter it into the _dl_main_map list. */
404 extern struct link_map *_dl_new_object (char *realname, const char *libname,
405 int type, struct link_map *loader)
408 /* Relocate the given object (if it hasn't already been).
409 SCOPE is passed to _dl_lookup_symbol in symbol lookups.
410 If LAZY is nonzero, don't relocate its PLT. */
411 extern void _dl_relocate_object (struct link_map *map,
412 struct r_scope_elem *scope[],
413 int lazy, int consider_profiling);
415 /* Call _dl_signal_error with a message about an unhandled reloc type.
416 TYPE is the result of ELFW(R_TYPE) (r_info), i.e. an R_<CPU>_* value.
417 PLT is nonzero if this was a PLT reloc; it just affects the message. */
418 extern void _dl_reloc_bad_type (struct link_map *map,
419 uint_fast8_t type, int plt)
421 __attribute__ ((__noreturn__));
423 /* Check the version dependencies of all objects available through
424 MAP. If VERBOSE print some more diagnostics. */
425 extern int _dl_check_all_versions (struct link_map *map, int verbose,
429 /* Check the version dependencies for MAP. If VERBOSE print some more
431 extern int _dl_check_map_versions (struct link_map *map, int verbose,
435 /* Initialize the object in SCOPE by calling the constructors with
436 ARGC, ARGV, and ENV as the parameters. */
437 extern void _dl_init (struct link_map *main_map, int argc, char **argv,
438 char **env) internal_function;
440 /* Call the finalizer functions of all shared objects whose
441 initializer functions have completed. */
442 extern void _dl_fini (void) internal_function;
444 /* The dynamic linker calls this function before and having changing
445 any shared object mappings. The `r_state' member of `struct r_debug'
446 says what change is taking place. This function's address is
447 the value of the `r_brk' member. */
448 extern void _dl_debug_state (void);
450 /* Initialize `struct r_debug' if it has not already been done. The
451 argument is the run-time load address of the dynamic linker, to be put
452 in the `r_ldbase' member. Returns the address of the structure. */
453 extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase)
456 /* Initialize the basic data structure for the search paths. */
457 extern void _dl_init_paths (const char *library_path) internal_function;
459 /* Gather the information needed to install the profiling tables and start
461 extern void _dl_start_profile (struct link_map *map, const char *output_dir)
464 /* The actual functions used to keep book on the calls. */
465 extern void _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc);
467 /* This function is simply a wrapper around the _dl_mcount function
468 which does not require a FROMPC parameter since this is the
470 extern void _dl_mcount_wrapper (void *selfpc);
472 /* Show the members of the auxiliary array passed up from the kernel. */
473 extern void _dl_show_auxv (void) internal_function;
475 /* Return all environment variables starting with `LD_', one after the
477 extern char *_dl_next_ld_env_entry (char ***position) internal_function;
479 /* Return an array with the names of the important hardware capabilities. */
480 extern const struct r_strlenpair *_dl_important_hwcaps (const char *platform,
483 size_t *max_capstrlen)
486 /* Look up NAME in ld.so.cache and return the file name stored there,
487 or null if none is found. */
488 extern const char *_dl_load_cache_lookup (const char *name)
491 /* If the system does not support MAP_COPY we cannot leave the file open
492 all the time since this would create problems when the file is replaced.
493 Therefore we provide this function to close the file and open it again
495 extern void _dl_unload_cache (void);
497 /* System-dependent function to read a file's whole contents
498 in the most convenient manner available. */
499 extern void *_dl_sysdep_read_whole_file (const char *file, size_t *sizep,
503 /* System-specific function to do initial startup for the dynamic linker.
504 After this, file access calls and getenv must work. This is responsible
505 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
506 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
507 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
508 void (*dl_main) (const ElfW(Phdr) *phdr,
510 ElfW(Addr) *user_entry));
512 extern void _dl_sysdep_start_cleanup (void)
518 #endif /* ldsodefs.h */