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