Update.
[platform/upstream/glibc.git] / elf / link.h
1 /* Run-time dynamic linker data structures for loaded ELF shared objects.
2    Copyright (C) 1995, 1996, 1997, 1998 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 _LINK_H
21 #define _LINK_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
32 __BEGIN_DECLS
33
34 /* We use this macro to refer to ELF types independent of the native wordsize.
35    `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'.  */
36 #define ElfW(type)      _ElfW (Elf, __ELF_NATIVE_CLASS, type)
37 #define ELFW(type)      _ElfW (ELF, __ELF_NATIVE_CLASS, type)
38 #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
39 #define _ElfW_1(e,w,t)  e##w##t
40
41 #include <bits/elfclass.h>              /* Defines __ELF_NATIVE_CLASS.  */
42
43 /* Rendezvous structure used by the run-time dynamic linker to communicate
44    details of shared object loading to the debugger.  If the executable's
45    dynamic section has a DT_DEBUG element, the run-time linker sets that
46    element's value to the address where this structure can be found.  */
47
48 struct r_debug
49   {
50     int r_version;              /* Version number for this protocol.  */
51
52     struct link_map *r_map;     /* Head of the chain of loaded objects.  */
53
54     /* This is the address of a function internal to the run-time linker,
55        that will always be called when the linker begins to map in a
56        library or unmap it, and again when the mapping change is complete.
57        The debugger can set a breakpoint at this address if it wants to
58        notice shared object mapping changes.  */
59     ElfW(Addr) r_brk;
60     enum
61       {
62         /* This state value describes the mapping change taking place when
63            the `r_brk' address is called.  */
64         RT_CONSISTENT,          /* Mapping change is complete.  */
65         RT_ADD,                 /* Beginning to add a new object.  */
66         RT_DELETE               /* Beginning to remove an object mapping.  */
67       } r_state;
68
69     ElfW(Addr) r_ldbase;        /* Base address the linker is loaded at.  */
70   };
71
72 /* This is the instance of that structure used by the dynamic linker.  */
73 extern struct r_debug _r_debug;
74
75 /* This symbol refers to the "dynamic structure" in the `.dynamic' section
76    of whatever module refers to `_DYNAMIC'.  So, to find its own
77    `struct r_debug', a program could do:
78      for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL)
79        if (dyn->d_tag == DT_DEBUG) r_debug = (struct r_debug) dyn->d_un.d_ptr;
80    */
81
82 extern ElfW(Dyn) _DYNAMIC[];
83
84 /* For the version handling we need an array with only names and their
85    hash values.  */
86 struct r_found_version
87   {
88     const char *name;
89     ElfW(Word) hash;
90
91     int hidden;
92     const char *filename;
93   };
94
95 /* We want to cache information about the searches for shared objects.  */
96
97 enum r_dir_status { unknown, nonexisting, existing };
98
99 struct r_search_path_elem
100   {
101     const char *dirname;
102
103     size_t dirnamelen;
104     enum r_dir_status dirstatus;
105
106     size_t machdirnamelen;
107     enum r_dir_status machdirstatus;
108
109     /* Strings saying where the definition came from.  */
110     const char *what;
111     const char *where;
112
113     /* This link is only used in the `all_dirs' member of `r_search_path'.  */
114     struct r_search_path_elem *next;
115   };
116
117
118 /* A data structure for a simple single linked list of strings.  */
119 struct libname_list
120   {
121     const char *name;           /* Name requested (before search).  */
122     struct libname_list *next;  /* Link to next name for this object.  */
123   };
124
125
126 /* Structure describing a loaded shared object.  The `l_next' and `l_prev'
127    members form a chain of all the shared objects loaded at startup.
128
129    These data structures exist in space used by the run-time dynamic linker;
130    modifying them may have disastrous results.  */
131
132 struct link_map
133   {
134     /* These first few members are part of the protocol with the debugger.
135        This is the same format used in SVR4.  */
136
137     ElfW(Addr) l_addr;          /* Base address shared object is loaded at.  */
138     char *l_name;               /* Absolute file name object was found in.  */
139     ElfW(Dyn) *l_ld;            /* Dynamic section of the shared object.  */
140     struct link_map *l_next, *l_prev; /* Chain of loaded objects.  */
141
142     /* All following members are internal to the dynamic linker.
143        They may change without notice.  */
144
145     struct libname_list  *l_libname;
146     /* Indexed pointers to dynamic section.
147        [0,DT_NUM) are indexed by the processor-independent tags.
148        [DT_NUM,DT_NUM+DT_PROCNUM) are indexed by the tag minus DT_LOPROC.
149        [DT_NUM+DT_PROCNUM,DT_NUM+DT_PROCNUM+DT_EXTRANUM) are indexed
150        by DT_EXTRATAGIDX(tagvalue) and
151        [DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM,
152         DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM)
153        are indexed by DT_EXTRATAGIDX(tagvalue) (see <elf.h>).  */
154
155     ElfW(Dyn) *l_info[DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM];
156     const ElfW(Phdr) *l_phdr;   /* Pointer to program header table in core.  */
157     ElfW(Addr) l_entry;         /* Entry point location.  */
158     ElfW(Half) l_phnum;         /* Number of program header entries.  */
159
160     /* Array of DT_NEEDED dependencies and their dependencies, in
161        dependency order for symbol lookup.  This is null before the
162        dependencies have been loaded.  */
163     struct link_map **l_searchlist;
164     unsigned int l_nsearchlist;
165
166     /* We keep another list in which we keep duplicates.  This is
167        needed in _dl_lookup_symbol_skip to implemented RTLD_NEXT.  */
168     struct link_map **l_dupsearchlist;
169     unsigned int l_ndupsearchlist;
170
171     /* Dependent object that first caused this object to be loaded.  */
172     struct link_map *l_loader;
173
174     /* Symbol hash table.  */
175     ElfW(Symndx) l_nbuckets;
176     const ElfW(Symndx) *l_buckets, *l_chain;
177
178     unsigned int l_opencount;   /* Reference count for dlopen/dlclose.  */
179     enum                        /* Where this object came from.  */
180       {
181         lt_executable,          /* The main executable program.  */
182         lt_library,             /* Library needed by main executable.  */
183         lt_loaded               /* Extra run-time loaded shared object.  */
184       } l_type:2;
185     unsigned int l_relocated:1; /* Nonzero if object's relocations done.  */
186     unsigned int l_init_called:1; /* Nonzero if DT_INIT function called.  */
187     unsigned int l_init_running:1; /* Nonzero while DT_INIT function runs.  */
188     unsigned int l_global:1;    /* Nonzero if object in _dl_global_scope.  */
189     unsigned int l_reserved:2;  /* Reserved for internal use.  */
190
191     /* Array with version names.  */
192     unsigned int l_nversions;
193     struct r_found_version *l_versions;
194
195     /* Collected information about own RPATH directories.  */
196     struct r_search_path_elem **l_rpath_dirs;
197
198     /* Collected results of relocation while profiling.  */
199     ElfW(Addr) *l_reloc_result;
200
201     /* Pointer to the version information if available.  */
202     ElfW(Half) *l_versyms;
203   };
204
205
206 /* Test whether given NAME matches any of the names of the given object.  */
207 static __inline int
208 __attribute__ ((unused))
209 _dl_name_match_p (const char *__name, struct link_map *__map)
210 {
211   int __found = strcmp (__name, __map->l_name) == 0;
212   struct libname_list *__runp = __map->l_libname;
213
214   while (! __found && __runp != NULL)
215     if (strcmp (__name, __runp->name) == 0)
216       __found = 1;
217     else
218       __runp = __runp->next;
219
220   return __found;
221 }
222
223 /* Function used as argument for `_dl_receive_error' function.  The
224    arguments are the error code, error string, and the objname the
225    error occurred in.  */
226 typedef void (*receiver_fct) (int, const char *, const char *);
227 \f
228 /* Internal functions of the run-time dynamic linker.
229    These can be accessed if you link again the dynamic linker
230    as a shared library, as in `-lld' or `/lib/ld.so' explicitly;
231    but are not normally of interest to user programs.
232
233    The `-ldl' library functions in <dlfcn.h> provide a simple
234    user interface to run-time dynamic linking.  */
235
236
237 /* Parameters passed to the dynamic linker.  */
238 extern char **_dl_argv;
239
240 /* Cached value of `getpagesize ()'.  */
241 extern size_t _dl_pagesize;
242
243 /* File descriptor referring to the zero-fill device.  */
244 extern int _dl_zerofd;
245
246 /* Name of the shared object to be profiled (if any).  */
247 extern const char *_dl_profile;
248 /* Map of shared object to be profiled.  */
249 extern struct link_map *_dl_profile_map;
250
251 /* If nonzero the appropriate debug information is printed.  */
252 extern int _dl_debug_libs;
253 extern int _dl_debug_impcalls;
254 extern int _dl_debug_bindings;
255 extern int _dl_debug_symbols;
256
257 /* File deccriptor to write debug messages to.  */
258 extern int _dl_debug_fd;
259
260 /* OS-dependent function to open the zero-fill device.  */
261 extern int _dl_sysdep_open_zero_fill (void); /* dl-sysdep.c */
262
263 /* OS-dependent function to write a message on the specified
264    descriptor FD.  All arguments are `const char *'; args until a null
265    pointer are concatenated to form the message to print.  */
266 extern void _dl_sysdep_output (int fd, const char *string, ...);
267
268 /* OS-dependent function to write a debug message on the specified
269    descriptor for this.  All arguments are `const char *'; args until
270    a null pointer are concatenated to form the message to print.  */
271 #define _dl_debug_message(string, args...) \
272   _dl_sysdep_output (_dl_debug_fd, string, ##args)
273
274 /* OS-dependent function to write a message on the standard output.
275    All arguments are `const char *'; args until a null pointer
276    are concatenated to form the message to print.  */
277 #define _dl_sysdep_message(string, args...) \
278   _dl_sysdep_output (STDOUT_FILENO, string, ##args)
279
280 /* OS-dependent function to write a message on the standard error.
281    All arguments are `const char *'; args until a null pointer
282    are concatenated to form the message to print.  */
283 #define _dl_sysdep_error(string, args...) \
284   _dl_sysdep_output (STDERR_FILENO, string, ##args)
285
286 /* OS-dependent function to give a fatal error message and exit
287    when the dynamic linker fails before the program is fully linked.
288    All arguments are `const char *'; args until a null pointer
289    are concatenated to form the message to print.  */
290 #define _dl_sysdep_fatal(string, args...) \
291   do                                                                          \
292     {                                                                         \
293       _dl_sysdep_output (STDERR_FILENO, string, ##args);                      \
294       _exit (127);                                                            \
295     }                                                                         \
296   while (1)
297
298 /* Nonzero if the program should be "secure" (i.e. it's setuid or somesuch).
299    This tells the dynamic linker to ignore environment variables.  */
300 extern int _dl_secure;
301
302 /* This function is called by all the internal dynamic linker functions
303    when they encounter an error.  ERRCODE is either an `errno' code or
304    zero; OBJECT is the name of the problematical shared object, or null if
305    it is a general problem; ERRSTRING is a string describing the specific
306    problem.  */
307 extern void _dl_signal_error (int errcode,
308                               const char *object,
309                               const char *errstring);
310
311 /* Call OPERATE, catching errors from `dl_signal_error'.  If there is no
312    error, *ERRSTRING is set to null.  If there is an error, *ERRSTRING and
313    *OBJECT are set to the strings passed to _dl_signal_error, and the error
314    code passed is the return value.  ERRSTRING if nonzero points to a
315    malloc'ed string which the caller has to free after use.
316    ARGS is passed as argument to OPERATE.  */
317 extern int _dl_catch_error (char **errstring,
318                             const char **object,
319                             void (*operate) (void *),
320                             void *args);
321
322 /* Call OPERATE, receiving errors from `dl_signal_error'.  Unlike
323    `_dl_catch_error' the operation is resumed after the OPERATE
324    function returns.
325    ARGS is passed as argument to OPERATE.  */
326 extern void _dl_receive_error (receiver_fct fct, void (*operate) (void *),
327                                void *args);
328
329
330 /* Helper function for <dlfcn.h> functions.  Runs the OPERATE function via
331    _dl_catch_error.  Returns zero for success, nonzero for failure; and
332    arranges for `dlerror' to return the error details.
333    ARGS is passed as argument to OPERATE.  */
334 extern int _dlerror_run (void (*operate) (void *), void *args);
335
336
337 /* Open the shared object NAME and map in its segments.
338    LOADER's DT_RPATH is used in searching for NAME.
339    If the object is already opened, returns its existing map.
340    For preloaded shared objects PRELOADED is set to a non-zero
341    value to allow additional security checks.  */
342 extern struct link_map *_dl_map_object (struct link_map *loader,
343                                         const char *name, int preloaded,
344                                         int type, int trace_mode);
345
346 /* Call _dl_map_object on the dependencies of MAP, and set up
347    MAP->l_searchlist.  PRELOADS points to a vector of NPRELOADS previously
348    loaded objects that will be inserted into MAP->l_searchlist after MAP
349    but before its dependencies.  */
350 extern void _dl_map_object_deps (struct link_map *map,
351                                  struct link_map **preloads,
352                                  unsigned int npreloads, int trace_mode);
353
354 /* Cache the locations of MAP's hash table.  */
355 extern void _dl_setup_hash (struct link_map *map);
356
357
358 /* Open the shared object NAME, relocate it, and run its initializer if it
359    hasn't already been run.  MODE is as for `dlopen' (see <dlfcn.h>).  If
360    the object is already opened, returns its existing map.  */
361 extern struct link_map *_dl_open (const char *name, int mode);
362
363 /* Close an object previously opened by _dl_open.  */
364 extern void _dl_close (struct link_map *map);
365
366
367 /* Search loaded objects' symbol tables for a definition of the symbol
368    referred to by UNDEF.  *SYM is the symbol table entry containing the
369    reference; it is replaced with the defining symbol, and the base load
370    address of the defining object is returned.  SYMBOL_SCOPE is a
371    null-terminated list of object scopes to search; each object's
372    l_searchlist (i.e. the segment of the dependency tree starting at that
373    object) is searched in turn.  REFERENCE_NAME should name the object
374    containing the reference; it is used in error messages.
375    RELOC_TYPE is a machine-dependent reloc type, which is passed to
376    the `elf_machine_lookup_*_p' macros in dl-machine.h to affect which
377    symbols can be chosen.  */
378 extern ElfW(Addr) _dl_lookup_symbol (const char *undef,
379                                      const ElfW(Sym) **sym,
380                                      struct link_map *symbol_scope[],
381                                      const char *reference_name,
382                                      int reloc_type);
383
384 /* Lookup versioned symbol.  */
385 extern ElfW(Addr) _dl_lookup_versioned_symbol (const char *undef,
386                                                const ElfW(Sym) **sym,
387                                                struct link_map *symbol_scope[],
388                                                const char *reference_name,
389                                                const struct r_found_version *version,
390                                                int reloc_type);
391
392 /* For handling RTLD_NEXT we must be able to skip shared objects.  */
393 extern ElfW(Addr) _dl_lookup_symbol_skip (const char *undef,
394                                           const ElfW(Sym) **sym,
395                                           struct link_map *symbol_scope[],
396                                           const char *reference_name,
397                                           struct link_map *skip_this);
398
399 /* For handling RTLD_NEXT with versioned symbols we must be able to
400    skip shared objects.  */
401 extern ElfW(Addr) _dl_lookup_versioned_symbol_skip (const char *undef,
402                                                     const ElfW(Sym) **sym,
403                                                     struct link_map *symbol_scope[],
404                                                     const char *reference_name,
405                                                     const struct r_found_version *version,
406                                                     struct link_map *skip_this);
407
408 /* Locate shared object containing the given address.  */
409 extern int _dl_addr (const void *address, Dl_info *info);
410
411 /* Look up symbol NAME in MAP's scope and return its run-time address.  */
412 extern ElfW(Addr) _dl_symbol_value (struct link_map *map, const char *name);
413
414
415 /* Structure describing the dynamic linker itself.  */
416 extern struct link_map _dl_rtld_map;
417
418 /* The list of objects currently loaded is the third element of the
419    `_dl_default_scope' array, and the fourth element is always null.
420    This leaves two slots before it that are used when resolving
421    DT_SYMBOLIC objects' references one after it for normal references
422    (see below).  */
423 #define _dl_loaded      (_dl_default_scope[2])
424 extern struct link_map *_dl_default_scope[5];
425
426 /* Null-terminated list of objects in the dynamic `global scope'.  The
427    list starts at [2]; i.e. &_dl_global_scope[2] is the argument
428    passed to _dl_lookup_symbol to search the global scope.  To search
429    a specific object and its dependencies in preference to the global
430    scope, fill in the [1] slot and pass its address; for two specific
431    object scopes, fill [0] and [1].  The list is double-terminated; to
432    search the global scope and then a specific object and its
433    dependencies, set *_dl_global_scope_end.  This variable initially
434    points to _dl_default_scope, and _dl_loaded is always kept in [2]
435    of this list.  A new list is malloc'd when new objects are loaded
436    with RTLD_GLOBAL.  */
437 extern struct link_map **_dl_global_scope, **_dl_global_scope_end;
438 extern size_t _dl_global_scope_alloc; /* Number of slots malloc'd.  */
439
440 /* Hack _dl_global_scope[0] and [1] as necessary, and return a pointer into
441    _dl_global_scope that should be passed to _dl_lookup_symbol for symbol
442    references made in the object MAP's relocations.  */
443 extern struct link_map **_dl_object_relocation_scope (struct link_map *map);
444
445
446 /* Allocate a `struct link_map' for a new object being loaded,
447    and enter it into the _dl_loaded list.  */
448 extern struct link_map *_dl_new_object (char *realname, const char *libname,
449                                         int type);
450
451 /* Relocate the given object (if it hasn't already been).
452    SCOPE is passed to _dl_lookup_symbol in symbol lookups.
453    If LAZY is nonzero, don't relocate its PLT.  */
454 extern void _dl_relocate_object (struct link_map *map,
455                                  struct link_map *scope[],
456                                  int lazy);
457
458 /* Check the version dependencies of all objects available through
459    MAP.  If VERBOSE print some more diagnostics.  */
460 extern int _dl_check_all_versions (struct link_map *map, int verbose);
461
462 /* Check the version dependencies for MAP.  If VERBOSE print some more
463    diagnostics.  */
464 extern int _dl_check_map_versions (struct link_map *map, int verbose);
465
466 /* Return the address of the next initializer function for MAP or one of
467    its dependencies that has not yet been run.  When there are no more
468    initializers to be run, this returns zero.  The functions are returned
469    in the order they should be called.  */
470 extern ElfW(Addr) _dl_init_next (struct link_map *map);
471
472 /* Call the finalizer functions of all shared objects whose
473    initializer functions have completed.  */
474 extern void _dl_fini (void);
475
476 /* The dynamic linker calls this function before and having changing
477    any shared object mappings.  The `r_state' member of `struct r_debug'
478    says what change is taking place.  This function's address is
479    the value of the `r_brk' member.  */
480 extern void _dl_debug_state (void);
481
482 /* Initialize `struct r_debug' if it has not already been done.  The
483    argument is the run-time load address of the dynamic linker, to be put
484    in the `r_ldbase' member.  Returns the address of the structure.  */
485 extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase);
486
487 /* Initialize the basic data structure for the search paths.  */
488 extern void _dl_init_paths (const char *library_path);
489
490 /* Gather the information needed to install the profiling tables and start
491    the timers.  */
492 extern void _dl_start_profile (struct link_map *map, const char *output_dir);
493
494 /* The actual functions used to keep book on the calls.  */
495 extern void _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc);
496
497
498 /* Show the members of the auxiliary array passed up from the kernel.  */
499 extern void _dl_show_auxv (void);
500
501 /* Return all environment variables starting with `LD_', one after the
502    other.  */
503 extern char *_dl_next_ld_env_entry (char ***position);
504
505 __END_DECLS
506
507 #endif /* link.h */