kallsyms.c 11 KB

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