xref: /freebsd/sys/sys/linker.h (revision 326e27d81f29a67ad3042ead71d76c0e9541d98f)
1cea6c86cSDoug Rabson /*-
2326e27d8SDoug Rabson  * Copyright (c) 1997-2000 Doug Rabson
3cea6c86cSDoug Rabson  * All rights reserved.
4cea6c86cSDoug Rabson  *
5cea6c86cSDoug Rabson  * Redistribution and use in source and binary forms, with or without
6cea6c86cSDoug Rabson  * modification, are permitted provided that the following conditions
7cea6c86cSDoug Rabson  * are met:
8cea6c86cSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
9cea6c86cSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
10cea6c86cSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
11cea6c86cSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
12cea6c86cSDoug Rabson  *    documentation and/or other materials provided with the distribution.
13cea6c86cSDoug Rabson  *
14cea6c86cSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15cea6c86cSDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16cea6c86cSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17cea6c86cSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18cea6c86cSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19cea6c86cSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20cea6c86cSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21cea6c86cSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22cea6c86cSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23cea6c86cSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24cea6c86cSDoug Rabson  * SUCH DAMAGE.
25cea6c86cSDoug Rabson  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27cea6c86cSDoug Rabson  */
28cea6c86cSDoug Rabson 
29cea6c86cSDoug Rabson #ifndef _SYS_LINKER_H_
30cea6c86cSDoug Rabson #define _SYS_LINKER_H_
31cea6c86cSDoug Rabson 
32664a31e4SPeter Wemm #ifdef _KERNEL
33cea6c86cSDoug Rabson 
3481450045SPeter Wemm #include <machine/elf.h>
35326e27d8SDoug Rabson #include <sys/kobj.h>
3681450045SPeter Wemm 
37452ed6dcSBruce Evans #ifdef MALLOC_DECLARE
38452ed6dcSBruce Evans MALLOC_DECLARE(M_LINKER);
39452ed6dcSBruce Evans #endif
40cea6c86cSDoug Rabson 
41cea6c86cSDoug Rabson /*
42cea6c86cSDoug Rabson  * Object representing a file which has been loaded by the linker.
43cea6c86cSDoug Rabson  */
44cea6c86cSDoug Rabson typedef struct linker_file* linker_file_t;
45cea6c86cSDoug Rabson typedef TAILQ_HEAD(, linker_file) linker_file_list_t;
46cea6c86cSDoug Rabson 
47a2c99e3eSDoug Rabson typedef caddr_t linker_sym_t;		/* opaque symbol */
48d254af07SMatthew Dillon typedef c_caddr_t c_linker_sym_t;	/* const opaque symbol */
49a2c99e3eSDoug Rabson 
50a2c99e3eSDoug Rabson /*
51a2c99e3eSDoug Rabson  * expanded out linker_sym_t
52a2c99e3eSDoug Rabson  */
53a2c99e3eSDoug Rabson typedef struct linker_symval {
54a2c99e3eSDoug Rabson     const char*		name;
55a2c99e3eSDoug Rabson     caddr_t		value;
56a2c99e3eSDoug Rabson     size_t		size;
57a2c99e3eSDoug Rabson } linker_symval_t;
58a2c99e3eSDoug Rabson 
59cea6c86cSDoug Rabson struct common_symbol {
60cea6c86cSDoug Rabson     STAILQ_ENTRY(common_symbol) link;
61cea6c86cSDoug Rabson     char*		name;
62cea6c86cSDoug Rabson     caddr_t		address;
63cea6c86cSDoug Rabson };
64cea6c86cSDoug Rabson 
65cea6c86cSDoug Rabson struct linker_file {
66326e27d8SDoug Rabson     KOBJ_FIELDS;
67cea6c86cSDoug Rabson     int			refs;		/* reference count */
6881450045SPeter Wemm     int			userrefs;	/* kldload(2) count */
69149a155cSDoug Rabson     int			flags;
70149a155cSDoug Rabson #define LINKER_FILE_LINKED	0x1	/* file has been fully linked */
71cea6c86cSDoug Rabson     TAILQ_ENTRY(linker_file) link;	/* list of all loaded files */
72cea6c86cSDoug Rabson     char*		filename;	/* file which was loaded */
73cea6c86cSDoug Rabson     int			id;		/* unique id */
74cea6c86cSDoug Rabson     caddr_t		address;	/* load address */
75cea6c86cSDoug Rabson     size_t		size;		/* size of file */
76cea6c86cSDoug Rabson     int			ndeps;		/* number of dependancies */
77cea6c86cSDoug Rabson     linker_file_t*	deps;		/* list of dependancies */
78cea6c86cSDoug Rabson     STAILQ_HEAD(, common_symbol) common; /* list of common symbols */
79cea6c86cSDoug Rabson     TAILQ_HEAD(, module) modules;	/* modules in this file */
80cea6c86cSDoug Rabson };
81cea6c86cSDoug Rabson 
82cea6c86cSDoug Rabson /*
83cea6c86cSDoug Rabson  * Object implementing a class of file (a.out, elf, etc.)
84cea6c86cSDoug Rabson  */
85cea6c86cSDoug Rabson typedef struct linker_class *linker_class_t;
86cea6c86cSDoug Rabson typedef TAILQ_HEAD(, linker_class) linker_class_list_t;
87cea6c86cSDoug Rabson 
88cea6c86cSDoug Rabson struct linker_class {
89326e27d8SDoug Rabson     KOBJ_CLASS_FIELDS;
90cea6c86cSDoug Rabson     TAILQ_ENTRY(linker_class) link;	/* list of all file classes */
91cea6c86cSDoug Rabson };
92cea6c86cSDoug Rabson 
93cea6c86cSDoug Rabson /*
94cea6c86cSDoug Rabson  * The file which is currently loading.  Used to register modules with
95cea6c86cSDoug Rabson  * the files which contain them.
96cea6c86cSDoug Rabson  */
97d73424aaSBruce Evans extern linker_file_t	linker_current_file;
98cea6c86cSDoug Rabson 
99cea6c86cSDoug Rabson /*
10084e40f56SPeter Wemm  * The "file" for the kernel.
10184e40f56SPeter Wemm  */
10284e40f56SPeter Wemm extern linker_file_t	linker_kernel_file;
10384e40f56SPeter Wemm 
10484e40f56SPeter Wemm /*
105cea6c86cSDoug Rabson  * Add a new file class to the linker.
106cea6c86cSDoug Rabson  */
107326e27d8SDoug Rabson int linker_add_class(linker_class_t cls);
108cea6c86cSDoug Rabson 
109cea6c86cSDoug Rabson /*
110cea6c86cSDoug Rabson  * Load a file, trying each file class until one succeeds.
111cea6c86cSDoug Rabson  */
112cea6c86cSDoug Rabson int linker_load_file(const char* filename, linker_file_t* result);
113cea6c86cSDoug Rabson 
114cea6c86cSDoug Rabson /*
115cea6c86cSDoug Rabson  * Find a currently loaded file given its filename.
116cea6c86cSDoug Rabson  */
117cea6c86cSDoug Rabson linker_file_t linker_find_file_by_name(const char* filename);
118cea6c86cSDoug Rabson 
119cea6c86cSDoug Rabson /*
120cea6c86cSDoug Rabson  * Find a currently loaded file given its file id.
121cea6c86cSDoug Rabson  */
122cea6c86cSDoug Rabson linker_file_t linker_find_file_by_id(int fileid);
123cea6c86cSDoug Rabson 
124cea6c86cSDoug Rabson /*
125cea6c86cSDoug Rabson  * Called from a class handler when a file is laoded.
126cea6c86cSDoug Rabson  */
127326e27d8SDoug Rabson linker_file_t linker_make_file(const char* filename, linker_class_t cls);
128cea6c86cSDoug Rabson 
129cea6c86cSDoug Rabson /*
130cea6c86cSDoug Rabson  * Unload a file, freeing up memory.
131cea6c86cSDoug Rabson  */
132cea6c86cSDoug Rabson int linker_file_unload(linker_file_t file);
133cea6c86cSDoug Rabson 
134cea6c86cSDoug Rabson /*
135cea6c86cSDoug Rabson  * Add a dependancy to a file.
136cea6c86cSDoug Rabson  */
137cea6c86cSDoug Rabson int linker_file_add_dependancy(linker_file_t file, linker_file_t dep);
138cea6c86cSDoug Rabson 
139cea6c86cSDoug Rabson /*
140cea6c86cSDoug Rabson  * Lookup a symbol in a file.  If deps is TRUE, look in dependancies
141cea6c86cSDoug Rabson  * if not found in file.
142cea6c86cSDoug Rabson  */
143cea6c86cSDoug Rabson caddr_t linker_file_lookup_symbol(linker_file_t file, const char* name,
144cea6c86cSDoug Rabson 				  int deps);
145cea6c86cSDoug Rabson 
1467ec76718SMike Smith /*
14781450045SPeter Wemm  * Search the linker path for the module.  Return the full pathname in
14881450045SPeter Wemm  * a malloc'ed buffer.
14981450045SPeter Wemm  */
15081450045SPeter Wemm char *linker_search_path(const char *filename);
15181450045SPeter Wemm 
15281450045SPeter Wemm /*
15381450045SPeter Wemm  * DDB Helpers, tuned specifically for ddb/db_kld.c
15481450045SPeter Wemm  */
155fe08c21aSMatthew Dillon int linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym);
156fe08c21aSMatthew Dillon int linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp);
157fe08c21aSMatthew Dillon int linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval);
15881450045SPeter Wemm 
15981450045SPeter Wemm 
160664a31e4SPeter Wemm #endif	/* _KERNEL */
16181450045SPeter Wemm 
16281450045SPeter Wemm /*
1637ec76718SMike Smith  * Module information subtypes
1647ec76718SMike Smith  */
16581450045SPeter Wemm #define MODINFO_END		0x0000		/* End of list */
16681450045SPeter Wemm #define MODINFO_NAME		0x0001		/* Name of module (string) */
16781450045SPeter Wemm #define MODINFO_TYPE		0x0002		/* Type of module (string) */
16881450045SPeter Wemm #define MODINFO_ADDR		0x0003		/* Loaded address */
16981450045SPeter Wemm #define MODINFO_SIZE		0x0004		/* Size of module */
17081450045SPeter Wemm #define MODINFO_EMPTY		0x0005		/* Has been deleted */
171445a0357SDaniel C. Sobral #define MODINFO_ARGS		0x0006		/* Parameters string */
17281450045SPeter Wemm #define MODINFO_METADATA	0x8000		/* Module-specfic */
1737ec76718SMike Smith 
1747ec76718SMike Smith #define MODINFOMD_AOUTEXEC	0x0001		/* a.out exec header */
1757ec76718SMike Smith #define MODINFOMD_ELFHDR	0x0002		/* ELF header */
17681450045SPeter Wemm #define MODINFOMD_SSYM		0x0003		/* start of symbols */
17781450045SPeter Wemm #define MODINFOMD_ESYM		0x0004		/* end of symbols */
17881450045SPeter Wemm #define MODINFOMD_DYNAMIC	0x0005		/* _DYNAMIC pointer */
1797ec76718SMike Smith #define MODINFOMD_NOCOPY	0x8000		/* don't copy this metadata to the kernel */
1807ec76718SMike Smith 
18181450045SPeter Wemm #define MODINFOMD_DEPLIST	(0x4001 | MODINFOMD_NOCOPY)	/* depends on */
18281450045SPeter Wemm 
183664a31e4SPeter Wemm #ifdef _KERNEL
1847ec76718SMike Smith 
1851d13be33SMike Smith /*
1861d13be33SMike Smith  * Module lookup
1871d13be33SMike Smith  */
18881450045SPeter Wemm extern caddr_t		preload_metadata;
18981450045SPeter Wemm extern caddr_t		preload_search_by_name(const char *name);
19081450045SPeter Wemm extern caddr_t		preload_search_by_type(const char *type);
19181450045SPeter Wemm extern caddr_t		preload_search_next_name(caddr_t base);
19281450045SPeter Wemm extern caddr_t		preload_search_info(caddr_t mod, int inf);
19381450045SPeter Wemm extern void		preload_delete_name(const char *name);
19481450045SPeter Wemm extern void		preload_bootstrap_relocate(vm_offset_t offset);
1951d13be33SMike Smith 
196326e27d8SDoug Rabson #ifdef DDB
197326e27d8SDoug Rabson extern void		r_debug_state(void);
198326e27d8SDoug Rabson #endif
199326e27d8SDoug Rabson 
200cea6c86cSDoug Rabson #ifdef KLD_DEBUG
201cea6c86cSDoug Rabson 
202cea6c86cSDoug Rabson extern int kld_debug;
203cea6c86cSDoug Rabson #define KLD_DEBUG_FILE	1	/* file load/unload */
204cea6c86cSDoug Rabson #define KLD_DEBUG_SYM	2	/* symbol lookup */
205cea6c86cSDoug Rabson 
206cea6c86cSDoug Rabson #define KLD_DPF(cat, args)					\
207cea6c86cSDoug Rabson 	do {							\
20881450045SPeter Wemm 		if (kld_debug & KLD_DEBUG_##cat) printf args;	\
209cea6c86cSDoug Rabson 	} while (0)
210cea6c86cSDoug Rabson 
211cea6c86cSDoug Rabson #else
212cea6c86cSDoug Rabson 
213cea6c86cSDoug Rabson #define KLD_DPF(cat, args)
214cea6c86cSDoug Rabson 
215cea6c86cSDoug Rabson #endif
216cea6c86cSDoug Rabson 
21781450045SPeter Wemm /* Support functions */
218aa855a59SPeter Wemm int	elf_reloc(linker_file_t lf, const void *rel, int type, const char *sym);
219aa855a59SPeter Wemm /* values for type */
220aa855a59SPeter Wemm #define ELF_RELOC_REL	1
221aa855a59SPeter Wemm #define ELF_RELOC_RELA	2
22281450045SPeter Wemm 
223664a31e4SPeter Wemm #endif /* _KERNEL */
224cea6c86cSDoug Rabson 
225cea6c86cSDoug Rabson struct kld_file_stat {
226cea6c86cSDoug Rabson     int		version;	/* set to sizeof(linker_file_stat) */
227cea6c86cSDoug Rabson     char        name[MAXPATHLEN];
228cea6c86cSDoug Rabson     int		refs;
229cea6c86cSDoug Rabson     int		id;
230cea6c86cSDoug Rabson     caddr_t	address;	/* load address */
231cea6c86cSDoug Rabson     size_t	size;		/* size in bytes */
232cea6c86cSDoug Rabson };
233cea6c86cSDoug Rabson 
234ba031106SPeter Wemm struct kld_sym_lookup {
235ba031106SPeter Wemm     int		version;	/* set to sizeof(struct kld_sym_lookup) */
236ba031106SPeter Wemm     char	*symname;	/* Symbol name we are looking up */
237ba031106SPeter Wemm     u_long	symvalue;
238ba031106SPeter Wemm     size_t	symsize;
239ba031106SPeter Wemm };
240ba031106SPeter Wemm #define KLDSYM_LOOKUP	1
241ba031106SPeter Wemm 
242664a31e4SPeter Wemm #ifndef _KERNEL
243cea6c86cSDoug Rabson 
244cea6c86cSDoug Rabson #include <sys/cdefs.h>
245cea6c86cSDoug Rabson 
246cea6c86cSDoug Rabson __BEGIN_DECLS
247cea6c86cSDoug Rabson int	kldload(const char* file);
248cea6c86cSDoug Rabson int	kldunload(int fileid);
249cea6c86cSDoug Rabson int	kldfind(const char* file);
250cea6c86cSDoug Rabson int	kldnext(int fileid);
251cea6c86cSDoug Rabson int	kldstat(int fileid, struct kld_file_stat* stat);
252cea6c86cSDoug Rabson int	kldfirstmod(int fileid);
253ba031106SPeter Wemm int	kldsym(int _fileid, int _cmd, void *_data);
254cea6c86cSDoug Rabson __END_DECLS
255cea6c86cSDoug Rabson 
256cea6c86cSDoug Rabson #endif
257cea6c86cSDoug Rabson 
258452ed6dcSBruce Evans #endif /* !_SYS_LINKER_H_ */
259