kallsyms.c 12 KB

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