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