kallsyms.c 12 KB

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