rtld.h revision 1.20 1 /* $NetBSD: rtld.h,v 1.20 1999/12/05 18:36:25 fredb 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 <stddef.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <sys/exec_elf.h>
43 #include "rtldenv.h"
44 #include "link.h"
45
46 #if defined(_RTLD_SOURCE)
47
48 #define RTLD_DEFAULT_LIBRARY_PATH "/usr/lib"
49 #define _PATH_LD_HINTS "/etc/ld.so.conf"
50
51 #if 0
52 #define SVR4_LIBDIR "/usr/lib"
53 #endif
54
55 #define LIBDIRLEN (sizeof LIBDIR - 1)
56 #define SVR4_LIBDIRLEN (sizeof SVR4_LIBDIR - 1)
57
58 #ifndef PAGESIZE
59 # ifdef VARPSZ
60 extern int _rtld_pagesz;
61 # ifdef RTLD_DEBUG
62 # define PAGESIZE (assert(_rtld_pagesz), _rtld_pagesz)
63 # else
64 # define PAGESIZE _rtld_pagesz
65 # endif
66 # else
67 # ifndef __sparc__
68 # define PAGESIZE NBPG
69 # else
70 #error "Sparc has a variable page size"
71 # endif
72 # endif
73 #endif
74
75 #define round_down(x) ((x) & ~(PAGESIZE-1))
76 #define round_up(x) round_down((x) + PAGESIZE - 1)
77
78 #define NEW(type) ((type *) xmalloc(sizeof(type)))
79 #define CNEW(type) ((type *) xcalloc(sizeof(type)))
80
81 #endif /* _RTLD_SOURCE */
82
83 /*
84 * C++ has mandated the use of the following keywords for its new boolean
85 * type. We might as well follow their lead.
86 */
87 typedef enum {
88 false = 0,
89 true = 1
90 } bool;
91
92 struct Struct_Obj_Entry;
93
94 typedef struct Struct_Objlist_Entry {
95 SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
96 struct Struct_Obj_Entry *obj;
97 } Objlist_Entry;
98
99 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
100
101 typedef struct Struct_Needed_Entry {
102 struct Struct_Needed_Entry *next;
103 struct Struct_Obj_Entry *obj;
104 unsigned long name; /* Offset of name in string table */
105 } Needed_Entry;
106
107 typedef struct _rtld_search_path_t {
108 struct _rtld_search_path_t *sp_next;
109 const char *sp_path;
110 size_t sp_pathlen;
111 } Search_Path;
112
113 /*
114 * Shared object descriptor.
115 *
116 * Items marked with "(%)" are dynamically allocated, and must be freed
117 * when the structure is destroyed.
118 */
119
120 #define RTLD_MAGIC 0xd550b87a
121 #define RTLD_VERSION 1
122
123 typedef struct Struct_Obj_Entry {
124 Elf32_Word magic; /* Magic number (sanity check) */
125 Elf32_Word version; /* Version number of struct format */
126
127 struct Struct_Obj_Entry *next;
128 char *path; /* Pathname of underlying file (%) */
129 int refcount;
130 int dl_refcount; /* Number of times loaded by dlopen */
131
132 /* These items are computed by map_object() or by digest_phdr(). */
133 caddr_t mapbase; /* Base address of mapped region */
134 size_t mapsize; /* Size of mapped region in bytes */
135 size_t textsize; /* Size of text segment in bytes */
136 Elf_Addr vaddrbase; /* Base address in shared object file */
137 caddr_t relocbase; /* Reloc const = mapbase - *vaddrbase */
138 Elf_Dyn *dynamic; /* Dynamic section */
139 caddr_t entry; /* Entry point */
140 const Elf_Phdr *phdr; /* Program header if mapped, ow NULL */
141 size_t phsize; /* Size of program header in bytes */
142
143 /* Items from the dynamic section. */
144 Elf_Addr *pltgot; /* PLTGOT table */
145 const Elf_Rel *rel; /* Relocation entries */
146 const Elf_Rel *rellim; /* Limit of Relocation entries */
147 const Elf_RelA *rela; /* Relocation entries */
148 const Elf_RelA *relalim; /* Limit of Relocation entries */
149 const Elf_Rel *pltrel; /* PLT relocation entries */
150 const Elf_Rel *pltrellim; /* Limit of PLT relocation entries */
151 const Elf_RelA *pltrela; /* PLT relocation entries */
152 const Elf_RelA *pltrelalim; /* Limit of PLT relocation entries */
153 const Elf_Sym *symtab; /* Symbol table */
154 const char *strtab; /* String table */
155 unsigned long strsize; /* Size in bytes of string table */
156 #if defined(__mips__)
157 Elf_Word local_gotno; /* Number of local GOT entries */
158 Elf_Word symtabno; /* Number of dynamic symbols */
159 Elf_Word gotsym; /* First dynamic symbol in GOT */
160 #endif
161
162 const Elf_Word *buckets; /* Hash table buckets array */
163 unsigned long nbuckets; /* Number of buckets */
164 const Elf_Word *chains; /* Hash table chain array */
165 unsigned long nchains; /* Number of chains */
166
167 Search_Path *rpaths; /* Search path specified in object */
168 Needed_Entry *needed; /* Shared objects needed by this (%) */
169
170 void (*init) /* Initialization function to call */
171 __P((void));
172 void (*fini) /* Termination function to call */
173 __P((void));
174
175 /* Entry points for dlopen() and friends. */
176 void *(*dlopen) __P((const char *, int));
177 void *(*dlsym) __P((void *, const char *));
178 char *(*dlerror) __P((void));
179 int (*dlclose) __P((void *));
180
181 int mainprog:1; /* True if this is the main program */
182 int rtld:1; /* True if this is the dynamic linker */
183 int textrel:1; /* True if there are relocations to
184 * text seg */
185 int symbolic:1; /* True if generated with
186 * "-Bsymbolic" */
187 int printed:1; /* True if ldd has printed it */
188
189 struct link_map linkmap; /* for GDB */
190
191 /* These items are computed by map_object() or by digest_phdr(). */
192 const char *interp; /* Pathname of the interpreter, if any */
193 Objlist dldags; /* Object belongs to these dlopened DAGs (%) */
194 Objlist dagmembers; /* DAG has these members (%) */
195 dev_t dev; /* Object's filesystem's device */
196 ino_t ino; /* Object's inode number */
197 unsigned long mark; /* Set to "_rtld_curmark" to avoid
198 repeat visits */
199 } Obj_Entry;
200
201 #if defined(_RTLD_SOURCE)
202
203 extern struct r_debug _rtld_debug;
204 extern Search_Path *_rtld_default_paths;
205 extern Obj_Entry *_rtld_objlist;
206 extern Obj_Entry **_rtld_objtail;
207 extern Obj_Entry *_rtld_objmain;
208 extern Obj_Entry _rtld_objself;
209 extern Search_Path *_rtld_paths;
210 extern bool _rtld_trust;
211 extern const char *_rtld_error_message;
212 extern unsigned long _rtld_curmark;
213 extern Objlist _rtld_list_global;
214 extern Objlist _rtld_list_main;
215 extern Elf_Sym _rtld_sym_zero;
216
217 /* rtld_start.S */
218 void _rtld_bind_start __P((void));
219
220 /* rtld.c */
221 void _rtld_error __P((const char *, ...));
222 void _rtld_die __P((void));
223 char *_rtld_dlerror __P((void));
224 void *_rtld_dlopen __P((const char *, int));
225 void *_rtld_dlsym __P((void *, const char *));
226 int _rtld_dlclose __P((void *));
227 int _rtld_dladdr __P((const void *, Dl_info *));
228 void _rtld_debug_state __P((void));
229 void _rtld_linkmap_add __P((Obj_Entry *));
230 void _rtld_linkmap_delete __P((Obj_Entry *));
231
232 /* headers.c */
233 void _rtld_digest_dynamic __P((Obj_Entry *));
234 Obj_Entry *_rtld_digest_phdr __P((const Elf_Phdr *, int, caddr_t));
235
236 /* load.c */
237 Obj_Entry *_rtld_load_object __P((char *, bool));
238 int _rtld_load_needed_objects __P((Obj_Entry *));
239 int _rtld_preload __P((const char *, bool));
240
241 /* path.c */
242 void _rtld_add_paths __P((Search_Path **, const char *, bool));
243 void _rtld_process_hints __P((Search_Path **, const char *, bool));
244
245 /* reloc.c */
246 int _rtld_do_copy_relocations __P((const Obj_Entry *, bool));
247 caddr_t _rtld_bind __P((Obj_Entry *, Elf_Word));
248 int _rtld_relocate_objects __P((Obj_Entry *, bool, bool));
249 int _rtld_relocate_nonplt_object __P((Obj_Entry *,
250 const Elf_RelA *, bool));
251 int _rtld_relocate_plt_object __P((Obj_Entry *, const Elf_RelA *,
252 caddr_t *, bool, bool));
253
254 /* search.c */
255 char *_rtld_find_library __P((const char *, const Obj_Entry *));
256
257 /* symbol.c */
258 unsigned long _rtld_elf_hash __P((const char *));
259 const Elf_Sym *_rtld_symlook_obj __P((const char *, unsigned long,
260 const Obj_Entry *, bool));
261 const Elf_Sym *_rtld_find_symdef __P((const Obj_Entry *, Elf_Word,
262 const char *, Obj_Entry *, const Obj_Entry **, bool));
263 const Elf_Sym *_rtld_symlook_list(const char *, unsigned long,
264 Objlist *, const Obj_Entry **, bool in_plt);
265
266 /* map_object.c */
267 Obj_Entry *_rtld_map_object __P((const char *, int, const struct stat *));
268 void _rtld_obj_free(Obj_Entry *);
269 Obj_Entry *_rtld_obj_new(void);
270
271 #if defined(__mips__)
272 /* mips_reloc.c */
273 void _rtld_relocate_mips_got __P((Obj_Entry *));
274 caddr_t _rtld_bind_mips __P((Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr));
275 #endif
276
277 #if defined(__powerpc__)
278 /* ppc_reloc.c */
279 caddr_t _rtld_bind_powerpc __P((Obj_Entry *, Elf_Word));
280 int _rtld_reloc_powerpc_plt __P((Obj_Entry *, const Elf_RelA *, bool));
281 void _rtld_setup_powerpc_plt __P((const Obj_Entry *));
282 #endif
283
284 #endif /* _RTLD_SOURCE */
285
286 #endif /* RTLD_H */
287