modpost.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. *
  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. #include <ctype.h>
  14. #include "modpost.h"
  15. /* Are we using CONFIG_MODVERSIONS? */
  16. int modversions = 0;
  17. /* Warn about undefined symbols? (do so if we have vmlinux) */
  18. int have_vmlinux = 0;
  19. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  20. static int all_versions = 0;
  21. void
  22. fatal(const char *fmt, ...)
  23. {
  24. va_list arglist;
  25. fprintf(stderr, "FATAL: ");
  26. va_start(arglist, fmt);
  27. vfprintf(stderr, fmt, arglist);
  28. va_end(arglist);
  29. exit(1);
  30. }
  31. void
  32. warn(const char *fmt, ...)
  33. {
  34. va_list arglist;
  35. fprintf(stderr, "WARNING: ");
  36. va_start(arglist, fmt);
  37. vfprintf(stderr, fmt, arglist);
  38. va_end(arglist);
  39. }
  40. void *do_nofail(void *ptr, const char *expr)
  41. {
  42. if (!ptr) {
  43. fatal("modpost: Memory allocation failure: %s.\n", expr);
  44. }
  45. return ptr;
  46. }
  47. /* A list of all modules we processed */
  48. static struct module *modules;
  49. struct module *
  50. find_module(char *modname)
  51. {
  52. struct module *mod;
  53. for (mod = modules; mod; mod = mod->next)
  54. if (strcmp(mod->name, modname) == 0)
  55. break;
  56. return mod;
  57. }
  58. struct module *
  59. new_module(char *modname)
  60. {
  61. struct module *mod;
  62. char *p, *s;
  63. mod = NOFAIL(malloc(sizeof(*mod)));
  64. memset(mod, 0, sizeof(*mod));
  65. p = NOFAIL(strdup(modname));
  66. /* strip trailing .o */
  67. if ((s = strrchr(p, '.')) != NULL)
  68. if (strcmp(s, ".o") == 0)
  69. *s = '\0';
  70. /* add to list */
  71. mod->name = p;
  72. mod->next = modules;
  73. modules = mod;
  74. return mod;
  75. }
  76. /* A hash of all exported symbols,
  77. * struct symbol is also used for lists of unresolved symbols */
  78. #define SYMBOL_HASH_SIZE 1024
  79. struct symbol {
  80. struct symbol *next;
  81. struct module *module;
  82. unsigned int crc;
  83. int crc_valid;
  84. unsigned int weak:1;
  85. char name[0];
  86. };
  87. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  88. /* This is based on the hash agorithm from gdbm, via tdb */
  89. static inline unsigned int tdb_hash(const char *name)
  90. {
  91. unsigned value; /* Used to compute the hash value. */
  92. unsigned i; /* Used to cycle through random values. */
  93. /* Set the initial value from the key size. */
  94. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  95. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  96. return (1103515243 * value + 12345);
  97. }
  98. /* Allocate a new symbols for use in the hash of exported symbols or
  99. * the list of unresolved symbols per module */
  100. struct symbol *
  101. alloc_symbol(const char *name, unsigned int weak, struct symbol *next)
  102. {
  103. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  104. memset(s, 0, sizeof(*s));
  105. strcpy(s->name, name);
  106. s->weak = weak;
  107. s->next = next;
  108. return s;
  109. }
  110. /* For the hash of exported symbols */
  111. void
  112. new_symbol(const char *name, struct module *module, unsigned int *crc)
  113. {
  114. unsigned int hash;
  115. struct symbol *new;
  116. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  117. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  118. new->module = module;
  119. if (crc) {
  120. new->crc = *crc;
  121. new->crc_valid = 1;
  122. }
  123. }
  124. struct symbol *
  125. find_symbol(const char *name)
  126. {
  127. struct symbol *s;
  128. /* For our purposes, .foo matches foo. PPC64 needs this. */
  129. if (name[0] == '.')
  130. name++;
  131. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  132. if (strcmp(s->name, name) == 0)
  133. return s;
  134. }
  135. return NULL;
  136. }
  137. /* Add an exported symbol - it may have already been added without a
  138. * CRC, in this case just update the CRC */
  139. void
  140. add_exported_symbol(const char *name, struct module *module, unsigned int *crc)
  141. {
  142. struct symbol *s = find_symbol(name);
  143. if (!s) {
  144. new_symbol(name, module, crc);
  145. return;
  146. }
  147. if (crc) {
  148. s->crc = *crc;
  149. s->crc_valid = 1;
  150. }
  151. }
  152. void *
  153. grab_file(const char *filename, unsigned long *size)
  154. {
  155. struct stat st;
  156. void *map;
  157. int fd;
  158. fd = open(filename, O_RDONLY);
  159. if (fd < 0 || fstat(fd, &st) != 0)
  160. return NULL;
  161. *size = st.st_size;
  162. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  163. close(fd);
  164. if (map == MAP_FAILED)
  165. return NULL;
  166. return map;
  167. }
  168. /*
  169. Return a copy of the next line in a mmap'ed file.
  170. spaces in the beginning of the line is trimmed away.
  171. Return a pointer to a static buffer.
  172. */
  173. char*
  174. get_next_line(unsigned long *pos, void *file, unsigned long size)
  175. {
  176. static char line[4096];
  177. int skip = 1;
  178. size_t len = 0;
  179. signed char *p = (signed char *)file + *pos;
  180. char *s = line;
  181. for (; *pos < size ; (*pos)++)
  182. {
  183. if (skip && isspace(*p)) {
  184. p++;
  185. continue;
  186. }
  187. skip = 0;
  188. if (*p != '\n' && (*pos < size)) {
  189. len++;
  190. *s++ = *p++;
  191. if (len > 4095)
  192. break; /* Too long, stop */
  193. } else {
  194. /* End of string */
  195. *s = '\0';
  196. return line;
  197. }
  198. }
  199. /* End of buffer */
  200. return NULL;
  201. }
  202. void
  203. release_file(void *file, unsigned long size)
  204. {
  205. munmap(file, size);
  206. }
  207. void
  208. parse_elf(struct elf_info *info, const char *filename)
  209. {
  210. unsigned int i;
  211. Elf_Ehdr *hdr = info->hdr;
  212. Elf_Shdr *sechdrs;
  213. Elf_Sym *sym;
  214. hdr = grab_file(filename, &info->size);
  215. if (!hdr) {
  216. perror(filename);
  217. abort();
  218. }
  219. info->hdr = hdr;
  220. if (info->size < sizeof(*hdr))
  221. goto truncated;
  222. /* Fix endianness in ELF header */
  223. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  224. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  225. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  226. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  227. sechdrs = (void *)hdr + hdr->e_shoff;
  228. info->sechdrs = sechdrs;
  229. /* Fix endianness in section headers */
  230. for (i = 0; i < hdr->e_shnum; i++) {
  231. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  232. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  233. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  234. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  235. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  236. }
  237. /* Find symbol table. */
  238. for (i = 1; i < hdr->e_shnum; i++) {
  239. const char *secstrings
  240. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  241. if (sechdrs[i].sh_offset > info->size)
  242. goto truncated;
  243. if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
  244. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  245. info->modinfo_len = sechdrs[i].sh_size;
  246. }
  247. if (sechdrs[i].sh_type != SHT_SYMTAB)
  248. continue;
  249. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  250. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  251. + sechdrs[i].sh_size;
  252. info->strtab = (void *)hdr +
  253. sechdrs[sechdrs[i].sh_link].sh_offset;
  254. }
  255. if (!info->symtab_start) {
  256. fprintf(stderr, "modpost: %s no symtab?\n", filename);
  257. abort();
  258. }
  259. /* Fix endianness in symbols */
  260. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  261. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  262. sym->st_name = TO_NATIVE(sym->st_name);
  263. sym->st_value = TO_NATIVE(sym->st_value);
  264. sym->st_size = TO_NATIVE(sym->st_size);
  265. }
  266. return;
  267. truncated:
  268. fprintf(stderr, "modpost: %s is truncated.\n", filename);
  269. abort();
  270. }
  271. void
  272. parse_elf_finish(struct elf_info *info)
  273. {
  274. release_file(info->hdr, info->size);
  275. }
  276. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  277. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  278. void
  279. handle_modversions(struct module *mod, struct elf_info *info,
  280. Elf_Sym *sym, const char *symname)
  281. {
  282. unsigned int crc;
  283. switch (sym->st_shndx) {
  284. case SHN_COMMON:
  285. fprintf(stderr, "*** Warning: \"%s\" [%s] is COMMON symbol\n",
  286. symname, mod->name);
  287. break;
  288. case SHN_ABS:
  289. /* CRC'd symbol */
  290. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  291. crc = (unsigned int) sym->st_value;
  292. add_exported_symbol(symname + strlen(CRC_PFX),
  293. mod, &crc);
  294. }
  295. break;
  296. case SHN_UNDEF:
  297. /* undefined symbol */
  298. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  299. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  300. break;
  301. /* ignore global offset table */
  302. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  303. break;
  304. /* ignore __this_module, it will be resolved shortly */
  305. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  306. break;
  307. #ifdef STT_REGISTER
  308. if (info->hdr->e_machine == EM_SPARC ||
  309. info->hdr->e_machine == EM_SPARCV9) {
  310. /* Ignore register directives. */
  311. if (ELF_ST_TYPE(sym->st_info) == STT_REGISTER)
  312. break;
  313. }
  314. #endif
  315. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  316. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  317. mod->unres = alloc_symbol(symname +
  318. strlen(MODULE_SYMBOL_PREFIX),
  319. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  320. mod->unres);
  321. break;
  322. default:
  323. /* All exported symbols */
  324. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  325. add_exported_symbol(symname + strlen(KSYMTAB_PFX),
  326. mod, NULL);
  327. }
  328. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  329. mod->has_init = 1;
  330. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  331. mod->has_cleanup = 1;
  332. break;
  333. }
  334. }
  335. int
  336. is_vmlinux(const char *modname)
  337. {
  338. const char *myname;
  339. if ((myname = strrchr(modname, '/')))
  340. myname++;
  341. else
  342. myname = modname;
  343. return strcmp(myname, "vmlinux") == 0;
  344. }
  345. /* Parse tag=value strings from .modinfo section */
  346. static char *next_string(char *string, unsigned long *secsize)
  347. {
  348. /* Skip non-zero chars */
  349. while (string[0]) {
  350. string++;
  351. if ((*secsize)-- <= 1)
  352. return NULL;
  353. }
  354. /* Skip any zero padding. */
  355. while (!string[0]) {
  356. string++;
  357. if ((*secsize)-- <= 1)
  358. return NULL;
  359. }
  360. return string;
  361. }
  362. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  363. const char *tag)
  364. {
  365. char *p;
  366. unsigned int taglen = strlen(tag);
  367. unsigned long size = modinfo_len;
  368. for (p = modinfo; p; p = next_string(p, &size)) {
  369. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  370. return p + taglen + 1;
  371. }
  372. return NULL;
  373. }
  374. void
  375. read_symbols(char *modname)
  376. {
  377. const char *symname;
  378. char *version;
  379. struct module *mod;
  380. struct elf_info info = { };
  381. Elf_Sym *sym;
  382. parse_elf(&info, modname);
  383. mod = new_module(modname);
  384. /* When there's no vmlinux, don't print warnings about
  385. * unresolved symbols (since there'll be too many ;) */
  386. if (is_vmlinux(modname)) {
  387. unsigned int fake_crc = 0;
  388. have_vmlinux = 1;
  389. add_exported_symbol("struct_module", mod, &fake_crc);
  390. mod->skip = 1;
  391. }
  392. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  393. symname = info.strtab + sym->st_name;
  394. handle_modversions(mod, &info, sym, symname);
  395. handle_moddevtable(mod, &info, sym, symname);
  396. }
  397. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  398. if (version)
  399. maybe_frob_rcs_version(modname, version, info.modinfo,
  400. version - (char *)info.hdr);
  401. if (version || (all_versions && !is_vmlinux(modname)))
  402. get_src_version(modname, mod->srcversion,
  403. sizeof(mod->srcversion)-1);
  404. parse_elf_finish(&info);
  405. /* Our trick to get versioning for struct_module - it's
  406. * never passed as an argument to an exported function, so
  407. * the automatic versioning doesn't pick it up, but it's really
  408. * important anyhow */
  409. if (modversions)
  410. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  411. }
  412. #define SZ 500
  413. /* We first write the generated file into memory using the
  414. * following helper, then compare to the file on disk and
  415. * only update the later if anything changed */
  416. void __attribute__((format(printf, 2, 3)))
  417. buf_printf(struct buffer *buf, const char *fmt, ...)
  418. {
  419. char tmp[SZ];
  420. int len;
  421. va_list ap;
  422. va_start(ap, fmt);
  423. len = vsnprintf(tmp, SZ, fmt, ap);
  424. if (buf->size - buf->pos < len + 1) {
  425. buf->size += 128;
  426. buf->p = realloc(buf->p, buf->size);
  427. }
  428. strncpy(buf->p + buf->pos, tmp, len + 1);
  429. buf->pos += len;
  430. va_end(ap);
  431. }
  432. void
  433. buf_write(struct buffer *buf, const char *s, int len)
  434. {
  435. if (buf->size - buf->pos < len) {
  436. buf->size += len;
  437. buf->p = realloc(buf->p, buf->size);
  438. }
  439. strncpy(buf->p + buf->pos, s, len);
  440. buf->pos += len;
  441. }
  442. /* Header for the generated file */
  443. void
  444. add_header(struct buffer *b, struct module *mod)
  445. {
  446. buf_printf(b, "#include <linux/module.h>\n");
  447. buf_printf(b, "#include <linux/vermagic.h>\n");
  448. buf_printf(b, "#include <linux/compiler.h>\n");
  449. buf_printf(b, "\n");
  450. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  451. buf_printf(b, "\n");
  452. buf_printf(b, "#undef unix\n"); /* We have a module called "unix" */
  453. buf_printf(b, "struct module __this_module\n");
  454. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  455. buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
  456. if (mod->has_init)
  457. buf_printf(b, " .init = init_module,\n");
  458. if (mod->has_cleanup)
  459. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  460. " .exit = cleanup_module,\n"
  461. "#endif\n");
  462. buf_printf(b, "};\n");
  463. }
  464. /* Record CRCs for unresolved symbols */
  465. void
  466. add_versions(struct buffer *b, struct module *mod)
  467. {
  468. struct symbol *s, *exp;
  469. for (s = mod->unres; s; s = s->next) {
  470. exp = find_symbol(s->name);
  471. if (!exp || exp->module == mod) {
  472. if (have_vmlinux && !s->weak)
  473. fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
  474. "undefined!\n", s->name, mod->name);
  475. continue;
  476. }
  477. s->module = exp->module;
  478. s->crc_valid = exp->crc_valid;
  479. s->crc = exp->crc;
  480. }
  481. if (!modversions)
  482. return;
  483. buf_printf(b, "\n");
  484. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  485. buf_printf(b, "__attribute_used__\n");
  486. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  487. for (s = mod->unres; s; s = s->next) {
  488. if (!s->module) {
  489. continue;
  490. }
  491. if (!s->crc_valid) {
  492. fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
  493. "has no CRC!\n",
  494. s->name, mod->name);
  495. continue;
  496. }
  497. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  498. }
  499. buf_printf(b, "};\n");
  500. }
  501. void
  502. add_depends(struct buffer *b, struct module *mod, struct module *modules)
  503. {
  504. struct symbol *s;
  505. struct module *m;
  506. int first = 1;
  507. for (m = modules; m; m = m->next) {
  508. m->seen = is_vmlinux(m->name);
  509. }
  510. buf_printf(b, "\n");
  511. buf_printf(b, "static const char __module_depends[]\n");
  512. buf_printf(b, "__attribute_used__\n");
  513. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  514. buf_printf(b, "\"depends=");
  515. for (s = mod->unres; s; s = s->next) {
  516. if (!s->module)
  517. continue;
  518. if (s->module->seen)
  519. continue;
  520. s->module->seen = 1;
  521. buf_printf(b, "%s%s", first ? "" : ",",
  522. strrchr(s->module->name, '/') + 1);
  523. first = 0;
  524. }
  525. buf_printf(b, "\";\n");
  526. }
  527. void
  528. add_srcversion(struct buffer *b, struct module *mod)
  529. {
  530. if (mod->srcversion[0]) {
  531. buf_printf(b, "\n");
  532. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  533. mod->srcversion);
  534. }
  535. }
  536. void
  537. write_if_changed(struct buffer *b, const char *fname)
  538. {
  539. char *tmp;
  540. FILE *file;
  541. struct stat st;
  542. file = fopen(fname, "r");
  543. if (!file)
  544. goto write;
  545. if (fstat(fileno(file), &st) < 0)
  546. goto close_write;
  547. if (st.st_size != b->pos)
  548. goto close_write;
  549. tmp = NOFAIL(malloc(b->pos));
  550. if (fread(tmp, 1, b->pos, file) != b->pos)
  551. goto free_write;
  552. if (memcmp(tmp, b->p, b->pos) != 0)
  553. goto free_write;
  554. free(tmp);
  555. fclose(file);
  556. return;
  557. free_write:
  558. free(tmp);
  559. close_write:
  560. fclose(file);
  561. write:
  562. file = fopen(fname, "w");
  563. if (!file) {
  564. perror(fname);
  565. exit(1);
  566. }
  567. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  568. perror(fname);
  569. exit(1);
  570. }
  571. fclose(file);
  572. }
  573. void
  574. read_dump(const char *fname)
  575. {
  576. unsigned long size, pos = 0;
  577. void *file = grab_file(fname, &size);
  578. char *line;
  579. if (!file)
  580. /* No symbol versions, silently ignore */
  581. return;
  582. while ((line = get_next_line(&pos, file, size))) {
  583. char *symname, *modname, *d;
  584. unsigned int crc;
  585. struct module *mod;
  586. if (!(symname = strchr(line, '\t')))
  587. goto fail;
  588. *symname++ = '\0';
  589. if (!(modname = strchr(symname, '\t')))
  590. goto fail;
  591. *modname++ = '\0';
  592. if (strchr(modname, '\t'))
  593. goto fail;
  594. crc = strtoul(line, &d, 16);
  595. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  596. goto fail;
  597. if (!(mod = find_module(modname))) {
  598. if (is_vmlinux(modname)) {
  599. have_vmlinux = 1;
  600. }
  601. mod = new_module(NOFAIL(strdup(modname)));
  602. mod->skip = 1;
  603. }
  604. add_exported_symbol(symname, mod, &crc);
  605. }
  606. return;
  607. fail:
  608. fatal("parse error in symbol dump file\n");
  609. }
  610. void
  611. write_dump(const char *fname)
  612. {
  613. struct buffer buf = { };
  614. struct symbol *symbol;
  615. int n;
  616. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  617. symbol = symbolhash[n];
  618. while (symbol) {
  619. symbol = symbol->next;
  620. }
  621. }
  622. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  623. symbol = symbolhash[n];
  624. while (symbol) {
  625. buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
  626. symbol->name, symbol->module->name);
  627. symbol = symbol->next;
  628. }
  629. }
  630. write_if_changed(&buf, fname);
  631. }
  632. int
  633. main(int argc, char **argv)
  634. {
  635. struct module *mod;
  636. struct buffer buf = { };
  637. char fname[SZ];
  638. char *dump_read = NULL, *dump_write = NULL;
  639. int opt;
  640. while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
  641. switch(opt) {
  642. case 'i':
  643. dump_read = optarg;
  644. break;
  645. case 'm':
  646. modversions = 1;
  647. break;
  648. case 'o':
  649. dump_write = optarg;
  650. break;
  651. case 'a':
  652. all_versions = 1;
  653. break;
  654. default:
  655. exit(1);
  656. }
  657. }
  658. if (dump_read)
  659. read_dump(dump_read);
  660. while (optind < argc) {
  661. read_symbols(argv[optind++]);
  662. }
  663. for (mod = modules; mod; mod = mod->next) {
  664. if (mod->skip)
  665. continue;
  666. buf.pos = 0;
  667. add_header(&buf, mod);
  668. add_versions(&buf, mod);
  669. add_depends(&buf, mod, modules);
  670. add_moddevtable(&buf, mod);
  671. add_srcversion(&buf, mod);
  672. sprintf(fname, "%s.mod.c", mod->name);
  673. write_if_changed(&buf, fname);
  674. }
  675. if (dump_write)
  676. write_dump(dump_write);
  677. return 0;
  678. }