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_text(unsigned long addr)27 static inline int is_kernel_text(unsigned long addr)
28 {
29 	if (__is_kernel_text(addr))
30 		return 1;
31 	return in_gate_area_no_mm(addr);
32 }
33 
is_kernel(unsigned long addr)34 static inline int is_kernel(unsigned long addr)
35 {
36 	if (__is_kernel(addr))
37 		return 1;
38 	return in_gate_area_no_mm(addr);
39 }
40 
is_ksym_addr(unsigned long addr)41 static inline int is_ksym_addr(unsigned long addr)
42 {
43 	if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
44 		return is_kernel(addr);
45 
46 	return is_kernel_text(addr) || is_kernel_inittext(addr);
47 }
48 
dereference_symbol_descriptor(void * ptr)49 static inline void *dereference_symbol_descriptor(void *ptr)
50 {
51 #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
52 	struct module *mod;
53 
54 	ptr = dereference_kernel_function_descriptor(ptr);
55 	if (is_ksym_addr((unsigned long)ptr))
56 		return ptr;
57 
58 	preempt_disable();
59 	mod = __module_address((unsigned long)ptr);
60 	preempt_enable();
61 
62 	if (mod)
63 		ptr = dereference_module_function_descriptor(mod, ptr);
64 #endif
65 	return ptr;
66 }
67 
68 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
69 				      unsigned long),
70 			    void *data);
71 
72 #ifdef CONFIG_KALLSYMS
73 /* Lookup the address for a symbol. Returns 0 if not found. */
74 unsigned long kallsyms_lookup_name(const char *name);
75 
76 extern int kallsyms_lookup_size_offset(unsigned long addr,
77 				  unsigned long *symbolsize,
78 				  unsigned long *offset);
79 
80 /* Lookup an address.  modname is set to NULL if it's in the kernel. */
81 const char *kallsyms_lookup(unsigned long addr,
82 			    unsigned long *symbolsize,
83 			    unsigned long *offset,
84 			    char **modname, char *namebuf);
85 
86 /* Look up a kernel symbol and return it in a text buffer. */
87 extern int sprint_symbol(char *buffer, unsigned long address);
88 extern int sprint_symbol_build_id(char *buffer, unsigned long address);
89 extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
90 extern int sprint_backtrace(char *buffer, unsigned long address);
91 extern int sprint_backtrace_build_id(char *buffer, unsigned long address);
92 
93 int lookup_symbol_name(unsigned long addr, char *symname);
94 int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
95 
96 /* How and when do we show kallsyms values? */
97 extern bool kallsyms_show_value(const struct cred *cred);
98 
99 #else /* !CONFIG_KALLSYMS */
100 
kallsyms_lookup_name(const char * name)101 static inline unsigned long kallsyms_lookup_name(const char *name)
102 {
103 	return 0;
104 }
105 
kallsyms_lookup_size_offset(unsigned long addr,unsigned long * symbolsize,unsigned long * offset)106 static inline int kallsyms_lookup_size_offset(unsigned long addr,
107 					      unsigned long *symbolsize,
108 					      unsigned long *offset)
109 {
110 	return 0;
111 }
112 
kallsyms_lookup(unsigned long addr,unsigned long * symbolsize,unsigned long * offset,char ** modname,char * namebuf)113 static inline const char *kallsyms_lookup(unsigned long addr,
114 					  unsigned long *symbolsize,
115 					  unsigned long *offset,
116 					  char **modname, char *namebuf)
117 {
118 	return NULL;
119 }
120 
sprint_symbol(char * buffer,unsigned long addr)121 static inline int sprint_symbol(char *buffer, unsigned long addr)
122 {
123 	*buffer = '\0';
124 	return 0;
125 }
126 
sprint_symbol_build_id(char * buffer,unsigned long address)127 static inline int sprint_symbol_build_id(char *buffer, unsigned long address)
128 {
129 	*buffer = '\0';
130 	return 0;
131 }
132 
sprint_symbol_no_offset(char * buffer,unsigned long addr)133 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
134 {
135 	*buffer = '\0';
136 	return 0;
137 }
138 
sprint_backtrace(char * buffer,unsigned long addr)139 static inline int sprint_backtrace(char *buffer, unsigned long addr)
140 {
141 	*buffer = '\0';
142 	return 0;
143 }
144 
sprint_backtrace_build_id(char * buffer,unsigned long addr)145 static inline int sprint_backtrace_build_id(char *buffer, unsigned long addr)
146 {
147 	*buffer = '\0';
148 	return 0;
149 }
150 
lookup_symbol_name(unsigned long addr,char * symname)151 static inline int lookup_symbol_name(unsigned long addr, char *symname)
152 {
153 	return -ERANGE;
154 }
155 
lookup_symbol_attrs(unsigned long addr,unsigned long * size,unsigned long * offset,char * modname,char * name)156 static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
157 {
158 	return -ERANGE;
159 }
160 
kallsyms_show_value(const struct cred * cred)161 static inline bool kallsyms_show_value(const struct cred *cred)
162 {
163 	return false;
164 }
165 
166 #endif /*CONFIG_KALLSYMS*/
167 
print_ip_sym(const char * loglvl,unsigned long ip)168 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
169 {
170 	printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
171 }
172 
173 #endif /*_LINUX_KALLSYMS_H*/
174