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