vmlinux-kallsyms.c 11 KB

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