modpost.c 17 KB

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