kallsyms.c 12 KB

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