modpost.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006-2008 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. /* Warn about section mismatch in vmlinux if set to 1 */
  25. static int vmlinux_section_warnings = 1;
  26. /* Only warn about unresolved symbols */
  27. static int warn_unresolved = 0;
  28. /* How a symbol is exported */
  29. static int sec_mismatch_count = 0;
  30. static int sec_mismatch_verbose = 1;
  31. enum export {
  32. export_plain, export_unused, export_gpl,
  33. export_unused_gpl, export_gpl_future, export_unknown
  34. };
  35. #define PRINTF __attribute__ ((format (printf, 1, 2)))
  36. PRINTF void fatal(const char *fmt, ...)
  37. {
  38. va_list arglist;
  39. fprintf(stderr, "FATAL: ");
  40. va_start(arglist, fmt);
  41. vfprintf(stderr, fmt, arglist);
  42. va_end(arglist);
  43. exit(1);
  44. }
  45. PRINTF void warn(const char *fmt, ...)
  46. {
  47. va_list arglist;
  48. fprintf(stderr, "WARNING: ");
  49. va_start(arglist, fmt);
  50. vfprintf(stderr, fmt, arglist);
  51. va_end(arglist);
  52. }
  53. PRINTF void merror(const char *fmt, ...)
  54. {
  55. va_list arglist;
  56. fprintf(stderr, "ERROR: ");
  57. va_start(arglist, fmt);
  58. vfprintf(stderr, fmt, arglist);
  59. va_end(arglist);
  60. }
  61. static int is_vmlinux(const char *modname)
  62. {
  63. const char *myname;
  64. myname = strrchr(modname, '/');
  65. if (myname)
  66. myname++;
  67. else
  68. myname = modname;
  69. return (strcmp(myname, "vmlinux") == 0) ||
  70. (strcmp(myname, "vmlinux.o") == 0);
  71. }
  72. void *do_nofail(void *ptr, const char *expr)
  73. {
  74. if (!ptr)
  75. fatal("modpost: Memory allocation failure: %s.\n", expr);
  76. return ptr;
  77. }
  78. /* A list of all modules we processed */
  79. static struct module *modules;
  80. static struct module *find_module(char *modname)
  81. {
  82. struct module *mod;
  83. for (mod = modules; mod; mod = mod->next)
  84. if (strcmp(mod->name, modname) == 0)
  85. break;
  86. return mod;
  87. }
  88. static struct module *new_module(char *modname)
  89. {
  90. struct module *mod;
  91. char *p, *s;
  92. mod = NOFAIL(malloc(sizeof(*mod)));
  93. memset(mod, 0, sizeof(*mod));
  94. p = NOFAIL(strdup(modname));
  95. /* strip trailing .o */
  96. s = strrchr(p, '.');
  97. if (s != NULL)
  98. if (strcmp(s, ".o") == 0)
  99. *s = '\0';
  100. /* add to list */
  101. mod->name = p;
  102. mod->gpl_compatible = -1;
  103. mod->next = modules;
  104. modules = mod;
  105. return mod;
  106. }
  107. /* A hash of all exported symbols,
  108. * struct symbol is also used for lists of unresolved symbols */
  109. #define SYMBOL_HASH_SIZE 1024
  110. struct symbol {
  111. struct symbol *next;
  112. struct module *module;
  113. unsigned int crc;
  114. int crc_valid;
  115. unsigned int weak:1;
  116. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  117. unsigned int kernel:1; /* 1 if symbol is from kernel
  118. * (only for external modules) **/
  119. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  120. enum export export; /* Type of export */
  121. char name[0];
  122. };
  123. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  124. /* This is based on the hash agorithm from gdbm, via tdb */
  125. static inline unsigned int tdb_hash(const char *name)
  126. {
  127. unsigned value; /* Used to compute the hash value. */
  128. unsigned i; /* Used to cycle through random values. */
  129. /* Set the initial value from the key size. */
  130. for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
  131. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  132. return (1103515243 * value + 12345);
  133. }
  134. /**
  135. * Allocate a new symbols for use in the hash of exported symbols or
  136. * the list of unresolved symbols per module
  137. **/
  138. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  139. struct symbol *next)
  140. {
  141. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  142. memset(s, 0, sizeof(*s));
  143. strcpy(s->name, name);
  144. s->weak = weak;
  145. s->next = next;
  146. return s;
  147. }
  148. /* For the hash of exported symbols */
  149. static struct symbol *new_symbol(const char *name, struct module *module,
  150. enum export export)
  151. {
  152. unsigned int hash;
  153. struct symbol *new;
  154. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  155. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  156. new->module = module;
  157. new->export = export;
  158. return new;
  159. }
  160. static struct symbol *find_symbol(const char *name)
  161. {
  162. struct symbol *s;
  163. /* For our purposes, .foo matches foo. PPC64 needs this. */
  164. if (name[0] == '.')
  165. name++;
  166. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
  167. if (strcmp(s->name, name) == 0)
  168. return s;
  169. }
  170. return NULL;
  171. }
  172. static struct {
  173. const char *str;
  174. enum export export;
  175. } export_list[] = {
  176. { .str = "EXPORT_SYMBOL", .export = export_plain },
  177. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  178. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  179. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  180. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  181. { .str = "(unknown)", .export = export_unknown },
  182. };
  183. static const char *export_str(enum export ex)
  184. {
  185. return export_list[ex].str;
  186. }
  187. static enum export export_no(const char *s)
  188. {
  189. int i;
  190. if (!s)
  191. return export_unknown;
  192. for (i = 0; export_list[i].export != export_unknown; i++) {
  193. if (strcmp(export_list[i].str, s) == 0)
  194. return export_list[i].export;
  195. }
  196. return export_unknown;
  197. }
  198. static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
  199. {
  200. if (sec == elf->export_sec)
  201. return export_plain;
  202. else if (sec == elf->export_unused_sec)
  203. return export_unused;
  204. else if (sec == elf->export_gpl_sec)
  205. return export_gpl;
  206. else if (sec == elf->export_unused_gpl_sec)
  207. return export_unused_gpl;
  208. else if (sec == elf->export_gpl_future_sec)
  209. return export_gpl_future;
  210. else
  211. return export_unknown;
  212. }
  213. /**
  214. * Add an exported symbol - it may have already been added without a
  215. * CRC, in this case just update the CRC
  216. **/
  217. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  218. enum export export)
  219. {
  220. struct symbol *s = find_symbol(name);
  221. if (!s) {
  222. s = new_symbol(name, mod, export);
  223. } else {
  224. if (!s->preloaded) {
  225. warn("%s: '%s' exported twice. Previous export "
  226. "was in %s%s\n", mod->name, name,
  227. s->module->name,
  228. is_vmlinux(s->module->name) ?"":".ko");
  229. } else {
  230. /* In case Modules.symvers was out of date */
  231. s->module = mod;
  232. }
  233. }
  234. s->preloaded = 0;
  235. s->vmlinux = is_vmlinux(mod->name);
  236. s->kernel = 0;
  237. s->export = export;
  238. return s;
  239. }
  240. static void sym_update_crc(const char *name, struct module *mod,
  241. unsigned int crc, enum export export)
  242. {
  243. struct symbol *s = find_symbol(name);
  244. if (!s)
  245. s = new_symbol(name, mod, export);
  246. s->crc = crc;
  247. s->crc_valid = 1;
  248. }
  249. void *grab_file(const char *filename, unsigned long *size)
  250. {
  251. struct stat st;
  252. void *map;
  253. int fd;
  254. fd = open(filename, O_RDONLY);
  255. if (fd < 0 || fstat(fd, &st) != 0)
  256. return NULL;
  257. *size = st.st_size;
  258. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  259. close(fd);
  260. if (map == MAP_FAILED)
  261. return NULL;
  262. return map;
  263. }
  264. /**
  265. * Return a copy of the next line in a mmap'ed file.
  266. * spaces in the beginning of the line is trimmed away.
  267. * Return a pointer to a static buffer.
  268. **/
  269. char *get_next_line(unsigned long *pos, void *file, unsigned long size)
  270. {
  271. static char line[4096];
  272. int skip = 1;
  273. size_t len = 0;
  274. signed char *p = (signed char *)file + *pos;
  275. char *s = line;
  276. for (; *pos < size ; (*pos)++) {
  277. if (skip && isspace(*p)) {
  278. p++;
  279. continue;
  280. }
  281. skip = 0;
  282. if (*p != '\n' && (*pos < size)) {
  283. len++;
  284. *s++ = *p++;
  285. if (len > 4095)
  286. break; /* Too long, stop */
  287. } else {
  288. /* End of string */
  289. *s = '\0';
  290. return line;
  291. }
  292. }
  293. /* End of buffer */
  294. return NULL;
  295. }
  296. void release_file(void *file, unsigned long size)
  297. {
  298. munmap(file, size);
  299. }
  300. static int parse_elf(struct elf_info *info, const char *filename)
  301. {
  302. unsigned int i;
  303. Elf_Ehdr *hdr;
  304. Elf_Shdr *sechdrs;
  305. Elf_Sym *sym;
  306. hdr = grab_file(filename, &info->size);
  307. if (!hdr) {
  308. perror(filename);
  309. exit(1);
  310. }
  311. info->hdr = hdr;
  312. if (info->size < sizeof(*hdr)) {
  313. /* file too small, assume this is an empty .o file */
  314. return 0;
  315. }
  316. /* Is this a valid ELF file? */
  317. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  318. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  319. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  320. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  321. /* Not an ELF file - silently ignore it */
  322. return 0;
  323. }
  324. /* Fix endianness in ELF header */
  325. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  326. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  327. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  328. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  329. hdr->e_type = TO_NATIVE(hdr->e_type);
  330. sechdrs = (void *)hdr + hdr->e_shoff;
  331. info->sechdrs = sechdrs;
  332. /* Check if file offset is correct */
  333. if (hdr->e_shoff > info->size) {
  334. fatal("section header offset=%lu in file '%s' is bigger than "
  335. "filesize=%lu\n", (unsigned long)hdr->e_shoff,
  336. filename, info->size);
  337. return 0;
  338. }
  339. /* Fix endianness in section headers */
  340. for (i = 0; i < hdr->e_shnum; i++) {
  341. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  342. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  343. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  344. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  345. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  346. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  347. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  348. }
  349. /* Find symbol table. */
  350. for (i = 1; i < hdr->e_shnum; i++) {
  351. const char *secstrings
  352. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  353. const char *secname;
  354. if (sechdrs[i].sh_offset > info->size) {
  355. fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
  356. "sizeof(*hrd)=%zu\n", filename,
  357. (unsigned long)sechdrs[i].sh_offset,
  358. sizeof(*hdr));
  359. return 0;
  360. }
  361. secname = secstrings + sechdrs[i].sh_name;
  362. if (strcmp(secname, ".modinfo") == 0) {
  363. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  364. info->modinfo_len = sechdrs[i].sh_size;
  365. } else if (strcmp(secname, "__ksymtab") == 0)
  366. info->export_sec = i;
  367. else if (strcmp(secname, "__ksymtab_unused") == 0)
  368. info->export_unused_sec = i;
  369. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  370. info->export_gpl_sec = i;
  371. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  372. info->export_unused_gpl_sec = i;
  373. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  374. info->export_gpl_future_sec = i;
  375. if (sechdrs[i].sh_type != SHT_SYMTAB)
  376. continue;
  377. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  378. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  379. + sechdrs[i].sh_size;
  380. info->strtab = (void *)hdr +
  381. sechdrs[sechdrs[i].sh_link].sh_offset;
  382. }
  383. if (!info->symtab_start)
  384. fatal("%s has no symtab?\n", filename);
  385. /* Fix endianness in symbols */
  386. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  387. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  388. sym->st_name = TO_NATIVE(sym->st_name);
  389. sym->st_value = TO_NATIVE(sym->st_value);
  390. sym->st_size = TO_NATIVE(sym->st_size);
  391. }
  392. return 1;
  393. }
  394. static void parse_elf_finish(struct elf_info *info)
  395. {
  396. release_file(info->hdr, info->size);
  397. }
  398. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  399. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  400. static void handle_modversions(struct module *mod, struct elf_info *info,
  401. Elf_Sym *sym, const char *symname)
  402. {
  403. unsigned int crc;
  404. enum export export = export_from_sec(info, sym->st_shndx);
  405. switch (sym->st_shndx) {
  406. case SHN_COMMON:
  407. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  408. break;
  409. case SHN_ABS:
  410. /* CRC'd symbol */
  411. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  412. crc = (unsigned int) sym->st_value;
  413. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  414. export);
  415. }
  416. break;
  417. case SHN_UNDEF:
  418. /* undefined symbol */
  419. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  420. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  421. break;
  422. /* ignore global offset table */
  423. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  424. break;
  425. /* ignore __this_module, it will be resolved shortly */
  426. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  427. break;
  428. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  429. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  430. /* add compatibility with older glibc */
  431. #ifndef STT_SPARC_REGISTER
  432. #define STT_SPARC_REGISTER STT_REGISTER
  433. #endif
  434. if (info->hdr->e_machine == EM_SPARC ||
  435. info->hdr->e_machine == EM_SPARCV9) {
  436. /* Ignore register directives. */
  437. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  438. break;
  439. if (symname[0] == '.') {
  440. char *munged = strdup(symname);
  441. munged[0] = '_';
  442. munged[1] = toupper(munged[1]);
  443. symname = munged;
  444. }
  445. }
  446. #endif
  447. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  448. strlen(MODULE_SYMBOL_PREFIX)) == 0) {
  449. mod->unres =
  450. alloc_symbol(symname +
  451. strlen(MODULE_SYMBOL_PREFIX),
  452. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  453. mod->unres);
  454. }
  455. break;
  456. default:
  457. /* All exported symbols */
  458. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  459. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  460. export);
  461. }
  462. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  463. mod->has_init = 1;
  464. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  465. mod->has_cleanup = 1;
  466. break;
  467. }
  468. }
  469. /**
  470. * Parse tag=value strings from .modinfo section
  471. **/
  472. static char *next_string(char *string, unsigned long *secsize)
  473. {
  474. /* Skip non-zero chars */
  475. while (string[0]) {
  476. string++;
  477. if ((*secsize)-- <= 1)
  478. return NULL;
  479. }
  480. /* Skip any zero padding. */
  481. while (!string[0]) {
  482. string++;
  483. if ((*secsize)-- <= 1)
  484. return NULL;
  485. }
  486. return string;
  487. }
  488. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  489. const char *tag, char *info)
  490. {
  491. char *p;
  492. unsigned int taglen = strlen(tag);
  493. unsigned long size = modinfo_len;
  494. if (info) {
  495. size -= info - (char *)modinfo;
  496. modinfo = next_string(info, &size);
  497. }
  498. for (p = modinfo; p; p = next_string(p, &size)) {
  499. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  500. return p + taglen + 1;
  501. }
  502. return NULL;
  503. }
  504. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  505. const char *tag)
  506. {
  507. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  508. }
  509. /**
  510. * Test if string s ends in string sub
  511. * return 0 if match
  512. **/
  513. static int strrcmp(const char *s, const char *sub)
  514. {
  515. int slen, sublen;
  516. if (!s || !sub)
  517. return 1;
  518. slen = strlen(s);
  519. sublen = strlen(sub);
  520. if ((slen == 0) || (sublen == 0))
  521. return 1;
  522. if (sublen > slen)
  523. return 1;
  524. return memcmp(s + slen - sublen, sub, sublen);
  525. }
  526. static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
  527. {
  528. if (sym)
  529. return elf->strtab + sym->st_name;
  530. else
  531. return "";
  532. }
  533. static const char *sec_name(struct elf_info *elf, int shndx)
  534. {
  535. Elf_Shdr *sechdrs = elf->sechdrs;
  536. return (void *)elf->hdr +
  537. elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
  538. sechdrs[shndx].sh_name;
  539. }
  540. static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
  541. {
  542. return (void *)elf->hdr +
  543. elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
  544. sechdr->sh_name;
  545. }
  546. /* if sym is empty or point to a string
  547. * like ".[0-9]+" then return 1.
  548. * This is the optional prefix added by ld to some sections
  549. */
  550. static int number_prefix(const char *sym)
  551. {
  552. if (*sym++ == '\0')
  553. return 1;
  554. if (*sym != '.')
  555. return 0;
  556. do {
  557. char c = *sym++;
  558. if (c < '0' || c > '9')
  559. return 0;
  560. } while (*sym);
  561. return 1;
  562. }
  563. /* The pattern is an array of simple patterns.
  564. * "foo" will match an exact string equal to "foo"
  565. * "*foo" will match a string that ends with "foo"
  566. * "foo*" will match a string that begins with "foo"
  567. * "foo$" will match a string equal to "foo" or "foo.1"
  568. * where the '1' can be any number including several digits.
  569. * The $ syntax is for sections where ld append a dot number
  570. * to make section name unique.
  571. */
  572. int match(const char *sym, const char * const pat[])
  573. {
  574. const char *p;
  575. while (*pat) {
  576. p = *pat++;
  577. const char *endp = p + strlen(p) - 1;
  578. /* "*foo" */
  579. if (*p == '*') {
  580. if (strrcmp(sym, p + 1) == 0)
  581. return 1;
  582. }
  583. /* "foo*" */
  584. else if (*endp == '*') {
  585. if (strncmp(sym, p, strlen(p) - 1) == 0)
  586. return 1;
  587. }
  588. /* "foo$" */
  589. else if (*endp == '$') {
  590. if (strncmp(sym, p, strlen(p) - 1) == 0) {
  591. if (number_prefix(sym + strlen(p) - 1))
  592. return 1;
  593. }
  594. }
  595. /* no wildcards */
  596. else {
  597. if (strcmp(p, sym) == 0)
  598. return 1;
  599. }
  600. }
  601. /* no match */
  602. return 0;
  603. }
  604. /* sections that we do not want to do full section mismatch check on */
  605. static const char *section_white_list[] =
  606. { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
  607. /*
  608. * Is this section one we do not want to check?
  609. * This is often debug sections.
  610. * If we are going to check this section then
  611. * test if section name ends with a dot and a number.
  612. * This is used to find sections where the linker have
  613. * appended a dot-number to make the name unique.
  614. * The cause of this is often a section specified in assembler
  615. * without "ax" / "aw" and the same section used in .c
  616. * code where gcc add these.
  617. */
  618. static int check_section(const char *modname, const char *sec)
  619. {
  620. const char *e = sec + strlen(sec) - 1;
  621. if (match(sec, section_white_list))
  622. return 1;
  623. if (*e && isdigit(*e)) {
  624. /* consume all digits */
  625. while (*e && e != sec && isdigit(*e))
  626. e--;
  627. if (*e == '.') {
  628. warn("%s (%s): unexpected section name.\n"
  629. "The (.[number]+) following section name are "
  630. "ld generated and not expected.\n"
  631. "Did you forget to use \"ax\"/\"aw\" "
  632. "in a .S file?\n"
  633. "Note that for example <linux/init.h> contains\n"
  634. "section definitions for use in .S files.\n\n",
  635. modname, sec);
  636. }
  637. }
  638. return 0;
  639. }
  640. #define ALL_INIT_DATA_SECTIONS \
  641. ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
  642. #define ALL_EXIT_DATA_SECTIONS \
  643. ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
  644. #define ALL_INIT_TEXT_SECTIONS \
  645. ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
  646. #define ALL_EXIT_TEXT_SECTIONS \
  647. ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
  648. #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
  649. #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
  650. #define DATA_SECTIONS ".data$", ".data.rel$"
  651. #define TEXT_SECTIONS ".text$"
  652. #define INIT_SECTIONS ".init.data$", ".init.text$"
  653. #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
  654. #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
  655. #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
  656. #define EXIT_SECTIONS ".exit.data$", ".exit.text$"
  657. #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
  658. #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
  659. #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
  660. /* init data sections */
  661. static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
  662. /* all init sections */
  663. static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
  664. /* All init and exit sections (code + data) */
  665. static const char *init_exit_sections[] =
  666. {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
  667. /* data section */
  668. static const char *data_sections[] = { DATA_SECTIONS, NULL };
  669. /* sections that may refer to an init/exit section with no warning */
  670. static const char *initref_sections[] =
  671. {
  672. ".text.init.refok*",
  673. ".exit.text.refok*",
  674. ".data.init.refok*",
  675. NULL
  676. };
  677. /* symbols in .data that may refer to init/exit sections */
  678. static const char *symbol_white_list[] =
  679. {
  680. "*driver",
  681. "*_template", /* scsi uses *_template a lot */
  682. "*_timer", /* arm uses ops structures named _timer a lot */
  683. "*_sht", /* scsi also used *_sht to some extent */
  684. "*_ops",
  685. "*_probe",
  686. "*_probe_one",
  687. "*_console",
  688. NULL
  689. };
  690. static const char *head_sections[] = { ".head.text*", NULL };
  691. static const char *linker_symbols[] =
  692. { "__init_begin", "_sinittext", "_einittext", NULL };
  693. enum mismatch {
  694. NO_MISMATCH,
  695. TEXT_TO_INIT,
  696. DATA_TO_INIT,
  697. TEXT_TO_EXIT,
  698. DATA_TO_EXIT,
  699. XXXINIT_TO_INIT,
  700. XXXEXIT_TO_EXIT,
  701. INIT_TO_EXIT,
  702. EXIT_TO_INIT,
  703. EXPORT_TO_INIT_EXIT,
  704. };
  705. struct sectioncheck {
  706. const char *fromsec[20];
  707. const char *tosec[20];
  708. enum mismatch mismatch;
  709. };
  710. const struct sectioncheck sectioncheck[] = {
  711. /* Do not reference init/exit code/data from
  712. * normal code and data
  713. */
  714. {
  715. .fromsec = { TEXT_SECTIONS, NULL },
  716. .tosec = { ALL_INIT_SECTIONS, NULL },
  717. .mismatch = TEXT_TO_INIT,
  718. },
  719. {
  720. .fromsec = { DATA_SECTIONS, NULL },
  721. .tosec = { ALL_INIT_SECTIONS, NULL },
  722. .mismatch = DATA_TO_INIT,
  723. },
  724. {
  725. .fromsec = { TEXT_SECTIONS, NULL },
  726. .tosec = { ALL_EXIT_SECTIONS, NULL },
  727. .mismatch = TEXT_TO_EXIT,
  728. },
  729. {
  730. .fromsec = { DATA_SECTIONS, NULL },
  731. .tosec = { ALL_EXIT_SECTIONS, NULL },
  732. .mismatch = DATA_TO_EXIT,
  733. },
  734. /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
  735. {
  736. .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
  737. .tosec = { INIT_SECTIONS, NULL },
  738. .mismatch = XXXINIT_TO_INIT,
  739. },
  740. /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
  741. {
  742. .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
  743. .tosec = { EXIT_SECTIONS, NULL },
  744. .mismatch = XXXEXIT_TO_EXIT,
  745. },
  746. /* Do not use exit code/data from init code */
  747. {
  748. .fromsec = { ALL_INIT_SECTIONS, NULL },
  749. .tosec = { ALL_EXIT_SECTIONS, NULL },
  750. .mismatch = INIT_TO_EXIT,
  751. },
  752. /* Do not use init code/data from exit code */
  753. {
  754. .fromsec = { ALL_EXIT_SECTIONS, NULL },
  755. .tosec = { ALL_INIT_SECTIONS, NULL },
  756. .mismatch = EXIT_TO_INIT,
  757. },
  758. /* Do not export init/exit functions or data */
  759. {
  760. .fromsec = { "__ksymtab*", NULL },
  761. .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
  762. .mismatch = EXPORT_TO_INIT_EXIT
  763. }
  764. };
  765. static int section_mismatch(const char *fromsec, const char *tosec)
  766. {
  767. int i;
  768. int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
  769. const struct sectioncheck *check = &sectioncheck[0];
  770. for (i = 0; i < elems; i++) {
  771. if (match(fromsec, check->fromsec) &&
  772. match(tosec, check->tosec))
  773. return check->mismatch;
  774. check++;
  775. }
  776. return NO_MISMATCH;
  777. }
  778. /**
  779. * Whitelist to allow certain references to pass with no warning.
  780. *
  781. * Pattern 0:
  782. * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
  783. * The pattern is identified by:
  784. * fromsec = .text.init.refok* | .data.init.refok*
  785. *
  786. * Pattern 1:
  787. * If a module parameter is declared __initdata and permissions=0
  788. * then this is legal despite the warning generated.
  789. * We cannot see value of permissions here, so just ignore
  790. * this pattern.
  791. * The pattern is identified by:
  792. * tosec = .init.data
  793. * fromsec = .data*
  794. * atsym =__param*
  795. *
  796. * Pattern 2:
  797. * Many drivers utilise a *driver container with references to
  798. * add, remove, probe functions etc.
  799. * These functions may often be marked __init and we do not want to
  800. * warn here.
  801. * the pattern is identified by:
  802. * tosec = init or exit section
  803. * fromsec = data section
  804. * atsym = *driver, *_template, *_sht, *_ops, *_probe,
  805. * *probe_one, *_console, *_timer
  806. *
  807. * Pattern 3:
  808. * Whitelist all refereces from .text.head to .init.data
  809. * Whitelist all refereces from .text.head to .init.text
  810. *
  811. * Pattern 4:
  812. * Some symbols belong to init section but still it is ok to reference
  813. * these from non-init sections as these symbols don't have any memory
  814. * allocated for them and symbol address and value are same. So even
  815. * if init section is freed, its ok to reference those symbols.
  816. * For ex. symbols marking the init section boundaries.
  817. * This pattern is identified by
  818. * refsymname = __init_begin, _sinittext, _einittext
  819. *
  820. **/
  821. static int secref_whitelist(const char *fromsec, const char *fromsym,
  822. const char *tosec, const char *tosym)
  823. {
  824. /* Check for pattern 0 */
  825. if (match(fromsec, initref_sections))
  826. return 0;
  827. /* Check for pattern 1 */
  828. if (match(tosec, init_data_sections) &&
  829. match(fromsec, data_sections) &&
  830. (strncmp(fromsym, "__param", strlen("__param")) == 0))
  831. return 0;
  832. /* Check for pattern 2 */
  833. if (match(tosec, init_exit_sections) &&
  834. match(fromsec, data_sections) &&
  835. match(fromsym, symbol_white_list))
  836. return 0;
  837. /* Check for pattern 3 */
  838. if (match(fromsec, head_sections) &&
  839. match(tosec, init_sections))
  840. return 0;
  841. /* Check for pattern 4 */
  842. if (match(tosym, linker_symbols))
  843. return 0;
  844. return 1;
  845. }
  846. /**
  847. * Find symbol based on relocation record info.
  848. * In some cases the symbol supplied is a valid symbol so
  849. * return refsym. If st_name != 0 we assume this is a valid symbol.
  850. * In other cases the symbol needs to be looked up in the symbol table
  851. * based on section and address.
  852. * **/
  853. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
  854. Elf_Sym *relsym)
  855. {
  856. Elf_Sym *sym;
  857. Elf_Sym *near = NULL;
  858. Elf64_Sword distance = 20;
  859. Elf64_Sword d;
  860. if (relsym->st_name != 0)
  861. return relsym;
  862. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  863. if (sym->st_shndx != relsym->st_shndx)
  864. continue;
  865. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  866. continue;
  867. if (sym->st_value == addr)
  868. return sym;
  869. /* Find a symbol nearby - addr are maybe negative */
  870. d = sym->st_value - addr;
  871. if (d < 0)
  872. d = addr - sym->st_value;
  873. if (d < distance) {
  874. distance = d;
  875. near = sym;
  876. }
  877. }
  878. /* We need a close match */
  879. if (distance < 20)
  880. return near;
  881. else
  882. return NULL;
  883. }
  884. static inline int is_arm_mapping_symbol(const char *str)
  885. {
  886. return str[0] == '$' && strchr("atd", str[1])
  887. && (str[2] == '\0' || str[2] == '.');
  888. }
  889. /*
  890. * If there's no name there, ignore it; likewise, ignore it if it's
  891. * one of the magic symbols emitted used by current ARM tools.
  892. *
  893. * Otherwise if find_symbols_between() returns those symbols, they'll
  894. * fail the whitelist tests and cause lots of false alarms ... fixable
  895. * only by merging __exit and __init sections into __text, bloating
  896. * the kernel (which is especially evil on embedded platforms).
  897. */
  898. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  899. {
  900. const char *name = elf->strtab + sym->st_name;
  901. if (!name || !strlen(name))
  902. return 0;
  903. return !is_arm_mapping_symbol(name);
  904. }
  905. /*
  906. * Find symbols before or equal addr and after addr - in the section sec.
  907. * If we find two symbols with equal offset prefer one with a valid name.
  908. * The ELF format may have a better way to detect what type of symbol
  909. * it is, but this works for now.
  910. **/
  911. static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
  912. const char *sec)
  913. {
  914. Elf_Sym *sym;
  915. Elf_Sym *near = NULL;
  916. Elf_Addr distance = ~0;
  917. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  918. const char *symsec;
  919. if (sym->st_shndx >= SHN_LORESERVE)
  920. continue;
  921. symsec = sec_name(elf, sym->st_shndx);
  922. if (strcmp(symsec, sec) != 0)
  923. continue;
  924. if (!is_valid_name(elf, sym))
  925. continue;
  926. if (sym->st_value <= addr) {
  927. if ((addr - sym->st_value) < distance) {
  928. distance = addr - sym->st_value;
  929. near = sym;
  930. } else if ((addr - sym->st_value) == distance) {
  931. near = sym;
  932. }
  933. }
  934. }
  935. return near;
  936. }
  937. /*
  938. * Convert a section name to the function/data attribute
  939. * .init.text => __init
  940. * .cpuinit.data => __cpudata
  941. * .memexitconst => __memconst
  942. * etc.
  943. */
  944. static char *sec2annotation(const char *s)
  945. {
  946. if (match(s, init_exit_sections)) {
  947. char *p = malloc(20);
  948. char *r = p;
  949. *p++ = '_';
  950. *p++ = '_';
  951. if (*s == '.')
  952. s++;
  953. while (*s && *s != '.')
  954. *p++ = *s++;
  955. *p = '\0';
  956. if (*s == '.')
  957. s++;
  958. if (strstr(s, "rodata") != NULL)
  959. strcat(p, "const ");
  960. else if (strstr(s, "data") != NULL)
  961. strcat(p, "data ");
  962. else
  963. strcat(p, " ");
  964. return r; /* we leak her but we do not care */
  965. } else {
  966. return "";
  967. }
  968. }
  969. static int is_function(Elf_Sym *sym)
  970. {
  971. if (sym)
  972. return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
  973. else
  974. return 0;
  975. }
  976. /*
  977. * Print a warning about a section mismatch.
  978. * Try to find symbols near it so user can find it.
  979. * Check whitelist before warning - it may be a false positive.
  980. */
  981. static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
  982. const char *fromsec,
  983. unsigned long long fromaddr,
  984. const char *fromsym,
  985. int from_is_func,
  986. const char *tosec, const char *tosym,
  987. int to_is_func)
  988. {
  989. const char *from, *from_p;
  990. const char *to, *to_p;
  991. from = from_is_func ? "function" : "variable";
  992. from_p = from_is_func ? "()" : "";
  993. to = to_is_func ? "function" : "variable";
  994. to_p = to_is_func ? "()" : "";
  995. sec_mismatch_count++;
  996. if (!sec_mismatch_verbose)
  997. return;
  998. fprintf(stderr, "WARNING: %s(%s+0x%llx): Section mismatch in"
  999. " reference from the %s %s%s to the %s %s:%s%s\n",
  1000. modname, fromsec, fromaddr, from, fromsym, from_p,
  1001. to, tosec, tosym, to_p);
  1002. switch (mismatch) {
  1003. case TEXT_TO_INIT:
  1004. fprintf(stderr,
  1005. "The function %s %s() references\n"
  1006. "the %s %s%s%s.\n"
  1007. "This is often because %s lacks a %s\n"
  1008. "annotation or the annotation of %s is wrong.\n",
  1009. sec2annotation(fromsec), fromsym,
  1010. to, sec2annotation(tosec), tosym, to_p,
  1011. fromsym, sec2annotation(tosec), tosym);
  1012. break;
  1013. case DATA_TO_INIT: {
  1014. const char **s = symbol_white_list;
  1015. fprintf(stderr,
  1016. "The variable %s references\n"
  1017. "the %s %s%s%s\n"
  1018. "If the reference is valid then annotate the\n"
  1019. "variable with __init* (see linux/init.h) "
  1020. "or name the variable:\n",
  1021. fromsym, to, sec2annotation(tosec), tosym, to_p);
  1022. while (*s)
  1023. fprintf(stderr, "%s, ", *s++);
  1024. fprintf(stderr, "\n");
  1025. break;
  1026. }
  1027. case TEXT_TO_EXIT:
  1028. fprintf(stderr,
  1029. "The function %s() references a %s in an exit section.\n"
  1030. "Often the %s %s%s has valid usage outside the exit section\n"
  1031. "and the fix is to remove the %sannotation of %s.\n",
  1032. fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
  1033. break;
  1034. case DATA_TO_EXIT: {
  1035. const char **s = symbol_white_list;
  1036. fprintf(stderr,
  1037. "The variable %s references\n"
  1038. "the %s %s%s%s\n"
  1039. "If the reference is valid then annotate the\n"
  1040. "variable with __exit* (see linux/init.h) or "
  1041. "name the variable:\n",
  1042. fromsym, to, sec2annotation(tosec), tosym, to_p);
  1043. while (*s)
  1044. fprintf(stderr, "%s, ", *s++);
  1045. fprintf(stderr, "\n");
  1046. break;
  1047. }
  1048. case XXXINIT_TO_INIT:
  1049. case XXXEXIT_TO_EXIT:
  1050. fprintf(stderr,
  1051. "The %s %s%s%s references\n"
  1052. "a %s %s%s%s.\n"
  1053. "If %s is only used by %s then\n"
  1054. "annotate %s with a matching annotation.\n",
  1055. from, sec2annotation(fromsec), fromsym, from_p,
  1056. to, sec2annotation(tosec), tosym, to_p,
  1057. fromsym, tosym, fromsym);
  1058. break;
  1059. case INIT_TO_EXIT:
  1060. fprintf(stderr,
  1061. "The %s %s%s%s references\n"
  1062. "a %s %s%s%s.\n"
  1063. "This is often seen when error handling "
  1064. "in the init function\n"
  1065. "uses functionality in the exit path.\n"
  1066. "The fix is often to remove the %sannotation of\n"
  1067. "%s%s so it may be used outside an exit section.\n",
  1068. from, sec2annotation(fromsec), fromsym, from_p,
  1069. to, sec2annotation(tosec), tosym, to_p,
  1070. sec2annotation(tosec), tosym, to_p);
  1071. break;
  1072. case EXIT_TO_INIT:
  1073. fprintf(stderr,
  1074. "The %s %s%s%s references\n"
  1075. "a %s %s%s%s.\n"
  1076. "This is often seen when error handling "
  1077. "in the exit function\n"
  1078. "uses functionality in the init path.\n"
  1079. "The fix is often to remove the %sannotation of\n"
  1080. "%s%s so it may be used outside an init section.\n",
  1081. from, sec2annotation(fromsec), fromsym, from_p,
  1082. to, sec2annotation(tosec), tosym, to_p,
  1083. sec2annotation(tosec), tosym, to_p);
  1084. break;
  1085. case EXPORT_TO_INIT_EXIT:
  1086. fprintf(stderr,
  1087. "The symbol %s is exported and annotated %s\n"
  1088. "Fix this by removing the %sannotation of %s "
  1089. "or drop the export.\n",
  1090. tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
  1091. case NO_MISMATCH:
  1092. /* To get warnings on missing members */
  1093. break;
  1094. }
  1095. fprintf(stderr, "\n");
  1096. }
  1097. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  1098. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1099. {
  1100. const char *tosec;
  1101. enum mismatch mismatch;
  1102. tosec = sec_name(elf, sym->st_shndx);
  1103. mismatch = section_mismatch(fromsec, tosec);
  1104. if (mismatch != NO_MISMATCH) {
  1105. Elf_Sym *to;
  1106. Elf_Sym *from;
  1107. const char *tosym;
  1108. const char *fromsym;
  1109. from = find_elf_symbol2(elf, r->r_offset, fromsec);
  1110. fromsym = sym_name(elf, from);
  1111. to = find_elf_symbol(elf, r->r_addend, sym);
  1112. tosym = sym_name(elf, to);
  1113. /* check whitelist - we may ignore it */
  1114. if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
  1115. report_sec_mismatch(modname, mismatch,
  1116. fromsec, r->r_offset, fromsym,
  1117. is_function(from), tosec, tosym,
  1118. is_function(to));
  1119. }
  1120. }
  1121. }
  1122. static unsigned int *reloc_location(struct elf_info *elf,
  1123. Elf_Shdr *sechdr, Elf_Rela *r)
  1124. {
  1125. Elf_Shdr *sechdrs = elf->sechdrs;
  1126. int section = sechdr->sh_info;
  1127. return (void *)elf->hdr + sechdrs[section].sh_offset +
  1128. (r->r_offset - sechdrs[section].sh_addr);
  1129. }
  1130. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1131. {
  1132. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1133. unsigned int *location = reloc_location(elf, sechdr, r);
  1134. switch (r_typ) {
  1135. case R_386_32:
  1136. r->r_addend = TO_NATIVE(*location);
  1137. break;
  1138. case R_386_PC32:
  1139. r->r_addend = TO_NATIVE(*location) + 4;
  1140. /* For CONFIG_RELOCATABLE=y */
  1141. if (elf->hdr->e_type == ET_EXEC)
  1142. r->r_addend += r->r_offset;
  1143. break;
  1144. }
  1145. return 0;
  1146. }
  1147. static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1148. {
  1149. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1150. switch (r_typ) {
  1151. case R_ARM_ABS32:
  1152. /* From ARM ABI: (S + A) | T */
  1153. r->r_addend = (int)(long)
  1154. (elf->symtab_start + ELF_R_SYM(r->r_info));
  1155. break;
  1156. case R_ARM_PC24:
  1157. /* From ARM ABI: ((S + A) | T) - P */
  1158. r->r_addend = (int)(long)(elf->hdr +
  1159. sechdr->sh_offset +
  1160. (r->r_offset - sechdr->sh_addr));
  1161. break;
  1162. default:
  1163. return 1;
  1164. }
  1165. return 0;
  1166. }
  1167. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1168. {
  1169. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1170. unsigned int *location = reloc_location(elf, sechdr, r);
  1171. unsigned int inst;
  1172. if (r_typ == R_MIPS_HI16)
  1173. return 1; /* skip this */
  1174. inst = TO_NATIVE(*location);
  1175. switch (r_typ) {
  1176. case R_MIPS_LO16:
  1177. r->r_addend = inst & 0xffff;
  1178. break;
  1179. case R_MIPS_26:
  1180. r->r_addend = (inst & 0x03ffffff) << 2;
  1181. break;
  1182. case R_MIPS_32:
  1183. r->r_addend = inst;
  1184. break;
  1185. }
  1186. return 0;
  1187. }
  1188. static void section_rela(const char *modname, struct elf_info *elf,
  1189. Elf_Shdr *sechdr)
  1190. {
  1191. Elf_Sym *sym;
  1192. Elf_Rela *rela;
  1193. Elf_Rela r;
  1194. unsigned int r_sym;
  1195. const char *fromsec;
  1196. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  1197. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  1198. fromsec = sech_name(elf, sechdr);
  1199. fromsec += strlen(".rela");
  1200. /* if from section (name) is know good then skip it */
  1201. if (check_section(modname, fromsec))
  1202. return;
  1203. for (rela = start; rela < stop; rela++) {
  1204. r.r_offset = TO_NATIVE(rela->r_offset);
  1205. #if KERNEL_ELFCLASS == ELFCLASS64
  1206. if (elf->hdr->e_machine == EM_MIPS) {
  1207. unsigned int r_typ;
  1208. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  1209. r_sym = TO_NATIVE(r_sym);
  1210. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  1211. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1212. } else {
  1213. r.r_info = TO_NATIVE(rela->r_info);
  1214. r_sym = ELF_R_SYM(r.r_info);
  1215. }
  1216. #else
  1217. r.r_info = TO_NATIVE(rela->r_info);
  1218. r_sym = ELF_R_SYM(r.r_info);
  1219. #endif
  1220. r.r_addend = TO_NATIVE(rela->r_addend);
  1221. sym = elf->symtab_start + r_sym;
  1222. /* Skip special sections */
  1223. if (sym->st_shndx >= SHN_LORESERVE)
  1224. continue;
  1225. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1226. }
  1227. }
  1228. static void section_rel(const char *modname, struct elf_info *elf,
  1229. Elf_Shdr *sechdr)
  1230. {
  1231. Elf_Sym *sym;
  1232. Elf_Rel *rel;
  1233. Elf_Rela r;
  1234. unsigned int r_sym;
  1235. const char *fromsec;
  1236. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1237. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1238. fromsec = sech_name(elf, sechdr);
  1239. fromsec += strlen(".rel");
  1240. /* if from section (name) is know good then skip it */
  1241. if (check_section(modname, fromsec))
  1242. return;
  1243. for (rel = start; rel < stop; rel++) {
  1244. r.r_offset = TO_NATIVE(rel->r_offset);
  1245. #if KERNEL_ELFCLASS == ELFCLASS64
  1246. if (elf->hdr->e_machine == EM_MIPS) {
  1247. unsigned int r_typ;
  1248. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1249. r_sym = TO_NATIVE(r_sym);
  1250. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1251. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1252. } else {
  1253. r.r_info = TO_NATIVE(rel->r_info);
  1254. r_sym = ELF_R_SYM(r.r_info);
  1255. }
  1256. #else
  1257. r.r_info = TO_NATIVE(rel->r_info);
  1258. r_sym = ELF_R_SYM(r.r_info);
  1259. #endif
  1260. r.r_addend = 0;
  1261. switch (elf->hdr->e_machine) {
  1262. case EM_386:
  1263. if (addend_386_rel(elf, sechdr, &r))
  1264. continue;
  1265. break;
  1266. case EM_ARM:
  1267. if (addend_arm_rel(elf, sechdr, &r))
  1268. continue;
  1269. break;
  1270. case EM_MIPS:
  1271. if (addend_mips_rel(elf, sechdr, &r))
  1272. continue;
  1273. break;
  1274. }
  1275. sym = elf->symtab_start + r_sym;
  1276. /* Skip special sections */
  1277. if (sym->st_shndx >= SHN_LORESERVE)
  1278. continue;
  1279. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1280. }
  1281. }
  1282. /**
  1283. * A module includes a number of sections that are discarded
  1284. * either when loaded or when used as built-in.
  1285. * For loaded modules all functions marked __init and all data
  1286. * marked __initdata will be discarded when the module has been intialized.
  1287. * Likewise for modules used built-in the sections marked __exit
  1288. * are discarded because __exit marked function are supposed to be called
  1289. * only when a moduel is unloaded which never happes for built-in modules.
  1290. * The check_sec_ref() function traverses all relocation records
  1291. * to find all references to a section that reference a section that will
  1292. * be discarded and warns about it.
  1293. **/
  1294. static void check_sec_ref(struct module *mod, const char *modname,
  1295. struct elf_info *elf)
  1296. {
  1297. int i;
  1298. Elf_Shdr *sechdrs = elf->sechdrs;
  1299. /* Walk through all sections */
  1300. for (i = 0; i < elf->hdr->e_shnum; i++) {
  1301. /* We want to process only relocation sections and not .init */
  1302. if (sechdrs[i].sh_type == SHT_RELA)
  1303. section_rela(modname, elf, &elf->sechdrs[i]);
  1304. else if (sechdrs[i].sh_type == SHT_REL)
  1305. section_rel(modname, elf, &elf->sechdrs[i]);
  1306. }
  1307. }
  1308. static void read_symbols(char *modname)
  1309. {
  1310. const char *symname;
  1311. char *version;
  1312. char *license;
  1313. struct module *mod;
  1314. struct elf_info info = { };
  1315. Elf_Sym *sym;
  1316. if (!parse_elf(&info, modname))
  1317. return;
  1318. mod = new_module(modname);
  1319. /* When there's no vmlinux, don't print warnings about
  1320. * unresolved symbols (since there'll be too many ;) */
  1321. if (is_vmlinux(modname)) {
  1322. have_vmlinux = 1;
  1323. mod->skip = 1;
  1324. }
  1325. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1326. while (license) {
  1327. if (license_is_gpl_compatible(license))
  1328. mod->gpl_compatible = 1;
  1329. else {
  1330. mod->gpl_compatible = 0;
  1331. break;
  1332. }
  1333. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1334. "license", license);
  1335. }
  1336. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1337. symname = info.strtab + sym->st_name;
  1338. handle_modversions(mod, &info, sym, symname);
  1339. handle_moddevtable(mod, &info, sym, symname);
  1340. }
  1341. if (!is_vmlinux(modname) ||
  1342. (is_vmlinux(modname) && vmlinux_section_warnings))
  1343. check_sec_ref(mod, modname, &info);
  1344. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1345. if (version)
  1346. maybe_frob_rcs_version(modname, version, info.modinfo,
  1347. version - (char *)info.hdr);
  1348. if (version || (all_versions && !is_vmlinux(modname)))
  1349. get_src_version(modname, mod->srcversion,
  1350. sizeof(mod->srcversion)-1);
  1351. parse_elf_finish(&info);
  1352. /* Our trick to get versioning for struct_module - it's
  1353. * never passed as an argument to an exported function, so
  1354. * the automatic versioning doesn't pick it up, but it's really
  1355. * important anyhow */
  1356. if (modversions)
  1357. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1358. }
  1359. #define SZ 500
  1360. /* We first write the generated file into memory using the
  1361. * following helper, then compare to the file on disk and
  1362. * only update the later if anything changed */
  1363. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1364. const char *fmt, ...)
  1365. {
  1366. char tmp[SZ];
  1367. int len;
  1368. va_list ap;
  1369. va_start(ap, fmt);
  1370. len = vsnprintf(tmp, SZ, fmt, ap);
  1371. buf_write(buf, tmp, len);
  1372. va_end(ap);
  1373. }
  1374. void buf_write(struct buffer *buf, const char *s, int len)
  1375. {
  1376. if (buf->size - buf->pos < len) {
  1377. buf->size += len + SZ;
  1378. buf->p = realloc(buf->p, buf->size);
  1379. }
  1380. strncpy(buf->p + buf->pos, s, len);
  1381. buf->pos += len;
  1382. }
  1383. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1384. {
  1385. const char *e = is_vmlinux(m) ?"":".ko";
  1386. switch (exp) {
  1387. case export_gpl:
  1388. fatal("modpost: GPL-incompatible module %s%s "
  1389. "uses GPL-only symbol '%s'\n", m, e, s);
  1390. break;
  1391. case export_unused_gpl:
  1392. fatal("modpost: GPL-incompatible module %s%s "
  1393. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1394. break;
  1395. case export_gpl_future:
  1396. warn("modpost: GPL-incompatible module %s%s "
  1397. "uses future GPL-only symbol '%s'\n", m, e, s);
  1398. break;
  1399. case export_plain:
  1400. case export_unused:
  1401. case export_unknown:
  1402. /* ignore */
  1403. break;
  1404. }
  1405. }
  1406. static void check_for_unused(enum export exp, const char *m, const char *s)
  1407. {
  1408. const char *e = is_vmlinux(m) ?"":".ko";
  1409. switch (exp) {
  1410. case export_unused:
  1411. case export_unused_gpl:
  1412. warn("modpost: module %s%s "
  1413. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1414. break;
  1415. default:
  1416. /* ignore */
  1417. break;
  1418. }
  1419. }
  1420. static void check_exports(struct module *mod)
  1421. {
  1422. struct symbol *s, *exp;
  1423. for (s = mod->unres; s; s = s->next) {
  1424. const char *basename;
  1425. exp = find_symbol(s->name);
  1426. if (!exp || exp->module == mod)
  1427. continue;
  1428. basename = strrchr(mod->name, '/');
  1429. if (basename)
  1430. basename++;
  1431. else
  1432. basename = mod->name;
  1433. if (!mod->gpl_compatible)
  1434. check_for_gpl_usage(exp->export, basename, exp->name);
  1435. check_for_unused(exp->export, basename, exp->name);
  1436. }
  1437. }
  1438. /**
  1439. * Header for the generated file
  1440. **/
  1441. static void add_header(struct buffer *b, struct module *mod)
  1442. {
  1443. buf_printf(b, "#include <linux/module.h>\n");
  1444. buf_printf(b, "#include <linux/vermagic.h>\n");
  1445. buf_printf(b, "#include <linux/compiler.h>\n");
  1446. buf_printf(b, "\n");
  1447. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1448. buf_printf(b, "\n");
  1449. buf_printf(b, "struct module __this_module\n");
  1450. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1451. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1452. if (mod->has_init)
  1453. buf_printf(b, " .init = init_module,\n");
  1454. if (mod->has_cleanup)
  1455. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1456. " .exit = cleanup_module,\n"
  1457. "#endif\n");
  1458. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1459. buf_printf(b, "};\n");
  1460. }
  1461. /**
  1462. * Record CRCs for unresolved symbols
  1463. **/
  1464. static int add_versions(struct buffer *b, struct module *mod)
  1465. {
  1466. struct symbol *s, *exp;
  1467. int err = 0;
  1468. for (s = mod->unres; s; s = s->next) {
  1469. exp = find_symbol(s->name);
  1470. if (!exp || exp->module == mod) {
  1471. if (have_vmlinux && !s->weak) {
  1472. if (warn_unresolved) {
  1473. warn("\"%s\" [%s.ko] undefined!\n",
  1474. s->name, mod->name);
  1475. } else {
  1476. merror("\"%s\" [%s.ko] undefined!\n",
  1477. s->name, mod->name);
  1478. err = 1;
  1479. }
  1480. }
  1481. continue;
  1482. }
  1483. s->module = exp->module;
  1484. s->crc_valid = exp->crc_valid;
  1485. s->crc = exp->crc;
  1486. }
  1487. if (!modversions)
  1488. return err;
  1489. buf_printf(b, "\n");
  1490. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1491. buf_printf(b, "__used\n");
  1492. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1493. for (s = mod->unres; s; s = s->next) {
  1494. if (!s->module)
  1495. continue;
  1496. if (!s->crc_valid) {
  1497. warn("\"%s\" [%s.ko] has no CRC!\n",
  1498. s->name, mod->name);
  1499. continue;
  1500. }
  1501. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1502. }
  1503. buf_printf(b, "};\n");
  1504. return err;
  1505. }
  1506. static void add_depends(struct buffer *b, struct module *mod,
  1507. struct module *modules)
  1508. {
  1509. struct symbol *s;
  1510. struct module *m;
  1511. int first = 1;
  1512. for (m = modules; m; m = m->next)
  1513. m->seen = is_vmlinux(m->name);
  1514. buf_printf(b, "\n");
  1515. buf_printf(b, "static const char __module_depends[]\n");
  1516. buf_printf(b, "__used\n");
  1517. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1518. buf_printf(b, "\"depends=");
  1519. for (s = mod->unres; s; s = s->next) {
  1520. const char *p;
  1521. if (!s->module)
  1522. continue;
  1523. if (s->module->seen)
  1524. continue;
  1525. s->module->seen = 1;
  1526. p = strrchr(s->module->name, '/');
  1527. if (p)
  1528. p++;
  1529. else
  1530. p = s->module->name;
  1531. buf_printf(b, "%s%s", first ? "" : ",", p);
  1532. first = 0;
  1533. }
  1534. buf_printf(b, "\";\n");
  1535. }
  1536. static void add_srcversion(struct buffer *b, struct module *mod)
  1537. {
  1538. if (mod->srcversion[0]) {
  1539. buf_printf(b, "\n");
  1540. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1541. mod->srcversion);
  1542. }
  1543. }
  1544. static void write_if_changed(struct buffer *b, const char *fname)
  1545. {
  1546. char *tmp;
  1547. FILE *file;
  1548. struct stat st;
  1549. file = fopen(fname, "r");
  1550. if (!file)
  1551. goto write;
  1552. if (fstat(fileno(file), &st) < 0)
  1553. goto close_write;
  1554. if (st.st_size != b->pos)
  1555. goto close_write;
  1556. tmp = NOFAIL(malloc(b->pos));
  1557. if (fread(tmp, 1, b->pos, file) != b->pos)
  1558. goto free_write;
  1559. if (memcmp(tmp, b->p, b->pos) != 0)
  1560. goto free_write;
  1561. free(tmp);
  1562. fclose(file);
  1563. return;
  1564. free_write:
  1565. free(tmp);
  1566. close_write:
  1567. fclose(file);
  1568. write:
  1569. file = fopen(fname, "w");
  1570. if (!file) {
  1571. perror(fname);
  1572. exit(1);
  1573. }
  1574. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1575. perror(fname);
  1576. exit(1);
  1577. }
  1578. fclose(file);
  1579. }
  1580. /* parse Module.symvers file. line format:
  1581. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1582. **/
  1583. static void read_dump(const char *fname, unsigned int kernel)
  1584. {
  1585. unsigned long size, pos = 0;
  1586. void *file = grab_file(fname, &size);
  1587. char *line;
  1588. if (!file)
  1589. /* No symbol versions, silently ignore */
  1590. return;
  1591. while ((line = get_next_line(&pos, file, size))) {
  1592. char *symname, *modname, *d, *export, *end;
  1593. unsigned int crc;
  1594. struct module *mod;
  1595. struct symbol *s;
  1596. if (!(symname = strchr(line, '\t')))
  1597. goto fail;
  1598. *symname++ = '\0';
  1599. if (!(modname = strchr(symname, '\t')))
  1600. goto fail;
  1601. *modname++ = '\0';
  1602. if ((export = strchr(modname, '\t')) != NULL)
  1603. *export++ = '\0';
  1604. if (export && ((end = strchr(export, '\t')) != NULL))
  1605. *end = '\0';
  1606. crc = strtoul(line, &d, 16);
  1607. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1608. goto fail;
  1609. mod = find_module(modname);
  1610. if (!mod) {
  1611. if (is_vmlinux(modname))
  1612. have_vmlinux = 1;
  1613. mod = new_module(NOFAIL(strdup(modname)));
  1614. mod->skip = 1;
  1615. }
  1616. s = sym_add_exported(symname, mod, export_no(export));
  1617. s->kernel = kernel;
  1618. s->preloaded = 1;
  1619. sym_update_crc(symname, mod, crc, export_no(export));
  1620. }
  1621. return;
  1622. fail:
  1623. fatal("parse error in symbol dump file\n");
  1624. }
  1625. /* For normal builds always dump all symbols.
  1626. * For external modules only dump symbols
  1627. * that are not read from kernel Module.symvers.
  1628. **/
  1629. static int dump_sym(struct symbol *sym)
  1630. {
  1631. if (!external_module)
  1632. return 1;
  1633. if (sym->vmlinux || sym->kernel)
  1634. return 0;
  1635. return 1;
  1636. }
  1637. static void write_dump(const char *fname)
  1638. {
  1639. struct buffer buf = { };
  1640. struct symbol *symbol;
  1641. int n;
  1642. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1643. symbol = symbolhash[n];
  1644. while (symbol) {
  1645. if (dump_sym(symbol))
  1646. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1647. symbol->crc, symbol->name,
  1648. symbol->module->name,
  1649. export_str(symbol->export));
  1650. symbol = symbol->next;
  1651. }
  1652. }
  1653. write_if_changed(&buf, fname);
  1654. }
  1655. int main(int argc, char **argv)
  1656. {
  1657. struct module *mod;
  1658. struct buffer buf = { };
  1659. char *kernel_read = NULL, *module_read = NULL;
  1660. char *dump_write = NULL;
  1661. int opt;
  1662. int err;
  1663. while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) {
  1664. switch (opt) {
  1665. case 'i':
  1666. kernel_read = optarg;
  1667. break;
  1668. case 'I':
  1669. module_read = optarg;
  1670. external_module = 1;
  1671. break;
  1672. case 'm':
  1673. modversions = 1;
  1674. break;
  1675. case 'o':
  1676. dump_write = optarg;
  1677. break;
  1678. case 'a':
  1679. all_versions = 1;
  1680. break;
  1681. case 's':
  1682. vmlinux_section_warnings = 0;
  1683. break;
  1684. case 'S':
  1685. sec_mismatch_verbose = 0;
  1686. break;
  1687. case 'w':
  1688. warn_unresolved = 1;
  1689. break;
  1690. default:
  1691. exit(1);
  1692. }
  1693. }
  1694. if (kernel_read)
  1695. read_dump(kernel_read, 1);
  1696. if (module_read)
  1697. read_dump(module_read, 0);
  1698. while (optind < argc)
  1699. read_symbols(argv[optind++]);
  1700. for (mod = modules; mod; mod = mod->next) {
  1701. if (mod->skip)
  1702. continue;
  1703. check_exports(mod);
  1704. }
  1705. err = 0;
  1706. for (mod = modules; mod; mod = mod->next) {
  1707. char fname[strlen(mod->name) + 10];
  1708. if (mod->skip)
  1709. continue;
  1710. buf.pos = 0;
  1711. add_header(&buf, mod);
  1712. err |= add_versions(&buf, mod);
  1713. add_depends(&buf, mod, modules);
  1714. add_moddevtable(&buf, mod);
  1715. add_srcversion(&buf, mod);
  1716. sprintf(fname, "%s.mod.c", mod->name);
  1717. write_if_changed(&buf, fname);
  1718. }
  1719. if (dump_write)
  1720. write_dump(dump_write);
  1721. if (sec_mismatch_count && !sec_mismatch_verbose)
  1722. fprintf(stderr, "modpost: Found %d section mismatch(es).\n"
  1723. "To see full details build your kernel with:\n"
  1724. "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
  1725. sec_mismatch_count);
  1726. return err;
  1727. }