1 /* Data structure for communication from the run-time dynamic linker for
2    loaded ELF shared objects.
3    Copyright (C) 1995-2001, 2004, 2005, 2006, 2010 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 Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the 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    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef	_LINK_H
21 #define	_LINK_H	1
22 
23 #include <features.h>
24 #include <elf.h>
25 #ifdef __HAVE_SHARED__
26 #include <dlfcn.h>
27 #endif
28 #include <sys/types.h>
29 #if defined _LIBC && defined __UCLIBC_HAS_TLS__
30 #include <tls.h>
31 #endif
32 
33 /* We use this macro to refer to ELF types independent of the native wordsize.
34    `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'.  */
35 #define ElfW(type)	_ElfW (Elf, __ELF_NATIVE_CLASS, type)
36 #define _ElfW(e,w,t)	_ElfW_1 (e, w, _##t)
37 #define _ElfW_1(e,w,t)	e##w##t
38 
39 #define ELFW(type)	_ElfW (ELF, __ELF_NATIVE_CLASS, type)
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; ++dyn)
79        if (dyn->d_tag == DT_DEBUG)
80 	 r_debug = (struct r_debug *) dyn->d_un.d_ptr;
81    */
82 extern ElfW(Dyn) _DYNAMIC[];
83 
84 #ifdef __FDPIC__
85 # include <bits/elf-fdpic.h>
86 #endif
87 #ifdef __DSBT__
88 # include <bits/elf-dsbt.h>
89 #endif
90 
91 /* Structure describing a loaded shared object.  The `l_next' and `l_prev'
92    members form a chain of all the shared objects loaded at startup.
93 
94    These data structures exist in space used by the run-time dynamic linker;
95    modifying them may have disastrous results.  */
96 
97 struct link_map
98   {
99     /* These first few members are part of the protocol with the debugger.
100        This is the same format used in SVR4.  */
101 
102 #ifdef __FDPIC__
103     struct elf32_fdpic_loadaddr l_addr;
104 #else
105 #ifdef __DSBT__
106     struct elf32_dsbt_loadaddr l_addr;
107 #else
108     ElfW(Addr) l_addr;		/* Base address shared object is loaded at.  */
109 #endif
110 #endif
111     char *l_name;		/* Absolute file name object was found in.  */
112     ElfW(Dyn) *l_ld;		/* Dynamic section of the shared object.  */
113     struct link_map *l_next, *l_prev; /* Chain of loaded objects.  */
114 #ifdef _LIBC
115 #if defined(USE_TLS) && USE_TLS
116     /* Thread-local storage related info.  */
117 
118     /* Start of the initialization image.  */
119     void *l_tls_initimage;
120     /* Size of the initialization image.  */
121     size_t l_tls_initimage_size;
122     /* Size of the TLS block.  */
123     size_t l_tls_blocksize;
124     /* Alignment requirement of the TLS block.  */
125     size_t l_tls_align;
126     /* Offset of first byte module alignment.  */
127     size_t l_tls_firstbyte_offset;
128 # ifndef NO_TLS_OFFSET
129 #  define NO_TLS_OFFSET	0
130 # endif
131     /* For objects present at startup time: offset in the static TLS block.  */
132     ptrdiff_t l_tls_offset;
133     /* Index of the module in the dtv array.  */
134     size_t l_tls_modid;
135     /* Nonzero if _dl_init_static_tls should be called for this module */
136     unsigned int l_need_tls_init:1;
137 #endif
138 #endif
139   };
140 
141 #ifdef __USE_GNU
142 
143 #if 0
144 /* Version numbers for la_version handshake interface.  */
145 #define LAV_CURRENT	1
146 
147 /* Activity types signaled through la_activity.  */
148 enum
149   {
150     LA_ACT_CONSISTENT,		/* Link map consistent again.  */
151     LA_ACT_ADD,			/* New object will be added.  */
152     LA_ACT_DELETE		/* Objects will be removed.  */
153   };
154 
155 /* Values representing origin of name for dynamic loading.  */
156 enum
157   {
158     LA_SER_ORIG = 0x01,		/* Original name.  */
159     LA_SER_LIBPATH = 0x02,	/* Directory from LD_LIBRARY_PATH.  */
160     LA_SER_RUNPATH = 0x04,	/* Directory from RPATH/RUNPATH.  */
161     LA_SER_CONFIG = 0x08,	/* Found through ldconfig.  */
162     LA_SER_DEFAULT = 0x40,	/* Default directory.  */
163     LA_SER_SECURE = 0x80	/* Unused.  */
164   };
165 
166 /* Values for la_objopen return value.  */
167 enum
168   {
169     LA_FLG_BINDTO = 0x01,	/* Audit symbols bound to this object.  */
170     LA_FLG_BINDFROM = 0x02	/* Audit symbols bound from this object.  */
171   };
172 
173 /* Values for la_symbind flags parameter.  */
174 enum
175   {
176     LA_SYMB_NOPLTENTER = 0x01,	/* la_pltenter will not be called.  */
177     LA_SYMB_NOPLTEXIT = 0x02,	/* la_pltexit will not be called.  */
178     LA_SYMB_STRUCTCALL = 0x04,	/* Return value is a structure.  */
179     LA_SYMB_DLSYM = 0x08,	/* Binding due to dlsym call.  */
180     LA_SYMB_ALTVALUE = 0x10	/* Value has been changed by a previous
181 				   la_symbind call.  */
182   };
183 #endif
184 
185 struct dl_phdr_info
186   {
187 #ifdef __FDPIC__
188     struct elf32_fdpic_loadaddr dlpi_addr;
189 #else
190 #ifdef __DSBT__
191     struct elf32_dsbt_loadaddr dlpi_addr;
192 #else
193     ElfW(Addr) dlpi_addr;
194 #endif
195 #endif
196     const char *dlpi_name;
197     const ElfW(Phdr) *dlpi_phdr;
198     ElfW(Half) dlpi_phnum;
199 
200 #if 0
201     /* Note: Following members were introduced after the first
202        version of this structure was available.  Check the SIZE
203        argument passed to the dl_iterate_phdr callback to determine
204        whether or not each later member is available.  */
205 
206     /* Incremented when a new object may have been added.  */
207     unsigned long long int dlpi_adds;
208     /* Incremented when an object may have been removed.  */
209     unsigned long long int dlpi_subs;
210 
211     /* If there is a PT_TLS segment, its module ID as used in
212        TLS relocations, else zero.  */
213     size_t dlpi_tls_modid;
214 
215     /* The address of the calling thread's instance of this module's
216        PT_TLS segment, if it has one and it has been allocated
217        in the calling thread, otherwise a null pointer.  */
218     void *dlpi_tls_data;
219 #endif
220   };
221 
222 __BEGIN_DECLS
223 
224 extern int dl_iterate_phdr (int (*__callback) (struct dl_phdr_info *,
225 					       size_t, void *),
226 			    void *__data);
227 
228 
229 #if 0
230 /* Prototypes for the ld.so auditing interfaces.  These are not
231    defined anywhere in ld.so but instead have to be provided by the
232    auditing DSO.  */
233 extern unsigned int la_version (unsigned int __version);
234 extern void la_activity (uintptr_t *__cookie, unsigned int __flag);
235 extern char *la_objsearch (const char *__name, uintptr_t *__cookie,
236 			   unsigned int __flag);
237 extern unsigned int la_objopen (struct link_map *__map, Lmid_t __lmid,
238 				uintptr_t *__cookie);
239 extern void la_preinit (uintptr_t *__cookie);
240 extern uintptr_t la_symbind32 (Elf32_Sym *__sym, unsigned int __ndx,
241 			       uintptr_t *__refcook, uintptr_t *__defcook,
242 			       unsigned int *__flags, const char *__symname);
243 extern uintptr_t la_symbind64 (Elf64_Sym *__sym, unsigned int __ndx,
244 			       uintptr_t *__refcook, uintptr_t *__defcook,
245 			       unsigned int *__flags, const char *__symname);
246 extern unsigned int la_objclose (uintptr_t *__cookie);
247 #endif
248 
249 __END_DECLS
250 
251 #endif
252 
253 #endif /* link.h */
254