modpost.c 17 KB

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