kallsyms.c 16 KB

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