modpost.c 35 KB

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