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> |
647cafa2 | 33 | #include <linux/btf_ids.h> |
1da177e4 | 34 | |
71f8c155 | 35 | #include "kallsyms_internal.h" |
1da177e4 | 36 | |
ad6ccfad MK |
37 | /* |
38 | * Expand a compressed symbol data into the resulting uncompressed string, | |
e3f26752 | 39 | * if uncompressed string is too long (>= maxlen), it will be truncated, |
ad6ccfad MK |
40 | * given the offset to where the symbol is in the compressed stream. |
41 | */ | |
e3f26752 CG |
42 | static unsigned int kallsyms_expand_symbol(unsigned int off, |
43 | char *result, size_t maxlen) | |
1da177e4 LT |
44 | { |
45 | int len, skipped_first = 0; | |
cde26a6e MY |
46 | const char *tptr; |
47 | const u8 *data; | |
1da177e4 | 48 | |
ad6ccfad | 49 | /* Get the compressed symbol length from the first symbol byte. */ |
1da177e4 LT |
50 | data = &kallsyms_names[off]; |
51 | len = *data; | |
52 | data++; | |
73bbb944 MO |
53 | off++; |
54 | ||
55 | /* If MSB is 1, it is a "big" symbol, so needs an additional byte. */ | |
56 | if ((len & 0x80) != 0) { | |
57 | len = (len & 0x7F) | (*data << 7); | |
58 | data++; | |
59 | off++; | |
60 | } | |
1da177e4 | 61 | |
ad6ccfad MK |
62 | /* |
63 | * Update the offset to return the offset for the next symbol on | |
64 | * the compressed stream. | |
65 | */ | |
73bbb944 | 66 | off += len; |
1da177e4 | 67 | |
ad6ccfad MK |
68 | /* |
69 | * For every byte on the compressed symbol data, copy the table | |
70 | * entry for that byte. | |
71 | */ | |
72 | while (len) { | |
73 | tptr = &kallsyms_token_table[kallsyms_token_index[*data]]; | |
1da177e4 LT |
74 | data++; |
75 | len--; | |
76 | ||
77 | while (*tptr) { | |
ad6ccfad | 78 | if (skipped_first) { |
e3f26752 CG |
79 | if (maxlen <= 1) |
80 | goto tail; | |
1da177e4 LT |
81 | *result = *tptr; |
82 | result++; | |
e3f26752 | 83 | maxlen--; |
1da177e4 LT |
84 | } else |
85 | skipped_first = 1; | |
86 | tptr++; | |
87 | } | |
88 | } | |
89 | ||
e3f26752 CG |
90 | tail: |
91 | if (maxlen) | |
92 | *result = '\0'; | |
1da177e4 | 93 | |
ad6ccfad | 94 | /* Return to offset to the next symbol. */ |
1da177e4 LT |
95 | return off; |
96 | } | |
97 | ||
ad6ccfad MK |
98 | /* |
99 | * Get symbol type information. This is encoded as a single char at the | |
100 | * beginning of the symbol name. | |
101 | */ | |
1da177e4 LT |
102 | static char kallsyms_get_symbol_type(unsigned int off) |
103 | { | |
ad6ccfad MK |
104 | /* |
105 | * Get just the first code, look it up in the token table, | |
106 | * and return the first char from this token. | |
107 | */ | |
108 | return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]]; | |
1da177e4 LT |
109 | } |
110 | ||
111 | ||
ad6ccfad MK |
112 | /* |
113 | * Find the offset on the compressed stream given and index in the | |
114 | * kallsyms array. | |
115 | */ | |
1da177e4 LT |
116 | static unsigned int get_symbol_offset(unsigned long pos) |
117 | { | |
aad09470 | 118 | const u8 *name; |
73bbb944 | 119 | int i, len; |
1da177e4 | 120 | |
ad6ccfad MK |
121 | /* |
122 | * Use the closest marker we have. We have markers every 256 positions, | |
123 | * so that should be close enough. | |
124 | */ | |
125 | name = &kallsyms_names[kallsyms_markers[pos >> 8]]; | |
1da177e4 | 126 | |
ad6ccfad MK |
127 | /* |
128 | * Sequentially scan all the symbols up to the point we're searching | |
129 | * for. Every symbol is stored in a [<len>][<len> bytes of data] format, | |
130 | * so we just need to add the len to the current pointer for every | |
131 | * symbol we wish to skip. | |
132 | */ | |
73bbb944 MO |
133 | for (i = 0; i < (pos & 0xFF); i++) { |
134 | len = *name; | |
135 | ||
136 | /* | |
137 | * If MSB is 1, it is a "big" symbol, so we need to look into | |
138 | * the next byte (and skip it, too). | |
139 | */ | |
140 | if ((len & 0x80) != 0) | |
141 | len = ((len & 0x7F) | (name[1] << 7)) + 1; | |
142 | ||
143 | name = name + len + 1; | |
144 | } | |
1da177e4 LT |
145 | |
146 | return name - kallsyms_names; | |
147 | } | |
148 | ||
2213e9a6 AB |
149 | static unsigned long kallsyms_sym_address(int idx) |
150 | { | |
151 | if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) | |
152 | return kallsyms_addresses[idx]; | |
153 | ||
154 | /* values are unsigned offsets if --absolute-percpu is not in effect */ | |
155 | if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU)) | |
156 | return kallsyms_relative_base + (u32)kallsyms_offsets[idx]; | |
157 | ||
158 | /* ...otherwise, positive offsets are absolute values */ | |
159 | if (kallsyms_offsets[idx] >= 0) | |
160 | return kallsyms_offsets[idx]; | |
161 | ||
162 | /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ | |
163 | return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; | |
164 | } | |
165 | ||
6eb4bd92 | 166 | static bool cleanup_symbol_name(char *s) |
8b8e6b5d ST |
167 | { |
168 | char *res; | |
169 | ||
6eb4bd92 ND |
170 | if (!IS_ENABLED(CONFIG_LTO_CLANG)) |
171 | return false; | |
172 | ||
173 | /* | |
174 | * LLVM appends various suffixes for local functions and variables that | |
175 | * must be promoted to global scope as part of LTO. This can break | |
176 | * hooking of static functions with kprobes. '.' is not a valid | |
177 | * character in an identifier in C. Suffixes observed: | |
178 | * - foo.llvm.[0-9a-f]+ | |
179 | * - foo.[0-9a-f]+ | |
6eb4bd92 ND |
180 | */ |
181 | res = strchr(s, '.'); | |
182 | if (res) { | |
183 | *res = '\0'; | |
184 | return true; | |
185 | } | |
186 | ||
6eb4bd92 | 187 | return false; |
8b8e6b5d | 188 | } |
8b8e6b5d | 189 | |
60443c88 ZL |
190 | static int compare_symbol_name(const char *name, char *namebuf) |
191 | { | |
192 | int ret; | |
193 | ||
194 | ret = strcmp(name, namebuf); | |
195 | if (!ret) | |
196 | return ret; | |
197 | ||
198 | if (cleanup_symbol_name(namebuf) && !strcmp(name, namebuf)) | |
199 | return 0; | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
19bd8981 ZL |
204 | static unsigned int get_symbol_seq(int index) |
205 | { | |
206 | unsigned int i, seq = 0; | |
207 | ||
208 | for (i = 0; i < 3; i++) | |
209 | seq = (seq << 8) | kallsyms_seqs_of_names[3 * index + i]; | |
210 | ||
211 | return seq; | |
212 | } | |
213 | ||
60443c88 ZL |
214 | static int kallsyms_lookup_names(const char *name, |
215 | unsigned int *start, | |
216 | unsigned int *end) | |
217 | { | |
218 | int ret; | |
219 | int low, mid, high; | |
220 | unsigned int seq, off; | |
221 | char namebuf[KSYM_NAME_LEN]; | |
222 | ||
223 | low = 0; | |
224 | high = kallsyms_num_syms - 1; | |
225 | ||
226 | while (low <= high) { | |
227 | mid = low + (high - low) / 2; | |
19bd8981 | 228 | seq = get_symbol_seq(mid); |
60443c88 ZL |
229 | off = get_symbol_offset(seq); |
230 | kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); | |
231 | ret = compare_symbol_name(name, namebuf); | |
232 | if (ret > 0) | |
233 | low = mid + 1; | |
234 | else if (ret < 0) | |
235 | high = mid - 1; | |
236 | else | |
237 | break; | |
238 | } | |
239 | ||
240 | if (low > high) | |
241 | return -ESRCH; | |
242 | ||
243 | low = mid; | |
244 | while (low) { | |
19bd8981 | 245 | seq = get_symbol_seq(low - 1); |
60443c88 ZL |
246 | off = get_symbol_offset(seq); |
247 | kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); | |
248 | if (compare_symbol_name(name, namebuf)) | |
249 | break; | |
250 | low--; | |
251 | } | |
252 | *start = low; | |
253 | ||
254 | if (end) { | |
255 | high = mid; | |
256 | while (high < kallsyms_num_syms - 1) { | |
19bd8981 | 257 | seq = get_symbol_seq(high + 1); |
60443c88 ZL |
258 | off = get_symbol_offset(seq); |
259 | kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); | |
260 | if (compare_symbol_name(name, namebuf)) | |
261 | break; | |
262 | high++; | |
263 | } | |
264 | *end = high; | |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
1da177e4 LT |
270 | /* Lookup the address for this symbol. Returns 0 if not found. */ |
271 | unsigned long kallsyms_lookup_name(const char *name) | |
272 | { | |
60443c88 ZL |
273 | int ret; |
274 | unsigned int i; | |
1da177e4 | 275 | |
aecf489f JO |
276 | /* Skip the search for empty string. */ |
277 | if (!*name) | |
278 | return 0; | |
279 | ||
60443c88 ZL |
280 | ret = kallsyms_lookup_names(name, &i, NULL); |
281 | if (!ret) | |
19bd8981 | 282 | return kallsyms_sym_address(get_symbol_seq(i)); |
8b8e6b5d | 283 | |
1da177e4 LT |
284 | return module_kallsyms_lookup_name(name); |
285 | } | |
1da177e4 | 286 | |
013c1667 CH |
287 | /* |
288 | * Iterate over all symbols in vmlinux. For symbols from modules use | |
289 | * module_kallsyms_on_each_symbol instead. | |
290 | */ | |
75a66614 AK |
291 | int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, |
292 | unsigned long), | |
293 | void *data) | |
294 | { | |
295 | char namebuf[KSYM_NAME_LEN]; | |
296 | unsigned long i; | |
297 | unsigned int off; | |
298 | int ret; | |
299 | ||
300 | for (i = 0, off = 0; i < kallsyms_num_syms; i++) { | |
e3f26752 | 301 | off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); |
2213e9a6 | 302 | ret = fn(data, namebuf, NULL, kallsyms_sym_address(i)); |
75a66614 AK |
303 | if (ret != 0) |
304 | return ret; | |
f5bdb34b | 305 | cond_resched(); |
75a66614 | 306 | } |
013c1667 | 307 | return 0; |
75a66614 | 308 | } |
75a66614 | 309 | |
ffc50891 FBH |
310 | static unsigned long get_symbol_pos(unsigned long addr, |
311 | unsigned long *symbolsize, | |
312 | unsigned long *offset) | |
313 | { | |
314 | unsigned long symbol_start = 0, symbol_end = 0; | |
315 | unsigned long i, low, high, mid; | |
316 | ||
2ea03891 | 317 | /* This kernel should never had been booted. */ |
2213e9a6 AB |
318 | if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) |
319 | BUG_ON(!kallsyms_addresses); | |
320 | else | |
321 | BUG_ON(!kallsyms_offsets); | |
2ea03891 | 322 | |
ad6ccfad | 323 | /* Do a binary search on the sorted kallsyms_addresses array. */ |
ffc50891 FBH |
324 | low = 0; |
325 | high = kallsyms_num_syms; | |
326 | ||
327 | while (high - low > 1) { | |
2fc9c4e1 | 328 | mid = low + (high - low) / 2; |
2213e9a6 | 329 | if (kallsyms_sym_address(mid) <= addr) |
ffc50891 FBH |
330 | low = mid; |
331 | else | |
332 | high = mid; | |
333 | } | |
334 | ||
335 | /* | |
ad6ccfad MK |
336 | * Search for the first aliased symbol. Aliased |
337 | * symbols are symbols with the same address. | |
ffc50891 | 338 | */ |
2213e9a6 | 339 | while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) |
ffc50891 FBH |
340 | --low; |
341 | ||
2213e9a6 | 342 | symbol_start = kallsyms_sym_address(low); |
ffc50891 | 343 | |
ad6ccfad | 344 | /* Search for next non-aliased symbol. */ |
ffc50891 | 345 | for (i = low + 1; i < kallsyms_num_syms; i++) { |
2213e9a6 AB |
346 | if (kallsyms_sym_address(i) > symbol_start) { |
347 | symbol_end = kallsyms_sym_address(i); | |
ffc50891 FBH |
348 | break; |
349 | } | |
350 | } | |
351 | ||
ad6ccfad | 352 | /* If we found no next symbol, we use the end of the section. */ |
ffc50891 FBH |
353 | if (!symbol_end) { |
354 | if (is_kernel_inittext(addr)) | |
355 | symbol_end = (unsigned long)_einittext; | |
63b23e2c | 356 | else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) |
ffc50891 FBH |
357 | symbol_end = (unsigned long)_end; |
358 | else | |
359 | symbol_end = (unsigned long)_etext; | |
360 | } | |
361 | ||
ffb45122 AD |
362 | if (symbolsize) |
363 | *symbolsize = symbol_end - symbol_start; | |
364 | if (offset) | |
365 | *offset = addr - symbol_start; | |
ffc50891 FBH |
366 | |
367 | return low; | |
368 | } | |
369 | ||
370 | /* | |
371 | * Lookup an address but don't bother to find any names. | |
372 | */ | |
373 | int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, | |
374 | unsigned long *offset) | |
375 | { | |
6dd06c9f | 376 | char namebuf[KSYM_NAME_LEN]; |
74451e66 | 377 | |
2a1a3fa0 MZ |
378 | if (is_ksym_addr(addr)) { |
379 | get_symbol_pos(addr, symbolsize, offset); | |
380 | return 1; | |
381 | } | |
9294523e | 382 | return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) || |
74451e66 | 383 | !!__bpf_address_lookup(addr, symbolsize, offset, namebuf); |
ffc50891 FBH |
384 | } |
385 | ||
9294523e SB |
386 | static const char *kallsyms_lookup_buildid(unsigned long addr, |
387 | unsigned long *symbolsize, | |
388 | unsigned long *offset, char **modname, | |
389 | const unsigned char **modbuildid, char *namebuf) | |
1da177e4 | 390 | { |
74451e66 DB |
391 | const char *ret; |
392 | ||
9281acea | 393 | namebuf[KSYM_NAME_LEN - 1] = 0; |
1da177e4 LT |
394 | namebuf[0] = 0; |
395 | ||
ffc50891 FBH |
396 | if (is_ksym_addr(addr)) { |
397 | unsigned long pos; | |
1da177e4 | 398 | |
ffc50891 | 399 | pos = get_symbol_pos(addr, symbolsize, offset); |
1da177e4 | 400 | /* Grab name */ |
e3f26752 CG |
401 | kallsyms_expand_symbol(get_symbol_offset(pos), |
402 | namebuf, KSYM_NAME_LEN); | |
7a74fc49 KM |
403 | if (modname) |
404 | *modname = NULL; | |
9294523e SB |
405 | if (modbuildid) |
406 | *modbuildid = NULL; | |
8b8e6b5d ST |
407 | |
408 | ret = namebuf; | |
409 | goto found; | |
1da177e4 LT |
410 | } |
411 | ||
74451e66 DB |
412 | /* See if it's in a module or a BPF JITed image. */ |
413 | ret = module_address_lookup(addr, symbolsize, offset, | |
9294523e | 414 | modname, modbuildid, namebuf); |
74451e66 DB |
415 | if (!ret) |
416 | ret = bpf_address_lookup(addr, symbolsize, | |
417 | offset, modname, namebuf); | |
aba4b5c2 SRV |
418 | |
419 | if (!ret) | |
420 | ret = ftrace_mod_address_lookup(addr, symbolsize, | |
421 | offset, modname, namebuf); | |
8b8e6b5d ST |
422 | |
423 | found: | |
424 | cleanup_symbol_name(namebuf); | |
74451e66 | 425 | return ret; |
1da177e4 LT |
426 | } |
427 | ||
9294523e SB |
428 | /* |
429 | * Lookup an address | |
430 | * - modname is set to NULL if it's in the kernel. | |
431 | * - We guarantee that the returned name is valid until we reschedule even if. | |
432 | * It resides in a module. | |
433 | * - We also guarantee that modname will be valid until rescheduled. | |
434 | */ | |
435 | const char *kallsyms_lookup(unsigned long addr, | |
436 | unsigned long *symbolsize, | |
437 | unsigned long *offset, | |
438 | char **modname, char *namebuf) | |
439 | { | |
440 | return kallsyms_lookup_buildid(addr, symbolsize, offset, modname, | |
441 | NULL, namebuf); | |
442 | } | |
443 | ||
9d65cb4a AD |
444 | int lookup_symbol_name(unsigned long addr, char *symname) |
445 | { | |
8b8e6b5d ST |
446 | int res; |
447 | ||
9d65cb4a | 448 | symname[0] = '\0'; |
9281acea | 449 | symname[KSYM_NAME_LEN - 1] = '\0'; |
9d65cb4a AD |
450 | |
451 | if (is_ksym_addr(addr)) { | |
452 | unsigned long pos; | |
453 | ||
454 | pos = get_symbol_pos(addr, NULL, NULL); | |
455 | /* Grab name */ | |
e3f26752 CG |
456 | kallsyms_expand_symbol(get_symbol_offset(pos), |
457 | symname, KSYM_NAME_LEN); | |
8b8e6b5d | 458 | goto found; |
9d65cb4a | 459 | } |
ad6ccfad | 460 | /* See if it's in a module. */ |
8b8e6b5d ST |
461 | res = lookup_module_symbol_name(addr, symname); |
462 | if (res) | |
463 | return res; | |
464 | ||
465 | found: | |
466 | cleanup_symbol_name(symname); | |
467 | return 0; | |
9d65cb4a AD |
468 | } |
469 | ||
a5c43dae AD |
470 | int lookup_symbol_attrs(unsigned long addr, unsigned long *size, |
471 | unsigned long *offset, char *modname, char *name) | |
472 | { | |
8b8e6b5d ST |
473 | int res; |
474 | ||
a5c43dae | 475 | name[0] = '\0'; |
9281acea | 476 | name[KSYM_NAME_LEN - 1] = '\0'; |
a5c43dae AD |
477 | |
478 | if (is_ksym_addr(addr)) { | |
479 | unsigned long pos; | |
480 | ||
481 | pos = get_symbol_pos(addr, size, offset); | |
482 | /* Grab name */ | |
e3f26752 CG |
483 | kallsyms_expand_symbol(get_symbol_offset(pos), |
484 | name, KSYM_NAME_LEN); | |
a5c43dae | 485 | modname[0] = '\0'; |
8b8e6b5d | 486 | goto found; |
a5c43dae | 487 | } |
ad6ccfad | 488 | /* See if it's in a module. */ |
8b8e6b5d ST |
489 | res = lookup_module_symbol_attrs(addr, size, offset, modname, name); |
490 | if (res) | |
491 | return res; | |
492 | ||
493 | found: | |
494 | cleanup_symbol_name(name); | |
495 | return 0; | |
a5c43dae AD |
496 | } |
497 | ||
42e38083 | 498 | /* Look up a kernel symbol and return it in a text buffer. */ |
0f77a8d3 | 499 | static int __sprint_symbol(char *buffer, unsigned long address, |
9294523e | 500 | int symbol_offset, int add_offset, int add_buildid) |
1da177e4 LT |
501 | { |
502 | char *modname; | |
9294523e | 503 | const unsigned char *buildid; |
1da177e4 LT |
504 | const char *name; |
505 | unsigned long offset, size; | |
966c8c12 | 506 | int len; |
1da177e4 | 507 | |
0f77a8d3 | 508 | address += symbol_offset; |
9294523e SB |
509 | name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid, |
510 | buffer); | |
1da177e4 | 511 | if (!name) |
b86280aa | 512 | return sprintf(buffer, "0x%lx", address - symbol_offset); |
19769b76 | 513 | |
966c8c12 HD |
514 | if (name != buffer) |
515 | strcpy(buffer, name); | |
516 | len = strlen(buffer); | |
0f77a8d3 | 517 | offset -= symbol_offset; |
966c8c12 | 518 | |
4796dd20 SB |
519 | if (add_offset) |
520 | len += sprintf(buffer + len, "+%#lx/%#lx", offset, size); | |
521 | ||
9294523e SB |
522 | if (modname) { |
523 | len += sprintf(buffer + len, " [%s", modname); | |
524 | #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) | |
525 | if (add_buildid && buildid) { | |
526 | /* build ID should match length of sprintf */ | |
527 | #if IS_ENABLED(CONFIG_MODULES) | |
528 | static_assert(sizeof(typeof_member(struct module, build_id)) == 20); | |
529 | #endif | |
530 | len += sprintf(buffer + len, " %20phN", buildid); | |
531 | } | |
532 | #endif | |
533 | len += sprintf(buffer + len, "]"); | |
534 | } | |
966c8c12 HD |
535 | |
536 | return len; | |
42e38083 | 537 | } |
0f77a8d3 NK |
538 | |
539 | /** | |
540 | * sprint_symbol - Look up a kernel symbol and return it in a text buffer | |
541 | * @buffer: buffer to be stored | |
542 | * @address: address to lookup | |
543 | * | |
544 | * This function looks up a kernel symbol with @address and stores its name, | |
545 | * offset, size and module name to @buffer if possible. If no symbol was found, | |
546 | * just saves its @address as is. | |
547 | * | |
548 | * This function returns the number of bytes stored in @buffer. | |
549 | */ | |
550 | int sprint_symbol(char *buffer, unsigned long address) | |
551 | { | |
9294523e | 552 | return __sprint_symbol(buffer, address, 0, 1, 0); |
0f77a8d3 | 553 | } |
ad6ccfad | 554 | EXPORT_SYMBOL_GPL(sprint_symbol); |
42e38083 | 555 | |
9294523e SB |
556 | /** |
557 | * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer | |
558 | * @buffer: buffer to be stored | |
559 | * @address: address to lookup | |
560 | * | |
561 | * This function looks up a kernel symbol with @address and stores its name, | |
562 | * offset, size, module name and module build ID to @buffer if possible. If no | |
563 | * symbol was found, just saves its @address as is. | |
564 | * | |
565 | * This function returns the number of bytes stored in @buffer. | |
566 | */ | |
567 | int sprint_symbol_build_id(char *buffer, unsigned long address) | |
568 | { | |
569 | return __sprint_symbol(buffer, address, 0, 1, 1); | |
570 | } | |
571 | EXPORT_SYMBOL_GPL(sprint_symbol_build_id); | |
572 | ||
4796dd20 SB |
573 | /** |
574 | * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer | |
575 | * @buffer: buffer to be stored | |
576 | * @address: address to lookup | |
577 | * | |
578 | * This function looks up a kernel symbol with @address and stores its name | |
579 | * and module name to @buffer if possible. If no symbol was found, just saves | |
580 | * its @address as is. | |
581 | * | |
582 | * This function returns the number of bytes stored in @buffer. | |
583 | */ | |
584 | int sprint_symbol_no_offset(char *buffer, unsigned long address) | |
585 | { | |
9294523e | 586 | return __sprint_symbol(buffer, address, 0, 0, 0); |
4796dd20 SB |
587 | } |
588 | EXPORT_SYMBOL_GPL(sprint_symbol_no_offset); | |
589 | ||
0f77a8d3 NK |
590 | /** |
591 | * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer | |
592 | * @buffer: buffer to be stored | |
593 | * @address: address to lookup | |
594 | * | |
595 | * This function is for stack backtrace and does the same thing as | |
596 | * sprint_symbol() but with modified/decreased @address. If there is a | |
597 | * tail-call to the function marked "noreturn", gcc optimized out code after | |
598 | * the call so that the stack-saved return address could point outside of the | |
599 | * caller. This function ensures that kallsyms will find the original caller | |
600 | * by decreasing @address. | |
601 | * | |
602 | * This function returns the number of bytes stored in @buffer. | |
603 | */ | |
604 | int sprint_backtrace(char *buffer, unsigned long address) | |
605 | { | |
9294523e SB |
606 | return __sprint_symbol(buffer, address, -1, 1, 0); |
607 | } | |
608 | ||
609 | /** | |
610 | * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer | |
611 | * @buffer: buffer to be stored | |
612 | * @address: address to lookup | |
613 | * | |
614 | * This function is for stack backtrace and does the same thing as | |
615 | * sprint_symbol() but with modified/decreased @address. If there is a | |
616 | * tail-call to the function marked "noreturn", gcc optimized out code after | |
617 | * the call so that the stack-saved return address could point outside of the | |
618 | * caller. This function ensures that kallsyms will find the original caller | |
619 | * by decreasing @address. This function also appends the module build ID to | |
620 | * the @buffer if @address is within a kernel module. | |
621 | * | |
622 | * This function returns the number of bytes stored in @buffer. | |
623 | */ | |
624 | int sprint_backtrace_build_id(char *buffer, unsigned long address) | |
625 | { | |
626 | return __sprint_symbol(buffer, address, -1, 1, 1); | |
0f77a8d3 NK |
627 | } |
628 | ||
1da177e4 | 629 | /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ |
ad6ccfad | 630 | struct kallsym_iter { |
1da177e4 | 631 | loff_t pos; |
d83212d5 | 632 | loff_t pos_arch_end; |
74451e66 | 633 | loff_t pos_mod_end; |
6171a031 | 634 | loff_t pos_ftrace_mod_end; |
d002b8bc | 635 | loff_t pos_bpf_end; |
1da177e4 | 636 | unsigned long value; |
ad6ccfad | 637 | unsigned int nameoff; /* If iterating in core kernel symbols. */ |
1da177e4 | 638 | char type; |
9281acea TH |
639 | char name[KSYM_NAME_LEN]; |
640 | char module_name[MODULE_NAME_LEN]; | |
ea07890a | 641 | int exported; |
c0f3ea15 | 642 | int show_value; |
1da177e4 LT |
643 | }; |
644 | ||
d83212d5 AS |
645 | int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value, |
646 | char *type, char *name) | |
647 | { | |
648 | return -EINVAL; | |
649 | } | |
650 | ||
651 | static int get_ksymbol_arch(struct kallsym_iter *iter) | |
652 | { | |
653 | int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms, | |
654 | &iter->value, &iter->type, | |
655 | iter->name); | |
656 | ||
657 | if (ret < 0) { | |
658 | iter->pos_arch_end = iter->pos; | |
659 | return 0; | |
660 | } | |
661 | ||
662 | return 1; | |
663 | } | |
664 | ||
1da177e4 LT |
665 | static int get_ksymbol_mod(struct kallsym_iter *iter) |
666 | { | |
d83212d5 | 667 | int ret = module_get_kallsym(iter->pos - iter->pos_arch_end, |
74451e66 DB |
668 | &iter->value, &iter->type, |
669 | iter->name, iter->module_name, | |
670 | &iter->exported); | |
671 | if (ret < 0) { | |
672 | iter->pos_mod_end = iter->pos; | |
1da177e4 | 673 | return 0; |
74451e66 DB |
674 | } |
675 | ||
1da177e4 LT |
676 | return 1; |
677 | } | |
678 | ||
fc0ea795 AH |
679 | /* |
680 | * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace | |
681 | * purposes. In that case "__builtin__ftrace" is used as a module name, even | |
682 | * though "__builtin__ftrace" is not a module. | |
683 | */ | |
6171a031 SRV |
684 | static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) |
685 | { | |
686 | int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end, | |
687 | &iter->value, &iter->type, | |
688 | iter->name, iter->module_name, | |
689 | &iter->exported); | |
690 | if (ret < 0) { | |
691 | iter->pos_ftrace_mod_end = iter->pos; | |
692 | return 0; | |
693 | } | |
694 | ||
695 | return 1; | |
696 | } | |
697 | ||
74451e66 DB |
698 | static int get_ksymbol_bpf(struct kallsym_iter *iter) |
699 | { | |
d002b8bc AH |
700 | int ret; |
701 | ||
6934058d | 702 | strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN); |
74451e66 | 703 | iter->exported = 0; |
d002b8bc AH |
704 | ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, |
705 | &iter->value, &iter->type, | |
706 | iter->name); | |
707 | if (ret < 0) { | |
708 | iter->pos_bpf_end = iter->pos; | |
709 | return 0; | |
710 | } | |
711 | ||
712 | return 1; | |
713 | } | |
714 | ||
715 | /* | |
716 | * This uses "__builtin__kprobes" as a module name for symbols for pages | |
717 | * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a | |
718 | * module. | |
719 | */ | |
720 | static int get_ksymbol_kprobe(struct kallsym_iter *iter) | |
721 | { | |
722 | strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); | |
723 | iter->exported = 0; | |
724 | return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, | |
725 | &iter->value, &iter->type, | |
726 | iter->name) < 0 ? 0 : 1; | |
74451e66 DB |
727 | } |
728 | ||
1da177e4 LT |
729 | /* Returns space to next name. */ |
730 | static unsigned long get_ksymbol_core(struct kallsym_iter *iter) | |
731 | { | |
732 | unsigned off = iter->nameoff; | |
733 | ||
ea07890a | 734 | iter->module_name[0] = '\0'; |
2213e9a6 | 735 | iter->value = kallsyms_sym_address(iter->pos); |
1da177e4 LT |
736 | |
737 | iter->type = kallsyms_get_symbol_type(off); | |
738 | ||
e3f26752 | 739 | off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); |
1da177e4 LT |
740 | |
741 | return off - iter->nameoff; | |
742 | } | |
743 | ||
744 | static void reset_iter(struct kallsym_iter *iter, loff_t new_pos) | |
745 | { | |
746 | iter->name[0] = '\0'; | |
747 | iter->nameoff = get_symbol_offset(new_pos); | |
748 | iter->pos = new_pos; | |
6171a031 | 749 | if (new_pos == 0) { |
d83212d5 | 750 | iter->pos_arch_end = 0; |
74451e66 | 751 | iter->pos_mod_end = 0; |
6171a031 | 752 | iter->pos_ftrace_mod_end = 0; |
d002b8bc | 753 | iter->pos_bpf_end = 0; |
6171a031 | 754 | } |
74451e66 DB |
755 | } |
756 | ||
b9667942 AH |
757 | /* |
758 | * The end position (last + 1) of each additional kallsyms section is recorded | |
759 | * in iter->pos_..._end as each section is added, and so can be used to | |
760 | * determine which get_ksymbol_...() function to call next. | |
761 | */ | |
74451e66 DB |
762 | static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) |
763 | { | |
764 | iter->pos = pos; | |
765 | ||
d83212d5 AS |
766 | if ((!iter->pos_arch_end || iter->pos_arch_end > pos) && |
767 | get_ksymbol_arch(iter)) | |
768 | return 1; | |
769 | ||
b9667942 AH |
770 | if ((!iter->pos_mod_end || iter->pos_mod_end > pos) && |
771 | get_ksymbol_mod(iter)) | |
6171a031 | 772 | return 1; |
6171a031 | 773 | |
b9667942 AH |
774 | if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) && |
775 | get_ksymbol_ftrace_mod(iter)) | |
776 | return 1; | |
74451e66 | 777 | |
d002b8bc AH |
778 | if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) && |
779 | get_ksymbol_bpf(iter)) | |
780 | return 1; | |
781 | ||
782 | return get_ksymbol_kprobe(iter); | |
1da177e4 LT |
783 | } |
784 | ||
785 | /* Returns false if pos at or past end of file. */ | |
786 | static int update_iter(struct kallsym_iter *iter, loff_t pos) | |
787 | { | |
788 | /* Module symbols can be accessed randomly. */ | |
74451e66 DB |
789 | if (pos >= kallsyms_num_syms) |
790 | return update_iter_mod(iter, pos); | |
ad6ccfad | 791 | |
1da177e4 LT |
792 | /* If we're not on the desired position, reset to new position. */ |
793 | if (pos != iter->pos) | |
794 | reset_iter(iter, pos); | |
795 | ||
796 | iter->nameoff += get_ksymbol_core(iter); | |
797 | iter->pos++; | |
798 | ||
799 | return 1; | |
800 | } | |
801 | ||
802 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) | |
803 | { | |
804 | (*pos)++; | |
805 | ||
806 | if (!update_iter(m->private, *pos)) | |
807 | return NULL; | |
808 | return p; | |
809 | } | |
810 | ||
811 | static void *s_start(struct seq_file *m, loff_t *pos) | |
812 | { | |
813 | if (!update_iter(m->private, *pos)) | |
814 | return NULL; | |
815 | return m->private; | |
816 | } | |
817 | ||
818 | static void s_stop(struct seq_file *m, void *p) | |
819 | { | |
820 | } | |
821 | ||
822 | static int s_show(struct seq_file *m, void *p) | |
823 | { | |
668533dc | 824 | void *value; |
1da177e4 LT |
825 | struct kallsym_iter *iter = m->private; |
826 | ||
ad6ccfad | 827 | /* Some debugging symbols have no name. Ignore them. */ |
1da177e4 LT |
828 | if (!iter->name[0]) |
829 | return 0; | |
830 | ||
668533dc | 831 | value = iter->show_value ? (void *)iter->value : NULL; |
c0f3ea15 | 832 | |
ea07890a AD |
833 | if (iter->module_name[0]) { |
834 | char type; | |
835 | ||
ad6ccfad MK |
836 | /* |
837 | * Label it "global" if it is exported, | |
838 | * "local" if not exported. | |
839 | */ | |
ea07890a AD |
840 | type = iter->exported ? toupper(iter->type) : |
841 | tolower(iter->type); | |
668533dc | 842 | seq_printf(m, "%px %c %s\t[%s]\n", value, |
9f36e2c4 | 843 | type, iter->name, iter->module_name); |
ea07890a | 844 | } else |
668533dc | 845 | seq_printf(m, "%px %c %s\n", value, |
9f36e2c4 | 846 | iter->type, iter->name); |
1da177e4 LT |
847 | return 0; |
848 | } | |
849 | ||
15ad7cdc | 850 | static const struct seq_operations kallsyms_op = { |
1da177e4 LT |
851 | .start = s_start, |
852 | .next = s_next, | |
853 | .stop = s_stop, | |
854 | .show = s_show | |
855 | }; | |
856 | ||
647cafa2 AM |
857 | #ifdef CONFIG_BPF_SYSCALL |
858 | ||
859 | struct bpf_iter__ksym { | |
860 | __bpf_md_ptr(struct bpf_iter_meta *, meta); | |
861 | __bpf_md_ptr(struct kallsym_iter *, ksym); | |
862 | }; | |
863 | ||
864 | static int ksym_prog_seq_show(struct seq_file *m, bool in_stop) | |
865 | { | |
866 | struct bpf_iter__ksym ctx; | |
867 | struct bpf_iter_meta meta; | |
868 | struct bpf_prog *prog; | |
869 | ||
870 | meta.seq = m; | |
871 | prog = bpf_iter_get_info(&meta, in_stop); | |
872 | if (!prog) | |
873 | return 0; | |
874 | ||
875 | ctx.meta = &meta; | |
876 | ctx.ksym = m ? m->private : NULL; | |
877 | return bpf_iter_run_prog(prog, &ctx); | |
878 | } | |
879 | ||
880 | static int bpf_iter_ksym_seq_show(struct seq_file *m, void *p) | |
881 | { | |
882 | return ksym_prog_seq_show(m, false); | |
883 | } | |
884 | ||
885 | static void bpf_iter_ksym_seq_stop(struct seq_file *m, void *p) | |
886 | { | |
887 | if (!p) | |
888 | (void) ksym_prog_seq_show(m, true); | |
889 | else | |
890 | s_stop(m, p); | |
891 | } | |
892 | ||
893 | static const struct seq_operations bpf_iter_ksym_ops = { | |
894 | .start = s_start, | |
895 | .next = s_next, | |
896 | .stop = bpf_iter_ksym_seq_stop, | |
897 | .show = bpf_iter_ksym_seq_show, | |
898 | }; | |
899 | ||
900 | static int bpf_iter_ksym_init(void *priv_data, struct bpf_iter_aux_info *aux) | |
901 | { | |
902 | struct kallsym_iter *iter = priv_data; | |
903 | ||
904 | reset_iter(iter, 0); | |
905 | ||
906 | /* cache here as in kallsyms_open() case; use current process | |
907 | * credentials to tell BPF iterators if values should be shown. | |
908 | */ | |
909 | iter->show_value = kallsyms_show_value(current_cred()); | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
914 | DEFINE_BPF_ITER_FUNC(ksym, struct bpf_iter_meta *meta, struct kallsym_iter *ksym) | |
915 | ||
916 | static const struct bpf_iter_seq_info ksym_iter_seq_info = { | |
917 | .seq_ops = &bpf_iter_ksym_ops, | |
918 | .init_seq_private = bpf_iter_ksym_init, | |
919 | .fini_seq_private = NULL, | |
920 | .seq_priv_size = sizeof(struct kallsym_iter), | |
921 | }; | |
922 | ||
923 | static struct bpf_iter_reg ksym_iter_reg_info = { | |
924 | .target = "ksym", | |
925 | .feature = BPF_ITER_RESCHED, | |
926 | .ctx_arg_info_size = 1, | |
927 | .ctx_arg_info = { | |
928 | { offsetof(struct bpf_iter__ksym, ksym), | |
929 | PTR_TO_BTF_ID_OR_NULL }, | |
930 | }, | |
931 | .seq_info = &ksym_iter_seq_info, | |
932 | }; | |
933 | ||
934 | BTF_ID_LIST(btf_ksym_iter_id) | |
935 | BTF_ID(struct, kallsym_iter) | |
936 | ||
937 | static int __init bpf_ksym_iter_register(void) | |
938 | { | |
939 | ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id; | |
940 | return bpf_iter_reg_target(&ksym_iter_reg_info); | |
941 | } | |
942 | ||
943 | late_initcall(bpf_ksym_iter_register); | |
944 | ||
945 | #endif /* CONFIG_BPF_SYSCALL */ | |
946 | ||
c0f3ea15 LT |
947 | static inline int kallsyms_for_perf(void) |
948 | { | |
949 | #ifdef CONFIG_PERF_EVENTS | |
950 | extern int sysctl_perf_event_paranoid; | |
951 | if (sysctl_perf_event_paranoid <= 1) | |
952 | return 1; | |
953 | #endif | |
954 | return 0; | |
955 | } | |
956 | ||
957 | /* | |
958 | * We show kallsyms information even to normal users if we've enabled | |
959 | * kernel profiling and are explicitly not paranoid (so kptr_restrict | |
960 | * is clear, and sysctl_perf_event_paranoid isn't set). | |
961 | * | |
962 | * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to | |
963 | * block even that). | |
964 | */ | |
16025184 | 965 | bool kallsyms_show_value(const struct cred *cred) |
c0f3ea15 LT |
966 | { |
967 | switch (kptr_restrict) { | |
968 | case 0: | |
969 | if (kallsyms_for_perf()) | |
16025184 | 970 | return true; |
df561f66 | 971 | fallthrough; |
c0f3ea15 | 972 | case 1: |
16025184 KC |
973 | if (security_capable(cred, &init_user_ns, CAP_SYSLOG, |
974 | CAP_OPT_NOAUDIT) == 0) | |
975 | return true; | |
df561f66 | 976 | fallthrough; |
c0f3ea15 | 977 | default: |
16025184 | 978 | return false; |
c0f3ea15 LT |
979 | } |
980 | } | |
981 | ||
1da177e4 LT |
982 | static int kallsyms_open(struct inode *inode, struct file *file) |
983 | { | |
ad6ccfad MK |
984 | /* |
985 | * We keep iterator in m->private, since normal case is to | |
1da177e4 | 986 | * s_start from where we left off, so we avoid doing |
ad6ccfad MK |
987 | * using get_symbol_offset for every symbol. |
988 | */ | |
1da177e4 | 989 | struct kallsym_iter *iter; |
0049f26a | 990 | iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); |
1da177e4 LT |
991 | if (!iter) |
992 | return -ENOMEM; | |
993 | reset_iter(iter, 0); | |
994 | ||
16025184 KC |
995 | /* |
996 | * Instead of checking this on every s_show() call, cache | |
997 | * the result here at open time. | |
998 | */ | |
999 | iter->show_value = kallsyms_show_value(file->f_cred); | |
0049f26a | 1000 | return 0; |
1da177e4 LT |
1001 | } |
1002 | ||
67fc4e0c JW |
1003 | #ifdef CONFIG_KGDB_KDB |
1004 | const char *kdb_walk_kallsyms(loff_t *pos) | |
1005 | { | |
1006 | static struct kallsym_iter kdb_walk_kallsyms_iter; | |
1007 | if (*pos == 0) { | |
1008 | memset(&kdb_walk_kallsyms_iter, 0, | |
1009 | sizeof(kdb_walk_kallsyms_iter)); | |
1010 | reset_iter(&kdb_walk_kallsyms_iter, 0); | |
1011 | } | |
1012 | while (1) { | |
1013 | if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) | |
1014 | return NULL; | |
1015 | ++*pos; | |
1016 | /* Some debugging symbols have no name. Ignore them. */ | |
1017 | if (kdb_walk_kallsyms_iter.name[0]) | |
1018 | return kdb_walk_kallsyms_iter.name; | |
1019 | } | |
1020 | } | |
1021 | #endif /* CONFIG_KGDB_KDB */ | |
1022 | ||
97a32539 AD |
1023 | static const struct proc_ops kallsyms_proc_ops = { |
1024 | .proc_open = kallsyms_open, | |
1025 | .proc_read = seq_read, | |
1026 | .proc_lseek = seq_lseek, | |
1027 | .proc_release = seq_release_private, | |
1da177e4 LT |
1028 | }; |
1029 | ||
1030 | static int __init kallsyms_init(void) | |
1031 | { | |
97a32539 | 1032 | proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); |
1da177e4 LT |
1033 | return 0; |
1034 | } | |
ad6ccfad | 1035 | device_initcall(kallsyms_init); |