xref: /freebsd/sys/sys/linker.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1997-2000 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_LINKER_H_
30 #define _SYS_LINKER_H_
31 
32 #ifdef _KERNEL
33 
34 #include <machine/elf.h>
35 #include <sys/kobj.h>
36 
37 #ifdef MALLOC_DECLARE
38 MALLOC_DECLARE(M_LINKER);
39 #endif
40 
41 struct mod_depend;
42 
43 /*
44  * Object representing a file which has been loaded by the linker.
45  */
46 typedef struct linker_file* linker_file_t;
47 typedef TAILQ_HEAD(, linker_file) linker_file_list_t;
48 
49 typedef caddr_t linker_sym_t;		/* opaque symbol */
50 typedef c_caddr_t c_linker_sym_t;	/* const opaque symbol */
51 typedef int (*linker_function_name_callback_t)(const char *, void *);
52 
53 /*
54  * expanded out linker_sym_t
55  */
56 typedef struct linker_symval {
57     const char*		name;
58     caddr_t		value;
59     size_t		size;
60 } linker_symval_t;
61 
62 typedef int (*linker_function_nameval_callback_t)(linker_file_t, int, linker_symval_t *, void *);
63 
64 struct common_symbol {
65     STAILQ_ENTRY(common_symbol) link;
66     char*		name;
67     caddr_t		address;
68 };
69 
70 struct linker_file {
71     KOBJ_FIELDS;
72     int			refs;		/* reference count */
73     int			userrefs;	/* kldload(2) count */
74     int			flags;
75 #define LINKER_FILE_LINKED	0x1	/* file has been fully linked */
76 #define LINKER_FILE_MODULES	0x2	/* file has >0 modules at preload */
77     TAILQ_ENTRY(linker_file) link;	/* list of all loaded files */
78     char*		filename;	/* file which was loaded */
79     char*		pathname;	/* file name with full path */
80     int			id;		/* unique id */
81     caddr_t		address;	/* load address */
82     size_t		size;		/* size of file */
83     caddr_t		ctors_addr;	/* address of .ctors/.init_array */
84     size_t		ctors_size;	/* size of .ctors/.init_array */
85     enum {
86 	    LF_NONE = 0,
87 	    LF_CTORS,
88 	    LF_DTORS,
89     } ctors_invoked;			/* have we run ctors yet? */
90     caddr_t		dtors_addr;	/* address of .dtors/.fini_array */
91     size_t		dtors_size;	/* size of .dtors/.fini_array */
92     int			ndeps;		/* number of dependencies */
93     linker_file_t*	deps;		/* list of dependencies */
94     STAILQ_HEAD(, common_symbol) common; /* list of common symbols */
95     TAILQ_HEAD(, module) modules;	/* modules in this file */
96     TAILQ_ENTRY(linker_file) loaded;	/* preload dependency support */
97     int			loadcnt;	/* load counter value */
98 
99     /*
100      * Function Boundary Tracing (FBT) or Statically Defined Tracing (SDT)
101      * fields.
102      */
103     int			nenabled;	/* number of enabled probes. */
104     int			fbt_nentries;	/* number of fbt entries created. */
105 
106 #ifdef __arm__
107     caddr_t		exidx_addr;	/* Unwind data index table start */
108     size_t		exidx_size;	/* Unwind data index table size */
109 #endif
110 };
111 
112 /*
113  * Object implementing a class of file (a.out, elf, etc.)
114  */
115 typedef struct linker_class *linker_class_t;
116 typedef TAILQ_HEAD(, linker_class) linker_class_list_t;
117 
118 struct linker_class {
119     KOBJ_CLASS_FIELDS;
120     TAILQ_ENTRY(linker_class) link;	/* list of all file classes */
121 };
122 
123 /*
124  * Function type used when iterating over the list of linker files.
125  */
126 typedef int linker_predicate_t(linker_file_t, void *);
127 
128 /*
129  * The "file" for the kernel.
130  */
131 extern linker_file_t	linker_kernel_file;
132 
133 /*
134  * Obtain a reference to a module, loading it if required.
135  */
136 int linker_reference_module(const char* _modname, struct mod_depend *_verinfo,
137 			    linker_file_t* _result);
138 
139 /*
140  * Release a reference to a module, unloading it if there are no more
141  * references.  Note that one should either provide a module name and
142  * optional version info or a linker file, but not both.
143  */
144 int linker_release_module(const char *_modname, struct mod_depend *_verinfo,
145 			  linker_file_t _file);
146 
147 /*
148  * Iterate over all of the currently loaded linker files calling the
149  * predicate function while the function returns 0.  Returns the value
150  * returned by the last predicate function.
151  */
152 int linker_file_foreach(linker_predicate_t *_predicate, void *_context);
153 
154 /*
155  * Lookup a symbol in a file.  If deps is TRUE, look in dependencies
156  * if not found in file.
157  */
158 caddr_t linker_file_lookup_symbol(linker_file_t _file, const char* _name,
159 				  int _deps);
160 
161 /*
162  * Lookup a linker set in a file.  Return pointers to the first entry,
163  * last + 1, and count of entries.  Use: for (p = start; p < stop; p++) {}
164  * void *start is really: "struct yoursetmember ***start;"
165  */
166 int linker_file_lookup_set(linker_file_t _file, const char *_name,
167 			   void *_start, void *_stop, int *_count);
168 
169 /*
170  * List all functions in a file.
171  */
172 int linker_file_function_listall(linker_file_t,
173 				 linker_function_nameval_callback_t, void *);
174 
175 /*
176  * Functions solely for use by the linker class handlers.
177  */
178 int linker_add_class(linker_class_t _cls);
179 int linker_file_unload(linker_file_t _file, int flags);
180 int linker_load_dependencies(linker_file_t _lf);
181 linker_file_t linker_make_file(const char* _filename, linker_class_t _cls);
182 
183 /*
184  * DDB Helpers, tuned specifically for ddb/db_kld.c
185  */
186 int linker_ddb_lookup(const char *_symstr, c_linker_sym_t *_sym);
187 int linker_ddb_search_symbol(caddr_t _value, c_linker_sym_t *_sym,
188 			     long *_diffp);
189 int linker_ddb_symbol_values(c_linker_sym_t _sym, linker_symval_t *_symval);
190 int linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen,
191 				  long *offset);
192 
193 /*
194  * stack(9) helper for situations where kernel locking is required.
195  */
196 int linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
197     long *offset, int flags);
198 int linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
199     long *offset);
200 
201 /* HWPMC helper */
202 void *linker_hwpmc_list_objects(void);
203 
204 /* kldload/kldunload syscalls blocking */
205 #define	LINKER_UB_UNLOCK	0x0001	/* busy: unlock kld_sx locked on
206 					   return */
207 #define	LINKER_UB_LOCKED	0x0002	/* busy/unbusy: kld_sx locked on
208 					   entry */
209 #define	LINKER_UB_PCATCH	0x0004	/* busy: sleep interruptible */
210 int linker_kldload_busy(int flags);
211 void linker_kldload_unbusy(int flags);
212 
213 #endif	/* _KERNEL */
214 
215 /*
216  * Module information subtypes
217  */
218 #define MODINFO_END		0x0000		/* End of list */
219 #define MODINFO_NAME		0x0001		/* Name of module (string) */
220 #define MODINFO_TYPE		0x0002		/* Type of module (string) */
221 #define MODINFO_ADDR		0x0003		/* Loaded address */
222 #define MODINFO_SIZE		0x0004		/* Size of module */
223 #define MODINFO_EMPTY		0x0005		/* Has been deleted */
224 #define MODINFO_ARGS		0x0006		/* Parameters string */
225 #define MODINFO_METADATA	0x8000		/* Module-specfic */
226 
227 #define MODINFOMD_AOUTEXEC	0x0001		/* a.out exec header */
228 #define MODINFOMD_ELFHDR	0x0002		/* ELF header */
229 #define MODINFOMD_SSYM		0x0003		/* start of symbols */
230 #define MODINFOMD_ESYM		0x0004		/* end of symbols */
231 #define MODINFOMD_DYNAMIC	0x0005		/* _DYNAMIC pointer */
232 #define MODINFOMD_MB2HDR	0x0006		/* MB2 header info */
233 /* These values are MD on PowerPC */
234 #if !defined(__powerpc__)
235 #define MODINFOMD_ENVP		0x0006		/* envp[] */
236 #define MODINFOMD_HOWTO		0x0007		/* boothowto */
237 #define MODINFOMD_KERNEND	0x0008		/* kernend */
238 #endif
239 #define MODINFOMD_SHDR		0x0009		/* section header table */
240 #define MODINFOMD_CTORS_ADDR	0x000a		/* address of .ctors */
241 #define MODINFOMD_CTORS_SIZE	0x000b		/* size of .ctors */
242 #define MODINFOMD_FW_HANDLE	0x000c		/* Firmware dependent handle */
243 #define MODINFOMD_KEYBUF	0x000d		/* Crypto key intake buffer */
244 #define MODINFOMD_FONT		0x000e		/* Console font */
245 #define MODINFOMD_NOCOPY	0x8000		/* don't copy this metadata to the kernel */
246 
247 #define MODINFOMD_DEPLIST	(0x4001 | MODINFOMD_NOCOPY)	/* depends on */
248 
249 #ifdef _KERNEL
250 #define MD_FETCH(mdp, info, type) ({ \
251 	type *__p; \
252 	__p = (type *)preload_search_info((mdp), MODINFO_METADATA | (info)); \
253 	__p ? *__p : 0; \
254 })
255 #endif
256 
257 #define	LINKER_HINTS_VERSION	1		/* linker.hints file version */
258 #define	LINKER_HINTS_MAX	(1 << 20)	/* Allow at most 1MB for linker.hints */
259 
260 #ifdef _KERNEL
261 
262 /*
263  * Module lookup
264  */
265 extern vm_offset_t	preload_addr_relocate;
266 extern caddr_t		preload_metadata;
267 
268 extern void *		preload_fetch_addr(caddr_t _mod);
269 extern size_t		preload_fetch_size(caddr_t _mod);
270 extern caddr_t		preload_search_by_name(const char *_name);
271 extern caddr_t		preload_search_by_type(const char *_type);
272 extern caddr_t		preload_search_next_name(caddr_t _base);
273 extern caddr_t		preload_search_info(caddr_t _mod, int _inf);
274 extern void		preload_delete_name(const char *_name);
275 extern void		preload_bootstrap_relocate(vm_offset_t _offset);
276 extern void		preload_dump(void);
277 
278 #ifdef KLD_DEBUG
279 
280 extern int kld_debug;
281 #define KLD_DEBUG_FILE	1	/* file load/unload */
282 #define KLD_DEBUG_SYM	2	/* symbol lookup */
283 
284 #define KLD_DPF(cat, args)					\
285 	do {							\
286 		if (kld_debug & KLD_DEBUG_##cat) printf args;	\
287 	} while (0)
288 
289 #else
290 
291 #define KLD_DPF(cat, args)
292 
293 #endif
294 
295 typedef int elf_lookup_fn(linker_file_t, Elf_Size, int, Elf_Addr *);
296 
297 /* Support functions */
298 bool	elf_is_ifunc_reloc(Elf_Size r_info);
299 int	elf_reloc(linker_file_t _lf, Elf_Addr base, const void *_rel,
300 	    int _type, elf_lookup_fn _lu);
301 int	elf_reloc_local(linker_file_t _lf, Elf_Addr base, const void *_rel,
302 	    int _type, elf_lookup_fn _lu);
303 Elf_Addr elf_relocaddr(linker_file_t _lf, Elf_Addr addr);
304 const Elf_Sym *elf_get_sym(linker_file_t _lf, Elf_Size _symidx);
305 const char *elf_get_symname(linker_file_t _lf, Elf_Size _symidx);
306 void	link_elf_ireloc(caddr_t kmdp);
307 
308 #if defined(__aarch64__) || defined(__amd64__)
309 int	elf_reloc_late(linker_file_t _lf, Elf_Addr base, const void *_rel,
310 	    int _type, elf_lookup_fn _lu);
311 void	link_elf_late_ireloc(void);
312 #endif
313 
314 typedef struct linker_ctf {
315 	const uint8_t 	*ctftab;	/* Decompressed CTF data. */
316 	int 		ctfcnt;		/* Number of CTF data bytes. */
317 	const Elf_Sym	*symtab;	/* Ptr to the symbol table. */
318 	int		nsym;		/* Number of symbols. */
319 	const char	*strtab;	/* Ptr to the string table. */
320 	int 		strcnt;		/* Number of string bytes. */
321 	uint32_t	**ctfoffp;	/* Ptr to array of obj/fnc offsets. */
322 	uint32_t	**typoffp;	/* Ptr to array of type offsets. */
323 	long		*typlenp;	/* Ptr to number of type data entries. */
324 } linker_ctf_t;
325 
326 int	linker_ctf_get(linker_file_t, linker_ctf_t *);
327 int linker_ctf_lookup_sym_ddb(const char *symname, c_linker_sym_t *sym,
328     linker_ctf_t *lc);
329 int linker_ctf_lookup_typename_ddb(linker_ctf_t *lc, const char *typename);
330 
331 int elf_cpu_load_file(linker_file_t);
332 int elf_cpu_unload_file(linker_file_t);
333 int elf_cpu_parse_dynamic(caddr_t, Elf_Dyn *);
334 
335 /* values for type */
336 #define ELF_RELOC_REL	1
337 #define ELF_RELOC_RELA	2
338 
339 /*
340  * This is version 1 of the KLD file status structure. It is identified
341  * by its _size_ in the version field.
342  */
343 struct kld_file_stat_1 {
344     int		version;	/* set to sizeof(struct kld_file_stat_1) */
345     char        name[MAXPATHLEN];
346     int		refs;
347     int		id;
348     caddr_t	address;	/* load address */
349     size_t	size;		/* size in bytes */
350 };
351 #endif /* _KERNEL */
352 
353 struct kld_file_stat {
354     int		version;	/* set to sizeof(struct kld_file_stat) */
355     char        name[MAXPATHLEN];
356     int		refs;
357     int		id;
358     caddr_t	address;	/* load address */
359     size_t	size;		/* size in bytes */
360     char        pathname[MAXPATHLEN];
361 };
362 
363 struct kld_sym_lookup {
364     int		version;	/* set to sizeof(struct kld_sym_lookup) */
365     char	*symname;	/* Symbol name we are looking up */
366     u_long	symvalue;
367     size_t	symsize;
368 };
369 #define KLDSYM_LOOKUP	1
370 
371 /*
372  * Flags for kldunloadf() and linker_file_unload()
373  */
374 #define LINKER_UNLOAD_NORMAL	0
375 #define LINKER_UNLOAD_FORCE	1
376 
377 #ifndef _KERNEL
378 
379 #include <sys/cdefs.h>
380 
381 __BEGIN_DECLS
382 int	kldload(const char* _file);
383 int	kldunload(int _fileid);
384 int	kldunloadf(int _fileid, int flags);
385 int	kldfind(const char* _file);
386 int	kldnext(int _fileid);
387 int	kldstat(int _fileid, struct kld_file_stat* _stat);
388 int	kldfirstmod(int _fileid);
389 int	kldsym(int _fileid, int _cmd, void *_data);
390 __END_DECLS
391 
392 #endif
393 
394 #endif /* !_SYS_LINKER_H_ */
395