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/buildid.h>
11 #include <linux/kernel.h>
12 #include <linux/stddef.h>
14 #include <linux/module.h>
16 #include <asm/sections.h>
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)
27 static inline int is_kernel_inittext(unsigned long addr)
29 if (addr >= (unsigned long)_sinittext
30 && addr <= (unsigned long)_einittext)
35 static inline int is_kernel_text(unsigned long addr)
37 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
38 arch_is_kernel_text(addr))
40 return in_gate_area_no_mm(addr);
43 static inline int is_kernel(unsigned long addr)
45 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
47 return in_gate_area_no_mm(addr);
50 static inline int is_ksym_addr(unsigned long addr)
52 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
53 return is_kernel(addr);
55 return is_kernel_text(addr) || is_kernel_inittext(addr);
58 static inline void *dereference_symbol_descriptor(void *ptr)
60 #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
63 ptr = dereference_kernel_function_descriptor(ptr);
64 if (is_ksym_addr((unsigned long)ptr))
68 mod = __module_address((unsigned long)ptr);
72 ptr = dereference_module_function_descriptor(mod, ptr);
77 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
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);
85 extern int kallsyms_lookup_size_offset(unsigned long addr,
86 unsigned long *symbolsize,
87 unsigned long *offset);
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);
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);
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);
105 /* How and when do we show kallsyms values? */
106 extern bool kallsyms_show_value(const struct cred *cred);
108 #else /* !CONFIG_KALLSYMS */
110 static inline unsigned long kallsyms_lookup_name(const char *name)
115 static inline int kallsyms_lookup_size_offset(unsigned long addr,
116 unsigned long *symbolsize,
117 unsigned long *offset)
122 static inline const char *kallsyms_lookup(unsigned long addr,
123 unsigned long *symbolsize,
124 unsigned long *offset,
125 char **modname, char *namebuf)
130 static inline int sprint_symbol(char *buffer, unsigned long addr)
136 static inline int sprint_symbol_build_id(char *buffer, unsigned long address)
142 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
148 static inline int sprint_backtrace(char *buffer, unsigned long addr)
154 static inline int sprint_backtrace_build_id(char *buffer, unsigned long addr)
160 static inline int lookup_symbol_name(unsigned long addr, char *symname)
165 static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
170 static inline bool kallsyms_show_value(const struct cred *cred)
175 #endif /*CONFIG_KALLSYMS*/
177 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
179 printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
182 #endif /*_LINUX_KALLSYMS_H*/