kallsyms.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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];
  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. if (symbolsize)
  182. *symbolsize = symbol_end - symbol_start;
  183. if (offset)
  184. *offset = addr - symbol_start;
  185. return low;
  186. }
  187. /*
  188. * Lookup an address but don't bother to find any names.
  189. */
  190. int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
  191. unsigned long *offset)
  192. {
  193. if (is_ksym_addr(addr))
  194. return !!get_symbol_pos(addr, symbolsize, offset);
  195. return !!module_address_lookup(addr, symbolsize, offset, NULL);
  196. }
  197. /*
  198. * Lookup an address
  199. * - modname is set to NULL if it's in the kernel
  200. * - we guarantee that the returned name is valid until we reschedule even if
  201. * it resides in a module
  202. * - we also guarantee that modname will be valid until rescheduled
  203. */
  204. const char *kallsyms_lookup(unsigned long addr,
  205. unsigned long *symbolsize,
  206. unsigned long *offset,
  207. char **modname, char *namebuf)
  208. {
  209. const char *msym;
  210. namebuf[KSYM_NAME_LEN - 1] = 0;
  211. namebuf[0] = 0;
  212. if (is_ksym_addr(addr)) {
  213. unsigned long pos;
  214. pos = get_symbol_pos(addr, symbolsize, offset);
  215. /* Grab name */
  216. kallsyms_expand_symbol(get_symbol_offset(pos), namebuf);
  217. if (modname)
  218. *modname = NULL;
  219. return namebuf;
  220. }
  221. /* see if it's in a module */
  222. msym = module_address_lookup(addr, symbolsize, offset, modname);
  223. if (msym)
  224. return strncpy(namebuf, msym, KSYM_NAME_LEN - 1);
  225. return NULL;
  226. }
  227. int lookup_symbol_name(unsigned long addr, char *symname)
  228. {
  229. symname[0] = '\0';
  230. symname[KSYM_NAME_LEN - 1] = '\0';
  231. if (is_ksym_addr(addr)) {
  232. unsigned long pos;
  233. pos = get_symbol_pos(addr, NULL, NULL);
  234. /* Grab name */
  235. kallsyms_expand_symbol(get_symbol_offset(pos), symname);
  236. return 0;
  237. }
  238. /* see if it's in a module */
  239. return lookup_module_symbol_name(addr, symname);
  240. }
  241. int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
  242. unsigned long *offset, char *modname, char *name)
  243. {
  244. name[0] = '\0';
  245. name[KSYM_NAME_LEN - 1] = '\0';
  246. if (is_ksym_addr(addr)) {
  247. unsigned long pos;
  248. pos = get_symbol_pos(addr, size, offset);
  249. /* Grab name */
  250. kallsyms_expand_symbol(get_symbol_offset(pos), name);
  251. modname[0] = '\0';
  252. return 0;
  253. }
  254. /* see if it's in a module */
  255. return lookup_module_symbol_attrs(addr, size, offset, modname, name);
  256. }
  257. /* Look up a kernel symbol and return it in a text buffer. */
  258. int sprint_symbol(char *buffer, unsigned long address)
  259. {
  260. char *modname;
  261. const char *name;
  262. unsigned long offset, size;
  263. char namebuf[KSYM_NAME_LEN];
  264. name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
  265. if (!name)
  266. return sprintf(buffer, "0x%lx", address);
  267. if (modname)
  268. return sprintf(buffer, "%s+%#lx/%#lx [%s]", name, offset,
  269. size, modname);
  270. else
  271. return sprintf(buffer, "%s+%#lx/%#lx", name, offset, size);
  272. }
  273. /* Look up a kernel symbol and print it to the kernel messages. */
  274. void __print_symbol(const char *fmt, unsigned long address)
  275. {
  276. char buffer[KSYM_SYMBOL_LEN];
  277. sprint_symbol(buffer, address);
  278. printk(fmt, buffer);
  279. }
  280. /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
  281. struct kallsym_iter
  282. {
  283. loff_t pos;
  284. unsigned long value;
  285. unsigned int nameoff; /* If iterating in core kernel symbols */
  286. char type;
  287. char name[KSYM_NAME_LEN];
  288. char module_name[MODULE_NAME_LEN];
  289. int exported;
  290. };
  291. static int get_ksymbol_mod(struct kallsym_iter *iter)
  292. {
  293. if (module_get_kallsym(iter->pos - kallsyms_num_syms, &iter->value,
  294. &iter->type, iter->name, iter->module_name,
  295. &iter->exported) < 0)
  296. return 0;
  297. return 1;
  298. }
  299. /* Returns space to next name. */
  300. static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
  301. {
  302. unsigned off = iter->nameoff;
  303. iter->module_name[0] = '\0';
  304. iter->value = kallsyms_addresses[iter->pos];
  305. iter->type = kallsyms_get_symbol_type(off);
  306. off = kallsyms_expand_symbol(off, iter->name);
  307. return off - iter->nameoff;
  308. }
  309. static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
  310. {
  311. iter->name[0] = '\0';
  312. iter->nameoff = get_symbol_offset(new_pos);
  313. iter->pos = new_pos;
  314. }
  315. /* Returns false if pos at or past end of file. */
  316. static int update_iter(struct kallsym_iter *iter, loff_t pos)
  317. {
  318. /* Module symbols can be accessed randomly. */
  319. if (pos >= kallsyms_num_syms) {
  320. iter->pos = pos;
  321. return get_ksymbol_mod(iter);
  322. }
  323. /* If we're not on the desired position, reset to new position. */
  324. if (pos != iter->pos)
  325. reset_iter(iter, pos);
  326. iter->nameoff += get_ksymbol_core(iter);
  327. iter->pos++;
  328. return 1;
  329. }
  330. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  331. {
  332. (*pos)++;
  333. if (!update_iter(m->private, *pos))
  334. return NULL;
  335. return p;
  336. }
  337. static void *s_start(struct seq_file *m, loff_t *pos)
  338. {
  339. if (!update_iter(m->private, *pos))
  340. return NULL;
  341. return m->private;
  342. }
  343. static void s_stop(struct seq_file *m, void *p)
  344. {
  345. }
  346. static int s_show(struct seq_file *m, void *p)
  347. {
  348. struct kallsym_iter *iter = m->private;
  349. /* Some debugging symbols have no name. Ignore them. */
  350. if (!iter->name[0])
  351. return 0;
  352. if (iter->module_name[0]) {
  353. char type;
  354. /* Label it "global" if it is exported,
  355. * "local" if not exported. */
  356. type = iter->exported ? toupper(iter->type) :
  357. tolower(iter->type);
  358. seq_printf(m, "%0*lx %c %s\t[%s]\n",
  359. (int)(2*sizeof(void*)),
  360. iter->value, type, iter->name, iter->module_name);
  361. } else
  362. seq_printf(m, "%0*lx %c %s\n",
  363. (int)(2*sizeof(void*)),
  364. iter->value, iter->type, iter->name);
  365. return 0;
  366. }
  367. static const struct seq_operations kallsyms_op = {
  368. .start = s_start,
  369. .next = s_next,
  370. .stop = s_stop,
  371. .show = s_show
  372. };
  373. static int kallsyms_open(struct inode *inode, struct file *file)
  374. {
  375. /* We keep iterator in m->private, since normal case is to
  376. * s_start from where we left off, so we avoid doing
  377. * using get_symbol_offset for every symbol */
  378. struct kallsym_iter *iter;
  379. int ret;
  380. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  381. if (!iter)
  382. return -ENOMEM;
  383. reset_iter(iter, 0);
  384. ret = seq_open(file, &kallsyms_op);
  385. if (ret == 0)
  386. ((struct seq_file *)file->private_data)->private = iter;
  387. else
  388. kfree(iter);
  389. return ret;
  390. }
  391. static const struct file_operations kallsyms_operations = {
  392. .open = kallsyms_open,
  393. .read = seq_read,
  394. .llseek = seq_lseek,
  395. .release = seq_release_private,
  396. };
  397. static int __init kallsyms_init(void)
  398. {
  399. struct proc_dir_entry *entry;
  400. entry = create_proc_entry("kallsyms", 0444, NULL);
  401. if (entry)
  402. entry->proc_fops = &kallsyms_operations;
  403. return 0;
  404. }
  405. __initcall(kallsyms_init);
  406. EXPORT_SYMBOL(__print_symbol);
  407. EXPORT_SYMBOL_GPL(sprint_symbol);