vmlinux-kallsyms.c 6.9 KB

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