xref: /netbsd/sys/sys/ksyms.h (revision cfaa3dc0)
1 /*	$NetBSD: ksyms.h,v 1.42 2021/09/11 10:09:55 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2003 Anders Magnusson (ragge@ludd.luth.se).
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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _SYS_KSYMS_H_
31 #define _SYS_KSYMS_H_
32 
33 #if !defined(ELFSIZE) && !defined(_RUMPKERNEL)
34 #define ELFSIZE KERN_ELFSIZE
35 #endif
36 #include <sys/exec_elf.h>
37 
38 #ifdef _KSYMS_PRIVATE
39 #include <sys/ioccom.h>
40 #include <sys/queue.h>
41 
42 #ifdef _KERNEL
43 #include <sys/pslist.h>
44 #endif
45 
46 struct ksyms_symtab {
47 	TAILQ_ENTRY(ksyms_symtab) sd_queue; /* All active tables */
48 	const char *sd_name;	/* Name of this table */
49 	Elf_Sym *sd_symstart;	/* Address of symbol table */
50 	uintptr_t sd_minsym;	/* symbol with minimum value */
51 	uintptr_t sd_maxsym;	/* symbol with maximum value */
52 	char *sd_strstart;	/* Address of corresponding string table */
53 	int sd_usroffset;	/* Real address for userspace */
54 	int sd_symsize;		/* Size in bytes of symbol table */
55 	int sd_strsize;		/* Size of string table */
56 	int sd_nglob;		/* Number of global symbols */
57 	bool sd_gone;		/* dead but around for open() */
58 	void *sd_ctfstart;	/* Address of CTF contents */
59 	int sd_ctfsize;		/* Size in bytes of CTF contents */
60 	uint32_t *sd_nmap;	/* Name map for sorted symbols */
61 	int sd_nmapsize;	/* Total span of map */
62 
63 #ifdef _KERNEL
64 	struct pslist_entry sd_pslist;
65 #endif
66 };
67 
68 /*
69  * Static allocated ELF header.
70  * Basic info is filled in at attach, sizes at open.
71  */
72 #define	SHNOTE		1
73 #define	SYMTAB		2
74 #define	STRTAB		3
75 #define	SHSTRTAB	4
76 #define	SHBSS		5
77 #define	SHCTF		6
78 #define	NSECHDR		7
79 
80 #define	NPRGHDR		1
81 #define	SHSTRSIZ	64
82 
83 struct ksyms_hdr {
84 	Elf_Ehdr	kh_ehdr;
85 	Elf_Phdr	kh_phdr[NPRGHDR];
86 	Elf_Shdr	kh_shdr[NSECHDR];
87 	char 		kh_strtab[SHSTRSIZ];
88 	/* 0=NameSize, 1=DescSize, 2=Tag, 3="NetB", 4="SD\0\0", 5=Version */
89 	int32_t		kh_note[6];
90 };
91 #endif	/* _KSYMS_PRIVATE */
92 
93 /*
94  * Do a lookup of a symbol using the in-kernel lookup algorithm.
95  */
96 struct ksyms_ogsymbol {
97 	const char *kg_name;
98 	union {
99 		void *ku_sym;		 /* Normally Elf_Sym */
100 		unsigned long *ku_value;
101 	} _un;
102 #define	kg_sym _un.ku_sym
103 #define	kg_value _un.ku_value
104 };
105 
106 #ifdef ELFSIZE
107 struct ksyms_gsymbol {
108 	const char *kg_name;
109 	union {
110 		Elf_Sym ku_sym;
111 	} _un;
112 };
113 #endif
114 
115 struct ksyms_gvalue {
116 	const char *kv_name;
117 	uint64_t kv_value;
118 };
119 
120 #define	OKIOCGSYMBOL	_IOW('l', 1, struct ksyms_ogsymbol)
121 #define	OKIOCGVALUE	_IOW('l', 2, struct ksyms_ogsymbol)
122 #define	KIOCGSIZE	_IOR('l', 3, int)
123 #define	KIOCGVALUE	_IOWR('l', 4, struct ksyms_gvalue)
124 #define	KIOCGSYMBOL	_IOWR('l', 5, struct ksyms_gsymbol)
125 
126 
127 #if defined(_KERNEL) || defined(_KMEMUSER)
128 /*
129  * Definitions used in ksyms_getname() and ksyms_getval().
130  */
131 #define	KSYMS_CLOSEST	0001	/* Nearest lower match */
132 #define	KSYMS_EXACT	0002	/* Only exact match allowed */
133 #define KSYMS_EXTERN	0000	/* Only external symbols (pseudo) */
134 #define KSYMS_PROC	0100	/* Procedures only */
135 #define KSYMS_ANY	0200	/* Also local symbols (DDB use only) */
136 
137 #if defined(_KERNEL)
138 
139 typedef int (*ksyms_callback_t)(const char *, int, void *,
140 	uint32_t, int, void *);
141 
142 /*
143  * Prototypes
144  */
145 
146 int ksyms_getname(const char **, const char **, vaddr_t, int);
147 int ksyms_getval(const char *, const char *, unsigned long *, int);
148 int ksyms_getval_unlocked(const char *, const char *, Elf_Sym **,
149     unsigned long *, int);
150 struct ksyms_symtab *ksyms_get_mod(const char *);
151 int ksyms_mod_foreach(const char *mod, ksyms_callback_t, void *);
152 int ksyms_addsymtab(const char *, void *, vsize_t, char *, vsize_t);
153 int ksyms_delsymtab(const char *);
154 void ksyms_init(void);
155 void ksyms_addsyms_elf(int, void *, void *);
156 void ksyms_addsyms_explicit(void *, void *, size_t, void *, size_t);
157 bool ksyms_available(void);
158 int ksyms_sift(char *, char *, int);
159 void ksyms_modload(const char *, void *, vsize_t, char *, vsize_t);
160 void ksyms_modunload(const char *);
161 
162 #endif /* _KERNEL */
163 #endif /* _KERNEL || _KMEMUSER */
164 #endif /* _SYS_KSYMS_H_ */
165