1 /*	$NetBSD: ksyms.h,v 1.33 2015/09/06 06:01:02 dholland 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 #ifdef _KSYMS_PRIVATE
34 #include <sys/exec_elf.h>
35 #include <sys/ioccom.h>
36 #include <sys/queue.h>
37 
38 struct ksyms_symtab {
39 	TAILQ_ENTRY(ksyms_symtab) sd_queue; /* All active tables */
40 	const char *sd_name;	/* Name of this table */
41 	Elf_Sym *sd_symstart;	/* Address of symbol table */
42 	uintptr_t sd_minsym;	/* symbol with minimum value */
43 	uintptr_t sd_maxsym;	/* symbol with maximum value */
44 	char *sd_strstart;	/* Address of corresponding string table */
45 	int sd_usroffset;	/* Real address for userspace */
46 	int sd_symsize;		/* Size in bytes of symbol table */
47 	int sd_strsize;		/* Size of string table */
48 	int sd_nglob;		/* Number of global symbols */
49 	bool sd_gone;		/* dead but around for open() */
50 	void *sd_ctfstart;	/* Address of CTF contents */
51 	int sd_ctfsize;		/* Size in bytes of CTF contents */
52 	uint32_t *sd_nmap;	/* Name map for sorted symbols */
53 	int sd_nmapsize;	/* Total span of map */
54 };
55 
56 /*
57  * Static allocated ELF header.
58  * Basic info is filled in at attach, sizes at open.
59  */
60 #define	SHNOTE		1
61 #define	SYMTAB		2
62 #define	STRTAB		3
63 #define	SHSTRTAB	4
64 #define	SHBSS		5
65 #define	SHCTF		6
66 #define	NSECHDR		7
67 
68 #define	NPRGHDR		1
69 #define	SHSTRSIZ	64
70 
71 struct ksyms_hdr {
72 	Elf_Ehdr	kh_ehdr;
73 	Elf_Phdr	kh_phdr[NPRGHDR];
74 	Elf_Shdr	kh_shdr[NSECHDR];
75 	char 		kh_strtab[SHSTRSIZ];
76 	/* 0=NameSize, 1=DescSize, 2=Tag, 3="NetB", 4="SD\0\0", 5=Version */
77 	int32_t		kh_note[6];
78 };
79 #endif	/* _KSYMS_PRIVATE */
80 
81 /*
82  * Do a lookup of a symbol using the in-kernel lookup algorithm.
83  */
84 struct ksyms_ogsymbol {
85 	const char *kg_name;
86 	union {
87 		void *ku_sym;		 /* Normally Elf_Sym */
88 		unsigned long *ku_value;
89 	} _un;
90 #define	kg_sym _un.ku_sym
91 #define	kg_value _un.ku_value
92 };
93 
94 #ifdef ELFSIZE
95 struct ksyms_gsymbol {
96 	const char *kg_name;
97 	union {
98 		Elf_Sym ku_sym;
99 	} _un;
100 };
101 #endif
102 
103 struct ksyms_gvalue {
104 	const char *kv_name;
105 	uint64_t kv_value;
106 };
107 
108 #define	OKIOCGSYMBOL	_IOW('l', 1, struct ksyms_ogsymbol)
109 #define	OKIOCGVALUE	_IOW('l', 2, struct ksyms_ogsymbol)
110 #define	KIOCGSIZE	_IOR('l', 3, int)
111 #define	KIOCGVALUE	_IOWR('l', 4, struct ksyms_gvalue)
112 #define	KIOCGSYMBOL	_IOWR('l', 5, struct ksyms_gsymbol)
113 
114 
115 #if defined(_KERNEL) || defined(_KMEMUSER)
116 /*
117  * Definitions used in ksyms_getname() and ksyms_getval().
118  */
119 #define	KSYMS_CLOSEST	0001	/* Nearest lower match */
120 #define	KSYMS_EXACT	0002	/* Only exact match allowed */
121 #define KSYMS_EXTERN	0000	/* Only external symbols (pseudo) */
122 #define KSYMS_PROC	0100	/* Procedures only */
123 #define KSYMS_ANY	0200	/* Also local symbols (DDB use only) */
124 
125 typedef int (*ksyms_callback_t)(const char *, int, void *,
126 	uint32_t, int, void *);
127 
128 /*
129  * Prototypes
130  */
131 
132 int ksyms_getname(const char **, const char **, vaddr_t, int);
133 int ksyms_getval(const char *, const char *, unsigned long *, int);
134 int ksyms_getval_unlocked(const char *, const char *, unsigned long *, int);
135 struct ksyms_symtab *ksyms_get_mod(const char *);
136 int ksyms_mod_foreach(const char *mod, ksyms_callback_t, void *);
137 int ksyms_addsymtab(const char *, void *, vsize_t, char *, vsize_t);
138 int ksyms_delsymtab(const char *);
139 void ksyms_init(void);
140 void ksyms_addsyms_elf(int, void *, void *);
141 void ksyms_addsyms_explicit(void *, void *, size_t, void *, size_t);
142 int ksyms_sift(char *, char *, int);
143 void ksyms_modload(const char *, void *, vsize_t, char *, vsize_t);
144 void ksyms_modunload(const char *);
145 
146 #endif /* _KERNEL */
147 #endif /* _SYS_KSYMS_H_ */
148