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/compiler.h>
31 * These will be re-linked against their real values
32 * during the second link stage.
34 extern const unsigned long kallsyms_addresses[] __weak;
35 extern const int kallsyms_offsets[] __weak;
36 extern const u8 kallsyms_names[] __weak;
39 * Tell the compiler that the count isn't in the small data section if the arch
42 extern const unsigned int kallsyms_num_syms
43 __section(".rodata") __attribute__((weak));
45 extern const unsigned long kallsyms_relative_base
46 __section(".rodata") __attribute__((weak));
48 extern const char kallsyms_token_table[] __weak;
49 extern const u16 kallsyms_token_index[] __weak;
51 extern const unsigned int kallsyms_markers[] __weak;
54 * Expand a compressed symbol data into the resulting uncompressed string,
55 * if uncompressed string is too long (>= maxlen), it will be truncated,
56 * given the offset to where the symbol is in the compressed stream.
58 static unsigned int kallsyms_expand_symbol(unsigned int off,
59 char *result, size_t maxlen)
61 int len, skipped_first = 0;
65 /* Get the compressed symbol length from the first symbol byte. */
66 data = &kallsyms_names[off];
71 * Update the offset to return the offset for the next symbol on
72 * the compressed stream.
77 * For every byte on the compressed symbol data, copy the table
78 * entry for that byte.
81 tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
102 /* Return to offset to the next symbol. */
107 * Get symbol type information. This is encoded as a single char at the
108 * beginning of the symbol name.
110 static char kallsyms_get_symbol_type(unsigned int off)
113 * Get just the first code, look it up in the token table,
114 * and return the first char from this token.
116 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
121 * Find the offset on the compressed stream given and index in the
124 static unsigned int get_symbol_offset(unsigned long pos)
130 * Use the closest marker we have. We have markers every 256 positions,
131 * so that should be close enough.
133 name = &kallsyms_names[kallsyms_markers[pos >> 8]];
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.
141 for (i = 0; i < (pos & 0xFF); i++)
142 name = name + (*name) + 1;
144 return name - kallsyms_names;
147 static unsigned long kallsyms_sym_address(int idx)
149 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
150 return kallsyms_addresses[idx];
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];
156 /* ...otherwise, positive offsets are absolute values */
157 if (kallsyms_offsets[idx] >= 0)
158 return kallsyms_offsets[idx];
160 /* ...and negative offsets are relative to kallsyms_relative_base - 1 */
161 return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
164 #if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN)
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.
171 static inline bool cleanup_symbol_name(char *s)
175 res = strrchr(s, '$');
182 static inline bool cleanup_symbol_name(char *s) { return false; }
185 /* Lookup the address for this symbol. Returns 0 if not found. */
186 unsigned long kallsyms_lookup_name(const char *name)
188 char namebuf[KSYM_NAME_LEN];
192 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
193 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
195 if (strcmp(namebuf, name) == 0)
196 return kallsyms_sym_address(i);
198 if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
199 return kallsyms_sym_address(i);
201 return module_kallsyms_lookup_name(name);
204 #ifdef CONFIG_LIVEPATCH
206 * Iterate over all symbols in vmlinux. For symbols from modules use
207 * module_kallsyms_on_each_symbol instead.
209 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
213 char namebuf[KSYM_NAME_LEN];
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));
226 #endif /* CONFIG_LIVEPATCH */
228 static unsigned long get_symbol_pos(unsigned long addr,
229 unsigned long *symbolsize,
230 unsigned long *offset)
232 unsigned long symbol_start = 0, symbol_end = 0;
233 unsigned long i, low, high, mid;
235 /* This kernel should never had been booted. */
236 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
237 BUG_ON(!kallsyms_addresses);
239 BUG_ON(!kallsyms_offsets);
241 /* Do a binary search on the sorted kallsyms_addresses array. */
243 high = kallsyms_num_syms;
245 while (high - low > 1) {
246 mid = low + (high - low) / 2;
247 if (kallsyms_sym_address(mid) <= addr)
254 * Search for the first aliased symbol. Aliased
255 * symbols are symbols with the same address.
257 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
260 symbol_start = kallsyms_sym_address(low);
262 /* Search for next non-aliased symbol. */
263 for (i = low + 1; i < kallsyms_num_syms; i++) {
264 if (kallsyms_sym_address(i) > symbol_start) {
265 symbol_end = kallsyms_sym_address(i);
270 /* If we found no next symbol, we use the end of the section. */
272 if (is_kernel_inittext(addr))
273 symbol_end = (unsigned long)_einittext;
274 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
275 symbol_end = (unsigned long)_end;
277 symbol_end = (unsigned long)_etext;
281 *symbolsize = symbol_end - symbol_start;
283 *offset = addr - symbol_start;
289 * Lookup an address but don't bother to find any names.
291 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
292 unsigned long *offset)
294 char namebuf[KSYM_NAME_LEN];
296 if (is_ksym_addr(addr)) {
297 get_symbol_pos(addr, symbolsize, offset);
300 return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
301 !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
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.
311 const char *kallsyms_lookup(unsigned long addr,
312 unsigned long *symbolsize,
313 unsigned long *offset,
314 char **modname, char *namebuf)
318 namebuf[KSYM_NAME_LEN - 1] = 0;
321 if (is_ksym_addr(addr)) {
324 pos = get_symbol_pos(addr, symbolsize, offset);
326 kallsyms_expand_symbol(get_symbol_offset(pos),
327 namebuf, KSYM_NAME_LEN);
335 /* See if it's in a module or a BPF JITed image. */
336 ret = module_address_lookup(addr, symbolsize, offset,
339 ret = bpf_address_lookup(addr, symbolsize,
340 offset, modname, namebuf);
343 ret = ftrace_mod_address_lookup(addr, symbolsize,
344 offset, modname, namebuf);
347 cleanup_symbol_name(namebuf);
351 int lookup_symbol_name(unsigned long addr, char *symname)
356 symname[KSYM_NAME_LEN - 1] = '\0';
358 if (is_ksym_addr(addr)) {
361 pos = get_symbol_pos(addr, NULL, NULL);
363 kallsyms_expand_symbol(get_symbol_offset(pos),
364 symname, KSYM_NAME_LEN);
367 /* See if it's in a module. */
368 res = lookup_module_symbol_name(addr, symname);
373 cleanup_symbol_name(symname);
377 int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
378 unsigned long *offset, char *modname, char *name)
383 name[KSYM_NAME_LEN - 1] = '\0';
385 if (is_ksym_addr(addr)) {
388 pos = get_symbol_pos(addr, size, offset);
390 kallsyms_expand_symbol(get_symbol_offset(pos),
391 name, KSYM_NAME_LEN);
395 /* See if it's in a module. */
396 res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
401 cleanup_symbol_name(name);
405 /* Look up a kernel symbol and return it in a text buffer. */
406 static int __sprint_symbol(char *buffer, unsigned long address,
407 int symbol_offset, int add_offset)
411 unsigned long offset, size;
414 address += symbol_offset;
415 name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
417 return sprintf(buffer, "0x%lx", address - symbol_offset);
420 strcpy(buffer, name);
421 len = strlen(buffer);
422 offset -= symbol_offset;
425 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
428 len += sprintf(buffer + len, " [%s]", modname);
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
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.
442 * This function returns the number of bytes stored in @buffer.
444 int sprint_symbol(char *buffer, unsigned long address)
446 return __sprint_symbol(buffer, address, 0, 1);
448 EXPORT_SYMBOL_GPL(sprint_symbol);
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
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.
459 * This function returns the number of bytes stored in @buffer.
461 int sprint_symbol_no_offset(char *buffer, unsigned long address)
463 return __sprint_symbol(buffer, address, 0, 0);
465 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
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
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.
479 * This function returns the number of bytes stored in @buffer.
481 int sprint_backtrace(char *buffer, unsigned long address)
483 return __sprint_symbol(buffer, address, -1, 1);
486 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
487 struct kallsym_iter {
491 loff_t pos_ftrace_mod_end;
494 unsigned int nameoff; /* If iterating in core kernel symbols. */
496 char name[KSYM_NAME_LEN];
497 char module_name[MODULE_NAME_LEN];
502 int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value,
503 char *type, char *name)
508 static int get_ksymbol_arch(struct kallsym_iter *iter)
510 int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms,
511 &iter->value, &iter->type,
515 iter->pos_arch_end = iter->pos;
522 static int get_ksymbol_mod(struct kallsym_iter *iter)
524 int ret = module_get_kallsym(iter->pos - iter->pos_arch_end,
525 &iter->value, &iter->type,
526 iter->name, iter->module_name,
529 iter->pos_mod_end = iter->pos;
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.
541 static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
543 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
544 &iter->value, &iter->type,
545 iter->name, iter->module_name,
548 iter->pos_ftrace_mod_end = iter->pos;
555 static int get_ksymbol_bpf(struct kallsym_iter *iter)
559 strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
561 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
562 &iter->value, &iter->type,
565 iter->pos_bpf_end = iter->pos;
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
577 static int get_ksymbol_kprobe(struct kallsym_iter *iter)
579 strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
581 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
582 &iter->value, &iter->type,
583 iter->name) < 0 ? 0 : 1;
586 /* Returns space to next name. */
587 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
589 unsigned off = iter->nameoff;
591 iter->module_name[0] = '\0';
592 iter->value = kallsyms_sym_address(iter->pos);
594 iter->type = kallsyms_get_symbol_type(off);
596 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
598 return off - iter->nameoff;
601 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
603 iter->name[0] = '\0';
604 iter->nameoff = get_symbol_offset(new_pos);
607 iter->pos_arch_end = 0;
608 iter->pos_mod_end = 0;
609 iter->pos_ftrace_mod_end = 0;
610 iter->pos_bpf_end = 0;
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.
619 static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
623 if ((!iter->pos_arch_end || iter->pos_arch_end > pos) &&
624 get_ksymbol_arch(iter))
627 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
628 get_ksymbol_mod(iter))
631 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
632 get_ksymbol_ftrace_mod(iter))
635 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
636 get_ksymbol_bpf(iter))
639 return get_ksymbol_kprobe(iter);
642 /* Returns false if pos at or past end of file. */
643 static int update_iter(struct kallsym_iter *iter, loff_t pos)
645 /* Module symbols can be accessed randomly. */
646 if (pos >= kallsyms_num_syms)
647 return update_iter_mod(iter, pos);
649 /* If we're not on the desired position, reset to new position. */
650 if (pos != iter->pos)
651 reset_iter(iter, pos);
653 iter->nameoff += get_ksymbol_core(iter);
659 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
663 if (!update_iter(m->private, *pos))
668 static void *s_start(struct seq_file *m, loff_t *pos)
670 if (!update_iter(m->private, *pos))
675 static void s_stop(struct seq_file *m, void *p)
679 static int s_show(struct seq_file *m, void *p)
682 struct kallsym_iter *iter = m->private;
684 /* Some debugging symbols have no name. Ignore them. */
688 value = iter->show_value ? (void *)iter->value : NULL;
690 if (iter->module_name[0]) {
694 * Label it "global" if it is exported,
695 * "local" if not exported.
697 type = iter->exported ? toupper(iter->type) :
699 seq_printf(m, "%px %c %s\t[%s]\n", value,
700 type, iter->name, iter->module_name);
702 seq_printf(m, "%px %c %s\n", value,
703 iter->type, iter->name);
707 static const struct seq_operations kallsyms_op = {
714 static inline int kallsyms_for_perf(void)
716 #ifdef CONFIG_PERF_EVENTS
717 extern int sysctl_perf_event_paranoid;
718 if (sysctl_perf_event_paranoid <= 1)
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).
729 * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to
732 bool kallsyms_show_value(const struct cred *cred)
734 switch (kptr_restrict) {
736 if (kallsyms_for_perf())
740 if (security_capable(cred, &init_user_ns, CAP_SYSLOG,
741 CAP_OPT_NOAUDIT) == 0)
749 static int kallsyms_open(struct inode *inode, struct file *file)
752 * We keep iterator in m->private, since normal case is to
753 * s_start from where we left off, so we avoid doing
754 * using get_symbol_offset for every symbol.
756 struct kallsym_iter *iter;
757 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
763 * Instead of checking this on every s_show() call, cache
764 * the result here at open time.
766 iter->show_value = kallsyms_show_value(file->f_cred);
770 #ifdef CONFIG_KGDB_KDB
771 const char *kdb_walk_kallsyms(loff_t *pos)
773 static struct kallsym_iter kdb_walk_kallsyms_iter;
775 memset(&kdb_walk_kallsyms_iter, 0,
776 sizeof(kdb_walk_kallsyms_iter));
777 reset_iter(&kdb_walk_kallsyms_iter, 0);
780 if (!update_iter(&kdb_walk_kallsyms_iter, *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;
788 #endif /* CONFIG_KGDB_KDB */
790 static 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,
797 static int __init kallsyms_init(void)
799 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
802 device_initcall(kallsyms_init);