1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Rewritten and vastly simplified by Rusty Russell for in-kernel
4 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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)
23 static inline int is_kernel_inittext(unsigned long addr)
25 if (addr >= (unsigned long)_sinittext
26 && addr <= (unsigned long)_einittext)
31 static inline int is_kernel_text(unsigned long addr)
33 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
34 arch_is_kernel_text(addr))
36 return in_gate_area_no_mm(addr);
39 static inline int is_kernel(unsigned long addr)
41 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
43 return in_gate_area_no_mm(addr);
46 static inline int is_ksym_addr(unsigned long addr)
48 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
49 return is_kernel(addr);
51 return is_kernel_text(addr) || is_kernel_inittext(addr);
54 static inline void *dereference_symbol_descriptor(void *ptr)
56 #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
59 ptr = dereference_kernel_function_descriptor(ptr);
60 if (is_ksym_addr((unsigned long)ptr))
64 mod = __module_address((unsigned long)ptr);
68 ptr = dereference_module_function_descriptor(mod, ptr);
73 #ifdef CONFIG_KALLSYMS
74 /* Lookup the address for a symbol. Returns 0 if not found. */
75 unsigned long kallsyms_lookup_name(const char *name);
77 /* Call a function on each kallsyms symbol in the core kernel */
78 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
82 extern int kallsyms_lookup_size_offset(unsigned long addr,
83 unsigned long *symbolsize,
84 unsigned long *offset);
86 /* Lookup an address. modname is set to NULL if it's in the kernel. */
87 const char *kallsyms_lookup(unsigned long addr,
88 unsigned long *symbolsize,
89 unsigned long *offset,
90 char **modname, char *namebuf);
92 /* Look up a kernel symbol and return it in a text buffer. */
93 extern int sprint_symbol(char *buffer, unsigned long address);
94 extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
95 extern int sprint_backtrace(char *buffer, unsigned long address);
97 int lookup_symbol_name(unsigned long addr, char *symname);
98 int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
100 /* How and when do we show kallsyms values? */
101 extern int kallsyms_show_value(void);
103 #else /* !CONFIG_KALLSYMS */
105 static inline unsigned long kallsyms_lookup_name(const char *name)
110 static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *,
118 static inline int kallsyms_lookup_size_offset(unsigned long addr,
119 unsigned long *symbolsize,
120 unsigned long *offset)
125 static inline const char *kallsyms_lookup(unsigned long addr,
126 unsigned long *symbolsize,
127 unsigned long *offset,
128 char **modname, char *namebuf)
133 static inline int sprint_symbol(char *buffer, unsigned long addr)
139 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
145 static inline int sprint_backtrace(char *buffer, unsigned long addr)
151 static inline int lookup_symbol_name(unsigned long addr, char *symname)
156 static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
161 static inline int kallsyms_show_value(void)
166 #endif /*CONFIG_KALLSYMS*/
168 static inline void print_ip_sym(unsigned long ip)
170 printk("[<%px>] %pS\n", (void *) ip, (void *) ip);
173 #endif /*_LINUX_KALLSYMS_H*/