vmlinux-kallsyms.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return -1;
  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("machine__load_vmlinux_path ");
  94. goto out;
  95. }
  96. err = 0;
  97. /*
  98. * Step 7:
  99. *
  100. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  101. * in the kallsyms dso. For the ones that are in both, check its names and
  102. * end addresses too.
  103. */
  104. for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
  105. struct symbol *pair, *first_pair;
  106. bool backwards = true;
  107. sym = rb_entry(nd, struct symbol, rb_node);
  108. if (sym->start == sym->end)
  109. continue;
  110. first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
  111. pair = first_pair;
  112. if (pair && pair->start == sym->start) {
  113. next_pair:
  114. if (strcmp(sym->name, pair->name) == 0) {
  115. /*
  116. * kallsyms don't have the symbol end, so we
  117. * set that by using the next symbol start - 1,
  118. * in some cases we get this up to a page
  119. * wrong, trace_kmalloc when I was developing
  120. * this code was one such example, 2106 bytes
  121. * off the real size. More than that and we
  122. * _really_ have a problem.
  123. */
  124. s64 skew = sym->end - pair->end;
  125. if (llabs(skew) < page_size)
  126. continue;
  127. pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
  128. sym->start, sym->name, sym->end, pair->end);
  129. } else {
  130. struct rb_node *nnd;
  131. detour:
  132. nnd = backwards ? rb_prev(&pair->rb_node) :
  133. rb_next(&pair->rb_node);
  134. if (nnd) {
  135. struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
  136. if (next->start == sym->start) {
  137. pair = next;
  138. goto next_pair;
  139. }
  140. }
  141. if (backwards) {
  142. backwards = false;
  143. pair = first_pair;
  144. goto detour;
  145. }
  146. pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
  147. sym->start, sym->name, pair->name);
  148. }
  149. } else
  150. pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
  151. err = -1;
  152. }
  153. if (!verbose)
  154. goto out;
  155. pr_info("Maps only in vmlinux:\n");
  156. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  157. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  158. /*
  159. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  160. * the kernel will have the path for the vmlinux file being used,
  161. * so use the short name, less descriptive but the same ("[kernel]" in
  162. * both cases.
  163. */
  164. pair = map_groups__find_by_name(&kallsyms.kmaps, type,
  165. (pos->dso->kernel ?
  166. pos->dso->short_name :
  167. pos->dso->name));
  168. if (pair)
  169. pair->priv = 1;
  170. else
  171. map__fprintf(pos, stderr);
  172. }
  173. pr_info("Maps in vmlinux with a different name in kallsyms:\n");
  174. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  175. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  176. pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
  177. if (pair == NULL || pair->priv)
  178. continue;
  179. if (pair->start == pos->start) {
  180. pair->priv = 1;
  181. pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
  182. pos->start, pos->end, pos->pgoff, pos->dso->name);
  183. if (pos->pgoff != pair->pgoff || pos->end != pair->end)
  184. pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
  185. pair->start, pair->end, pair->pgoff);
  186. pr_info(" %s\n", pair->dso->name);
  187. pair->priv = 1;
  188. }
  189. }
  190. pr_info("Maps only in kallsyms:\n");
  191. for (nd = rb_first(&kallsyms.kmaps.maps[type]);
  192. nd; nd = rb_next(nd)) {
  193. struct map *pos = rb_entry(nd, struct map, rb_node);
  194. if (!pos->priv)
  195. map__fprintf(pos, stderr);
  196. }
  197. out:
  198. return err;
  199. }