Merge tag 'gpio-updates-for-v5.13-v2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / kernel / kallsyms.c
CommitLineData
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 34extern const unsigned long kallsyms_addresses[] __weak;
2213e9a6 35extern const int kallsyms_offsets[] __weak;
52f5684c 36extern 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 42extern const unsigned int kallsyms_num_syms
33def849 43__section(".rodata") __attribute__((weak));
9e6c1e63 44
2213e9a6 45extern const unsigned long kallsyms_relative_base
33def849 46__section(".rodata") __attribute__((weak));
2213e9a6 47
cde26a6e 48extern const char kallsyms_token_table[] __weak;
52f5684c 49extern const u16 kallsyms_token_index[] __weak;
1da177e4 50
80ffbaa5 51extern 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
58static 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
98tail:
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
110static 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
124static 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
147static 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
8b8e6b5d
ST
164#if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN)
165/*
166 * LLVM appends a hash to static function names when ThinLTO and CFI are
167 * both enabled, i.e. foo() becomes foo$707af9a22804d33c81801f27dcfe489b.
168 * This causes confusion and potentially breaks user space tools, so we
169 * strip the suffix from expanded symbol names.
170 */
171static inline bool cleanup_symbol_name(char *s)
172{
173 char *res;
174
175 res = strrchr(s, '$');
176 if (res)
177 *res = '\0';
178
179 return res != NULL;
180}
181#else
182static inline bool cleanup_symbol_name(char *s) { return false; }
183#endif
184
1da177e4
LT
185/* Lookup the address for this symbol. Returns 0 if not found. */
186unsigned long kallsyms_lookup_name(const char *name)
187{
9281acea 188 char namebuf[KSYM_NAME_LEN];
1da177e4
LT
189 unsigned long i;
190 unsigned int off;
191
192 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
e3f26752 193 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
1da177e4
LT
194
195 if (strcmp(namebuf, name) == 0)
2213e9a6 196 return kallsyms_sym_address(i);
8b8e6b5d
ST
197
198 if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
199 return kallsyms_sym_address(i);
1da177e4
LT
200 }
201 return module_kallsyms_lookup_name(name);
202}
1da177e4 203
3e355205 204#ifdef CONFIG_LIVEPATCH
013c1667
CH
205/*
206 * Iterate over all symbols in vmlinux. For symbols from modules use
207 * module_kallsyms_on_each_symbol instead.
208 */
75a66614
AK
209int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
210 unsigned long),
211 void *data)
212{
213 char namebuf[KSYM_NAME_LEN];
214 unsigned long i;
215 unsigned int off;
216 int ret;
217
218 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
e3f26752 219 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
2213e9a6 220 ret = fn(data, namebuf, NULL, kallsyms_sym_address(i));
75a66614
AK
221 if (ret != 0)
222 return ret;
223 }
013c1667 224 return 0;
75a66614 225}
3e355205 226#endif /* CONFIG_LIVEPATCH */
75a66614 227
ffc50891
FBH
228static unsigned long get_symbol_pos(unsigned long addr,
229 unsigned long *symbolsize,
230 unsigned long *offset)
231{
232 unsigned long symbol_start = 0, symbol_end = 0;
233 unsigned long i, low, high, mid;
234
2ea03891 235 /* This kernel should never had been booted. */
2213e9a6
AB
236 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
237 BUG_ON(!kallsyms_addresses);
238 else
239 BUG_ON(!kallsyms_offsets);
2ea03891 240
ad6ccfad 241 /* Do a binary search on the sorted kallsyms_addresses array. */
ffc50891
FBH
242 low = 0;
243 high = kallsyms_num_syms;
244
245 while (high - low > 1) {
2fc9c4e1 246 mid = low + (high - low) / 2;
2213e9a6 247 if (kallsyms_sym_address(mid) <= addr)
ffc50891
FBH
248 low = mid;
249 else
250 high = mid;
251 }
252
253 /*
ad6ccfad
MK
254 * Search for the first aliased symbol. Aliased
255 * symbols are symbols with the same address.
ffc50891 256 */
2213e9a6 257 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
ffc50891
FBH
258 --low;
259
2213e9a6 260 symbol_start = kallsyms_sym_address(low);
ffc50891 261
ad6ccfad 262 /* Search for next non-aliased symbol. */
ffc50891 263 for (i = low + 1; i < kallsyms_num_syms; i++) {
2213e9a6
AB
264 if (kallsyms_sym_address(i) > symbol_start) {
265 symbol_end = kallsyms_sym_address(i);
ffc50891
FBH
266 break;
267 }
268 }
269
ad6ccfad 270 /* If we found no next symbol, we use the end of the section. */
ffc50891
FBH
271 if (!symbol_end) {
272 if (is_kernel_inittext(addr))
273 symbol_end = (unsigned long)_einittext;
63b23e2c 274 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
ffc50891
FBH
275 symbol_end = (unsigned long)_end;
276 else
277 symbol_end = (unsigned long)_etext;
278 }
279
ffb45122
AD
280 if (symbolsize)
281 *symbolsize = symbol_end - symbol_start;
282 if (offset)
283 *offset = addr - symbol_start;
ffc50891
FBH
284
285 return low;
286}
287
288/*
289 * Lookup an address but don't bother to find any names.
290 */
291int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
292 unsigned long *offset)
293{
6dd06c9f 294 char namebuf[KSYM_NAME_LEN];
74451e66 295
2a1a3fa0
MZ
296 if (is_ksym_addr(addr)) {
297 get_symbol_pos(addr, symbolsize, offset);
298 return 1;
299 }
74451e66
DB
300 return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
301 !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
ffc50891
FBH
302}
303
1da177e4
LT
304/*
305 * Lookup an address
ad6ccfad
MK
306 * - modname is set to NULL if it's in the kernel.
307 * - We guarantee that the returned name is valid until we reschedule even if.
308 * It resides in a module.
309 * - We also guarantee that modname will be valid until rescheduled.
1da177e4
LT
310 */
311const char *kallsyms_lookup(unsigned long addr,
312 unsigned long *symbolsize,
313 unsigned long *offset,
314 char **modname, char *namebuf)
315{
74451e66
DB
316 const char *ret;
317
9281acea 318 namebuf[KSYM_NAME_LEN - 1] = 0;
1da177e4
LT
319 namebuf[0] = 0;
320
ffc50891
FBH
321 if (is_ksym_addr(addr)) {
322 unsigned long pos;
1da177e4 323
ffc50891 324 pos = get_symbol_pos(addr, symbolsize, offset);
1da177e4 325 /* Grab name */
e3f26752
CG
326 kallsyms_expand_symbol(get_symbol_offset(pos),
327 namebuf, KSYM_NAME_LEN);
7a74fc49
KM
328 if (modname)
329 *modname = NULL;
8b8e6b5d
ST
330
331 ret = namebuf;
332 goto found;
1da177e4
LT
333 }
334
74451e66
DB
335 /* See if it's in a module or a BPF JITed image. */
336 ret = module_address_lookup(addr, symbolsize, offset,
337 modname, namebuf);
338 if (!ret)
339 ret = bpf_address_lookup(addr, symbolsize,
340 offset, modname, namebuf);
aba4b5c2
SRV
341
342 if (!ret)
343 ret = ftrace_mod_address_lookup(addr, symbolsize,
344 offset, modname, namebuf);
8b8e6b5d
ST
345
346found:
347 cleanup_symbol_name(namebuf);
74451e66 348 return ret;
1da177e4
LT
349}
350
9d65cb4a
AD
351int lookup_symbol_name(unsigned long addr, char *symname)
352{
8b8e6b5d
ST
353 int res;
354
9d65cb4a 355 symname[0] = '\0';
9281acea 356 symname[KSYM_NAME_LEN - 1] = '\0';
9d65cb4a
AD
357
358 if (is_ksym_addr(addr)) {
359 unsigned long pos;
360
361 pos = get_symbol_pos(addr, NULL, NULL);
362 /* Grab name */
e3f26752
CG
363 kallsyms_expand_symbol(get_symbol_offset(pos),
364 symname, KSYM_NAME_LEN);
8b8e6b5d 365 goto found;
9d65cb4a 366 }
ad6ccfad 367 /* See if it's in a module. */
8b8e6b5d
ST
368 res = lookup_module_symbol_name(addr, symname);
369 if (res)
370 return res;
371
372found:
373 cleanup_symbol_name(symname);
374 return 0;
9d65cb4a
AD
375}
376
a5c43dae
AD
377int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
378 unsigned long *offset, char *modname, char *name)
379{
8b8e6b5d
ST
380 int res;
381
a5c43dae 382 name[0] = '\0';
9281acea 383 name[KSYM_NAME_LEN - 1] = '\0';
a5c43dae
AD
384
385 if (is_ksym_addr(addr)) {
386 unsigned long pos;
387
388 pos = get_symbol_pos(addr, size, offset);
389 /* Grab name */
e3f26752
CG
390 kallsyms_expand_symbol(get_symbol_offset(pos),
391 name, KSYM_NAME_LEN);
a5c43dae 392 modname[0] = '\0';
8b8e6b5d 393 goto found;
a5c43dae 394 }
ad6ccfad 395 /* See if it's in a module. */
8b8e6b5d
ST
396 res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
397 if (res)
398 return res;
399
400found:
401 cleanup_symbol_name(name);
402 return 0;
a5c43dae
AD
403}
404
42e38083 405/* Look up a kernel symbol and return it in a text buffer. */
0f77a8d3 406static int __sprint_symbol(char *buffer, unsigned long address,
4796dd20 407 int symbol_offset, int add_offset)
1da177e4
LT
408{
409 char *modname;
410 const char *name;
411 unsigned long offset, size;
966c8c12 412 int len;
1da177e4 413
0f77a8d3 414 address += symbol_offset;
966c8c12 415 name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
1da177e4 416 if (!name)
b86280aa 417 return sprintf(buffer, "0x%lx", address - symbol_offset);
19769b76 418
966c8c12
HD
419 if (name != buffer)
420 strcpy(buffer, name);
421 len = strlen(buffer);
0f77a8d3 422 offset -= symbol_offset;
966c8c12 423
4796dd20
SB
424 if (add_offset)
425 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
426
19769b76 427 if (modname)
4796dd20 428 len += sprintf(buffer + len, " [%s]", modname);
966c8c12
HD
429
430 return len;
42e38083 431}
0f77a8d3
NK
432
433/**
434 * sprint_symbol - Look up a kernel symbol and return it in a text buffer
435 * @buffer: buffer to be stored
436 * @address: address to lookup
437 *
438 * This function looks up a kernel symbol with @address and stores its name,
439 * offset, size and module name to @buffer if possible. If no symbol was found,
440 * just saves its @address as is.
441 *
442 * This function returns the number of bytes stored in @buffer.
443 */
444int sprint_symbol(char *buffer, unsigned long address)
445{
4796dd20 446 return __sprint_symbol(buffer, address, 0, 1);
0f77a8d3 447}
ad6ccfad 448EXPORT_SYMBOL_GPL(sprint_symbol);
42e38083 449
4796dd20
SB
450/**
451 * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
452 * @buffer: buffer to be stored
453 * @address: address to lookup
454 *
455 * This function looks up a kernel symbol with @address and stores its name
456 * and module name to @buffer if possible. If no symbol was found, just saves
457 * its @address as is.
458 *
459 * This function returns the number of bytes stored in @buffer.
460 */
461int sprint_symbol_no_offset(char *buffer, unsigned long address)
462{
463 return __sprint_symbol(buffer, address, 0, 0);
464}
465EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
466
0f77a8d3
NK
467/**
468 * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
469 * @buffer: buffer to be stored
470 * @address: address to lookup
471 *
472 * This function is for stack backtrace and does the same thing as
473 * sprint_symbol() but with modified/decreased @address. If there is a
474 * tail-call to the function marked "noreturn", gcc optimized out code after
475 * the call so that the stack-saved return address could point outside of the
476 * caller. This function ensures that kallsyms will find the original caller
477 * by decreasing @address.
478 *
479 * This function returns the number of bytes stored in @buffer.
480 */
481int sprint_backtrace(char *buffer, unsigned long address)
482{
4796dd20 483 return __sprint_symbol(buffer, address, -1, 1);
0f77a8d3
NK
484}
485
1da177e4 486/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
ad6ccfad 487struct kallsym_iter {
1da177e4 488 loff_t pos;
d83212d5 489 loff_t pos_arch_end;
74451e66 490 loff_t pos_mod_end;
6171a031 491 loff_t pos_ftrace_mod_end;
d002b8bc 492 loff_t pos_bpf_end;
1da177e4 493 unsigned long value;
ad6ccfad 494 unsigned int nameoff; /* If iterating in core kernel symbols. */
1da177e4 495 char type;
9281acea
TH
496 char name[KSYM_NAME_LEN];
497 char module_name[MODULE_NAME_LEN];
ea07890a 498 int exported;
c0f3ea15 499 int show_value;
1da177e4
LT
500};
501
d83212d5
AS
502int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value,
503 char *type, char *name)
504{
505 return -EINVAL;
506}
507
508static int get_ksymbol_arch(struct kallsym_iter *iter)
509{
510 int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms,
511 &iter->value, &iter->type,
512 iter->name);
513
514 if (ret < 0) {
515 iter->pos_arch_end = iter->pos;
516 return 0;
517 }
518
519 return 1;
520}
521
1da177e4
LT
522static int get_ksymbol_mod(struct kallsym_iter *iter)
523{
d83212d5 524 int ret = module_get_kallsym(iter->pos - iter->pos_arch_end,
74451e66
DB
525 &iter->value, &iter->type,
526 iter->name, iter->module_name,
527 &iter->exported);
528 if (ret < 0) {
529 iter->pos_mod_end = iter->pos;
1da177e4 530 return 0;
74451e66
DB
531 }
532
1da177e4
LT
533 return 1;
534}
535
fc0ea795
AH
536/*
537 * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
538 * purposes. In that case "__builtin__ftrace" is used as a module name, even
539 * though "__builtin__ftrace" is not a module.
540 */
6171a031
SRV
541static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
542{
543 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
544 &iter->value, &iter->type,
545 iter->name, iter->module_name,
546 &iter->exported);
547 if (ret < 0) {
548 iter->pos_ftrace_mod_end = iter->pos;
549 return 0;
550 }
551
552 return 1;
553}
554
74451e66
DB
555static int get_ksymbol_bpf(struct kallsym_iter *iter)
556{
d002b8bc
AH
557 int ret;
558
6934058d 559 strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
74451e66 560 iter->exported = 0;
d002b8bc
AH
561 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
562 &iter->value, &iter->type,
563 iter->name);
564 if (ret < 0) {
565 iter->pos_bpf_end = iter->pos;
566 return 0;
567 }
568
569 return 1;
570}
571
572/*
573 * This uses "__builtin__kprobes" as a module name for symbols for pages
574 * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
575 * module.
576 */
577static int get_ksymbol_kprobe(struct kallsym_iter *iter)
578{
579 strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
580 iter->exported = 0;
581 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
582 &iter->value, &iter->type,
583 iter->name) < 0 ? 0 : 1;
74451e66
DB
584}
585
1da177e4
LT
586/* Returns space to next name. */
587static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
588{
589 unsigned off = iter->nameoff;
590
ea07890a 591 iter->module_name[0] = '\0';
2213e9a6 592 iter->value = kallsyms_sym_address(iter->pos);
1da177e4
LT
593
594 iter->type = kallsyms_get_symbol_type(off);
595
e3f26752 596 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
1da177e4
LT
597
598 return off - iter->nameoff;
599}
600
601static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
602{
603 iter->name[0] = '\0';
604 iter->nameoff = get_symbol_offset(new_pos);
605 iter->pos = new_pos;
6171a031 606 if (new_pos == 0) {
d83212d5 607 iter->pos_arch_end = 0;
74451e66 608 iter->pos_mod_end = 0;
6171a031 609 iter->pos_ftrace_mod_end = 0;
d002b8bc 610 iter->pos_bpf_end = 0;
6171a031 611 }
74451e66
DB
612}
613
b9667942
AH
614/*
615 * The end position (last + 1) of each additional kallsyms section is recorded
616 * in iter->pos_..._end as each section is added, and so can be used to
617 * determine which get_ksymbol_...() function to call next.
618 */
74451e66
DB
619static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
620{
621 iter->pos = pos;
622
d83212d5
AS
623 if ((!iter->pos_arch_end || iter->pos_arch_end > pos) &&
624 get_ksymbol_arch(iter))
625 return 1;
626
b9667942
AH
627 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
628 get_ksymbol_mod(iter))
6171a031 629 return 1;
6171a031 630
b9667942
AH
631 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
632 get_ksymbol_ftrace_mod(iter))
633 return 1;
74451e66 634
d002b8bc
AH
635 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
636 get_ksymbol_bpf(iter))
637 return 1;
638
639 return get_ksymbol_kprobe(iter);
1da177e4
LT
640}
641
642/* Returns false if pos at or past end of file. */
643static int update_iter(struct kallsym_iter *iter, loff_t pos)
644{
645 /* Module symbols can be accessed randomly. */
74451e66
DB
646 if (pos >= kallsyms_num_syms)
647 return update_iter_mod(iter, pos);
ad6ccfad 648
1da177e4
LT
649 /* If we're not on the desired position, reset to new position. */
650 if (pos != iter->pos)
651 reset_iter(iter, pos);
652
653 iter->nameoff += get_ksymbol_core(iter);
654 iter->pos++;
655
656 return 1;
657}
658
659static void *s_next(struct seq_file *m, void *p, loff_t *pos)
660{
661 (*pos)++;
662
663 if (!update_iter(m->private, *pos))
664 return NULL;
665 return p;
666}
667
668static void *s_start(struct seq_file *m, loff_t *pos)
669{
670 if (!update_iter(m->private, *pos))
671 return NULL;
672 return m->private;
673}
674
675static void s_stop(struct seq_file *m, void *p)
676{
677}
678
679static int s_show(struct seq_file *m, void *p)
680{
668533dc 681 void *value;
1da177e4
LT
682 struct kallsym_iter *iter = m->private;
683
ad6ccfad 684 /* Some debugging symbols have no name. Ignore them. */
1da177e4
LT
685 if (!iter->name[0])
686 return 0;
687
668533dc 688 value = iter->show_value ? (void *)iter->value : NULL;
c0f3ea15 689
ea07890a
AD
690 if (iter->module_name[0]) {
691 char type;
692
ad6ccfad
MK
693 /*
694 * Label it "global" if it is exported,
695 * "local" if not exported.
696 */
ea07890a
AD
697 type = iter->exported ? toupper(iter->type) :
698 tolower(iter->type);
668533dc 699 seq_printf(m, "%px %c %s\t[%s]\n", value,
9f36e2c4 700 type, iter->name, iter->module_name);
ea07890a 701 } else
668533dc 702 seq_printf(m, "%px %c %s\n", value,
9f36e2c4 703 iter->type, iter->name);
1da177e4
LT
704 return 0;
705}
706
15ad7cdc 707static const struct seq_operations kallsyms_op = {
1da177e4
LT
708 .start = s_start,
709 .next = s_next,
710 .stop = s_stop,
711 .show = s_show
712};
713
c0f3ea15
LT
714static inline int kallsyms_for_perf(void)
715{
716#ifdef CONFIG_PERF_EVENTS
717 extern int sysctl_perf_event_paranoid;
718 if (sysctl_perf_event_paranoid <= 1)
719 return 1;
720#endif
721 return 0;
722}
723
724/*
725 * We show kallsyms information even to normal users if we've enabled
726 * kernel profiling and are explicitly not paranoid (so kptr_restrict
727 * is clear, and sysctl_perf_event_paranoid isn't set).
728 *
729 * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to
730 * block even that).
731 */
16025184 732bool kallsyms_show_value(const struct cred *cred)
c0f3ea15
LT
733{
734 switch (kptr_restrict) {
735 case 0:
736 if (kallsyms_for_perf())
16025184 737 return true;
df561f66 738 fallthrough;
c0f3ea15 739 case 1:
16025184
KC
740 if (security_capable(cred, &init_user_ns, CAP_SYSLOG,
741 CAP_OPT_NOAUDIT) == 0)
742 return true;
df561f66 743 fallthrough;
c0f3ea15 744 default:
16025184 745 return false;
c0f3ea15
LT
746 }
747}
748
1da177e4
LT
749static int kallsyms_open(struct inode *inode, struct file *file)
750{
ad6ccfad
MK
751 /*
752 * We keep iterator in m->private, since normal case is to
1da177e4 753 * s_start from where we left off, so we avoid doing
ad6ccfad
MK
754 * using get_symbol_offset for every symbol.
755 */
1da177e4 756 struct kallsym_iter *iter;
0049f26a 757 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
1da177e4
LT
758 if (!iter)
759 return -ENOMEM;
760 reset_iter(iter, 0);
761
16025184
KC
762 /*
763 * Instead of checking this on every s_show() call, cache
764 * the result here at open time.
765 */
766 iter->show_value = kallsyms_show_value(file->f_cred);
0049f26a 767 return 0;
1da177e4
LT
768}
769
67fc4e0c
JW
770#ifdef CONFIG_KGDB_KDB
771const char *kdb_walk_kallsyms(loff_t *pos)
772{
773 static struct kallsym_iter kdb_walk_kallsyms_iter;
774 if (*pos == 0) {
775 memset(&kdb_walk_kallsyms_iter, 0,
776 sizeof(kdb_walk_kallsyms_iter));
777 reset_iter(&kdb_walk_kallsyms_iter, 0);
778 }
779 while (1) {
780 if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
781 return NULL;
782 ++*pos;
783 /* Some debugging symbols have no name. Ignore them. */
784 if (kdb_walk_kallsyms_iter.name[0])
785 return kdb_walk_kallsyms_iter.name;
786 }
787}
788#endif /* CONFIG_KGDB_KDB */
789
97a32539
AD
790static const struct proc_ops kallsyms_proc_ops = {
791 .proc_open = kallsyms_open,
792 .proc_read = seq_read,
793 .proc_lseek = seq_lseek,
794 .proc_release = seq_release_private,
1da177e4
LT
795};
796
797static int __init kallsyms_init(void)
798{
97a32539 799 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
1da177e4
LT
800 return 0;
801}
ad6ccfad 802device_initcall(kallsyms_init);