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