modpost.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. 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, "#undef unix\n"); /* We have a module called "unix" */
  464. buf_printf(b, "struct module __this_module\n");
  465. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  466. buf_printf(b, " .name = __stringify(KBUILD_MODNAME),\n");
  467. if (mod->has_init)
  468. buf_printf(b, " .init = init_module,\n");
  469. if (mod->has_cleanup)
  470. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  471. " .exit = cleanup_module,\n"
  472. "#endif\n");
  473. buf_printf(b, "};\n");
  474. }
  475. /* Record CRCs for unresolved symbols */
  476. void
  477. add_versions(struct buffer *b, struct module *mod)
  478. {
  479. struct symbol *s, *exp;
  480. for (s = mod->unres; s; s = s->next) {
  481. exp = find_symbol(s->name);
  482. if (!exp || exp->module == mod) {
  483. if (have_vmlinux && !s->weak)
  484. fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
  485. "undefined!\n", s->name, mod->name);
  486. continue;
  487. }
  488. s->module = exp->module;
  489. s->crc_valid = exp->crc_valid;
  490. s->crc = exp->crc;
  491. }
  492. if (!modversions)
  493. return;
  494. buf_printf(b, "\n");
  495. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  496. buf_printf(b, "__attribute_used__\n");
  497. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  498. for (s = mod->unres; s; s = s->next) {
  499. if (!s->module) {
  500. continue;
  501. }
  502. if (!s->crc_valid) {
  503. fprintf(stderr, "*** Warning: \"%s\" [%s.ko] "
  504. "has no CRC!\n",
  505. s->name, mod->name);
  506. continue;
  507. }
  508. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  509. }
  510. buf_printf(b, "};\n");
  511. }
  512. void
  513. add_depends(struct buffer *b, struct module *mod, struct module *modules)
  514. {
  515. struct symbol *s;
  516. struct module *m;
  517. int first = 1;
  518. for (m = modules; m; m = m->next) {
  519. m->seen = is_vmlinux(m->name);
  520. }
  521. buf_printf(b, "\n");
  522. buf_printf(b, "static const char __module_depends[]\n");
  523. buf_printf(b, "__attribute_used__\n");
  524. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  525. buf_printf(b, "\"depends=");
  526. for (s = mod->unres; s; s = s->next) {
  527. if (!s->module)
  528. continue;
  529. if (s->module->seen)
  530. continue;
  531. s->module->seen = 1;
  532. buf_printf(b, "%s%s", first ? "" : ",",
  533. strrchr(s->module->name, '/') + 1);
  534. first = 0;
  535. }
  536. buf_printf(b, "\";\n");
  537. }
  538. void
  539. add_srcversion(struct buffer *b, struct module *mod)
  540. {
  541. if (mod->srcversion[0]) {
  542. buf_printf(b, "\n");
  543. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  544. mod->srcversion);
  545. }
  546. }
  547. void
  548. write_if_changed(struct buffer *b, const char *fname)
  549. {
  550. char *tmp;
  551. FILE *file;
  552. struct stat st;
  553. file = fopen(fname, "r");
  554. if (!file)
  555. goto write;
  556. if (fstat(fileno(file), &st) < 0)
  557. goto close_write;
  558. if (st.st_size != b->pos)
  559. goto close_write;
  560. tmp = NOFAIL(malloc(b->pos));
  561. if (fread(tmp, 1, b->pos, file) != b->pos)
  562. goto free_write;
  563. if (memcmp(tmp, b->p, b->pos) != 0)
  564. goto free_write;
  565. free(tmp);
  566. fclose(file);
  567. return;
  568. free_write:
  569. free(tmp);
  570. close_write:
  571. fclose(file);
  572. write:
  573. file = fopen(fname, "w");
  574. if (!file) {
  575. perror(fname);
  576. exit(1);
  577. }
  578. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  579. perror(fname);
  580. exit(1);
  581. }
  582. fclose(file);
  583. }
  584. void
  585. read_dump(const char *fname)
  586. {
  587. unsigned long size, pos = 0;
  588. void *file = grab_file(fname, &size);
  589. char *line;
  590. if (!file)
  591. /* No symbol versions, silently ignore */
  592. return;
  593. while ((line = get_next_line(&pos, file, size))) {
  594. char *symname, *modname, *d;
  595. unsigned int crc;
  596. struct module *mod;
  597. if (!(symname = strchr(line, '\t')))
  598. goto fail;
  599. *symname++ = '\0';
  600. if (!(modname = strchr(symname, '\t')))
  601. goto fail;
  602. *modname++ = '\0';
  603. if (strchr(modname, '\t'))
  604. goto fail;
  605. crc = strtoul(line, &d, 16);
  606. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  607. goto fail;
  608. if (!(mod = find_module(modname))) {
  609. if (is_vmlinux(modname)) {
  610. have_vmlinux = 1;
  611. }
  612. mod = new_module(NOFAIL(strdup(modname)));
  613. mod->skip = 1;
  614. }
  615. add_exported_symbol(symname, mod, &crc);
  616. }
  617. return;
  618. fail:
  619. fatal("parse error in symbol dump file\n");
  620. }
  621. void
  622. write_dump(const char *fname)
  623. {
  624. struct buffer buf = { };
  625. struct symbol *symbol;
  626. int n;
  627. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  628. symbol = symbolhash[n];
  629. while (symbol) {
  630. symbol = symbol->next;
  631. }
  632. }
  633. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  634. symbol = symbolhash[n];
  635. while (symbol) {
  636. buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
  637. symbol->name, symbol->module->name);
  638. symbol = symbol->next;
  639. }
  640. }
  641. write_if_changed(&buf, fname);
  642. }
  643. int
  644. main(int argc, char **argv)
  645. {
  646. struct module *mod;
  647. struct buffer buf = { };
  648. char fname[SZ];
  649. char *dump_read = NULL, *dump_write = NULL;
  650. int opt;
  651. while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
  652. switch(opt) {
  653. case 'i':
  654. dump_read = optarg;
  655. break;
  656. case 'm':
  657. modversions = 1;
  658. break;
  659. case 'o':
  660. dump_write = optarg;
  661. break;
  662. case 'a':
  663. all_versions = 1;
  664. break;
  665. default:
  666. exit(1);
  667. }
  668. }
  669. if (dump_read)
  670. read_dump(dump_read);
  671. while (optind < argc) {
  672. read_symbols(argv[optind++]);
  673. }
  674. for (mod = modules; mod; mod = mod->next) {
  675. if (mod->skip)
  676. continue;
  677. buf.pos = 0;
  678. add_header(&buf, mod);
  679. add_versions(&buf, mod);
  680. add_depends(&buf, mod, modules);
  681. add_moddevtable(&buf, mod);
  682. add_srcversion(&buf, mod);
  683. sprintf(fname, "%s.mod.c", mod->name);
  684. write_if_changed(&buf, fname);
  685. }
  686. if (dump_write)
  687. write_dump(dump_write);
  688. return 0;
  689. }