kallsyms.c 10 KB

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