modpost.c 36 KB

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