Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * kallsyms.c: in-kernel printing of symbolic oopses and stack traces. | |
4 | * | |
5 | * Rewritten and vastly simplified by Rusty Russell for in-kernel | |
6 | * module loader: | |
7 | * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation | |
8 | * | |
9 | * ChangeLog: | |
10 | * | |
11 | * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com> | |
12 | * Changed the compression method from stem compression to "table lookup" | |
13 | * compression (see scripts/kallsyms.c for a more complete description) | |
14 | */ | |
15 | #include <linux/kallsyms.h> | |
1da177e4 LT |
16 | #include <linux/init.h> |
17 | #include <linux/seq_file.h> | |
18 | #include <linux/fs.h> | |
67fc4e0c | 19 | #include <linux/kdb.h> |
1da177e4 LT |
20 | #include <linux/err.h> |
21 | #include <linux/proc_fs.h> | |
4e57b681 | 22 | #include <linux/sched.h> /* for cond_resched */ |
07354a00 | 23 | #include <linux/ctype.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
74451e66 | 25 | #include <linux/filter.h> |
aba4b5c2 | 26 | #include <linux/ftrace.h> |
d002b8bc | 27 | #include <linux/kprobes.h> |
52f5684c | 28 | #include <linux/compiler.h> |
1da177e4 | 29 | |
ad6ccfad MK |
30 | /* |
31 | * These will be re-linked against their real values | |
32 | * during the second link stage. | |
33 | */ | |
52f5684c | 34 | extern const unsigned long kallsyms_addresses[] __weak; |
2213e9a6 | 35 | extern const int kallsyms_offsets[] __weak; |
52f5684c | 36 | extern const u8 kallsyms_names[] __weak; |
1da177e4 | 37 | |
ad6ccfad MK |
38 | /* |
39 | * Tell the compiler that the count isn't in the small data section if the arch | |
40 | * has one (eg: FRV). | |
9e6c1e63 | 41 | */ |
80ffbaa5 | 42 | extern const unsigned int kallsyms_num_syms |
33def849 | 43 | __section(".rodata") __attribute__((weak)); |
9e6c1e63 | 44 | |
2213e9a6 | 45 | extern const unsigned long kallsyms_relative_base |
33def849 | 46 | __section(".rodata") __attribute__((weak)); |
2213e9a6 | 47 | |
cde26a6e | 48 | extern const char kallsyms_token_table[] __weak; |
52f5684c | 49 | extern const u16 kallsyms_token_index[] __weak; |
1da177e4 | 50 | |
80ffbaa5 | 51 | extern const unsigned int kallsyms_markers[] __weak; |
1da177e4 | 52 | |
ad6ccfad MK |
53 | /* |
54 | * Expand a compressed symbol data into the resulting uncompressed string, | |
e3f26752 | 55 | * if uncompressed string is too long (>= maxlen), it will be truncated, |
ad6ccfad MK |
56 | * given the offset to where the symbol is in the compressed stream. |
57 | */ | |
e3f26752 CG |
58 | static unsigned int kallsyms_expand_symbol(unsigned int off, |
59 | char *result, size_t maxlen) | |
1da177e4 LT |
60 | { |
61 | int len, skipped_first = 0; | |
cde26a6e MY |
62 | const char *tptr; |
63 | const u8 *data; | |
1da177e4 | 64 | |
ad6ccfad | 65 | /* Get the compressed symbol length from the first symbol byte. */ |
1da177e4 LT |
66 | data = &kallsyms_names[off]; |
67 | len = *data; | |
68 | data++; | |
69 | ||
ad6ccfad MK |
70 | /* |
71 | * Update the offset to return the offset for the next symbol on | |
72 | * the compressed stream. | |
73 | */ | |
1da177e4 LT |
74 | off += len + 1; |
75 | ||
ad6ccfad MK |
76 | /* |
77 | * For every byte on the compressed symbol data, copy the table | |
78 | * entry for that byte. | |
79 | */ | |
80 | while (len) { | |
81 | tptr = &kallsyms_token_table[kallsyms_token_index[*data]]; | |
1da177e4 LT |
82 | data++; |
83 | len--; | |
84 | ||
85 | while (*tptr) { | |
ad6ccfad | 86 | if (skipped_first) { |
e3f26752 CG |
87 | if (maxlen <= 1) |
88 | goto tail; | |
1da177e4 LT |
89 | *result = *tptr; |
90 | result++; | |
e3f26752 | 91 | maxlen--; |
1da177e4 LT |
92 | } else |
93 | skipped_first = 1; | |
94 | tptr++; | |
95 | } | |
96 | } | |
97 | ||
e3f26752 CG |
98 | tail: |
99 | if (maxlen) | |
100 | *result = '\0'; | |
1da177e4 | 101 | |
ad6ccfad | 102 | /* Return to offset to the next symbol. */ |
1da177e4 LT |
103 | return off; |
104 | } | |
105 | ||
ad6ccfad MK |
106 | /* |
107 | * Get symbol type information. This is encoded as a single char at the | |
108 | * beginning of the symbol name. | |
109 | */ | |
1da177e4 LT |
110 | static char kallsyms_get_symbol_type(unsigned int off) |
111 | { | |
ad6ccfad MK |
112 | /* |
113 | * Get just the first code, look it up in the token table, | |
114 | * and return the first char from this token. | |
115 | */ | |
116 | return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]]; | |
1da177e4 LT |
117 | } |
118 | ||
119 | ||
ad6ccfad MK |
120 | /* |
121 | * Find the offset on the compressed stream given and index in the | |
122 | * kallsyms array. | |
123 | */ | |
1da177e4 LT |
124 | static unsigned int get_symbol_offset(unsigned long pos) |
125 | { | |
aad09470 | 126 | const u8 *name; |
1da177e4 LT |
127 | int i; |
128 | ||
ad6ccfad MK |
129 | /* |
130 | * Use the closest marker we have. We have markers every 256 positions, | |
131 | * so that should be close enough. | |
132 | */ | |
133 | name = &kallsyms_names[kallsyms_markers[pos >> 8]]; | |
1da177e4 | 134 | |
ad6ccfad MK |
135 | /* |
136 | * Sequentially scan all the symbols up to the point we're searching | |
137 | * for. Every symbol is stored in a [<len>][<len> bytes of data] format, | |
138 | * so we just need to add the len to the current pointer for every | |
139 | * symbol we wish to skip. | |
140 | */ | |
141 | for (i = 0; i < (pos & 0xFF); i++) | |
1da177e4 LT |
142 | name = name + (*name) + 1; |
143 | ||
144 | return name - kallsyms_names; | |
145 | } | |
146 | ||
2213e9a6 AB |
147 | static unsigned long kallsyms_sym_address(int idx) |
148 | { | |
149 | if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) | |
150 | return kallsyms_addresses[idx]; | |
151 | ||
152 | /* values are unsigned offsets if --absolute-percpu is not in effect */ | |
153 | if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU)) | |
154 | return kallsyms_relative_base + (u32)kallsyms_offsets[idx]; | |
155 | ||
156 | /* ...otherwise, positive offsets are absolute values */ | |
157 | if (kallsyms_offsets[idx] >= 0) | |
158 | return kallsyms_offsets[idx]; | |
159 | ||
160 | /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ | |
161 | return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; | |
162 | } | |
163 | ||
1da177e4 LT |
164 | /* Lookup the address for this symbol. Returns 0 if not found. */ |
165 | unsigned long kallsyms_lookup_name(const char *name) | |
166 | { | |
9281acea | 167 | char namebuf[KSYM_NAME_LEN]; |
1da177e4 LT |
168 | unsigned long i; |
169 | unsigned int off; | |
170 | ||
171 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { | |
e3f26752 | 172 | off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); |
1da177e4 LT |
173 | |
174 | if (strcmp(namebuf, name) == 0) | |
2213e9a6 | 175 | return kallsyms_sym_address(i); |
1da177e4 LT |
176 | } |
177 | return module_kallsyms_lookup_name(name); | |
178 | } | |
1da177e4 | 179 | |
3e355205 | 180 | #ifdef CONFIG_LIVEPATCH |
013c1667 CH |
181 | /* |
182 | * Iterate over all symbols in vmlinux. For symbols from modules use | |
183 | * module_kallsyms_on_each_symbol instead. | |
184 | */ | |
75a66614 AK |
185 | int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, |
186 | unsigned long), | |
187 | void *data) | |
188 | { | |
189 | char namebuf[KSYM_NAME_LEN]; | |
190 | unsigned long i; | |
191 | unsigned int off; | |
192 | int ret; | |
193 | ||
194 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { | |
e3f26752 | 195 | off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); |
2213e9a6 | 196 | ret = fn(data, namebuf, NULL, kallsyms_sym_address(i)); |
75a66614 AK |
197 | if (ret != 0) |
198 | return ret; | |
199 | } | |
013c1667 | 200 | return 0; |
75a66614 | 201 | } |
3e355205 | 202 | #endif /* CONFIG_LIVEPATCH */ |
75a66614 | 203 | |
ffc50891 FBH |
204 | static unsigned long get_symbol_pos(unsigned long addr, |
205 | unsigned long *symbolsize, | |
206 | unsigned long *offset) | |
207 | { | |
208 | unsigned long symbol_start = 0, symbol_end = 0; | |
209 | unsigned long i, low, high, mid; | |
210 | ||
2ea03891 | 211 | /* This kernel should never had been booted. */ |
2213e9a6 AB |
212 | if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) |
213 | BUG_ON(!kallsyms_addresses); | |
214 | else | |
215 | BUG_ON(!kallsyms_offsets); | |
2ea03891 | 216 | |
ad6ccfad | 217 | /* Do a binary search on the sorted kallsyms_addresses array. */ |
ffc50891 FBH |
218 | low = 0; |
219 | high = kallsyms_num_syms; | |
220 | ||
221 | while (high - low > 1) { | |
2fc9c4e1 | 222 | mid = low + (high - low) / 2; |
2213e9a6 | 223 | if (kallsyms_sym_address(mid) <= addr) |
ffc50891 FBH |
224 | low = mid; |
225 | else | |
226 | high = mid; | |
227 | } | |
228 | ||
229 | /* | |
ad6ccfad MK |
230 | * Search for the first aliased symbol. Aliased |
231 | * symbols are symbols with the same address. | |
ffc50891 | 232 | */ |
2213e9a6 | 233 | while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) |
ffc50891 FBH |
234 | --low; |
235 | ||
2213e9a6 | 236 | symbol_start = kallsyms_sym_address(low); |
ffc50891 | 237 | |
ad6ccfad | 238 | /* Search for next non-aliased symbol. */ |
ffc50891 | 239 | for (i = low + 1; i < kallsyms_num_syms; i++) { |
2213e9a6 AB |
240 | if (kallsyms_sym_address(i) > symbol_start) { |
241 | symbol_end = kallsyms_sym_address(i); | |
ffc50891 FBH |
242 | break; |
243 | } | |
244 | } | |
245 | ||
ad6ccfad | 246 | /* If we found no next symbol, we use the end of the section. */ |
ffc50891 FBH |
247 | if (!symbol_end) { |
248 | if (is_kernel_inittext(addr)) | |
249 | symbol_end = (unsigned long)_einittext; | |
63b23e2c | 250 | else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) |
ffc50891 FBH |
251 | symbol_end = (unsigned long)_end; |
252 | else | |
253 | symbol_end = (unsigned long)_etext; | |
254 | } | |
255 | ||
ffb45122 AD |
256 | if (symbolsize) |
257 | *symbolsize = symbol_end - symbol_start; | |
258 | if (offset) | |
259 | *offset = addr - symbol_start; | |
ffc50891 FBH |
260 | |
261 | return low; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Lookup an address but don't bother to find any names. | |
266 | */ | |
267 | int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, | |
268 | unsigned long *offset) | |
269 | { | |
6dd06c9f | 270 | char namebuf[KSYM_NAME_LEN]; |
74451e66 | 271 | |
2a1a3fa0 MZ |
272 | if (is_ksym_addr(addr)) { |
273 | get_symbol_pos(addr, symbolsize, offset); | |
274 | return 1; | |
275 | } | |
74451e66 DB |
276 | return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) || |
277 | !!__bpf_address_lookup(addr, symbolsize, offset, namebuf); | |
ffc50891 FBH |
278 | } |
279 | ||
1da177e4 LT |
280 | /* |
281 | * Lookup an address | |
ad6ccfad MK |
282 | * - modname is set to NULL if it's in the kernel. |
283 | * - We guarantee that the returned name is valid until we reschedule even if. | |
284 | * It resides in a module. | |
285 | * - We also guarantee that modname will be valid until rescheduled. | |
1da177e4 LT |
286 | */ |
287 | const char *kallsyms_lookup(unsigned long addr, | |
288 | unsigned long *symbolsize, | |
289 | unsigned long *offset, | |
290 | char **modname, char *namebuf) | |
291 | { | |
74451e66 DB |
292 | const char *ret; |
293 | ||
9281acea | 294 | namebuf[KSYM_NAME_LEN - 1] = 0; |
1da177e4 LT |
295 | namebuf[0] = 0; |
296 | ||
ffc50891 FBH |
297 | if (is_ksym_addr(addr)) { |
298 | unsigned long pos; | |
1da177e4 | 299 | |
ffc50891 | 300 | pos = get_symbol_pos(addr, symbolsize, offset); |
1da177e4 | 301 | /* Grab name */ |
e3f26752 CG |
302 | kallsyms_expand_symbol(get_symbol_offset(pos), |
303 | namebuf, KSYM_NAME_LEN); | |
7a74fc49 KM |
304 | if (modname) |
305 | *modname = NULL; | |
1da177e4 LT |
306 | return namebuf; |
307 | } | |
308 | ||
74451e66 DB |
309 | /* See if it's in a module or a BPF JITed image. */ |
310 | ret = module_address_lookup(addr, symbolsize, offset, | |
311 | modname, namebuf); | |
312 | if (!ret) | |
313 | ret = bpf_address_lookup(addr, symbolsize, | |
314 | offset, modname, namebuf); | |
aba4b5c2 SRV |
315 | |
316 | if (!ret) | |
317 | ret = ftrace_mod_address_lookup(addr, symbolsize, | |
318 | offset, modname, namebuf); | |
74451e66 | 319 | return ret; |
1da177e4 LT |
320 | } |
321 | ||
9d65cb4a AD |
322 | int lookup_symbol_name(unsigned long addr, char *symname) |
323 | { | |
324 | symname[0] = '\0'; | |
9281acea | 325 | symname[KSYM_NAME_LEN - 1] = '\0'; |
9d65cb4a AD |
326 | |
327 | if (is_ksym_addr(addr)) { | |
328 | unsigned long pos; | |
329 | ||
330 | pos = get_symbol_pos(addr, NULL, NULL); | |
331 | /* Grab name */ | |
e3f26752 CG |
332 | kallsyms_expand_symbol(get_symbol_offset(pos), |
333 | symname, KSYM_NAME_LEN); | |
9d65cb4a AD |
334 | return 0; |
335 | } | |
ad6ccfad | 336 | /* See if it's in a module. */ |
9d65cb4a AD |
337 | return lookup_module_symbol_name(addr, symname); |
338 | } | |
339 | ||
a5c43dae AD |
340 | int lookup_symbol_attrs(unsigned long addr, unsigned long *size, |
341 | unsigned long *offset, char *modname, char *name) | |
342 | { | |
343 | name[0] = '\0'; | |
9281acea | 344 | name[KSYM_NAME_LEN - 1] = '\0'; |
a5c43dae AD |
345 | |
346 | if (is_ksym_addr(addr)) { | |
347 | unsigned long pos; | |
348 | ||
349 | pos = get_symbol_pos(addr, size, offset); | |
350 | /* Grab name */ | |
e3f26752 CG |
351 | kallsyms_expand_symbol(get_symbol_offset(pos), |
352 | name, KSYM_NAME_LEN); | |
a5c43dae AD |
353 | modname[0] = '\0'; |
354 | return 0; | |
355 | } | |
ad6ccfad | 356 | /* See if it's in a module. */ |
a5c43dae AD |
357 | return lookup_module_symbol_attrs(addr, size, offset, modname, name); |
358 | } | |
359 | ||
42e38083 | 360 | /* Look up a kernel symbol and return it in a text buffer. */ |
0f77a8d3 | 361 | static int __sprint_symbol(char *buffer, unsigned long address, |
4796dd20 | 362 | int symbol_offset, int add_offset) |
1da177e4 LT |
363 | { |
364 | char *modname; | |
365 | const char *name; | |
366 | unsigned long offset, size; | |
966c8c12 | 367 | int len; |
1da177e4 | 368 | |
0f77a8d3 | 369 | address += symbol_offset; |
966c8c12 | 370 | name = kallsyms_lookup(address, &size, &offset, &modname, buffer); |
1da177e4 | 371 | if (!name) |
b86280aa | 372 | return sprintf(buffer, "0x%lx", address - symbol_offset); |
19769b76 | 373 | |
966c8c12 HD |
374 | if (name != buffer) |
375 | strcpy(buffer, name); | |
376 | len = strlen(buffer); | |
0f77a8d3 | 377 | offset -= symbol_offset; |
966c8c12 | 378 | |
4796dd20 SB |
379 | if (add_offset) |
380 | len += sprintf(buffer + len, "+%#lx/%#lx", offset, size); | |
381 | ||
19769b76 | 382 | if (modname) |
4796dd20 | 383 | len += sprintf(buffer + len, " [%s]", modname); |
966c8c12 HD |
384 | |
385 | return len; | |
42e38083 | 386 | } |
0f77a8d3 NK |
387 | |
388 | /** | |
389 | * sprint_symbol - Look up a kernel symbol and return it in a text buffer | |
390 | * @buffer: buffer to be stored | |
391 | * @address: address to lookup | |
392 | * | |
393 | * This function looks up a kernel symbol with @address and stores its name, | |
394 | * offset, size and module name to @buffer if possible. If no symbol was found, | |
395 | * just saves its @address as is. | |
396 | * | |
397 | * This function returns the number of bytes stored in @buffer. | |
398 | */ | |
399 | int sprint_symbol(char *buffer, unsigned long address) | |
400 | { | |
4796dd20 | 401 | return __sprint_symbol(buffer, address, 0, 1); |
0f77a8d3 | 402 | } |
ad6ccfad | 403 | EXPORT_SYMBOL_GPL(sprint_symbol); |
42e38083 | 404 | |
4796dd20 SB |
405 | /** |
406 | * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer | |
407 | * @buffer: buffer to be stored | |
408 | * @address: address to lookup | |
409 | * | |
410 | * This function looks up a kernel symbol with @address and stores its name | |
411 | * and module name to @buffer if possible. If no symbol was found, just saves | |
412 | * its @address as is. | |
413 | * | |
414 | * This function returns the number of bytes stored in @buffer. | |
415 | */ | |
416 | int sprint_symbol_no_offset(char *buffer, unsigned long address) | |
417 | { | |
418 | return __sprint_symbol(buffer, address, 0, 0); | |
419 | } | |
420 | EXPORT_SYMBOL_GPL(sprint_symbol_no_offset); | |
421 | ||
0f77a8d3 NK |
422 | /** |
423 | * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer | |
424 | * @buffer: buffer to be stored | |
425 | * @address: address to lookup | |
426 | * | |
427 | * This function is for stack backtrace and does the same thing as | |
428 | * sprint_symbol() but with modified/decreased @address. If there is a | |
429 | * tail-call to the function marked "noreturn", gcc optimized out code after | |
430 | * the call so that the stack-saved return address could point outside of the | |
431 | * caller. This function ensures that kallsyms will find the original caller | |
432 | * by decreasing @address. | |
433 | * | |
434 | * This function returns the number of bytes stored in @buffer. | |
435 | */ | |
436 | int sprint_backtrace(char *buffer, unsigned long address) | |
437 | { | |
4796dd20 | 438 | return __sprint_symbol(buffer, address, -1, 1); |
0f77a8d3 NK |
439 | } |
440 | ||
1da177e4 | 441 | /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ |
ad6ccfad | 442 | struct kallsym_iter { |
1da177e4 | 443 | loff_t pos; |
d83212d5 | 444 | loff_t pos_arch_end; |
74451e66 | 445 | loff_t pos_mod_end; |
6171a031 | 446 | loff_t pos_ftrace_mod_end; |
d002b8bc | 447 | loff_t pos_bpf_end; |
1da177e4 | 448 | unsigned long value; |
ad6ccfad | 449 | unsigned int nameoff; /* If iterating in core kernel symbols. */ |
1da177e4 | 450 | char type; |
9281acea TH |
451 | char name[KSYM_NAME_LEN]; |
452 | char module_name[MODULE_NAME_LEN]; | |
ea07890a | 453 | int exported; |
c0f3ea15 | 454 | int show_value; |
1da177e4 LT |
455 | }; |
456 | ||
d83212d5 AS |
457 | int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value, |
458 | char *type, char *name) | |
459 | { | |
460 | return -EINVAL; | |
461 | } | |
462 | ||
463 | static int get_ksymbol_arch(struct kallsym_iter *iter) | |
464 | { | |
465 | int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms, | |
466 | &iter->value, &iter->type, | |
467 | iter->name); | |
468 | ||
469 | if (ret < 0) { | |
470 | iter->pos_arch_end = iter->pos; | |
471 | return 0; | |
472 | } | |
473 | ||
474 | return 1; | |
475 | } | |
476 | ||
1da177e4 LT |
477 | static int get_ksymbol_mod(struct kallsym_iter *iter) |
478 | { | |
d83212d5 | 479 | int ret = module_get_kallsym(iter->pos - iter->pos_arch_end, |
74451e66 DB |
480 | &iter->value, &iter->type, |
481 | iter->name, iter->module_name, | |
482 | &iter->exported); | |
483 | if (ret < 0) { | |
484 | iter->pos_mod_end = iter->pos; | |
1da177e4 | 485 | return 0; |
74451e66 DB |
486 | } |
487 | ||
1da177e4 LT |
488 | return 1; |
489 | } | |
490 | ||
fc0ea795 AH |
491 | /* |
492 | * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace | |
493 | * purposes. In that case "__builtin__ftrace" is used as a module name, even | |
494 | * though "__builtin__ftrace" is not a module. | |
495 | */ | |
6171a031 SRV |
496 | static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) |
497 | { | |
498 | int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end, | |
499 | &iter->value, &iter->type, | |
500 | iter->name, iter->module_name, | |
501 | &iter->exported); | |
502 | if (ret < 0) { | |
503 | iter->pos_ftrace_mod_end = iter->pos; | |
504 | return 0; | |
505 | } | |
506 | ||
507 | return 1; | |
508 | } | |
509 | ||
74451e66 DB |
510 | static int get_ksymbol_bpf(struct kallsym_iter *iter) |
511 | { | |
d002b8bc AH |
512 | int ret; |
513 | ||
6934058d | 514 | strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN); |
74451e66 | 515 | iter->exported = 0; |
d002b8bc AH |
516 | ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, |
517 | &iter->value, &iter->type, | |
518 | iter->name); | |
519 | if (ret < 0) { | |
520 | iter->pos_bpf_end = iter->pos; | |
521 | return 0; | |
522 | } | |
523 | ||
524 | return 1; | |
525 | } | |
526 | ||
527 | /* | |
528 | * This uses "__builtin__kprobes" as a module name for symbols for pages | |
529 | * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a | |
530 | * module. | |
531 | */ | |
532 | static int get_ksymbol_kprobe(struct kallsym_iter *iter) | |
533 | { | |
534 | strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); | |
535 | iter->exported = 0; | |
536 | return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, | |
537 | &iter->value, &iter->type, | |
538 | iter->name) < 0 ? 0 : 1; | |
74451e66 DB |
539 | } |
540 | ||
1da177e4 LT |
541 | /* Returns space to next name. */ |
542 | static unsigned long get_ksymbol_core(struct kallsym_iter *iter) | |
543 | { | |
544 | unsigned off = iter->nameoff; | |
545 | ||
ea07890a | 546 | iter->module_name[0] = '\0'; |
2213e9a6 | 547 | iter->value = kallsyms_sym_address(iter->pos); |
1da177e4 LT |
548 | |
549 | iter->type = kallsyms_get_symbol_type(off); | |
550 | ||
e3f26752 | 551 | off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); |
1da177e4 LT |
552 | |
553 | return off - iter->nameoff; | |
554 | } | |
555 | ||
556 | static void reset_iter(struct kallsym_iter *iter, loff_t new_pos) | |
557 | { | |
558 | iter->name[0] = '\0'; | |
559 | iter->nameoff = get_symbol_offset(new_pos); | |
560 | iter->pos = new_pos; | |
6171a031 | 561 | if (new_pos == 0) { |
d83212d5 | 562 | iter->pos_arch_end = 0; |
74451e66 | 563 | iter->pos_mod_end = 0; |
6171a031 | 564 | iter->pos_ftrace_mod_end = 0; |
d002b8bc | 565 | iter->pos_bpf_end = 0; |
6171a031 | 566 | } |
74451e66 DB |
567 | } |
568 | ||
b9667942 AH |
569 | /* |
570 | * The end position (last + 1) of each additional kallsyms section is recorded | |
571 | * in iter->pos_..._end as each section is added, and so can be used to | |
572 | * determine which get_ksymbol_...() function to call next. | |
573 | */ | |
74451e66 DB |
574 | static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) |
575 | { | |
576 | iter->pos = pos; | |
577 | ||
d83212d5 AS |
578 | if ((!iter->pos_arch_end || iter->pos_arch_end > pos) && |
579 | get_ksymbol_arch(iter)) | |
580 | return 1; | |
581 | ||
b9667942 AH |
582 | if ((!iter->pos_mod_end || iter->pos_mod_end > pos) && |
583 | get_ksymbol_mod(iter)) | |
6171a031 | 584 | return 1; |
6171a031 | 585 | |
b9667942 AH |
586 | if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) && |
587 | get_ksymbol_ftrace_mod(iter)) | |
588 | return 1; | |
74451e66 | 589 | |
d002b8bc AH |
590 | if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) && |
591 | get_ksymbol_bpf(iter)) | |
592 | return 1; | |
593 | ||
594 | return get_ksymbol_kprobe(iter); | |
1da177e4 LT |
595 | } |
596 | ||
597 | /* Returns false if pos at or past end of file. */ | |
598 | static int update_iter(struct kallsym_iter *iter, loff_t pos) | |
599 | { | |
600 | /* Module symbols can be accessed randomly. */ | |
74451e66 DB |
601 | if (pos >= kallsyms_num_syms) |
602 | return update_iter_mod(iter, pos); | |
ad6ccfad | 603 | |
1da177e4 LT |
604 | /* If we're not on the desired position, reset to new position. */ |
605 | if (pos != iter->pos) | |
606 | reset_iter(iter, pos); | |
607 | ||
608 | iter->nameoff += get_ksymbol_core(iter); | |
609 | iter->pos++; | |
610 | ||
611 | return 1; | |
612 | } | |
613 | ||
614 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) | |
615 | { | |
616 | (*pos)++; | |
617 | ||
618 | if (!update_iter(m->private, *pos)) | |
619 | return NULL; | |
620 | return p; | |
621 | } | |
622 | ||
623 | static void *s_start(struct seq_file *m, loff_t *pos) | |
624 | { | |
625 | if (!update_iter(m->private, *pos)) | |
626 | return NULL; | |
627 | return m->private; | |
628 | } | |
629 | ||
630 | static void s_stop(struct seq_file *m, void *p) | |
631 | { | |
632 | } | |
633 | ||
634 | static int s_show(struct seq_file *m, void *p) | |
635 | { | |
668533dc | 636 | void *value; |
1da177e4 LT |
637 | struct kallsym_iter *iter = m->private; |
638 | ||
ad6ccfad | 639 | /* Some debugging symbols have no name. Ignore them. */ |
1da177e4 LT |
640 | if (!iter->name[0]) |
641 | return 0; | |
642 | ||
668533dc | 643 | value = iter->show_value ? (void *)iter->value : NULL; |
c0f3ea15 | 644 | |
ea07890a AD |
645 | if (iter->module_name[0]) { |
646 | char type; | |
647 | ||
ad6ccfad MK |
648 | /* |
649 | * Label it "global" if it is exported, | |
650 | * "local" if not exported. | |
651 | */ | |
ea07890a AD |
652 | type = iter->exported ? toupper(iter->type) : |
653 | tolower(iter->type); | |
668533dc | 654 | seq_printf(m, "%px %c %s\t[%s]\n", value, |
9f36e2c4 | 655 | type, iter->name, iter->module_name); |
ea07890a | 656 | } else |
668533dc | 657 | seq_printf(m, "%px %c %s\n", value, |
9f36e2c4 | 658 | iter->type, iter->name); |
1da177e4 LT |
659 | return 0; |
660 | } | |
661 | ||
15ad7cdc | 662 | static const struct seq_operations kallsyms_op = { |
1da177e4 LT |
663 | .start = s_start, |
664 | .next = s_next, | |
665 | .stop = s_stop, | |
666 | .show = s_show | |
667 | }; | |
668 | ||
c0f3ea15 LT |
669 | static inline int kallsyms_for_perf(void) |
670 | { | |
671 | #ifdef CONFIG_PERF_EVENTS | |
672 | extern int sysctl_perf_event_paranoid; | |
673 | if (sysctl_perf_event_paranoid <= 1) | |
674 | return 1; | |
675 | #endif | |
676 | return 0; | |
677 | } | |
678 | ||
679 | /* | |
680 | * We show kallsyms information even to normal users if we've enabled | |
681 | * kernel profiling and are explicitly not paranoid (so kptr_restrict | |
682 | * is clear, and sysctl_perf_event_paranoid isn't set). | |
683 | * | |
684 | * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to | |
685 | * block even that). | |
686 | */ | |
16025184 | 687 | bool kallsyms_show_value(const struct cred *cred) |
c0f3ea15 LT |
688 | { |
689 | switch (kptr_restrict) { | |
690 | case 0: | |
691 | if (kallsyms_for_perf()) | |
16025184 | 692 | return true; |
df561f66 | 693 | fallthrough; |
c0f3ea15 | 694 | case 1: |
16025184 KC |
695 | if (security_capable(cred, &init_user_ns, CAP_SYSLOG, |
696 | CAP_OPT_NOAUDIT) == 0) | |
697 | return true; | |
df561f66 | 698 | fallthrough; |
c0f3ea15 | 699 | default: |
16025184 | 700 | return false; |
c0f3ea15 LT |
701 | } |
702 | } | |
703 | ||
1da177e4 LT |
704 | static int kallsyms_open(struct inode *inode, struct file *file) |
705 | { | |
ad6ccfad MK |
706 | /* |
707 | * We keep iterator in m->private, since normal case is to | |
1da177e4 | 708 | * s_start from where we left off, so we avoid doing |
ad6ccfad MK |
709 | * using get_symbol_offset for every symbol. |
710 | */ | |
1da177e4 | 711 | struct kallsym_iter *iter; |
0049f26a | 712 | iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); |
1da177e4 LT |
713 | if (!iter) |
714 | return -ENOMEM; | |
715 | reset_iter(iter, 0); | |
716 | ||
16025184 KC |
717 | /* |
718 | * Instead of checking this on every s_show() call, cache | |
719 | * the result here at open time. | |
720 | */ | |
721 | iter->show_value = kallsyms_show_value(file->f_cred); | |
0049f26a | 722 | return 0; |
1da177e4 LT |
723 | } |
724 | ||
67fc4e0c JW |
725 | #ifdef CONFIG_KGDB_KDB |
726 | const char *kdb_walk_kallsyms(loff_t *pos) | |
727 | { | |
728 | static struct kallsym_iter kdb_walk_kallsyms_iter; | |
729 | if (*pos == 0) { | |
730 | memset(&kdb_walk_kallsyms_iter, 0, | |
731 | sizeof(kdb_walk_kallsyms_iter)); | |
732 | reset_iter(&kdb_walk_kallsyms_iter, 0); | |
733 | } | |
734 | while (1) { | |
735 | if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) | |
736 | return NULL; | |
737 | ++*pos; | |
738 | /* Some debugging symbols have no name. Ignore them. */ | |
739 | if (kdb_walk_kallsyms_iter.name[0]) | |
740 | return kdb_walk_kallsyms_iter.name; | |
741 | } | |
742 | } | |
743 | #endif /* CONFIG_KGDB_KDB */ | |
744 | ||
97a32539 AD |
745 | static const struct proc_ops kallsyms_proc_ops = { |
746 | .proc_open = kallsyms_open, | |
747 | .proc_read = seq_read, | |
748 | .proc_lseek = seq_lseek, | |
749 | .proc_release = seq_release_private, | |
1da177e4 LT |
750 | }; |
751 | ||
752 | static int __init kallsyms_init(void) | |
753 | { | |
97a32539 | 754 | proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); |
1da177e4 LT |
755 | return 0; |
756 | } | |
ad6ccfad | 757 | device_initcall(kallsyms_init); |