kallsyms.c 12 KB

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