#include <linux/compiler.h>
#include <linux/rbtree.h>
#include <inttypes.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "dso.h"
#include "map.h"
#include "symbol.h"
#include <internal/lib.h>
#include "tests.h"
#include "debug.h"
#include "machine.h"
#define UM(x) map__unmap_ip(kallsyms_map, (x))
static bool is_ignored_symbol(const char *name, char type)
{
static const char * const ignored_symbols[] = {
"kallsyms_offsets",
"kallsyms_num_syms",
"kallsyms_names",
"kallsyms_markers",
"kallsyms_token_table",
"kallsyms_token_index",
"_SDA_BASE_",
"_SDA2_BASE_",
NULL
};
static const char * const ignored_prefixes[] = {
"$",
".L",
"__crc_",
"__efistub_",
"__kvm_nvhe_$",
"__kvm_nvhe_.L",
"__AArch64ADRPThunk_",
"__ARMV5PILongThunk_",
"__ARMV7PILongThunk_",
"__ThumbV7PILongThunk_",
"__LA25Thunk_",
"__microLA25Thunk_",
NULL
};
static const char * const ignored_suffixes[] = {
"_from_arm",
"_from_thumb",
"_veneer",
NULL
};
static const char * const ignored_matches[] = {
".long_branch.",
".plt_branch.",
NULL
};
const char * const *p;
for (p = ignored_symbols; *p; p++)
if (!strcmp(name, *p))
return true;
for (p = ignored_prefixes; *p; p++)
if (!strncmp(name, *p, strlen(*p)))
return true;
for (p = ignored_suffixes; *p; p++) {
int l = strlen(name) - strlen(*p);
if (l >= 0 && !strcmp(name + l, *p))
return true;
}
for (p = ignored_matches; *p; p++) {
if (strstr(name, *p))
return true;
}
if (type == 'U' || type == 'u')
return true;
if (type == 'N' || type == 'n')
return true;
if (toupper(type) == 'A') {
if (strcmp(name, "__kernel_syscall_via_break") &&
strcmp(name, "__kernel_syscall_via_epc") &&
strcmp(name, "__kernel_sigtramp") &&
strcmp(name, "__gp"))
return true;
}
return false;
}
struct test__vmlinux_matches_kallsyms_cb_args {
struct machine kallsyms;
struct map *vmlinux_map;
bool header_printed;
};
static int test__vmlinux_matches_kallsyms_cb1(struct map *map, void *data)
{
struct test__vmlinux_matches_kallsyms_cb_args *args = data;
struct dso *dso = map__dso(map);
struct map *pair = maps__find_by_name(args->kallsyms.kmaps,
(dso__kernel(dso) ? dso__short_name(dso) : dso__name(dso)));
if (pair) {
map__set_priv(pair);
map__put(pair);
} else {
if (!args->header_printed) {
pr_info("WARN: Maps only in vmlinux:\n");
args->header_printed = true;
}
map__fprintf(map, stderr);
}
return 0;
}
static int test__vmlinux_matches_kallsyms_cb2(struct map *map, void *data)
{
struct test__vmlinux_matches_kallsyms_cb_args *args = data;
struct map *pair;
u64 mem_start = map__unmap_ip(args->vmlinux_map, map__start(map));
u64 mem_end = map__unmap_ip(args->vmlinux_map, map__end(map));
pair = maps__find(args->kallsyms.kmaps, mem_start);
if (pair != NULL && !map__priv(pair) && map__start(pair) == mem_start) {
struct dso *dso = map__dso(map);
if (!args->header_printed) {
pr_info("WARN: Maps in vmlinux with a different name in kallsyms:\n");
args->header_printed = true;
}
pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
map__start(map), map__end(map), map__pgoff(map), dso__name(dso));
if (mem_end != map__end(pair))
pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64,
map__start(pair), map__end(pair), map__pgoff(pair));
pr_info(" %s\n", dso__name(dso));
map__set_priv(pair);
}
map__put(pair);
return 0;
}
static int test__vmlinux_matches_kallsyms_cb3(struct map *map, void *data)
{
struct test__vmlinux_matches_kallsyms_cb_args *args = data;
if (!map__priv(map)) {
if (!args->header_printed) {
pr_info("WARN: Maps only in kallsyms:\n");
args->header_printed = true;
}
map__fprintf(map, stderr);
}
return 0;
}
static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused,
int subtest __maybe_unused)
{
int err = TEST_FAIL;
struct rb_node *nd;
struct symbol *sym;
struct map *kallsyms_map;
struct machine vmlinux;
struct maps *maps;
u64 mem_start, mem_end;
struct test__vmlinux_matches_kallsyms_cb_args args;
machine__init(&args.kallsyms, "", HOST_KERNEL_ID);
machine__init(&vmlinux, "", HOST_KERNEL_ID);
maps = machine__kernel_maps(&vmlinux);
if (machine__create_kernel_maps(&args.kallsyms) < 0) {
pr_debug("machine__create_kernel_maps failed");
err = TEST_SKIP;
goto out;
}
if (machine__load_kallsyms(&args.kallsyms, "/proc/kallsyms") <= 0) {
pr_debug("machine__load_kallsyms failed");
err = TEST_SKIP;
goto out;
}
kallsyms_map = machine__kernel_map(&args.kallsyms);
if (machine__create_kernel_maps(&vmlinux) < 0) {
pr_info("machine__create_kernel_maps failed");
goto out;
}
args.vmlinux_map = machine__kernel_map(&vmlinux);
if (machine__load_vmlinux_path(&vmlinux) <= 0) {
pr_info("Couldn't find a vmlinux that matches the kernel running on this machine, skipping test\n");
err = TEST_SKIP;
goto out;
}
err = 0;
map__for_each_symbol(args.vmlinux_map, sym, nd) {
struct symbol *pair, *first_pair;
sym = rb_entry(nd, struct symbol, rb_node);
if (sym->start == sym->end)
continue;
mem_start = map__unmap_ip(args.vmlinux_map, sym->start);
mem_end = map__unmap_ip(args.vmlinux_map, sym->end);
first_pair = machine__find_kernel_symbol(&args.kallsyms, mem_start, NULL);
pair = first_pair;
if (pair && UM(pair->start) == mem_start) {
next_pair:
if (arch__compare_symbol_names(sym->name, pair->name) == 0) {
s64 skew = mem_end - UM(pair->end);
if (llabs(skew) >= page_size)
pr_debug("WARN: %#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
mem_start, sym->name, mem_end,
UM(pair->end));
continue;
} else {
pair = machine__find_kernel_symbol_by_name(&args.kallsyms,
sym->name, NULL);
if (pair) {
if (UM(pair->start) == mem_start)
goto next_pair;
pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
mem_start, sym->name, pair->name);
} else {
pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
mem_start, sym->name, first_pair->name);
}
continue;
}
} else if (mem_start == map__end(args.kallsyms.vmlinux_map)) {
continue;
} else if (is_ignored_symbol(sym->name, sym->type)) {
continue;
} else {
pr_debug("ERR : %#" PRIx64 ": %s not on kallsyms\n",
mem_start, sym->name);
}
err = -1;
}
if (verbose <= 0)
goto out;
args.header_printed = false;
maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb1, &args);
args.header_printed = false;
maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb2, &args);
args.header_printed = false;
maps = machine__kernel_maps(&args.kallsyms);
maps__for_each_map(maps, test__vmlinux_matches_kallsyms_cb3, &args);
out:
machine__exit(&args.kallsyms);
machine__exit(&vmlinux);
return err;
}
DEFINE_SUITE("vmlinux symtab matches kallsyms", vmlinux_matches_kallsyms);