kallsyms.c 14 KB

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