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