Merge tag 'perf-tools-for-v6.4-3-2023-05-06' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / kallsyms.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2/* 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
40e48eed 9#include <linux/errno.h>
9294523e 10#include <linux/buildid.h>
2711b793 11#include <linux/kernel.h>
5a75983e 12#include <linux/stddef.h>
04b8eb7a
SS
13#include <linux/mm.h>
14#include <linux/module.h>
15
16#include <asm/sections.h>
1da177e4 17
b8a94bfb 18#define KSYM_NAME_LEN 512
9294523e
SB
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)
1da177e4 23
16025184 24struct cred;
75a66614
AK
25struct module;
26
04b8eb7a
SS
27static inline int is_kernel_text(unsigned long addr)
28{
8f6e42e8 29 if (__is_kernel_text(addr))
04b8eb7a
SS
30 return 1;
31 return in_gate_area_no_mm(addr);
32}
33
34static inline int is_kernel(unsigned long addr)
35{
8f6e42e8 36 if (__is_kernel(addr))
04b8eb7a
SS
37 return 1;
38 return in_gate_area_no_mm(addr);
39}
40
41static inline int is_ksym_addr(unsigned long addr)
42{
43 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
44 return is_kernel(addr);
45
46 return is_kernel_text(addr) || is_kernel_inittext(addr);
47}
48
49static inline void *dereference_symbol_descriptor(void *ptr)
50{
a257cacc 51#ifdef CONFIG_HAVE_FUNCTION_DESCRIPTORS
04b8eb7a
SS
52 struct module *mod;
53
54 ptr = dereference_kernel_function_descriptor(ptr);
55 if (is_ksym_addr((unsigned long)ptr))
56 return ptr;
57
58 preempt_disable();
59 mod = __module_address((unsigned long)ptr);
60 preempt_enable();
61
62 if (mod)
63 ptr = dereference_module_function_descriptor(mod, ptr);
64#endif
65 return ptr;
66}
67
d721def7 68#ifdef CONFIG_KALLSYMS
30f3bb09 69unsigned long kallsyms_sym_address(int idx);
3703bd54 70int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long),
75a66614 71 void *data);
4dc533e0
ZL
72int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
73 const char *name, void *data);
75a66614 74
3e355205
CH
75/* Lookup the address for a symbol. Returns 0 if not found. */
76unsigned long kallsyms_lookup_name(const char *name);
77
ffc50891
FBH
78extern int kallsyms_lookup_size_offset(unsigned long addr,
79 unsigned long *symbolsize,
80 unsigned long *offset);
81
1da177e4
LT
82/* Lookup an address. modname is set to NULL if it's in the kernel. */
83const char *kallsyms_lookup(unsigned long addr,
84 unsigned long *symbolsize,
85 unsigned long *offset,
86 char **modname, char *namebuf);
87
42e38083
RP
88/* Look up a kernel symbol and return it in a text buffer. */
89extern int sprint_symbol(char *buffer, unsigned long address);
9294523e 90extern int sprint_symbol_build_id(char *buffer, unsigned long address);
4796dd20 91extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
0f77a8d3 92extern int sprint_backtrace(char *buffer, unsigned long address);
9294523e 93extern int sprint_backtrace_build_id(char *buffer, unsigned long address);
42e38083 94
9d65cb4a 95int lookup_symbol_name(unsigned long addr, char *symname);
a5c43dae 96int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
9d65cb4a 97
e4a8ca3b 98/* How and when do we show kallsyms values? */
16025184 99extern bool kallsyms_show_value(const struct cred *cred);
e4a8ca3b 100
1da177e4
LT
101#else /* !CONFIG_KALLSYMS */
102
103static inline unsigned long kallsyms_lookup_name(const char *name)
104{
105 return 0;
106}
107
ffc50891
FBH
108static inline int kallsyms_lookup_size_offset(unsigned long addr,
109 unsigned long *symbolsize,
110 unsigned long *offset)
111{
112 return 0;
113}
114
1da177e4
LT
115static inline const char *kallsyms_lookup(unsigned long addr,
116 unsigned long *symbolsize,
117 unsigned long *offset,
118 char **modname, char *namebuf)
119{
120 return NULL;
121}
122
42e38083
RP
123static inline int sprint_symbol(char *buffer, unsigned long addr)
124{
125 *buffer = '\0';
126 return 0;
127}
128
9294523e
SB
129static inline int sprint_symbol_build_id(char *buffer, unsigned long address)
130{
131 *buffer = '\0';
132 return 0;
133}
134
4796dd20
SB
135static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
136{
137 *buffer = '\0';
138 return 0;
139}
140
0f77a8d3
NK
141static inline int sprint_backtrace(char *buffer, unsigned long addr)
142{
143 *buffer = '\0';
144 return 0;
145}
146
9294523e
SB
147static inline int sprint_backtrace_build_id(char *buffer, unsigned long addr)
148{
149 *buffer = '\0';
150 return 0;
151}
152
9d65cb4a
AD
153static inline int lookup_symbol_name(unsigned long addr, char *symname)
154{
155 return -ERANGE;
156}
157
a5c43dae
AD
158static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
159{
160 return -ERANGE;
161}
162
16025184 163static inline bool kallsyms_show_value(const struct cred *cred)
e4a8ca3b
AB
164{
165 return false;
166}
167
3703bd54
ZL
168static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long),
169 void *data)
d721def7
JO
170{
171 return -EOPNOTSUPP;
172}
4dc533e0
ZL
173
174static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
175 const char *name, void *data)
176{
177 return -EOPNOTSUPP;
178}
1da177e4
LT
179#endif /*CONFIG_KALLSYMS*/
180
2062a4e8 181static inline void print_ip_sym(const char *loglvl, unsigned long ip)
2711b793 182{
2062a4e8 183 printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
2711b793 184}
8d8fdf5c 185
1da177e4 186#endif /*_LINUX_KALLSYMS_H*/