Commit | Line | Data |
---|---|---|
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> |
9294523e | 28 | #include <linux/build_bug.h> |
52f5684c | 29 | #include <linux/compiler.h> |
9294523e SB |
30 | #include <linux/module.h> |
31 | #include <linux/kernel.h> | |
bed0d9a5 | 32 | #include <linux/bsearch.h> |
1da177e4 | 33 | |
ad6ccfad MK |
34 | /* |
35 | * These will be re-linked against their real values | |
36 | * during the second link stage. | |
37 | */ | |
52f5684c | 38 | extern const unsigned long kallsyms_addresses[] __weak; |
2213e9a6 | 39 | extern const int kallsyms_offsets[] __weak; |
52f5684c | 40 | extern const u8 kallsyms_names[] __weak; |
1da177e4 | 41 | |
ad6ccfad MK |
42 | /* |
43 | * Tell the compiler that the count isn't in the small data section if the arch | |
44 | * has one (eg: FRV). | |
9e6c1e63 | 45 | */ |
80ffbaa5 | 46 | extern const unsigned int kallsyms_num_syms |
33def849 | 47 | __section(".rodata") __attribute__((weak)); |
9e6c1e63 | 48 | |
2213e9a6 | 49 | extern const unsigned long kallsyms_relative_base |
33def849 | 50 | __section(".rodata") __attribute__((weak)); |
2213e9a6 | 51 | |
cde26a6e | 52 | extern const char kallsyms_token_table[] __weak; |
52f5684c | 53 | extern const u16 kallsyms_token_index[] __weak; |
1da177e4 | 54 | |
80ffbaa5 | 55 | extern const unsigned int kallsyms_markers[] __weak; |
1da177e4 | 56 | |
ad6ccfad MK |
57 | /* |
58 | * Expand a compressed symbol data into the resulting uncompressed string, | |
e3f26752 | 59 | * if uncompressed string is too long (>= maxlen), it will be truncated, |
ad6ccfad MK |
60 | * given the offset to where the symbol is in the compressed stream. |
61 | */ | |
e3f26752 CG |
62 | static unsigned int kallsyms_expand_symbol(unsigned int off, |
63 | char *result, size_t maxlen) | |
1da177e4 LT |
64 | { |
65 | int len, skipped_first = 0; | |
cde26a6e MY |
66 | const char *tptr; |
67 | const u8 *data; | |
1da177e4 | 68 | |
ad6ccfad | 69 | /* Get the compressed symbol length from the first symbol byte. */ |
1da177e4 LT |
70 | data = &kallsyms_names[off]; |
71 | len = *data; | |
72 | data++; | |
73 | ||
ad6ccfad MK |
74 | /* |
75 | * Update the offset to return the offset for the next symbol on | |
76 | * the compressed stream. | |
77 | */ | |
1da177e4 LT |
78 | off += len + 1; |
79 | ||
ad6ccfad MK |
80 | /* |
81 | * For every byte on the compressed symbol data, copy the table | |
82 | * entry for that byte. | |
83 | */ | |
84 | while (len) { | |
85 | tptr = &kallsyms_token_table[kallsyms_token_index[*data]]; | |
1da177e4 LT |
86 | data++; |
87 | len--; | |
88 | ||
89 | while (*tptr) { | |
ad6ccfad | 90 | if (skipped_first) { |
e3f26752 CG |
91 | if (maxlen <= 1) |
92 | goto tail; | |
1da177e4 LT |
93 | *result = *tptr; |
94 | result++; | |
e3f26752 | 95 | maxlen--; |
1da177e4 LT |
96 | } else |
97 | skipped_first = 1; | |
98 | tptr++; | |
99 | } | |
100 | } | |
101 | ||
e3f26752 CG |
102 | tail: |
103 | if (maxlen) | |
104 | *result = '\0'; | |
1da177e4 | 105 | |
ad6ccfad | 106 | /* Return to offset to the next symbol. */ |
1da177e4 LT |
107 | return off; |
108 | } | |
109 | ||
ad6ccfad MK |
110 | /* |
111 | * Get symbol type information. This is encoded as a single char at the | |
112 | * beginning of the symbol name. | |
113 | */ | |
1da177e4 LT |
114 | static char kallsyms_get_symbol_type(unsigned int off) |
115 | { | |
ad6ccfad MK |
116 | /* |
117 | * Get just the first code, look it up in the token table, | |
118 | * and return the first char from this token. | |
119 | */ | |
120 | return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]]; | |
1da177e4 LT |
121 | } |
122 | ||
123 | ||
ad6ccfad MK |
124 | /* |
125 | * Find the offset on the compressed stream given and index in the | |
126 | * kallsyms array. | |
127 | */ | |
1da177e4 LT |
128 | static unsigned int get_symbol_offset(unsigned long pos) |
129 | { | |
aad09470 | 130 | const u8 *name; |
1da177e4 LT |
131 | int i; |
132 | ||
ad6ccfad MK |
133 | /* |
134 | * Use the closest marker we have. We have markers every 256 positions, | |
135 | * so that should be close enough. | |
136 | */ | |
137 | name = &kallsyms_names[kallsyms_markers[pos >> 8]]; | |
1da177e4 | 138 | |
ad6ccfad MK |
139 | /* |
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. | |
144 | */ | |
145 | for (i = 0; i < (pos & 0xFF); i++) | |
1da177e4 LT |
146 | name = name + (*name) + 1; |
147 | ||
148 | return name - kallsyms_names; | |
149 | } | |
150 | ||
2213e9a6 AB |
151 | static unsigned long kallsyms_sym_address(int idx) |
152 | { | |
153 | if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) | |
154 | return kallsyms_addresses[idx]; | |
155 | ||
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]; | |
159 | ||
160 | /* ...otherwise, positive offsets are absolute values */ | |
161 | if (kallsyms_offsets[idx] >= 0) | |
162 | return kallsyms_offsets[idx]; | |
163 | ||
164 | /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ | |
165 | return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; | |
166 | } | |
167 | ||
6eb4bd92 | 168 | static bool cleanup_symbol_name(char *s) |
8b8e6b5d ST |
169 | { |
170 | char *res; | |
171 | ||
6eb4bd92 ND |
172 | if (!IS_ENABLED(CONFIG_LTO_CLANG)) |
173 | return false; | |
174 | ||
175 | /* | |
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]+ | |
181 | * - foo.[0-9a-f]+ | |
182 | * - foo.[0-9a-f]+.cfi_jt | |
183 | */ | |
184 | res = strchr(s, '.'); | |
185 | if (res) { | |
186 | *res = '\0'; | |
187 | return true; | |
188 | } | |
189 | ||
190 | if (!IS_ENABLED(CONFIG_CFI_CLANG) || | |
191 | !IS_ENABLED(CONFIG_LTO_CLANG_THIN) || | |
192 | CONFIG_CLANG_VERSION >= 130000) | |
193 | return false; | |
194 | ||
195 | /* | |
196 | * Prior to LLVM 13, the following suffixes were observed when thinLTO | |
197 | * and CFI are both enabled: | |
198 | * - foo$[0-9]+ | |
199 | */ | |
8b8e6b5d | 200 | res = strrchr(s, '$'); |
6eb4bd92 | 201 | if (res) { |
8b8e6b5d | 202 | *res = '\0'; |
6eb4bd92 ND |
203 | return true; |
204 | } | |
8b8e6b5d | 205 | |
6eb4bd92 | 206 | return false; |
8b8e6b5d | 207 | } |
8b8e6b5d | 208 | |
1da177e4 LT |
209 | /* Lookup the address for this symbol. Returns 0 if not found. */ |
210 | unsigned long kallsyms_lookup_name(const char *name) | |
211 | { | |
9281acea | 212 | char namebuf[KSYM_NAME_LEN]; |
1da177e4 LT |
213 | unsigned long i; |
214 | unsigned int off; | |
215 | ||
aecf489f JO |
216 | /* Skip the search for empty string. */ |
217 | if (!*name) | |
218 | return 0; | |
219 | ||
1da177e4 | 220 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { |
e3f26752 | 221 | off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); |
1da177e4 LT |
222 | |
223 | if (strcmp(namebuf, name) == 0) | |
2213e9a6 | 224 | return kallsyms_sym_address(i); |
8b8e6b5d ST |
225 | |
226 | if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0) | |
227 | return kallsyms_sym_address(i); | |
1da177e4 LT |
228 | } |
229 | return module_kallsyms_lookup_name(name); | |
230 | } | |
1da177e4 | 231 | |
013c1667 CH |
232 | /* |
233 | * Iterate over all symbols in vmlinux. For symbols from modules use | |
234 | * module_kallsyms_on_each_symbol instead. | |
235 | */ | |
75a66614 AK |
236 | int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, |
237 | unsigned long), | |
238 | void *data) | |
239 | { | |
240 | char namebuf[KSYM_NAME_LEN]; | |
241 | unsigned long i; | |
242 | unsigned int off; | |
243 | int ret; | |
244 | ||
245 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { | |
e3f26752 | 246 | off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); |
2213e9a6 | 247 | ret = fn(data, namebuf, NULL, kallsyms_sym_address(i)); |
75a66614 AK |
248 | if (ret != 0) |
249 | return ret; | |
f5bdb34b | 250 | cond_resched(); |
75a66614 | 251 | } |
013c1667 | 252 | return 0; |
75a66614 | 253 | } |
75a66614 | 254 | |
ffc50891 FBH |
255 | static unsigned long get_symbol_pos(unsigned long addr, |
256 | unsigned long *symbolsize, | |
257 | unsigned long *offset) | |
258 | { | |
259 | unsigned long symbol_start = 0, symbol_end = 0; | |
260 | unsigned long i, low, high, mid; | |
261 | ||
2ea03891 | 262 | /* This kernel should never had been booted. */ |
2213e9a6 AB |
263 | if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) |
264 | BUG_ON(!kallsyms_addresses); | |
265 | else | |
266 | BUG_ON(!kallsyms_offsets); | |
2ea03891 | 267 | |
ad6ccfad | 268 | /* Do a binary search on the sorted kallsyms_addresses array. */ |
ffc50891 FBH |
269 | low = 0; |
270 | high = kallsyms_num_syms; | |
271 | ||
272 | while (high - low > 1) { | |
2fc9c4e1 | 273 | mid = low + (high - low) / 2; |
2213e9a6 | 274 | if (kallsyms_sym_address(mid) <= addr) |
ffc50891 FBH |
275 | low = mid; |
276 | else | |
277 | high = mid; | |
278 | } | |
279 | ||
280 | /* | |
ad6ccfad MK |
281 | * Search for the first aliased symbol. Aliased |
282 | * symbols are symbols with the same address. | |
ffc50891 | 283 | */ |
2213e9a6 | 284 | while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) |
ffc50891 FBH |
285 | --low; |
286 | ||
2213e9a6 | 287 | symbol_start = kallsyms_sym_address(low); |
ffc50891 | 288 | |
ad6ccfad | 289 | /* Search for next non-aliased symbol. */ |
ffc50891 | 290 | for (i = low + 1; i < kallsyms_num_syms; i++) { |
2213e9a6 AB |
291 | if (kallsyms_sym_address(i) > symbol_start) { |
292 | symbol_end = kallsyms_sym_address(i); | |
ffc50891 FBH |
293 | break; |
294 | } | |
295 | } | |
296 | ||
ad6ccfad | 297 | /* If we found no next symbol, we use the end of the section. */ |
ffc50891 FBH |
298 | if (!symbol_end) { |
299 | if (is_kernel_inittext(addr)) | |
300 | symbol_end = (unsigned long)_einittext; | |
63b23e2c | 301 | else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) |
ffc50891 FBH |
302 | symbol_end = (unsigned long)_end; |
303 | else | |
304 | symbol_end = (unsigned long)_etext; | |
305 | } | |
306 | ||
ffb45122 AD |
307 | if (symbolsize) |
308 | *symbolsize = symbol_end - symbol_start; | |
309 | if (offset) | |
310 | *offset = addr - symbol_start; | |
ffc50891 FBH |
311 | |
312 | return low; | |
313 | } | |
314 | ||
315 | /* | |
316 | * Lookup an address but don't bother to find any names. | |
317 | */ | |
318 | int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, | |
319 | unsigned long *offset) | |
320 | { | |
6dd06c9f | 321 | char namebuf[KSYM_NAME_LEN]; |
74451e66 | 322 | |
2a1a3fa0 MZ |
323 | if (is_ksym_addr(addr)) { |
324 | get_symbol_pos(addr, symbolsize, offset); | |
325 | return 1; | |
326 | } | |
9294523e | 327 | return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) || |
74451e66 | 328 | !!__bpf_address_lookup(addr, symbolsize, offset, namebuf); |
ffc50891 FBH |
329 | } |
330 | ||
9294523e SB |
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) | |
1da177e4 | 335 | { |
74451e66 DB |
336 | const char *ret; |
337 | ||
9281acea | 338 | namebuf[KSYM_NAME_LEN - 1] = 0; |
1da177e4 LT |
339 | namebuf[0] = 0; |
340 | ||
ffc50891 FBH |
341 | if (is_ksym_addr(addr)) { |
342 | unsigned long pos; | |
1da177e4 | 343 | |
ffc50891 | 344 | pos = get_symbol_pos(addr, symbolsize, offset); |
1da177e4 | 345 | /* Grab name */ |
e3f26752 CG |
346 | kallsyms_expand_symbol(get_symbol_offset(pos), |
347 | namebuf, KSYM_NAME_LEN); | |
7a74fc49 KM |
348 | if (modname) |
349 | *modname = NULL; | |
9294523e SB |
350 | if (modbuildid) |
351 | *modbuildid = NULL; | |
8b8e6b5d ST |
352 | |
353 | ret = namebuf; | |
354 | goto found; | |
1da177e4 LT |
355 | } |
356 | ||
74451e66 DB |
357 | /* See if it's in a module or a BPF JITed image. */ |
358 | ret = module_address_lookup(addr, symbolsize, offset, | |
9294523e | 359 | modname, modbuildid, namebuf); |
74451e66 DB |
360 | if (!ret) |
361 | ret = bpf_address_lookup(addr, symbolsize, | |
362 | offset, modname, namebuf); | |
aba4b5c2 SRV |
363 | |
364 | if (!ret) | |
365 | ret = ftrace_mod_address_lookup(addr, symbolsize, | |
366 | offset, modname, namebuf); | |
8b8e6b5d ST |
367 | |
368 | found: | |
369 | cleanup_symbol_name(namebuf); | |
74451e66 | 370 | return ret; |
1da177e4 LT |
371 | } |
372 | ||
9294523e SB |
373 | /* |
374 | * Lookup an address | |
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. | |
379 | */ | |
380 | const char *kallsyms_lookup(unsigned long addr, | |
381 | unsigned long *symbolsize, | |
382 | unsigned long *offset, | |
383 | char **modname, char *namebuf) | |
384 | { | |
385 | return kallsyms_lookup_buildid(addr, symbolsize, offset, modname, | |
386 | NULL, namebuf); | |
387 | } | |
388 | ||
9d65cb4a AD |
389 | int lookup_symbol_name(unsigned long addr, char *symname) |
390 | { | |
8b8e6b5d ST |
391 | int res; |
392 | ||
9d65cb4a | 393 | symname[0] = '\0'; |
9281acea | 394 | symname[KSYM_NAME_LEN - 1] = '\0'; |
9d65cb4a AD |
395 | |
396 | if (is_ksym_addr(addr)) { | |
397 | unsigned long pos; | |
398 | ||
399 | pos = get_symbol_pos(addr, NULL, NULL); | |
400 | /* Grab name */ | |
e3f26752 CG |
401 | kallsyms_expand_symbol(get_symbol_offset(pos), |
402 | symname, KSYM_NAME_LEN); | |
8b8e6b5d | 403 | goto found; |
9d65cb4a | 404 | } |
ad6ccfad | 405 | /* See if it's in a module. */ |
8b8e6b5d ST |
406 | res = lookup_module_symbol_name(addr, symname); |
407 | if (res) | |
408 | return res; | |
409 | ||
410 | found: | |
411 | cleanup_symbol_name(symname); | |
412 | return 0; | |
9d65cb4a AD |
413 | } |
414 | ||
a5c43dae AD |
415 | int lookup_symbol_attrs(unsigned long addr, unsigned long *size, |
416 | unsigned long *offset, char *modname, char *name) | |
417 | { | |
8b8e6b5d ST |
418 | int res; |
419 | ||
a5c43dae | 420 | name[0] = '\0'; |
9281acea | 421 | name[KSYM_NAME_LEN - 1] = '\0'; |
a5c43dae AD |
422 | |
423 | if (is_ksym_addr(addr)) { | |
424 | unsigned long pos; | |
425 | ||
426 | pos = get_symbol_pos(addr, size, offset); | |
427 | /* Grab name */ | |
e3f26752 CG |
428 | kallsyms_expand_symbol(get_symbol_offset(pos), |
429 | name, KSYM_NAME_LEN); | |
a5c43dae | 430 | modname[0] = '\0'; |
8b8e6b5d | 431 | goto found; |
a5c43dae | 432 | } |
ad6ccfad | 433 | /* See if it's in a module. */ |
8b8e6b5d ST |
434 | res = lookup_module_symbol_attrs(addr, size, offset, modname, name); |
435 | if (res) | |
436 | return res; | |
437 | ||
438 | found: | |
439 | cleanup_symbol_name(name); | |
440 | return 0; | |
a5c43dae AD |
441 | } |
442 | ||
42e38083 | 443 | /* Look up a kernel symbol and return it in a text buffer. */ |
0f77a8d3 | 444 | static int __sprint_symbol(char *buffer, unsigned long address, |
9294523e | 445 | int symbol_offset, int add_offset, int add_buildid) |
1da177e4 LT |
446 | { |
447 | char *modname; | |
9294523e | 448 | const unsigned char *buildid; |
1da177e4 LT |
449 | const char *name; |
450 | unsigned long offset, size; | |
966c8c12 | 451 | int len; |
1da177e4 | 452 | |
0f77a8d3 | 453 | address += symbol_offset; |
9294523e SB |
454 | name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid, |
455 | buffer); | |
1da177e4 | 456 | if (!name) |
b86280aa | 457 | return sprintf(buffer, "0x%lx", address - symbol_offset); |
19769b76 | 458 | |
966c8c12 HD |
459 | if (name != buffer) |
460 | strcpy(buffer, name); | |
461 | len = strlen(buffer); | |
0f77a8d3 | 462 | offset -= symbol_offset; |
966c8c12 | 463 | |
4796dd20 SB |
464 | if (add_offset) |
465 | len += sprintf(buffer + len, "+%#lx/%#lx", offset, size); | |
466 | ||
9294523e SB |
467 | if (modname) { |
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); | |
474 | #endif | |
475 | len += sprintf(buffer + len, " %20phN", buildid); | |
476 | } | |
477 | #endif | |
478 | len += sprintf(buffer + len, "]"); | |
479 | } | |
966c8c12 HD |
480 | |
481 | return len; | |
42e38083 | 482 | } |
0f77a8d3 NK |
483 | |
484 | /** | |
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 | |
488 | * | |
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. | |
492 | * | |
493 | * This function returns the number of bytes stored in @buffer. | |
494 | */ | |
495 | int sprint_symbol(char *buffer, unsigned long address) | |
496 | { | |
9294523e | 497 | return __sprint_symbol(buffer, address, 0, 1, 0); |
0f77a8d3 | 498 | } |
ad6ccfad | 499 | EXPORT_SYMBOL_GPL(sprint_symbol); |
42e38083 | 500 | |
9294523e SB |
501 | /** |
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 | |
505 | * | |
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. | |
509 | * | |
510 | * This function returns the number of bytes stored in @buffer. | |
511 | */ | |
512 | int sprint_symbol_build_id(char *buffer, unsigned long address) | |
513 | { | |
514 | return __sprint_symbol(buffer, address, 0, 1, 1); | |
515 | } | |
516 | EXPORT_SYMBOL_GPL(sprint_symbol_build_id); | |
517 | ||
4796dd20 SB |
518 | /** |
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 | |
522 | * | |
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. | |
526 | * | |
527 | * This function returns the number of bytes stored in @buffer. | |
528 | */ | |
529 | int sprint_symbol_no_offset(char *buffer, unsigned long address) | |
530 | { | |
9294523e | 531 | return __sprint_symbol(buffer, address, 0, 0, 0); |
4796dd20 SB |
532 | } |
533 | EXPORT_SYMBOL_GPL(sprint_symbol_no_offset); | |
534 | ||
0f77a8d3 NK |
535 | /** |
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 | |
539 | * | |
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. | |
546 | * | |
547 | * This function returns the number of bytes stored in @buffer. | |
548 | */ | |
549 | int sprint_backtrace(char *buffer, unsigned long address) | |
550 | { | |
9294523e SB |
551 | return __sprint_symbol(buffer, address, -1, 1, 0); |
552 | } | |
553 | ||
554 | /** | |
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 | |
558 | * | |
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. | |
566 | * | |
567 | * This function returns the number of bytes stored in @buffer. | |
568 | */ | |
569 | int sprint_backtrace_build_id(char *buffer, unsigned long address) | |
570 | { | |
571 | return __sprint_symbol(buffer, address, -1, 1, 1); | |
0f77a8d3 NK |
572 | } |
573 | ||
1da177e4 | 574 | /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ |
ad6ccfad | 575 | struct kallsym_iter { |
1da177e4 | 576 | loff_t pos; |
d83212d5 | 577 | loff_t pos_arch_end; |
74451e66 | 578 | loff_t pos_mod_end; |
6171a031 | 579 | loff_t pos_ftrace_mod_end; |
d002b8bc | 580 | loff_t pos_bpf_end; |
1da177e4 | 581 | unsigned long value; |
ad6ccfad | 582 | unsigned int nameoff; /* If iterating in core kernel symbols. */ |
1da177e4 | 583 | char type; |
9281acea TH |
584 | char name[KSYM_NAME_LEN]; |
585 | char module_name[MODULE_NAME_LEN]; | |
ea07890a | 586 | int exported; |
c0f3ea15 | 587 | int show_value; |
1da177e4 LT |
588 | }; |
589 | ||
d83212d5 AS |
590 | int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value, |
591 | char *type, char *name) | |
592 | { | |
593 | return -EINVAL; | |
594 | } | |
595 | ||
596 | static int get_ksymbol_arch(struct kallsym_iter *iter) | |
597 | { | |
598 | int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms, | |
599 | &iter->value, &iter->type, | |
600 | iter->name); | |
601 | ||
602 | if (ret < 0) { | |
603 | iter->pos_arch_end = iter->pos; | |
604 | return 0; | |
605 | } | |
606 | ||
607 | return 1; | |
608 | } | |
609 | ||
1da177e4 LT |
610 | static int get_ksymbol_mod(struct kallsym_iter *iter) |
611 | { | |
d83212d5 | 612 | int ret = module_get_kallsym(iter->pos - iter->pos_arch_end, |
74451e66 DB |
613 | &iter->value, &iter->type, |
614 | iter->name, iter->module_name, | |
615 | &iter->exported); | |
616 | if (ret < 0) { | |
617 | iter->pos_mod_end = iter->pos; | |
1da177e4 | 618 | return 0; |
74451e66 DB |
619 | } |
620 | ||
1da177e4 LT |
621 | return 1; |
622 | } | |
623 | ||
fc0ea795 AH |
624 | /* |
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. | |
628 | */ | |
6171a031 SRV |
629 | static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) |
630 | { | |
631 | int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end, | |
632 | &iter->value, &iter->type, | |
633 | iter->name, iter->module_name, | |
634 | &iter->exported); | |
635 | if (ret < 0) { | |
636 | iter->pos_ftrace_mod_end = iter->pos; | |
637 | return 0; | |
638 | } | |
639 | ||
640 | return 1; | |
641 | } | |
642 | ||
74451e66 DB |
643 | static int get_ksymbol_bpf(struct kallsym_iter *iter) |
644 | { | |
d002b8bc AH |
645 | int ret; |
646 | ||
6934058d | 647 | strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN); |
74451e66 | 648 | iter->exported = 0; |
d002b8bc AH |
649 | ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, |
650 | &iter->value, &iter->type, | |
651 | iter->name); | |
652 | if (ret < 0) { | |
653 | iter->pos_bpf_end = iter->pos; | |
654 | return 0; | |
655 | } | |
656 | ||
657 | return 1; | |
658 | } | |
659 | ||
660 | /* | |
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 | |
663 | * module. | |
664 | */ | |
665 | static int get_ksymbol_kprobe(struct kallsym_iter *iter) | |
666 | { | |
667 | strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); | |
668 | iter->exported = 0; | |
669 | return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, | |
670 | &iter->value, &iter->type, | |
671 | iter->name) < 0 ? 0 : 1; | |
74451e66 DB |
672 | } |
673 | ||
1da177e4 LT |
674 | /* Returns space to next name. */ |
675 | static unsigned long get_ksymbol_core(struct kallsym_iter *iter) | |
676 | { | |
677 | unsigned off = iter->nameoff; | |
678 | ||
ea07890a | 679 | iter->module_name[0] = '\0'; |
2213e9a6 | 680 | iter->value = kallsyms_sym_address(iter->pos); |
1da177e4 LT |
681 | |
682 | iter->type = kallsyms_get_symbol_type(off); | |
683 | ||
e3f26752 | 684 | off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); |
1da177e4 LT |
685 | |
686 | return off - iter->nameoff; | |
687 | } | |
688 | ||
689 | static void reset_iter(struct kallsym_iter *iter, loff_t new_pos) | |
690 | { | |
691 | iter->name[0] = '\0'; | |
692 | iter->nameoff = get_symbol_offset(new_pos); | |
693 | iter->pos = new_pos; | |
6171a031 | 694 | if (new_pos == 0) { |
d83212d5 | 695 | iter->pos_arch_end = 0; |
74451e66 | 696 | iter->pos_mod_end = 0; |
6171a031 | 697 | iter->pos_ftrace_mod_end = 0; |
d002b8bc | 698 | iter->pos_bpf_end = 0; |
6171a031 | 699 | } |
74451e66 DB |
700 | } |
701 | ||
b9667942 AH |
702 | /* |
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. | |
706 | */ | |
74451e66 DB |
707 | static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) |
708 | { | |
709 | iter->pos = pos; | |
710 | ||
d83212d5 AS |
711 | if ((!iter->pos_arch_end || iter->pos_arch_end > pos) && |
712 | get_ksymbol_arch(iter)) | |
713 | return 1; | |
714 | ||
b9667942 AH |
715 | if ((!iter->pos_mod_end || iter->pos_mod_end > pos) && |
716 | get_ksymbol_mod(iter)) | |
6171a031 | 717 | return 1; |
6171a031 | 718 | |
b9667942 AH |
719 | if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) && |
720 | get_ksymbol_ftrace_mod(iter)) | |
721 | return 1; | |
74451e66 | 722 | |
d002b8bc AH |
723 | if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) && |
724 | get_ksymbol_bpf(iter)) | |
725 | return 1; | |
726 | ||
727 | return get_ksymbol_kprobe(iter); | |
1da177e4 LT |
728 | } |
729 | ||
730 | /* Returns false if pos at or past end of file. */ | |
731 | static int update_iter(struct kallsym_iter *iter, loff_t pos) | |
732 | { | |
733 | /* Module symbols can be accessed randomly. */ | |
74451e66 DB |
734 | if (pos >= kallsyms_num_syms) |
735 | return update_iter_mod(iter, pos); | |
ad6ccfad | 736 | |
1da177e4 LT |
737 | /* If we're not on the desired position, reset to new position. */ |
738 | if (pos != iter->pos) | |
739 | reset_iter(iter, pos); | |
740 | ||
741 | iter->nameoff += get_ksymbol_core(iter); | |
742 | iter->pos++; | |
743 | ||
744 | return 1; | |
745 | } | |
746 | ||
747 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) | |
748 | { | |
749 | (*pos)++; | |
750 | ||
751 | if (!update_iter(m->private, *pos)) | |
752 | return NULL; | |
753 | return p; | |
754 | } | |
755 | ||
756 | static void *s_start(struct seq_file *m, loff_t *pos) | |
757 | { | |
758 | if (!update_iter(m->private, *pos)) | |
759 | return NULL; | |
760 | return m->private; | |
761 | } | |
762 | ||
763 | static void s_stop(struct seq_file *m, void *p) | |
764 | { | |
765 | } | |
766 | ||
767 | static int s_show(struct seq_file *m, void *p) | |
768 | { | |
668533dc | 769 | void *value; |
1da177e4 LT |
770 | struct kallsym_iter *iter = m->private; |
771 | ||
ad6ccfad | 772 | /* Some debugging symbols have no name. Ignore them. */ |
1da177e4 LT |
773 | if (!iter->name[0]) |
774 | return 0; | |
775 | ||
668533dc | 776 | value = iter->show_value ? (void *)iter->value : NULL; |
c0f3ea15 | 777 | |
ea07890a AD |
778 | if (iter->module_name[0]) { |
779 | char type; | |
780 | ||
ad6ccfad MK |
781 | /* |
782 | * Label it "global" if it is exported, | |
783 | * "local" if not exported. | |
784 | */ | |
ea07890a AD |
785 | type = iter->exported ? toupper(iter->type) : |
786 | tolower(iter->type); | |
668533dc | 787 | seq_printf(m, "%px %c %s\t[%s]\n", value, |
9f36e2c4 | 788 | type, iter->name, iter->module_name); |
ea07890a | 789 | } else |
668533dc | 790 | seq_printf(m, "%px %c %s\n", value, |
9f36e2c4 | 791 | iter->type, iter->name); |
1da177e4 LT |
792 | return 0; |
793 | } | |
794 | ||
15ad7cdc | 795 | static const struct seq_operations kallsyms_op = { |
1da177e4 LT |
796 | .start = s_start, |
797 | .next = s_next, | |
798 | .stop = s_stop, | |
799 | .show = s_show | |
800 | }; | |
801 | ||
c0f3ea15 LT |
802 | static inline int kallsyms_for_perf(void) |
803 | { | |
804 | #ifdef CONFIG_PERF_EVENTS | |
805 | extern int sysctl_perf_event_paranoid; | |
806 | if (sysctl_perf_event_paranoid <= 1) | |
807 | return 1; | |
808 | #endif | |
809 | return 0; | |
810 | } | |
811 | ||
812 | /* | |
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). | |
816 | * | |
817 | * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to | |
818 | * block even that). | |
819 | */ | |
16025184 | 820 | bool kallsyms_show_value(const struct cred *cred) |
c0f3ea15 LT |
821 | { |
822 | switch (kptr_restrict) { | |
823 | case 0: | |
824 | if (kallsyms_for_perf()) | |
16025184 | 825 | return true; |
df561f66 | 826 | fallthrough; |
c0f3ea15 | 827 | case 1: |
16025184 KC |
828 | if (security_capable(cred, &init_user_ns, CAP_SYSLOG, |
829 | CAP_OPT_NOAUDIT) == 0) | |
830 | return true; | |
df561f66 | 831 | fallthrough; |
c0f3ea15 | 832 | default: |
16025184 | 833 | return false; |
c0f3ea15 LT |
834 | } |
835 | } | |
836 | ||
1da177e4 LT |
837 | static int kallsyms_open(struct inode *inode, struct file *file) |
838 | { | |
ad6ccfad MK |
839 | /* |
840 | * We keep iterator in m->private, since normal case is to | |
1da177e4 | 841 | * s_start from where we left off, so we avoid doing |
ad6ccfad MK |
842 | * using get_symbol_offset for every symbol. |
843 | */ | |
1da177e4 | 844 | struct kallsym_iter *iter; |
0049f26a | 845 | iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); |
1da177e4 LT |
846 | if (!iter) |
847 | return -ENOMEM; | |
848 | reset_iter(iter, 0); | |
849 | ||
16025184 KC |
850 | /* |
851 | * Instead of checking this on every s_show() call, cache | |
852 | * the result here at open time. | |
853 | */ | |
854 | iter->show_value = kallsyms_show_value(file->f_cred); | |
0049f26a | 855 | return 0; |
1da177e4 LT |
856 | } |
857 | ||
67fc4e0c JW |
858 | #ifdef CONFIG_KGDB_KDB |
859 | const char *kdb_walk_kallsyms(loff_t *pos) | |
860 | { | |
861 | static struct kallsym_iter kdb_walk_kallsyms_iter; | |
862 | if (*pos == 0) { | |
863 | memset(&kdb_walk_kallsyms_iter, 0, | |
864 | sizeof(kdb_walk_kallsyms_iter)); | |
865 | reset_iter(&kdb_walk_kallsyms_iter, 0); | |
866 | } | |
867 | while (1) { | |
868 | if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) | |
869 | return NULL; | |
870 | ++*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; | |
874 | } | |
875 | } | |
876 | #endif /* CONFIG_KGDB_KDB */ | |
877 | ||
97a32539 AD |
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, | |
1da177e4 LT |
883 | }; |
884 | ||
885 | static int __init kallsyms_init(void) | |
886 | { | |
97a32539 | 887 | proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); |
1da177e4 LT |
888 | return 0; |
889 | } | |
ad6ccfad | 890 | device_initcall(kallsyms_init); |