kallsyms: Fix kallsyms_selftest failure
[linux-2.6-block.git] / kernel / kallsyms.c
CommitLineData
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
42static 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
90tail:
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
102static 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
116static 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
30f3bb09 149unsigned long kallsyms_sym_address(int idx)
2213e9a6
AB
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 166static 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
8cc32a9b 177 * character in an identifier in C. Suffixes only in LLVM LTO observed:
6eb4bd92 178 * - foo.llvm.[0-9a-f]+
6eb4bd92 179 */
8cc32a9b 180 res = strstr(s, ".llvm.");
6eb4bd92
ND
181 if (res) {
182 *res = '\0';
183 return true;
184 }
185
6eb4bd92 186 return false;
8b8e6b5d 187}
8b8e6b5d 188
60443c88
ZL
189static int compare_symbol_name(const char *name, char *namebuf)
190{
33f0467f
YS
191 /* The kallsyms_seqs_of_names is sorted based on names after
192 * cleanup_symbol_name() (see scripts/kallsyms.c) if clang lto is enabled.
193 * To ensure correct bisection in kallsyms_lookup_names(), do
194 * cleanup_symbol_name(namebuf) before comparing name and namebuf.
195 */
196 cleanup_symbol_name(namebuf);
197 return strcmp(name, namebuf);
60443c88
ZL
198}
199
19bd8981
ZL
200static unsigned int get_symbol_seq(int index)
201{
202 unsigned int i, seq = 0;
203
204 for (i = 0; i < 3; i++)
205 seq = (seq << 8) | kallsyms_seqs_of_names[3 * index + i];
206
207 return seq;
208}
209
60443c88
ZL
210static int kallsyms_lookup_names(const char *name,
211 unsigned int *start,
212 unsigned int *end)
213{
214 int ret;
215 int low, mid, high;
216 unsigned int seq, off;
217 char namebuf[KSYM_NAME_LEN];
218
219 low = 0;
220 high = kallsyms_num_syms - 1;
221
222 while (low <= high) {
223 mid = low + (high - low) / 2;
19bd8981 224 seq = get_symbol_seq(mid);
60443c88
ZL
225 off = get_symbol_offset(seq);
226 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
227 ret = compare_symbol_name(name, namebuf);
228 if (ret > 0)
229 low = mid + 1;
230 else if (ret < 0)
231 high = mid - 1;
232 else
233 break;
234 }
235
236 if (low > high)
237 return -ESRCH;
238
239 low = mid;
240 while (low) {
19bd8981 241 seq = get_symbol_seq(low - 1);
60443c88
ZL
242 off = get_symbol_offset(seq);
243 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
244 if (compare_symbol_name(name, namebuf))
245 break;
246 low--;
247 }
248 *start = low;
249
250 if (end) {
251 high = mid;
252 while (high < kallsyms_num_syms - 1) {
19bd8981 253 seq = get_symbol_seq(high + 1);
60443c88
ZL
254 off = get_symbol_offset(seq);
255 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
256 if (compare_symbol_name(name, namebuf))
257 break;
258 high++;
259 }
260 *end = high;
261 }
262
263 return 0;
264}
265
1da177e4
LT
266/* Lookup the address for this symbol. Returns 0 if not found. */
267unsigned long kallsyms_lookup_name(const char *name)
268{
60443c88
ZL
269 int ret;
270 unsigned int i;
1da177e4 271
aecf489f
JO
272 /* Skip the search for empty string. */
273 if (!*name)
274 return 0;
275
60443c88
ZL
276 ret = kallsyms_lookup_names(name, &i, NULL);
277 if (!ret)
19bd8981 278 return kallsyms_sym_address(get_symbol_seq(i));
8b8e6b5d 279
1da177e4
LT
280 return module_kallsyms_lookup_name(name);
281}
1da177e4 282
013c1667
CH
283/*
284 * Iterate over all symbols in vmlinux. For symbols from modules use
285 * module_kallsyms_on_each_symbol instead.
286 */
3703bd54 287int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long),
75a66614
AK
288 void *data)
289{
290 char namebuf[KSYM_NAME_LEN];
291 unsigned long i;
292 unsigned int off;
293 int ret;
294
295 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
e3f26752 296 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
3703bd54 297 ret = fn(data, namebuf, kallsyms_sym_address(i));
75a66614
AK
298 if (ret != 0)
299 return ret;
f5bdb34b 300 cond_resched();
75a66614 301 }
013c1667 302 return 0;
75a66614 303}
75a66614 304
4dc533e0
ZL
305int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long),
306 const char *name, void *data)
307{
308 int ret;
309 unsigned int i, start, end;
310
311 ret = kallsyms_lookup_names(name, &start, &end);
312 if (ret)
313 return 0;
314
315 for (i = start; !ret && i <= end; i++) {
316 ret = fn(data, kallsyms_sym_address(get_symbol_seq(i)));
317 cond_resched();
318 }
319
320 return ret;
321}
322
ffc50891
FBH
323static unsigned long get_symbol_pos(unsigned long addr,
324 unsigned long *symbolsize,
325 unsigned long *offset)
326{
327 unsigned long symbol_start = 0, symbol_end = 0;
328 unsigned long i, low, high, mid;
329
2ea03891 330 /* This kernel should never had been booted. */
2213e9a6
AB
331 if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
332 BUG_ON(!kallsyms_addresses);
333 else
334 BUG_ON(!kallsyms_offsets);
2ea03891 335
ad6ccfad 336 /* Do a binary search on the sorted kallsyms_addresses array. */
ffc50891
FBH
337 low = 0;
338 high = kallsyms_num_syms;
339
340 while (high - low > 1) {
2fc9c4e1 341 mid = low + (high - low) / 2;
2213e9a6 342 if (kallsyms_sym_address(mid) <= addr)
ffc50891
FBH
343 low = mid;
344 else
345 high = mid;
346 }
347
348 /*
ad6ccfad
MK
349 * Search for the first aliased symbol. Aliased
350 * symbols are symbols with the same address.
ffc50891 351 */
2213e9a6 352 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
ffc50891
FBH
353 --low;
354
2213e9a6 355 symbol_start = kallsyms_sym_address(low);
ffc50891 356
ad6ccfad 357 /* Search for next non-aliased symbol. */
ffc50891 358 for (i = low + 1; i < kallsyms_num_syms; i++) {
2213e9a6
AB
359 if (kallsyms_sym_address(i) > symbol_start) {
360 symbol_end = kallsyms_sym_address(i);
ffc50891
FBH
361 break;
362 }
363 }
364
ad6ccfad 365 /* If we found no next symbol, we use the end of the section. */
ffc50891
FBH
366 if (!symbol_end) {
367 if (is_kernel_inittext(addr))
368 symbol_end = (unsigned long)_einittext;
63b23e2c 369 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
ffc50891
FBH
370 symbol_end = (unsigned long)_end;
371 else
372 symbol_end = (unsigned long)_etext;
373 }
374
ffb45122
AD
375 if (symbolsize)
376 *symbolsize = symbol_end - symbol_start;
377 if (offset)
378 *offset = addr - symbol_start;
ffc50891
FBH
379
380 return low;
381}
382
383/*
384 * Lookup an address but don't bother to find any names.
385 */
386int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
387 unsigned long *offset)
388{
6dd06c9f 389 char namebuf[KSYM_NAME_LEN];
74451e66 390
2a1a3fa0
MZ
391 if (is_ksym_addr(addr)) {
392 get_symbol_pos(addr, symbolsize, offset);
393 return 1;
394 }
9294523e 395 return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) ||
74451e66 396 !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
ffc50891
FBH
397}
398
9294523e
SB
399static const char *kallsyms_lookup_buildid(unsigned long addr,
400 unsigned long *symbolsize,
401 unsigned long *offset, char **modname,
402 const unsigned char **modbuildid, char *namebuf)
1da177e4 403{
74451e66
DB
404 const char *ret;
405
9281acea 406 namebuf[KSYM_NAME_LEN - 1] = 0;
1da177e4
LT
407 namebuf[0] = 0;
408
ffc50891
FBH
409 if (is_ksym_addr(addr)) {
410 unsigned long pos;
1da177e4 411
ffc50891 412 pos = get_symbol_pos(addr, symbolsize, offset);
1da177e4 413 /* Grab name */
e3f26752
CG
414 kallsyms_expand_symbol(get_symbol_offset(pos),
415 namebuf, KSYM_NAME_LEN);
7a74fc49
KM
416 if (modname)
417 *modname = NULL;
9294523e
SB
418 if (modbuildid)
419 *modbuildid = NULL;
8b8e6b5d
ST
420
421 ret = namebuf;
422 goto found;
1da177e4
LT
423 }
424
74451e66
DB
425 /* See if it's in a module or a BPF JITed image. */
426 ret = module_address_lookup(addr, symbolsize, offset,
9294523e 427 modname, modbuildid, namebuf);
74451e66
DB
428 if (!ret)
429 ret = bpf_address_lookup(addr, symbolsize,
430 offset, modname, namebuf);
aba4b5c2
SRV
431
432 if (!ret)
433 ret = ftrace_mod_address_lookup(addr, symbolsize,
434 offset, modname, namebuf);
8b8e6b5d
ST
435
436found:
437 cleanup_symbol_name(namebuf);
74451e66 438 return ret;
1da177e4
LT
439}
440
9294523e
SB
441/*
442 * Lookup an address
443 * - modname is set to NULL if it's in the kernel.
444 * - We guarantee that the returned name is valid until we reschedule even if.
445 * It resides in a module.
446 * - We also guarantee that modname will be valid until rescheduled.
447 */
448const char *kallsyms_lookup(unsigned long addr,
449 unsigned long *symbolsize,
450 unsigned long *offset,
451 char **modname, char *namebuf)
452{
453 return kallsyms_lookup_buildid(addr, symbolsize, offset, modname,
454 NULL, namebuf);
455}
456
9d65cb4a
AD
457int lookup_symbol_name(unsigned long addr, char *symname)
458{
8b8e6b5d
ST
459 int res;
460
9d65cb4a 461 symname[0] = '\0';
9281acea 462 symname[KSYM_NAME_LEN - 1] = '\0';
9d65cb4a
AD
463
464 if (is_ksym_addr(addr)) {
465 unsigned long pos;
466
467 pos = get_symbol_pos(addr, NULL, NULL);
468 /* Grab name */
e3f26752
CG
469 kallsyms_expand_symbol(get_symbol_offset(pos),
470 symname, KSYM_NAME_LEN);
8b8e6b5d 471 goto found;
9d65cb4a 472 }
ad6ccfad 473 /* See if it's in a module. */
8b8e6b5d
ST
474 res = lookup_module_symbol_name(addr, symname);
475 if (res)
476 return res;
477
478found:
479 cleanup_symbol_name(symname);
480 return 0;
9d65cb4a
AD
481}
482
42e38083 483/* Look up a kernel symbol and return it in a text buffer. */
0f77a8d3 484static int __sprint_symbol(char *buffer, unsigned long address,
9294523e 485 int symbol_offset, int add_offset, int add_buildid)
1da177e4
LT
486{
487 char *modname;
9294523e 488 const unsigned char *buildid;
1da177e4
LT
489 const char *name;
490 unsigned long offset, size;
966c8c12 491 int len;
1da177e4 492
0f77a8d3 493 address += symbol_offset;
9294523e
SB
494 name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid,
495 buffer);
1da177e4 496 if (!name)
b86280aa 497 return sprintf(buffer, "0x%lx", address - symbol_offset);
19769b76 498
966c8c12
HD
499 if (name != buffer)
500 strcpy(buffer, name);
501 len = strlen(buffer);
0f77a8d3 502 offset -= symbol_offset;
966c8c12 503
4796dd20
SB
504 if (add_offset)
505 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
506
9294523e
SB
507 if (modname) {
508 len += sprintf(buffer + len, " [%s", modname);
509#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
510 if (add_buildid && buildid) {
511 /* build ID should match length of sprintf */
512#if IS_ENABLED(CONFIG_MODULES)
513 static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
514#endif
515 len += sprintf(buffer + len, " %20phN", buildid);
516 }
517#endif
518 len += sprintf(buffer + len, "]");
519 }
966c8c12
HD
520
521 return len;
42e38083 522}
0f77a8d3
NK
523
524/**
525 * sprint_symbol - Look up a kernel symbol and return it in a text buffer
526 * @buffer: buffer to be stored
527 * @address: address to lookup
528 *
529 * This function looks up a kernel symbol with @address and stores its name,
530 * offset, size and module name to @buffer if possible. If no symbol was found,
531 * just saves its @address as is.
532 *
533 * This function returns the number of bytes stored in @buffer.
534 */
535int sprint_symbol(char *buffer, unsigned long address)
536{
9294523e 537 return __sprint_symbol(buffer, address, 0, 1, 0);
0f77a8d3 538}
ad6ccfad 539EXPORT_SYMBOL_GPL(sprint_symbol);
42e38083 540
9294523e
SB
541/**
542 * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer
543 * @buffer: buffer to be stored
544 * @address: address to lookup
545 *
546 * This function looks up a kernel symbol with @address and stores its name,
547 * offset, size, module name and module build ID to @buffer if possible. If no
548 * symbol was found, just saves its @address as is.
549 *
550 * This function returns the number of bytes stored in @buffer.
551 */
552int sprint_symbol_build_id(char *buffer, unsigned long address)
553{
554 return __sprint_symbol(buffer, address, 0, 1, 1);
555}
556EXPORT_SYMBOL_GPL(sprint_symbol_build_id);
557
4796dd20
SB
558/**
559 * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
560 * @buffer: buffer to be stored
561 * @address: address to lookup
562 *
563 * This function looks up a kernel symbol with @address and stores its name
564 * and module name to @buffer if possible. If no symbol was found, just saves
565 * its @address as is.
566 *
567 * This function returns the number of bytes stored in @buffer.
568 */
569int sprint_symbol_no_offset(char *buffer, unsigned long address)
570{
9294523e 571 return __sprint_symbol(buffer, address, 0, 0, 0);
4796dd20
SB
572}
573EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
574
0f77a8d3
NK
575/**
576 * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
577 * @buffer: buffer to be stored
578 * @address: address to lookup
579 *
580 * This function is for stack backtrace and does the same thing as
581 * sprint_symbol() but with modified/decreased @address. If there is a
582 * tail-call to the function marked "noreturn", gcc optimized out code after
583 * the call so that the stack-saved return address could point outside of the
584 * caller. This function ensures that kallsyms will find the original caller
585 * by decreasing @address.
586 *
587 * This function returns the number of bytes stored in @buffer.
588 */
589int sprint_backtrace(char *buffer, unsigned long address)
590{
9294523e
SB
591 return __sprint_symbol(buffer, address, -1, 1, 0);
592}
593
594/**
595 * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
596 * @buffer: buffer to be stored
597 * @address: address to lookup
598 *
599 * This function is for stack backtrace and does the same thing as
600 * sprint_symbol() but with modified/decreased @address. If there is a
601 * tail-call to the function marked "noreturn", gcc optimized out code after
602 * the call so that the stack-saved return address could point outside of the
603 * caller. This function ensures that kallsyms will find the original caller
604 * by decreasing @address. This function also appends the module build ID to
605 * the @buffer if @address is within a kernel module.
606 *
607 * This function returns the number of bytes stored in @buffer.
608 */
609int sprint_backtrace_build_id(char *buffer, unsigned long address)
610{
611 return __sprint_symbol(buffer, address, -1, 1, 1);
0f77a8d3
NK
612}
613
1da177e4 614/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
ad6ccfad 615struct kallsym_iter {
1da177e4 616 loff_t pos;
74451e66 617 loff_t pos_mod_end;
6171a031 618 loff_t pos_ftrace_mod_end;
d002b8bc 619 loff_t pos_bpf_end;
1da177e4 620 unsigned long value;
ad6ccfad 621 unsigned int nameoff; /* If iterating in core kernel symbols. */
1da177e4 622 char type;
9281acea
TH
623 char name[KSYM_NAME_LEN];
624 char module_name[MODULE_NAME_LEN];
ea07890a 625 int exported;
c0f3ea15 626 int show_value;
1da177e4
LT
627};
628
1da177e4
LT
629static int get_ksymbol_mod(struct kallsym_iter *iter)
630{
15d5daa0 631 int ret = module_get_kallsym(iter->pos - kallsyms_num_syms,
74451e66
DB
632 &iter->value, &iter->type,
633 iter->name, iter->module_name,
634 &iter->exported);
635 if (ret < 0) {
636 iter->pos_mod_end = iter->pos;
1da177e4 637 return 0;
74451e66
DB
638 }
639
1da177e4
LT
640 return 1;
641}
642
fc0ea795
AH
643/*
644 * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
645 * purposes. In that case "__builtin__ftrace" is used as a module name, even
646 * though "__builtin__ftrace" is not a module.
647 */
6171a031
SRV
648static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
649{
650 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
651 &iter->value, &iter->type,
652 iter->name, iter->module_name,
653 &iter->exported);
654 if (ret < 0) {
655 iter->pos_ftrace_mod_end = iter->pos;
656 return 0;
657 }
658
659 return 1;
660}
661
74451e66
DB
662static int get_ksymbol_bpf(struct kallsym_iter *iter)
663{
d002b8bc
AH
664 int ret;
665
33457938 666 strscpy(iter->module_name, "bpf", MODULE_NAME_LEN);
74451e66 667 iter->exported = 0;
d002b8bc
AH
668 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
669 &iter->value, &iter->type,
670 iter->name);
671 if (ret < 0) {
672 iter->pos_bpf_end = iter->pos;
673 return 0;
674 }
675
676 return 1;
677}
678
679/*
680 * This uses "__builtin__kprobes" as a module name for symbols for pages
681 * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
682 * module.
683 */
684static int get_ksymbol_kprobe(struct kallsym_iter *iter)
685{
33457938 686 strscpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
d002b8bc
AH
687 iter->exported = 0;
688 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
689 &iter->value, &iter->type,
690 iter->name) < 0 ? 0 : 1;
74451e66
DB
691}
692
1da177e4
LT
693/* Returns space to next name. */
694static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
695{
696 unsigned off = iter->nameoff;
697
ea07890a 698 iter->module_name[0] = '\0';
2213e9a6 699 iter->value = kallsyms_sym_address(iter->pos);
1da177e4
LT
700
701 iter->type = kallsyms_get_symbol_type(off);
702
e3f26752 703 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
1da177e4
LT
704
705 return off - iter->nameoff;
706}
707
708static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
709{
710 iter->name[0] = '\0';
711 iter->nameoff = get_symbol_offset(new_pos);
712 iter->pos = new_pos;
6171a031 713 if (new_pos == 0) {
74451e66 714 iter->pos_mod_end = 0;
6171a031 715 iter->pos_ftrace_mod_end = 0;
d002b8bc 716 iter->pos_bpf_end = 0;
6171a031 717 }
74451e66
DB
718}
719
b9667942
AH
720/*
721 * The end position (last + 1) of each additional kallsyms section is recorded
722 * in iter->pos_..._end as each section is added, and so can be used to
723 * determine which get_ksymbol_...() function to call next.
724 */
74451e66
DB
725static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
726{
727 iter->pos = pos;
728
b9667942
AH
729 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
730 get_ksymbol_mod(iter))
6171a031 731 return 1;
6171a031 732
b9667942
AH
733 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
734 get_ksymbol_ftrace_mod(iter))
735 return 1;
74451e66 736
d002b8bc
AH
737 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
738 get_ksymbol_bpf(iter))
739 return 1;
740
741 return get_ksymbol_kprobe(iter);
1da177e4
LT
742}
743
744/* Returns false if pos at or past end of file. */
745static int update_iter(struct kallsym_iter *iter, loff_t pos)
746{
747 /* Module symbols can be accessed randomly. */
74451e66
DB
748 if (pos >= kallsyms_num_syms)
749 return update_iter_mod(iter, pos);
ad6ccfad 750
1da177e4
LT
751 /* If we're not on the desired position, reset to new position. */
752 if (pos != iter->pos)
753 reset_iter(iter, pos);
754
755 iter->nameoff += get_ksymbol_core(iter);
756 iter->pos++;
757
758 return 1;
759}
760
761static void *s_next(struct seq_file *m, void *p, loff_t *pos)
762{
763 (*pos)++;
764
765 if (!update_iter(m->private, *pos))
766 return NULL;
767 return p;
768}
769
770static void *s_start(struct seq_file *m, loff_t *pos)
771{
772 if (!update_iter(m->private, *pos))
773 return NULL;
774 return m->private;
775}
776
777static void s_stop(struct seq_file *m, void *p)
778{
779}
780
781static int s_show(struct seq_file *m, void *p)
782{
668533dc 783 void *value;
1da177e4
LT
784 struct kallsym_iter *iter = m->private;
785
ad6ccfad 786 /* Some debugging symbols have no name. Ignore them. */
1da177e4
LT
787 if (!iter->name[0])
788 return 0;
789
668533dc 790 value = iter->show_value ? (void *)iter->value : NULL;
c0f3ea15 791
ea07890a
AD
792 if (iter->module_name[0]) {
793 char type;
794
ad6ccfad
MK
795 /*
796 * Label it "global" if it is exported,
797 * "local" if not exported.
798 */
ea07890a
AD
799 type = iter->exported ? toupper(iter->type) :
800 tolower(iter->type);
668533dc 801 seq_printf(m, "%px %c %s\t[%s]\n", value,
9f36e2c4 802 type, iter->name, iter->module_name);
ea07890a 803 } else
668533dc 804 seq_printf(m, "%px %c %s\n", value,
9f36e2c4 805 iter->type, iter->name);
1da177e4
LT
806 return 0;
807}
808
15ad7cdc 809static const struct seq_operations kallsyms_op = {
1da177e4
LT
810 .start = s_start,
811 .next = s_next,
812 .stop = s_stop,
813 .show = s_show
814};
815
647cafa2
AM
816#ifdef CONFIG_BPF_SYSCALL
817
818struct bpf_iter__ksym {
819 __bpf_md_ptr(struct bpf_iter_meta *, meta);
820 __bpf_md_ptr(struct kallsym_iter *, ksym);
821};
822
823static int ksym_prog_seq_show(struct seq_file *m, bool in_stop)
824{
825 struct bpf_iter__ksym ctx;
826 struct bpf_iter_meta meta;
827 struct bpf_prog *prog;
828
829 meta.seq = m;
830 prog = bpf_iter_get_info(&meta, in_stop);
831 if (!prog)
832 return 0;
833
834 ctx.meta = &meta;
835 ctx.ksym = m ? m->private : NULL;
836 return bpf_iter_run_prog(prog, &ctx);
837}
838
839static int bpf_iter_ksym_seq_show(struct seq_file *m, void *p)
840{
841 return ksym_prog_seq_show(m, false);
842}
843
844static void bpf_iter_ksym_seq_stop(struct seq_file *m, void *p)
845{
846 if (!p)
847 (void) ksym_prog_seq_show(m, true);
848 else
849 s_stop(m, p);
850}
851
852static const struct seq_operations bpf_iter_ksym_ops = {
853 .start = s_start,
854 .next = s_next,
855 .stop = bpf_iter_ksym_seq_stop,
856 .show = bpf_iter_ksym_seq_show,
857};
858
859static int bpf_iter_ksym_init(void *priv_data, struct bpf_iter_aux_info *aux)
860{
861 struct kallsym_iter *iter = priv_data;
862
863 reset_iter(iter, 0);
864
865 /* cache here as in kallsyms_open() case; use current process
866 * credentials to tell BPF iterators if values should be shown.
867 */
868 iter->show_value = kallsyms_show_value(current_cred());
869
870 return 0;
871}
872
873DEFINE_BPF_ITER_FUNC(ksym, struct bpf_iter_meta *meta, struct kallsym_iter *ksym)
874
875static const struct bpf_iter_seq_info ksym_iter_seq_info = {
876 .seq_ops = &bpf_iter_ksym_ops,
877 .init_seq_private = bpf_iter_ksym_init,
878 .fini_seq_private = NULL,
879 .seq_priv_size = sizeof(struct kallsym_iter),
880};
881
882static struct bpf_iter_reg ksym_iter_reg_info = {
883 .target = "ksym",
884 .feature = BPF_ITER_RESCHED,
885 .ctx_arg_info_size = 1,
886 .ctx_arg_info = {
887 { offsetof(struct bpf_iter__ksym, ksym),
888 PTR_TO_BTF_ID_OR_NULL },
889 },
890 .seq_info = &ksym_iter_seq_info,
891};
892
893BTF_ID_LIST(btf_ksym_iter_id)
894BTF_ID(struct, kallsym_iter)
895
896static int __init bpf_ksym_iter_register(void)
897{
898 ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id;
899 return bpf_iter_reg_target(&ksym_iter_reg_info);
900}
901
902late_initcall(bpf_ksym_iter_register);
903
904#endif /* CONFIG_BPF_SYSCALL */
905
1da177e4
LT
906static int kallsyms_open(struct inode *inode, struct file *file)
907{
ad6ccfad
MK
908 /*
909 * We keep iterator in m->private, since normal case is to
1da177e4 910 * s_start from where we left off, so we avoid doing
ad6ccfad
MK
911 * using get_symbol_offset for every symbol.
912 */
1da177e4 913 struct kallsym_iter *iter;
0049f26a 914 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
1da177e4
LT
915 if (!iter)
916 return -ENOMEM;
917 reset_iter(iter, 0);
918
16025184
KC
919 /*
920 * Instead of checking this on every s_show() call, cache
921 * the result here at open time.
922 */
923 iter->show_value = kallsyms_show_value(file->f_cred);
0049f26a 924 return 0;
1da177e4
LT
925}
926
67fc4e0c
JW
927#ifdef CONFIG_KGDB_KDB
928const char *kdb_walk_kallsyms(loff_t *pos)
929{
930 static struct kallsym_iter kdb_walk_kallsyms_iter;
931 if (*pos == 0) {
932 memset(&kdb_walk_kallsyms_iter, 0,
933 sizeof(kdb_walk_kallsyms_iter));
934 reset_iter(&kdb_walk_kallsyms_iter, 0);
935 }
936 while (1) {
937 if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
938 return NULL;
939 ++*pos;
940 /* Some debugging symbols have no name. Ignore them. */
941 if (kdb_walk_kallsyms_iter.name[0])
942 return kdb_walk_kallsyms_iter.name;
943 }
944}
945#endif /* CONFIG_KGDB_KDB */
946
97a32539
AD
947static const struct proc_ops kallsyms_proc_ops = {
948 .proc_open = kallsyms_open,
949 .proc_read = seq_read,
950 .proc_lseek = seq_lseek,
951 .proc_release = seq_release_private,
1da177e4
LT
952};
953
954static int __init kallsyms_init(void)
955{
97a32539 956 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
1da177e4
LT
957 return 0;
958}
ad6ccfad 959device_initcall(kallsyms_init);