1
2
3
4
5
6#ifndef _LINUX_KALLSYMS_H
7#define _LINUX_KALLSYMS_H
8
9#include <linux/errno.h>
10#include <linux/kernel.h>
11#include <linux/stddef.h>
12#include <linux/mm.h>
13#include <linux/module.h>
14
15#include <asm/sections.h>
16
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)
20
21struct cred;
22struct module;
23
24static inline int is_kernel_inittext(unsigned long addr)
25{
26 if (addr >= (unsigned long)_sinittext
27 && addr <= (unsigned long)_einittext)
28 return 1;
29 return 0;
30}
31
32static inline int is_kernel_text(unsigned long addr)
33{
34 if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
35 arch_is_kernel_text(addr))
36 return 1;
37 return in_gate_area_no_mm(addr);
38}
39
40static inline int is_kernel(unsigned long addr)
41{
42 if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
43 return 1;
44 return in_gate_area_no_mm(addr);
45}
46
47static inline int is_ksym_addr(unsigned long addr)
48{
49 if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
50 return is_kernel(addr);
51
52 return is_kernel_text(addr) || is_kernel_inittext(addr);
53}
54
55static inline void *dereference_symbol_descriptor(void *ptr)
56{
57#ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR
58 struct module *mod;
59
60 ptr = dereference_kernel_function_descriptor(ptr);
61 if (is_ksym_addr((unsigned long)ptr))
62 return ptr;
63
64 preempt_disable();
65 mod = __module_address((unsigned long)ptr);
66 preempt_enable();
67
68 if (mod)
69 ptr = dereference_module_function_descriptor(mod, ptr);
70#endif
71 return ptr;
72}
73
74#ifdef CONFIG_KALLSYMS
75
76unsigned long kallsyms_lookup_name(const char *name);
77
78
79int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
80 unsigned long),
81 void *data);
82
83extern int kallsyms_lookup_size_offset(unsigned long addr,
84 unsigned long *symbolsize,
85 unsigned long *offset);
86
87
88const char *kallsyms_lookup(unsigned long addr,
89 unsigned long *symbolsize,
90 unsigned long *offset,
91 char **modname, char *namebuf);
92
93
94extern int sprint_symbol(char *buffer, unsigned long address);
95extern int sprint_symbol_no_offset(char *buffer, unsigned long address);
96extern int sprint_backtrace(char *buffer, unsigned long address);
97
98int lookup_symbol_name(unsigned long addr, char *symname);
99int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
100
101
102extern bool kallsyms_show_value(const struct cred *cred);
103
104#else
105
106static inline unsigned long kallsyms_lookup_name(const char *name)
107{
108 return 0;
109}
110
111static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *,
112 struct module *,
113 unsigned long),
114 void *data)
115{
116 return 0;
117}
118
119static inline int kallsyms_lookup_size_offset(unsigned long addr,
120 unsigned long *symbolsize,
121 unsigned long *offset)
122{
123 return 0;
124}
125
126static inline const char *kallsyms_lookup(unsigned long addr,
127 unsigned long *symbolsize,
128 unsigned long *offset,
129 char **modname, char *namebuf)
130{
131 return NULL;
132}
133
134static inline int sprint_symbol(char *buffer, unsigned long addr)
135{
136 *buffer = '\0';
137 return 0;
138}
139
140static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr)
141{
142 *buffer = '\0';
143 return 0;
144}
145
146static inline int sprint_backtrace(char *buffer, unsigned long addr)
147{
148 *buffer = '\0';
149 return 0;
150}
151
152static inline int lookup_symbol_name(unsigned long addr, char *symname)
153{
154 return -ERANGE;
155}
156
157static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
158{
159 return -ERANGE;
160}
161
162static inline bool kallsyms_show_value(const struct cred *cred)
163{
164 return false;
165}
166
167#endif
168
169static inline void print_ip_sym(const char *loglvl, unsigned long ip)
170{
171 printk("%s[<%px>] %pS\n", loglvl, (void *) ip, (void *) ip);
172}
173
174#endif
175