1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Obtain symbols from vmlinux for usage by kallsyms. Replaces mksysmap. 4 * 5 * To retain compatibility, it provides the same output as nm, only faster. 6 */ 7 8 #include <ctype.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <xalloc.h> 13 14 #include "elf-parse.h" 15 #include "kallsyms.h" 16 17 /* 18 * What mksysmap dropped from System.map, by name: symbols that are not needed 19 * there, or not suitable for kallsyms. 20 */ 21 static const char *const sysmap_omit_prefixes[] = { 22 /* local symbols for ARM, MIPS, etc. */ 23 "$", 24 /* local labels, .LBB, .Ltmpxxx, .L__unnamed_xx, .LASANPC, etc. */ 25 ".L", 26 /* arm64 EFI stub namespace */ 27 "__efistub_", 28 /* arm64 local symbols in PIE namespace */ 29 "__pi_$", 30 "__pi_.L", 31 /* arm64 local symbols in non-VHE KVM namespace */ 32 "__kvm_nvhe_$", 33 "__kvm_nvhe_.L", 34 /* CFI type identifiers */ 35 "__kcfi_typeid_", 36 "__kvm_nvhe___kcfi_typeid_", 37 "__pi___kcfi_typeid_", 38 /* CRC from modversions */ 39 "__crc_", 40 /* EXPORT_SYMBOL (symbol name) */ 41 "__kstrtab_", 42 /* EXPORT_SYMBOL (namespace) */ 43 "__kstrtabns_", 44 /* MODULE_DEVICE_TABLE (symbol name) */ 45 "__mod_device_table__", 46 }; 47 48 static const char *const sysmap_omit_suffixes[] = { 49 /* arm */ 50 "_from_arm", 51 "_from_thumb", 52 "_veneer", 53 }; 54 55 static const char *const sysmap_omit_names[] = { 56 /* for LoongArch? */ 57 "L0", 58 /* ppc */ 59 "_SDA_BASE_", 60 "_SDA2_BASE_", 61 }; 62 63 /* Symbols that contain the pattern anywhere are dropped. */ 64 static const char *const sysmap_omit_patterns[] = { 65 /* ppc stub */ 66 ".long_branch.", 67 ".plt_branch.", 68 }; 69 70 /* __<alnum>*Thunk_: the linker's range extension thunks on arm. */ 71 static bool is_range_thunk(const char *name) 72 { 73 const char *p; 74 75 if (!string_starts_with(name, "__")) 76 return false; 77 for (p = name + 2; isalnum((unsigned char)*p); p++) 78 ; 79 return p - name >= 7 && *p == '_' && strncmp(p - 5, "Thunk", 5) == 0; 80 } 81 82 /* __UNIQUE_ID_modinfo_<n>: the MODULE_INFO() strings of built-in code. */ 83 static bool is_modinfo_id(const char *name) 84 { 85 static const char prefix[] = "__UNIQUE_ID_modinfo_"; 86 const char *p; 87 88 if (!string_starts_with(name, prefix)) 89 return false; 90 for (p = name + strlen(prefix); isdigit((unsigned char)*p); p++) 91 ; 92 return !*p; 93 } 94 95 static bool sysmap_omits(const char *name, char type) 96 { 97 size_t i; 98 99 /* Absolute, undefined and debugging symbols. */ 100 if (type == 'a' || type == 'N' || type == 'U' || type == 'w') 101 return true; 102 103 for (i = 0; i < ARRAY_SIZE(sysmap_omit_prefixes); i++) 104 if (string_starts_with(name, sysmap_omit_prefixes[i])) 105 return true; 106 for (i = 0; i < ARRAY_SIZE(sysmap_omit_suffixes); i++) 107 if (string_ends_with(name, sysmap_omit_suffixes[i])) 108 return true; 109 for (i = 0; i < ARRAY_SIZE(sysmap_omit_names); i++) 110 if (strcmp(name, sysmap_omit_names[i]) == 0) 111 return true; 112 for (i = 0; i < ARRAY_SIZE(sysmap_omit_patterns); i++) 113 if (strstr(name, sysmap_omit_patterns[i])) 114 return true; 115 116 return is_range_thunk(name) || is_modinfo_id(name); 117 } 118 119 /* nm's letter for a symbol defined in a section, as BFD classifies it. */ 120 static char section_symbol_type(Elf_Shdr *shdr, const char *secname) 121 { 122 static const char *const debug_prefixes[] = { 123 ".debug", ".zdebug", ".gnu.debuglto_.debug_", 124 ".gnu.linkonce.wi.", ".line", ".stab", 125 }; 126 const uint64_t flags = shdr_flags(shdr); 127 size_t i; 128 129 if (flags & SHF_EXECINSTR) 130 return 't'; 131 if (flags & SHF_ALLOC) { 132 if (shdr_type(shdr) == SHT_NOBITS) 133 return 'b'; 134 return flags & SHF_WRITE ? 'd' : 'r'; 135 } 136 for (i = 0; i < ARRAY_SIZE(debug_prefixes); i++) 137 if (string_starts_with(secname, debug_prefixes[i])) 138 return 'N'; 139 if (shdr_type(shdr) != SHT_NOBITS && !(flags & SHF_WRITE)) 140 return 'n'; 141 return '?'; 142 } 143 144 /* The letter nm prints for a symbol, or 0 for one it leaves out. */ 145 static char elf_symbol_type(const struct elf_file *elf, Elf_Sym *sym) 146 { 147 unsigned int bind = sym_bind(sym), type = sym_type(sym); 148 unsigned int shndx = sym_shndx(sym); 149 Elf_Shdr *shdr; 150 char c; 151 152 if (type == STT_SECTION || type == STT_FILE) 153 return 0; 154 if (shndx == SHN_COMMON) 155 return 'C'; 156 if (shndx == SHN_UNDEF) { 157 if (bind == STB_WEAK) 158 return type == STT_OBJECT ? 'v' : 'w'; 159 return 'U'; 160 } 161 if (type == STT_GNU_IFUNC) 162 return 'i'; 163 if (bind == STB_WEAK) 164 return type == STT_OBJECT ? 'V' : 'W'; 165 if (bind == STB_GNU_UNIQUE) 166 return 'u'; 167 if (bind != STB_GLOBAL && bind != STB_LOCAL) 168 return '?'; 169 170 if (shndx == SHN_ABS) { 171 c = 'a'; 172 } else if (shndx < elf->shnum) { 173 shdr = elf_section(elf, shndx); 174 c = section_symbol_type(shdr, elf_section_name(elf, shdr)); 175 } else { 176 return '?'; 177 } 178 179 return bind == STB_GLOBAL ? toupper(c) : c; 180 } 181 182 /* nm -n order: by address, then by name. */ 183 static int compare_symbols(const void *a, const void *b) 184 { 185 const struct sysmap_symbol *sa = a, *sb = b; 186 187 if (sa->addr != sb->addr) 188 return sa->addr < sb->addr ? -1 : 1; 189 return strcmp(sa->name, sb->name); 190 } 191 192 /* The symbols "nm -n | mksysmap" would list, in that order. */ 193 static struct sysmap_symbol *elf_read_symbols(const struct elf_file *elf, 194 size_t *nr_kept) 195 { 196 struct sysmap_symbol *syms = xmalloc(elf->nr_syms * sizeof(*syms)); 197 size_t i, n = 0; 198 199 for (i = 1; i < elf->nr_syms; i++) { 200 Elf_Sym *sym = elf_symbol(elf, i); 201 const char *name = elf->strtab + sym_name(sym); 202 char type = elf_symbol_type(elf, sym); 203 204 if (!type || sysmap_omits(name, type)) 205 continue; 206 207 syms[n].addr = sym_value(sym); 208 syms[n].name = name; 209 syms[n].type = type; 210 n++; 211 } 212 213 qsort(syms, n, sizeof(*syms), compare_symbols); 214 *nr_kept = n; 215 return syms; 216 } 217 218 struct sysmap *sysmap_read(const char *path) 219 { 220 struct sysmap *map = xcalloc(1, sizeof(*map)); 221 222 /* The names point into the mapping; keep it until the map is freed. */ 223 if (elf_open_ro(path, (1 << ET_EXEC) | (1 << ET_DYN), &map->elf)) 224 exit(EXIT_FAILURE); 225 map->syms = elf_read_symbols(&map->elf, &map->nr_syms); 226 map->addr_width = elf_map_long_size(map->elf.base) * 2; 227 228 return map; 229 } 230 231 void sysmap_write(const struct sysmap *map, FILE *out) 232 { 233 size_t i; 234 235 for (i = 0; i < map->nr_syms; i++) { 236 const struct sysmap_symbol *s = &map->syms[i]; 237 238 fprintf(out, "%0*llx %c %s\n", map->addr_width, s->addr, s->type, 239 s->name); 240 } 241 } 242 243 void sysmap_free(struct sysmap *map) 244 { 245 free(map->syms); 246 elf_close(&map->elf); 247 free(map); 248 } 249