Update.
[jlayton/glibc.git] / elf / link.h
1 /* Data structure for communication from the run-time dynamic linker for
2    loaded ELF shared objects.
3    Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #ifndef _LINK_H
22 #define _LINK_H 1
23
24 #include <features.h>
25 #include <elf.h>
26 #include <dlfcn.h>
27 #include <sys/types.h>
28
29 /* We use this macro to refer to ELF types independent of the native wordsize.
30    `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'.  */
31 #define ElfW(type)      _ElfW (Elf, __ELF_NATIVE_CLASS, type)
32 #define _ElfW(e,w,t)    _ElfW_1 (e, w, _##t)
33 #define _ElfW_1(e,w,t)  e##w##t
34
35 #include <bits/elfclass.h>              /* Defines __ELF_NATIVE_CLASS.  */
36
37 /* Rendezvous structure used by the run-time dynamic linker to communicate
38    details of shared object loading to the debugger.  If the executable's
39    dynamic section has a DT_DEBUG element, the run-time linker sets that
40    element's value to the address where this structure can be found.  */
41
42 struct r_debug
43   {
44     int r_version;              /* Version number for this protocol.  */
45
46     struct link_map *r_map;     /* Head of the chain of loaded objects.  */
47
48     /* This is the address of a function internal to the run-time linker,
49        that will always be called when the linker begins to map in a
50        library or unmap it, and again when the mapping change is complete.
51        The debugger can set a breakpoint at this address if it wants to
52        notice shared object mapping changes.  */
53     ElfW(Addr) r_brk;
54     enum
55       {
56         /* This state value describes the mapping change taking place when
57            the `r_brk' address is called.  */
58         RT_CONSISTENT,          /* Mapping change is complete.  */
59         RT_ADD,                 /* Beginning to add a new object.  */
60         RT_DELETE               /* Beginning to remove an object mapping.  */
61       } r_state;
62
63     ElfW(Addr) r_ldbase;        /* Base address the linker is loaded at.  */
64   };
65
66 /* This is the instance of that structure used by the dynamic linker.  */
67 extern struct r_debug _r_debug;
68
69 /* This symbol refers to the "dynamic structure" in the `.dynamic' section
70    of whatever module refers to `_DYNAMIC'.  So, to find its own
71    `struct r_debug', a program could do:
72      for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL)
73        if (dyn->d_tag == DT_DEBUG) r_debug = (struct r_debug) dyn->d_un.d_ptr;
74    */
75 extern ElfW(Dyn) _DYNAMIC[];
76
77
78 /* Some internal data structures of the dynamic linker used in the
79    linker map.  We only provide forward declarations.  */
80 struct libname_list;
81 struct r_found_version;
82 struct r_search_path_elem;
83
84 /* Forward declaration.  */
85 struct link_map;
86
87 /* Structure to describe a single list of scope elements.  The lookup
88    functions get passed an array of pointers to such structures.  */
89 struct r_scope_elem
90 {
91   /* Array of maps for the scope.  */
92   struct link_map **r_list;
93   /* Number of entries in the scope.  */
94   unsigned int r_nlist;
95
96   /* Array of maps which also includes duplicates.  */
97   struct link_map **r_duplist;
98   /* Number of elements in this list.  */
99   unsigned int r_nduplist;
100 };
101
102
103 /* Structure describing a loaded shared object.  The `l_next' and `l_prev'
104    members form a chain of all the shared objects loaded at startup.
105
106    These data structures exist in space used by the run-time dynamic linker;
107    modifying them may have disastrous results.
108
109    This data structure might change in future, if necessary.  User-level
110    programs must avoid defining objects of this type.  */
111
112 struct link_map
113   {
114     /* These first few members are part of the protocol with the debugger.
115        This is the same format used in SVR4.  */
116
117     ElfW(Addr) l_addr;          /* Base address shared object is loaded at.  */
118     char *l_name;               /* Absolute file name object was found in.  */
119     ElfW(Dyn) *l_ld;            /* Dynamic section of the shared object.  */
120     struct link_map *l_next, *l_prev; /* Chain of loaded objects.  */
121
122     /* All following members are internal to the dynamic linker.
123        They may change without notice.  */
124
125     struct libname_list *l_libname;
126     /* Indexed pointers to dynamic section.
127        [0,DT_NUM) are indexed by the processor-independent tags.
128        [DT_NUM,DT_NUM+DT_PROCNUM) are indexed by the tag minus DT_LOPROC.
129        [DT_NUM+DT_PROCNUM,DT_NUM+DT_PROCNUM+DT_EXTRANUM) are indexed
130        by DT_EXTRATAGIDX(tagvalue) and
131        [DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM,
132         DT_NUM+DT_PROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM)
133        are indexed by DT_EXTRATAGIDX(tagvalue) (see <elf.h>).  */
134
135     ElfW(Dyn) *l_info[DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM];
136     const ElfW(Phdr) *l_phdr;   /* Pointer to program header table in core.  */
137     ElfW(Addr) l_entry;         /* Entry point location.  */
138     ElfW(Half) l_phnum;         /* Number of program header entries.  */
139
140     /* Array of DT_NEEDED dependencies and their dependencies, in
141        dependency order for symbol lookup (with and without
142        duplicates).  There is no entry before the dependencies have
143        been loaded.  */
144     struct r_scope_elem l_searchlist;
145
146     /* We need a special searchlist to process objects marked with
147        DT_SYMBOLIC.  */
148     struct r_scope_elem l_symbolic_searchlist;
149
150     /* Dependent object that first caused this object to be loaded.  */
151     struct link_map *l_loader;
152
153     /* Symbol hash table.  */
154     Elf_Symndx l_nbuckets;
155     const Elf_Symndx *l_buckets, *l_chain;
156
157     unsigned int l_opencount;   /* Reference count for dlopen/dlclose.  */
158     enum                        /* Where this object came from.  */
159       {
160         lt_executable,          /* The main executable program.  */
161         lt_library,             /* Library needed by main executable.  */
162         lt_loaded               /* Extra run-time loaded shared object.  */
163       } l_type:2;
164     unsigned int l_relocated:1; /* Nonzero if object's relocations done.  */
165     unsigned int l_init_called:1; /* Nonzero if DT_INIT function called.  */
166     unsigned int l_global:1;    /* Nonzero if object in _dl_global_scope.  */
167     unsigned int l_reserved:2;  /* Reserved for internal use.  */
168
169     /* Array with version names.  */
170     unsigned int l_nversions;
171     struct r_found_version *l_versions;
172
173     /* Collected information about own RPATH directories.  */
174     struct r_search_path_elem **l_rpath_dirs;
175
176     /* Collected results of relocation while profiling.  */
177     ElfW(Addr) *l_reloc_result;
178
179     /* Pointer to the version information if available.  */
180     ElfW(Versym) *l_versyms;
181
182     /* String specifying the path where this object was found.  */
183     const char *l_origin;
184
185     /* Start and finish of memory map for this object.  l_map_start
186        need not be the same as l_addr.  */
187     ElfW(Addr) l_map_start, l_map_end;
188
189     /* This is an array defining the lookup scope for this link map.
190      There are at most three different scope lists.  */
191     struct r_scope_elem *l_scope[4];
192
193     /* A similar array, this time only with the local scope.  This is
194        used occasionally.  */
195     struct r_scope_elem *l_local_scope[2];
196
197     /* This information is kept to check for sure whether a shared
198        object is the same as one already loaded.  */
199     dev_t l_dev;
200     ino_t l_ino;
201
202     /* Nonzero if the data structure pointed to by `l_phdr' is allocated.  */
203     int l_phdr_allocated;
204
205     /* Counter for running constructors and destructors.  */
206     unsigned int l_runcount;
207
208     /* Number of constructors.  We compute this during loading to avoid
209        duplication of this during the possibly many calls to _dl_init_next.  */
210     unsigned int l_initcount;
211
212     /* Collected information about own RUNPATH directories.  */
213     struct r_search_path_elem **l_runpath_dirs;
214
215     /* Number of pre-constructors.  We compute this during loading to avoid
216        duplication of this during the possibly many calls to _dl_init_next.  */
217     unsigned int l_preinitcount;
218   };
219
220 #endif /* link.h */