1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Rewritten and vastly simplified by Rusty Russell for in-kernel
6 #ifndef _LINUX_KALLSYMS_H
7 #define _LINUX_KALLSYMS_H
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/stddef.h>
13 #include <linux/module.h>
15 #include <asm/sections.h>
17 #define KSYM_NAME_LEN 128
18 #define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s]") + (KSYM_NAME_LEN - 1) + \
19 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + 1)
24 static inline int is_kernel_inittext(unsigned long addr)
26 if (addr >= (unsigned long)_sinittext
27 && addr <= (unsigned long)_einittext)
32 static inline int is_kernel_text(unsigned long addr)
34 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
35 arch_is_kernel_text(addr))
37 return in_gate_area_no_mm(addr);
40 static inline int is_kernel(unsigned long addr)
42 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
44 return in_gate_area_no_mm(addr);
47 static inline int is_ksym_addr(unsigned long addr)
49 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
50 return is_kernel(addr);
52 return is_kernel_text(addr) || is_kernel_inittext(addr);
55 static inline void *dereference_symbol_descriptor(void *ptr)
57 #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
60 ptr = dereference_kernel_function_descriptor(ptr);
61 if (is_ksym_addr((unsigned long)ptr))
65 mod = __module_address((unsigned long)ptr);
69 ptr = dereference_module_function_descriptor(mod, ptr);
74 #ifdef CONFIG_KALLSYMS
75 /* Lookup the address for a symbol. Returns 0 if not found. */
76 unsigned long kallsyms_lookup_name(const char *name);
78 /* Call a function on each kallsyms symbol in the core kernel */
79 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
83 extern int kallsyms_lookup_size_offset(unsigned long addr,
84 unsigned long *symbolsize,
85 unsigned long *offset);
87 /* Lookup an address. modname is set to NULL if it's in the kernel. */
88 const char *kallsyms_lookup(unsigned long addr,
89 unsigned long *symbolsize,
90 unsigned long *offset,
91 char **modname, char *namebuf);
93 /* Look up a kernel symbol and return it in a text buffer. */
94 extern int sprint_symbol(char *buffer, unsigned long address);
95 extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
96 extern int sprint_backtrace(char *buffer, unsigned long address);
98 int lookup_symbol_name(unsigned long addr, char *symname);
99 int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
101 /* How and when do we show kallsyms values? */
102 extern bool kallsyms_show_value(const struct cred *cred);
104 #else /* !CONFIG_KALLSYMS */
106 static inline unsigned long kallsyms_lookup_name(const char *name)
111 static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *,
119 static inline int kallsyms_lookup_size_offset(unsigned long addr,
120 unsigned long *symbolsize,
121 unsigned long *offset)
126 static inline const char *kallsyms_lookup(unsigned long addr,
127 unsigned long *symbolsize,
128 unsigned long *offset,
129 char **modname, char *namebuf)
134 static inline int sprint_symbol(char *buffer, unsigned long addr)
140 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
146 static inline int sprint_backtrace(char *buffer, unsigned long addr)
152 static inline int lookup_symbol_name(unsigned long addr, char *symname)
157 static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
162 static inline bool kallsyms_show_value(const struct cred *cred)
167 #endif /*CONFIG_KALLSYMS*/
169 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
171 printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
174 #endif /*_LINUX_KALLSYMS_H*/