modpost.c 38 KB

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