kallsyms.c 14 KB

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