modpost.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006 Sam Ravnborg
  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. #include "../../include/linux/license.h"
  16. /* Are we using CONFIG_MODVERSIONS? */
  17. int modversions = 0;
  18. /* Warn about undefined symbols? (do so if we have vmlinux) */
  19. int have_vmlinux = 0;
  20. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  21. static int all_versions = 0;
  22. /* If we are modposting external module set to 1 */
  23. static int external_module = 0;
  24. /* How a symbol is exported */
  25. enum export {export_plain, export_gpl, export_gpl_future, export_unknown};
  26. void fatal(const char *fmt, ...)
  27. {
  28. va_list arglist;
  29. fprintf(stderr, "FATAL: ");
  30. va_start(arglist, fmt);
  31. vfprintf(stderr, fmt, arglist);
  32. va_end(arglist);
  33. exit(1);
  34. }
  35. void warn(const char *fmt, ...)
  36. {
  37. va_list arglist;
  38. fprintf(stderr, "WARNING: ");
  39. va_start(arglist, fmt);
  40. vfprintf(stderr, fmt, arglist);
  41. va_end(arglist);
  42. }
  43. static int is_vmlinux(const char *modname)
  44. {
  45. const char *myname;
  46. if ((myname = strrchr(modname, '/')))
  47. myname++;
  48. else
  49. myname = modname;
  50. return strcmp(myname, "vmlinux") == 0;
  51. }
  52. void *do_nofail(void *ptr, const char *expr)
  53. {
  54. if (!ptr) {
  55. fatal("modpost: Memory allocation failure: %s.\n", expr);
  56. }
  57. return ptr;
  58. }
  59. /* A list of all modules we processed */
  60. static struct module *modules;
  61. static struct module *find_module(char *modname)
  62. {
  63. struct module *mod;
  64. for (mod = modules; mod; mod = mod->next)
  65. if (strcmp(mod->name, modname) == 0)
  66. break;
  67. return mod;
  68. }
  69. static struct module *new_module(char *modname)
  70. {
  71. struct module *mod;
  72. char *p, *s;
  73. mod = NOFAIL(malloc(sizeof(*mod)));
  74. memset(mod, 0, sizeof(*mod));
  75. p = NOFAIL(strdup(modname));
  76. /* strip trailing .o */
  77. if ((s = strrchr(p, '.')) != NULL)
  78. if (strcmp(s, ".o") == 0)
  79. *s = '\0';
  80. /* add to list */
  81. mod->name = p;
  82. mod->gpl_compatible = -1;
  83. mod->next = modules;
  84. modules = mod;
  85. return mod;
  86. }
  87. /* A hash of all exported symbols,
  88. * struct symbol is also used for lists of unresolved symbols */
  89. #define SYMBOL_HASH_SIZE 1024
  90. struct symbol {
  91. struct symbol *next;
  92. struct module *module;
  93. unsigned int crc;
  94. int crc_valid;
  95. unsigned int weak:1;
  96. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  97. unsigned int kernel:1; /* 1 if symbol is from kernel
  98. * (only for external modules) **/
  99. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  100. enum export export; /* Type of export */
  101. char name[0];
  102. };
  103. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  104. /* This is based on the hash agorithm from gdbm, via tdb */
  105. static inline unsigned int tdb_hash(const char *name)
  106. {
  107. unsigned value; /* Used to compute the hash value. */
  108. unsigned i; /* Used to cycle through random values. */
  109. /* Set the initial value from the key size. */
  110. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  111. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  112. return (1103515243 * value + 12345);
  113. }
  114. /**
  115. * Allocate a new symbols for use in the hash of exported symbols or
  116. * the list of unresolved symbols per module
  117. **/
  118. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  119. struct symbol *next)
  120. {
  121. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  122. memset(s, 0, sizeof(*s));
  123. strcpy(s->name, name);
  124. s->weak = weak;
  125. s->next = next;
  126. return s;
  127. }
  128. /* For the hash of exported symbols */
  129. static struct symbol *new_symbol(const char *name, struct module *module,
  130. enum export export)
  131. {
  132. unsigned int hash;
  133. struct symbol *new;
  134. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  135. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  136. new->module = module;
  137. new->export = export;
  138. return new;
  139. }
  140. static struct symbol *find_symbol(const char *name)
  141. {
  142. struct symbol *s;
  143. /* For our purposes, .foo matches foo. PPC64 needs this. */
  144. if (name[0] == '.')
  145. name++;
  146. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  147. if (strcmp(s->name, name) == 0)
  148. return s;
  149. }
  150. return NULL;
  151. }
  152. static struct {
  153. const char *str;
  154. enum export export;
  155. } export_list[] = {
  156. { .str = "EXPORT_SYMBOL", .export = export_plain },
  157. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  158. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  159. { .str = "(unknown)", .export = export_unknown },
  160. };
  161. static const char *export_str(enum export ex)
  162. {
  163. return export_list[ex].str;
  164. }
  165. static enum export export_no(const char * s)
  166. {
  167. int i;
  168. for (i = 0; export_list[i].export != export_unknown; i++) {
  169. if (strcmp(export_list[i].str, s) == 0)
  170. return export_list[i].export;
  171. }
  172. return export_unknown;
  173. }
  174. static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
  175. {
  176. if (sec == elf->export_sec)
  177. return export_plain;
  178. else if (sec == elf->export_gpl_sec)
  179. return export_gpl;
  180. else if (sec == elf->export_gpl_future_sec)
  181. return export_gpl_future;
  182. else
  183. return export_unknown;
  184. }
  185. /**
  186. * Add an exported symbol - it may have already been added without a
  187. * CRC, in this case just update the CRC
  188. **/
  189. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  190. enum export export)
  191. {
  192. struct symbol *s = find_symbol(name);
  193. if (!s) {
  194. s = new_symbol(name, mod, export);
  195. } else {
  196. if (!s->preloaded) {
  197. warn("%s: '%s' exported twice. Previous export "
  198. "was in %s%s\n", mod->name, name,
  199. s->module->name,
  200. is_vmlinux(s->module->name) ?"":".ko");
  201. }
  202. }
  203. s->preloaded = 0;
  204. s->vmlinux = is_vmlinux(mod->name);
  205. s->kernel = 0;
  206. s->export = export;
  207. return s;
  208. }
  209. static void sym_update_crc(const char *name, struct module *mod,
  210. unsigned int crc, enum export export)
  211. {
  212. struct symbol *s = find_symbol(name);
  213. if (!s)
  214. s = new_symbol(name, mod, export);
  215. s->crc = crc;
  216. s->crc_valid = 1;
  217. }
  218. void *grab_file(const char *filename, unsigned long *size)
  219. {
  220. struct stat st;
  221. void *map;
  222. int fd;
  223. fd = open(filename, O_RDONLY);
  224. if (fd < 0 || fstat(fd, &st) != 0)
  225. return NULL;
  226. *size = st.st_size;
  227. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  228. close(fd);
  229. if (map == MAP_FAILED)
  230. return NULL;
  231. return map;
  232. }
  233. /**
  234. * Return a copy of the next line in a mmap'ed file.
  235. * spaces in the beginning of the line is trimmed away.
  236. * Return a pointer to a static buffer.
  237. **/
  238. char* get_next_line(unsigned long *pos, void *file, unsigned long size)
  239. {
  240. static char line[4096];
  241. int skip = 1;
  242. size_t len = 0;
  243. signed char *p = (signed char *)file + *pos;
  244. char *s = line;
  245. for (; *pos < size ; (*pos)++)
  246. {
  247. if (skip && isspace(*p)) {
  248. p++;
  249. continue;
  250. }
  251. skip = 0;
  252. if (*p != '\n' && (*pos < size)) {
  253. len++;
  254. *s++ = *p++;
  255. if (len > 4095)
  256. break; /* Too long, stop */
  257. } else {
  258. /* End of string */
  259. *s = '\0';
  260. return line;
  261. }
  262. }
  263. /* End of buffer */
  264. return NULL;
  265. }
  266. void release_file(void *file, unsigned long size)
  267. {
  268. munmap(file, size);
  269. }
  270. static void parse_elf(struct elf_info *info, const char *filename)
  271. {
  272. unsigned int i;
  273. Elf_Ehdr *hdr = info->hdr;
  274. Elf_Shdr *sechdrs;
  275. Elf_Sym *sym;
  276. hdr = grab_file(filename, &info->size);
  277. if (!hdr) {
  278. perror(filename);
  279. exit(1);
  280. }
  281. info->hdr = hdr;
  282. if (info->size < sizeof(*hdr))
  283. goto truncated;
  284. /* Fix endianness in ELF header */
  285. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  286. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  287. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  288. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  289. sechdrs = (void *)hdr + hdr->e_shoff;
  290. info->sechdrs = sechdrs;
  291. /* Fix endianness in section headers */
  292. for (i = 0; i < hdr->e_shnum; i++) {
  293. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  294. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  295. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  296. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  297. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  298. }
  299. /* Find symbol table. */
  300. for (i = 1; i < hdr->e_shnum; i++) {
  301. const char *secstrings
  302. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  303. const char *secname;
  304. if (sechdrs[i].sh_offset > info->size)
  305. goto truncated;
  306. secname = secstrings + sechdrs[i].sh_name;
  307. if (strcmp(secname, ".modinfo") == 0) {
  308. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  309. info->modinfo_len = sechdrs[i].sh_size;
  310. } else if (strcmp(secname, "__ksymtab") == 0)
  311. info->export_sec = i;
  312. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  313. info->export_gpl_sec = i;
  314. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  315. info->export_gpl_future_sec = i;
  316. if (sechdrs[i].sh_type != SHT_SYMTAB)
  317. continue;
  318. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  319. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  320. + sechdrs[i].sh_size;
  321. info->strtab = (void *)hdr +
  322. sechdrs[sechdrs[i].sh_link].sh_offset;
  323. }
  324. if (!info->symtab_start) {
  325. fatal("%s has no symtab?\n", filename);
  326. }
  327. /* Fix endianness in symbols */
  328. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  329. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  330. sym->st_name = TO_NATIVE(sym->st_name);
  331. sym->st_value = TO_NATIVE(sym->st_value);
  332. sym->st_size = TO_NATIVE(sym->st_size);
  333. }
  334. return;
  335. truncated:
  336. fatal("%s is truncated.\n", filename);
  337. }
  338. static void parse_elf_finish(struct elf_info *info)
  339. {
  340. release_file(info->hdr, info->size);
  341. }
  342. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  343. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  344. static void handle_modversions(struct module *mod, struct elf_info *info,
  345. Elf_Sym *sym, const char *symname)
  346. {
  347. unsigned int crc;
  348. enum export export = export_from_sec(info, sym->st_shndx);
  349. switch (sym->st_shndx) {
  350. case SHN_COMMON:
  351. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  352. break;
  353. case SHN_ABS:
  354. /* CRC'd symbol */
  355. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  356. crc = (unsigned int) sym->st_value;
  357. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  358. export);
  359. }
  360. break;
  361. case SHN_UNDEF:
  362. /* undefined symbol */
  363. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  364. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  365. break;
  366. /* ignore global offset table */
  367. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  368. break;
  369. /* ignore __this_module, it will be resolved shortly */
  370. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  371. break;
  372. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  373. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  374. /* add compatibility with older glibc */
  375. #ifndef STT_SPARC_REGISTER
  376. #define STT_SPARC_REGISTER STT_REGISTER
  377. #endif
  378. if (info->hdr->e_machine == EM_SPARC ||
  379. info->hdr->e_machine == EM_SPARCV9) {
  380. /* Ignore register directives. */
  381. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  382. break;
  383. if (symname[0] == '.') {
  384. char *munged = strdup(symname);
  385. munged[0] = '_';
  386. munged[1] = toupper(munged[1]);
  387. symname = munged;
  388. }
  389. }
  390. #endif
  391. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  392. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  393. mod->unres = alloc_symbol(symname +
  394. strlen(MODULE_SYMBOL_PREFIX),
  395. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  396. mod->unres);
  397. break;
  398. default:
  399. /* All exported symbols */
  400. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  401. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  402. export);
  403. }
  404. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  405. mod->has_init = 1;
  406. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  407. mod->has_cleanup = 1;
  408. break;
  409. }
  410. }
  411. /**
  412. * Parse tag=value strings from .modinfo section
  413. **/
  414. static char *next_string(char *string, unsigned long *secsize)
  415. {
  416. /* Skip non-zero chars */
  417. while (string[0]) {
  418. string++;
  419. if ((*secsize)-- <= 1)
  420. return NULL;
  421. }
  422. /* Skip any zero padding. */
  423. while (!string[0]) {
  424. string++;
  425. if ((*secsize)-- <= 1)
  426. return NULL;
  427. }
  428. return string;
  429. }
  430. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  431. const char *tag, char *info)
  432. {
  433. char *p;
  434. unsigned int taglen = strlen(tag);
  435. unsigned long size = modinfo_len;
  436. if (info) {
  437. size -= info - (char *)modinfo;
  438. modinfo = next_string(info, &size);
  439. }
  440. for (p = modinfo; p; p = next_string(p, &size)) {
  441. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  442. return p + taglen + 1;
  443. }
  444. return NULL;
  445. }
  446. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  447. const char *tag)
  448. {
  449. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  450. }
  451. /**
  452. * Test if string s ends in string sub
  453. * return 0 if match
  454. **/
  455. static int strrcmp(const char *s, const char *sub)
  456. {
  457. int slen, sublen;
  458. if (!s || !sub)
  459. return 1;
  460. slen = strlen(s);
  461. sublen = strlen(sub);
  462. if ((slen == 0) || (sublen == 0))
  463. return 1;
  464. if (sublen > slen)
  465. return 1;
  466. return memcmp(s + slen - sublen, sub, sublen);
  467. }
  468. /**
  469. * Whitelist to allow certain references to pass with no warning.
  470. * Pattern 1:
  471. * If a module parameter is declared __initdata and permissions=0
  472. * then this is legal despite the warning generated.
  473. * We cannot see value of permissions here, so just ignore
  474. * this pattern.
  475. * The pattern is identified by:
  476. * tosec = .init.data
  477. * fromsec = .data*
  478. * atsym =__param*
  479. *
  480. * Pattern 2:
  481. * Many drivers utilise a *driver container with references to
  482. * add, remove, probe functions etc.
  483. * These functions may often be marked __init and we do not want to
  484. * warn here.
  485. * the pattern is identified by:
  486. * tosec = .init.text | .exit.text | .init.data
  487. * fromsec = .data
  488. * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
  489. **/
  490. static int secref_whitelist(const char *tosec, const char *fromsec,
  491. const char *atsym)
  492. {
  493. int f1 = 1, f2 = 1;
  494. const char **s;
  495. const char *pat2sym[] = {
  496. "driver",
  497. "_template", /* scsi uses *_template a lot */
  498. "_sht", /* scsi also used *_sht to some extent */
  499. "_ops",
  500. "_probe",
  501. "_probe_one",
  502. NULL
  503. };
  504. /* Check for pattern 1 */
  505. if (strcmp(tosec, ".init.data") != 0)
  506. f1 = 0;
  507. if (strncmp(fromsec, ".data", strlen(".data")) != 0)
  508. f1 = 0;
  509. if (strncmp(atsym, "__param", strlen("__param")) != 0)
  510. f1 = 0;
  511. if (f1)
  512. return f1;
  513. /* Check for pattern 2 */
  514. if ((strcmp(tosec, ".init.text") != 0) &&
  515. (strcmp(tosec, ".exit.text") != 0) &&
  516. (strcmp(tosec, ".init.data") != 0))
  517. f2 = 0;
  518. if (strcmp(fromsec, ".data") != 0)
  519. f2 = 0;
  520. for (s = pat2sym; *s; s++)
  521. if (strrcmp(atsym, *s) == 0)
  522. f1 = 1;
  523. return f1 && f2;
  524. }
  525. /**
  526. * Find symbol based on relocation record info.
  527. * In some cases the symbol supplied is a valid symbol so
  528. * return refsym. If st_name != 0 we assume this is a valid symbol.
  529. * In other cases the symbol needs to be looked up in the symbol table
  530. * based on section and address.
  531. * **/
  532. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  533. Elf_Sym *relsym)
  534. {
  535. Elf_Sym *sym;
  536. if (relsym->st_name != 0)
  537. return relsym;
  538. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  539. if (sym->st_shndx != relsym->st_shndx)
  540. continue;
  541. if (sym->st_value == addr)
  542. return sym;
  543. }
  544. return NULL;
  545. }
  546. /*
  547. * Find symbols before or equal addr and after addr - in the section sec.
  548. * If we find two symbols with equal offset prefer one with a valid name.
  549. * The ELF format may have a better way to detect what type of symbol
  550. * it is, but this works for now.
  551. **/
  552. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  553. const char *sec,
  554. Elf_Sym **before, Elf_Sym **after)
  555. {
  556. Elf_Sym *sym;
  557. Elf_Ehdr *hdr = elf->hdr;
  558. Elf_Addr beforediff = ~0;
  559. Elf_Addr afterdiff = ~0;
  560. const char *secstrings = (void *)hdr +
  561. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  562. *before = NULL;
  563. *after = NULL;
  564. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  565. const char *symsec;
  566. if (sym->st_shndx >= SHN_LORESERVE)
  567. continue;
  568. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  569. if (strcmp(symsec, sec) != 0)
  570. continue;
  571. if (sym->st_value <= addr) {
  572. if ((addr - sym->st_value) < beforediff) {
  573. beforediff = addr - sym->st_value;
  574. *before = sym;
  575. }
  576. else if ((addr - sym->st_value) == beforediff) {
  577. /* equal offset, valid name? */
  578. const char *name = elf->strtab + sym->st_name;
  579. if (name && strlen(name))
  580. *before = sym;
  581. }
  582. }
  583. else
  584. {
  585. if ((sym->st_value - addr) < afterdiff) {
  586. afterdiff = sym->st_value - addr;
  587. *after = sym;
  588. }
  589. else if ((sym->st_value - addr) == afterdiff) {
  590. /* equal offset, valid name? */
  591. const char *name = elf->strtab + sym->st_name;
  592. if (name && strlen(name))
  593. *after = sym;
  594. }
  595. }
  596. }
  597. }
  598. /**
  599. * Print a warning about a section mismatch.
  600. * Try to find symbols near it so user can find it.
  601. * Check whitelist before warning - it may be a false positive.
  602. **/
  603. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  604. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  605. {
  606. const char *refsymname = "";
  607. Elf_Sym *before, *after;
  608. Elf_Sym *refsym;
  609. Elf_Ehdr *hdr = elf->hdr;
  610. Elf_Shdr *sechdrs = elf->sechdrs;
  611. const char *secstrings = (void *)hdr +
  612. sechdrs[hdr->e_shstrndx].sh_offset;
  613. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  614. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  615. refsym = find_elf_symbol(elf, r.r_addend, sym);
  616. if (refsym && strlen(elf->strtab + refsym->st_name))
  617. refsymname = elf->strtab + refsym->st_name;
  618. /* check whitelist - we may ignore it */
  619. if (before &&
  620. secref_whitelist(secname, fromsec, elf->strtab + before->st_name))
  621. return;
  622. if (before && after) {
  623. warn("%s - Section mismatch: reference to %s:%s from %s "
  624. "between '%s' (at offset 0x%llx) and '%s'\n",
  625. modname, secname, refsymname, fromsec,
  626. elf->strtab + before->st_name,
  627. (long long)r.r_offset,
  628. elf->strtab + after->st_name);
  629. } else if (before) {
  630. warn("%s - Section mismatch: reference to %s:%s from %s "
  631. "after '%s' (at offset 0x%llx)\n",
  632. modname, secname, refsymname, fromsec,
  633. elf->strtab + before->st_name,
  634. (long long)r.r_offset);
  635. } else if (after) {
  636. warn("%s - Section mismatch: reference to %s:%s from %s "
  637. "before '%s' (at offset -0x%llx)\n",
  638. modname, secname, refsymname, fromsec,
  639. elf->strtab + after->st_name,
  640. (long long)r.r_offset);
  641. } else {
  642. warn("%s - Section mismatch: reference to %s:%s from %s "
  643. "(offset 0x%llx)\n",
  644. modname, secname, fromsec, refsymname,
  645. (long long)r.r_offset);
  646. }
  647. }
  648. /**
  649. * A module includes a number of sections that are discarded
  650. * either when loaded or when used as built-in.
  651. * For loaded modules all functions marked __init and all data
  652. * marked __initdata will be discarded when the module has been intialized.
  653. * Likewise for modules used built-in the sections marked __exit
  654. * are discarded because __exit marked function are supposed to be called
  655. * only when a moduel is unloaded which never happes for built-in modules.
  656. * The check_sec_ref() function traverses all relocation records
  657. * to find all references to a section that reference a section that will
  658. * be discarded and warns about it.
  659. **/
  660. static void check_sec_ref(struct module *mod, const char *modname,
  661. struct elf_info *elf,
  662. int section(const char*),
  663. int section_ref_ok(const char *))
  664. {
  665. int i;
  666. Elf_Sym *sym;
  667. Elf_Ehdr *hdr = elf->hdr;
  668. Elf_Shdr *sechdrs = elf->sechdrs;
  669. const char *secstrings = (void *)hdr +
  670. sechdrs[hdr->e_shstrndx].sh_offset;
  671. /* Walk through all sections */
  672. for (i = 0; i < hdr->e_shnum; i++) {
  673. const char *name = secstrings + sechdrs[i].sh_name;
  674. const char *secname;
  675. Elf_Rela r;
  676. unsigned int r_sym;
  677. /* We want to process only relocation sections and not .init */
  678. if (sechdrs[i].sh_type == SHT_RELA) {
  679. Elf_Rela *rela;
  680. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  681. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  682. name += strlen(".rela");
  683. if (section_ref_ok(name))
  684. continue;
  685. for (rela = start; rela < stop; rela++) {
  686. r.r_offset = TO_NATIVE(rela->r_offset);
  687. #if KERNEL_ELFCLASS == ELFCLASS64
  688. if (hdr->e_machine == EM_MIPS) {
  689. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  690. r_sym = TO_NATIVE(r_sym);
  691. } else {
  692. r.r_info = TO_NATIVE(rela->r_info);
  693. r_sym = ELF_R_SYM(r.r_info);
  694. }
  695. #else
  696. r.r_info = TO_NATIVE(rela->r_info);
  697. r_sym = ELF_R_SYM(r.r_info);
  698. #endif
  699. r.r_addend = TO_NATIVE(rela->r_addend);
  700. sym = elf->symtab_start + r_sym;
  701. /* Skip special sections */
  702. if (sym->st_shndx >= SHN_LORESERVE)
  703. continue;
  704. secname = secstrings +
  705. sechdrs[sym->st_shndx].sh_name;
  706. if (section(secname))
  707. warn_sec_mismatch(modname, name,
  708. elf, sym, r);
  709. }
  710. } else if (sechdrs[i].sh_type == SHT_REL) {
  711. Elf_Rel *rel;
  712. Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
  713. Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
  714. name += strlen(".rel");
  715. if (section_ref_ok(name))
  716. continue;
  717. for (rel = start; rel < stop; rel++) {
  718. r.r_offset = TO_NATIVE(rel->r_offset);
  719. #if KERNEL_ELFCLASS == ELFCLASS64
  720. if (hdr->e_machine == EM_MIPS) {
  721. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  722. r_sym = TO_NATIVE(r_sym);
  723. } else {
  724. r.r_info = TO_NATIVE(rel->r_info);
  725. r_sym = ELF_R_SYM(r.r_info);
  726. }
  727. #else
  728. r.r_info = TO_NATIVE(rel->r_info);
  729. r_sym = ELF_R_SYM(r.r_info);
  730. #endif
  731. r.r_addend = 0;
  732. sym = elf->symtab_start + r_sym;
  733. /* Skip special sections */
  734. if (sym->st_shndx >= SHN_LORESERVE)
  735. continue;
  736. secname = secstrings +
  737. sechdrs[sym->st_shndx].sh_name;
  738. if (section(secname))
  739. warn_sec_mismatch(modname, name,
  740. elf, sym, r);
  741. }
  742. }
  743. }
  744. }
  745. /**
  746. * Functions used only during module init is marked __init and is stored in
  747. * a .init.text section. Likewise data is marked __initdata and stored in
  748. * a .init.data section.
  749. * If this section is one of these sections return 1
  750. * See include/linux/init.h for the details
  751. **/
  752. static int init_section(const char *name)
  753. {
  754. if (strcmp(name, ".init") == 0)
  755. return 1;
  756. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  757. return 1;
  758. return 0;
  759. }
  760. /**
  761. * Identify sections from which references to a .init section is OK.
  762. *
  763. * Unfortunately references to read only data that referenced .init
  764. * sections had to be excluded. Almost all of these are false
  765. * positives, they are created by gcc. The downside of excluding rodata
  766. * is that there really are some user references from rodata to
  767. * init code, e.g. drivers/video/vgacon.c:
  768. *
  769. * const struct consw vga_con = {
  770. * con_startup: vgacon_startup,
  771. *
  772. * where vgacon_startup is __init. If you want to wade through the false
  773. * positives, take out the check for rodata.
  774. **/
  775. static int init_section_ref_ok(const char *name)
  776. {
  777. const char **s;
  778. /* Absolute section names */
  779. const char *namelist1[] = {
  780. ".init",
  781. ".opd", /* see comment [OPD] at exit_section_ref_ok() */
  782. ".toc1", /* used by ppc64 */
  783. ".stab",
  784. ".rodata",
  785. ".text.lock",
  786. "__bug_table", /* used by powerpc for BUG() */
  787. ".pci_fixup_header",
  788. ".pci_fixup_final",
  789. ".pdr",
  790. "__param",
  791. "__ex_table",
  792. ".fixup",
  793. ".smp_locks",
  794. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  795. NULL
  796. };
  797. /* Start of section names */
  798. const char *namelist2[] = {
  799. ".init.",
  800. ".altinstructions",
  801. ".eh_frame",
  802. ".debug",
  803. NULL
  804. };
  805. /* part of section name */
  806. const char *namelist3 [] = {
  807. ".unwind", /* sample: IA_64.unwind.init.text */
  808. NULL
  809. };
  810. for (s = namelist1; *s; s++)
  811. if (strcmp(*s, name) == 0)
  812. return 1;
  813. for (s = namelist2; *s; s++)
  814. if (strncmp(*s, name, strlen(*s)) == 0)
  815. return 1;
  816. for (s = namelist3; *s; s++)
  817. if (strstr(name, *s) != NULL)
  818. return 1;
  819. if (strrcmp(name, ".init") == 0)
  820. return 1;
  821. return 0;
  822. }
  823. /*
  824. * Functions used only during module exit is marked __exit and is stored in
  825. * a .exit.text section. Likewise data is marked __exitdata and stored in
  826. * a .exit.data section.
  827. * If this section is one of these sections return 1
  828. * See include/linux/init.h for the details
  829. **/
  830. static int exit_section(const char *name)
  831. {
  832. if (strcmp(name, ".exit.text") == 0)
  833. return 1;
  834. if (strcmp(name, ".exit.data") == 0)
  835. return 1;
  836. return 0;
  837. }
  838. /*
  839. * Identify sections from which references to a .exit section is OK.
  840. *
  841. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  842. * For our future {in}sanity, add a comment that this is the ppc .opd
  843. * section, not the ia64 .opd section.
  844. * ia64 .opd should not point to discarded sections.
  845. * [.rodata] like for .init.text we ignore .rodata references -same reason
  846. **/
  847. static int exit_section_ref_ok(const char *name)
  848. {
  849. const char **s;
  850. /* Absolute section names */
  851. const char *namelist1[] = {
  852. ".exit.text",
  853. ".exit.data",
  854. ".init.text",
  855. ".rodata",
  856. ".opd", /* See comment [OPD] */
  857. ".toc1", /* used by ppc64 */
  858. ".altinstructions",
  859. ".pdr",
  860. "__bug_table", /* used by powerpc for BUG() */
  861. ".exitcall.exit",
  862. ".eh_frame",
  863. ".stab",
  864. "__ex_table",
  865. ".fixup",
  866. ".smp_locks",
  867. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  868. NULL
  869. };
  870. /* Start of section names */
  871. const char *namelist2[] = {
  872. ".debug",
  873. NULL
  874. };
  875. /* part of section name */
  876. const char *namelist3 [] = {
  877. ".unwind", /* Sample: IA_64.unwind.exit.text */
  878. NULL
  879. };
  880. for (s = namelist1; *s; s++)
  881. if (strcmp(*s, name) == 0)
  882. return 1;
  883. for (s = namelist2; *s; s++)
  884. if (strncmp(*s, name, strlen(*s)) == 0)
  885. return 1;
  886. for (s = namelist3; *s; s++)
  887. if (strstr(name, *s) != NULL)
  888. return 1;
  889. return 0;
  890. }
  891. static void read_symbols(char *modname)
  892. {
  893. const char *symname;
  894. char *version;
  895. char *license;
  896. struct module *mod;
  897. struct elf_info info = { };
  898. Elf_Sym *sym;
  899. parse_elf(&info, modname);
  900. mod = new_module(modname);
  901. /* When there's no vmlinux, don't print warnings about
  902. * unresolved symbols (since there'll be too many ;) */
  903. if (is_vmlinux(modname)) {
  904. have_vmlinux = 1;
  905. mod->skip = 1;
  906. }
  907. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  908. while (license) {
  909. if (license_is_gpl_compatible(license))
  910. mod->gpl_compatible = 1;
  911. else {
  912. mod->gpl_compatible = 0;
  913. break;
  914. }
  915. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  916. "license", license);
  917. }
  918. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  919. symname = info.strtab + sym->st_name;
  920. handle_modversions(mod, &info, sym, symname);
  921. handle_moddevtable(mod, &info, sym, symname);
  922. }
  923. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  924. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  925. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  926. if (version)
  927. maybe_frob_rcs_version(modname, version, info.modinfo,
  928. version - (char *)info.hdr);
  929. if (version || (all_versions && !is_vmlinux(modname)))
  930. get_src_version(modname, mod->srcversion,
  931. sizeof(mod->srcversion)-1);
  932. parse_elf_finish(&info);
  933. /* Our trick to get versioning for struct_module - it's
  934. * never passed as an argument to an exported function, so
  935. * the automatic versioning doesn't pick it up, but it's really
  936. * important anyhow */
  937. if (modversions)
  938. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  939. }
  940. #define SZ 500
  941. /* We first write the generated file into memory using the
  942. * following helper, then compare to the file on disk and
  943. * only update the later if anything changed */
  944. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  945. const char *fmt, ...)
  946. {
  947. char tmp[SZ];
  948. int len;
  949. va_list ap;
  950. va_start(ap, fmt);
  951. len = vsnprintf(tmp, SZ, fmt, ap);
  952. buf_write(buf, tmp, len);
  953. va_end(ap);
  954. }
  955. void buf_write(struct buffer *buf, const char *s, int len)
  956. {
  957. if (buf->size - buf->pos < len) {
  958. buf->size += len + SZ;
  959. buf->p = realloc(buf->p, buf->size);
  960. }
  961. strncpy(buf->p + buf->pos, s, len);
  962. buf->pos += len;
  963. }
  964. void check_license(struct module *mod)
  965. {
  966. struct symbol *s, *exp;
  967. for (s = mod->unres; s; s = s->next) {
  968. const char *basename;
  969. if (mod->gpl_compatible == 1) {
  970. /* GPL-compatible modules may use all symbols */
  971. continue;
  972. }
  973. exp = find_symbol(s->name);
  974. if (!exp || exp->module == mod)
  975. continue;
  976. basename = strrchr(mod->name, '/');
  977. if (basename)
  978. basename++;
  979. switch (exp->export) {
  980. case export_gpl:
  981. fatal("modpost: GPL-incompatible module %s "
  982. "uses GPL-only symbol '%s'\n",
  983. basename ? basename : mod->name,
  984. exp->name);
  985. break;
  986. case export_gpl_future:
  987. warn("modpost: GPL-incompatible module %s "
  988. "uses future GPL-only symbol '%s'\n",
  989. basename ? basename : mod->name,
  990. exp->name);
  991. break;
  992. case export_plain: /* ignore */ break;
  993. case export_unknown: /* ignore */ break;
  994. }
  995. }
  996. }
  997. /**
  998. * Header for the generated file
  999. **/
  1000. static void add_header(struct buffer *b, struct module *mod)
  1001. {
  1002. buf_printf(b, "#include <linux/module.h>\n");
  1003. buf_printf(b, "#include <linux/vermagic.h>\n");
  1004. buf_printf(b, "#include <linux/compiler.h>\n");
  1005. buf_printf(b, "\n");
  1006. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1007. buf_printf(b, "\n");
  1008. buf_printf(b, "struct module __this_module\n");
  1009. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1010. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1011. if (mod->has_init)
  1012. buf_printf(b, " .init = init_module,\n");
  1013. if (mod->has_cleanup)
  1014. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1015. " .exit = cleanup_module,\n"
  1016. "#endif\n");
  1017. buf_printf(b, "};\n");
  1018. }
  1019. /**
  1020. * Record CRCs for unresolved symbols
  1021. **/
  1022. static void add_versions(struct buffer *b, struct module *mod)
  1023. {
  1024. struct symbol *s, *exp;
  1025. for (s = mod->unres; s; s = s->next) {
  1026. exp = find_symbol(s->name);
  1027. if (!exp || exp->module == mod) {
  1028. if (have_vmlinux && !s->weak)
  1029. warn("\"%s\" [%s.ko] undefined!\n",
  1030. s->name, mod->name);
  1031. continue;
  1032. }
  1033. s->module = exp->module;
  1034. s->crc_valid = exp->crc_valid;
  1035. s->crc = exp->crc;
  1036. }
  1037. if (!modversions)
  1038. return;
  1039. buf_printf(b, "\n");
  1040. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1041. buf_printf(b, "__attribute_used__\n");
  1042. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1043. for (s = mod->unres; s; s = s->next) {
  1044. if (!s->module) {
  1045. continue;
  1046. }
  1047. if (!s->crc_valid) {
  1048. warn("\"%s\" [%s.ko] has no CRC!\n",
  1049. s->name, mod->name);
  1050. continue;
  1051. }
  1052. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1053. }
  1054. buf_printf(b, "};\n");
  1055. }
  1056. static void add_depends(struct buffer *b, struct module *mod,
  1057. struct module *modules)
  1058. {
  1059. struct symbol *s;
  1060. struct module *m;
  1061. int first = 1;
  1062. for (m = modules; m; m = m->next) {
  1063. m->seen = is_vmlinux(m->name);
  1064. }
  1065. buf_printf(b, "\n");
  1066. buf_printf(b, "static const char __module_depends[]\n");
  1067. buf_printf(b, "__attribute_used__\n");
  1068. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1069. buf_printf(b, "\"depends=");
  1070. for (s = mod->unres; s; s = s->next) {
  1071. if (!s->module)
  1072. continue;
  1073. if (s->module->seen)
  1074. continue;
  1075. s->module->seen = 1;
  1076. buf_printf(b, "%s%s", first ? "" : ",",
  1077. strrchr(s->module->name, '/') + 1);
  1078. first = 0;
  1079. }
  1080. buf_printf(b, "\";\n");
  1081. }
  1082. static void add_srcversion(struct buffer *b, struct module *mod)
  1083. {
  1084. if (mod->srcversion[0]) {
  1085. buf_printf(b, "\n");
  1086. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1087. mod->srcversion);
  1088. }
  1089. }
  1090. static void write_if_changed(struct buffer *b, const char *fname)
  1091. {
  1092. char *tmp;
  1093. FILE *file;
  1094. struct stat st;
  1095. file = fopen(fname, "r");
  1096. if (!file)
  1097. goto write;
  1098. if (fstat(fileno(file), &st) < 0)
  1099. goto close_write;
  1100. if (st.st_size != b->pos)
  1101. goto close_write;
  1102. tmp = NOFAIL(malloc(b->pos));
  1103. if (fread(tmp, 1, b->pos, file) != b->pos)
  1104. goto free_write;
  1105. if (memcmp(tmp, b->p, b->pos) != 0)
  1106. goto free_write;
  1107. free(tmp);
  1108. fclose(file);
  1109. return;
  1110. free_write:
  1111. free(tmp);
  1112. close_write:
  1113. fclose(file);
  1114. write:
  1115. file = fopen(fname, "w");
  1116. if (!file) {
  1117. perror(fname);
  1118. exit(1);
  1119. }
  1120. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1121. perror(fname);
  1122. exit(1);
  1123. }
  1124. fclose(file);
  1125. }
  1126. /* parse Module.symvers file. line format:
  1127. * 0x12345678<tab>symbol<tab>module[<tab>export]
  1128. **/
  1129. static void read_dump(const char *fname, unsigned int kernel)
  1130. {
  1131. unsigned long size, pos = 0;
  1132. void *file = grab_file(fname, &size);
  1133. char *line;
  1134. if (!file)
  1135. /* No symbol versions, silently ignore */
  1136. return;
  1137. while ((line = get_next_line(&pos, file, size))) {
  1138. char *symname, *modname, *d, *export;
  1139. unsigned int crc;
  1140. struct module *mod;
  1141. struct symbol *s;
  1142. if (!(symname = strchr(line, '\t')))
  1143. goto fail;
  1144. *symname++ = '\0';
  1145. if (!(modname = strchr(symname, '\t')))
  1146. goto fail;
  1147. *modname++ = '\0';
  1148. if ((export = strchr(modname, '\t')) != NULL)
  1149. *export++ = '\0';
  1150. crc = strtoul(line, &d, 16);
  1151. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1152. goto fail;
  1153. if (!(mod = find_module(modname))) {
  1154. if (is_vmlinux(modname)) {
  1155. have_vmlinux = 1;
  1156. }
  1157. mod = new_module(NOFAIL(strdup(modname)));
  1158. mod->skip = 1;
  1159. }
  1160. s = sym_add_exported(symname, mod, export_no(export));
  1161. s->kernel = kernel;
  1162. s->preloaded = 1;
  1163. sym_update_crc(symname, mod, crc, export_no(export));
  1164. }
  1165. return;
  1166. fail:
  1167. fatal("parse error in symbol dump file\n");
  1168. }
  1169. /* For normal builds always dump all symbols.
  1170. * For external modules only dump symbols
  1171. * that are not read from kernel Module.symvers.
  1172. **/
  1173. static int dump_sym(struct symbol *sym)
  1174. {
  1175. if (!external_module)
  1176. return 1;
  1177. if (sym->vmlinux || sym->kernel)
  1178. return 0;
  1179. return 1;
  1180. }
  1181. static void write_dump(const char *fname)
  1182. {
  1183. struct buffer buf = { };
  1184. struct symbol *symbol;
  1185. int n;
  1186. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1187. symbol = symbolhash[n];
  1188. while (symbol) {
  1189. if (dump_sym(symbol))
  1190. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1191. symbol->crc, symbol->name,
  1192. symbol->module->name,
  1193. export_str(symbol->export));
  1194. symbol = symbol->next;
  1195. }
  1196. }
  1197. write_if_changed(&buf, fname);
  1198. }
  1199. int main(int argc, char **argv)
  1200. {
  1201. struct module *mod;
  1202. struct buffer buf = { };
  1203. char fname[SZ];
  1204. char *kernel_read = NULL, *module_read = NULL;
  1205. char *dump_write = NULL;
  1206. int opt;
  1207. while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
  1208. switch(opt) {
  1209. case 'i':
  1210. kernel_read = optarg;
  1211. break;
  1212. case 'I':
  1213. module_read = optarg;
  1214. external_module = 1;
  1215. break;
  1216. case 'm':
  1217. modversions = 1;
  1218. break;
  1219. case 'o':
  1220. dump_write = optarg;
  1221. break;
  1222. case 'a':
  1223. all_versions = 1;
  1224. break;
  1225. default:
  1226. exit(1);
  1227. }
  1228. }
  1229. if (kernel_read)
  1230. read_dump(kernel_read, 1);
  1231. if (module_read)
  1232. read_dump(module_read, 0);
  1233. while (optind < argc) {
  1234. read_symbols(argv[optind++]);
  1235. }
  1236. for (mod = modules; mod; mod = mod->next) {
  1237. if (mod->skip)
  1238. continue;
  1239. check_license(mod);
  1240. }
  1241. for (mod = modules; mod; mod = mod->next) {
  1242. if (mod->skip)
  1243. continue;
  1244. buf.pos = 0;
  1245. add_header(&buf, mod);
  1246. add_versions(&buf, mod);
  1247. add_depends(&buf, mod, modules);
  1248. add_moddevtable(&buf, mod);
  1249. add_srcversion(&buf, mod);
  1250. sprintf(fname, "%s.mod.c", mod->name);
  1251. write_if_changed(&buf, fname);
  1252. }
  1253. if (dump_write)
  1254. write_dump(dump_write);
  1255. return 0;
  1256. }