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