kallsyms.c 14 KB

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