modpost.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 "__crc_"
  277. #define KSYMTAB_PFX "__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. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  308. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  309. /* add compatibility with older glibc */
  310. #ifndef STT_SPARC_REGISTER
  311. #define STT_SPARC_REGISTER STT_REGISTER
  312. #endif
  313. if (info->hdr->e_machine == EM_SPARC ||
  314. info->hdr->e_machine == EM_SPARCV9) {
  315. /* Ignore register directives. */
  316. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  317. break;
  318. if (symname[0] == '.') {
  319. char *munged = strdup(symname);
  320. munged[0] = '_';
  321. munged[1] = toupper(munged[1]);
  322. symname = munged;
  323. }
  324. }
  325. #endif
  326. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  327. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  328. mod->unres = alloc_symbol(symname +
  329. strlen(MODULE_SYMBOL_PREFIX),
  330. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  331. mod->unres);
  332. break;
  333. default:
  334. /* All exported symbols */
  335. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  336. add_exported_symbol(symname + strlen(KSYMTAB_PFX),
  337. mod, NULL);
  338. }
  339. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  340. mod->has_init = 1;
  341. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  342. mod->has_cleanup = 1;
  343. break;
  344. }
  345. }
  346. int
  347. is_vmlinux(const char *modname)
  348. {
  349. const char *myname;
  350. if ((myname = strrchr(modname, '/')))
  351. myname++;
  352. else
  353. myname = modname;
  354. return strcmp(myname, "vmlinux") == 0;
  355. }
  356. /* Parse tag=value strings from .modinfo section */
  357. static char *next_string(char *string, unsigned long *secsize)
  358. {
  359. /* Skip non-zero chars */
  360. while (string[0]) {
  361. string++;
  362. if ((*secsize)-- <= 1)
  363. return NULL;
  364. }
  365. /* Skip any zero padding. */
  366. while (!string[0]) {
  367. string++;
  368. if ((*secsize)-- <= 1)
  369. return NULL;
  370. }
  371. return string;
  372. }
  373. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  374. const char *tag)
  375. {
  376. char *p;
  377. unsigned int taglen = strlen(tag);
  378. unsigned long size = modinfo_len;
  379. for (p = modinfo; p; p = next_string(p, &size)) {
  380. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  381. return p + taglen + 1;
  382. }
  383. return NULL;
  384. }
  385. void
  386. read_symbols(char *modname)
  387. {
  388. const char *symname;
  389. char *version;
  390. struct module *mod;
  391. struct elf_info info = { };
  392. Elf_Sym *sym;
  393. parse_elf(&info, modname);
  394. mod = new_module(modname);
  395. /* When there's no vmlinux, don't print warnings about
  396. * unresolved symbols (since there'll be too many ;) */
  397. if (is_vmlinux(modname)) {
  398. unsigned int fake_crc = 0;
  399. have_vmlinux = 1;
  400. add_exported_symbol("struct_module", mod, &fake_crc);
  401. mod->skip = 1;
  402. }
  403. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  404. symname = info.strtab + sym->st_name;
  405. handle_modversions(mod, &info, sym, symname);
  406. handle_moddevtable(mod, &info, sym, symname);
  407. }
  408. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  409. if (version)
  410. maybe_frob_rcs_version(modname, version, info.modinfo,
  411. version - (char *)info.hdr);
  412. if (version || (all_versions && !is_vmlinux(modname)))
  413. get_src_version(modname, mod->srcversion,
  414. sizeof(mod->srcversion)-1);
  415. parse_elf_finish(&info);
  416. /* Our trick to get versioning for struct_module - it's
  417. * never passed as an argument to an exported function, so
  418. * the automatic versioning doesn't pick it up, but it's really
  419. * important anyhow */
  420. if (modversions)
  421. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  422. }
  423. #define SZ 500
  424. /* We first write the generated file into memory using the
  425. * following helper, then compare to the file on disk and
  426. * only update the later if anything changed */
  427. void __attribute__((format(printf, 2, 3)))
  428. buf_printf(struct buffer *buf, const char *fmt, ...)
  429. {
  430. char tmp[SZ];
  431. int len;
  432. va_list ap;
  433. va_start(ap, fmt);
  434. len = vsnprintf(tmp, SZ, fmt, ap);
  435. if (buf->size - buf->pos < len + 1) {
  436. buf->size += 128;
  437. buf->p = realloc(buf->p, buf->size);
  438. }
  439. strncpy(buf->p + buf->pos, tmp, len + 1);
  440. buf->pos += len;
  441. va_end(ap);
  442. }
  443. void
  444. buf_write(struct buffer *buf, const char *s, int len)
  445. {
  446. if (buf->size - buf->pos < len) {
  447. buf->size += len;
  448. buf->p = realloc(buf->p, buf->size);
  449. }
  450. strncpy(buf->p + buf->pos, s, len);
  451. buf->pos += len;
  452. }
  453. /* Header for the generated file */
  454. void
  455. add_header(struct buffer *b, struct module *mod)
  456. {
  457. buf_printf(b, "#include <linux/module.h>\n");
  458. buf_printf(b, "#include <linux/vermagic.h>\n");
  459. buf_printf(b, "#include <linux/compiler.h>\n");
  460. buf_printf(b, "\n");
  461. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  462. buf_printf(b, "\n");
  463. buf_printf(b, "struct module __this_module\n");
  464. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  465. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  466. if (mod->has_init)
  467. buf_printf(b, " .init = init_module,\n");
  468. if (mod->has_cleanup)
  469. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  470. " .exit = cleanup_module,\n"
  471. "#endif\n");
  472. buf_printf(b, "};\n");
  473. }
  474. /* Record CRCs for unresolved symbols */
  475. void
  476. add_versions(struct buffer *b, struct module *mod)
  477. {
  478. struct symbol *s, *exp;
  479. for (s = mod->unres; s; s = s->next) {
  480. exp = find_symbol(s->name);
  481. if (!exp || exp->module == mod) {
  482. if (have_vmlinux && !s->weak)
  483. fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
  484. "undefined!\n", s->name, mod->name);
  485. continue;
  486. }
  487. s->module = exp->module;
  488. s->crc_valid = exp->crc_valid;
  489. s->crc = exp->crc;
  490. }
  491. if (!modversions)
  492. return;
  493. buf_printf(b, "\n");
  494. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  495. buf_printf(b, "__attribute_used__\n");
  496. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  497. for (s = mod->unres; s; s = s->next) {
  498. if (!s->module) {
  499. continue;
  500. }
  501. if (!s->crc_valid) {
  502. fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
  503. "has no CRC!\n",
  504. s->name, mod->name);
  505. continue;
  506. }
  507. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  508. }
  509. buf_printf(b, "};\n");
  510. }
  511. void
  512. add_depends(struct buffer *b, struct module *mod, struct module *modules)
  513. {
  514. struct symbol *s;
  515. struct module *m;
  516. int first = 1;
  517. for (m = modules; m; m = m->next) {
  518. m->seen = is_vmlinux(m->name);
  519. }
  520. buf_printf(b, "\n");
  521. buf_printf(b, "static const char __module_depends[]\n");
  522. buf_printf(b, "__attribute_used__\n");
  523. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  524. buf_printf(b, "\"depends=");
  525. for (s = mod->unres; s; s = s->next) {
  526. if (!s->module)
  527. continue;
  528. if (s->module->seen)
  529. continue;
  530. s->module->seen = 1;
  531. buf_printf(b, "%s%s", first ? "" : ",",
  532. strrchr(s->module->name, '/') + 1);
  533. first = 0;
  534. }
  535. buf_printf(b, "\";\n");
  536. }
  537. void
  538. add_srcversion(struct buffer *b, struct module *mod)
  539. {
  540. if (mod->srcversion[0]) {
  541. buf_printf(b, "\n");
  542. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  543. mod->srcversion);
  544. }
  545. }
  546. void
  547. write_if_changed(struct buffer *b, const char *fname)
  548. {
  549. char *tmp;
  550. FILE *file;
  551. struct stat st;
  552. file = fopen(fname, "r");
  553. if (!file)
  554. goto write;
  555. if (fstat(fileno(file), &st) < 0)
  556. goto close_write;
  557. if (st.st_size != b->pos)
  558. goto close_write;
  559. tmp = NOFAIL(malloc(b->pos));
  560. if (fread(tmp, 1, b->pos, file) != b->pos)
  561. goto free_write;
  562. if (memcmp(tmp, b->p, b->pos) != 0)
  563. goto free_write;
  564. free(tmp);
  565. fclose(file);
  566. return;
  567. free_write:
  568. free(tmp);
  569. close_write:
  570. fclose(file);
  571. write:
  572. file = fopen(fname, "w");
  573. if (!file) {
  574. perror(fname);
  575. exit(1);
  576. }
  577. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  578. perror(fname);
  579. exit(1);
  580. }
  581. fclose(file);
  582. }
  583. void
  584. read_dump(const char *fname)
  585. {
  586. unsigned long size, pos = 0;
  587. void *file = grab_file(fname, &size);
  588. char *line;
  589. if (!file)
  590. /* No symbol versions, silently ignore */
  591. return;
  592. while ((line = get_next_line(&pos, file, size))) {
  593. char *symname, *modname, *d;
  594. unsigned int crc;
  595. struct module *mod;
  596. if (!(symname = strchr(line, '\t')))
  597. goto fail;
  598. *symname++ = '\0';
  599. if (!(modname = strchr(symname, '\t')))
  600. goto fail;
  601. *modname++ = '\0';
  602. if (strchr(modname, '\t'))
  603. goto fail;
  604. crc = strtoul(line, &d, 16);
  605. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  606. goto fail;
  607. if (!(mod = find_module(modname))) {
  608. if (is_vmlinux(modname)) {
  609. have_vmlinux = 1;
  610. }
  611. mod = new_module(NOFAIL(strdup(modname)));
  612. mod->skip = 1;
  613. }
  614. add_exported_symbol(symname, mod, &crc);
  615. }
  616. return;
  617. fail:
  618. fatal("parse error in symbol dump file\n");
  619. }
  620. void
  621. write_dump(const char *fname)
  622. {
  623. struct buffer buf = { };
  624. struct symbol *symbol;
  625. int n;
  626. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  627. symbol = symbolhash[n];
  628. while (symbol) {
  629. symbol = symbol->next;
  630. }
  631. }
  632. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  633. symbol = symbolhash[n];
  634. while (symbol) {
  635. buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
  636. symbol->name, symbol->module->name);
  637. symbol = symbol->next;
  638. }
  639. }
  640. write_if_changed(&buf, fname);
  641. }
  642. int
  643. main(int argc, char **argv)
  644. {
  645. struct module *mod;
  646. struct buffer buf = { };
  647. char fname[SZ];
  648. char *dump_read = NULL, *dump_write = NULL;
  649. int opt;
  650. while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
  651. switch(opt) {
  652. case 'i':
  653. dump_read = optarg;
  654. break;
  655. case 'm':
  656. modversions = 1;
  657. break;
  658. case 'o':
  659. dump_write = optarg;
  660. break;
  661. case 'a':
  662. all_versions = 1;
  663. break;
  664. default:
  665. exit(1);
  666. }
  667. }
  668. if (dump_read)
  669. read_dump(dump_read);
  670. while (optind < argc) {
  671. read_symbols(argv[optind++]);
  672. }
  673. for (mod = modules; mod; mod = mod->next) {
  674. if (mod->skip)
  675. continue;
  676. buf.pos = 0;
  677. add_header(&buf, mod);
  678. add_versions(&buf, mod);
  679. add_depends(&buf, mod, modules);
  680. add_moddevtable(&buf, mod);
  681. add_srcversion(&buf, mod);
  682. sprintf(fname, "%s.mod.c", mod->name);
  683. write_if_changed(&buf, fname);
  684. }
  685. if (dump_write)
  686. write_dump(dump_write);
  687. return 0;
  688. }