kallsyms.c 10 KB

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