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 512
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_text(unsigned long addr)
29 if (__is_kernel_text(addr))
31 return in_gate_area_no_mm(addr);
34 static inline int is_kernel(unsigned long addr)
36 if (__is_kernel(addr))
38 return in_gate_area_no_mm(addr);
41 static inline int is_ksym_addr(unsigned long addr)
43 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
44 return is_kernel(addr);
46 return is_kernel_text(addr) || is_kernel_inittext(addr);
49 static inline void *dereference_symbol_descriptor(void *ptr)
51 #ifdef CONFIG_HAVE_FUNCTION_DESCRIPTORS
54 ptr = dereference_kernel_function_descriptor(ptr);
55 if (is_ksym_addr((unsigned long)ptr))
59 mod = __module_address((unsigned long)ptr);
63 ptr = dereference_module_function_descriptor(mod, ptr);
68 #ifdef CONFIG_KALLSYMS
69 unsigned long kallsyms_sym_address(int idx);
70 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
73 int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
74 const char *name, void *data);
76 /* Lookup the address for a symbol. Returns 0 if not found. */
77 unsigned long kallsyms_lookup_name(const char *name);
79 extern int kallsyms_lookup_size_offset(unsigned long addr,
80 unsigned long *symbolsize,
81 unsigned long *offset);
83 /* Lookup an address. modname is set to NULL if it's in the kernel. */
84 const char *kallsyms_lookup(unsigned long addr,
85 unsigned long *symbolsize,
86 unsigned long *offset,
87 char **modname, char *namebuf);
89 /* Look up a kernel symbol and return it in a text buffer. */
90 extern int sprint_symbol(char *buffer, unsigned long address);
91 extern int sprint_symbol_build_id(char *buffer, unsigned long address);
92 extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
93 extern int sprint_backtrace(char *buffer, unsigned long address);
94 extern int sprint_backtrace_build_id(char *buffer, unsigned long address);
96 int lookup_symbol_name(unsigned long addr, char *symname);
97 int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
99 /* How and when do we show kallsyms values? */
100 extern bool kallsyms_show_value(const struct cred *cred);
102 #else /* !CONFIG_KALLSYMS */
104 static inline unsigned long kallsyms_lookup_name(const char *name)
109 static inline int kallsyms_lookup_size_offset(unsigned long addr,
110 unsigned long *symbolsize,
111 unsigned long *offset)
116 static inline const char *kallsyms_lookup(unsigned long addr,
117 unsigned long *symbolsize,
118 unsigned long *offset,
119 char **modname, char *namebuf)
124 static inline int sprint_symbol(char *buffer, unsigned long addr)
130 static inline int sprint_symbol_build_id(char *buffer, unsigned long address)
136 static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
142 static inline int sprint_backtrace(char *buffer, unsigned long addr)
148 static inline int sprint_backtrace_build_id(char *buffer, unsigned long addr)
154 static inline int lookup_symbol_name(unsigned long addr, char *symname)
159 static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
164 static inline bool kallsyms_show_value(const struct cred *cred)
169 static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
170 unsigned long), void *data)
175 static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
176 const char *name, void *data)
180 #endif /*CONFIG_KALLSYMS*/
182 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
184 printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
187 #endif /*_LINUX_KALLSYMS_H*/