1 // SPDX-License-Identifier: GPL-2.0-or-later 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 2 /* 3 * Module kallsyms support 3 * Module kallsyms support 4 * 4 * 5 * Copyright (C) 2010 Rusty Russell 5 * Copyright (C) 2010 Rusty Russell 6 */ 6 */ 7 7 8 #include <linux/module.h> 8 #include <linux/module.h> 9 #include <linux/module_symbol.h> 9 #include <linux/module_symbol.h> 10 #include <linux/kallsyms.h> 10 #include <linux/kallsyms.h> 11 #include <linux/buildid.h> 11 #include <linux/buildid.h> 12 #include <linux/bsearch.h> 12 #include <linux/bsearch.h> 13 #include "internal.h" 13 #include "internal.h" 14 14 15 /* Lookup exported symbol in given range of ke 15 /* Lookup exported symbol in given range of kernel_symbols */ 16 static const struct kernel_symbol *lookup_expo 16 static const struct kernel_symbol *lookup_exported_symbol(const char *name, 17 17 const struct kernel_symbol *start, 18 18 const struct kernel_symbol *stop) 19 { 19 { 20 return bsearch(name, start, stop - sta 20 return bsearch(name, start, stop - start, 21 sizeof(struct kernel_s 21 sizeof(struct kernel_symbol), cmp_name); 22 } 22 } 23 23 24 static int is_exported(const char *name, unsig 24 static int is_exported(const char *name, unsigned long value, 25 const struct module *mo 25 const struct module *mod) 26 { 26 { 27 const struct kernel_symbol *ks; 27 const struct kernel_symbol *ks; 28 28 29 if (!mod) 29 if (!mod) 30 ks = lookup_exported_symbol(na 30 ks = lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab); 31 else 31 else 32 ks = lookup_exported_symbol(na 32 ks = lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms); 33 33 34 return ks && kernel_symbol_value(ks) = 34 return ks && kernel_symbol_value(ks) == value; 35 } 35 } 36 36 37 /* As per nm */ 37 /* As per nm */ 38 static char elf_type(const Elf_Sym *sym, const 38 static char elf_type(const Elf_Sym *sym, const struct load_info *info) 39 { 39 { 40 const Elf_Shdr *sechdrs = info->sechdr 40 const Elf_Shdr *sechdrs = info->sechdrs; 41 41 42 if (ELF_ST_BIND(sym->st_info) == STB_W 42 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 43 if (ELF_ST_TYPE(sym->st_info) 43 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) 44 return 'v'; 44 return 'v'; 45 else 45 else 46 return 'w'; 46 return 'w'; 47 } 47 } 48 if (sym->st_shndx == SHN_UNDEF) 48 if (sym->st_shndx == SHN_UNDEF) 49 return 'U'; 49 return 'U'; 50 if (sym->st_shndx == SHN_ABS || sym->s 50 if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu) 51 return 'a'; 51 return 'a'; 52 if (sym->st_shndx >= SHN_LORESERVE) 52 if (sym->st_shndx >= SHN_LORESERVE) 53 return '?'; 53 return '?'; 54 if (sechdrs[sym->st_shndx].sh_flags & 54 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) 55 return 't'; 55 return 't'; 56 if (sechdrs[sym->st_shndx].sh_flags & 56 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC && 57 sechdrs[sym->st_shndx].sh_type != 57 sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { 58 if (!(sechdrs[sym->st_shndx].s 58 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) 59 return 'r'; 59 return 'r'; 60 else if (sechdrs[sym->st_shndx 60 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) 61 return 'g'; 61 return 'g'; 62 else 62 else 63 return 'd'; 63 return 'd'; 64 } 64 } 65 if (sechdrs[sym->st_shndx].sh_type == 65 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { 66 if (sechdrs[sym->st_shndx].sh_ 66 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) 67 return 's'; 67 return 's'; 68 else 68 else 69 return 'b'; 69 return 'b'; 70 } 70 } 71 if (strstarts(info->secstrings + sechd 71 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name, 72 ".debug")) { 72 ".debug")) { 73 return 'n'; 73 return 'n'; 74 } 74 } 75 return '?'; 75 return '?'; 76 } 76 } 77 77 78 static bool is_core_symbol(const Elf_Sym *src, 78 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs, 79 unsigned int shnum, 79 unsigned int shnum, unsigned int pcpundx) 80 { 80 { 81 const Elf_Shdr *sec; 81 const Elf_Shdr *sec; 82 enum mod_mem_type type; 82 enum mod_mem_type type; 83 83 84 if (src->st_shndx == SHN_UNDEF || 84 if (src->st_shndx == SHN_UNDEF || 85 src->st_shndx >= shnum || 85 src->st_shndx >= shnum || 86 !src->st_name) 86 !src->st_name) 87 return false; 87 return false; 88 88 89 #ifdef CONFIG_KALLSYMS_ALL 89 #ifdef CONFIG_KALLSYMS_ALL 90 if (src->st_shndx == pcpundx) 90 if (src->st_shndx == pcpundx) 91 return true; 91 return true; 92 #endif 92 #endif 93 93 94 sec = sechdrs + src->st_shndx; 94 sec = sechdrs + src->st_shndx; 95 type = sec->sh_entsize >> SH_ENTSIZE_T 95 type = sec->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; 96 if (!(sec->sh_flags & SHF_ALLOC) 96 if (!(sec->sh_flags & SHF_ALLOC) 97 #ifndef CONFIG_KALLSYMS_ALL 97 #ifndef CONFIG_KALLSYMS_ALL 98 || !(sec->sh_flags & SHF_EXECINSTR 98 || !(sec->sh_flags & SHF_EXECINSTR) 99 #endif 99 #endif 100 || mod_mem_type_is_init(type)) 100 || mod_mem_type_is_init(type)) 101 return false; 101 return false; 102 102 103 return true; 103 return true; 104 } 104 } 105 105 106 /* 106 /* 107 * We only allocate and copy the strings neede 107 * We only allocate and copy the strings needed by the parts of symtab 108 * we keep. This is simple, but has the effec 108 * we keep. This is simple, but has the effect of making multiple 109 * copies of duplicates. We could be more sop 109 * copies of duplicates. We could be more sophisticated, see 110 * linux-kernel thread starting with 110 * linux-kernel thread starting with 111 * <73defb5e4bca04a6431392cc341112b1@localhost 111 * <73defb5e4bca04a6431392cc341112b1@localhost>. 112 */ 112 */ 113 void layout_symtab(struct module *mod, struct 113 void layout_symtab(struct module *mod, struct load_info *info) 114 { 114 { 115 Elf_Shdr *symsect = info->sechdrs + in 115 Elf_Shdr *symsect = info->sechdrs + info->index.sym; 116 Elf_Shdr *strsect = info->sechdrs + in 116 Elf_Shdr *strsect = info->sechdrs + info->index.str; 117 const Elf_Sym *src; 117 const Elf_Sym *src; 118 unsigned int i, nsrc, ndst, strtab_siz 118 unsigned int i, nsrc, ndst, strtab_size = 0; 119 struct module_memory *mod_mem_data = & 119 struct module_memory *mod_mem_data = &mod->mem[MOD_DATA]; 120 struct module_memory *mod_mem_init_dat 120 struct module_memory *mod_mem_init_data = &mod->mem[MOD_INIT_DATA]; 121 121 122 /* Put symbol section at end of init p 122 /* Put symbol section at end of init part of module. */ 123 symsect->sh_flags |= SHF_ALLOC; 123 symsect->sh_flags |= SHF_ALLOC; 124 symsect->sh_entsize = module_get_offse 124 symsect->sh_entsize = module_get_offset_and_type(mod, MOD_INIT_DATA, 125 125 symsect, info->index.sym); 126 pr_debug("\t%s\n", info->secstrings + 126 pr_debug("\t%s\n", info->secstrings + symsect->sh_name); 127 127 128 src = (void *)info->hdr + symsect->sh_ 128 src = (void *)info->hdr + symsect->sh_offset; 129 nsrc = symsect->sh_size / sizeof(*src) 129 nsrc = symsect->sh_size / sizeof(*src); 130 130 131 /* Compute total space required for th 131 /* Compute total space required for the core symbols' strtab. */ 132 for (ndst = i = 0; i < nsrc; i++) { 132 for (ndst = i = 0; i < nsrc; i++) { 133 if (i == 0 || is_livepatch_mod 133 if (i == 0 || is_livepatch_module(mod) || 134 is_core_symbol(src + i, in 134 is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum, 135 info->index 135 info->index.pcpu)) { 136 strtab_size += strlen( 136 strtab_size += strlen(&info->strtab[src[i].st_name]) + 1; 137 ndst++; 137 ndst++; 138 } 138 } 139 } 139 } 140 140 141 /* Append room for core symbols at end 141 /* Append room for core symbols at end of core part. */ 142 info->symoffs = ALIGN(mod_mem_data->si 142 info->symoffs = ALIGN(mod_mem_data->size, symsect->sh_addralign ?: 1); 143 info->stroffs = mod_mem_data->size = i 143 info->stroffs = mod_mem_data->size = info->symoffs + ndst * sizeof(Elf_Sym); 144 mod_mem_data->size += strtab_size; 144 mod_mem_data->size += strtab_size; 145 /* Note add_kallsyms() computes strtab 145 /* Note add_kallsyms() computes strtab_size as core_typeoffs - stroffs */ 146 info->core_typeoffs = mod_mem_data->si 146 info->core_typeoffs = mod_mem_data->size; 147 mod_mem_data->size += ndst * sizeof(ch 147 mod_mem_data->size += ndst * sizeof(char); 148 148 149 /* Put string table section at end of 149 /* Put string table section at end of init part of module. */ 150 strsect->sh_flags |= SHF_ALLOC; 150 strsect->sh_flags |= SHF_ALLOC; 151 strsect->sh_entsize = module_get_offse 151 strsect->sh_entsize = module_get_offset_and_type(mod, MOD_INIT_DATA, 152 152 strsect, info->index.str); 153 pr_debug("\t%s\n", info->secstrings + 153 pr_debug("\t%s\n", info->secstrings + strsect->sh_name); 154 154 155 /* We'll tack temporary mod_kallsyms o 155 /* We'll tack temporary mod_kallsyms on the end. */ 156 mod_mem_init_data->size = ALIGN(mod_me 156 mod_mem_init_data->size = ALIGN(mod_mem_init_data->size, 157 __alig 157 __alignof__(struct mod_kallsyms)); 158 info->mod_kallsyms_init_off = mod_mem_ 158 info->mod_kallsyms_init_off = mod_mem_init_data->size; 159 159 160 mod_mem_init_data->size += sizeof(stru 160 mod_mem_init_data->size += sizeof(struct mod_kallsyms); 161 info->init_typeoffs = mod_mem_init_dat 161 info->init_typeoffs = mod_mem_init_data->size; 162 mod_mem_init_data->size += nsrc * size 162 mod_mem_init_data->size += nsrc * sizeof(char); 163 } 163 } 164 164 165 /* 165 /* 166 * We use the full symtab and strtab which lay 166 * We use the full symtab and strtab which layout_symtab arranged to 167 * be appended to the init section. Later we 167 * be appended to the init section. Later we switch to the cut-down 168 * core-only ones. 168 * core-only ones. 169 */ 169 */ 170 void add_kallsyms(struct module *mod, const st 170 void add_kallsyms(struct module *mod, const struct load_info *info) 171 { 171 { 172 unsigned int i, ndst; 172 unsigned int i, ndst; 173 const Elf_Sym *src; 173 const Elf_Sym *src; 174 Elf_Sym *dst; 174 Elf_Sym *dst; 175 char *s; 175 char *s; 176 Elf_Shdr *symsec = &info->sechdrs[info 176 Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; 177 unsigned long strtab_size; 177 unsigned long strtab_size; 178 void *data_base = mod->mem[MOD_DATA].b 178 void *data_base = mod->mem[MOD_DATA].base; 179 void *init_data_base = mod->mem[MOD_IN 179 void *init_data_base = mod->mem[MOD_INIT_DATA].base; 180 180 181 /* Set up to point into init section. 181 /* Set up to point into init section. */ 182 mod->kallsyms = (void __rcu *)init_dat 182 mod->kallsyms = (void __rcu *)init_data_base + 183 info->mod_kallsyms_init_off; 183 info->mod_kallsyms_init_off; 184 184 185 rcu_read_lock(); 185 rcu_read_lock(); 186 /* The following is safe since this po 186 /* The following is safe since this pointer cannot change */ 187 rcu_dereference(mod->kallsyms)->symtab 187 rcu_dereference(mod->kallsyms)->symtab = (void *)symsec->sh_addr; 188 rcu_dereference(mod->kallsyms)->num_sy 188 rcu_dereference(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym); 189 /* Make sure we get permanent strtab: 189 /* Make sure we get permanent strtab: don't use info->strtab. */ 190 rcu_dereference(mod->kallsyms)->strtab 190 rcu_dereference(mod->kallsyms)->strtab = 191 (void *)info->sechdrs[info->in 191 (void *)info->sechdrs[info->index.str].sh_addr; 192 rcu_dereference(mod->kallsyms)->typeta 192 rcu_dereference(mod->kallsyms)->typetab = init_data_base + info->init_typeoffs; 193 193 194 /* 194 /* 195 * Now populate the cut down core kall 195 * Now populate the cut down core kallsyms for after init 196 * and set types up while we still hav 196 * and set types up while we still have access to sections. 197 */ 197 */ 198 mod->core_kallsyms.symtab = dst = data 198 mod->core_kallsyms.symtab = dst = data_base + info->symoffs; 199 mod->core_kallsyms.strtab = s = data_b 199 mod->core_kallsyms.strtab = s = data_base + info->stroffs; 200 mod->core_kallsyms.typetab = data_base 200 mod->core_kallsyms.typetab = data_base + info->core_typeoffs; 201 strtab_size = info->core_typeoffs - in 201 strtab_size = info->core_typeoffs - info->stroffs; 202 src = rcu_dereference(mod->kallsyms)-> 202 src = rcu_dereference(mod->kallsyms)->symtab; 203 for (ndst = i = 0; i < rcu_dereference 203 for (ndst = i = 0; i < rcu_dereference(mod->kallsyms)->num_symtab; i++) { 204 rcu_dereference(mod->kallsyms) 204 rcu_dereference(mod->kallsyms)->typetab[i] = elf_type(src + i, info); 205 if (i == 0 || is_livepatch_mod 205 if (i == 0 || is_livepatch_module(mod) || 206 is_core_symbol(src + i, in 206 is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum, 207 info->index 207 info->index.pcpu)) { 208 ssize_t ret; 208 ssize_t ret; 209 209 210 mod->core_kallsyms.typ 210 mod->core_kallsyms.typetab[ndst] = 211 rcu_dereference(mo 211 rcu_dereference(mod->kallsyms)->typetab[i]; 212 dst[ndst] = src[i]; 212 dst[ndst] = src[i]; 213 dst[ndst++].st_name = 213 dst[ndst++].st_name = s - mod->core_kallsyms.strtab; 214 ret = strscpy(s, 214 ret = strscpy(s, 215 &rcu_der 215 &rcu_dereference(mod->kallsyms)->strtab[src[i].st_name], 216 strtab_s 216 strtab_size); 217 if (ret < 0) 217 if (ret < 0) 218 break; 218 break; 219 s += ret + 1; 219 s += ret + 1; 220 strtab_size -= ret + 1 220 strtab_size -= ret + 1; 221 } 221 } 222 } 222 } 223 rcu_read_unlock(); 223 rcu_read_unlock(); 224 mod->core_kallsyms.num_symtab = ndst; 224 mod->core_kallsyms.num_symtab = ndst; 225 } 225 } 226 226 227 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) 227 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) 228 void init_build_id(struct module *mod, const s 228 void init_build_id(struct module *mod, const struct load_info *info) 229 { 229 { 230 const Elf_Shdr *sechdr; 230 const Elf_Shdr *sechdr; 231 unsigned int i; 231 unsigned int i; 232 232 233 for (i = 0; i < info->hdr->e_shnum; i+ 233 for (i = 0; i < info->hdr->e_shnum; i++) { 234 sechdr = &info->sechdrs[i]; 234 sechdr = &info->sechdrs[i]; 235 if (!sect_empty(sechdr) && sec 235 if (!sect_empty(sechdr) && sechdr->sh_type == SHT_NOTE && 236 !build_id_parse_buf((void 236 !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id, 237 sechdr 237 sechdr->sh_size)) 238 break; 238 break; 239 } 239 } 240 } 240 } 241 #else 241 #else 242 void init_build_id(struct module *mod, const s 242 void init_build_id(struct module *mod, const struct load_info *info) 243 { 243 { 244 } 244 } 245 #endif 245 #endif 246 246 247 static const char *kallsyms_symbol_name(struct 247 static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum) 248 { 248 { 249 return kallsyms->strtab + kallsyms->sy 249 return kallsyms->strtab + kallsyms->symtab[symnum].st_name; 250 } 250 } 251 251 252 /* 252 /* 253 * Given a module and address, find the corres 253 * Given a module and address, find the corresponding symbol and return its name 254 * while providing its size and offset if need 254 * while providing its size and offset if needed. 255 */ 255 */ 256 static const char *find_kallsyms_symbol(struct 256 static const char *find_kallsyms_symbol(struct module *mod, 257 unsign 257 unsigned long addr, 258 unsign 258 unsigned long *size, 259 unsign 259 unsigned long *offset) 260 { 260 { 261 unsigned int i, best = 0; 261 unsigned int i, best = 0; 262 unsigned long nextval, bestval; 262 unsigned long nextval, bestval; 263 struct mod_kallsyms *kallsyms = rcu_de 263 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms); 264 struct module_memory *mod_mem; 264 struct module_memory *mod_mem; 265 265 266 /* At worse, next value is at end of m 266 /* At worse, next value is at end of module */ 267 if (within_module_init(addr, mod)) 267 if (within_module_init(addr, mod)) 268 mod_mem = &mod->mem[MOD_INIT_T 268 mod_mem = &mod->mem[MOD_INIT_TEXT]; 269 else 269 else 270 mod_mem = &mod->mem[MOD_TEXT]; 270 mod_mem = &mod->mem[MOD_TEXT]; 271 271 272 nextval = (unsigned long)mod_mem->base 272 nextval = (unsigned long)mod_mem->base + mod_mem->size; 273 273 274 bestval = kallsyms_symbol_value(&kalls 274 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]); 275 275 276 /* 276 /* 277 * Scan for closest preceding symbol, 277 * Scan for closest preceding symbol, and next symbol. (ELF 278 * starts real symbols at 1). 278 * starts real symbols at 1). 279 */ 279 */ 280 for (i = 1; i < kallsyms->num_symtab; 280 for (i = 1; i < kallsyms->num_symtab; i++) { 281 const Elf_Sym *sym = &kallsyms 281 const Elf_Sym *sym = &kallsyms->symtab[i]; 282 unsigned long thisval = kallsy 282 unsigned long thisval = kallsyms_symbol_value(sym); 283 283 284 if (sym->st_shndx == SHN_UNDEF 284 if (sym->st_shndx == SHN_UNDEF) 285 continue; 285 continue; 286 286 287 /* 287 /* 288 * We ignore unnamed symbols: 288 * We ignore unnamed symbols: they're uninformative 289 * and inserted at a whim. 289 * and inserted at a whim. 290 */ 290 */ 291 if (*kallsyms_symbol_name(kall 291 if (*kallsyms_symbol_name(kallsyms, i) == '\0' || 292 is_mapping_symbol(kallsyms 292 is_mapping_symbol(kallsyms_symbol_name(kallsyms, i))) 293 continue; 293 continue; 294 294 295 if (thisval <= addr && thisval 295 if (thisval <= addr && thisval > bestval) { 296 best = i; 296 best = i; 297 bestval = thisval; 297 bestval = thisval; 298 } 298 } 299 if (thisval > addr && thisval 299 if (thisval > addr && thisval < nextval) 300 nextval = thisval; 300 nextval = thisval; 301 } 301 } 302 302 303 if (!best) 303 if (!best) 304 return NULL; 304 return NULL; 305 305 306 if (size) 306 if (size) 307 *size = nextval - bestval; 307 *size = nextval - bestval; 308 if (offset) 308 if (offset) 309 *offset = addr - bestval; 309 *offset = addr - bestval; 310 310 311 return kallsyms_symbol_name(kallsyms, 311 return kallsyms_symbol_name(kallsyms, best); 312 } 312 } 313 313 314 void * __weak dereference_module_function_desc 314 void * __weak dereference_module_function_descriptor(struct module *mod, 315 315 void *ptr) 316 { 316 { 317 return ptr; 317 return ptr; 318 } 318 } 319 319 320 /* 320 /* 321 * For kallsyms to ask for address resolution. 321 * For kallsyms to ask for address resolution. NULL means not found. Careful 322 * not to lock to avoid deadlock on oopses, si 322 * not to lock to avoid deadlock on oopses, simply disable preemption. 323 */ 323 */ 324 int module_address_lookup(unsigned long addr, 324 int module_address_lookup(unsigned long addr, 325 unsigned long *size, 325 unsigned long *size, 326 unsigned long *offse 326 unsigned long *offset, 327 char **modname, 327 char **modname, 328 const unsigned char 328 const unsigned char **modbuildid, 329 char *namebuf) 329 char *namebuf) 330 { 330 { 331 const char *sym; 331 const char *sym; 332 int ret = 0; 332 int ret = 0; 333 struct module *mod; 333 struct module *mod; 334 334 335 preempt_disable(); 335 preempt_disable(); 336 mod = __module_address(addr); 336 mod = __module_address(addr); 337 if (mod) { 337 if (mod) { 338 if (modname) 338 if (modname) 339 *modname = mod->name; 339 *modname = mod->name; 340 if (modbuildid) { 340 if (modbuildid) { 341 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) 341 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) 342 *modbuildid = mod->bui 342 *modbuildid = mod->build_id; 343 #else 343 #else 344 *modbuildid = NULL; 344 *modbuildid = NULL; 345 #endif 345 #endif 346 } 346 } 347 347 348 sym = find_kallsyms_symbol(mod 348 sym = find_kallsyms_symbol(mod, addr, size, offset); 349 349 350 if (sym) 350 if (sym) 351 ret = strscpy(namebuf, 351 ret = strscpy(namebuf, sym, KSYM_NAME_LEN); 352 } 352 } 353 preempt_enable(); 353 preempt_enable(); 354 354 355 return ret; 355 return ret; 356 } 356 } 357 357 358 int lookup_module_symbol_name(unsigned long ad 358 int lookup_module_symbol_name(unsigned long addr, char *symname) 359 { 359 { 360 struct module *mod; 360 struct module *mod; 361 361 362 preempt_disable(); 362 preempt_disable(); 363 list_for_each_entry_rcu(mod, &modules, 363 list_for_each_entry_rcu(mod, &modules, list) { 364 if (mod->state == MODULE_STATE 364 if (mod->state == MODULE_STATE_UNFORMED) 365 continue; 365 continue; 366 if (within_module(addr, mod)) 366 if (within_module(addr, mod)) { 367 const char *sym; 367 const char *sym; 368 368 369 sym = find_kallsyms_sy 369 sym = find_kallsyms_symbol(mod, addr, NULL, NULL); 370 if (!sym) 370 if (!sym) 371 goto out; 371 goto out; 372 372 373 strscpy(symname, sym, 373 strscpy(symname, sym, KSYM_NAME_LEN); 374 preempt_enable(); 374 preempt_enable(); 375 return 0; 375 return 0; 376 } 376 } 377 } 377 } 378 out: 378 out: 379 preempt_enable(); 379 preempt_enable(); 380 return -ERANGE; 380 return -ERANGE; 381 } 381 } 382 382 383 int module_get_kallsym(unsigned int symnum, un 383 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 384 char *name, char *modul 384 char *name, char *module_name, int *exported) 385 { 385 { 386 struct module *mod; 386 struct module *mod; 387 387 388 preempt_disable(); 388 preempt_disable(); 389 list_for_each_entry_rcu(mod, &modules, 389 list_for_each_entry_rcu(mod, &modules, list) { 390 struct mod_kallsyms *kallsyms; 390 struct mod_kallsyms *kallsyms; 391 391 392 if (mod->state == MODULE_STATE 392 if (mod->state == MODULE_STATE_UNFORMED) 393 continue; 393 continue; 394 kallsyms = rcu_dereference_sch 394 kallsyms = rcu_dereference_sched(mod->kallsyms); 395 if (symnum < kallsyms->num_sym 395 if (symnum < kallsyms->num_symtab) { 396 const Elf_Sym *sym = & 396 const Elf_Sym *sym = &kallsyms->symtab[symnum]; 397 397 398 *value = kallsyms_symb 398 *value = kallsyms_symbol_value(sym); 399 *type = kallsyms->type 399 *type = kallsyms->typetab[symnum]; 400 strscpy(name, kallsyms 400 strscpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN); 401 strscpy(module_name, m 401 strscpy(module_name, mod->name, MODULE_NAME_LEN); 402 *exported = is_exporte 402 *exported = is_exported(name, *value, mod); 403 preempt_enable(); 403 preempt_enable(); 404 return 0; 404 return 0; 405 } 405 } 406 symnum -= kallsyms->num_symtab 406 symnum -= kallsyms->num_symtab; 407 } 407 } 408 preempt_enable(); 408 preempt_enable(); 409 return -ERANGE; 409 return -ERANGE; 410 } 410 } 411 411 412 /* Given a module and name of symbol, find and 412 /* Given a module and name of symbol, find and return the symbol's value */ 413 static unsigned long __find_kallsyms_symbol_va 413 static unsigned long __find_kallsyms_symbol_value(struct module *mod, const char *name) 414 { 414 { 415 unsigned int i; 415 unsigned int i; 416 struct mod_kallsyms *kallsyms = rcu_de 416 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms); 417 417 418 for (i = 0; i < kallsyms->num_symtab; 418 for (i = 0; i < kallsyms->num_symtab; i++) { 419 const Elf_Sym *sym = &kallsyms 419 const Elf_Sym *sym = &kallsyms->symtab[i]; 420 420 421 if (strcmp(name, kallsyms_symb 421 if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) == 0 && 422 sym->st_shndx != SHN_UNDEF 422 sym->st_shndx != SHN_UNDEF) 423 return kallsyms_symbol 423 return kallsyms_symbol_value(sym); 424 } 424 } 425 return 0; 425 return 0; 426 } 426 } 427 427 428 static unsigned long __module_kallsyms_lookup_ 428 static unsigned long __module_kallsyms_lookup_name(const char *name) 429 { 429 { 430 struct module *mod; 430 struct module *mod; 431 char *colon; 431 char *colon; 432 432 433 colon = strnchr(name, MODULE_NAME_LEN, 433 colon = strnchr(name, MODULE_NAME_LEN, ':'); 434 if (colon) { 434 if (colon) { 435 mod = find_module_all(name, co 435 mod = find_module_all(name, colon - name, false); 436 if (mod) 436 if (mod) 437 return __find_kallsyms 437 return __find_kallsyms_symbol_value(mod, colon + 1); 438 return 0; 438 return 0; 439 } 439 } 440 440 441 list_for_each_entry_rcu(mod, &modules, 441 list_for_each_entry_rcu(mod, &modules, list) { 442 unsigned long ret; 442 unsigned long ret; 443 443 444 if (mod->state == MODULE_STATE 444 if (mod->state == MODULE_STATE_UNFORMED) 445 continue; 445 continue; 446 ret = __find_kallsyms_symbol_v 446 ret = __find_kallsyms_symbol_value(mod, name); 447 if (ret) 447 if (ret) 448 return ret; 448 return ret; 449 } 449 } 450 return 0; 450 return 0; 451 } 451 } 452 452 453 /* Look for this name: can be of form module:n 453 /* Look for this name: can be of form module:name. */ 454 unsigned long module_kallsyms_lookup_name(cons 454 unsigned long module_kallsyms_lookup_name(const char *name) 455 { 455 { 456 unsigned long ret; 456 unsigned long ret; 457 457 458 /* Don't lock: we're in enough trouble 458 /* Don't lock: we're in enough trouble already. */ 459 preempt_disable(); 459 preempt_disable(); 460 ret = __module_kallsyms_lookup_name(na 460 ret = __module_kallsyms_lookup_name(name); 461 preempt_enable(); 461 preempt_enable(); 462 return ret; 462 return ret; 463 } 463 } 464 464 465 unsigned long find_kallsyms_symbol_value(struc 465 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name) 466 { 466 { 467 unsigned long ret; 467 unsigned long ret; 468 468 469 preempt_disable(); 469 preempt_disable(); 470 ret = __find_kallsyms_symbol_value(mod 470 ret = __find_kallsyms_symbol_value(mod, name); 471 preempt_enable(); 471 preempt_enable(); 472 return ret; 472 return ret; 473 } 473 } 474 474 475 int module_kallsyms_on_each_symbol(const char 475 int module_kallsyms_on_each_symbol(const char *modname, 476 int (*fn)(v 476 int (*fn)(void *, const char *, unsigned long), 477 void *data) 477 void *data) 478 { 478 { 479 struct module *mod; 479 struct module *mod; 480 unsigned int i; 480 unsigned int i; 481 int ret = 0; 481 int ret = 0; 482 482 483 mutex_lock(&module_mutex); 483 mutex_lock(&module_mutex); 484 list_for_each_entry(mod, &modules, lis 484 list_for_each_entry(mod, &modules, list) { 485 struct mod_kallsyms *kallsyms; 485 struct mod_kallsyms *kallsyms; 486 486 487 if (mod->state == MODULE_STATE 487 if (mod->state == MODULE_STATE_UNFORMED) 488 continue; 488 continue; 489 489 490 if (modname && strcmp(modname, 490 if (modname && strcmp(modname, mod->name)) 491 continue; 491 continue; 492 492 493 /* Use rcu_dereference_sched() 493 /* Use rcu_dereference_sched() to remain compliant with the sparse tool */ 494 preempt_disable(); 494 preempt_disable(); 495 kallsyms = rcu_dereference_sch 495 kallsyms = rcu_dereference_sched(mod->kallsyms); 496 preempt_enable(); 496 preempt_enable(); 497 497 498 for (i = 0; i < kallsyms->num_ 498 for (i = 0; i < kallsyms->num_symtab; i++) { 499 const Elf_Sym *sym = & 499 const Elf_Sym *sym = &kallsyms->symtab[i]; 500 500 501 if (sym->st_shndx == S 501 if (sym->st_shndx == SHN_UNDEF) 502 continue; 502 continue; 503 503 504 ret = fn(data, kallsym 504 ret = fn(data, kallsyms_symbol_name(kallsyms, i), 505 kallsyms_symb 505 kallsyms_symbol_value(sym)); 506 if (ret != 0) 506 if (ret != 0) 507 goto out; 507 goto out; 508 } 508 } 509 509 510 /* 510 /* 511 * The given module is found, 511 * The given module is found, the subsequent modules do not 512 * need to be compared. 512 * need to be compared. 513 */ 513 */ 514 if (modname) 514 if (modname) 515 break; 515 break; 516 } 516 } 517 out: 517 out: 518 mutex_unlock(&module_mutex); 518 mutex_unlock(&module_mutex); 519 return ret; 519 return ret; 520 } 520 } 521 521
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.