PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/mod/modpost.c

http://github.com/mirrors/linux
C | 2681 lines | 2035 code | 313 blank | 333 comment | 449 complexity | 3d309a0628041c8314a98ef5ab3849a3 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006-2008 Sam Ravnborg
  6. * Based in part on module-init-tools/depmod.c,file2alias
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. * Usage: modpost vmlinux module1.o module2.o ...
  12. */
  13. #define _GNU_SOURCE
  14. #include <elf.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include <limits.h>
  19. #include <stdbool.h>
  20. #include <errno.h>
  21. #include "modpost.h"
  22. #include "../../include/linux/license.h"
  23. /* Are we using CONFIG_MODVERSIONS? */
  24. static int modversions = 0;
  25. /* Warn about undefined symbols? (do so if we have vmlinux) */
  26. static int have_vmlinux = 0;
  27. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  28. static int all_versions = 0;
  29. /* If we are modposting external module set to 1 */
  30. static int external_module = 0;
  31. /* Warn about section mismatch in vmlinux if set to 1 */
  32. static int vmlinux_section_warnings = 1;
  33. /* Only warn about unresolved symbols */
  34. static int warn_unresolved = 0;
  35. /* How a symbol is exported */
  36. static int sec_mismatch_count = 0;
  37. static int sec_mismatch_fatal = 0;
  38. /* ignore missing files */
  39. static int ignore_missing_files;
  40. /* If set to 1, only warn (instead of error) about missing ns imports */
  41. static int allow_missing_ns_imports;
  42. enum export {
  43. export_plain, export_unused, export_gpl,
  44. export_unused_gpl, export_gpl_future, export_unknown
  45. };
  46. /* In kernel, this size is defined in linux/module.h;
  47. * here we use Elf_Addr instead of long for covering cross-compile
  48. */
  49. #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
  50. void __attribute__((format(printf, 2, 3)))
  51. modpost_log(enum loglevel loglevel, const char *fmt, ...)
  52. {
  53. va_list arglist;
  54. switch (loglevel) {
  55. case LOG_WARN:
  56. fprintf(stderr, "WARNING: ");
  57. break;
  58. case LOG_ERROR:
  59. fprintf(stderr, "ERROR: ");
  60. break;
  61. case LOG_FATAL:
  62. fprintf(stderr, "FATAL: ");
  63. break;
  64. default: /* invalid loglevel, ignore */
  65. break;
  66. }
  67. fprintf(stderr, "modpost: ");
  68. va_start(arglist, fmt);
  69. vfprintf(stderr, fmt, arglist);
  70. va_end(arglist);
  71. if (loglevel == LOG_FATAL)
  72. exit(1);
  73. }
  74. static inline bool strends(const char *str, const char *postfix)
  75. {
  76. if (strlen(str) < strlen(postfix))
  77. return false;
  78. return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
  79. }
  80. static int is_vmlinux(const char *modname)
  81. {
  82. const char *myname;
  83. myname = strrchr(modname, '/');
  84. if (myname)
  85. myname++;
  86. else
  87. myname = modname;
  88. return (strcmp(myname, "vmlinux") == 0) ||
  89. (strcmp(myname, "vmlinux.o") == 0);
  90. }
  91. void *do_nofail(void *ptr, const char *expr)
  92. {
  93. if (!ptr)
  94. fatal("Memory allocation failure: %s.\n", expr);
  95. return ptr;
  96. }
  97. /* A list of all modules we processed */
  98. static struct module *modules;
  99. static struct module *find_module(const char *modname)
  100. {
  101. struct module *mod;
  102. for (mod = modules; mod; mod = mod->next)
  103. if (strcmp(mod->name, modname) == 0)
  104. break;
  105. return mod;
  106. }
  107. static struct module *new_module(const char *modname)
  108. {
  109. struct module *mod;
  110. char *p;
  111. mod = NOFAIL(malloc(sizeof(*mod)));
  112. memset(mod, 0, sizeof(*mod));
  113. p = NOFAIL(strdup(modname));
  114. /* strip trailing .o */
  115. if (strends(p, ".o")) {
  116. p[strlen(p) - 2] = '\0';
  117. mod->is_dot_o = 1;
  118. }
  119. /* add to list */
  120. mod->name = p;
  121. mod->gpl_compatible = -1;
  122. mod->next = modules;
  123. modules = mod;
  124. return mod;
  125. }
  126. /* A hash of all exported symbols,
  127. * struct symbol is also used for lists of unresolved symbols */
  128. #define SYMBOL_HASH_SIZE 1024
  129. struct symbol {
  130. struct symbol *next;
  131. struct module *module;
  132. unsigned int crc;
  133. int crc_valid;
  134. char *namespace;
  135. unsigned int weak:1;
  136. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  137. unsigned int kernel:1; /* 1 if symbol is from kernel
  138. * (only for external modules) **/
  139. unsigned int is_static:1; /* 1 if symbol is not global */
  140. enum export export; /* Type of export */
  141. char name[0];
  142. };
  143. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  144. /* This is based on the hash agorithm from gdbm, via tdb */
  145. static inline unsigned int tdb_hash(const char *name)
  146. {
  147. unsigned value; /* Used to compute the hash value. */
  148. unsigned i; /* Used to cycle through random values. */
  149. /* Set the initial value from the key size. */
  150. for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
  151. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  152. return (1103515243 * value + 12345);
  153. }
  154. /**
  155. * Allocate a new symbols for use in the hash of exported symbols or
  156. * the list of unresolved symbols per module
  157. **/
  158. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  159. struct symbol *next)
  160. {
  161. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  162. memset(s, 0, sizeof(*s));
  163. strcpy(s->name, name);
  164. s->weak = weak;
  165. s->next = next;
  166. s->is_static = 1;
  167. return s;
  168. }
  169. /* For the hash of exported symbols */
  170. static struct symbol *new_symbol(const char *name, struct module *module,
  171. enum export export)
  172. {
  173. unsigned int hash;
  174. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  175. symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  176. return symbolhash[hash];
  177. }
  178. static struct symbol *find_symbol(const char *name)
  179. {
  180. struct symbol *s;
  181. /* For our purposes, .foo matches foo. PPC64 needs this. */
  182. if (name[0] == '.')
  183. name++;
  184. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
  185. if (strcmp(s->name, name) == 0)
  186. return s;
  187. }
  188. return NULL;
  189. }
  190. static bool contains_namespace(struct namespace_list *list,
  191. const char *namespace)
  192. {
  193. for (; list; list = list->next)
  194. if (!strcmp(list->namespace, namespace))
  195. return true;
  196. return false;
  197. }
  198. static void add_namespace(struct namespace_list **list, const char *namespace)
  199. {
  200. struct namespace_list *ns_entry;
  201. if (!contains_namespace(*list, namespace)) {
  202. ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
  203. strlen(namespace) + 1));
  204. strcpy(ns_entry->namespace, namespace);
  205. ns_entry->next = *list;
  206. *list = ns_entry;
  207. }
  208. }
  209. static bool module_imports_namespace(struct module *module,
  210. const char *namespace)
  211. {
  212. return contains_namespace(module->imported_namespaces, namespace);
  213. }
  214. static const struct {
  215. const char *str;
  216. enum export export;
  217. } export_list[] = {
  218. { .str = "EXPORT_SYMBOL", .export = export_plain },
  219. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  220. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  221. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  222. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  223. { .str = "(unknown)", .export = export_unknown },
  224. };
  225. static const char *export_str(enum export ex)
  226. {
  227. return export_list[ex].str;
  228. }
  229. static enum export export_no(const char *s)
  230. {
  231. int i;
  232. if (!s)
  233. return export_unknown;
  234. for (i = 0; export_list[i].export != export_unknown; i++) {
  235. if (strcmp(export_list[i].str, s) == 0)
  236. return export_list[i].export;
  237. }
  238. return export_unknown;
  239. }
  240. static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
  241. {
  242. return (void *)elf->hdr +
  243. elf->sechdrs[elf->secindex_strings].sh_offset +
  244. sechdr->sh_name;
  245. }
  246. static const char *sec_name(struct elf_info *elf, int secindex)
  247. {
  248. return sech_name(elf, &elf->sechdrs[secindex]);
  249. }
  250. static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
  251. {
  252. unsigned int secindex = get_secindex(info, sym);
  253. Elf_Shdr *sechdr = &info->sechdrs[secindex];
  254. unsigned long offset;
  255. offset = sym->st_value;
  256. if (info->hdr->e_type != ET_REL)
  257. offset -= sechdr->sh_addr;
  258. return (void *)info->hdr + sechdr->sh_offset + offset;
  259. }
  260. #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
  261. static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
  262. {
  263. const char *secname = sec_name(elf, sec);
  264. if (strstarts(secname, "___ksymtab+"))
  265. return export_plain;
  266. else if (strstarts(secname, "___ksymtab_unused+"))
  267. return export_unused;
  268. else if (strstarts(secname, "___ksymtab_gpl+"))
  269. return export_gpl;
  270. else if (strstarts(secname, "___ksymtab_unused_gpl+"))
  271. return export_unused_gpl;
  272. else if (strstarts(secname, "___ksymtab_gpl_future+"))
  273. return export_gpl_future;
  274. else
  275. return export_unknown;
  276. }
  277. static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
  278. {
  279. if (sec == elf->export_sec)
  280. return export_plain;
  281. else if (sec == elf->export_unused_sec)
  282. return export_unused;
  283. else if (sec == elf->export_gpl_sec)
  284. return export_gpl;
  285. else if (sec == elf->export_unused_gpl_sec)
  286. return export_unused_gpl;
  287. else if (sec == elf->export_gpl_future_sec)
  288. return export_gpl_future;
  289. else
  290. return export_unknown;
  291. }
  292. static const char *namespace_from_kstrtabns(const struct elf_info *info,
  293. const Elf_Sym *sym)
  294. {
  295. const char *value = sym_get_data(info, sym);
  296. return value[0] ? value : NULL;
  297. }
  298. static void sym_update_namespace(const char *symname, const char *namespace)
  299. {
  300. struct symbol *s = find_symbol(symname);
  301. /*
  302. * That symbol should have been created earlier and thus this is
  303. * actually an assertion.
  304. */
  305. if (!s) {
  306. merror("Could not update namespace(%s) for symbol %s\n",
  307. namespace, symname);
  308. return;
  309. }
  310. free(s->namespace);
  311. s->namespace =
  312. namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
  313. }
  314. /**
  315. * Add an exported symbol - it may have already been added without a
  316. * CRC, in this case just update the CRC
  317. **/
  318. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  319. enum export export)
  320. {
  321. struct symbol *s = find_symbol(name);
  322. if (!s) {
  323. s = new_symbol(name, mod, export);
  324. } else if (!external_module || is_vmlinux(s->module->name) ||
  325. s->module == mod) {
  326. warn("%s: '%s' exported twice. Previous export was in %s%s\n",
  327. mod->name, name, s->module->name,
  328. is_vmlinux(s->module->name) ? "" : ".ko");
  329. return s;
  330. }
  331. s->module = mod;
  332. s->vmlinux = is_vmlinux(mod->name);
  333. s->kernel = 0;
  334. s->export = export;
  335. return s;
  336. }
  337. static void sym_set_crc(const char *name, unsigned int crc)
  338. {
  339. struct symbol *s = find_symbol(name);
  340. /*
  341. * Ignore stand-alone __crc_*, which might be auto-generated symbols
  342. * such as __*_veneer in ARM ELF.
  343. */
  344. if (!s)
  345. return;
  346. s->crc = crc;
  347. s->crc_valid = 1;
  348. }
  349. void *grab_file(const char *filename, unsigned long *size)
  350. {
  351. struct stat st;
  352. void *map = MAP_FAILED;
  353. int fd;
  354. fd = open(filename, O_RDONLY);
  355. if (fd < 0)
  356. return NULL;
  357. if (fstat(fd, &st))
  358. goto failed;
  359. *size = st.st_size;
  360. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  361. failed:
  362. close(fd);
  363. if (map == MAP_FAILED)
  364. return NULL;
  365. return map;
  366. }
  367. /**
  368. * Return a copy of the next line in a mmap'ed file.
  369. * spaces in the beginning of the line is trimmed away.
  370. * Return a pointer to a static buffer.
  371. **/
  372. char *get_next_line(unsigned long *pos, void *file, unsigned long size)
  373. {
  374. static char line[4096];
  375. int skip = 1;
  376. size_t len = 0;
  377. signed char *p = (signed char *)file + *pos;
  378. char *s = line;
  379. for (; *pos < size ; (*pos)++) {
  380. if (skip && isspace(*p)) {
  381. p++;
  382. continue;
  383. }
  384. skip = 0;
  385. if (*p != '\n' && (*pos < size)) {
  386. len++;
  387. *s++ = *p++;
  388. if (len > 4095)
  389. break; /* Too long, stop */
  390. } else {
  391. /* End of string */
  392. *s = '\0';
  393. return line;
  394. }
  395. }
  396. /* End of buffer */
  397. return NULL;
  398. }
  399. void release_file(void *file, unsigned long size)
  400. {
  401. munmap(file, size);
  402. }
  403. static int parse_elf(struct elf_info *info, const char *filename)
  404. {
  405. unsigned int i;
  406. Elf_Ehdr *hdr;
  407. Elf_Shdr *sechdrs;
  408. Elf_Sym *sym;
  409. const char *secstrings;
  410. unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
  411. hdr = grab_file(filename, &info->size);
  412. if (!hdr) {
  413. if (ignore_missing_files) {
  414. fprintf(stderr, "%s: %s (ignored)\n", filename,
  415. strerror(errno));
  416. return 0;
  417. }
  418. perror(filename);
  419. exit(1);
  420. }
  421. info->hdr = hdr;
  422. if (info->size < sizeof(*hdr)) {
  423. /* file too small, assume this is an empty .o file */
  424. return 0;
  425. }
  426. /* Is this a valid ELF file? */
  427. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  428. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  429. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  430. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  431. /* Not an ELF file - silently ignore it */
  432. return 0;
  433. }
  434. /* Fix endianness in ELF header */
  435. hdr->e_type = TO_NATIVE(hdr->e_type);
  436. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  437. hdr->e_version = TO_NATIVE(hdr->e_version);
  438. hdr->e_entry = TO_NATIVE(hdr->e_entry);
  439. hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
  440. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  441. hdr->e_flags = TO_NATIVE(hdr->e_flags);
  442. hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
  443. hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
  444. hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
  445. hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
  446. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  447. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  448. sechdrs = (void *)hdr + hdr->e_shoff;
  449. info->sechdrs = sechdrs;
  450. /* Check if file offset is correct */
  451. if (hdr->e_shoff > info->size) {
  452. fatal("section header offset=%lu in file '%s' is bigger than "
  453. "filesize=%lu\n", (unsigned long)hdr->e_shoff,
  454. filename, info->size);
  455. return 0;
  456. }
  457. if (hdr->e_shnum == SHN_UNDEF) {
  458. /*
  459. * There are more than 64k sections,
  460. * read count from .sh_size.
  461. */
  462. info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
  463. }
  464. else {
  465. info->num_sections = hdr->e_shnum;
  466. }
  467. if (hdr->e_shstrndx == SHN_XINDEX) {
  468. info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
  469. }
  470. else {
  471. info->secindex_strings = hdr->e_shstrndx;
  472. }
  473. /* Fix endianness in section headers */
  474. for (i = 0; i < info->num_sections; i++) {
  475. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  476. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  477. sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
  478. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  479. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  480. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  481. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  482. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  483. sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
  484. sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
  485. }
  486. /* Find symbol table. */
  487. secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
  488. for (i = 1; i < info->num_sections; i++) {
  489. const char *secname;
  490. int nobits = sechdrs[i].sh_type == SHT_NOBITS;
  491. if (!nobits && sechdrs[i].sh_offset > info->size) {
  492. fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
  493. "sizeof(*hrd)=%zu\n", filename,
  494. (unsigned long)sechdrs[i].sh_offset,
  495. sizeof(*hdr));
  496. return 0;
  497. }
  498. secname = secstrings + sechdrs[i].sh_name;
  499. if (strcmp(secname, ".modinfo") == 0) {
  500. if (nobits)
  501. fatal("%s has NOBITS .modinfo\n", filename);
  502. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  503. info->modinfo_len = sechdrs[i].sh_size;
  504. } else if (strcmp(secname, "__ksymtab") == 0)
  505. info->export_sec = i;
  506. else if (strcmp(secname, "__ksymtab_unused") == 0)
  507. info->export_unused_sec = i;
  508. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  509. info->export_gpl_sec = i;
  510. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  511. info->export_unused_gpl_sec = i;
  512. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  513. info->export_gpl_future_sec = i;
  514. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  515. unsigned int sh_link_idx;
  516. symtab_idx = i;
  517. info->symtab_start = (void *)hdr +
  518. sechdrs[i].sh_offset;
  519. info->symtab_stop = (void *)hdr +
  520. sechdrs[i].sh_offset + sechdrs[i].sh_size;
  521. sh_link_idx = sechdrs[i].sh_link;
  522. info->strtab = (void *)hdr +
  523. sechdrs[sh_link_idx].sh_offset;
  524. }
  525. /* 32bit section no. table? ("more than 64k sections") */
  526. if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
  527. symtab_shndx_idx = i;
  528. info->symtab_shndx_start = (void *)hdr +
  529. sechdrs[i].sh_offset;
  530. info->symtab_shndx_stop = (void *)hdr +
  531. sechdrs[i].sh_offset + sechdrs[i].sh_size;
  532. }
  533. }
  534. if (!info->symtab_start)
  535. fatal("%s has no symtab?\n", filename);
  536. /* Fix endianness in symbols */
  537. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  538. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  539. sym->st_name = TO_NATIVE(sym->st_name);
  540. sym->st_value = TO_NATIVE(sym->st_value);
  541. sym->st_size = TO_NATIVE(sym->st_size);
  542. }
  543. if (symtab_shndx_idx != ~0U) {
  544. Elf32_Word *p;
  545. if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
  546. fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
  547. filename, sechdrs[symtab_shndx_idx].sh_link,
  548. symtab_idx);
  549. /* Fix endianness */
  550. for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
  551. p++)
  552. *p = TO_NATIVE(*p);
  553. }
  554. return 1;
  555. }
  556. static void parse_elf_finish(struct elf_info *info)
  557. {
  558. release_file(info->hdr, info->size);
  559. }
  560. static int ignore_undef_symbol(struct elf_info *info, const char *symname)
  561. {
  562. /* ignore __this_module, it will be resolved shortly */
  563. if (strcmp(symname, "__this_module") == 0)
  564. return 1;
  565. /* ignore global offset table */
  566. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  567. return 1;
  568. if (info->hdr->e_machine == EM_PPC)
  569. /* Special register function linked on all modules during final link of .ko */
  570. if (strstarts(symname, "_restgpr_") ||
  571. strstarts(symname, "_savegpr_") ||
  572. strstarts(symname, "_rest32gpr_") ||
  573. strstarts(symname, "_save32gpr_") ||
  574. strstarts(symname, "_restvr_") ||
  575. strstarts(symname, "_savevr_"))
  576. return 1;
  577. if (info->hdr->e_machine == EM_PPC64)
  578. /* Special register function linked on all modules during final link of .ko */
  579. if (strstarts(symname, "_restgpr0_") ||
  580. strstarts(symname, "_savegpr0_") ||
  581. strstarts(symname, "_restvr_") ||
  582. strstarts(symname, "_savevr_") ||
  583. strcmp(symname, ".TOC.") == 0)
  584. return 1;
  585. /* Do not ignore this symbol */
  586. return 0;
  587. }
  588. static void handle_modversion(const struct module *mod,
  589. const struct elf_info *info,
  590. const Elf_Sym *sym, const char *symname)
  591. {
  592. unsigned int crc;
  593. if (sym->st_shndx == SHN_UNDEF) {
  594. warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
  595. symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
  596. return;
  597. }
  598. if (sym->st_shndx == SHN_ABS) {
  599. crc = sym->st_value;
  600. } else {
  601. unsigned int *crcp;
  602. /* symbol points to the CRC in the ELF object */
  603. crcp = sym_get_data(info, sym);
  604. crc = TO_NATIVE(*crcp);
  605. }
  606. sym_set_crc(symname, crc);
  607. }
  608. static void handle_symbol(struct module *mod, struct elf_info *info,
  609. const Elf_Sym *sym, const char *symname)
  610. {
  611. enum export export;
  612. const char *name;
  613. if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
  614. strstarts(symname, "__ksymtab"))
  615. export = export_from_secname(info, get_secindex(info, sym));
  616. else
  617. export = export_from_sec(info, get_secindex(info, sym));
  618. switch (sym->st_shndx) {
  619. case SHN_COMMON:
  620. if (strstarts(symname, "__gnu_lto_")) {
  621. /* Should warn here, but modpost runs before the linker */
  622. } else
  623. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  624. break;
  625. case SHN_UNDEF:
  626. /* undefined symbol */
  627. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  628. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  629. break;
  630. if (ignore_undef_symbol(info, symname))
  631. break;
  632. if (info->hdr->e_machine == EM_SPARC ||
  633. info->hdr->e_machine == EM_SPARCV9) {
  634. /* Ignore register directives. */
  635. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  636. break;
  637. if (symname[0] == '.') {
  638. char *munged = NOFAIL(strdup(symname));
  639. munged[0] = '_';
  640. munged[1] = toupper(munged[1]);
  641. symname = munged;
  642. }
  643. }
  644. mod->unres = alloc_symbol(symname,
  645. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  646. mod->unres);
  647. break;
  648. default:
  649. /* All exported symbols */
  650. if (strstarts(symname, "__ksymtab_")) {
  651. name = symname + strlen("__ksymtab_");
  652. sym_add_exported(name, mod, export);
  653. }
  654. if (strcmp(symname, "init_module") == 0)
  655. mod->has_init = 1;
  656. if (strcmp(symname, "cleanup_module") == 0)
  657. mod->has_cleanup = 1;
  658. break;
  659. }
  660. }
  661. /**
  662. * Parse tag=value strings from .modinfo section
  663. **/
  664. static char *next_string(char *string, unsigned long *secsize)
  665. {
  666. /* Skip non-zero chars */
  667. while (string[0]) {
  668. string++;
  669. if ((*secsize)-- <= 1)
  670. return NULL;
  671. }
  672. /* Skip any zero padding. */
  673. while (!string[0]) {
  674. string++;
  675. if ((*secsize)-- <= 1)
  676. return NULL;
  677. }
  678. return string;
  679. }
  680. static char *get_next_modinfo(struct elf_info *info, const char *tag,
  681. char *prev)
  682. {
  683. char *p;
  684. unsigned int taglen = strlen(tag);
  685. char *modinfo = info->modinfo;
  686. unsigned long size = info->modinfo_len;
  687. if (prev) {
  688. size -= prev - modinfo;
  689. modinfo = next_string(prev, &size);
  690. }
  691. for (p = modinfo; p; p = next_string(p, &size)) {
  692. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  693. return p + taglen + 1;
  694. }
  695. return NULL;
  696. }
  697. static char *get_modinfo(struct elf_info *info, const char *tag)
  698. {
  699. return get_next_modinfo(info, tag, NULL);
  700. }
  701. /**
  702. * Test if string s ends in string sub
  703. * return 0 if match
  704. **/
  705. static int strrcmp(const char *s, const char *sub)
  706. {
  707. int slen, sublen;
  708. if (!s || !sub)
  709. return 1;
  710. slen = strlen(s);
  711. sublen = strlen(sub);
  712. if ((slen == 0) || (sublen == 0))
  713. return 1;
  714. if (sublen > slen)
  715. return 1;
  716. return memcmp(s + slen - sublen, sub, sublen);
  717. }
  718. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  719. {
  720. if (sym)
  721. return elf->strtab + sym->st_name;
  722. else
  723. return "(unknown)";
  724. }
  725. /* The pattern is an array of simple patterns.
  726. * "foo" will match an exact string equal to "foo"
  727. * "*foo" will match a string that ends with "foo"
  728. * "foo*" will match a string that begins with "foo"
  729. * "*foo*" will match a string that contains "foo"
  730. */
  731. static int match(const char *sym, const char * const pat[])
  732. {
  733. const char *p;
  734. while (*pat) {
  735. p = *pat++;
  736. const char *endp = p + strlen(p) - 1;
  737. /* "*foo*" */
  738. if (*p == '*' && *endp == '*') {
  739. char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
  740. char *here = strstr(sym, bare);
  741. free(bare);
  742. if (here != NULL)
  743. return 1;
  744. }
  745. /* "*foo" */
  746. else if (*p == '*') {
  747. if (strrcmp(sym, p + 1) == 0)
  748. return 1;
  749. }
  750. /* "foo*" */
  751. else if (*endp == '*') {
  752. if (strncmp(sym, p, strlen(p) - 1) == 0)
  753. return 1;
  754. }
  755. /* no wildcards */
  756. else {
  757. if (strcmp(p, sym) == 0)
  758. return 1;
  759. }
  760. }
  761. /* no match */
  762. return 0;
  763. }
  764. /* sections that we do not want to do full section mismatch check on */
  765. static const char *const section_white_list[] =
  766. {
  767. ".comment*",
  768. ".debug*",
  769. ".cranges", /* sh64 */
  770. ".zdebug*", /* Compressed debug sections. */
  771. ".GCC.command.line", /* record-gcc-switches */
  772. ".mdebug*", /* alpha, score, mips etc. */
  773. ".pdr", /* alpha, score, mips etc. */
  774. ".stab*",
  775. ".note*",
  776. ".got*",
  777. ".toc*",
  778. ".xt.prop", /* xtensa */
  779. ".xt.lit", /* xtensa */
  780. ".arcextmap*", /* arc */
  781. ".gnu.linkonce.arcext*", /* arc : modules */
  782. ".cmem*", /* EZchip */
  783. ".fmt_slot*", /* EZchip */
  784. ".gnu.lto*",
  785. ".discard.*",
  786. NULL
  787. };
  788. /*
  789. * This is used to find sections missing the SHF_ALLOC flag.
  790. * The cause of this is often a section specified in assembler
  791. * without "ax" / "aw".
  792. */
  793. static void check_section(const char *modname, struct elf_info *elf,
  794. Elf_Shdr *sechdr)
  795. {
  796. const char *sec = sech_name(elf, sechdr);
  797. if (sechdr->sh_type == SHT_PROGBITS &&
  798. !(sechdr->sh_flags & SHF_ALLOC) &&
  799. !match(sec, section_white_list)) {
  800. warn("%s (%s): unexpected non-allocatable section.\n"
  801. "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
  802. "Note that for example <linux/init.h> contains\n"
  803. "section definitions for use in .S files.\n\n",
  804. modname, sec);
  805. }
  806. }
  807. #define ALL_INIT_DATA_SECTIONS \
  808. ".init.setup", ".init.rodata", ".meminit.rodata", \
  809. ".init.data", ".meminit.data"
  810. #define ALL_EXIT_DATA_SECTIONS \
  811. ".exit.data", ".memexit.data"
  812. #define ALL_INIT_TEXT_SECTIONS \
  813. ".init.text", ".meminit.text"
  814. #define ALL_EXIT_TEXT_SECTIONS \
  815. ".exit.text", ".memexit.text"
  816. #define ALL_PCI_INIT_SECTIONS \
  817. ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
  818. ".pci_fixup_enable", ".pci_fixup_resume", \
  819. ".pci_fixup_resume_early", ".pci_fixup_suspend"
  820. #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
  821. #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
  822. #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
  823. #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
  824. #define DATA_SECTIONS ".data", ".data.rel"
  825. #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
  826. ".kprobes.text", ".cpuidle.text"
  827. #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
  828. ".fixup", ".entry.text", ".exception.text", ".text.*", \
  829. ".coldtext"
  830. #define INIT_SECTIONS ".init.*"
  831. #define MEM_INIT_SECTIONS ".meminit.*"
  832. #define EXIT_SECTIONS ".exit.*"
  833. #define MEM_EXIT_SECTIONS ".memexit.*"
  834. #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
  835. TEXT_SECTIONS, OTHER_TEXT_SECTIONS
  836. /* init data sections */
  837. static const char *const init_data_sections[] =
  838. { ALL_INIT_DATA_SECTIONS, NULL };
  839. /* all init sections */
  840. static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
  841. /* All init and exit sections (code + data) */
  842. static const char *const init_exit_sections[] =
  843. {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
  844. /* all text sections */
  845. static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
  846. /* data section */
  847. static const char *const data_sections[] = { DATA_SECTIONS, NULL };
  848. /* symbols in .data that may refer to init/exit sections */
  849. #define DEFAULT_SYMBOL_WHITE_LIST \
  850. "*driver", \
  851. "*_template", /* scsi uses *_template a lot */ \
  852. "*_timer", /* arm uses ops structures named _timer a lot */ \
  853. "*_sht", /* scsi also used *_sht to some extent */ \
  854. "*_ops", \
  855. "*_probe", \
  856. "*_probe_one", \
  857. "*_console"
  858. static const char *const head_sections[] = { ".head.text*", NULL };
  859. static const char *const linker_symbols[] =
  860. { "__init_begin", "_sinittext", "_einittext", NULL };
  861. static const char *const optim_symbols[] = { "*.constprop.*", NULL };
  862. enum mismatch {
  863. TEXT_TO_ANY_INIT,
  864. DATA_TO_ANY_INIT,
  865. TEXT_TO_ANY_EXIT,
  866. DATA_TO_ANY_EXIT,
  867. XXXINIT_TO_SOME_INIT,
  868. XXXEXIT_TO_SOME_EXIT,
  869. ANY_INIT_TO_ANY_EXIT,
  870. ANY_EXIT_TO_ANY_INIT,
  871. EXPORT_TO_INIT_EXIT,
  872. EXTABLE_TO_NON_TEXT,
  873. };
  874. /**
  875. * Describe how to match sections on different criterias:
  876. *
  877. * @fromsec: Array of sections to be matched.
  878. *
  879. * @bad_tosec: Relocations applied to a section in @fromsec to a section in
  880. * this array is forbidden (black-list). Can be empty.
  881. *
  882. * @good_tosec: Relocations applied to a section in @fromsec must be
  883. * targetting sections in this array (white-list). Can be empty.
  884. *
  885. * @mismatch: Type of mismatch.
  886. *
  887. * @symbol_white_list: Do not match a relocation to a symbol in this list
  888. * even if it is targetting a section in @bad_to_sec.
  889. *
  890. * @handler: Specific handler to call when a match is found. If NULL,
  891. * default_mismatch_handler() will be called.
  892. *
  893. */
  894. struct sectioncheck {
  895. const char *fromsec[20];
  896. const char *bad_tosec[20];
  897. const char *good_tosec[20];
  898. enum mismatch mismatch;
  899. const char *symbol_white_list[20];
  900. void (*handler)(const char *modname, struct elf_info *elf,
  901. const struct sectioncheck* const mismatch,
  902. Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
  903. };
  904. static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
  905. const struct sectioncheck* const mismatch,
  906. Elf_Rela *r, Elf_Sym *sym,
  907. const char *fromsec);
  908. static const struct sectioncheck sectioncheck[] = {
  909. /* Do not reference init/exit code/data from
  910. * normal code and data
  911. */
  912. {
  913. .fromsec = { TEXT_SECTIONS, NULL },
  914. .bad_tosec = { ALL_INIT_SECTIONS, NULL },
  915. .mismatch = TEXT_TO_ANY_INIT,
  916. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  917. },
  918. {
  919. .fromsec = { DATA_SECTIONS, NULL },
  920. .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
  921. .mismatch = DATA_TO_ANY_INIT,
  922. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  923. },
  924. {
  925. .fromsec = { DATA_SECTIONS, NULL },
  926. .bad_tosec = { INIT_SECTIONS, NULL },
  927. .mismatch = DATA_TO_ANY_INIT,
  928. .symbol_white_list = {
  929. "*_template", "*_timer", "*_sht", "*_ops",
  930. "*_probe", "*_probe_one", "*_console", NULL
  931. },
  932. },
  933. {
  934. .fromsec = { TEXT_SECTIONS, NULL },
  935. .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
  936. .mismatch = TEXT_TO_ANY_EXIT,
  937. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  938. },
  939. {
  940. .fromsec = { DATA_SECTIONS, NULL },
  941. .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
  942. .mismatch = DATA_TO_ANY_EXIT,
  943. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  944. },
  945. /* Do not reference init code/data from meminit code/data */
  946. {
  947. .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
  948. .bad_tosec = { INIT_SECTIONS, NULL },
  949. .mismatch = XXXINIT_TO_SOME_INIT,
  950. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  951. },
  952. /* Do not reference exit code/data from memexit code/data */
  953. {
  954. .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
  955. .bad_tosec = { EXIT_SECTIONS, NULL },
  956. .mismatch = XXXEXIT_TO_SOME_EXIT,
  957. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  958. },
  959. /* Do not use exit code/data from init code */
  960. {
  961. .fromsec = { ALL_INIT_SECTIONS, NULL },
  962. .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
  963. .mismatch = ANY_INIT_TO_ANY_EXIT,
  964. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  965. },
  966. /* Do not use init code/data from exit code */
  967. {
  968. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  969. .bad_tosec = { ALL_INIT_SECTIONS, NULL },
  970. .mismatch = ANY_EXIT_TO_ANY_INIT,
  971. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  972. },
  973. {
  974. .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
  975. .bad_tosec = { INIT_SECTIONS, NULL },
  976. .mismatch = ANY_INIT_TO_ANY_EXIT,
  977. .symbol_white_list = { NULL },
  978. },
  979. /* Do not export init/exit functions or data */
  980. {
  981. .fromsec = { "__ksymtab*", NULL },
  982. .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
  983. .mismatch = EXPORT_TO_INIT_EXIT,
  984. .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
  985. },
  986. {
  987. .fromsec = { "__ex_table", NULL },
  988. /* If you're adding any new black-listed sections in here, consider
  989. * adding a special 'printer' for them in scripts/check_extable.
  990. */
  991. .bad_tosec = { ".altinstr_replacement", NULL },
  992. .good_tosec = {ALL_TEXT_SECTIONS , NULL},
  993. .mismatch = EXTABLE_TO_NON_TEXT,
  994. .handler = extable_mismatch_handler,
  995. }
  996. };
  997. static const struct sectioncheck *section_mismatch(
  998. const char *fromsec, const char *tosec)
  999. {
  1000. int i;
  1001. int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
  1002. const struct sectioncheck *check = &sectioncheck[0];
  1003. /*
  1004. * The target section could be the SHT_NUL section when we're
  1005. * handling relocations to un-resolved symbols, trying to match it
  1006. * doesn't make much sense and causes build failures on parisc
  1007. * architectures.
  1008. */
  1009. if (*tosec == '\0')
  1010. return NULL;
  1011. for (i = 0; i < elems; i++) {
  1012. if (match(fromsec, check->fromsec)) {
  1013. if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
  1014. return check;
  1015. if (check->good_tosec[0] && !match(tosec, check->good_tosec))
  1016. return check;
  1017. }
  1018. check++;
  1019. }
  1020. return NULL;
  1021. }
  1022. /**
  1023. * Whitelist to allow certain references to pass with no warning.
  1024. *
  1025. * Pattern 1:
  1026. * If a module parameter is declared __initdata and permissions=0
  1027. * then this is legal despite the warning generated.
  1028. * We cannot see value of permissions here, so just ignore
  1029. * this pattern.
  1030. * The pattern is identified by:
  1031. * tosec = .init.data
  1032. * fromsec = .data*
  1033. * atsym =__param*
  1034. *
  1035. * Pattern 1a:
  1036. * module_param_call() ops can refer to __init set function if permissions=0
  1037. * The pattern is identified by:
  1038. * tosec = .init.text
  1039. * fromsec = .data*
  1040. * atsym = __param_ops_*
  1041. *
  1042. * Pattern 2:
  1043. * Many drivers utilise a *driver container with references to
  1044. * add, remove, probe functions etc.
  1045. * the pattern is identified by:
  1046. * tosec = init or exit section
  1047. * fromsec = data section
  1048. * atsym = *driver, *_template, *_sht, *_ops, *_probe,
  1049. * *probe_one, *_console, *_timer
  1050. *
  1051. * Pattern 3:
  1052. * Whitelist all references from .head.text to any init section
  1053. *
  1054. * Pattern 4:
  1055. * Some symbols belong to init section but still it is ok to reference
  1056. * these from non-init sections as these symbols don't have any memory
  1057. * allocated for them and symbol address and value are same. So even
  1058. * if init section is freed, its ok to reference those symbols.
  1059. * For ex. symbols marking the init section boundaries.
  1060. * This pattern is identified by
  1061. * refsymname = __init_begin, _sinittext, _einittext
  1062. *
  1063. * Pattern 5:
  1064. * GCC may optimize static inlines when fed constant arg(s) resulting
  1065. * in functions like cpumask_empty() -- generating an associated symbol
  1066. * cpumask_empty.constprop.3 that appears in the audit. If the const that
  1067. * is passed in comes from __init, like say nmi_ipi_mask, we get a
  1068. * meaningless section warning. May need to add isra symbols too...
  1069. * This pattern is identified by
  1070. * tosec = init section
  1071. * fromsec = text section
  1072. * refsymname = *.constprop.*
  1073. *
  1074. * Pattern 6:
  1075. * Hide section mismatch warnings for ELF local symbols. The goal
  1076. * is to eliminate false positive modpost warnings caused by
  1077. * compiler-generated ELF local symbol names such as ".LANCHOR1".
  1078. * Autogenerated symbol names bypass modpost's "Pattern 2"
  1079. * whitelisting, which relies on pattern-matching against symbol
  1080. * names to work. (One situation where gcc can autogenerate ELF
  1081. * local symbols is when "-fsection-anchors" is used.)
  1082. **/
  1083. static int secref_whitelist(const struct sectioncheck *mismatch,
  1084. const char *fromsec, const char *fromsym,
  1085. const char *tosec, const char *tosym)
  1086. {
  1087. /* Check for pattern 1 */
  1088. if (match(tosec, init_data_sections) &&
  1089. match(fromsec, data_sections) &&
  1090. strstarts(fromsym, "__param"))
  1091. return 0;
  1092. /* Check for pattern 1a */
  1093. if (strcmp(tosec, ".init.text") == 0 &&
  1094. match(fromsec, data_sections) &&
  1095. strstarts(fromsym, "__param_ops_"))
  1096. return 0;
  1097. /* Check for pattern 2 */
  1098. if (match(tosec, init_exit_sections) &&
  1099. match(fromsec, data_sections) &&
  1100. match(fromsym, mismatch->symbol_white_list))
  1101. return 0;
  1102. /* Check for pattern 3 */
  1103. if (match(fromsec, head_sections) &&
  1104. match(tosec, init_sections))
  1105. return 0;
  1106. /* Check for pattern 4 */
  1107. if (match(tosym, linker_symbols))
  1108. return 0;
  1109. /* Check for pattern 5 */
  1110. if (match(fromsec, text_sections) &&
  1111. match(tosec, init_sections) &&
  1112. match(fromsym, optim_symbols))
  1113. return 0;
  1114. /* Check for pattern 6 */
  1115. if (strstarts(fromsym, ".L"))
  1116. return 0;
  1117. return 1;
  1118. }
  1119. static inline int is_arm_mapping_symbol(const char *str)
  1120. {
  1121. return str[0] == '$' && strchr("axtd", str[1])
  1122. && (str[2] == '\0' || str[2] == '.');
  1123. }
  1124. /*
  1125. * If there's no name there, ignore it; likewise, ignore it if it's
  1126. * one of the magic symbols emitted used by current ARM tools.
  1127. *
  1128. * Otherwise if find_symbols_between() returns those symbols, they'll
  1129. * fail the whitelist tests and cause lots of false alarms ... fixable
  1130. * only by merging __exit and __init sections into __text, bloating
  1131. * the kernel (which is especially evil on embedded platforms).
  1132. */
  1133. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  1134. {
  1135. const char *name = elf->strtab + sym->st_name;
  1136. if (!name || !strlen(name))
  1137. return 0;
  1138. return !is_arm_mapping_symbol(name);
  1139. }
  1140. /**
  1141. * Find symbol based on relocation record info.
  1142. * In some cases the symbol supplied is a valid symbol so
  1143. * return refsym. If st_name != 0 we assume this is a valid symbol.
  1144. * In other cases the symbol needs to be looked up in the symbol table
  1145. * based on section and address.
  1146. * **/
  1147. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
  1148. Elf_Sym *relsym)
  1149. {
  1150. Elf_Sym *sym;
  1151. Elf_Sym *near = NULL;
  1152. Elf64_Sword distance = 20;
  1153. Elf64_Sword d;
  1154. unsigned int relsym_secindex;
  1155. if (relsym->st_name != 0)
  1156. return relsym;
  1157. relsym_secindex = get_secindex(elf, relsym);
  1158. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  1159. if (get_secindex(elf, sym) != relsym_secindex)
  1160. continue;
  1161. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  1162. continue;
  1163. if (!is_valid_name(elf, sym))
  1164. continue;
  1165. if (sym->st_value == addr)
  1166. return sym;
  1167. /* Find a symbol nearby - addr are maybe negative */
  1168. d = sym->st_value - addr;
  1169. if (d < 0)
  1170. d = addr - sym->st_value;
  1171. if (d < distance) {
  1172. distance = d;
  1173. near = sym;
  1174. }
  1175. }
  1176. /* We need a close match */
  1177. if (distance < 20)
  1178. return near;
  1179. else
  1180. return NULL;
  1181. }
  1182. /*
  1183. * Find symbols before or equal addr and after addr - in the section sec.
  1184. * If we find two symbols with equal offset prefer one with a valid name.
  1185. * The ELF format may have a better way to detect what type of symbol
  1186. * it is, but this works for now.
  1187. **/
  1188. static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
  1189. const char *sec)
  1190. {
  1191. Elf_Sym *sym;
  1192. Elf_Sym *near = NULL;
  1193. Elf_Addr distance = ~0;
  1194. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  1195. const char *symsec;
  1196. if (is_shndx_special(sym->st_shndx))
  1197. continue;
  1198. symsec = sec_name(elf, get_secindex(elf, sym));
  1199. if (strcmp(symsec, sec) != 0)
  1200. continue;
  1201. if (!is_valid_name(elf, sym))
  1202. continue;
  1203. if (sym->st_value <= addr) {
  1204. if ((addr - sym->st_value) < distance) {
  1205. distance = addr - sym->st_value;
  1206. near = sym;
  1207. } else if ((addr - sym->st_value) == distance) {
  1208. near = sym;
  1209. }
  1210. }
  1211. }
  1212. return near;
  1213. }
  1214. /*
  1215. * Convert a section name to the function/data attribute
  1216. * .init.text => __init
  1217. * .memexitconst => __memconst
  1218. * etc.
  1219. *
  1220. * The memory of returned value has been allocated on a heap. The user of this
  1221. * method should free it after usage.
  1222. */
  1223. static char *sec2annotation(const char *s)
  1224. {
  1225. if (match(s, init_exit_sections)) {
  1226. char *p = NOFAIL(malloc(20));
  1227. char *r = p;
  1228. *p++ = '_';
  1229. *p++ = '_';
  1230. if (*s == '.')
  1231. s++;
  1232. while (*s && *s != '.')
  1233. *p++ = *s++;
  1234. *p = '\0';
  1235. if (*s == '.')
  1236. s++;
  1237. if (strstr(s, "rodata") != NULL)
  1238. strcat(p, "const ");
  1239. else if (strstr(s, "data") != NULL)
  1240. strcat(p, "data ");
  1241. else
  1242. strcat(p, " ");
  1243. return r;
  1244. } else {
  1245. return NOFAIL(strdup(""));
  1246. }
  1247. }
  1248. static int is_function(Elf_Sym *sym)
  1249. {
  1250. if (sym)
  1251. return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
  1252. else
  1253. return -1;
  1254. }
  1255. static void print_section_list(const char * const list[20])
  1256. {
  1257. const char *const *s = list;
  1258. while (*s) {
  1259. fprintf(stderr, "%s", *s);
  1260. s++;
  1261. if (*s)
  1262. fprintf(stderr, ", ");
  1263. }
  1264. fprintf(stderr, "\n");
  1265. }
  1266. static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
  1267. {
  1268. switch (is_func) {
  1269. case 0: *name = "variable"; *name_p = ""; break;
  1270. case 1: *name = "function"; *name_p = "()"; break;
  1271. default: *name = "(unknown reference)"; *name_p = ""; break;
  1272. }
  1273. }
  1274. /*
  1275. * Print a warning about a section mismatch.
  1276. * Try to find symbols near it so user can find it.
  1277. * Check whitelist before warning - it may be a false positive.
  1278. */
  1279. static void report_sec_mismatch(const char *modname,
  1280. const struct sectioncheck *mismatch,
  1281. const char *fromsec,
  1282. unsigned long long fromaddr,
  1283. const char *fromsym,
  1284. int from_is_func,
  1285. const char *tosec, const char *tosym,
  1286. int to_is_func)
  1287. {
  1288. const char *from, *from_p;
  1289. const char *to, *to_p;
  1290. char *prl_from;
  1291. char *prl_to;
  1292. sec_mismatch_count++;
  1293. get_pretty_name(from_is_func, &from, &from_p);
  1294. get_pretty_name(to_is_func, &to, &to_p);
  1295. warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
  1296. "to the %s %s:%s%s\n",
  1297. modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
  1298. tosym, to_p);
  1299. switch (mismatch->mismatch) {
  1300. case TEXT_TO_ANY_INIT:
  1301. prl_from = sec2annotation(fromsec);
  1302. prl_to = sec2annotation(tosec);
  1303. fprintf(stderr,
  1304. "The function %s%s() references\n"
  1305. "the %s %s%s%s.\n"
  1306. "This is often because %s lacks a %s\n"
  1307. "annotation or the annotation of %s is wrong.\n",
  1308. prl_from, fromsym,
  1309. to, prl_to, tosym, to_p,
  1310. fromsym, prl_to, tosym);
  1311. free(prl_from);
  1312. free(prl_to);
  1313. break;
  1314. case DATA_TO_ANY_INIT: {
  1315. prl_to = sec2annotation(tosec);
  1316. fprintf(stderr,
  1317. "The variable %s references\n"
  1318. "the %s %s%s%s\n"
  1319. "If the reference is valid then annotate the\n"
  1320. "variable with __init* or __refdata (see linux/init.h) "
  1321. "or name the variable:\n",
  1322. fromsym, to, prl_to, tosym, to_p);
  1323. print_section_list(mismatch->symbol_white_list);
  1324. free(prl_to);
  1325. break;
  1326. }
  1327. case TEXT_TO_ANY_EXIT:
  1328. prl_to = sec2annotation(tosec);
  1329. fprintf(stderr,
  1330. "The function %s() references a %s in an exit section.\n"
  1331. "Often the %s %s%s has valid usage outside the exit section\n"
  1332. "and the fix is to remove the %sannotation of %s.\n",
  1333. fromsym, to, to, tosym, to_p, prl_to, tosym);
  1334. free(prl_to);
  1335. break;
  1336. case DATA_TO_ANY_EXIT: {
  1337. prl_to = sec2annotation(tosec);
  1338. fprintf(stderr,
  1339. "The variable %s references\n"
  1340. "the %s %s%s%s\n"
  1341. "If the reference is valid then annotate the\n"
  1342. "variable with __exit* (see linux/init.h) or "
  1343. "name the variable:\n",
  1344. fromsym, to, prl_to, tosym, to_p);
  1345. print_section_list(mismatch->symbol_white_list);
  1346. free(prl_to);
  1347. break;
  1348. }
  1349. case XXXINIT_TO_SOME_INIT:
  1350. case XXXEXIT_TO_SOME_EXIT:
  1351. prl_from = sec2annotation(fromsec);
  1352. prl_to = sec2annotation(tosec);
  1353. fprintf(stderr,
  1354. "The %s %s%s%s references\n"
  1355. "a %s %s%s%s.\n"
  1356. "If %s is only used by %s then\n"
  1357. "annotate %s with a matching annotation.\n",
  1358. from, prl_from, fromsym, from_p,
  1359. to, prl_to, tosym, to_p,
  1360. tosym, fromsym, tosym);
  1361. free(prl_from);
  1362. free(prl_to);
  1363. break;
  1364. case ANY_INIT_TO_ANY_EXIT:
  1365. prl_from = sec2annotation(fromsec);
  1366. prl_to = sec2annotation(tosec);
  1367. fprintf(stderr,
  1368. "The %s %s%s%s references\n"
  1369. "a %s %s%s%s.\n"
  1370. "This is often seen when error handling "
  1371. "in the init function\n"
  1372. "uses functionality in the exit path.\n"
  1373. "The fix is often to remove the %sannotation of\n"
  1374. "%s%s so it may be used outside an exit section.\n",
  1375. from, prl_from, fromsym, from_p,
  1376. to, prl_to, tosym, to_p,
  1377. prl_to, tosym, to_p);
  1378. free(prl_from);
  1379. free(prl_to);
  1380. break;
  1381. case ANY_EXIT_TO_ANY_INIT:
  1382. prl_from = sec2annotation(fromsec);
  1383. prl_to = sec2annotation(tosec);
  1384. fprintf(stderr,
  1385. "The %s %s%s%s references\n"
  1386. "a %s %s%s%s.\n"
  1387. "This is often seen when error handling "
  1388. "in the exit function\n"
  1389. "uses functionality in the init path.\n"
  1390. "The fix is often to remove the %sannotation of\n"
  1391. "%s%s so it may be used outside an init section.\n",
  1392. from, prl_from, fromsym, from_p,
  1393. to, prl_to, tosym, to_p,
  1394. prl_to, tosym, to_p);
  1395. free(prl_from);
  1396. free(prl_to);
  1397. break;
  1398. case EXPORT_TO_INIT_EXIT:
  1399. prl_to = sec2annotation(tosec);
  1400. fprintf(stderr,
  1401. "The symbol %s is exported and annotated %s\n"
  1402. "Fix this by removing the %sannotation of %s "
  1403. "or drop the export.\n",
  1404. tosym, prl_to, prl_to, tosym);
  1405. free(prl_to);
  1406. break;
  1407. case EXTABLE_TO_NON_TEXT:
  1408. fatal("There's a special handler for this mismatch type, "
  1409. "we should never get here.");
  1410. break;
  1411. }
  1412. fprintf(stderr, "\n");
  1413. }
  1414. static void default_mismatch_handler(const char *modname, struct elf_info *elf,
  1415. const struct sectioncheck* const mismatch,
  1416. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1417. {
  1418. const char *tosec;
  1419. Elf_Sym *to;
  1420. Elf_Sym *from;
  1421. const char *tosym;
  1422. const char *fromsym;
  1423. from = find_elf_symbol2(elf, r->r_offset, fromsec);
  1424. fromsym = sym_name(elf, from);
  1425. if (strstarts(fromsym, "reference___initcall"))
  1426. return;
  1427. tosec = sec_name(elf, get_secindex(elf, sym));
  1428. to = find_elf_symbol(elf, r->r_addend, sym);
  1429. tosym = sym_name(elf, to);
  1430. /* check whitelist - we may ignore it */
  1431. if (secref_whitelist(mismatch,
  1432. fromsec, fromsym, tosec, tosym)) {
  1433. report_sec_mismatch(modname, mismatch,
  1434. fromsec, r->r_offset, fromsym,
  1435. is_function(from), tosec, tosym,
  1436. is_function(to));
  1437. }
  1438. }
  1439. static int is_executable_section(struct elf_info* elf, unsigned int section_index)
  1440. {
  1441. if (section_index > elf->num_sections)
  1442. fatal("section_index is outside elf->num_sections!\n");
  1443. return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
  1444. }
  1445. /*
  1446. * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
  1447. * to know the sizeof(struct exception_table_entry) for the target architecture.
  1448. */
  1449. static unsigned int extable_entry_size = 0;
  1450. static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
  1451. {
  1452. /*
  1453. * If we're currently checking the second relocation within __ex_table,
  1454. * that relocation offset tells us the offsetof(struct
  1455. * exception_table_entry, fixup) which is equal to sizeof(struct
  1456. * exception_table_entry) divided by two. We use that to our advantage
  1457. * since there's no portable way to get that size as every architecture
  1458. * seems to go with different sized types. Not pretty but better than
  1459. * hard-coding the size for every architecture..
  1460. */
  1461. if (!extable_entry_size)
  1462. extable_entry_size = r->r_offset * 2;
  1463. }
  1464. static inline bool is_extable_fault_address(Elf_Rela *r)
  1465. {
  1466. /*
  1467. * extable_entry_size is only discovered after we've handled the
  1468. * _second_ relocation in __ex_table, so only abort when we're not
  1469. * handling the first reloc and extable_entry_size is zero.
  1470. */
  1471. if (r->r_offset && extable_entry_size == 0)
  1472. fatal("extable_entry size hasn't been discovered!\n");
  1473. return ((r->r_offset == 0) ||
  1474. (r->r_offset % extable_entry_size == 0));
  1475. }
  1476. #define is_second_extable_reloc(Start, Cur, Sec) \
  1477. (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
  1478. static void report_extable_warnings(const char* modname, struct elf_info* elf,
  1479. const struct sectioncheck* const mismatch,
  1480. Elf_Rela* r, Elf_Sym* sym,
  1481. const char* fromsec, const char* tosec)
  1482. {
  1483. Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
  1484. const char* fromsym_name = sym_name(elf, fromsym);
  1485. Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
  1486. const char* tosym_name = sym_name(elf, tosym);
  1487. const char* from_pretty_name;
  1488. const char* from_pretty_name_p;
  1489. const char* to_pretty_name;
  1490. const char* to_pretty_name_p;
  1491. get_pretty_name(is_function(fromsym),
  1492. &from_pretty_name, &from_pretty_name_p);
  1493. get_pretty_name(is_function(tosym),
  1494. &to_pretty_name, &to_pretty_name_p);
  1495. warn("%s(%s+0x%lx): Section mismatch in reference"
  1496. " from the %s %s%s to the %s %s:%s%s\n",
  1497. modname, fromsec, (long)r->r_offset, from_pretty_name,
  1498. fromsym_name, from_pretty_name_p,
  1499. to_pretty_name, tosec, tosym_name, to_pretty_name_p);
  1500. if (!match(tosec, mismatch->bad_tosec) &&
  1501. is_executable_section(elf, get_secindex(elf, sym)))
  1502. fprintf(stderr,
  1503. "The relocation at %s+0x%lx references\n"
  1504. "section \"%s\" which is not in the list of\n"
  1505. "authorized sections. If you're adding a new section\n"
  1506. "and/or if this reference is valid, add \"%s\" to the\n"
  1507. "list of authorized sections to jump to on fault.\n"
  1508. "This can be achieved by adding \"%s\" to \n"
  1509. "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
  1510. fromsec, (long)r->r_offset, tosec, tosec, tosec);
  1511. }
  1512. static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
  1513. const struct sectioncheck* const mismatch,
  1514. Elf_Rela* r, Elf_Sym* sym,
  1515. const char *fromsec)
  1516. {
  1517. const char* tosec = sec_name(elf, get_secindex(elf, sym));
  1518. sec_mismatch_count++;
  1519. report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
  1520. if (match(tosec, mismatch->bad_tosec))
  1521. fatal("The relocation at %s+0x%lx references\n"
  1522. "section \"%s\" which is black-listed.\n"
  1523. "Something is seriously wrong and should be fixed.\n"
  1524. "You might get more information about where this is\n"
  1525. "coming from by using scripts/check_extable.sh %s\n",
  1526. fromsec, (long)r->r_offset, tosec, modname);
  1527. else if (!is_executable_section(elf, get_secindex(elf, sym))) {
  1528. if (is_extable_fault_address(r))
  1529. fatal("The relocation at %s+0x%lx references\n"
  1530. "section \"%s\" which is not executable, IOW\n"
  1531. "it is not possible for the kernel to fault\n"
  1532. "at that address. Something is seriously wrong\n"
  1533. "and should be fixed.\n",
  1534. fromsec, (long)r->r_offset, tosec);
  1535. else
  1536. fatal("The relocation at %s+0x%lx references\n"
  1537. "section \"%s\" which is not executable, IOW\n"
  1538. "the kernel will fault if it ever tries to\n"
  1539. "jump to it. Something is seriously wrong\n"
  1540. "and should be fixed.\n",
  1541. fromsec, (long)r->r_offset, tosec);
  1542. }
  1543. }
  1544. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  1545. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1546. {
  1547. const char *tosec = sec_name(elf, get_secindex(elf, sym));
  1548. const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
  1549. if (mismatch) {
  1550. if (mismatch->handler)
  1551. mismatch->handler(modname, elf, mismatch,
  1552. r, sym, fromsec);
  1553. else
  1554. default_mismatch_handler(modname, elf, mismatch,
  1555. r, sym, fromsec);
  1556. }
  1557. }
  1558. static unsigned int *reloc_location(struct elf_info *elf,
  1559. Elf_Shdr *sechdr, Elf_Rela *r)
  1560. {
  1561. Elf_Shdr *sechdrs = elf->sechdrs;
  1562. int section = sechdr->sh_info;
  1563. return (void *)elf->hdr + sechdrs[section].sh_offset +
  1564. r->r_offset;
  1565. }
  1566. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1567. {
  1568. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1569. unsigned int *location = reloc_location(elf, sechdr, r);
  1570. switch (r_typ) {
  1571. case R_386_32:
  1572. r->r_addend = TO_NATIVE(*location);
  1573. break;
  1574. case R_386_PC32:
  1575. r->r_addend = TO_NATIVE(*location) + 4;
  1576. /* For CONFIG_RELOCATABLE=y */
  1577. if (elf->hdr->e_type == ET_EXEC)
  1578. r->r_addend += r->r_offset;
  1579. break;
  1580. }
  1581. return 0;
  1582. }
  1583. #ifndef R_ARM_CALL
  1584. #define R_ARM_CALL 28
  1585. #endif
  1586. #ifndef R_ARM_JUMP24
  1587. #define R_ARM_JUMP24 29
  1588. #endif
  1589. #ifndef R_ARM_THM_CALL
  1590. #define R_ARM_THM_CALL 10
  1591. #endif
  1592. #ifndef R_ARM_THM_JUMP24
  1593. #define R_ARM_THM_JUMP24 30
  1594. #endif
  1595. #ifndef R_ARM_THM_JUMP19
  1596. #define R_ARM_THM_JUMP19 51
  1597. #endif
  1598. static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1599. {
  1600. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1601. switch (r_typ) {
  1602. case R_ARM_ABS32:
  1603. /* From ARM ABI: (S + A) | T */
  1604. r->r_addend = (int)(long)
  1605. (elf->symtab_start + ELF_R_SYM(r->r_info));
  1606. break;
  1607. case R_ARM_PC24:
  1608. case R_ARM_CALL:
  1609. case R_ARM_JUMP24:
  1610. case R_ARM_THM_CALL:
  1611. case R_ARM_THM_JUMP24:
  1612. case R_ARM_THM_JUMP19:
  1613. /* From ARM ABI: ((S + A) | T) - P */
  1614. r->r_addend = (int)(long)(elf->hdr +
  1615. sechdr->sh_offset +
  1616. (r->r_offset - sechdr->sh_addr));
  1617. break;
  1618. default:
  1619. return 1;
  1620. }
  1621. return 0;
  1622. }
  1623. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1624. {
  1625. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1626. unsigned int *location = reloc_location(elf, sechdr, r);
  1627. unsigned int inst;
  1628. if (r_typ == R_MIPS_HI16)
  1629. return 1; /* skip this */
  1630. inst = TO_NATIVE(*location);
  1631. switch (r_typ) {
  1632. case R_MIPS_LO16:
  1633. r->r_addend = inst & 0xffff;
  1634. break;
  1635. case R_MIPS_26:
  1636. r->r_addend = (inst & 0x03ffffff) << 2;
  1637. break;
  1638. case R_MIPS_32:
  1639. r->r_addend = inst;
  1640. break;
  1641. }
  1642. return 0;
  1643. }
  1644. static void section_rela(const char *modname, struct elf_info *elf,
  1645. Elf_Shdr *sechdr)
  1646. {
  1647. Elf_Sym *sym;
  1648. Elf_Rela *rela;
  1649. Elf_Rela r;
  1650. unsigned int r_sym;
  1651. const char *fromsec;
  1652. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  1653. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  1654. fromsec = sech_name(elf, sechdr);
  1655. fromsec += strlen(".rela");
  1656. /* if from section (name) is know good then skip it */
  1657. if (match(fromsec, section_white_list))
  1658. return;
  1659. for (rela = start; rela < stop; rela++) {
  1660. r.r_offset = TO_NATIVE(rela->r_offset);
  1661. #if KERNEL_ELFCLASS == ELFCLASS64
  1662. if (elf->hdr->e_machine == EM_MIPS) {
  1663. unsigned int r_typ;
  1664. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  1665. r_sym = TO_NATIVE(r_sym);
  1666. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  1667. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1668. } else {
  1669. r.r_info = TO_NATIVE(rela->r_info);
  1670. r_sym = ELF_R_SYM(r.r_info);
  1671. }
  1672. #else
  1673. r.r_info = TO_NATIVE(rela->r_info);
  1674. r_sym = ELF_R_SYM(r.r_info);
  1675. #endif
  1676. r.r_addend = TO_NATIVE(rela->r_addend);
  1677. sym = elf->symtab_start + r_sym;
  1678. /* Skip special sections */
  1679. if (is_shndx_special(sym->st_shndx))
  1680. continue;
  1681. if (is_second_extable_reloc(start, rela, fromsec))
  1682. find_extable_entry_size(fromsec, &r);
  1683. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1684. }
  1685. }
  1686. static void section_rel(const char *modname, struct elf_info *elf,
  1687. Elf_Shdr *sechdr)
  1688. {
  1689. Elf_Sym *sym;
  1690. Elf_Rel *rel;
  1691. Elf_Rela r;
  1692. unsigned int r_sym;
  1693. const char *fromsec;
  1694. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1695. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1696. fromsec = sech_name(elf, sechdr);
  1697. fromsec += strlen(".rel");
  1698. /* if from section (name) is know good then skip it */
  1699. if (match(fromsec, section_white_list))
  1700. return;
  1701. for (rel = start; rel < stop; rel++) {
  1702. r.r_offset = TO_NATIVE(rel->r_offset);
  1703. #if KERNEL_ELFCLASS == ELFCLASS64
  1704. if (elf->hdr->e_machine == EM_MIPS) {
  1705. unsigned int r_typ;
  1706. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1707. r_sym = TO_NATIVE(r_sym);
  1708. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1709. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1710. } else {
  1711. r.r_info = TO_NATIVE(rel->r_info);
  1712. r_sym = ELF_R_SYM(r.r_info);
  1713. }
  1714. #else
  1715. r.r_info = TO_NATIVE(rel->r_info);
  1716. r_sym = ELF_R_SYM(r.r_info);
  1717. #endif
  1718. r.r_addend = 0;
  1719. switch (elf->hdr->e_machine) {
  1720. case EM_386:
  1721. if (addend_386_rel(elf, sechdr, &r))
  1722. continue;
  1723. break;
  1724. case EM_ARM:
  1725. if (addend_arm_rel(elf, sechdr, &r))
  1726. continue;
  1727. break;
  1728. case EM_MIPS:
  1729. if (addend_mips_rel(elf, sechdr, &r))
  1730. continue;
  1731. break;
  1732. }
  1733. sym = elf->symtab_start + r_sym;
  1734. /* Skip special sections */
  1735. if (is_shndx_special(sym->st_shndx))
  1736. continue;
  1737. if (is_second_extable_reloc(start, rel, fromsec))
  1738. find_extable_entry_size(fromsec, &r);
  1739. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1740. }
  1741. }
  1742. /**
  1743. * A module includes a number of sections that are discarded
  1744. * either when loaded or when used as built-in.
  1745. * For loaded modules all functions marked __init and all data
  1746. * marked __initdata will be discarded when the module has been initialized.
  1747. * Likewise for modules used built-in the sections marked __exit
  1748. * are discarded because __exit marked function are supposed to be called
  1749. * only when a module is unloaded which never happens for built-in modules.
  1750. * The check_sec_ref() function traverses all relocation records
  1751. * to find all references to a section that reference a section that will
  1752. * be discarded and warns about it.
  1753. **/
  1754. static void check_sec_ref(struct module *mod, const char *modname,
  1755. struct elf_info *elf)
  1756. {
  1757. int i;
  1758. Elf_Shdr *sechdrs = elf->sechdrs;
  1759. /* Walk through all sections */
  1760. for (i = 0; i < elf->num_sections; i++) {
  1761. check_section(modname, elf, &elf->sechdrs[i]);
  1762. /* We want to process only relocation sections and not .init */
  1763. if (sechdrs[i].sh_type == SHT_RELA)
  1764. section_rela(modname, elf, &elf->sechdrs[i]);
  1765. else if (sechdrs[i].sh_type == SHT_REL)
  1766. section_rel(modname, elf, &elf->sechdrs[i]);
  1767. }
  1768. }
  1769. static char *remove_dot(char *s)
  1770. {
  1771. size_t n = strcspn(s, ".");
  1772. if (n && s[n]) {
  1773. size_t m = strspn(s + n + 1, "0123456789");
  1774. if (m && (s[n + m] == '.' || s[n + m] == 0))
  1775. s[n] = 0;
  1776. }
  1777. return s;
  1778. }
  1779. static void read_symbols(const char *modname)
  1780. {
  1781. const char *symname;
  1782. char *version;
  1783. char *license;
  1784. char *namespace;
  1785. struct module *mod;
  1786. struct elf_info info = { };
  1787. Elf_Sym *sym;
  1788. if (!parse_elf(&info, modname))
  1789. return;
  1790. mod = new_module(modname);
  1791. /* When there's no vmlinux, don't print warnings about
  1792. * unresolved symbols (since there'll be too many ;) */
  1793. if (is_vmlinux(modname)) {
  1794. have_vmlinux = 1;
  1795. mod->skip = 1;
  1796. }
  1797. license = get_modinfo(&info, "license");
  1798. if (!license && !is_vmlinux(modname))
  1799. warn("missing MODULE_LICENSE() in %s\n"
  1800. "see include/linux/module.h for "
  1801. "more information\n", modname);
  1802. while (license) {
  1803. if (license_is_gpl_compatible(license))
  1804. mod->gpl_compatible = 1;
  1805. else {
  1806. mod->gpl_compatible = 0;
  1807. break;
  1808. }
  1809. license = get_next_modinfo(&info, "license", license);
  1810. }
  1811. namespace = get_modinfo(&info, "import_ns");
  1812. while (namespace) {
  1813. add_namespace(&mod->imported_namespaces, namespace);
  1814. namespace = get_next_modinfo(&info, "import_ns", namespace);
  1815. }
  1816. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1817. symname = remove_dot(info.strtab + sym->st_name);
  1818. handle_symbol(mod, &info, sym, symname);
  1819. handle_moddevtable(mod, &info, sym, symname);
  1820. }
  1821. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1822. symname = remove_dot(info.strtab + sym->st_name);
  1823. /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
  1824. if (strstarts(symname, "__kstrtabns_"))
  1825. sym_update_namespace(symname + strlen("__kstrtabns_"),
  1826. namespace_from_kstrtabns(&info,
  1827. sym));
  1828. if (strstarts(symname, "__crc_"))
  1829. handle_modversion(mod, &info, sym,
  1830. symname + strlen("__crc_"));
  1831. }
  1832. // check for static EXPORT_SYMBOL_* functions && global vars
  1833. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1834. unsigned char bind = ELF_ST_BIND(sym->st_info);
  1835. if (bind == STB_GLOBAL || bind == STB_WEAK) {
  1836. struct symbol *s =
  1837. find_symbol(remove_dot(info.strtab +
  1838. sym->st_name));
  1839. if (s)
  1840. s->is_static = 0;
  1841. }
  1842. }
  1843. if (!is_vmlinux(modname) || vmlinux_section_warnings)
  1844. check_sec_ref(mod, modname, &info);
  1845. version = get_modinfo(&info, "version");
  1846. if (version)
  1847. maybe_frob_rcs_version(modname, version, info.modinfo,
  1848. version - (char *)info.hdr);
  1849. if (version || (all_versions && !is_vmlinux(modname)))
  1850. get_src_version(modname, mod->srcversion,
  1851. sizeof(mod->srcversion)-1);
  1852. parse_elf_finish(&info);
  1853. /* Our trick to get versioning for module struct etc. - it's
  1854. * never passed as an argument to an exported function, so
  1855. * the automatic versioning doesn't pick it up, but it's really
  1856. * important anyhow */
  1857. if (modversions)
  1858. mod->unres = alloc_symbol("module_layout", 0, mod->unres);
  1859. }
  1860. static void read_symbols_from_files(const char *filename)
  1861. {
  1862. FILE *in = stdin;
  1863. char fname[PATH_MAX];
  1864. if (strcmp(filename, "-") != 0) {
  1865. in = fopen(filename, "r");
  1866. if (!in)
  1867. fatal("Can't open filenames file %s: %m", filename);
  1868. }
  1869. while (fgets(fname, PATH_MAX, in) != NULL) {
  1870. if (strends(fname, "\n"))
  1871. fname[strlen(fname)-1] = '\0';
  1872. read_symbols(fname);
  1873. }
  1874. if (in != stdin)
  1875. fclose(in);
  1876. }
  1877. #define SZ 500
  1878. /* We first write the generated file into memory using the
  1879. * following helper, then compare to the file on disk and
  1880. * only update the later if anything changed */
  1881. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1882. const char *fmt, ...)
  1883. {
  1884. char tmp[SZ];
  1885. int len;
  1886. va_list ap;
  1887. va_start(ap, fmt);
  1888. len = vsnprintf(tmp, SZ, fmt, ap);
  1889. buf_write(buf, tmp, len);
  1890. va_end(ap);
  1891. }
  1892. void buf_write(struct buffer *buf, const char *s, int len)
  1893. {
  1894. if (buf->size - buf->pos < len) {
  1895. buf->size += len + SZ;
  1896. buf->p = NOFAIL(realloc(buf->p, buf->size));
  1897. }
  1898. strncpy(buf->p + buf->pos, s, len);
  1899. buf->pos += len;
  1900. }
  1901. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1902. {
  1903. const char *e = is_vmlinux(m) ?"":".ko";
  1904. switch (exp) {
  1905. case export_gpl:
  1906. fatal("GPL-incompatible module %s%s "
  1907. "uses GPL-only symbol '%s'\n", m, e, s);
  1908. break;
  1909. case export_unused_gpl:
  1910. fatal("GPL-incompatible module %s%s "
  1911. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1912. break;
  1913. case export_gpl_future:
  1914. warn("GPL-incompatible module %s%s "
  1915. "uses future GPL-only symbol '%s'\n", m, e, s);
  1916. break;
  1917. case export_plain:
  1918. case export_unused:
  1919. case export_unknown:
  1920. /* ignore */
  1921. break;
  1922. }
  1923. }
  1924. static void check_for_unused(enum export exp, const char *m, const char *s)
  1925. {
  1926. const char *e = is_vmlinux(m) ?"":".ko";
  1927. switch (exp) {
  1928. case export_unused:
  1929. case export_unused_gpl:
  1930. warn("module %s%s "
  1931. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1932. break;
  1933. default:
  1934. /* ignore */
  1935. break;
  1936. }
  1937. }
  1938. static int check_exports(struct module *mod)
  1939. {
  1940. struct symbol *s, *exp;
  1941. int err = 0;
  1942. for (s = mod->unres; s; s = s->next) {
  1943. const char *basename;
  1944. exp = find_symbol(s->name);
  1945. if (!exp || exp->module == mod) {
  1946. if (have_vmlinux && !s->weak) {
  1947. modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
  1948. "\"%s\" [%s.ko] undefined!\n",
  1949. s->name, mod->name);
  1950. if (!warn_unresolved)
  1951. err = 1;
  1952. }
  1953. continue;
  1954. }
  1955. basename = strrchr(mod->name, '/');
  1956. if (basename)
  1957. basename++;
  1958. else
  1959. basename = mod->name;
  1960. if (exp->namespace &&
  1961. !module_imports_namespace(mod, exp->namespace)) {
  1962. modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
  1963. "module %s uses symbol %s from namespace %s, but does not import it.\n",
  1964. basename, exp->name, exp->namespace);
  1965. if (!allow_missing_ns_imports)
  1966. err = 1;
  1967. add_namespace(&mod->missing_namespaces, exp->namespace);
  1968. }
  1969. if (!mod->gpl_compatible)
  1970. check_for_gpl_usage(exp->export, basename, exp->name);
  1971. check_for_unused(exp->export, basename, exp->name);
  1972. }
  1973. return err;
  1974. }
  1975. static int check_modname_len(struct module *mod)
  1976. {
  1977. const char *mod_name;
  1978. mod_name = strrchr(mod->name, '/');
  1979. if (mod_name == NULL)
  1980. mod_name = mod->name;
  1981. else
  1982. mod_name++;
  1983. if (strlen(mod_name) >= MODULE_NAME_LEN) {
  1984. merror("module name is too long [%s.ko]\n", mod->name);
  1985. return 1;
  1986. }
  1987. return 0;
  1988. }
  1989. /**
  1990. * Header for the generated file
  1991. **/
  1992. static void add_header(struct buffer *b, struct module *mod)
  1993. {
  1994. buf_printf(b, "#include <linux/module.h>\n");
  1995. /*
  1996. * Include build-salt.h after module.h in order to
  1997. * inherit the definitions.
  1998. */
  1999. buf_printf(b, "#include <linux/build-salt.h>\n");
  2000. buf_printf(b, "#include <linux/vermagic.h>\n");
  2001. buf_printf(b, "#include <linux/compiler.h>\n");
  2002. buf_printf(b, "\n");
  2003. buf_printf(b, "BUILD_SALT;\n");
  2004. buf_printf(b, "\n");
  2005. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  2006. buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
  2007. buf_printf(b, "\n");
  2008. buf_printf(b, "__visible struct module __this_module\n");
  2009. buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
  2010. buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
  2011. if (mod->has_init)
  2012. buf_printf(b, "\t.init = init_module,\n");
  2013. if (mod->has_cleanup)
  2014. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  2015. "\t.exit = cleanup_module,\n"
  2016. "#endif\n");
  2017. buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
  2018. buf_printf(b, "};\n");
  2019. }
  2020. static void add_intree_flag(struct buffer *b, int is_intree)
  2021. {
  2022. if (is_intree)
  2023. buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
  2024. }
  2025. /* Cannot check for assembler */
  2026. static void add_retpoline(struct buffer *b)
  2027. {
  2028. buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
  2029. buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
  2030. buf_printf(b, "#endif\n");
  2031. }
  2032. static void add_staging_flag(struct buffer *b, const char *name)
  2033. {
  2034. if (strstarts(name, "drivers/staging"))
  2035. buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
  2036. }
  2037. /**
  2038. * Record CRCs for unresolved symbols
  2039. **/
  2040. static int add_versions(struct buffer *b, struct module *mod)
  2041. {
  2042. struct symbol *s, *exp;
  2043. int err = 0;
  2044. for (s = mod->unres; s; s = s->next) {
  2045. exp = find_symbol(s->name);
  2046. if (!exp || exp->module == mod)
  2047. continue;
  2048. s->module = exp->module;
  2049. s->crc_valid = exp->crc_valid;
  2050. s->crc = exp->crc;
  2051. }
  2052. if (!modversions)
  2053. return err;
  2054. buf_printf(b, "\n");
  2055. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  2056. buf_printf(b, "__used __section(__versions) = {\n");
  2057. for (s = mod->unres; s; s = s->next) {
  2058. if (!s->module)
  2059. continue;
  2060. if (!s->crc_valid) {
  2061. warn("\"%s\" [%s.ko] has no CRC!\n",
  2062. s->name, mod->name);
  2063. continue;
  2064. }
  2065. if (strlen(s->name) >= MODULE_NAME_LEN) {
  2066. merror("too long symbol \"%s\" [%s.ko]\n",
  2067. s->name, mod->name);
  2068. err = 1;
  2069. break;
  2070. }
  2071. buf_printf(b, "\t{ %#8x, \"%s\" },\n",
  2072. s->crc, s->name);
  2073. }
  2074. buf_printf(b, "};\n");
  2075. return err;
  2076. }
  2077. static void add_depends(struct buffer *b, struct module *mod)
  2078. {
  2079. struct symbol *s;
  2080. int first = 1;
  2081. /* Clear ->seen flag of modules that own symbols needed by this. */
  2082. for (s = mod->unres; s; s = s->next)
  2083. if (s->module)
  2084. s->module->seen = is_vmlinux(s->module->name);
  2085. buf_printf(b, "\n");
  2086. buf_printf(b, "MODULE_INFO(depends, \"");
  2087. for (s = mod->unres; s; s = s->next) {
  2088. const char *p;
  2089. if (!s->module)
  2090. continue;
  2091. if (s->module->seen)
  2092. continue;
  2093. s->module->seen = 1;
  2094. p = strrchr(s->module->name, '/');
  2095. if (p)
  2096. p++;
  2097. else
  2098. p = s->module->name;
  2099. buf_printf(b, "%s%s", first ? "" : ",", p);
  2100. first = 0;
  2101. }
  2102. buf_printf(b, "\");\n");
  2103. }
  2104. static void add_srcversion(struct buffer *b, struct module *mod)
  2105. {
  2106. if (mod->srcversion[0]) {
  2107. buf_printf(b, "\n");
  2108. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  2109. mod->srcversion);
  2110. }
  2111. }
  2112. static void write_if_changed(struct buffer *b, const char *fname)
  2113. {
  2114. char *tmp;
  2115. FILE *file;
  2116. struct stat st;
  2117. file = fopen(fname, "r");
  2118. if (!file)
  2119. goto write;
  2120. if (fstat(fileno(file), &st) < 0)
  2121. goto close_write;
  2122. if (st.st_size != b->pos)
  2123. goto close_write;
  2124. tmp = NOFAIL(malloc(b->pos));
  2125. if (fread(tmp, 1, b->pos, file) != b->pos)
  2126. goto free_write;
  2127. if (memcmp(tmp, b->p, b->pos) != 0)
  2128. goto free_write;
  2129. free(tmp);
  2130. fclose(file);
  2131. return;
  2132. free_write:
  2133. free(tmp);
  2134. close_write:
  2135. fclose(file);
  2136. write:
  2137. file = fopen(fname, "w");
  2138. if (!file) {
  2139. perror(fname);
  2140. exit(1);
  2141. }
  2142. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  2143. perror(fname);
  2144. exit(1);
  2145. }
  2146. fclose(file);
  2147. }
  2148. /* parse Module.symvers file. line format:
  2149. * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
  2150. **/
  2151. static void read_dump(const char *fname, unsigned int kernel)
  2152. {
  2153. unsigned long size, pos = 0;
  2154. void *file = grab_file(fname, &size);
  2155. char *line;
  2156. if (!file)
  2157. /* No symbol versions, silently ignore */
  2158. return;
  2159. while ((line = get_next_line(&pos, file, size))) {
  2160. char *symname, *namespace, *modname, *d, *export;
  2161. unsigned int crc;
  2162. struct module *mod;
  2163. struct symbol *s;
  2164. if (!(symname = strchr(line, '\t')))
  2165. goto fail;
  2166. *symname++ = '\0';
  2167. if (!(modname = strchr(symname, '\t')))
  2168. goto fail;
  2169. *modname++ = '\0';
  2170. if (!(export = strchr(modname, '\t')))
  2171. goto fail;
  2172. *export++ = '\0';
  2173. if (!(namespace = strchr(export, '\t')))
  2174. goto fail;
  2175. *namespace++ = '\0';
  2176. crc = strtoul(line, &d, 16);
  2177. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  2178. goto fail;
  2179. mod = find_module(modname);
  2180. if (!mod) {
  2181. if (is_vmlinux(modname))
  2182. have_vmlinux = 1;
  2183. mod = new_module(modname);
  2184. mod->skip = 1;
  2185. }
  2186. s = sym_add_exported(symname, mod, export_no(export));
  2187. s->kernel = kernel;
  2188. s->is_static = 0;
  2189. sym_set_crc(symname, crc);
  2190. sym_update_namespace(symname, namespace);
  2191. }
  2192. release_file(file, size);
  2193. return;
  2194. fail:
  2195. release_file(file, size);
  2196. fatal("parse error in symbol dump file\n");
  2197. }
  2198. /* For normal builds always dump all symbols.
  2199. * For external modules only dump symbols
  2200. * that are not read from kernel Module.symvers.
  2201. **/
  2202. static int dump_sym(struct symbol *sym)
  2203. {
  2204. if (!external_module)
  2205. return 1;
  2206. if (sym->vmlinux || sym->kernel)
  2207. return 0;
  2208. return 1;
  2209. }
  2210. static void write_dump(const char *fname)
  2211. {
  2212. struct buffer buf = { };
  2213. struct symbol *symbol;
  2214. const char *namespace;
  2215. int n;
  2216. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  2217. symbol = symbolhash[n];
  2218. while (symbol) {
  2219. if (dump_sym(symbol)) {
  2220. namespace = symbol->namespace;
  2221. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
  2222. symbol->crc, symbol->name,
  2223. symbol->module->name,
  2224. export_str(symbol->export),
  2225. namespace ? namespace : "");
  2226. }
  2227. symbol = symbol->next;
  2228. }
  2229. }
  2230. write_if_changed(&buf, fname);
  2231. free(buf.p);
  2232. }
  2233. static void write_namespace_deps_files(const char *fname)
  2234. {
  2235. struct module *mod;
  2236. struct namespace_list *ns;
  2237. struct buffer ns_deps_buf = {};
  2238. for (mod = modules; mod; mod = mod->next) {
  2239. if (mod->skip || !mod->missing_namespaces)
  2240. continue;
  2241. buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
  2242. for (ns = mod->missing_namespaces; ns; ns = ns->next)
  2243. buf_printf(&ns_deps_buf, " %s", ns->namespace);
  2244. buf_printf(&ns_deps_buf, "\n");
  2245. }
  2246. write_if_changed(&ns_deps_buf, fname);
  2247. free(ns_deps_buf.p);
  2248. }
  2249. struct ext_sym_list {
  2250. struct ext_sym_list *next;
  2251. const char *file;
  2252. };
  2253. int main(int argc, char **argv)
  2254. {
  2255. struct module *mod;
  2256. struct buffer buf = { };
  2257. char *kernel_read = NULL;
  2258. char *missing_namespace_deps = NULL;
  2259. char *dump_write = NULL, *files_source = NULL;
  2260. int opt;
  2261. int err;
  2262. int n;
  2263. struct ext_sym_list *extsym_iter;
  2264. struct ext_sym_list *extsym_start = NULL;
  2265. while ((opt = getopt(argc, argv, "i:e:mnsT:o:awENd:")) != -1) {
  2266. switch (opt) {
  2267. case 'i':
  2268. kernel_read = optarg;
  2269. external_module = 1;
  2270. break;
  2271. case 'e':
  2272. external_module = 1;
  2273. extsym_iter =
  2274. NOFAIL(malloc(sizeof(*extsym_iter)));
  2275. extsym_iter->next = extsym_start;
  2276. extsym_iter->file = optarg;
  2277. extsym_start = extsym_iter;
  2278. break;
  2279. case 'm':
  2280. modversions = 1;
  2281. break;
  2282. case 'n':
  2283. ignore_missing_files = 1;
  2284. break;
  2285. case 'o':
  2286. dump_write = optarg;
  2287. break;
  2288. case 'a':
  2289. all_versions = 1;
  2290. break;
  2291. case 's':
  2292. vmlinux_section_warnings = 0;
  2293. break;
  2294. case 'T':
  2295. files_source = optarg;
  2296. break;
  2297. case 'w':
  2298. warn_unresolved = 1;
  2299. break;
  2300. case 'E':
  2301. sec_mismatch_fatal = 1;
  2302. break;
  2303. case 'N':
  2304. allow_missing_ns_imports = 1;
  2305. break;
  2306. case 'd':
  2307. missing_namespace_deps = optarg;
  2308. break;
  2309. default:
  2310. exit(1);
  2311. }
  2312. }
  2313. if (kernel_read)
  2314. read_dump(kernel_read, 1);
  2315. while (extsym_start) {
  2316. read_dump(extsym_start->file, 0);
  2317. extsym_iter = extsym_start->next;
  2318. free(extsym_start);
  2319. extsym_start = extsym_iter;
  2320. }
  2321. while (optind < argc)
  2322. read_symbols(argv[optind++]);
  2323. if (files_source)
  2324. read_symbols_from_files(files_source);
  2325. err = 0;
  2326. for (mod = modules; mod; mod = mod->next) {
  2327. char fname[PATH_MAX];
  2328. if (mod->skip)
  2329. continue;
  2330. buf.pos = 0;
  2331. err |= check_modname_len(mod);
  2332. err |= check_exports(mod);
  2333. add_header(&buf, mod);
  2334. add_intree_flag(&buf, !external_module);
  2335. add_retpoline(&buf);
  2336. add_staging_flag(&buf, mod->name);
  2337. err |= add_versions(&buf, mod);
  2338. add_depends(&buf, mod);
  2339. add_moddevtable(&buf, mod);
  2340. add_srcversion(&buf, mod);
  2341. sprintf(fname, "%s.mod.c", mod->name);
  2342. write_if_changed(&buf, fname);
  2343. }
  2344. if (missing_namespace_deps)
  2345. write_namespace_deps_files(missing_namespace_deps);
  2346. if (dump_write)
  2347. write_dump(dump_write);
  2348. if (sec_mismatch_count && sec_mismatch_fatal)
  2349. fatal("Section mismatches detected.\n"
  2350. "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
  2351. for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
  2352. struct symbol *s;
  2353. for (s = symbolhash[n]; s; s = s->next) {
  2354. /*
  2355. * Do not check "vmlinux". This avoids the same warnings
  2356. * shown twice, and false-positives for ARCH=um.
  2357. */
  2358. if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
  2359. continue;
  2360. if (s->is_static)
  2361. warn("\"%s\" [%s] is a static %s\n",
  2362. s->name, s->module->name,
  2363. export_str(s->export));
  2364. }
  2365. }
  2366. free(buf.p);
  2367. return err;
  2368. }