vmlinux-kallsyms.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <ctype.h>
  7. #include <stdlib.h>
  8. #include "dso.h"
  9. #include "map.h"
  10. #include "symbol.h"
  11. #include <internal/lib.h> // page_size
  12. #include "tests.h"
  13. #include "debug.h"
  14. #include "machine.h"
  15. #define UM(x) map__unmap_ip(kallsyms_map, (x))
  16. static bool is_ignored_symbol(const char *name, char type)
  17. {
  18. /* Symbol names that exactly match to the following are ignored.*/
  19. static const char * const ignored_symbols[] = {
  20. /*
  21. * Symbols which vary between passes. Passes 1 and 2 must have
  22. * identical symbol lists. The kallsyms_* symbols below are
  23. * only added after pass 1, they would be included in pass 2
  24. * when --all-symbols is specified so exclude them to get a
  25. * stable symbol list.
  26. */
  27. "kallsyms_offsets",
  28. "kallsyms_num_syms",
  29. "kallsyms_names",
  30. "kallsyms_markers",
  31. "kallsyms_token_table",
  32. "kallsyms_token_index",
  33. /* Exclude linker generated symbols which vary between passes */
  34. "_SDA_BASE_", /* ppc */
  35. "_SDA2_BASE_", /* ppc */
  36. NULL
  37. };
  38. /* Symbol names that begin with the following are ignored.*/
  39. static const char * const ignored_prefixes[] = {
  40. "$", /* local symbols for ARM, MIPS, etc. */
  41. ".L", /* local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc. */
  42. "__crc_", /* modversions */
  43. "__efistub_", /* arm64 EFI stub namespace */
  44. "__kvm_nvhe_$", /* arm64 local symbols in non-VHE KVM namespace */
  45. "__kvm_nvhe_.L", /* arm64 local symbols in non-VHE KVM namespace */
  46. "__AArch64ADRPThunk_", /* arm64 lld */
  47. "__ARMV5PILongThunk_", /* arm lld */
  48. "__ARMV7PILongThunk_",
  49. "__ThumbV7PILongThunk_",
  50. "__LA25Thunk_", /* mips lld */
  51. "__microLA25Thunk_",
  52. NULL
  53. };
  54. /* Symbol names that end with the following are ignored.*/
  55. static const char * const ignored_suffixes[] = {
  56. "_from_arm", /* arm */
  57. "_from_thumb", /* arm */
  58. "_veneer", /* arm */
  59. NULL
  60. };
  61. /* Symbol names that contain the following are ignored.*/
  62. static const char * const ignored_matches[] = {
  63. ".long_branch.", /* ppc stub */
  64. ".plt_branch.", /* ppc stub */
  65. NULL
  66. };
  67. const char * const *p;
  68. for (p = ignored_symbols; *p; p++)
  69. if (!strcmp(name, *p))
  70. return true;
  71. for (p = ignored_prefixes; *p; p++)
  72. if (!strncmp(name, *p, strlen(*p)))
  73. return true;
  74. for (p = ignored_suffixes; *p; p++) {
  75. int l = strlen(name) - strlen(*p);
  76. if (l >= 0 && !strcmp(name + l, *p))
  77. return true;
  78. }
  79. for (p = ignored_matches; *p; p++) {
  80. if (strstr(name, *p))
  81. return true;
  82. }
  83. if (type == 'U' || type == 'u')
  84. return true;
  85. /* exclude debugging symbols */
  86. if (type == 'N' || type == 'n')
  87. return true;
  88. if (toupper(type) == 'A') {
  89. /* Keep these useful absolute symbols */
  90. if (strcmp(name, "__kernel_syscall_via_break") &&
  91. strcmp(name, "__kernel_syscall_via_epc") &&
  92. strcmp(name, "__kernel_sigtramp") &&
  93. strcmp(name, "__gp"))
  94. return true;
  95. }
  96. return false;
  97. }
  98. struct test__vmlinux_matches_kallsyms_cb_args {
  99. struct machine kallsyms;
  100. struct map *vmlinux_map;
  101. bool header_printed;
  102. };
  103. static int test__vmlinux_matches_kallsyms_cb1(struct map *map, void *data)
  104. {
  105. struct test__vmlinux_matches_kallsyms_cb_args *args = data;
  106. struct dso *dso = map__dso(map);
  107. /*
  108. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  109. * the kernel will have the path for the vmlinux file being used, so use
  110. * the short name, less descriptive but the same ("[kernel]" in both
  111. * cases.
  112. */
  113. struct map *pair = maps__find_by_name(args->kallsyms.kmaps,
  114. (dso__kernel(dso) ? dso__short_name(dso) : dso__name(dso)));
  115. if (pair) {
  116. map__set_priv(pair);
  117. map__put(pair);
  118. } else {
  119. if (!args->header_printed) {
  120. pr_info("WARN: Maps only in vmlinux:\n");
  121. args->header_printed = true;
  122. }
  123. map__fprintf(map, stderr);
  124. }
  125. return 0;
  126. }
  127. static int test__vmlinux_matches_kallsyms_cb2(struct map *map, void *data)
  128. {
  129. struct test__vmlinux_matches_kallsyms_cb_args *args = data;
  130. struct map *pair;
  131. u64 mem_start = map__unmap_ip(args->vmlinux_map, map__start(map));
  132. u64 mem_end = map__unmap_ip(args->vmlinux_map, map__end(map));
  133. pair = maps__find(args->kallsyms.kmaps, mem_start);
  134. if (pair != NULL && !map__priv(pair) && map__start(pair) == mem_start) {
  135. struct dso *dso = map__dso(map);
  136. if (!args->header_printed) {
  137. pr_info("WARN: Maps in vmlinux with a different name in kallsyms:\n");
  138. args->header_printed = true;
  139. }
  140. pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
  141. map__start(map), map__end(map), map__pgoff(map), dso__name(dso));
  142. if (mem_end != map__end(pair))
  143. pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64,
  144. map__start(pair), map__end(pair), map__pgoff(pair));
  145. pr_info(" %s\n", dso__name(dso));
  146. map__set_priv(pair);
  147. }
  148. map__put(pair);
  149. return 0;
  150. }
  151. static int test__vmlinux_matches_kallsyms_cb3(struct map *map, void *data)
  152. {
  153. struct test__vmlinux_matches_kallsyms_cb_args *args = data;
  154. if (!map__priv(map)) {
  155. if (!args->header_printed) {
  156. pr_info("WARN: Maps only in kallsyms:\n");
  157. args->header_printed = true;
  158. }
  159. map__fprintf(map, stderr);
  160. }
  161. return 0;
  162. }
  163. static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused,
  164. int subtest __maybe_unused)
  165. {
  166. int err = TEST_FAIL;
  167. struct rb_node *nd;
  168. struct symbol *sym;
  169. struct map *kallsyms_map;
  170. struct machine vmlinux;
  171. struct maps *maps;
  172. u64 mem_start, mem_end;
  173. struct test__vmlinux_matches_kallsyms_cb_args args;
  174. /*
  175. * Step 1:
  176. *
  177. * Init the machines that will hold kernel, modules obtained from
  178. * both vmlinux + .ko files and from /proc/kallsyms split by modules.
  179. */
  180. machine__init(&args.kallsyms, "", HOST_KERNEL_ID);
  181. machine__init(&vmlinux, "", HOST_KERNEL_ID);
  182. maps = machine__kernel_maps(&vmlinux);
  183. /*
  184. * Step 2:
  185. *
  186. * Create the kernel maps for kallsyms and the DSO where we will then
  187. * load /proc/kallsyms. Also create the modules maps from /proc/modules
  188. * and find the .ko files that match them in /lib/modules/`uname -r`/.
  189. */
  190. if (machine__create_kernel_maps(&args.kallsyms) < 0) {
  191. pr_debug("machine__create_kernel_maps failed");
  192. err = TEST_SKIP;
  193. goto out;
  194. }
  195. /*
  196. * Step 3:
  197. *
  198. * Load and split /proc/kallsyms into multiple maps, one per module.
  199. * Do not use kcore, as this test was designed before kcore support
  200. * and has parts that only make sense if using the non-kcore code.
  201. * XXX: extend it to stress the kcorre code as well, hint: the list
  202. * of modules extracted from /proc/kcore, in its current form, can't
  203. * be compacted against the list of modules found in the "vmlinux"
  204. * code and with the one got from /proc/modules from the "kallsyms" code.
  205. */
  206. if (machine__load_kallsyms(&args.kallsyms, "/proc/kallsyms") <= 0) {
  207. pr_debug("machine__load_kallsyms failed");
  208. err = TEST_SKIP;
  209. goto out;
  210. }
  211. /*
  212. * Step 4:
  213. *
  214. * kallsyms will be internally on demand sorted by name so that we can
  215. * find the reference relocation * symbol, i.e. the symbol we will use
  216. * to see if the running kernel was relocated by checking if it has the
  217. * same value in the vmlinux file we load.
  218. */
  219. kallsyms_map = machine__kernel_map(&args.kallsyms);
  220. /*
  221. * Step 5:
  222. *
  223. * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
  224. */
  225. if (machine__create_kernel_maps(&vmlinux) < 0) {
  226. pr_info("machine__create_kernel_maps failed");
  227. goto out;
  228. }
  229. args.vmlinux_map = machine__kernel_map(&vmlinux);
  230. /*
  231. * Step 6:
  232. *
  233. * Locate a vmlinux file in the vmlinux path that has a buildid that
  234. * matches the one of the running kernel.
  235. *
  236. * While doing that look if we find the ref reloc symbol, if we find it
  237. * we'll have its ref_reloc_symbol.unrelocated_addr and then
  238. * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
  239. * to fixup the symbols.
  240. */
  241. if (machine__load_vmlinux_path(&vmlinux) <= 0) {
  242. pr_info("Couldn't find a vmlinux that matches the kernel running on this machine, skipping test\n");
  243. err = TEST_SKIP;
  244. goto out;
  245. }
  246. err = 0;
  247. /*
  248. * Step 7:
  249. *
  250. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  251. * in the kallsyms dso. For the ones that are in both, check its names and
  252. * end addresses too.
  253. */
  254. map__for_each_symbol(args.vmlinux_map, sym, nd) {
  255. struct symbol *pair, *first_pair;
  256. sym = rb_entry(nd, struct symbol, rb_node);
  257. if (sym->start == sym->end)
  258. continue;
  259. mem_start = map__unmap_ip(args.vmlinux_map, sym->start);
  260. mem_end = map__unmap_ip(args.vmlinux_map, sym->end);
  261. first_pair = machine__find_kernel_symbol(&args.kallsyms, mem_start, NULL);
  262. pair = first_pair;
  263. if (pair && UM(pair->start) == mem_start) {
  264. next_pair:
  265. if (arch__compare_symbol_names(sym->name, pair->name) == 0) {
  266. /*
  267. * kallsyms don't have the symbol end, so we
  268. * set that by using the next symbol start - 1,
  269. * in some cases we get this up to a page
  270. * wrong, trace_kmalloc when I was developing
  271. * this code was one such example, 2106 bytes
  272. * off the real size. More than that and we
  273. * _really_ have a problem.
  274. */
  275. s64 skew = mem_end - UM(pair->end);
  276. if (llabs(skew) >= page_size)
  277. pr_debug("WARN: %#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
  278. mem_start, sym->name, mem_end,
  279. UM(pair->end));
  280. /*
  281. * Do not count this as a failure, because we
  282. * could really find a case where it's not
  283. * possible to get proper function end from
  284. * kallsyms.
  285. */
  286. continue;
  287. } else {
  288. pair = machine__find_kernel_symbol_by_name(&args.kallsyms,
  289. sym->name, NULL);
  290. if (pair) {
  291. if (UM(pair->start) == mem_start)
  292. goto next_pair;
  293. pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
  294. mem_start, sym->name, pair->name);
  295. } else {
  296. pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
  297. mem_start, sym->name, first_pair->name);
  298. }
  299. continue;
  300. }
  301. } else if (mem_start == map__end(args.kallsyms.vmlinux_map)) {
  302. /*
  303. * Ignore aliases to _etext, i.e. to the end of the kernel text area,
  304. * such as __indirect_thunk_end.
  305. */
  306. continue;
  307. } else if (is_ignored_symbol(sym->name, sym->type)) {
  308. /*
  309. * Ignore hidden symbols, see scripts/kallsyms.c for the details
  310. */
  311. continue;
  312. } else {
  313. pr_debug("ERR : %#" PRIx64 ": %s not on kallsyms\n",
  314. mem_start, sym->name);
  315. }
  316. err = -1;
  317. }
  318. if (verbose <= 0)
  319. goto out;
  320. args.header_printed = false;
  321. maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb1, &args);
  322. args.header_printed = false;
  323. maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb2, &args);
  324. args.header_printed = false;
  325. maps = machine__kernel_maps(&args.kallsyms);
  326. maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb3, &args);
  327. out:
  328. machine__exit(&args.kallsyms);
  329. machine__exit(&vmlinux);
  330. return err;
  331. }
  332. DEFINE_SUITE("vmlinux symtab matches kallsyms", vmlinux_matches_kallsyms);