vmlinux-kallsyms.c 6.8 KB

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