kallsyms.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
  3. *
  4. * Rewritten and vastly simplified by Rusty Russell for in-kernel
  5. * module loader:
  6. * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
  7. *
  8. * ChangeLog:
  9. *
  10. * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
  11. * Changed the compression method from stem compression to "table lookup"
  12. * compression (see scripts/kallsyms.c for a more complete description)
  13. */
  14. #include <linux/kallsyms.h>
  15. #include <linux/module.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/mm.h>
  24. #include <linux/ctype.h>
  25. #include <linux/slab.h>
  26. #include <linux/filter.h>
  27. #include <linux/compiler.h>
  28. #include <asm/sections.h>
  29. #ifdef CONFIG_KALLSYMS_ALL
  30. #define all_var 1
  31. #else
  32. #define all_var 0
  33. #endif
  34. /*
  35. * These will be re-linked against their real values
  36. * during the second link stage.
  37. */
  38. extern const unsigned long kallsyms_addresses[] __weak;
  39. extern const int kallsyms_offsets[] __weak;
  40. extern const u8 kallsyms_names[] __weak;
  41. /*
  42. * Tell the compiler that the count isn't in the small data section if the arch
  43. * has one (eg: FRV).
  44. */
  45. extern const unsigned long kallsyms_num_syms
  46. __attribute__((weak, section(".rodata")));
  47. extern const unsigned long kallsyms_relative_base
  48. __attribute__((weak, section(".rodata")));
  49. extern const u8 kallsyms_token_table[] __weak;
  50. extern const u16 kallsyms_token_index[] __weak;
  51. extern const unsigned long kallsyms_markers[] __weak;
  52. static inline int is_kernel_inittext(unsigned long addr)
  53. {
  54. if (addr >= (unsigned long)_sinittext
  55. && addr <= (unsigned long)_einittext)
  56. return 1;
  57. return 0;
  58. }
  59. static inline int is_kernel_text(unsigned long addr)
  60. {
  61. if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
  62. arch_is_kernel_text(addr))
  63. return 1;
  64. return in_gate_area_no_mm(addr);
  65. }
  66. static inline int is_kernel(unsigned long addr)
  67. {
  68. if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
  69. return 1;
  70. return in_gate_area_no_mm(addr);
  71. }
  72. static int is_ksym_addr(unsigned long addr)
  73. {
  74. if (all_var)
  75. return is_kernel(addr);
  76. return is_kernel_text(addr) || is_kernel_inittext(addr);
  77. }
  78. /*
  79. * Expand a compressed symbol data into the resulting uncompressed string,
  80. * if uncompressed string is too long (>= maxlen), it will be truncated,
  81. * given the offset to where the symbol is in the compressed stream.
  82. */
  83. static unsigned int kallsyms_expand_symbol(unsigned int off,
  84. char *result, size_t maxlen)
  85. {
  86. int len, skipped_first = 0;
  87. const u8 *tptr, *data;
  88. /* Get the compressed symbol length from the first symbol byte. */
  89. data = &kallsyms_names[off];
  90. len = *data;
  91. data++;
  92. /*
  93. * Update the offset to return the offset for the next symbol on
  94. * the compressed stream.
  95. */
  96. off += len + 1;
  97. /*
  98. * For every byte on the compressed symbol data, copy the table
  99. * entry for that byte.
  100. */
  101. while (len) {
  102. tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
  103. data++;
  104. len--;
  105. while (*tptr) {
  106. if (skipped_first) {
  107. if (maxlen <= 1)
  108. goto tail;
  109. *result = *tptr;
  110. result++;
  111. maxlen--;
  112. } else
  113. skipped_first = 1;
  114. tptr++;
  115. }
  116. }
  117. tail:
  118. if (maxlen)
  119. *result = '\0';
  120. /* Return to offset to the next symbol. */
  121. return off;
  122. }
  123. /*
  124. * Get symbol type information. This is encoded as a single char at the
  125. * beginning of the symbol name.
  126. */
  127. static char kallsyms_get_symbol_type(unsigned int off)
  128. {
  129. /*
  130. * Get just the first code, look it up in the token table,
  131. * and return the first char from this token.
  132. */
  133. return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
  134. }
  135. /*
  136. * Find the offset on the compressed stream given and index in the
  137. * kallsyms array.
  138. */
  139. static unsigned int get_symbol_offset(unsigned long pos)
  140. {
  141. const u8 *name;
  142. int i;
  143. /*
  144. * Use the closest marker we have. We have markers every 256 positions,
  145. * so that should be close enough.
  146. */
  147. name = &kallsyms_names[kallsyms_markers[pos >> 8]];
  148. /*
  149. * Sequentially scan all the symbols up to the point we're searching
  150. * for. Every symbol is stored in a [<len>][<len> bytes of data] format,
  151. * so we just need to add the len to the current pointer for every
  152. * symbol we wish to skip.
  153. */
  154. for (i = 0; i < (pos & 0xFF); i++)
  155. name = name + (*name) + 1;
  156. return name - kallsyms_names;
  157. }
  158. static unsigned long kallsyms_sym_address(int idx)
  159. {
  160. if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
  161. return kallsyms_addresses[idx];
  162. /* values are unsigned offsets if --absolute-percpu is not in effect */
  163. if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU))
  164. return kallsyms_relative_base + (u32)kallsyms_offsets[idx];
  165. /* ...otherwise, positive offsets are absolute values */
  166. if (kallsyms_offsets[idx] >= 0)
  167. return kallsyms_offsets[idx];
  168. /* ...and negative offsets are relative to kallsyms_relative_base - 1 */
  169. return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
  170. }
  171. /* Lookup the address for this symbol. Returns 0 if not found. */
  172. unsigned long kallsyms_lookup_name(const char *name)
  173. {
  174. char namebuf[KSYM_NAME_LEN];
  175. unsigned long i;
  176. unsigned int off;
  177. for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
  178. off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
  179. if (strcmp(namebuf, name) == 0)
  180. return kallsyms_sym_address(i);
  181. }
  182. return module_kallsyms_lookup_name(name);
  183. }
  184. EXPORT_SYMBOL_GPL(kallsyms_lookup_name);
  185. int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
  186. unsigned long),
  187. void *data)
  188. {
  189. char namebuf[KSYM_NAME_LEN];
  190. unsigned long i;
  191. unsigned int off;
  192. int ret;
  193. for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
  194. off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
  195. ret = fn(data, namebuf, NULL, kallsyms_sym_address(i));
  196. if (ret != 0)
  197. return ret;
  198. }
  199. return module_kallsyms_on_each_symbol(fn, data);
  200. }
  201. EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol);
  202. static unsigned long get_symbol_pos(unsigned long addr,
  203. unsigned long *symbolsize,
  204. unsigned long *offset)
  205. {
  206. unsigned long symbol_start = 0, symbol_end = 0;
  207. unsigned long i, low, high, mid;
  208. /* This kernel should never had been booted. */
  209. if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
  210. BUG_ON(!kallsyms_addresses);
  211. else
  212. BUG_ON(!kallsyms_offsets);
  213. /* Do a binary search on the sorted kallsyms_addresses array. */
  214. low = 0;
  215. high = kallsyms_num_syms;
  216. while (high - low > 1) {
  217. mid = low + (high - low) / 2;
  218. if (kallsyms_sym_address(mid) <= addr)
  219. low = mid;
  220. else
  221. high = mid;
  222. }
  223. /*
  224. * Search for the first aliased symbol. Aliased
  225. * symbols are symbols with the same address.
  226. */
  227. while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
  228. --low;
  229. symbol_start = kallsyms_sym_address(low);
  230. /* Search for next non-aliased symbol. */
  231. for (i = low + 1; i < kallsyms_num_syms; i++) {
  232. if (kallsyms_sym_address(i) > symbol_start) {
  233. symbol_end = kallsyms_sym_address(i);
  234. break;
  235. }
  236. }
  237. /* If we found no next symbol, we use the end of the section. */
  238. if (!symbol_end) {
  239. if (is_kernel_inittext(addr))
  240. symbol_end = (unsigned long)_einittext;
  241. else if (all_var)
  242. symbol_end = (unsigned long)_end;
  243. else
  244. symbol_end = (unsigned long)_etext;
  245. }
  246. if (symbolsize)
  247. *symbolsize = symbol_end - symbol_start;
  248. if (offset)
  249. *offset = addr - symbol_start;
  250. return low;
  251. }
  252. /*
  253. * Lookup an address but don't bother to find any names.
  254. */
  255. int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
  256. unsigned long *offset)
  257. {
  258. char namebuf[KSYM_NAME_LEN];
  259. if (is_ksym_addr(addr))
  260. return !!get_symbol_pos(addr, symbolsize, offset);
  261. return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
  262. !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
  263. }
  264. /*
  265. * Lookup an address
  266. * - modname is set to NULL if it's in the kernel.
  267. * - We guarantee that the returned name is valid until we reschedule even if.
  268. * It resides in a module.
  269. * - We also guarantee that modname will be valid until rescheduled.
  270. */
  271. const char *kallsyms_lookup(unsigned long addr,
  272. unsigned long *symbolsize,
  273. unsigned long *offset,
  274. char **modname, char *namebuf)
  275. {
  276. const char *ret;
  277. namebuf[KSYM_NAME_LEN - 1] = 0;
  278. namebuf[0] = 0;
  279. if (is_ksym_addr(addr)) {
  280. unsigned long pos;
  281. pos = get_symbol_pos(addr, symbolsize, offset);
  282. /* Grab name */
  283. kallsyms_expand_symbol(get_symbol_offset(pos),
  284. namebuf, KSYM_NAME_LEN);
  285. if (modname)
  286. *modname = NULL;
  287. return namebuf;
  288. }
  289. /* See if it's in a module or a BPF JITed image. */
  290. ret = module_address_lookup(addr, symbolsize, offset,
  291. modname, namebuf);
  292. if (!ret)
  293. ret = bpf_address_lookup(addr, symbolsize,
  294. offset, modname, namebuf);
  295. return ret;
  296. }
  297. int lookup_symbol_name(unsigned long addr, char *symname)
  298. {
  299. symname[0] = '\0';
  300. symname[KSYM_NAME_LEN - 1] = '\0';
  301. if (is_ksym_addr(addr)) {
  302. unsigned long pos;
  303. pos = get_symbol_pos(addr, NULL, NULL);
  304. /* Grab name */
  305. kallsyms_expand_symbol(get_symbol_offset(pos),
  306. symname, KSYM_NAME_LEN);
  307. return 0;
  308. }
  309. /* See if it's in a module. */
  310. return lookup_module_symbol_name(addr, symname);
  311. }
  312. int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
  313. unsigned long *offset, char *modname, char *name)
  314. {
  315. name[0] = '\0';
  316. name[KSYM_NAME_LEN - 1] = '\0';
  317. if (is_ksym_addr(addr)) {
  318. unsigned long pos;
  319. pos = get_symbol_pos(addr, size, offset);
  320. /* Grab name */
  321. kallsyms_expand_symbol(get_symbol_offset(pos),
  322. name, KSYM_NAME_LEN);
  323. modname[0] = '\0';
  324. return 0;
  325. }
  326. /* See if it's in a module. */
  327. return lookup_module_symbol_attrs(addr, size, offset, modname, name);
  328. }
  329. /* Look up a kernel symbol and return it in a text buffer. */
  330. static int __sprint_symbol(char *buffer, unsigned long address,
  331. int symbol_offset, int add_offset)
  332. {
  333. char *modname;
  334. const char *name;
  335. unsigned long offset, size;
  336. int len;
  337. address += symbol_offset;
  338. name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
  339. if (!name)
  340. return sprintf(buffer, "0x%lx", address - symbol_offset);
  341. if (name != buffer)
  342. strcpy(buffer, name);
  343. len = strlen(buffer);
  344. offset -= symbol_offset;
  345. if (add_offset)
  346. len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
  347. if (modname)
  348. len += sprintf(buffer + len, " [%s]", modname);
  349. return len;
  350. }
  351. /**
  352. * sprint_symbol - Look up a kernel symbol and return it in a text buffer
  353. * @buffer: buffer to be stored
  354. * @address: address to lookup
  355. *
  356. * This function looks up a kernel symbol with @address and stores its name,
  357. * offset, size and module name to @buffer if possible. If no symbol was found,
  358. * just saves its @address as is.
  359. *
  360. * This function returns the number of bytes stored in @buffer.
  361. */
  362. int sprint_symbol(char *buffer, unsigned long address)
  363. {
  364. return __sprint_symbol(buffer, address, 0, 1);
  365. }
  366. EXPORT_SYMBOL_GPL(sprint_symbol);
  367. /**
  368. * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
  369. * @buffer: buffer to be stored
  370. * @address: address to lookup
  371. *
  372. * This function looks up a kernel symbol with @address and stores its name
  373. * and module name to @buffer if possible. If no symbol was found, just saves
  374. * its @address as is.
  375. *
  376. * This function returns the number of bytes stored in @buffer.
  377. */
  378. int sprint_symbol_no_offset(char *buffer, unsigned long address)
  379. {
  380. return __sprint_symbol(buffer, address, 0, 0);
  381. }
  382. EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
  383. /**
  384. * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
  385. * @buffer: buffer to be stored
  386. * @address: address to lookup
  387. *
  388. * This function is for stack backtrace and does the same thing as
  389. * sprint_symbol() but with modified/decreased @address. If there is a
  390. * tail-call to the function marked "noreturn", gcc optimized out code after
  391. * the call so that the stack-saved return address could point outside of the
  392. * caller. This function ensures that kallsyms will find the original caller
  393. * by decreasing @address.
  394. *
  395. * This function returns the number of bytes stored in @buffer.
  396. */
  397. int sprint_backtrace(char *buffer, unsigned long address)
  398. {
  399. return __sprint_symbol(buffer, address, -1, 1);
  400. }
  401. /* Look up a kernel symbol and print it to the kernel messages. */
  402. void __print_symbol(const char *fmt, unsigned long address)
  403. {
  404. char buffer[KSYM_SYMBOL_LEN];
  405. sprint_symbol(buffer, address);
  406. printk(fmt, buffer);
  407. }
  408. EXPORT_SYMBOL(__print_symbol);
  409. /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
  410. struct kallsym_iter {
  411. loff_t pos;
  412. loff_t pos_mod_end;
  413. unsigned long value;
  414. unsigned int nameoff; /* If iterating in core kernel symbols. */
  415. char type;
  416. char name[KSYM_NAME_LEN];
  417. char module_name[MODULE_NAME_LEN];
  418. int exported;
  419. };
  420. static int get_ksymbol_mod(struct kallsym_iter *iter)
  421. {
  422. int ret = module_get_kallsym(iter->pos - kallsyms_num_syms,
  423. &iter->value, &iter->type,
  424. iter->name, iter->module_name,
  425. &iter->exported);
  426. if (ret < 0) {
  427. iter->pos_mod_end = iter->pos;
  428. return 0;
  429. }
  430. return 1;
  431. }
  432. static int get_ksymbol_bpf(struct kallsym_iter *iter)
  433. {
  434. iter->module_name[0] = '\0';
  435. iter->exported = 0;
  436. return bpf_get_kallsym(iter->pos - iter->pos_mod_end,
  437. &iter->value, &iter->type,
  438. iter->name) < 0 ? 0 : 1;
  439. }
  440. /* Returns space to next name. */
  441. static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
  442. {
  443. unsigned off = iter->nameoff;
  444. iter->module_name[0] = '\0';
  445. iter->value = kallsyms_sym_address(iter->pos);
  446. iter->type = kallsyms_get_symbol_type(off);
  447. off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
  448. return off - iter->nameoff;
  449. }
  450. static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
  451. {
  452. iter->name[0] = '\0';
  453. iter->nameoff = get_symbol_offset(new_pos);
  454. iter->pos = new_pos;
  455. if (new_pos == 0)
  456. iter->pos_mod_end = 0;
  457. }
  458. static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
  459. {
  460. iter->pos = pos;
  461. if (iter->pos_mod_end > 0 &&
  462. iter->pos_mod_end < iter->pos)
  463. return get_ksymbol_bpf(iter);
  464. if (!get_ksymbol_mod(iter))
  465. return get_ksymbol_bpf(iter);
  466. return 1;
  467. }
  468. /* Returns false if pos at or past end of file. */
  469. static int update_iter(struct kallsym_iter *iter, loff_t pos)
  470. {
  471. /* Module symbols can be accessed randomly. */
  472. if (pos >= kallsyms_num_syms)
  473. return update_iter_mod(iter, pos);
  474. /* If we're not on the desired position, reset to new position. */
  475. if (pos != iter->pos)
  476. reset_iter(iter, pos);
  477. iter->nameoff += get_ksymbol_core(iter);
  478. iter->pos++;
  479. return 1;
  480. }
  481. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  482. {
  483. (*pos)++;
  484. if (!update_iter(m->private, *pos))
  485. return NULL;
  486. return p;
  487. }
  488. static void *s_start(struct seq_file *m, loff_t *pos)
  489. {
  490. if (!update_iter(m->private, *pos))
  491. return NULL;
  492. return m->private;
  493. }
  494. static void s_stop(struct seq_file *m, void *p)
  495. {
  496. }
  497. static int s_show(struct seq_file *m, void *p)
  498. {
  499. struct kallsym_iter *iter = m->private;
  500. /* Some debugging symbols have no name. Ignore them. */
  501. if (!iter->name[0])
  502. return 0;
  503. if (iter->module_name[0]) {
  504. char type;
  505. /*
  506. * Label it "global" if it is exported,
  507. * "local" if not exported.
  508. */
  509. type = iter->exported ? toupper(iter->type) :
  510. tolower(iter->type);
  511. seq_printf(m, "%pK %c %s\t[%s]\n", (void *)iter->value,
  512. type, iter->name, iter->module_name);
  513. } else
  514. seq_printf(m, "%pK %c %s\n", (void *)iter->value,
  515. iter->type, iter->name);
  516. return 0;
  517. }
  518. static const struct seq_operations kallsyms_op = {
  519. .start = s_start,
  520. .next = s_next,
  521. .stop = s_stop,
  522. .show = s_show
  523. };
  524. static int kallsyms_open(struct inode *inode, struct file *file)
  525. {
  526. /*
  527. * We keep iterator in m->private, since normal case is to
  528. * s_start from where we left off, so we avoid doing
  529. * using get_symbol_offset for every symbol.
  530. */
  531. struct kallsym_iter *iter;
  532. iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
  533. if (!iter)
  534. return -ENOMEM;
  535. reset_iter(iter, 0);
  536. return 0;
  537. }
  538. #ifdef CONFIG_KGDB_KDB
  539. const char *kdb_walk_kallsyms(loff_t *pos)
  540. {
  541. static struct kallsym_iter kdb_walk_kallsyms_iter;
  542. if (*pos == 0) {
  543. memset(&kdb_walk_kallsyms_iter, 0,
  544. sizeof(kdb_walk_kallsyms_iter));
  545. reset_iter(&kdb_walk_kallsyms_iter, 0);
  546. }
  547. while (1) {
  548. if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
  549. return NULL;
  550. ++*pos;
  551. /* Some debugging symbols have no name. Ignore them. */
  552. if (kdb_walk_kallsyms_iter.name[0])
  553. return kdb_walk_kallsyms_iter.name;
  554. }
  555. }
  556. #endif /* CONFIG_KGDB_KDB */
  557. static const struct file_operations kallsyms_operations = {
  558. .open = kallsyms_open,
  559. .read = seq_read,
  560. .llseek = seq_lseek,
  561. .release = seq_release_private,
  562. };
  563. static int __init kallsyms_init(void)
  564. {
  565. proc_create("kallsyms", 0444, NULL, &kallsyms_operations);
  566. return 0;
  567. }
  568. device_initcall(kallsyms_init);