kallsyms.c 15 KB

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