1 // SPDX-License-Identifier: GPL-2.0-only
3 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
5 * Rewritten and vastly simplified by Rusty Russell for in-kernel
7 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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)
15 #include <linux/kallsyms.h>
16 #include <linux/init.h>
17 #include <linux/seq_file.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/kprobes.h>
28 #include <linux/build_bug.h>
29 #include <linux/compiler.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bsearch.h>
35 * These will be re-linked against their real values
36 * during the second link stage.
38 extern const unsigned long kallsyms_addresses[] __weak;
39 extern const int kallsyms_offsets[] __weak;
40 extern const u8 kallsyms_names[] __weak;
43 * Tell the compiler that the count isn't in the small data section if the arch
46 extern const unsigned int kallsyms_num_syms
47 __section(".rodata") __attribute__((weak));
49 extern const unsigned long kallsyms_relative_base
50 __section(".rodata") __attribute__((weak));
52 extern const char kallsyms_token_table[] __weak;
53 extern const u16 kallsyms_token_index[] __weak;
55 extern const unsigned int kallsyms_markers[] __weak;
58 * Expand a compressed symbol data into the resulting uncompressed string,
59 * if uncompressed string is too long (>= maxlen), it will be truncated,
60 * given the offset to where the symbol is in the compressed stream.
62 static unsigned int kallsyms_expand_symbol(unsigned int off,
63 char *result, size_t maxlen)
65 int len, skipped_first = 0;
69 /* Get the compressed symbol length from the first symbol byte. */
70 data = &kallsyms_names[off];
75 * Update the offset to return the offset for the next symbol on
76 * the compressed stream.
81 * For every byte on the compressed symbol data, copy the table
82 * entry for that byte.
85 tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
106 /* Return to offset to the next symbol. */
111 * Get symbol type information. This is encoded as a single char at the
112 * beginning of the symbol name.
114 static char kallsyms_get_symbol_type(unsigned int off)
117 * Get just the first code, look it up in the token table,
118 * and return the first char from this token.
120 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
125 * Find the offset on the compressed stream given and index in the
128 static unsigned int get_symbol_offset(unsigned long pos)
134 * Use the closest marker we have. We have markers every 256 positions,
135 * so that should be close enough.
137 name = &kallsyms_names[kallsyms_markers[pos >> 8]];
140 * Sequentially scan all the symbols up to the point we're searching
141 * for. Every symbol is stored in a [<len>][<len> bytes of data] format,
142 * so we just need to add the len to the current pointer for every
143 * symbol we wish to skip.
145 for (i = 0; i < (pos & 0xFF); i++)
146 name = name + (*name) + 1;
148 return name - kallsyms_names;
151 static unsigned long kallsyms_sym_address(int idx)
153 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
154 return kallsyms_addresses[idx];
156 /* values are unsigned offsets if --absolute-percpu is not in effect */
157 if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU))
158 return kallsyms_relative_base + (u32)kallsyms_offsets[idx];
160 /* ...otherwise, positive offsets are absolute values */
161 if (kallsyms_offsets[idx] >= 0)
162 return kallsyms_offsets[idx];
164 /* ...and negative offsets are relative to kallsyms_relative_base - 1 */
165 return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
168 static bool cleanup_symbol_name(char *s)
172 if (!IS_ENABLED(CONFIG_LTO_CLANG))
176 * LLVM appends various suffixes for local functions and variables that
177 * must be promoted to global scope as part of LTO. This can break
178 * hooking of static functions with kprobes. '.' is not a valid
179 * character in an identifier in C. Suffixes observed:
180 * - foo.llvm.[0-9a-f]+
182 * - foo.[0-9a-f]+.cfi_jt
184 res = strchr(s, '.');
190 if (!IS_ENABLED(CONFIG_CFI_CLANG) ||
191 !IS_ENABLED(CONFIG_LTO_CLANG_THIN) ||
192 CONFIG_CLANG_VERSION >= 130000)
196 * Prior to LLVM 13, the following suffixes were observed when thinLTO
197 * and CFI are both enabled:
200 res = strrchr(s, '$');
209 /* Lookup the address for this symbol. Returns 0 if not found. */
210 unsigned long kallsyms_lookup_name(const char *name)
212 char namebuf[KSYM_NAME_LEN];
216 /* Skip the search for empty string. */
220 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
221 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
223 if (strcmp(namebuf, name) == 0)
224 return kallsyms_sym_address(i);
226 if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
227 return kallsyms_sym_address(i);
229 return module_kallsyms_lookup_name(name);
233 * Iterate over all symbols in vmlinux. For symbols from modules use
234 * module_kallsyms_on_each_symbol instead.
236 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
240 char namebuf[KSYM_NAME_LEN];
245 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
246 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
247 ret = fn(data, namebuf, NULL, kallsyms_sym_address(i));
255 static unsigned long get_symbol_pos(unsigned long addr,
256 unsigned long *symbolsize,
257 unsigned long *offset)
259 unsigned long symbol_start = 0, symbol_end = 0;
260 unsigned long i, low, high, mid;
262 /* This kernel should never had been booted. */
263 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
264 BUG_ON(!kallsyms_addresses);
266 BUG_ON(!kallsyms_offsets);
268 /* Do a binary search on the sorted kallsyms_addresses array. */
270 high = kallsyms_num_syms;
272 while (high - low > 1) {
273 mid = low + (high - low) / 2;
274 if (kallsyms_sym_address(mid) <= addr)
281 * Search for the first aliased symbol. Aliased
282 * symbols are symbols with the same address.
284 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
287 symbol_start = kallsyms_sym_address(low);
289 /* Search for next non-aliased symbol. */
290 for (i = low + 1; i < kallsyms_num_syms; i++) {
291 if (kallsyms_sym_address(i) > symbol_start) {
292 symbol_end = kallsyms_sym_address(i);
297 /* If we found no next symbol, we use the end of the section. */
299 if (is_kernel_inittext(addr))
300 symbol_end = (unsigned long)_einittext;
301 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
302 symbol_end = (unsigned long)_end;
304 symbol_end = (unsigned long)_etext;
308 *symbolsize = symbol_end - symbol_start;
310 *offset = addr - symbol_start;
316 * Lookup an address but don't bother to find any names.
318 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
319 unsigned long *offset)
321 char namebuf[KSYM_NAME_LEN];
323 if (is_ksym_addr(addr)) {
324 get_symbol_pos(addr, symbolsize, offset);
327 return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) ||
328 !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
331 static const char *kallsyms_lookup_buildid(unsigned long addr,
332 unsigned long *symbolsize,
333 unsigned long *offset, char **modname,
334 const unsigned char **modbuildid, char *namebuf)
338 namebuf[KSYM_NAME_LEN - 1] = 0;
341 if (is_ksym_addr(addr)) {
344 pos = get_symbol_pos(addr, symbolsize, offset);
346 kallsyms_expand_symbol(get_symbol_offset(pos),
347 namebuf, KSYM_NAME_LEN);
357 /* See if it's in a module or a BPF JITed image. */
358 ret = module_address_lookup(addr, symbolsize, offset,
359 modname, modbuildid, namebuf);
361 ret = bpf_address_lookup(addr, symbolsize,
362 offset, modname, namebuf);
365 ret = ftrace_mod_address_lookup(addr, symbolsize,
366 offset, modname, namebuf);
369 cleanup_symbol_name(namebuf);
375 * - modname is set to NULL if it's in the kernel.
376 * - We guarantee that the returned name is valid until we reschedule even if.
377 * It resides in a module.
378 * - We also guarantee that modname will be valid until rescheduled.
380 const char *kallsyms_lookup(unsigned long addr,
381 unsigned long *symbolsize,
382 unsigned long *offset,
383 char **modname, char *namebuf)
385 return kallsyms_lookup_buildid(addr, symbolsize, offset, modname,
389 int lookup_symbol_name(unsigned long addr, char *symname)
394 symname[KSYM_NAME_LEN - 1] = '\0';
396 if (is_ksym_addr(addr)) {
399 pos = get_symbol_pos(addr, NULL, NULL);
401 kallsyms_expand_symbol(get_symbol_offset(pos),
402 symname, KSYM_NAME_LEN);
405 /* See if it's in a module. */
406 res = lookup_module_symbol_name(addr, symname);
411 cleanup_symbol_name(symname);
415 int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
416 unsigned long *offset, char *modname, char *name)
421 name[KSYM_NAME_LEN - 1] = '\0';
423 if (is_ksym_addr(addr)) {
426 pos = get_symbol_pos(addr, size, offset);
428 kallsyms_expand_symbol(get_symbol_offset(pos),
429 name, KSYM_NAME_LEN);
433 /* See if it's in a module. */
434 res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
439 cleanup_symbol_name(name);
443 /* Look up a kernel symbol and return it in a text buffer. */
444 static int __sprint_symbol(char *buffer, unsigned long address,
445 int symbol_offset, int add_offset, int add_buildid)
448 const unsigned char *buildid;
450 unsigned long offset, size;
453 address += symbol_offset;
454 name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid,
457 return sprintf(buffer, "0x%lx", address - symbol_offset);
460 strcpy(buffer, name);
461 len = strlen(buffer);
462 offset -= symbol_offset;
465 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
468 len += sprintf(buffer + len, " [%s", modname);
469 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
470 if (add_buildid && buildid) {
471 /* build ID should match length of sprintf */
472 #if IS_ENABLED(CONFIG_MODULES)
473 static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
475 len += sprintf(buffer + len, " %20phN", buildid);
478 len += sprintf(buffer + len, "]");
485 * sprint_symbol - Look up a kernel symbol and return it in a text buffer
486 * @buffer: buffer to be stored
487 * @address: address to lookup
489 * This function looks up a kernel symbol with @address and stores its name,
490 * offset, size and module name to @buffer if possible. If no symbol was found,
491 * just saves its @address as is.
493 * This function returns the number of bytes stored in @buffer.
495 int sprint_symbol(char *buffer, unsigned long address)
497 return __sprint_symbol(buffer, address, 0, 1, 0);
499 EXPORT_SYMBOL_GPL(sprint_symbol);
502 * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer
503 * @buffer: buffer to be stored
504 * @address: address to lookup
506 * This function looks up a kernel symbol with @address and stores its name,
507 * offset, size, module name and module build ID to @buffer if possible. If no
508 * symbol was found, just saves its @address as is.
510 * This function returns the number of bytes stored in @buffer.
512 int sprint_symbol_build_id(char *buffer, unsigned long address)
514 return __sprint_symbol(buffer, address, 0, 1, 1);
516 EXPORT_SYMBOL_GPL(sprint_symbol_build_id);
519 * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
520 * @buffer: buffer to be stored
521 * @address: address to lookup
523 * This function looks up a kernel symbol with @address and stores its name
524 * and module name to @buffer if possible. If no symbol was found, just saves
525 * its @address as is.
527 * This function returns the number of bytes stored in @buffer.
529 int sprint_symbol_no_offset(char *buffer, unsigned long address)
531 return __sprint_symbol(buffer, address, 0, 0, 0);
533 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
536 * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
537 * @buffer: buffer to be stored
538 * @address: address to lookup
540 * This function is for stack backtrace and does the same thing as
541 * sprint_symbol() but with modified/decreased @address. If there is a
542 * tail-call to the function marked "noreturn", gcc optimized out code after
543 * the call so that the stack-saved return address could point outside of the
544 * caller. This function ensures that kallsyms will find the original caller
545 * by decreasing @address.
547 * This function returns the number of bytes stored in @buffer.
549 int sprint_backtrace(char *buffer, unsigned long address)
551 return __sprint_symbol(buffer, address, -1, 1, 0);
555 * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
556 * @buffer: buffer to be stored
557 * @address: address to lookup
559 * This function is for stack backtrace and does the same thing as
560 * sprint_symbol() but with modified/decreased @address. If there is a
561 * tail-call to the function marked "noreturn", gcc optimized out code after
562 * the call so that the stack-saved return address could point outside of the
563 * caller. This function ensures that kallsyms will find the original caller
564 * by decreasing @address. This function also appends the module build ID to
565 * the @buffer if @address is within a kernel module.
567 * This function returns the number of bytes stored in @buffer.
569 int sprint_backtrace_build_id(char *buffer, unsigned long address)
571 return __sprint_symbol(buffer, address, -1, 1, 1);
574 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
575 struct kallsym_iter {
579 loff_t pos_ftrace_mod_end;
582 unsigned int nameoff; /* If iterating in core kernel symbols. */
584 char name[KSYM_NAME_LEN];
585 char module_name[MODULE_NAME_LEN];
590 int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value,
591 char *type, char *name)
596 static int get_ksymbol_arch(struct kallsym_iter *iter)
598 int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms,
599 &iter->value, &iter->type,
603 iter->pos_arch_end = iter->pos;
610 static int get_ksymbol_mod(struct kallsym_iter *iter)
612 int ret = module_get_kallsym(iter->pos - iter->pos_arch_end,
613 &iter->value, &iter->type,
614 iter->name, iter->module_name,
617 iter->pos_mod_end = iter->pos;
625 * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
626 * purposes. In that case "__builtin__ftrace" is used as a module name, even
627 * though "__builtin__ftrace" is not a module.
629 static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
631 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
632 &iter->value, &iter->type,
633 iter->name, iter->module_name,
636 iter->pos_ftrace_mod_end = iter->pos;
643 static int get_ksymbol_bpf(struct kallsym_iter *iter)
647 strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
649 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
650 &iter->value, &iter->type,
653 iter->pos_bpf_end = iter->pos;
661 * This uses "__builtin__kprobes" as a module name for symbols for pages
662 * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
665 static int get_ksymbol_kprobe(struct kallsym_iter *iter)
667 strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
669 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
670 &iter->value, &iter->type,
671 iter->name) < 0 ? 0 : 1;
674 /* Returns space to next name. */
675 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
677 unsigned off = iter->nameoff;
679 iter->module_name[0] = '\0';
680 iter->value = kallsyms_sym_address(iter->pos);
682 iter->type = kallsyms_get_symbol_type(off);
684 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
686 return off - iter->nameoff;
689 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
691 iter->name[0] = '\0';
692 iter->nameoff = get_symbol_offset(new_pos);
695 iter->pos_arch_end = 0;
696 iter->pos_mod_end = 0;
697 iter->pos_ftrace_mod_end = 0;
698 iter->pos_bpf_end = 0;
703 * The end position (last + 1) of each additional kallsyms section is recorded
704 * in iter->pos_..._end as each section is added, and so can be used to
705 * determine which get_ksymbol_...() function to call next.
707 static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
711 if ((!iter->pos_arch_end || iter->pos_arch_end > pos) &&
712 get_ksymbol_arch(iter))
715 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
716 get_ksymbol_mod(iter))
719 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
720 get_ksymbol_ftrace_mod(iter))
723 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
724 get_ksymbol_bpf(iter))
727 return get_ksymbol_kprobe(iter);
730 /* Returns false if pos at or past end of file. */
731 static int update_iter(struct kallsym_iter *iter, loff_t pos)
733 /* Module symbols can be accessed randomly. */
734 if (pos >= kallsyms_num_syms)
735 return update_iter_mod(iter, pos);
737 /* If we're not on the desired position, reset to new position. */
738 if (pos != iter->pos)
739 reset_iter(iter, pos);
741 iter->nameoff += get_ksymbol_core(iter);
747 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
751 if (!update_iter(m->private, *pos))
756 static void *s_start(struct seq_file *m, loff_t *pos)
758 if (!update_iter(m->private, *pos))
763 static void s_stop(struct seq_file *m, void *p)
767 static int s_show(struct seq_file *m, void *p)
770 struct kallsym_iter *iter = m->private;
772 /* Some debugging symbols have no name. Ignore them. */
776 value = iter->show_value ? (void *)iter->value : NULL;
778 if (iter->module_name[0]) {
782 * Label it "global" if it is exported,
783 * "local" if not exported.
785 type = iter->exported ? toupper(iter->type) :
787 seq_printf(m, "%px %c %s\t[%s]\n", value,
788 type, iter->name, iter->module_name);
790 seq_printf(m, "%px %c %s\n", value,
791 iter->type, iter->name);
795 static const struct seq_operations kallsyms_op = {
802 static inline int kallsyms_for_perf(void)
804 #ifdef CONFIG_PERF_EVENTS
805 extern int sysctl_perf_event_paranoid;
806 if (sysctl_perf_event_paranoid <= 1)
813 * We show kallsyms information even to normal users if we've enabled
814 * kernel profiling and are explicitly not paranoid (so kptr_restrict
815 * is clear, and sysctl_perf_event_paranoid isn't set).
817 * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to
820 bool kallsyms_show_value(const struct cred *cred)
822 switch (kptr_restrict) {
824 if (kallsyms_for_perf())
828 if (security_capable(cred, &init_user_ns, CAP_SYSLOG,
829 CAP_OPT_NOAUDIT) == 0)
837 static int kallsyms_open(struct inode *inode, struct file *file)
840 * We keep iterator in m->private, since normal case is to
841 * s_start from where we left off, so we avoid doing
842 * using get_symbol_offset for every symbol.
844 struct kallsym_iter *iter;
845 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
851 * Instead of checking this on every s_show() call, cache
852 * the result here at open time.
854 iter->show_value = kallsyms_show_value(file->f_cred);
858 #ifdef CONFIG_KGDB_KDB
859 const char *kdb_walk_kallsyms(loff_t *pos)
861 static struct kallsym_iter kdb_walk_kallsyms_iter;
863 memset(&kdb_walk_kallsyms_iter, 0,
864 sizeof(kdb_walk_kallsyms_iter));
865 reset_iter(&kdb_walk_kallsyms_iter, 0);
868 if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
871 /* Some debugging symbols have no name. Ignore them. */
872 if (kdb_walk_kallsyms_iter.name[0])
873 return kdb_walk_kallsyms_iter.name;
876 #endif /* CONFIG_KGDB_KDB */
878 static const struct proc_ops kallsyms_proc_ops = {
879 .proc_open = kallsyms_open,
880 .proc_read = seq_read,
881 .proc_lseek = seq_lseek,
882 .proc_release = seq_release_private,
885 static int __init kallsyms_init(void)
887 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
890 device_initcall(kallsyms_init);