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