kallsyms.c 12 KB

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