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