rtld.h revision 1.107 1 /* $NetBSD: rtld.h,v 1.107 2011/12/02 09:06:49 skrll 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 <signal.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/queue.h>
44 #include <sys/exec_elf.h>
45 #include <sys/tls.h>
46 #include "rtldenv.h"
47 #include "link.h"
48
49 #if defined(_RTLD_SOURCE)
50
51 #ifndef RTLD_DEFAULT_LIBRARY_PATH
52 #define RTLD_DEFAULT_LIBRARY_PATH "/usr/lib"
53 #endif
54 #define _PATH_LD_HINTS "/etc/ld.so.conf"
55
56 extern size_t _rtld_pagesz;
57
58 #define round_down(x) ((x) & ~(_rtld_pagesz - 1))
59 #define round_up(x) round_down((x) + _rtld_pagesz - 1)
60
61 #define NEW(type) ((type *) xmalloc(sizeof(type)))
62 #define CNEW(type) ((type *) xcalloc(sizeof(type)))
63
64 /*
65 * Fill in a DoneList with an allocation large enough to hold all of
66 * the currently-loaded objects. Keep this in a macro since it calls
67 * alloca and we want that to occur within the scope of the caller.
68 */
69 #define _rtld_donelist_init(dlp) \
70 ((dlp)->num_alloc = _rtld_objcount, \
71 (dlp)->objs = alloca((dlp)->num_alloc * sizeof((dlp)->objs[0])), \
72 assert((dlp)->objs != NULL), \
73 (dlp)->num_used = 0)
74
75 #endif /* _RTLD_SOURCE */
76
77 /*
78 * C++ has mandated the use of the following keywords for its new boolean
79 * type. We might as well follow their lead.
80 */
81 struct Struct_Obj_Entry;
82
83 typedef struct Struct_Objlist_Entry {
84 SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
85 struct Struct_Obj_Entry *obj;
86 } Objlist_Entry;
87
88 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
89
90 typedef struct Struct_Name_Entry {
91 STAILQ_ENTRY(Struct_Name_Entry) link;
92 char name[1];
93 } Name_Entry;
94
95 typedef struct Struct_Needed_Entry {
96 struct Struct_Needed_Entry *next;
97 struct Struct_Obj_Entry *obj;
98 unsigned long name; /* Offset of name in string table */
99 } Needed_Entry;
100
101 typedef struct _rtld_search_path_t {
102 struct _rtld_search_path_t *sp_next;
103 const char *sp_path;
104 size_t sp_pathlen;
105 } Search_Path;
106
107 typedef struct Struct_Ver_Entry {
108 Elf_Word hash;
109 u_int flags;
110 const char *name;
111 const char *file;
112 } Ver_Entry;
113
114 /* Ver_Entry.flags */
115 #define VER_INFO_HIDDEN 0x01
116
117
118 #define RTLD_MAX_ENTRY 10
119 #define RTLD_MAX_LIBRARY 4
120 #define RTLD_MAX_CTL 2
121 typedef struct _rtld_library_xform_t {
122 struct _rtld_library_xform_t *next;
123 char *name;
124 const char *ctlname;
125 struct {
126 char *value;
127 char *library[RTLD_MAX_LIBRARY];
128 } entry[RTLD_MAX_ENTRY];
129 } Library_Xform;
130
131 /*
132 * Shared object descriptor.
133 *
134 * Items marked with "(%)" are dynamically allocated, and must be freed
135 * when the structure is destroyed.
136 *
137 * The layout of this structure needs to be preserved because pre-2.0 binaries
138 * hard-coded the location of dlopen() and friends.
139 */
140
141 #define RTLD_MAGIC 0xd550b87a
142 #define RTLD_VERSION 1
143
144 typedef struct Struct_Obj_Entry {
145 Elf32_Word magic; /* Magic number (sanity check) */
146 Elf32_Word version; /* Version number of struct format */
147
148 struct Struct_Obj_Entry *next;
149 char *path; /* Pathname of underlying file (%) */
150 int refcount;
151 int dl_refcount; /* Number of times loaded by dlopen */
152
153 /* These items are computed by map_object() or by digest_phdr(). */
154 caddr_t mapbase; /* Base address of mapped region */
155 size_t mapsize; /* Size of mapped region in bytes */
156 size_t textsize; /* Size of text segment in bytes */
157 Elf_Addr vaddrbase; /* Base address in shared object file */
158 caddr_t relocbase; /* Reloc const = mapbase - *vaddrbase */
159 Elf_Dyn *dynamic; /* Dynamic section */
160 caddr_t entry; /* Entry point */
161 const Elf_Phdr *phdr; /* Program header (may be xmalloc'ed) */
162 size_t phsize; /* Size of program header in bytes */
163
164 /* Items from the dynamic section. */
165 Elf_Addr *pltgot; /* PLTGOT table */
166 const Elf_Rel *rel; /* Relocation entries */
167 const Elf_Rel *rellim; /* Limit of Relocation entries */
168 const Elf_Rela *rela; /* Relocation entries */
169 const Elf_Rela *relalim; /* Limit of Relocation entries */
170 const Elf_Rel *pltrel; /* PLT relocation entries */
171 const Elf_Rel *pltrellim; /* Limit of PLT relocation entries */
172 const Elf_Rela *pltrela; /* PLT relocation entries */
173 const Elf_Rela *pltrelalim; /* Limit of PLT relocation entries */
174 const Elf_Sym *symtab; /* Symbol table */
175 const char *strtab; /* String table */
176 unsigned long strsize; /* Size in bytes of string table */
177 #ifdef __mips__
178 Elf_Word local_gotno; /* Number of local GOT entries */
179 Elf_Word symtabno; /* Number of dynamic symbols */
180 Elf_Word gotsym; /* First dynamic symbol in GOT */
181 #endif
182
183 const Elf_Symindx *buckets; /* Hash table buckets array */
184 unsigned long unused1; /* Used to be nbuckets */
185 const Elf_Symindx *chains; /* Hash table chain array */
186 unsigned long nchains; /* Number of chains */
187
188 Search_Path *rpaths; /* Search path specified in object */
189 Needed_Entry *needed; /* Shared objects needed by this (%) */
190
191 void (*init)(void); /* Initialization function to call */
192 void (*fini)(void); /* Termination function to call */
193
194 /*
195 * BACKWARDS COMPAT Entry points for dlopen() and friends.
196 *
197 * DO NOT MOVE OR ADD TO THE LIST
198 *
199 */
200 void *(*dlopen)(const char *, int);
201 void *(*dlsym)(void *, const char *);
202 char *(*dlerror)(void);
203 int (*dlclose)(void *);
204 int (*dladdr)(const void *, Dl_info *);
205
206 u_int32_t mainprog:1, /* True if this is the main program */
207 rtld:1, /* True if this is the dynamic linker */
208 textrel:1, /* True if there are relocations to
209 * text seg */
210 symbolic:1, /* True if generated with
211 * "-Bsymbolic" */
212 printed:1, /* True if ldd has printed it */
213 isdynamic:1, /* True if this is a pure PIC object */
214 mainref:1, /* True if on _rtld_list_main */
215 globalref:1, /* True if on _rtld_list_global */
216 init_done:1, /* True if .init has been added */
217 init_called:1, /* True if .init function has been
218 * called */
219 fini_called:1, /* True if .fini function has been
220 * called */
221 z_now:1, /* True if object's symbols should be
222 bound immediately */
223 z_nodelete:1, /* True if object should never be
224 unloaded */
225 z_initfirst:1, /* True if object's .init/.fini take
226 * priority over others */
227 z_noopen:1, /* True if object should never be
228 dlopen'ed */
229 phdr_loaded:1, /* Phdr is loaded and doesn't need to
230 * be freed. */
231 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
232 tls_done:1, /* True if static TLS offset
233 * has been allocated */
234 #endif
235 ref_nodel:1; /* Refcount increased to prevent dlclose */
236
237 struct link_map linkmap; /* for GDB */
238
239 /* These items are computed by map_object() or by digest_phdr(). */
240 const char *interp; /* Pathname of the interpreter, if any */
241 Objlist dldags; /* Object belongs to these dlopened DAGs (%) */
242 Objlist dagmembers; /* DAG has these members (%) */
243 dev_t dev; /* Object's filesystem's device */
244 ino_t ino; /* Object's inode number */
245
246 void *ehdr;
247
248 uint32_t nbuckets; /* Number of buckets */
249 uint32_t nbuckets_m; /* Precomputed for fast remainder */
250 uint8_t nbuckets_s1;
251 uint8_t nbuckets_s2;
252 size_t pathlen; /* Pathname length */
253 STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we
254 know about. */
255
256 #ifdef __powerpc__
257 Elf_Addr *gotptr; /* GOT table (secure-plt only) */
258 #endif
259
260 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
261 /* Thread Local Storage support for this module */
262 size_t tlsindex; /* Index in DTV */
263 void *tlsinit; /* Base address of TLS init block */
264 size_t tlsinitsize; /* Size of TLS init block */
265 size_t tlssize; /* Size of TLS block */
266 size_t tlsoffset; /* Offset in the static TLS block */
267 size_t tlsalign; /* Needed alignment for static TLS */
268 #endif
269
270 /* symbol versioning */
271 const Elf_Verneed *verneed; /* Required versions. */
272 Elf_Word verneednum; /* Number of entries in verneed table */
273 const Elf_Verdef *verdef; /* Provided versions. */
274 Elf_Word verdefnum; /* Number of entries in verdef table */
275 const Elf_Versym *versyms; /* Symbol versions table */
276
277 Ver_Entry *vertab; /* Versions required/defined by this
278 * object */
279 int vertabnum; /* Number of entries in vertab */
280 } Obj_Entry;
281
282 typedef struct Struct_DoneList {
283 const Obj_Entry **objs; /* Array of object pointers */
284 unsigned int num_alloc; /* Allocated size of the array */
285 unsigned int num_used; /* Number of array slots used */
286 } DoneList;
287
288
289 #if defined(_RTLD_SOURCE)
290
291 extern struct r_debug _rtld_debug;
292 extern Search_Path *_rtld_default_paths;
293 extern Obj_Entry *_rtld_objlist;
294 extern Obj_Entry **_rtld_objtail;
295 extern u_int _rtld_objcount;
296 extern u_int _rtld_objloads;
297 extern Obj_Entry *_rtld_objmain;
298 extern Obj_Entry _rtld_objself;
299 extern Search_Path *_rtld_paths;
300 extern Library_Xform *_rtld_xforms;
301 extern bool _rtld_trust;
302 extern Objlist _rtld_list_global;
303 extern Objlist _rtld_list_main;
304 extern Elf_Sym _rtld_sym_zero;
305
306 #define RTLD_MODEMASK 0x3
307
308 /* Flags to be passed into _rtld_symlook_ family of functions. */
309 #define SYMLOOK_IN_PLT 0x01 /* Lookup for PLT symbol */
310 #define SYMLOOK_DLSYM 0x02 /* Return newes versioned symbol.
311 Used by dlsym. */
312
313 /* Flags for _rtld_load_object() and friends. */
314 #define _RTLD_GLOBAL 0x01 /* Add object to global DAG. */
315 #define _RTLD_MAIN 0x02
316 #define _RTLD_NOLOAD 0x04 /* dlopen() specified RTLD_NOLOAD. */
317 #define _RTLD_DLOPEN 0x08 /* Load_object() called from dlopen(). */
318
319 /* Preallocation for static TLS model */
320 #define RTLD_STATIC_TLS_RESERVATION 64
321
322 /* rtld.c */
323
324 /* We export these symbols using _rtld_symbol_lookup and is_exported. */
325 __dso_public char *dlerror(void);
326 __dso_public void *dlopen(const char *, int);
327 __dso_public void *dlsym(void *, const char *);
328 __dso_public int dlclose(void *);
329 __dso_public int dladdr(const void *, Dl_info *);
330 __dso_public int dlinfo(void *, int, void *);
331 __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
332 void *);
333
334 /* These aren't exported */
335 void _rtld_error(const char *, ...)
336 __attribute__((__format__(__printf__,1,2)));
337 void _rtld_die(void) __attribute__((__noreturn__));
338 void *_rtld_objmain_sym(const char *);
339 __dso_public void _rtld_debug_state(void);
340 void _rtld_linkmap_add(Obj_Entry *);
341 void _rtld_linkmap_delete(Obj_Entry *);
342 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
343 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
344 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
345 void _rtld_ref_dag(Obj_Entry *);
346
347 void _rtld_shared_enter(void);
348 void _rtld_shared_exit(void);
349 void _rtld_exclusive_enter(sigset_t *);
350 void _rtld_exclusive_exit(sigset_t *);
351
352 /* expand.c */
353 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
354 const char *);
355
356 /* headers.c */
357 void _rtld_digest_dynamic(const char *, Obj_Entry *);
358 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
359
360 /* load.c */
361 Obj_Entry *_rtld_load_object(const char *, int);
362 int _rtld_load_needed_objects(Obj_Entry *, int);
363 int _rtld_preload(const char *);
364
365 #define OBJ_ERR (Obj_Entry *)(-1)
366 /* path.c */
367 void _rtld_add_paths(const char *, Search_Path **, const char *);
368 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
369 const char *);
370 int _rtld_sysctl(const char *, void *, size_t *);
371
372 /* reloc.c */
373 int _rtld_do_copy_relocations(const Obj_Entry *);
374 int _rtld_relocate_objects(Obj_Entry *, bool);
375 int _rtld_relocate_nonplt_objects(Obj_Entry *);
376 int _rtld_relocate_plt_lazy(const Obj_Entry *);
377 int _rtld_relocate_plt_objects(const Obj_Entry *);
378 void _rtld_setup_pltgot(const Obj_Entry *);
379
380 /* search.c */
381 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
382
383 /* symbol.c */
384 unsigned long _rtld_elf_hash(const char *);
385 const Elf_Sym *_rtld_symlook_obj(const char *, unsigned long,
386 const Obj_Entry *, u_int, const Ver_Entry *);
387 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
388 const Obj_Entry **, u_int);
389 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
390 const Obj_Entry **, bool);
391
392 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
393 const Objlist *, const Obj_Entry **, u_int, const Ver_Entry *, DoneList *);
394 const Elf_Sym *_rtld_symlook_default(const char *, unsigned long,
395 const Obj_Entry *, const Obj_Entry **, u_int, const Ver_Entry *);
396 const Elf_Sym *_rtld_symlook_needed(const char *, unsigned long,
397 const Needed_Entry *, const Obj_Entry **, u_int, const Ver_Entry *,
398 DoneList *, DoneList *);
399 #ifdef COMBRELOC
400 void _rtld_combreloc_reset(const Obj_Entry *);
401 #endif
402
403 /* symver.c */
404 int _rtld_object_match_name(const Obj_Entry *, const char *);
405 int _rtld_verify_object_versions(Obj_Entry *);
406
407 static __inline const Ver_Entry *
408 _rtld_fetch_ventry(const Obj_Entry *obj, unsigned long symnum)
409 {
410 Elf_Half vernum;
411
412 if (obj->vertab) {
413 vernum = VER_NDX(obj->versyms[symnum].vs_vers);
414 if (vernum >= obj->vertabnum) {
415 _rtld_error("%s: symbol %s has wrong verneed value %d",
416 obj->path, &obj->strtab[symnum], vernum);
417 } else if (obj->vertab[vernum].hash) {
418 return &obj->vertab[vernum];
419 }
420 }
421 return NULL;
422 }
423
424 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
425 /* tls.c */
426 void *_rtld_tls_get_addr(void *, size_t, size_t);
427 void _rtld_tls_initial_allocation(void);
428 void *_rtld_tls_module_allocate(size_t index);
429 int _rtld_tls_offset_allocate(Obj_Entry *);
430 void _rtld_tls_offset_free(Obj_Entry *);
431
432 extern size_t _rtld_tls_dtv_generation;
433 extern size_t _rtld_tls_max_index;
434
435 __dso_public extern void *__tls_get_addr(void *);
436 #ifdef __i386__
437 __dso_public extern void *___tls_get_addr(void *)
438 __attribute__((__regparm__(1)));
439 #endif
440 #endif
441
442 /* map_object.c */
443 struct stat;
444 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
445 void _rtld_obj_free(Obj_Entry *);
446 Obj_Entry *_rtld_obj_new(void);
447
448 /* function descriptors */
449 #ifdef __HAVE_FUNCTION_DESCRIPTORS
450 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
451 const Elf_Sym *, Elf_Addr);
452 const void *_rtld_function_descriptor_function(const void *);
453 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
454
455 #endif /* _RTLD_SOURCE */
456
457 #endif /* RTLD_H */
458