1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Rewritten and vastly simplified by Rusty Russell for in-kernel
3  * module loader:
4  *   Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5  */
6 #ifndef _LINUX_KALLSYMS_H
7 #define _LINUX_KALLSYMS_H
8 
9 #include <linux/errno.h>
10 #include <linux/buildid.h>
11 #include <linux/kernel.h>
12 #include <linux/stddef.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 
16 #include <asm/sections.h>
17 
18 #define KSYM_NAME_LEN 128
19 #define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + \
20 			(KSYM_NAME_LEN - 1) + \
21 			2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \
22 			(BUILD_ID_SIZE_MAX * 2) + 1)
23 
24 struct cred;
25 struct module;
26 
is_kernel_inittext(unsigned long addr)27 static inline int is_kernel_inittext(unsigned long addr)
28 {
29 	if (addr >= (unsigned long)_sinittext
30 	    && addr <= (unsigned long)_einittext)
31 		return 1;
32 	return 0;
33 }
34 
is_kernel_text(unsigned long addr)35 static inline int is_kernel_text(unsigned long addr)
36 {
37 	if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
38 	    arch_is_kernel_text(addr))
39 		return 1;
40 	return in_gate_area_no_mm(addr);
41 }
42 
is_kernel(unsigned long addr)43 static inline int is_kernel(unsigned long addr)
44 {
45 	if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
46 		return 1;
47 	return in_gate_area_no_mm(addr);
48 }
49 
is_ksym_addr(unsigned long addr)50 static inline int is_ksym_addr(unsigned long addr)
51 {
52 	if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
53 		return is_kernel(addr);
54 
55 	return is_kernel_text(addr) || is_kernel_inittext(addr);
56 }
57 
dereference_symbol_descriptor(void * ptr)58 static inline void *dereference_symbol_descriptor(void *ptr)
59 {
60 #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
61 	struct module *mod;
62 
63 	ptr = dereference_kernel_function_descriptor(ptr);
64 	if (is_ksym_addr((unsigned long)ptr))
65 		return ptr;
66 
67 	preempt_disable();
68 	mod = __module_address((unsigned long)ptr);
69 	preempt_enable();
70 
71 	if (mod)
72 		ptr = dereference_module_function_descriptor(mod, ptr);
73 #endif
74 	return ptr;
75 }
76 
77 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
78 				      unsigned long),
79 			    void *data);
80 
81 #ifdef CONFIG_KALLSYMS
82 /* Lookup the address for a symbol. Returns 0 if not found. */
83 unsigned long kallsyms_lookup_name(const char *name);
84 
85 extern int kallsyms_lookup_size_offset(unsigned long addr,
86 				  unsigned long *symbolsize,
87 				  unsigned long *offset);
88 
89 /* Lookup an address.  modname is set to NULL if it's in the kernel. */
90 const char *kallsyms_lookup(unsigned long addr,
91 			    unsigned long *symbolsize,
92 			    unsigned long *offset,
93 			    char **modname, char *namebuf);
94 
95 /* Look up a kernel symbol and return it in a text buffer. */
96 extern int sprint_symbol(char *buffer, unsigned long address);
97 extern int sprint_symbol_build_id(char *buffer, unsigned long address);
98 extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
99 extern int sprint_backtrace(char *buffer, unsigned long address);
100 extern int sprint_backtrace_build_id(char *buffer, unsigned long address);
101 
102 int lookup_symbol_name(unsigned long addr, char *symname);
103 int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
104 
105 /* How and when do we show kallsyms values? */
106 extern bool kallsyms_show_value(const struct cred *cred);
107 
108 #else /* !CONFIG_KALLSYMS */
109 
kallsyms_lookup_name(const char * name)110 static inline unsigned long kallsyms_lookup_name(const char *name)
111 {
112 	return 0;
113 }
114 
kallsyms_lookup_size_offset(unsigned long addr,unsigned long * symbolsize,unsigned long * offset)115 static inline int kallsyms_lookup_size_offset(unsigned long addr,
116 					      unsigned long *symbolsize,
117 					      unsigned long *offset)
118 {
119 	return 0;
120 }
121 
kallsyms_lookup(unsigned long addr,unsigned long * symbolsize,unsigned long * offset,char ** modname,char * namebuf)122 static inline const char *kallsyms_lookup(unsigned long addr,
123 					  unsigned long *symbolsize,
124 					  unsigned long *offset,
125 					  char **modname, char *namebuf)
126 {
127 	return NULL;
128 }
129 
sprint_symbol(char * buffer,unsigned long addr)130 static inline int sprint_symbol(char *buffer, unsigned long addr)
131 {
132 	*buffer = '\0';
133 	return 0;
134 }
135 
sprint_symbol_build_id(char * buffer,unsigned long address)136 static inline int sprint_symbol_build_id(char *buffer, unsigned long address)
137 {
138 	*buffer = '\0';
139 	return 0;
140 }
141 
sprint_symbol_no_offset(char * buffer,unsigned long addr)142 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
143 {
144 	*buffer = '\0';
145 	return 0;
146 }
147 
sprint_backtrace(char * buffer,unsigned long addr)148 static inline int sprint_backtrace(char *buffer, unsigned long addr)
149 {
150 	*buffer = '\0';
151 	return 0;
152 }
153 
sprint_backtrace_build_id(char * buffer,unsigned long addr)154 static inline int sprint_backtrace_build_id(char *buffer, unsigned long addr)
155 {
156 	*buffer = '\0';
157 	return 0;
158 }
159 
lookup_symbol_name(unsigned long addr,char * symname)160 static inline int lookup_symbol_name(unsigned long addr, char *symname)
161 {
162 	return -ERANGE;
163 }
164 
lookup_symbol_attrs(unsigned long addr,unsigned long * size,unsigned long * offset,char * modname,char * name)165 static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
166 {
167 	return -ERANGE;
168 }
169 
kallsyms_show_value(const struct cred * cred)170 static inline bool kallsyms_show_value(const struct cred *cred)
171 {
172 	return false;
173 }
174 
175 #endif /*CONFIG_KALLSYMS*/
176 
print_ip_sym(const char * loglvl,unsigned long ip)177 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
178 {
179 	printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
180 }
181 
182 #endif /*_LINUX_KALLSYMS_H*/
183