blob: 98338dc6b5d275acdc80b2026ee6ac7111cb9f7d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* 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
Adrian Bunk40e48ee2007-07-07 00:54:09 +02009#include <linux/errno.h>
Vegard Nossum2711b792008-07-25 01:45:56 -070010#include <linux/kernel.h>
Kamalesh Babulal5a759832007-11-05 14:50:55 -080011#include <linux/stddef.h>
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +090012#include <linux/mm.h>
13#include <linux/module.h>
14
15#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Tejun Heo9281ace2007-07-17 04:03:51 -070017#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Anders Kaseorg75a66612008-12-05 19:03:58 -050021struct module;
22
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +090023static inline int is_kernel_inittext(unsigned long addr)
24{
25 if (addr >= (unsigned long)_sinittext
26 && addr <= (unsigned long)_einittext)
27 return 1;
28 return 0;
29}
30
31static inline int is_kernel_text(unsigned long addr)
32{
33 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
34 arch_is_kernel_text(addr))
35 return 1;
36 return in_gate_area_no_mm(addr);
37}
38
39static inline int is_kernel(unsigned long addr)
40{
41 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
42 return 1;
43 return in_gate_area_no_mm(addr);
44}
45
46static inline int is_ksym_addr(unsigned long addr)
47{
48 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
49 return is_kernel(addr);
50
51 return is_kernel_text(addr) || is_kernel_inittext(addr);
52}
53
54static inline void *dereference_symbol_descriptor(void *ptr)
55{
56#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
57 struct module *mod;
58
59 ptr = dereference_kernel_function_descriptor(ptr);
60 if (is_ksym_addr((unsigned long)ptr))
61 return ptr;
62
63 preempt_disable();
64 mod = __module_address((unsigned long)ptr);
65 preempt_enable();
66
67 if (mod)
68 ptr = dereference_module_function_descriptor(mod, ptr);
69#endif
70 return ptr;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef CONFIG_KALLSYMS
74/* Lookup the address for a symbol. Returns 0 if not found. */
75unsigned long kallsyms_lookup_name(const char *name);
76
Anders Kaseorg75a66612008-12-05 19:03:58 -050077/* Call a function on each kallsyms symbol in the core kernel */
78int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
79 unsigned long),
80 void *data);
81
Franck Bui-Huuffc50892006-10-03 01:13:48 -070082extern int kallsyms_lookup_size_offset(unsigned long addr,
83 unsigned long *symbolsize,
84 unsigned long *offset);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* Lookup an address. modname is set to NULL if it's in the kernel. */
87const char *kallsyms_lookup(unsigned long addr,
88 unsigned long *symbolsize,
89 unsigned long *offset,
90 char **modname, char *namebuf);
91
Robert Peterson42e38082007-04-30 15:09:48 -070092/* Look up a kernel symbol and return it in a text buffer. */
93extern int sprint_symbol(char *buffer, unsigned long address);
Stephen Boyd4796dd22012-05-29 15:07:33 -070094extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
Namhyung Kim0f77a8d2011-03-24 11:42:29 +090095extern int sprint_backtrace(char *buffer, unsigned long address);
Robert Peterson42e38082007-04-30 15:09:48 -070096
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -070097int lookup_symbol_name(unsigned long addr, char *symname);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -070098int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -070099
Arnd Bergmanne4a8ca32017-11-13 17:51:00 +0100100/* How and when do we show kallsyms values? */
101extern int kallsyms_show_value(void);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#else /* !CONFIG_KALLSYMS */
104
105static inline unsigned long kallsyms_lookup_name(const char *name)
106{
107 return 0;
108}
109
Anders Kaseorg75a66612008-12-05 19:03:58 -0500110static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *,
111 struct module *,
112 unsigned long),
113 void *data)
114{
115 return 0;
116}
117
Franck Bui-Huuffc50892006-10-03 01:13:48 -0700118static inline int kallsyms_lookup_size_offset(unsigned long addr,
119 unsigned long *symbolsize,
120 unsigned long *offset)
121{
122 return 0;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static inline const char *kallsyms_lookup(unsigned long addr,
126 unsigned long *symbolsize,
127 unsigned long *offset,
128 char **modname, char *namebuf)
129{
130 return NULL;
131}
132
Robert Peterson42e38082007-04-30 15:09:48 -0700133static inline int sprint_symbol(char *buffer, unsigned long addr)
134{
135 *buffer = '\0';
136 return 0;
137}
138
Stephen Boyd4796dd22012-05-29 15:07:33 -0700139static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
140{
141 *buffer = '\0';
142 return 0;
143}
144
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900145static inline int sprint_backtrace(char *buffer, unsigned long addr)
146{
147 *buffer = '\0';
148 return 0;
149}
150
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700151static inline int lookup_symbol_name(unsigned long addr, char *symname)
152{
153 return -ERANGE;
154}
155
Alexey Dobriyana5c43da2007-05-08 00:28:47 -0700156static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
157{
158 return -ERANGE;
159}
160
Arnd Bergmanne4a8ca32017-11-13 17:51:00 +0100161static inline int kallsyms_show_value(void)
162{
163 return false;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif /*CONFIG_KALLSYMS*/
167
Dmitry Safonov2062a4e2020-06-08 21:29:56 -0700168static inline void print_ip_sym(const char *loglvl, unsigned long ip)
Vegard Nossum2711b792008-07-25 01:45:56 -0700169{
Dmitry Safonov2062a4e2020-06-08 21:29:56 -0700170 printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
Vegard Nossum2711b792008-07-25 01:45:56 -0700171}
Heiko Carstens8d8fdf52006-07-03 00:24:25 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif /*_LINUX_KALLSYMS_H*/