kallsyms.c 11 KB

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