Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* Rewritten and vastly simplified by Rusty Russell for in-kernel |
| 3 | * module loader: |
| 4 | * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation |
| 5 | */ |
| 6 | #ifndef _LINUX_KALLSYMS_H |
| 7 | #define _LINUX_KALLSYMS_H |
| 8 | |
Adrian Bunk | 40e48ee | 2007-07-07 00:54:09 +0200 | [diff] [blame] | 9 | #include <linux/errno.h> |
Vegard Nossum | 2711b79 | 2008-07-25 01:45:56 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Kamalesh Babulal | 5a75983 | 2007-11-05 14:50:55 -0800 | [diff] [blame] | 11 | #include <linux/stddef.h> |
Sergey Senozhatsky | 04b8eb7 | 2017-12-06 13:36:49 +0900 | [diff] [blame^] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/module.h> |
| 14 | |
| 15 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 17 | #define KSYM_NAME_LEN 128 |
| 18 | #define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s]") + (KSYM_NAME_LEN - 1) + \ |
| 19 | 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 516fb7f | 2017-11-12 18:44:23 -0800 | [diff] [blame] | 21 | #ifndef CONFIG_64BIT |
| 22 | # define KALLSYM_FMT "%08lx" |
| 23 | #else |
| 24 | # define KALLSYM_FMT "%016lx" |
| 25 | #endif |
| 26 | |
Anders Kaseorg | 75a6661 | 2008-12-05 19:03:58 -0500 | [diff] [blame] | 27 | struct module; |
| 28 | |
Sergey Senozhatsky | 04b8eb7 | 2017-12-06 13:36:49 +0900 | [diff] [blame^] | 29 | static inline int is_kernel_inittext(unsigned long addr) |
| 30 | { |
| 31 | if (addr >= (unsigned long)_sinittext |
| 32 | && addr <= (unsigned long)_einittext) |
| 33 | return 1; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static inline int is_kernel_text(unsigned long addr) |
| 38 | { |
| 39 | if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) || |
| 40 | arch_is_kernel_text(addr)) |
| 41 | return 1; |
| 42 | return in_gate_area_no_mm(addr); |
| 43 | } |
| 44 | |
| 45 | static inline int is_kernel(unsigned long addr) |
| 46 | { |
| 47 | if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end) |
| 48 | return 1; |
| 49 | return in_gate_area_no_mm(addr); |
| 50 | } |
| 51 | |
| 52 | static inline int is_ksym_addr(unsigned long addr) |
| 53 | { |
| 54 | if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) |
| 55 | return is_kernel(addr); |
| 56 | |
| 57 | return is_kernel_text(addr) || is_kernel_inittext(addr); |
| 58 | } |
| 59 | |
| 60 | static inline void *dereference_symbol_descriptor(void *ptr) |
| 61 | { |
| 62 | #ifdef HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR |
| 63 | struct module *mod; |
| 64 | |
| 65 | ptr = dereference_kernel_function_descriptor(ptr); |
| 66 | if (is_ksym_addr((unsigned long)ptr)) |
| 67 | return ptr; |
| 68 | |
| 69 | preempt_disable(); |
| 70 | mod = __module_address((unsigned long)ptr); |
| 71 | preempt_enable(); |
| 72 | |
| 73 | if (mod) |
| 74 | ptr = dereference_module_function_descriptor(mod, ptr); |
| 75 | #endif |
| 76 | return ptr; |
| 77 | } |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #ifdef CONFIG_KALLSYMS |
| 80 | /* Lookup the address for a symbol. Returns 0 if not found. */ |
| 81 | unsigned long kallsyms_lookup_name(const char *name); |
| 82 | |
Anders Kaseorg | 75a6661 | 2008-12-05 19:03:58 -0500 | [diff] [blame] | 83 | /* Call a function on each kallsyms symbol in the core kernel */ |
| 84 | int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, |
| 85 | unsigned long), |
| 86 | void *data); |
| 87 | |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 88 | extern int kallsyms_lookup_size_offset(unsigned long addr, |
| 89 | unsigned long *symbolsize, |
| 90 | unsigned long *offset); |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* Lookup an address. modname is set to NULL if it's in the kernel. */ |
| 93 | const char *kallsyms_lookup(unsigned long addr, |
| 94 | unsigned long *symbolsize, |
| 95 | unsigned long *offset, |
| 96 | char **modname, char *namebuf); |
| 97 | |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 98 | /* Look up a kernel symbol and return it in a text buffer. */ |
| 99 | extern int sprint_symbol(char *buffer, unsigned long address); |
Stephen Boyd | 4796dd2 | 2012-05-29 15:07:33 -0700 | [diff] [blame] | 100 | extern int sprint_symbol_no_offset(char *buffer, unsigned long address); |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 101 | extern int sprint_backtrace(char *buffer, unsigned long address); |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 102 | |
| 103 | /* Look up a kernel symbol and print it to the kernel messages. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | extern void __print_symbol(const char *fmt, unsigned long address); |
| 105 | |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 106 | int lookup_symbol_name(unsigned long addr, char *symname); |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 107 | int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name); |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 108 | |
Arnd Bergmann | e4a8ca3 | 2017-11-13 17:51:00 +0100 | [diff] [blame] | 109 | /* How and when do we show kallsyms values? */ |
| 110 | extern int kallsyms_show_value(void); |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #else /* !CONFIG_KALLSYMS */ |
| 113 | |
| 114 | static inline unsigned long kallsyms_lookup_name(const char *name) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
Anders Kaseorg | 75a6661 | 2008-12-05 19:03:58 -0500 | [diff] [blame] | 119 | static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, |
| 120 | struct module *, |
| 121 | unsigned long), |
| 122 | void *data) |
| 123 | { |
| 124 | return 0; |
| 125 | } |
| 126 | |
Franck Bui-Huu | ffc5089 | 2006-10-03 01:13:48 -0700 | [diff] [blame] | 127 | static inline int kallsyms_lookup_size_offset(unsigned long addr, |
| 128 | unsigned long *symbolsize, |
| 129 | unsigned long *offset) |
| 130 | { |
| 131 | return 0; |
| 132 | } |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static inline const char *kallsyms_lookup(unsigned long addr, |
| 135 | unsigned long *symbolsize, |
| 136 | unsigned long *offset, |
| 137 | char **modname, char *namebuf) |
| 138 | { |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Robert Peterson | 42e3808 | 2007-04-30 15:09:48 -0700 | [diff] [blame] | 142 | static inline int sprint_symbol(char *buffer, unsigned long addr) |
| 143 | { |
| 144 | *buffer = '\0'; |
| 145 | return 0; |
| 146 | } |
| 147 | |
Stephen Boyd | 4796dd2 | 2012-05-29 15:07:33 -0700 | [diff] [blame] | 148 | static inline int sprint_symbol_no_offset(char *buffer, unsigned long addr) |
| 149 | { |
| 150 | *buffer = '\0'; |
| 151 | return 0; |
| 152 | } |
| 153 | |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 154 | static inline int sprint_backtrace(char *buffer, unsigned long addr) |
| 155 | { |
| 156 | *buffer = '\0'; |
| 157 | return 0; |
| 158 | } |
| 159 | |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 160 | static inline int lookup_symbol_name(unsigned long addr, char *symname) |
| 161 | { |
| 162 | return -ERANGE; |
| 163 | } |
| 164 | |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 165 | static inline int lookup_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name) |
| 166 | { |
| 167 | return -ERANGE; |
| 168 | } |
| 169 | |
Arnd Bergmann | e4a8ca3 | 2017-11-13 17:51:00 +0100 | [diff] [blame] | 170 | static inline int kallsyms_show_value(void) |
| 171 | { |
| 172 | return false; |
| 173 | } |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* Stupid that this does nothing, but I didn't create this mess. */ |
| 176 | #define __print_symbol(fmt, addr) |
| 177 | #endif /*CONFIG_KALLSYMS*/ |
| 178 | |
| 179 | /* This macro allows us to keep printk typechecking */ |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 180 | static __printf(1, 2) |
| 181 | void __check_printsym_format(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Heiko Carstens | b02454f | 2006-07-03 00:24:06 -0700 | [diff] [blame] | 185 | static inline void print_symbol(const char *fmt, unsigned long addr) |
| 186 | { |
| 187 | __check_printsym_format(fmt, ""); |
| 188 | __print_symbol(fmt, (unsigned long) |
| 189 | __builtin_extract_return_addr((void *)addr)); |
| 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Vegard Nossum | 2711b79 | 2008-07-25 01:45:56 -0700 | [diff] [blame] | 192 | static inline void print_ip_sym(unsigned long ip) |
| 193 | { |
Vegard Nossum | 3f1712ba | 2008-07-29 22:33:32 -0700 | [diff] [blame] | 194 | printk("[<%p>] %pS\n", (void *) ip, (void *) ip); |
Vegard Nossum | 2711b79 | 2008-07-25 01:45:56 -0700 | [diff] [blame] | 195 | } |
Heiko Carstens | 8d8fdf5 | 2006-07-03 00:24:25 -0700 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | #endif /*_LINUX_KALLSYMS_H*/ |