modpost.c 17 KB

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