1199767fToomas Soome/*-
2199767fToomas Soome * Copyright (c) 1997-2000 Doug Rabson
3199767fToomas Soome * All rights reserved.
4199767fToomas Soome *
5199767fToomas Soome * Redistribution and use in source and binary forms, with or without
6199767fToomas Soome * modification, are permitted provided that the following conditions
7199767fToomas Soome * are met:
8199767fToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767fToomas Soome *    notice, this list of conditions and the following disclaimer.
10199767fToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767fToomas Soome *    notice, this list of conditions and the following disclaimer in the
12199767fToomas Soome *    documentation and/or other materials provided with the distribution.
13199767fToomas Soome *
14199767fToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767fToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767fToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767fToomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767fToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767fToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767fToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767fToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767fToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767fToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767fToomas Soome * SUCH DAMAGE.
25199767fToomas Soome *
26199767fToomas Soome * $FreeBSD$
27199767fToomas Soome */
28199767fToomas Soome
29199767fToomas Soome#ifndef _SYS_LINKER_H_
30199767fToomas Soome#define _SYS_LINKER_H_
31199767fToomas Soome
32199767fToomas Soome#ifdef _KERNEL
33199767fToomas Soome
34199767fToomas Soome#include <machine/elf.h>
35199767fToomas Soome#include <sys/kobj.h>
36199767fToomas Soome
37199767fToomas Soome#ifdef MALLOC_DECLARE
38199767fToomas SoomeMALLOC_DECLARE(M_LINKER);
39199767fToomas Soome#endif
40199767fToomas Soome
41199767fToomas Soomestruct mod_depend;
42199767fToomas Soome
43199767fToomas Soome/*
44199767fToomas Soome * Object representing a file which has been loaded by the linker.
45199767fToomas Soome */
46199767fToomas Soometypedef struct linker_file* linker_file_t;
47199767fToomas Soometypedef TAILQ_HEAD(, linker_file) linker_file_list_t;
48199767fToomas Soome
49199767fToomas Soometypedef caddr_t linker_sym_t;		/* opaque symbol */
50199767fToomas Soometypedef c_caddr_t c_linker_sym_t;	/* const opaque symbol */
51199767fToomas Soometypedef int (*linker_function_name_callback_t)(const char *, void *);
52199767fToomas Soome
53199767fToomas Soome/*
54199767fToomas Soome * expanded out linker_sym_t
55199767fToomas Soome */
56199767fToomas Soometypedef struct linker_symval {
57199767fToomas Soome    const char*		name;
58199767fToomas Soome    caddr_t		value;
59199767fToomas Soome    size_t		size;
60199767fToomas Soome} linker_symval_t;
61199767fToomas Soome
62199767fToomas Soometypedef int (*linker_function_nameval_callback_t)(linker_file_t, int, linker_symval_t *, void *);
63199767fToomas Soome
64199767fToomas Soomestruct common_symbol {
65199767fToomas Soome    STAILQ_ENTRY(common_symbol) link;
66199767fToomas Soome    char*		name;
67199767fToomas Soome    caddr_t		address;
68199767fToomas Soome};
69199767fToomas Soome
70199767fToomas Soomestruct linker_file {
71199767fToomas Soome    KOBJ_FIELDS;
72199767fToomas Soome    int			refs;		/* reference count */
73199767fToomas Soome    int			userrefs;	/* kldload(2) count */
74199767fToomas Soome    int			flags;
75199767fToomas Soome#define LINKER_FILE_LINKED	0x1	/* file has been fully linked */
76199767fToomas Soome    TAILQ_ENTRY(linker_file) link;	/* list of all loaded files */
77199767fToomas Soome    char*		filename;	/* file which was loaded */
78199767fToomas Soome    char*		pathname;	/* file name with full path */
79199767fToomas Soome    int			id;		/* unique id */
80199767fToomas Soome    caddr_t		address;	/* load address */
81199767fToomas Soome    size_t		size;		/* size of file */
82199767fToomas Soome    caddr_t		ctors_addr;	/* address of .ctors */
83199767fToomas Soome    size_t		ctors_size;	/* size of .ctors */
84199767fToomas Soome    int			ndeps;		/* number of dependencies */
85199767fToomas Soome    linker_file_t*	deps;		/* list of dependencies */
86199767fToomas Soome    STAILQ_HEAD(, common_symbol) common; /* list of common symbols */
87199767fToomas Soome    TAILQ_HEAD(, module) modules;	/* modules in this file */
88199767fToomas Soome    TAILQ_ENTRY(linker_file) loaded;	/* preload dependency support */
89199767fToomas Soome    int			loadcnt;	/* load counter value */
90199767fToomas Soome
91199767fToomas Soome    /*
92199767fToomas Soome     * Function Boundary Tracing (FBT) or Statically Defined Tracing (SDT)
93199767fToomas Soome     * fields.
94199767fToomas Soome     */
95199767fToomas Soome    int			nenabled;	/* number of enabled probes. */
96199767fToomas Soome    int			fbt_nentries;	/* number of fbt entries created. */
97199767fToomas Soome};
98199767fToomas Soome
99199767fToomas Soome/*
100199767fToomas Soome * Object implementing a class of file (a.out, elf, etc.)
101199767fToomas Soome */
102199767fToomas Soometypedef struct linker_class *linker_class_t;
103199767fToomas Soometypedef TAILQ_HEAD(, linker_class) linker_class_list_t;
104199767fToomas Soome
105199767fToomas Soomestruct linker_class {
106199767fToomas Soome    KOBJ_CLASS_FIELDS;
107199767fToomas Soome    TAILQ_ENTRY(linker_class) link;	/* list of all file classes */
108199767fToomas Soome};
109199767fToomas Soome
110199767fToomas Soome/*
111199767fToomas Soome * Function type used when iterating over the list of linker files.
112199767fToomas Soome */
113199767fToomas Soometypedef int linker_predicate_t(linker_file_t, void *);
114199767fToomas Soome
115199767fToomas Soome/*
116199767fToomas Soome * The "file" for the kernel.
117199767fToomas Soome */
118199767fToomas Soomeextern linker_file_t	linker_kernel_file;
119199767fToomas Soome
120199767fToomas Soome/*
121199767fToomas Soome * Obtain a reference to a module, loading it if required.
122199767fToomas Soome */
123199767fToomas Soomeint linker_reference_module(const char* _modname, struct mod_depend *_verinfo,
124199767fToomas Soome			    linker_file_t* _result);
125199767fToomas Soome
126199767fToomas Soome/*
127199767fToomas Soome * Release a reference to a module, unloading it if there are no more
128199767fToomas Soome * references.  Note that one should either provide a module name and
129199767fToomas Soome * optional version info or a linker file, but not both.
130199767fToomas Soome */
131199767fToomas Soomeint linker_release_module(const char *_modname, struct mod_depend *_verinfo,
132199767fToomas Soome			  linker_file_t _file);
133199767fToomas Soome
134199767fToomas Soome/*
135199767fToomas Soome * Iterate over all of the currently loaded linker files calling the
136199767fToomas Soome * predicate function while the function returns 0.  Returns the value
137199767fToomas Soome * returned by the last predicate function.
138199767fToomas Soome */
139199767fToomas Soomeint linker_file_foreach(linker_predicate_t *_predicate, void *_context);
140199767fToomas Soome
141199767fToomas Soome/*
142199767fToomas Soome * Lookup a symbol in a file.  If deps is TRUE, look in dependencies
143199767fToomas Soome * if not found in file.
144199767fToomas Soome */
145199767fToomas Soomecaddr_t linker_file_lookup_symbol(linker_file_t _file, const char* _name,
146199767fToomas Soome				  int _deps);
147199767fToomas Soome
148199767fToomas Soome/*
149199767fToomas Soome * Lookup a linker set in a file.  Return pointers to the first entry,
150199767fToomas Soome * last + 1, and count of entries.  Use: for (p = start; p < stop; p++) {}
151199767fToomas Soome * void *start is really: "struct yoursetmember ***start;"
152199767fToomas Soome */
153199767fToomas Soomeint linker_file_lookup_set(linker_file_t _file, const char *_name,
154199767fToomas Soome			   void *_start, void *_stop, int *_count);
155199767fToomas Soome
156199767fToomas Soome/*
157199767fToomas Soome * List all functions in a file.
158199767fToomas Soome */
159199767fToomas Soomeint linker_file_function_listall(linker_file_t,
160199767fToomas Soome				 linker_function_nameval_callback_t, void *);
161199767fToomas Soome
162199767fToomas Soome/*
163199767fToomas Soome * Functions soley for use by the linker class handlers.
164199767fToomas Soome */
165199767fToomas Soomeint linker_add_class(linker_class_t _cls);
166199767fToomas Soomeint linker_file_unload(linker_file_t _file, int flags);
167199767fToomas Soomeint linker_load_dependencies(linker_file_t _lf);
168199767fToomas Soomelinker_file_t linker_make_file(const char* _filename, linker_class_t _cls);
169199767fToomas Soome
170199767fToomas Soome/*
171199767fToomas Soome * DDB Helpers, tuned specifically for ddb/db_kld.c
172199767fToomas Soome */
173199767fToomas Soomeint linker_ddb_lookup(const char *_symstr, c_linker_sym_t *_sym);
174199767fToomas Soomeint linker_ddb_search_symbol(caddr_t _value, c_linker_sym_t *_sym,
175199767fToomas Soome			     long *_diffp);
176199767fToomas Soomeint linker_ddb_symbol_values(c_linker_sym_t _sym, linker_symval_t *_symval);
177199767fToomas Soomeint linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
178199767fToomas Soome				  long *offset);
179199767fToomas Soome
180199767fToomas Soome/*
181199767fToomas Soome * stack(9) helper for situations where kernel locking is required.
182199767fToomas Soome */
183199767fToomas Soomeint linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
184199767fToomas Soome    long *offset);
185199767fToomas Soome
186199767fToomas Soome
187199767fToomas Soome/* HWPMC helper */
188199767fToomas Soomevoid *linker_hwpmc_list_objects(void);
189199767fToomas Soome
190199767fToomas Soome#endif	/* _KERNEL */
191199767fToomas Soome
192199767fToomas Soome/*
193199767fToomas Soome * Module information subtypes
194199767fToomas Soome */
195199767fToomas Soome#define MODINFO_END		0x0000		/* End of list */
196199767fToomas Soome#define MODINFO_NAME		0x0001		/* Name of module (string) */
197199767fToomas Soome#define MODINFO_TYPE		0x0002		/* Type of module (string) */
198199767fToomas Soome#define MODINFO_ADDR		0x0003		/* Loaded address */
199199767fToomas Soome#define MODINFO_SIZE		0x0004		/* Size of module */
200199767fToomas Soome#define MODINFO_EMPTY		0x0005		/* Has been deleted */
201199767fToomas Soome#define MODINFO_ARGS		0x0006		/* Parameters string */
202199767fToomas Soome#define MODINFO_METADATA	0x8000		/* Module-specfic */
203199767fToomas Soome
204199767fToomas Soome#define MODINFOMD_AOUTEXEC	0x0001		/* a.out exec header */
205199767fToomas Soome#define MODINFOMD_ELFHDR	0x0002		/* ELF header */
206199767fToomas Soome#define MODINFOMD_SSYM		0x0003		/* start of symbols */
207199767fToomas Soome#define MODINFOMD_ESYM		0x0004		/* end of symbols */
208199767fToomas Soome#define MODINFOMD_DYNAMIC	0x0005		/* _DYNAMIC pointer */
209199767fToomas Soome/* These values are MD on these two platforms */
210199767fToomas Soome#if !defined(__sparc64__) && !defined(__powerpc__)
211199767fToomas Soome#define MODINFOMD_ENVP		0x0006		/* envp[] */
212199767fToomas Soome#define MODINFOMD_HOWTO		0x0007		/* boothowto */
213199767fToomas Soome#define MODINFOMD_KERNEND	0x0008		/* kernend */
214199767fToomas Soome#endif
215199767fToomas Soome#define MODINFOMD_SHDR		0x0009		/* section header table */
216199767fToomas Soome#define MODINFOMD_CTORS_ADDR	0x000a		/* address of .ctors */
217199767fToomas Soome#define MODINFOMD_CTORS_SIZE	0x000b		/* size of .ctors */
218199767fToomas Soome#define MODINFOMD_FW_HANDLE	0x000c		/* Firmware dependent handle */
219199767fToomas Soome#define MODINFOMD_NOCOPY	0x8000		/* don't copy this metadata to the kernel */
220199767fToomas Soome
221199767fToomas Soome#define MODINFOMD_DEPLIST	(0x4001 | MODINFOMD_NOCOPY)	/* depends on */
222199767fToomas Soome
223199767fToomas Soome#ifdef _KERNEL
224199767fToomas Soome#define MD_FETCH(mdp, info, type) ({ \
225199767fToomas Soome	type *__p; \
226199767fToomas Soome	__p = (type *)preload_search_info((mdp), MODINFO_METADATA | (info)); \
227199767fToomas Soome	__p ? *__p : 0; \
228199767fToomas Soome})
229199767fToomas Soome#endif
230199767fToomas Soome
231199767fToomas Soome#define	LINKER_HINTS_VERSION	1		/* linker.hints file version */
232199767fToomas Soome#define	LINKER_HINTS_MAX	(1 << 20)	/* Allow at most 1MB for linker.hints */
233199767fToomas Soome
234199767fToomas Soome#ifdef _KERNEL
235199767fToomas Soome
236199767fToomas Soome/*
237199767fToomas Soome * Module lookup
238199767fToomas Soome */
239199767fToomas Soomeextern vm_offset_t	preload_addr_relocate;
240199767fToomas Soomeextern caddr_t		preload_metadata;
241199767fToomas Soome
242199767fToomas Soomeextern void *		preload_fetch_addr(caddr_t _mod);
243199767fToomas Soomeextern size_t		preload_fetch_size(caddr_t _mod);
244199767fToomas Soomeextern caddr_t		preload_search_by_name(const char *_name);
245199767fToomas Soomeextern caddr_t		preload_search_by_type(const char *_type);
246199767fToomas Soomeextern caddr_t		preload_search_next_name(caddr_t _base);
247199767fToomas Soomeextern caddr_t		preload_search_info(caddr_t _mod, int _inf);
248199767fToomas Soomeextern void		preload_delete_name(const char *_name);
249199767fToomas Soomeextern void		preload_bootstrap_relocate(vm_offset_t _offset);
250199767fToomas Soome
251199767fToomas Soome#ifdef KLD_DEBUG
252199767fToomas Soome
253199767fToomas Soomeextern int kld_debug;
254199767fToomas Soome#define KLD_DEBUG_FILE	1	/* file load/unload */
255199767fToomas Soome#define KLD_DEBUG_SYM	2	/* symbol lookup */
256199767fToomas Soome
257199767fToomas Soome#define KLD_DPF(cat, args)					\
258199767fToomas Soome	do {							\
259199767fToomas Soome		if (kld_debug & KLD_DEBUG_##cat) printf args;	\
260199767fToomas Soome	} while (0)
261199767fToomas Soome
262199767fToomas Soome#else
263199767fToomas Soome
264199767fToomas Soome#define KLD_DPF(cat, args)
265199767fToomas Soome
266199767fToomas Soome#endif
267199767fToomas Soome
268199767fToomas Soometypedef int elf_lookup_fn(linker_file_t, Elf_Size, int, Elf_Addr *);
269199767fToomas Soome
270199767fToomas Soome/* Support functions */
271199767fToomas Soomeint	elf_reloc(linker_file_t _lf, Elf_Addr base, const void *_rel, int _type, elf_lookup_fn _lu);
272199767fToomas Soomeint	elf_reloc_local(linker_file_t _lf, Elf_Addr base, const void *_rel, int _type, elf_lookup_fn _lu);
273199767fToomas SoomeElf_Addr elf_relocaddr(linker_file_t _lf, Elf_Addr addr);
274199767fToomas Soomeconst Elf_Sym *elf_get_sym(linker_file_t _lf, Elf_Size _symidx);
275199767fToomas Soomeconst char *elf_get_symname(linker_file_t _lf, Elf_Size _symidx);
276199767fToomas Soome
277199767fToomas Soometypedef struct linker_ctf {
278199767fToomas Soome	const uint8_t 	*ctftab;	/* Decompressed CTF data. */
279199767fToomas Soome	int 		ctfcnt;		/* Number of CTF data bytes. */
280199767fToomas Soome	const Elf_Sym	*symtab;	/* Ptr to the symbol table. */
281199767fToomas Soome	int		nsym;		/* Number of symbols. */
282199767fToomas Soome	const char	*strtab;	/* Ptr to the string table. */
283199767fToomas Soome	int 		strcnt;		/* Number of string bytes. */
284199767fToomas Soome	uint32_t	**ctfoffp;	/* Ptr to array of obj/fnc offsets. */
285199767fToomas Soome	uint32_t	**typoffp;	/* Ptr to array of type offsets. */
286199767fToomas Soome	long		*typlenp;	/* Ptr to number of type data entries. */
287199767fToomas Soome} linker_ctf_t;
288199767fToomas Soome
289199767fToomas Soomeint	linker_ctf_get(linker_file_t, linker_ctf_t *);
290199767fToomas Soome
291199767fToomas Soomeint elf_cpu_load_file(linker_file_t);
292199767fToomas Soomeint elf_cpu_unload_file(linker_file_t);
293199767fToomas Soome
294199767fToomas Soome/* values for type */
295199767fToomas Soome#define ELF_RELOC_REL	1
296199767fToomas Soome#define ELF_RELOC_RELA	2
297199767fToomas Soome
298199767fToomas Soome/*
299199767fToomas Soome * This is version 1 of the KLD file status structure. It is identified
300199767fToomas Soome * by its _size_ in the version field.
301199767fToomas Soome */
302199767fToomas Soomestruct kld_file_stat_1 {
303199767fToomas Soome    int		version;	/* set to sizeof(struct kld_file_stat_1) */
304199767fToomas Soome    char        name[MAXPATHLEN];
305199767fToomas Soome    int		refs;
306199767fToomas Soome    int		id;
307199767fToomas Soome    caddr_t	address;	/* load address */
308199767fToomas Soome    size_t	size;		/* size in bytes */
309199767fToomas Soome};
310199767fToomas Soome#endif /* _KERNEL */
311199767fToomas Soome
312199767fToomas Soomestruct kld_file_stat {
313199767fToomas Soome    int		version;	/* set to sizeof(struct kld_file_stat) */
314199767fToomas Soome    char        name[MAXPATHLEN];
315199767fToomas Soome    int		refs;
316199767fToomas Soome    int		id;
317199767fToomas Soome    caddr_t	address;	/* load address */
318199767fToomas Soome    size_t	size;		/* size in bytes */
319199767fToomas Soome    char        pathname[MAXPATHLEN];
320199767fToomas Soome};
321199767fToomas Soome
322199767fToomas Soomestruct kld_sym_lookup {
323199767fToomas Soome    int		version;	/* set to sizeof(struct kld_sym_lookup) */
324199767fToomas Soome    char	*symname;	/* Symbol name we are looking up */
325199767fToomas Soome    u_long	symvalue;
326199767fToomas Soome    size_t	symsize;
327199767fToomas Soome};
328199767fToomas Soome#define KLDSYM_LOOKUP	1
329199767fToomas Soome
330199767fToomas Soome/*
331199767fToomas Soome * Flags for kldunloadf() and linker_file_unload()
332199767fToomas Soome */
333199767fToomas Soome#define LINKER_UNLOAD_NORMAL	0
334199767fToomas Soome#define LINKER_UNLOAD_FORCE	1
335199767fToomas Soome
336199767fToomas Soome#ifndef _KERNEL
337199767fToomas Soome
338199767fToomas Soome#include <sys/cdefs.h>
339199767fToomas Soome
340199767fToomas Soome__BEGIN_DECLS
341199767fToomas Soomeint	kldload(const char* _file);
342199767fToomas Soomeint	kldunload(int _fileid);
343199767fToomas Soomeint	kldunloadf(int _fileid, int flags);
344199767fToomas Soomeint	kldfind(const char* _file);
345199767fToomas Soomeint	kldnext(int _fileid);
346199767fToomas Soomeint	kldstat(int _fileid, struct kld_file_stat* _stat);
347199767fToomas Soomeint	kldfirstmod(int _fileid);
348199767fToomas Soomeint	kldsym(int _fileid, int _cmd, void *_data);
349199767fToomas Soome__END_DECLS
350199767fToomas Soome
351199767fToomas Soome#endif
352199767fToomas Soome
353199767fToomas Soome#endif /* !_SYS_LINKER_H_ */
354