vmlinux-kallsyms.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <linux/compiler.h>
  2. #include <linux/rbtree.h>
  3. #include <string.h>
  4. #include "map.h"
  5. #include "symbol.h"
  6. #include "util.h"
  7. #include "tests.h"
  8. #include "debug.h"
  9. #include "machine.h"
  10. static int vmlinux_matches_kallsyms_filter(struct map *map __maybe_unused,
  11. struct symbol *sym)
  12. {
  13. bool *visited = symbol__priv(sym);
  14. *visited = true;
  15. return 0;
  16. }
  17. int test__vmlinux_matches_kallsyms(void)
  18. {
  19. int err = -1;
  20. struct rb_node *nd;
  21. struct symbol *sym;
  22. struct map *kallsyms_map, *vmlinux_map;
  23. struct machine kallsyms, vmlinux;
  24. enum map_type type = MAP__FUNCTION;
  25. struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
  26. /*
  27. * Step 1:
  28. *
  29. * Init the machines that will hold kernel, modules obtained from
  30. * both vmlinux + .ko files and from /proc/kallsyms split by modules.
  31. */
  32. machine__init(&kallsyms, "", HOST_KERNEL_ID);
  33. machine__init(&vmlinux, "", HOST_KERNEL_ID);
  34. /*
  35. * Step 2:
  36. *
  37. * Create the kernel maps for kallsyms and the DSO where we will then
  38. * load /proc/kallsyms. Also create the modules maps from /proc/modules
  39. * and find the .ko files that match them in /lib/modules/`uname -r`/.
  40. */
  41. if (machine__create_kernel_maps(&kallsyms) < 0) {
  42. pr_debug("machine__create_kernel_maps ");
  43. goto out;
  44. }
  45. /*
  46. * Step 3:
  47. *
  48. * Load and split /proc/kallsyms into multiple maps, one per module.
  49. */
  50. if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
  51. pr_debug("dso__load_kallsyms ");
  52. goto out;
  53. }
  54. /*
  55. * Step 4:
  56. *
  57. * kallsyms will be internally on demand sorted by name so that we can
  58. * find the reference relocation * symbol, i.e. the symbol we will use
  59. * to see if the running kernel was relocated by checking if it has the
  60. * same value in the vmlinux file we load.
  61. */
  62. kallsyms_map = machine__kernel_map(&kallsyms, type);
  63. sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
  64. if (sym == NULL) {
  65. pr_debug("dso__find_symbol_by_name ");
  66. goto out;
  67. }
  68. ref_reloc_sym.addr = sym->start;
  69. /*
  70. * Step 5:
  71. *
  72. * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
  73. */
  74. if (machine__create_kernel_maps(&vmlinux) < 0) {
  75. pr_debug("machine__create_kernel_maps ");
  76. goto out;
  77. }
  78. vmlinux_map = machine__kernel_map(&vmlinux, type);
  79. map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
  80. /*
  81. * Step 6:
  82. *
  83. * Locate a vmlinux file in the vmlinux path that has a buildid that
  84. * matches the one of the running kernel.
  85. *
  86. * While doing that look if we find the ref reloc symbol, if we find it
  87. * we'll have its ref_reloc_symbol.unrelocated_addr and then
  88. * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
  89. * to fixup the symbols.
  90. */
  91. if (machine__load_vmlinux_path(&vmlinux, type,
  92. vmlinux_matches_kallsyms_filter) <= 0) {
  93. pr_debug("Couldn't find a vmlinux that matches the kernel running on this machine, skipping test\n");
  94. err = TEST_SKIP;
  95. goto out;
  96. }
  97. err = 0;
  98. /*
  99. * Step 7:
  100. *
  101. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  102. * in the kallsyms dso. For the ones that are in both, check its names and
  103. * end addresses too.
  104. */
  105. for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
  106. struct symbol *pair, *first_pair;
  107. bool backwards = true;
  108. sym = rb_entry(nd, struct symbol, rb_node);
  109. if (sym->start == sym->end)
  110. continue;
  111. first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
  112. pair = first_pair;
  113. if (pair && pair->start == sym->start) {
  114. next_pair:
  115. if (strcmp(sym->name, pair->name) == 0) {
  116. /*
  117. * kallsyms don't have the symbol end, so we
  118. * set that by using the next symbol start - 1,
  119. * in some cases we get this up to a page
  120. * wrong, trace_kmalloc when I was developing
  121. * this code was one such example, 2106 bytes
  122. * off the real size. More than that and we
  123. * _really_ have a problem.
  124. */
  125. s64 skew = sym->end - pair->end;
  126. if (llabs(skew) < page_size)
  127. continue;
  128. pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
  129. sym->start, sym->name, sym->end, pair->end);
  130. } else {
  131. struct rb_node *nnd;
  132. detour:
  133. nnd = backwards ? rb_prev(&pair->rb_node) :
  134. rb_next(&pair->rb_node);
  135. if (nnd) {
  136. struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
  137. if (next->start == sym->start) {
  138. pair = next;
  139. goto next_pair;
  140. }
  141. }
  142. if (backwards) {
  143. backwards = false;
  144. pair = first_pair;
  145. goto detour;
  146. }
  147. pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
  148. sym->start, sym->name, pair->name);
  149. }
  150. } else
  151. pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
  152. err = -1;
  153. }
  154. if (!verbose)
  155. goto out;
  156. pr_info("Maps only in vmlinux:\n");
  157. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  158. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  159. /*
  160. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  161. * the kernel will have the path for the vmlinux file being used,
  162. * so use the short name, less descriptive but the same ("[kernel]" in
  163. * both cases.
  164. */
  165. pair = map_groups__find_by_name(&kallsyms.kmaps, type,
  166. (pos->dso->kernel ?
  167. pos->dso->short_name :
  168. pos->dso->name));
  169. if (pair)
  170. pair->priv = 1;
  171. else
  172. map__fprintf(pos, stderr);
  173. }
  174. pr_info("Maps in vmlinux with a different name in kallsyms:\n");
  175. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  176. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  177. pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
  178. if (pair == NULL || pair->priv)
  179. continue;
  180. if (pair->start == pos->start) {
  181. pair->priv = 1;
  182. pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
  183. pos->start, pos->end, pos->pgoff, pos->dso->name);
  184. if (pos->pgoff != pair->pgoff || pos->end != pair->end)
  185. pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
  186. pair->start, pair->end, pair->pgoff);
  187. pr_info(" %s\n", pair->dso->name);
  188. pair->priv = 1;
  189. }
  190. }
  191. pr_info("Maps only in kallsyms:\n");
  192. for (nd = rb_first(&kallsyms.kmaps.maps[type]);
  193. nd; nd = rb_next(nd)) {
  194. struct map *pos = rb_entry(nd, struct map, rb_node);
  195. if (!pos->priv)
  196. map__fprintf(pos, stderr);
  197. }
  198. out:
  199. machine__exit(&kallsyms);
  200. machine__exit(&vmlinux);
  201. return err;
  202. }