kallsyms.c 22 KB

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