rtld.h revision 1.103 1 /* $NetBSD: rtld.h,v 1.103 2011/03/12 22:54:36 joerg Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Polstra.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef RTLD_H
35 #define RTLD_H
36
37 #include <dlfcn.h>
38 #include <stdbool.h>
39 #include <stddef.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/queue.h>
43 #include <sys/exec_elf.h>
44 #include <sys/tls.h>
45 #include "rtldenv.h"
46 #include "link.h"
47
48 #if defined(_RTLD_SOURCE)
49
50 #ifndef RTLD_DEFAULT_LIBRARY_PATH
51 #define RTLD_DEFAULT_LIBRARY_PATH "/usr/lib"
52 #endif
53 #define _PATH_LD_HINTS "/etc/ld.so.conf"
54
55 extern size_t _rtld_pagesz;
56
57 #define round_down(x) ((x) & ~(_rtld_pagesz - 1))
58 #define round_up(x) round_down((x) + _rtld_pagesz - 1)
59
60 #define NEW(type) ((type *) xmalloc(sizeof(type)))
61 #define CNEW(type) ((type *) xcalloc(sizeof(type)))
62
63 /*
64 * Fill in a DoneList with an allocation large enough to hold all of
65 * the currently-loaded objects. Keep this in a macro since it calls
66 * alloca and we want that to occur within the scope of the caller.
67 */
68 #define _rtld_donelist_init(dlp) \
69 ((dlp)->num_alloc = _rtld_objcount, \
70 (dlp)->objs = alloca((dlp)->num_alloc * sizeof((dlp)->objs[0])), \
71 assert((dlp)->objs != NULL), \
72 (dlp)->num_used = 0)
73
74 #endif /* _RTLD_SOURCE */
75
76 /*
77 * C++ has mandated the use of the following keywords for its new boolean
78 * type. We might as well follow their lead.
79 */
80 struct Struct_Obj_Entry;
81
82 typedef struct Struct_Objlist_Entry {
83 SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
84 struct Struct_Obj_Entry *obj;
85 } Objlist_Entry;
86
87 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
88
89 typedef struct Struct_Name_Entry {
90 STAILQ_ENTRY(Struct_Name_Entry) link;
91 char name[1];
92 } Name_Entry;
93
94 typedef struct Struct_Needed_Entry {
95 struct Struct_Needed_Entry *next;
96 struct Struct_Obj_Entry *obj;
97 unsigned long name; /* Offset of name in string table */
98 } Needed_Entry;
99
100 typedef struct _rtld_search_path_t {
101 struct _rtld_search_path_t *sp_next;
102 const char *sp_path;
103 size_t sp_pathlen;
104 } Search_Path;
105
106
107 #define RTLD_MAX_ENTRY 10
108 #define RTLD_MAX_LIBRARY 4
109 #define RTLD_MAX_CTL 2
110 typedef struct _rtld_library_xform_t {
111 struct _rtld_library_xform_t *next;
112 char *name;
113 const char *ctlname;
114 struct {
115 char *value;
116 char *library[RTLD_MAX_LIBRARY];
117 } entry[RTLD_MAX_ENTRY];
118 } Library_Xform;
119
120 /*
121 * Shared object descriptor.
122 *
123 * Items marked with "(%)" are dynamically allocated, and must be freed
124 * when the structure is destroyed.
125 *
126 * The layout of this structure needs to be preserved because pre-2.0 binaries
127 * hard-coded the location of dlopen() and friends.
128 */
129
130 #define RTLD_MAGIC 0xd550b87a
131 #define RTLD_VERSION 1
132
133 typedef struct Struct_Obj_Entry {
134 Elf32_Word magic; /* Magic number (sanity check) */
135 Elf32_Word version; /* Version number of struct format */
136
137 struct Struct_Obj_Entry *next;
138 char *path; /* Pathname of underlying file (%) */
139 int refcount;
140 int dl_refcount; /* Number of times loaded by dlopen */
141
142 /* These items are computed by map_object() or by digest_phdr(). */
143 caddr_t mapbase; /* Base address of mapped region */
144 size_t mapsize; /* Size of mapped region in bytes */
145 size_t textsize; /* Size of text segment in bytes */
146 Elf_Addr vaddrbase; /* Base address in shared object file */
147 caddr_t relocbase; /* Reloc const = mapbase - *vaddrbase */
148 Elf_Dyn *dynamic; /* Dynamic section */
149 caddr_t entry; /* Entry point */
150 const Elf_Phdr *phdr; /* Program header (may be xmalloc'ed) */
151 size_t phsize; /* Size of program header in bytes */
152
153 /* Items from the dynamic section. */
154 Elf_Addr *pltgot; /* PLTGOT table */
155 const Elf_Rel *rel; /* Relocation entries */
156 const Elf_Rel *rellim; /* Limit of Relocation entries */
157 const Elf_Rela *rela; /* Relocation entries */
158 const Elf_Rela *relalim; /* Limit of Relocation entries */
159 const Elf_Rel *pltrel; /* PLT relocation entries */
160 const Elf_Rel *pltrellim; /* Limit of PLT relocation entries */
161 const Elf_Rela *pltrela; /* PLT relocation entries */
162 const Elf_Rela *pltrelalim; /* Limit of PLT relocation entries */
163 const Elf_Sym *symtab; /* Symbol table */
164 const char *strtab; /* String table */
165 unsigned long strsize; /* Size in bytes of string table */
166 #ifdef __mips__
167 Elf_Word local_gotno; /* Number of local GOT entries */
168 Elf_Word symtabno; /* Number of dynamic symbols */
169 Elf_Word gotsym; /* First dynamic symbol in GOT */
170 #endif
171
172 const Elf_Symindx *buckets; /* Hash table buckets array */
173 unsigned long unused1; /* Used to be nbuckets */
174 const Elf_Symindx *chains; /* Hash table chain array */
175 unsigned long nchains; /* Number of chains */
176
177 Search_Path *rpaths; /* Search path specified in object */
178 Needed_Entry *needed; /* Shared objects needed by this (%) */
179
180 void (*init)(void); /* Initialization function to call */
181 void (*fini)(void); /* Termination function to call */
182
183 /* Entry points for dlopen() and friends. */
184 void *(*dlopen)(const char *, int);
185 void *(*dlsym)(void *, const char *);
186 char *(*dlerror)(void);
187 int (*dlclose)(void *);
188 int (*dladdr)(const void *, Dl_info *);
189
190 u_int32_t mainprog:1, /* True if this is the main program */
191 rtld:1, /* True if this is the dynamic linker */
192 textrel:1, /* True if there are relocations to
193 * text seg */
194 symbolic:1, /* True if generated with
195 * "-Bsymbolic" */
196 printed:1, /* True if ldd has printed it */
197 isdynamic:1, /* True if this is a pure PIC object */
198 mainref:1, /* True if on _rtld_list_main */
199 globalref:1, /* True if on _rtld_list_global */
200 init_done:1, /* True if .init has been added */
201 init_called:1, /* True if .init function has been
202 * called */
203 fini_called:1, /* True if .fini function has been
204 * called */
205 z_now:1, /* True if object's symbols should be
206 bound immediately */
207 z_nodelete:1, /* True if object should never be
208 unloaded */
209 z_initfirst:1, /* True if object's .init/.fini take
210 * priority over others */
211 z_noopen:1, /* True if object should never be
212 dlopen'ed */
213 phdr_loaded:1, /* Phdr is loaded and doesn't need to
214 * be freed. */
215 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
216 tls_done:1, /* True if static TLS offset
217 * has been allocated */
218 #endif
219 ref_nodel:1; /* Refcount increased to prevent dlclose */
220
221 struct link_map linkmap; /* for GDB */
222
223 /* These items are computed by map_object() or by digest_phdr(). */
224 const char *interp; /* Pathname of the interpreter, if any */
225 Objlist dldags; /* Object belongs to these dlopened DAGs (%) */
226 Objlist dagmembers; /* DAG has these members (%) */
227 dev_t dev; /* Object's filesystem's device */
228 ino_t ino; /* Object's inode number */
229
230 void *ehdr;
231
232 uint32_t nbuckets; /* Number of buckets */
233 uint32_t nbuckets_m; /* Precomputed for fast remainder */
234 uint8_t nbuckets_s1;
235 uint8_t nbuckets_s2;
236 size_t pathlen; /* Pathname length */
237 STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we
238 know about. */
239
240 #ifdef __powerpc__
241 Elf_Addr *gotptr; /* GOT table (secure-plt only) */
242 #endif
243
244 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
245 /* Thread Local Storage support for this module */
246 size_t tlsindex; /* Index in DTV */
247 void *tlsinit; /* Base address of TLS init block */
248 size_t tlsinitsize; /* Size of TLS init block */
249 size_t tlssize; /* Size of TLS block */
250 size_t tlsoffset; /* Offset in the static TLS block */
251 size_t tlsalign; /* Needed alignment for static TLS */
252 #endif
253 } Obj_Entry;
254
255 typedef struct Struct_DoneList {
256 const Obj_Entry **objs; /* Array of object pointers */
257 unsigned int num_alloc; /* Allocated size of the array */
258 unsigned int num_used; /* Number of array slots used */
259 } DoneList;
260
261
262 #if defined(_RTLD_SOURCE)
263
264 extern struct r_debug _rtld_debug;
265 extern Search_Path *_rtld_default_paths;
266 extern Obj_Entry *_rtld_objlist;
267 extern Obj_Entry **_rtld_objtail;
268 extern u_int _rtld_objcount;
269 extern u_int _rtld_objloads;
270 extern Obj_Entry *_rtld_objmain;
271 extern Obj_Entry _rtld_objself;
272 extern Search_Path *_rtld_paths;
273 extern Library_Xform *_rtld_xforms;
274 extern bool _rtld_trust;
275 extern Objlist _rtld_list_global;
276 extern Objlist _rtld_list_main;
277 extern Elf_Sym _rtld_sym_zero;
278
279 #define RTLD_MODEMASK 0x3
280
281 /* Flags for _rtld_load_object() and friends. */
282 #define _RTLD_GLOBAL 0x01 /* Add object to global DAG. */
283 #define _RTLD_MAIN 0x02
284 #define _RTLD_NOLOAD 0x04 /* dlopen() specified RTLD_NOLOAD. */
285 #define _RTLD_DLOPEN 0x08 /* Load_object() called from dlopen(). */
286
287 /* Preallocation for static TLS model */
288 #define RTLD_STATIC_TLS_RESERVATION 64
289
290 /* rtld.c */
291
292 /* We export these symbols using _rtld_symbol_lookup and is_exported. */
293 __dso_public char *dlerror(void);
294 __dso_public void *dlopen(const char *, int);
295 __dso_public void *dlsym(void *, const char *);
296 __dso_public int dlclose(void *);
297 __dso_public int dladdr(const void *, Dl_info *);
298 __dso_public int dlinfo(void *, int, void *);
299 __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
300 void *);
301
302 /* These aren't exported */
303 void _rtld_error(const char *, ...)
304 __attribute__((__format__(__printf__,1,2)));
305 void _rtld_die(void) __attribute__((__noreturn__));
306 void *_rtld_objmain_sym(const char *);
307 __dso_public void _rtld_debug_state(void);
308 void _rtld_linkmap_add(Obj_Entry *);
309 void _rtld_linkmap_delete(Obj_Entry *);
310 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
311 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
312 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
313 void _rtld_ref_dag(Obj_Entry *);
314
315 /* expand.c */
316 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
317 const char *);
318
319 /* headers.c */
320 void _rtld_digest_dynamic(const char *, Obj_Entry *);
321 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
322
323 /* load.c */
324 Obj_Entry *_rtld_load_object(const char *, int);
325 int _rtld_load_needed_objects(Obj_Entry *, int);
326 int _rtld_preload(const char *);
327
328 #define OBJ_ERR (Obj_Entry *)(-1)
329 /* path.c */
330 void _rtld_add_paths(const char *, Search_Path **, const char *);
331 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
332 const char *);
333 int _rtld_sysctl(const char *, void *, size_t *);
334
335 /* reloc.c */
336 int _rtld_do_copy_relocations(const Obj_Entry *);
337 int _rtld_relocate_objects(Obj_Entry *, bool);
338 int _rtld_relocate_nonplt_objects(Obj_Entry *);
339 int _rtld_relocate_plt_lazy(const Obj_Entry *);
340 int _rtld_relocate_plt_objects(const Obj_Entry *);
341 void _rtld_setup_pltgot(const Obj_Entry *);
342
343 /* search.c */
344 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
345
346 /* symbol.c */
347 unsigned long _rtld_elf_hash(const char *);
348 const Elf_Sym *_rtld_symlook_obj(const char *, unsigned long,
349 const Obj_Entry *, bool);
350 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
351 const Obj_Entry **, bool);
352 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
353 const Obj_Entry **, bool);
354
355 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
356 const Objlist *, const Obj_Entry **, bool, DoneList *);
357 const Elf_Sym *_rtld_symlook_default(const char *, unsigned long,
358 const Obj_Entry *, const Obj_Entry **, bool);
359 const Elf_Sym *_rtld_symlook_needed(const char *, unsigned long,
360 const Needed_Entry *, const Obj_Entry **, bool,
361 DoneList *, DoneList *);
362 #ifdef COMBRELOC
363 void _rtld_combreloc_reset(const Obj_Entry *);
364 #endif
365
366 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
367 /* tls.c */
368 void *_rtld_tls_get_addr(void *, size_t, size_t);
369 void _rtld_tls_initial_allocation(void);
370 void *_rtld_tls_module_allocate(size_t index);
371 int _rtld_tls_offset_allocate(Obj_Entry *);
372 void _rtld_tls_offset_free(Obj_Entry *);
373
374 extern size_t _rtld_tls_dtv_generation;
375 extern size_t _rtld_tls_max_index;
376
377 __dso_public extern void *__tls_get_addr(void *);
378 #ifdef __i386__
379 __dso_public extern void *___tls_get_addr(void *)
380 __attribute__((__regparm__(1)));
381 #endif
382 #endif
383
384 /* map_object.c */
385 struct stat;
386 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
387 void _rtld_obj_free(Obj_Entry *);
388 Obj_Entry *_rtld_obj_new(void);
389
390 /* function descriptors */
391 #ifdef __HAVE_FUNCTION_DESCRIPTORS
392 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
393 const Elf_Sym *, Elf_Addr);
394 const void *_rtld_function_descriptor_function(const void *);
395 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
396
397 #endif /* _RTLD_SOURCE */
398
399 #endif /* RTLD_H */
400