1 // SPDX-License-Identifier: GPL-2.0-only 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> 16 #include <linux/init.h> 17 #include <linux/seq_file.h> 18 #include <linux/fs.h> 19 #include <linux/kdb.h> 20 #include <linux/err.h> 21 #include <linux/proc_fs.h> 22 #include <linux/sched.h> /* for cond_resched */ 23 #include <linux/ctype.h> 24 #include <linux/slab.h> 25 #include <linux/filter.h> 26 #include <linux/ftrace.h> 27 #include <linux/kprobes.h> 28 #include <linux/build_bug.h> 29 #include <linux/compiler.h> 30 #include <linux/module.h> 31 #include <linux/kernel.h> 32 #include <linux/bsearch.h> 33 #include <linux/btf_ids.h> 34 35 #include "kallsyms_internal.h" 36 37 /* 38 * Expand a compressed symbol data into the resulting uncompressed string, 39 * if uncompressed string is too long (>= maxlen), it will be truncated, 40 * given the offset to where the symbol is in the compressed stream. 41 */ 42 static unsigned int kallsyms_expand_symbol(unsigned int off, 43 char *result, size_t maxlen) 44 { 45 int len, skipped_first = 0; 46 const char *tptr; 47 const u8 *data; 48 49 /* Get the compressed symbol length from the first symbol byte. */ 50 data = &kallsyms_names[off]; 51 len = *data; 52 data++; 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 } 61 62 /* 63 * Update the offset to return the offset for the next symbol on 64 * the compressed stream. 65 */ 66 off += len; 67 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]]; 74 data++; 75 len--; 76 77 while (*tptr) { 78 if (skipped_first) { 79 if (maxlen <= 1) 80 goto tail; 81 *result = *tptr; 82 result++; 83 maxlen--; 84 } else 85 skipped_first = 1; 86 tptr++; 87 } 88 } 89 90 tail: 91 if (maxlen) 92 *result = '\0'; 93 94 /* Return to offset to the next symbol. */ 95 return off; 96 } 97 98 /* 99 * Get symbol type information. This is encoded as a single char at the 100 * beginning of the symbol name. 101 */ 102 static char kallsyms_get_symbol_type(unsigned int off) 103 { 104 /* 105 * Get just the first code, look it up in the token table, 106 * and return the first char from this token. If MSB of length 107 * is 1, it is a "big" symbol, so needs an additional byte. 108 */ 109 if (kallsyms_names[off] & 0x80) 110 off++; 111 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]]; 112 } 113 114 115 /* 116 * Find the offset on the compressed stream given and index in the 117 * kallsyms array. 118 */ 119 static unsigned int get_symbol_offset(unsigned long pos) 120 { 121 const u8 *name; 122 int i, len; 123 124 /* 125 * Use the closest marker we have. We have markers every 256 positions, 126 * so that should be close enough. 127 */ 128 name = &kallsyms_names[kallsyms_markers[pos >> 8]]; 129 130 /* 131 * Sequentially scan all the symbols up to the point we're searching 132 * for. Every symbol is stored in a [<len>][<len> bytes of data] format, 133 * so we just need to add the len to the current pointer for every 134 * symbol we wish to skip. 135 */ 136 for (i = 0; i < (pos & 0xFF); i++) { 137 len = *name; 138 139 /* 140 * If MSB is 1, it is a "big" symbol, so we need to look into 141 * the next byte (and skip it, too). 142 */ 143 if ((len & 0x80) != 0) 144 len = ((len & 0x7F) | (name[1] << 7)) + 1; 145 146 name = name + len + 1; 147 } 148 149 return name - kallsyms_names; 150 } 151 152 unsigned long kallsyms_sym_address(int idx) 153 { 154 /* non-relocatable 32-bit kernels just embed the value directly */ 155 if (!IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_RELOCATABLE)) 156 return (u32)kallsyms_offsets[idx]; 157 return (unsigned long)offset_to_ptr(kallsyms_offsets + idx); 158 } 159 160 static unsigned int get_symbol_seq(int index) 161 { 162 unsigned int i, seq = 0; 163 164 for (i = 0; i < 3; i++) 165 seq = (seq << 8) | kallsyms_seqs_of_names[3 * index + i]; 166 167 return seq; 168 } 169 170 static int kallsyms_lookup_names(const char *name, 171 unsigned int *start, 172 unsigned int *end) 173 { 174 int ret; 175 int low, mid, high; 176 unsigned int seq, off; 177 char namebuf[KSYM_NAME_LEN]; 178 179 low = 0; 180 high = kallsyms_num_syms - 1; 181 182 while (low <= high) { 183 mid = low + (high - low) / 2; 184 seq = get_symbol_seq(mid); 185 off = get_symbol_offset(seq); 186 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 187 ret = strcmp(name, namebuf); 188 if (ret > 0) 189 low = mid + 1; 190 else if (ret < 0) 191 high = mid - 1; 192 else 193 break; 194 } 195 196 if (low > high) 197 return -ESRCH; 198 199 low = mid; 200 while (low) { 201 seq = get_symbol_seq(low - 1); 202 off = get_symbol_offset(seq); 203 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 204 if (strcmp(name, namebuf)) 205 break; 206 low--; 207 } 208 *start = low; 209 210 if (end) { 211 high = mid; 212 while (high < kallsyms_num_syms - 1) { 213 seq = get_symbol_seq(high + 1); 214 off = get_symbol_offset(seq); 215 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 216 if (strcmp(name, namebuf)) 217 break; 218 high++; 219 } 220 *end = high; 221 } 222 223 return 0; 224 } 225 226 /* Lookup the address for this symbol. Returns 0 if not found. */ 227 unsigned long kallsyms_lookup_name(const char *name) 228 { 229 int ret; 230 unsigned int i; 231 232 /* Skip the search for empty string. */ 233 if (!*name) 234 return 0; 235 236 ret = kallsyms_lookup_names(name, &i, NULL); 237 if (!ret) 238 return kallsyms_sym_address(get_symbol_seq(i)); 239 240 return module_kallsyms_lookup_name(name); 241 } 242 243 /* 244 * Iterate over all symbols in vmlinux. For symbols from modules use 245 * module_kallsyms_on_each_symbol instead. 246 */ 247 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long), 248 void *data) 249 { 250 char namebuf[KSYM_NAME_LEN]; 251 unsigned long i; 252 unsigned int off; 253 int ret; 254 255 for (i = 0, off = 0; i < kallsyms_num_syms; i++) { 256 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 257 ret = fn(data, namebuf, kallsyms_sym_address(i)); 258 if (ret != 0) 259 return ret; 260 cond_resched(); 261 } 262 return 0; 263 } 264 265 int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long), 266 const char *name, void *data) 267 { 268 int ret; 269 unsigned int i, start, end; 270 271 ret = kallsyms_lookup_names(name, &start, &end); 272 if (ret) 273 return 0; 274 275 for (i = start; !ret && i <= end; i++) { 276 ret = fn(data, kallsyms_sym_address(get_symbol_seq(i))); 277 cond_resched(); 278 } 279 280 return ret; 281 } 282 283 static unsigned long get_symbol_pos(unsigned long addr, 284 unsigned long *symbolsize, 285 unsigned long *offset) 286 { 287 unsigned long symbol_start = 0, symbol_end = 0; 288 unsigned long i, low, high, mid; 289 290 /* Do a binary search on the sorted kallsyms_offsets array. */ 291 low = 0; 292 high = kallsyms_num_syms; 293 294 while (high - low > 1) { 295 mid = low + (high - low) / 2; 296 if (kallsyms_sym_address(mid) <= addr) 297 low = mid; 298 else 299 high = mid; 300 } 301 302 /* 303 * Search for the first aliased symbol. Aliased 304 * symbols are symbols with the same address. 305 */ 306 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) 307 --low; 308 309 symbol_start = kallsyms_sym_address(low); 310 311 /* Search for next non-aliased symbol. */ 312 for (i = low + 1; i < kallsyms_num_syms; i++) { 313 if (kallsyms_sym_address(i) > symbol_start) { 314 symbol_end = kallsyms_sym_address(i); 315 break; 316 } 317 } 318 319 /* If we found no next symbol, we use the end of the section. */ 320 if (!symbol_end) { 321 if (is_kernel_inittext(addr)) 322 symbol_end = (unsigned long)_einittext; 323 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) 324 symbol_end = (unsigned long)_end; 325 else 326 symbol_end = (unsigned long)_etext; 327 } 328 329 if (symbolsize) 330 *symbolsize = symbol_end - symbol_start; 331 if (offset) 332 *offset = addr - symbol_start; 333 334 return low; 335 } 336 337 /* 338 * Lookup an address but don't bother to find any names. 339 */ 340 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, 341 unsigned long *offset) 342 { 343 char namebuf[KSYM_NAME_LEN]; 344 345 if (is_ksym_addr(addr)) { 346 get_symbol_pos(addr, symbolsize, offset); 347 return 1; 348 } 349 return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) || 350 !!bpf_address_lookup(addr, symbolsize, offset, namebuf); 351 } 352 353 static int kallsyms_lookup_buildid(unsigned long addr, 354 unsigned long *symbolsize, 355 unsigned long *offset, char **modname, 356 const unsigned char **modbuildid, char *namebuf) 357 { 358 int ret; 359 360 /* 361 * kallsyms_lookus() returns pointer to namebuf on success and 362 * NULL on error. But some callers ignore the return value. 363 * Instead they expect @namebuf filled either with valid 364 * or empty string. 365 */ 366 namebuf[0] = 0; 367 /* 368 * Initialize the module-related return values. They are not set 369 * when the symbol is in vmlinux or it is a bpf address. 370 */ 371 if (modname) 372 *modname = NULL; 373 if (modbuildid) 374 *modbuildid = NULL; 375 376 if (is_ksym_addr(addr)) { 377 unsigned long pos; 378 379 pos = get_symbol_pos(addr, symbolsize, offset); 380 /* Grab name */ 381 kallsyms_expand_symbol(get_symbol_offset(pos), 382 namebuf, KSYM_NAME_LEN); 383 384 return strlen(namebuf); 385 } 386 387 /* See if it's in a module or a BPF JITed image. */ 388 ret = module_address_lookup(addr, symbolsize, offset, 389 modname, modbuildid, namebuf); 390 if (!ret) 391 ret = bpf_address_lookup(addr, symbolsize, offset, namebuf); 392 393 if (!ret) 394 ret = ftrace_mod_address_lookup(addr, symbolsize, offset, 395 modname, modbuildid, namebuf); 396 397 return ret; 398 } 399 400 /* 401 * Lookup an address 402 * - modname is set to NULL if it's in the kernel. 403 * - We guarantee that the returned name is valid until we reschedule even if. 404 * It resides in a module. 405 * - We also guarantee that modname will be valid until rescheduled. 406 */ 407 const char *kallsyms_lookup(unsigned long addr, 408 unsigned long *symbolsize, 409 unsigned long *offset, 410 char **modname, char *namebuf) 411 { 412 int ret = kallsyms_lookup_buildid(addr, symbolsize, offset, modname, 413 NULL, namebuf); 414 415 if (!ret) 416 return NULL; 417 418 return namebuf; 419 } 420 421 int lookup_symbol_name(unsigned long addr, char *symname) 422 { 423 symname[0] = '\0'; 424 symname[KSYM_NAME_LEN - 1] = '\0'; 425 426 if (is_ksym_addr(addr)) { 427 unsigned long pos; 428 429 pos = get_symbol_pos(addr, NULL, NULL); 430 /* Grab name */ 431 kallsyms_expand_symbol(get_symbol_offset(pos), 432 symname, KSYM_NAME_LEN); 433 return 0; 434 } 435 /* See if it's in a module. */ 436 return lookup_module_symbol_name(addr, symname); 437 } 438 439 #ifdef CONFIG_STACKTRACE_BUILD_ID 440 441 static int append_buildid(char *buffer, const char *modname, 442 const unsigned char *buildid) 443 { 444 if (!modname) 445 return 0; 446 447 if (!buildid) { 448 pr_warn_once("Undefined buildid for the module %s\n", modname); 449 return 0; 450 } 451 452 /* build ID should match length of sprintf */ 453 #ifdef CONFIG_MODULES 454 static_assert(sizeof(typeof_member(struct module, build_id)) == 20); 455 #endif 456 457 return sprintf(buffer, " %20phN", buildid); 458 } 459 460 #else /* CONFIG_STACKTRACE_BUILD_ID */ 461 462 static int append_buildid(char *buffer, const char *modname, 463 const unsigned char *buildid) 464 { 465 return 0; 466 } 467 468 #endif /* CONFIG_STACKTRACE_BUILD_ID */ 469 470 /* Look up a kernel symbol and return it in a text buffer. */ 471 static int __sprint_symbol(char *buffer, unsigned long address, 472 int symbol_offset, int add_offset, int add_buildid) 473 { 474 char *modname; 475 const unsigned char *buildid; 476 unsigned long offset, size; 477 int len; 478 479 /* Prevent module removal until modname and modbuildid are printed */ 480 guard(rcu)(); 481 482 address += symbol_offset; 483 len = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid, 484 buffer); 485 if (!len) 486 return sprintf(buffer, "0x%lx", address - symbol_offset); 487 488 offset -= symbol_offset; 489 490 if (add_offset) 491 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size); 492 493 if (modname) { 494 len += sprintf(buffer + len, " [%s", modname); 495 if (add_buildid) 496 len += append_buildid(buffer + len, modname, buildid); 497 len += sprintf(buffer + len, "]"); 498 } 499 500 return len; 501 } 502 503 /** 504 * sprint_symbol - Look up a kernel symbol and return it in a text buffer 505 * @buffer: buffer to be stored 506 * @address: address to lookup 507 * 508 * This function looks up a kernel symbol with @address and stores its name, 509 * offset, size and module name to @buffer if possible. If no symbol was found, 510 * just saves its @address as is. 511 * 512 * This function returns the number of bytes stored in @buffer. 513 */ 514 int sprint_symbol(char *buffer, unsigned long address) 515 { 516 return __sprint_symbol(buffer, address, 0, 1, 0); 517 } 518 EXPORT_SYMBOL_GPL(sprint_symbol); 519 520 /** 521 * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer 522 * @buffer: buffer to be stored 523 * @address: address to lookup 524 * 525 * This function looks up a kernel symbol with @address and stores its name, 526 * offset, size, module name and module build ID to @buffer if possible. If no 527 * symbol was found, just saves its @address as is. 528 * 529 * This function returns the number of bytes stored in @buffer. 530 */ 531 int sprint_symbol_build_id(char *buffer, unsigned long address) 532 { 533 return __sprint_symbol(buffer, address, 0, 1, 1); 534 } 535 EXPORT_SYMBOL_GPL(sprint_symbol_build_id); 536 537 /** 538 * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer 539 * @buffer: buffer to be stored 540 * @address: address to lookup 541 * 542 * This function looks up a kernel symbol with @address and stores its name 543 * and module name to @buffer if possible. If no symbol was found, just saves 544 * its @address as is. 545 * 546 * This function returns the number of bytes stored in @buffer. 547 */ 548 int sprint_symbol_no_offset(char *buffer, unsigned long address) 549 { 550 return __sprint_symbol(buffer, address, 0, 0, 0); 551 } 552 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset); 553 554 /** 555 * sprint_backtrace - 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. 565 * 566 * This function returns the number of bytes stored in @buffer. 567 */ 568 int sprint_backtrace(char *buffer, unsigned long address) 569 { 570 return __sprint_symbol(buffer, address, -1, 1, 0); 571 } 572 573 /** 574 * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer 575 * @buffer: buffer to be stored 576 * @address: address to lookup 577 * 578 * This function is for stack backtrace and does the same thing as 579 * sprint_symbol() but with modified/decreased @address. If there is a 580 * tail-call to the function marked "noreturn", gcc optimized out code after 581 * the call so that the stack-saved return address could point outside of the 582 * caller. This function ensures that kallsyms will find the original caller 583 * by decreasing @address. This function also appends the module build ID to 584 * the @buffer if @address is within a kernel module. 585 * 586 * This function returns the number of bytes stored in @buffer. 587 */ 588 int sprint_backtrace_build_id(char *buffer, unsigned long address) 589 { 590 return __sprint_symbol(buffer, address, -1, 1, 1); 591 } 592 593 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ 594 struct kallsym_iter { 595 loff_t pos; 596 loff_t pos_mod_end; 597 loff_t pos_ftrace_mod_end; 598 loff_t pos_bpf_end; 599 unsigned long value; 600 unsigned int nameoff; /* If iterating in core kernel symbols. */ 601 char type; 602 char name[KSYM_NAME_LEN]; 603 char module_name[MODULE_NAME_LEN]; 604 int exported; 605 int show_value; 606 }; 607 608 static int get_ksymbol_mod(struct kallsym_iter *iter) 609 { 610 int ret = module_get_kallsym(iter->pos - kallsyms_num_syms, 611 &iter->value, &iter->type, 612 iter->name, iter->module_name, 613 &iter->exported); 614 if (ret < 0) { 615 iter->pos_mod_end = iter->pos; 616 return 0; 617 } 618 619 return 1; 620 } 621 622 /* 623 * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace 624 * purposes. In that case "__builtin__ftrace" is used as a module name, even 625 * though "__builtin__ftrace" is not a module. 626 */ 627 static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) 628 { 629 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end, 630 &iter->value, &iter->type, 631 iter->name, iter->module_name, 632 &iter->exported); 633 if (ret < 0) { 634 iter->pos_ftrace_mod_end = iter->pos; 635 return 0; 636 } 637 638 return 1; 639 } 640 641 static int get_ksymbol_bpf(struct kallsym_iter *iter) 642 { 643 int ret; 644 645 strscpy(iter->module_name, "bpf", MODULE_NAME_LEN); 646 iter->exported = 0; 647 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, 648 &iter->value, &iter->type, 649 iter->name); 650 if (ret < 0) { 651 iter->pos_bpf_end = iter->pos; 652 return 0; 653 } 654 655 return 1; 656 } 657 658 /* 659 * This uses "__builtin__kprobes" as a module name for symbols for pages 660 * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a 661 * module. 662 */ 663 static int get_ksymbol_kprobe(struct kallsym_iter *iter) 664 { 665 strscpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); 666 iter->exported = 0; 667 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, 668 &iter->value, &iter->type, 669 iter->name) < 0 ? 0 : 1; 670 } 671 672 /* Returns space to next name. */ 673 static unsigned long get_ksymbol_core(struct kallsym_iter *iter) 674 { 675 unsigned off = iter->nameoff; 676 677 iter->module_name[0] = '\0'; 678 iter->value = kallsyms_sym_address(iter->pos); 679 680 iter->type = kallsyms_get_symbol_type(off); 681 682 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); 683 684 return off - iter->nameoff; 685 } 686 687 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos) 688 { 689 iter->name[0] = '\0'; 690 iter->nameoff = get_symbol_offset(new_pos); 691 iter->pos = new_pos; 692 if (new_pos == 0) { 693 iter->pos_mod_end = 0; 694 iter->pos_ftrace_mod_end = 0; 695 iter->pos_bpf_end = 0; 696 } 697 } 698 699 /* 700 * The end position (last + 1) of each additional kallsyms section is recorded 701 * in iter->pos_..._end as each section is added, and so can be used to 702 * determine which get_ksymbol_...() function to call next. 703 */ 704 static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) 705 { 706 iter->pos = pos; 707 708 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) && 709 get_ksymbol_mod(iter)) 710 return 1; 711 712 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) && 713 get_ksymbol_ftrace_mod(iter)) 714 return 1; 715 716 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) && 717 get_ksymbol_bpf(iter)) 718 return 1; 719 720 return get_ksymbol_kprobe(iter); 721 } 722 723 /* Returns false if pos at or past end of file. */ 724 static int update_iter(struct kallsym_iter *iter, loff_t pos) 725 { 726 /* Module symbols can be accessed randomly. */ 727 if (pos >= kallsyms_num_syms) 728 return update_iter_mod(iter, pos); 729 730 /* If we're not on the desired position, reset to new position. */ 731 if (pos != iter->pos) 732 reset_iter(iter, pos); 733 734 iter->nameoff += get_ksymbol_core(iter); 735 iter->pos++; 736 737 return 1; 738 } 739 740 static void *s_next(struct seq_file *m, void *p, loff_t *pos) 741 { 742 (*pos)++; 743 744 if (!update_iter(m->private, *pos)) 745 return NULL; 746 return p; 747 } 748 749 static void *s_start(struct seq_file *m, loff_t *pos) 750 { 751 if (!update_iter(m->private, *pos)) 752 return NULL; 753 return m->private; 754 } 755 756 static void s_stop(struct seq_file *m, void *p) 757 { 758 } 759 760 static int s_show(struct seq_file *m, void *p) 761 { 762 void *value; 763 struct kallsym_iter *iter = m->private; 764 765 /* Some debugging symbols have no name. Ignore them. */ 766 if (!iter->name[0]) 767 return 0; 768 769 value = iter->show_value ? (void *)iter->value : NULL; 770 771 if (iter->module_name[0]) { 772 char type; 773 774 /* 775 * Label it "global" if it is exported, 776 * "local" if not exported. 777 */ 778 type = iter->exported ? toupper(iter->type) : 779 tolower(iter->type); 780 seq_printf(m, "%px %c %s\t[%s]\n", value, 781 type, iter->name, iter->module_name); 782 } else 783 seq_printf(m, "%px %c %s\n", value, 784 iter->type, iter->name); 785 return 0; 786 } 787 788 static const struct seq_operations kallsyms_op = { 789 .start = s_start, 790 .next = s_next, 791 .stop = s_stop, 792 .show = s_show 793 }; 794 795 #ifdef CONFIG_BPF_SYSCALL 796 797 struct bpf_iter__ksym { 798 __bpf_md_ptr(struct bpf_iter_meta *, meta); 799 __bpf_md_ptr(struct kallsym_iter *, ksym); 800 }; 801 802 static int ksym_prog_seq_show(struct seq_file *m, bool in_stop) 803 { 804 struct bpf_iter__ksym ctx; 805 struct bpf_iter_meta meta; 806 struct bpf_prog *prog; 807 808 meta.seq = m; 809 prog = bpf_iter_get_info(&meta, in_stop); 810 if (!prog) 811 return 0; 812 813 ctx.meta = &meta; 814 ctx.ksym = m ? m->private : NULL; 815 return bpf_iter_run_prog(prog, &ctx); 816 } 817 818 static int bpf_iter_ksym_seq_show(struct seq_file *m, void *p) 819 { 820 return ksym_prog_seq_show(m, false); 821 } 822 823 static void bpf_iter_ksym_seq_stop(struct seq_file *m, void *p) 824 { 825 if (!p) 826 (void) ksym_prog_seq_show(m, true); 827 else 828 s_stop(m, p); 829 } 830 831 static const struct seq_operations bpf_iter_ksym_ops = { 832 .start = s_start, 833 .next = s_next, 834 .stop = bpf_iter_ksym_seq_stop, 835 .show = bpf_iter_ksym_seq_show, 836 }; 837 838 static int bpf_iter_ksym_init(void *priv_data, struct bpf_iter_aux_info *aux) 839 { 840 struct kallsym_iter *iter = priv_data; 841 842 reset_iter(iter, 0); 843 844 /* cache here as in kallsyms_open() case; use current process 845 * credentials to tell BPF iterators if values should be shown. 846 */ 847 iter->show_value = kallsyms_show_value(current_cred()); 848 849 return 0; 850 } 851 852 DEFINE_BPF_ITER_FUNC(ksym, struct bpf_iter_meta *meta, struct kallsym_iter *ksym) 853 854 static const struct bpf_iter_seq_info ksym_iter_seq_info = { 855 .seq_ops = &bpf_iter_ksym_ops, 856 .init_seq_private = bpf_iter_ksym_init, 857 .fini_seq_private = NULL, 858 .seq_priv_size = sizeof(struct kallsym_iter), 859 }; 860 861 static struct bpf_iter_reg ksym_iter_reg_info = { 862 .target = "ksym", 863 .feature = BPF_ITER_RESCHED, 864 .ctx_arg_info_size = 1, 865 .ctx_arg_info = { 866 { offsetof(struct bpf_iter__ksym, ksym), 867 PTR_TO_BTF_ID_OR_NULL }, 868 }, 869 .seq_info = &ksym_iter_seq_info, 870 }; 871 872 BTF_ID_LIST_SINGLE(btf_ksym_iter_id, struct, kallsym_iter) 873 874 static int __init bpf_ksym_iter_register(void) 875 { 876 ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id; 877 return bpf_iter_reg_target(&ksym_iter_reg_info); 878 } 879 880 late_initcall(bpf_ksym_iter_register); 881 882 #endif /* CONFIG_BPF_SYSCALL */ 883 884 static int kallsyms_open(struct inode *inode, struct file *file) 885 { 886 /* 887 * We keep iterator in m->private, since normal case is to 888 * s_start from where we left off, so we avoid doing 889 * using get_symbol_offset for every symbol. 890 */ 891 struct kallsym_iter *iter; 892 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); 893 if (!iter) 894 return -ENOMEM; 895 reset_iter(iter, 0); 896 897 /* 898 * Instead of checking this on every s_show() call, cache 899 * the result here at open time. 900 */ 901 iter->show_value = kallsyms_show_value(file->f_cred); 902 return 0; 903 } 904 905 #ifdef CONFIG_KGDB_KDB 906 const char *kdb_walk_kallsyms(loff_t *pos) 907 { 908 static struct kallsym_iter kdb_walk_kallsyms_iter; 909 if (*pos == 0) { 910 memset(&kdb_walk_kallsyms_iter, 0, 911 sizeof(kdb_walk_kallsyms_iter)); 912 reset_iter(&kdb_walk_kallsyms_iter, 0); 913 } 914 while (1) { 915 if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) 916 return NULL; 917 ++*pos; 918 /* Some debugging symbols have no name. Ignore them. */ 919 if (kdb_walk_kallsyms_iter.name[0]) 920 return kdb_walk_kallsyms_iter.name; 921 } 922 } 923 #endif /* CONFIG_KGDB_KDB */ 924 925 static const struct proc_ops kallsyms_proc_ops = { 926 .proc_open = kallsyms_open, 927 .proc_read = seq_read, 928 .proc_lseek = seq_lseek, 929 .proc_release = seq_release_private, 930 }; 931 932 static int __init kallsyms_init(void) 933 { 934 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); 935 return 0; 936 } 937 device_initcall(kallsyms_init); 938