xref: /freebsd/sys/sys/linker.h (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*-
2  * Copyright (c) 1997 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$Id: linker.h,v 1.4 1998/08/12 08:44:21 dfr Exp $
27  */
28 
29 #ifndef _SYS_LINKER_H_
30 #define _SYS_LINKER_H_
31 
32 #ifdef KERNEL
33 
34 #ifdef MALLOC_DECLARE
35 MALLOC_DECLARE(M_LINKER);
36 #endif
37 
38 /*
39  * Object representing a file which has been loaded by the linker.
40  */
41 typedef struct linker_file* linker_file_t;
42 typedef TAILQ_HEAD(, linker_file) linker_file_list_t;
43 
44 typedef caddr_t linker_sym_t;	/* opaque symbol */
45 
46 /*
47  * expanded out linker_sym_t
48  */
49 typedef struct linker_symval {
50     const char*		name;
51     caddr_t		value;
52     size_t		size;
53 } linker_symval_t;
54 
55 struct linker_file_ops {
56     /*
57      * Lookup a symbol in the file's symbol table.  If the symbol is
58      * not found then return ENOENT, otherwise zero.  If the symbol
59      * found is a common symbol, return with *address set to zero and
60      * *size set to the size of the common space required.  Otherwise
61      * set *address the value of the symbol.
62      */
63     int			(*lookup_symbol)(linker_file_t, const char* name,
64 					 linker_sym_t* sym);
65 
66     void		(*symbol_values)(linker_file_t, linker_sym_t,
67 					 linker_symval_t*);
68 
69     int			(*search_symbol)(linker_file_t, caddr_t value,
70 					 linker_sym_t* sym, long* diffp);
71 
72     /*
73      * Unload a file, releasing dependancies and freeing storage.
74      */
75     void		(*unload)(linker_file_t);
76 };
77 
78 struct common_symbol {
79     STAILQ_ENTRY(common_symbol) link;
80     char*		name;
81     caddr_t		address;
82 };
83 
84 struct linker_file {
85     int			refs;		/* reference count */
86     int			userrefs;	/* modload(2) count */
87     TAILQ_ENTRY(linker_file) link;	/* list of all loaded files */
88     char*		filename;	/* file which was loaded */
89     int			id;		/* unique id */
90     caddr_t		address;	/* load address */
91     size_t		size;		/* size of file */
92     int			ndeps;		/* number of dependancies */
93     linker_file_t*	deps;		/* list of dependancies */
94     STAILQ_HEAD(, common_symbol) common; /* list of common symbols */
95     TAILQ_HEAD(, module) modules;	/* modules in this file */
96     void*		priv;		/* implementation data */
97 
98     struct linker_file_ops* ops;
99 };
100 
101 /*
102  * Object implementing a class of file (a.out, elf, etc.)
103  */
104 typedef struct linker_class *linker_class_t;
105 typedef TAILQ_HEAD(, linker_class) linker_class_list_t;
106 
107 struct linker_class_ops {
108     /*
109      * Load a file, returning the new linker_file_t in *result.  If
110      * the class does not recognise the file type, zero should be
111      * returned, without modifying *result.  If the file is
112      * recognised, the file should be loaded, *result set to the new
113      * file and zero returned.  If some other error is detected an
114      * appropriate errno should be returned.
115      */
116     int		(*load_file)(const char* filename, linker_file_t* result);
117 };
118 
119 struct linker_class {
120     TAILQ_ENTRY(linker_class) link;	/* list of all file classes */
121     const char*		desc;		/* description (e.g. "a.out") */
122     void*		priv;		/* implementation data */
123 
124     struct linker_class_ops *ops;
125 };
126 
127 /*
128  * The file which is currently loading.  Used to register modules with
129  * the files which contain them.
130  */
131 extern linker_file_t	linker_current_file;
132 
133 /*
134  * Add a new file class to the linker.
135  */
136 int linker_add_class(const char* desc, void* priv,
137 		     struct linker_class_ops* ops);
138 
139 /*
140  * Load a file, trying each file class until one succeeds.
141  */
142 int linker_load_file(const char* filename, linker_file_t* result);
143 
144 /*
145  * Find a currently loaded file given its filename.
146  */
147 linker_file_t linker_find_file_by_name(const char* filename);
148 
149 /*
150  * Find a currently loaded file given its file id.
151  */
152 linker_file_t linker_find_file_by_id(int fileid);
153 
154 /*
155  * Called from a class handler when a file is laoded.
156  */
157 linker_file_t linker_make_file(const char* filename, void* priv,
158 			       struct linker_file_ops* ops);
159 
160 /*
161  * Unload a file, freeing up memory.
162  */
163 int linker_file_unload(linker_file_t file);
164 
165 /*
166  * Add a dependancy to a file.
167  */
168 int linker_file_add_dependancy(linker_file_t file, linker_file_t dep);
169 
170 /*
171  * Lookup a symbol in a file.  If deps is TRUE, look in dependancies
172  * if not found in file.
173  */
174 caddr_t linker_file_lookup_symbol(linker_file_t file, const char* name,
175 				  int deps);
176 
177 /*
178  * Module information subtypes
179  */
180 #define MODINFO_NAME		0x0000
181 #define MODINFO_TYPE		0x0001
182 #define MODINFO_ADDR		0x0002
183 #define MODINFO_SIZE		0x0003
184 #define MODINFO_METADATA	0x8000
185 
186 #define MODINFOMD_AOUTEXEC	0x0001		/* a.out exec header */
187 #define MODINFOMD_ELFHDR	0x0002		/* ELF header */
188 #define MODINFOMD_NOCOPY	0x8000		/* don't copy this metadata to the kernel */
189 
190 #define KLD_IDENT_SYMNAME	"kld_identifier_"
191 #define MODINFOMD_KLDIDENT	(MODINFOMD_NOCOPY | 0x4000)
192 #define MODINFOMD_KLDDEP	(MODINFOMD_NOCOPY | 0x4001)
193 
194 #ifdef KLD_DEBUG
195 
196 extern int kld_debug;
197 #define KLD_DEBUG_FILE	1	/* file load/unload */
198 #define KLD_DEBUG_SYM	2	/* symbol lookup */
199 
200 #define KLD_DPF(cat, args)					\
201 	do {							\
202 		if (KLD_debug & KLD_DEBUG_##cat) printf args;	\
203 	} while (0)
204 
205 #else
206 
207 #define KLD_DPF(cat, args)
208 
209 #endif
210 
211 #endif /* KERNEL */
212 
213 struct kld_file_stat {
214     int		version;	/* set to sizeof(linker_file_stat) */
215     char        name[MAXPATHLEN];
216     int		refs;
217     int		id;
218     caddr_t	address;	/* load address */
219     size_t	size;		/* size in bytes */
220 };
221 
222 #ifndef KERNEL
223 
224 #include <sys/cdefs.h>
225 
226 __BEGIN_DECLS
227 int	kldload(const char* file);
228 int	kldunload(int fileid);
229 int	kldfind(const char* file);
230 int	kldnext(int fileid);
231 int	kldstat(int fileid, struct kld_file_stat* stat);
232 int	kldfirstmod(int fileid);
233 __END_DECLS
234 
235 #endif
236 
237 #endif /* !_SYS_LINKER_H_ */
238