* sysdeps/generic/ldsodefs.h (_dl_load_lock): Declare it here with
[platform/upstream/glibc.git] / sysdeps / generic / ldsodefs.h
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.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 #ifndef _LDSODEFS_H
21 #define _LDSODEFS_H     1
22
23 #include <features.h>
24
25 #define __need_size_t
26 #define __need_NULL
27 #include <stddef.h>
28 #include <string.h>
29
30 #include <elf.h>
31 #include <dlfcn.h>
32 #include <link.h>
33 #include <dl-lookupcfg.h>
34 #include <bits/libc-lock.h>
35
36 __BEGIN_DECLS
37
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)
41
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)
50 #else
51 # define D_PTR(map,i) map->i->d_un.d_ptr
52 #endif
53
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
56    link map.  */
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)
61 #else
62 typedef ElfW(Addr) lookup_t;
63 # define LOOKUP_VALUE(map) map->l_addr
64 # define LOOKUP_VALUE_ADDRESS(address) address
65 #endif
66
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)
76 #endif
77
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)
83 #endif
84
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)
89 #endif
90
91 /* For the version handling we need an array with only names and their
92    hash values.  */
93 struct r_found_version
94   {
95     const char *name;
96     ElfW(Word) hash;
97
98     int hidden;
99     const char *filename;
100   };
101
102 /* We want to cache information about the searches for shared objects.  */
103
104 enum r_dir_status { unknown, nonexisting, existing };
105
106 struct r_search_path_elem
107   {
108     /* This link is only used in the `all_dirs' member of `r_search_path'.  */
109     struct r_search_path_elem *next;
110
111     /* Strings saying where the definition came from.  */
112     const char *what;
113     const char *where;
114
115     /* Basename for this search path element.  The string must end with
116        a slash character.  */
117     const char *dirname;
118     size_t dirnamelen;
119
120     enum r_dir_status status[0];
121   };
122
123 struct r_strlenpair
124   {
125     const char *str;
126     size_t len;
127   };
128
129
130 /* A data structure for a simple single linked list of strings.  */
131 struct libname_list
132   {
133     const char *name;           /* Name requested (before search).  */
134     struct libname_list *next;  /* Link to next name for this object.  */
135     int dont_free;              /* Flag whether this element should be freed
136                                    if the object is not entirely unloaded.  */
137   };
138
139
140 /* Test whether given NAME matches any of the names of the given object.  */
141 static __inline int
142 __attribute__ ((unused))
143 _dl_name_match_p (const char *__name, struct link_map *__map)
144 {
145   int __found = strcmp (__name, __map->l_name) == 0;
146   struct libname_list *__runp = __map->l_libname;
147
148   while (! __found && __runp != NULL)
149     if (strcmp (__name, __runp->name) == 0)
150       __found = 1;
151     else
152       __runp = __runp->next;
153
154   return __found;
155 }
156
157 /* Function used as argument for `_dl_receive_error' function.  The
158    arguments are the error code, error string, and the objname the
159    error occurred in.  */
160 typedef void (*receiver_fct) (int, const char *, const char *);
161 \f
162 /* Internal functions of the run-time dynamic linker.
163    These can be accessed if you link again the dynamic linker
164    as a shared library, as in `-lld' or `/lib/ld.so' explicitly;
165    but are not normally of interest to user programs.
166
167    The `-ldl' library functions in <dlfcn.h> provide a simple
168    user interface to run-time dynamic linking.  */
169
170
171 /* Parameters passed to the dynamic linker.  */
172 extern char **_dl_argv;
173
174 /* Cached value of `getpagesize ()'.  */
175 extern size_t _dl_pagesize;
176
177 /* OS version.  */
178 extern unsigned int _dl_osversion;
179
180 /* File descriptor referring to the zero-fill device.  */
181 extern int _dl_zerofd;
182
183 /* Name of the shared object to be profiled (if any).  */
184 extern const char *_dl_profile;
185 /* Map of shared object to be profiled.  */
186 extern struct link_map *_dl_profile_map;
187 /* Filename of the output file.  */
188 extern const char *_dl_profile_output;
189
190 /* If nonzero the appropriate debug information is printed.  */
191 extern int _dl_debug_mask;
192 #define DL_DEBUG_LIBS       (1 << 0)
193 #define DL_DEBUG_IMPCALLS   (1 << 1)
194 #define DL_DEBUG_BINDINGS   (1 << 2)
195 #define DL_DEBUG_SYMBOLS    (1 << 3)
196 #define DL_DEBUG_VERSIONS   (1 << 4)
197 #define DL_DEBUG_RELOC      (1 << 5)
198 #define DL_DEBUG_FILES      (1 << 6)
199 #define DL_DEBUG_STATISTICS (1 << 7)
200
201 /* Expect cache ID.  */
202 extern int _dl_correct_cache_id;
203
204 /* Mask for hardware capabilities that are available.  */
205 extern unsigned long int _dl_hwcap;
206
207 /* Mask for important hardware capabilities we honour. */
208 extern unsigned long int _dl_hwcap_mask;
209
210 /* File descriptor to write debug messages to.  */
211 extern int _dl_debug_fd;
212
213 /* Names of shared object for which the RPATH should be ignored.  */
214 extern const char *_dl_inhibit_rpath;
215
216 /* Nonzero if references should be treated as weak during runtime linking.  */
217 extern int _dl_dynamic_weak;
218
219 /* The array with message we print as a last resort.  */
220 extern const char _dl_out_of_memory[];
221
222 /* Nonzero if runtime lookups should not update the .got/.plt.  */
223 extern int _dl_bind_not;
224
225 /* List of search directories.  */
226 extern struct r_search_path_elem *_dl_all_dirs;
227 extern struct r_search_path_elem *_dl_init_all_dirs;
228
229 /* OS-dependent function to open the zero-fill device.  */
230 extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */
231
232
233 /* During the program run we must not modify the global data of
234    loaded shared object simultanously in two threads.  Therefore we
235    protect `_dl_open' and `_dl_close' in dl-close.c.
236
237    This must be a recursive lock since the initializer function of
238    the loaded object might as well require a call to this function.
239    At this time it is not anymore a problem to modify the tables.  */
240 __libc_lock_define_recursive (extern, _dl_load_lock)
241
242
243 /* Write message on the debug file descriptor.  The parameters are
244    interpreted as for a `printf' call.  All the lines start with a
245    tag showing the PID.  */
246 extern void _dl_debug_printf (const char *fmt, ...)
247      __attribute__ ((__format__ (__printf__, 1, 2)));
248
249 /* Write message on the debug file descriptor.  The parameters are
250    interpreted as for a `printf' call.  All the lines buf the first
251    start with a tag showing the PID.  */
252 extern void _dl_debug_printf_c (const char *fmt, ...)
253      __attribute__ ((__format__ (__printf__, 1, 2)));
254
255
256 /* Write a message on the specified descriptor FD.  The parameters are
257    interpreted as for a `printf' call.  */
258 extern void _dl_dprintf (int fd, const char *fmt, ...)
259      __attribute__ ((__format__ (__printf__, 2, 3)));
260
261 /* Write a message on the specified descriptor standard output.  The
262    parameters are interpreted as for a `printf' call.  */
263 #define _dl_printf(fmt, args...) \
264   _dl_dprintf (STDOUT_FILENO, fmt, ##args)
265
266 /* Write a message on the specified descriptor standard error.  The
267    parameters are interpreted as for a `printf' call.  */
268 #define _dl_error_printf(fmt, args...) \
269   _dl_dprintf (STDERR_FILENO, fmt, ##args)
270
271 /* Write a message on the specified descriptor standard error and exit
272    the program.  The parameters are interpreted as for a `printf' call.  */
273 #define _dl_fatal_printf(fmt, args...) \
274   do                                                                          \
275     {                                                                         \
276       _dl_dprintf (STDERR_FILENO, fmt, ##args);                               \
277       _exit (127);                                                            \
278     }                                                                         \
279   while (1)
280
281
282 /* This function is called by all the internal dynamic linker functions
283    when they encounter an error.  ERRCODE is either an `errno' code or
284    zero; OBJECT is the name of the problematical shared object, or null if
285    it is a general problem; ERRSTRING is a string describing the specific
286    problem.  */
287 extern void _dl_signal_error (int errcode, const char *object,
288                               const char *errstring)
289      internal_function
290      __attribute__ ((__noreturn__));
291
292 /* Like _dl_signal_error, but may return when called in the context of
293    _dl_receive_error.  */
294 extern void _dl_signal_cerror (int errcode,
295                                const char *object,
296                                const char *errstring)
297      internal_function;
298
299 /* Call OPERATE, receiving errors from `dl_signal_cerror'.  Unlike
300    `_dl_catch_error' the operation is resumed after the OPERATE
301    function returns.
302    ARGS is passed as argument to OPERATE.  */
303 extern void _dl_receive_error (receiver_fct fct, void (*operate) (void *),
304                                void *args)
305      internal_function;
306
307
308 /* Open the shared object NAME and map in its segments.
309    LOADER's DT_RPATH is used in searching for NAME.
310    If the object is already opened, returns its existing map.
311    For preloaded shared objects PRELOADED is set to a non-zero
312    value to allow additional security checks.  */
313 extern struct link_map *_dl_map_object (struct link_map *loader,
314                                         const char *name, int preloaded,
315                                         int type, int trace_mode, int mode)
316      internal_function;
317
318 /* Call _dl_map_object on the dependencies of MAP, and set up
319    MAP->l_searchlist.  PRELOADS points to a vector of NPRELOADS previously
320    loaded objects that will be inserted into MAP->l_searchlist after MAP
321    but before its dependencies.  */
322 extern void _dl_map_object_deps (struct link_map *map,
323                                  struct link_map **preloads,
324                                  unsigned int npreloads, int trace_mode)
325      internal_function;
326
327 /* Cache the locations of MAP's hash table.  */
328 extern void _dl_setup_hash (struct link_map *map) internal_function;
329
330
331 /* Search loaded objects' symbol tables for a definition of the symbol
332    referred to by UNDEF.  *SYM is the symbol table entry containing the
333    reference; it is replaced with the defining symbol, and the base load
334    address of the defining object is returned.  SYMBOL_SCOPE is a
335    null-terminated list of object scopes to search; each object's
336    l_searchlist (i.e. the segment of the dependency tree starting at that
337    object) is searched in turn.  REFERENCE_NAME should name the object
338    containing the reference; it is used in error messages.
339    RELOC_TYPE is a machine-dependent reloc type, which is passed to
340    the `elf_machine_lookup_*_p' macros in dl-machine.h to affect which
341    symbols can be chosen.  */
342 extern lookup_t _dl_lookup_symbol (const char *undef,
343                                    struct link_map *undef_map,
344                                    const ElfW(Sym) **sym,
345                                    struct r_scope_elem *symbol_scope[],
346                                    int reloc_type, int explicit)
347      internal_function;
348
349 /* Lookup versioned symbol.  */
350 extern lookup_t _dl_lookup_versioned_symbol (const char *undef,
351                                              struct link_map *undef_map,
352                                              const ElfW(Sym) **sym,
353                                              struct r_scope_elem *symbol_scope[],
354                                              const struct r_found_version *version,
355                                              int reloc_type, int explicit)
356      internal_function;
357
358 /* For handling RTLD_NEXT we must be able to skip shared objects.  */
359 extern lookup_t _dl_lookup_symbol_skip (const char *undef,
360                                         struct link_map *undef_map,
361                                         const ElfW(Sym) **sym,
362                                         struct r_scope_elem *symbol_scope[],
363                                         struct link_map *skip_this)
364      internal_function;
365
366 /* For handling RTLD_NEXT with versioned symbols we must be able to
367    skip shared objects.  */
368 extern lookup_t _dl_lookup_versioned_symbol_skip (const char *undef,
369                                                   struct link_map *undef_map,
370                                                   const ElfW(Sym) **sym,
371                                                   struct r_scope_elem *symbol_scope[],
372                                                   const struct r_found_version *version,
373                                                   struct link_map *skip_this)
374      internal_function;
375
376 /* Look up symbol NAME in MAP's scope and return its run-time address.  */
377 extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name)
378      internal_function;
379
380
381 /* Structure describing the dynamic linker itself.  */
382 extern struct link_map _dl_rtld_map;
383 /* And a pointer to the map for the main map.  */
384 extern struct link_map *_dl_loaded;
385 /* Number of object in the _dl_loaded list.  */
386 extern unsigned int _dl_nloaded;
387 /* Array representing global scope.  */
388 extern struct r_scope_elem *_dl_global_scope[2];
389 /* Direct pointer to the searchlist of the main object.  */
390 extern struct r_scope_elem *_dl_main_searchlist;
391 /* Copy of the content of `_dl_main_searchlist'.  */
392 extern struct r_scope_elem _dl_initial_searchlist;
393 /* This is zero at program start to signal that the global scope map is
394    allocated by rtld.  Later it keeps the size of the map.  It might be
395    reset if in _dl_close if the last global object is removed.  */
396 extern size_t _dl_global_scope_alloc;
397
398 /* Allocate a `struct link_map' for a new object being loaded,
399    and enter it into the _dl_main_map list.  */
400 extern struct link_map *_dl_new_object (char *realname, const char *libname,
401                                         int type, struct link_map *loader)
402      internal_function;
403
404 /* Relocate the given object (if it hasn't already been).
405    SCOPE is passed to _dl_lookup_symbol in symbol lookups.
406    If LAZY is nonzero, don't relocate its PLT.  */
407 extern void _dl_relocate_object (struct link_map *map,
408                                  struct r_scope_elem *scope[],
409                                  int lazy, int consider_profiling);
410
411 /* Call _dl_signal_error with a message about an unhandled reloc type.
412    TYPE is the result of ELFW(R_TYPE) (r_info), i.e. an R_<CPU>_* value.
413    PLT is nonzero if this was a PLT reloc; it just affects the message.  */
414 extern void _dl_reloc_bad_type (struct link_map *map,
415                                 uint_fast8_t type, int plt)
416      internal_function
417      __attribute__ ((__noreturn__));
418
419 /* Check the version dependencies of all objects available through
420    MAP.  If VERBOSE print some more diagnostics.  */
421 extern int _dl_check_all_versions (struct link_map *map, int verbose,
422                                    int trace_mode)
423      internal_function;
424
425 /* Check the version dependencies for MAP.  If VERBOSE print some more
426    diagnostics.  */
427 extern int _dl_check_map_versions (struct link_map *map, int verbose,
428                                    int trace_mode)
429      internal_function;
430
431 /* Initialize the object in SCOPE by calling the constructors with
432    ARGC, ARGV, and ENV as the parameters.  */
433 extern void _dl_init (struct link_map *main_map, int argc, char **argv,
434                       char **env) internal_function;
435
436 /* Call the finalizer functions of all shared objects whose
437    initializer functions have completed.  */
438 extern void _dl_fini (void) internal_function;
439
440 /* The dynamic linker calls this function before and having changing
441    any shared object mappings.  The `r_state' member of `struct r_debug'
442    says what change is taking place.  This function's address is
443    the value of the `r_brk' member.  */
444 extern void _dl_debug_state (void);
445
446 /* Initialize `struct r_debug' if it has not already been done.  The
447    argument is the run-time load address of the dynamic linker, to be put
448    in the `r_ldbase' member.  Returns the address of the structure.  */
449 extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase)
450      internal_function;
451
452 /* Initialize the basic data structure for the search paths.  */
453 extern void _dl_init_paths (const char *library_path) internal_function;
454
455 /* Gather the information needed to install the profiling tables and start
456    the timers.  */
457 extern void _dl_start_profile (struct link_map *map, const char *output_dir)
458      internal_function;
459
460 /* The actual functions used to keep book on the calls.  */
461 extern void _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc);
462
463 /* This function is simply a wrapper around the _dl_mcount function
464    which does not require a FROMPC parameter since this is the
465    calling function.  */
466 extern void _dl_mcount_wrapper (void *selfpc);
467
468 /* Show the members of the auxiliary array passed up from the kernel.  */
469 extern void _dl_show_auxv (void) internal_function;
470
471 /* Return all environment variables starting with `LD_', one after the
472    other.  */
473 extern char *_dl_next_ld_env_entry (char ***position) internal_function;
474
475 /* Return an array with the names of the important hardware capabilities.  */
476 extern const struct r_strlenpair *_dl_important_hwcaps (const char *platform,
477                                                         size_t paltform_len,
478                                                         size_t *sz,
479                                                         size_t *max_capstrlen)
480      internal_function;
481
482 /* Look up NAME in ld.so.cache and return the file name stored there,
483    or null if none is found.  */
484 extern const char *_dl_load_cache_lookup (const char *name)
485      internal_function;
486
487 /* If the system does not support MAP_COPY we cannot leave the file open
488    all the time since this would create problems when the file is replaced.
489    Therefore we provide this function to close the file and open it again
490    once needed.  */
491 extern void _dl_unload_cache (void);
492
493 /* System-dependent function to read a file's whole contents
494    in the most convenient manner available.  */
495 extern void *_dl_sysdep_read_whole_file (const char *file, size_t *sizep,
496                                          int prot)
497      internal_function;
498
499 /* System-specific function to do initial startup for the dynamic linker.
500    After this, file access calls and getenv must work.  This is responsible
501    for setting __libc_enable_secure if we need to be secure (e.g. setuid),
502    and for setting _dl_argc and _dl_argv, and then calling _dl_main.  */
503 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
504                                     void (*dl_main) (const ElfW(Phdr) *phdr,
505                                                      ElfW(Word) phnum,
506                                                      ElfW(Addr) *user_entry));
507
508 extern void _dl_sysdep_start_cleanup (void)
509      internal_function;
510
511
512 __END_DECLS
513
514 #endif /* ldsodefs.h */