modpost.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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 "(unknown)";
  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 -1;
  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. switch (from_is_func) {
  992. case 0: from = "variable"; from_p = ""; break;
  993. case 1: from = "function"; from_p = "()"; break;
  994. default: from = "(unknown reference)"; from_p = ""; break;
  995. }
  996. switch (to_is_func) {
  997. case 0: to = "variable"; to_p = ""; break;
  998. case 1: to = "function"; to_p = "()"; break;
  999. default: to = "(unknown reference)"; to_p = ""; break;
  1000. }
  1001. sec_mismatch_count++;
  1002. if (!sec_mismatch_verbose)
  1003. return;
  1004. warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
  1005. "to the %s %s:%s%s\n",
  1006. modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
  1007. tosym, to_p);
  1008. switch (mismatch) {
  1009. case TEXT_TO_INIT:
  1010. fprintf(stderr,
  1011. "The function %s%s() references\n"
  1012. "the %s %s%s%s.\n"
  1013. "This is often because %s lacks a %s\n"
  1014. "annotation or the annotation of %s is wrong.\n",
  1015. sec2annotation(fromsec), fromsym,
  1016. to, sec2annotation(tosec), tosym, to_p,
  1017. fromsym, sec2annotation(tosec), tosym);
  1018. break;
  1019. case DATA_TO_INIT: {
  1020. const char **s = symbol_white_list;
  1021. fprintf(stderr,
  1022. "The variable %s references\n"
  1023. "the %s %s%s%s\n"
  1024. "If the reference is valid then annotate the\n"
  1025. "variable with __init* (see linux/init.h) "
  1026. "or name the variable:\n",
  1027. fromsym, to, sec2annotation(tosec), tosym, to_p);
  1028. while (*s)
  1029. fprintf(stderr, "%s, ", *s++);
  1030. fprintf(stderr, "\n");
  1031. break;
  1032. }
  1033. case TEXT_TO_EXIT:
  1034. fprintf(stderr,
  1035. "The function %s() references a %s in an exit section.\n"
  1036. "Often the %s %s%s has valid usage outside the exit section\n"
  1037. "and the fix is to remove the %sannotation of %s.\n",
  1038. fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
  1039. break;
  1040. case DATA_TO_EXIT: {
  1041. const char **s = symbol_white_list;
  1042. fprintf(stderr,
  1043. "The variable %s references\n"
  1044. "the %s %s%s%s\n"
  1045. "If the reference is valid then annotate the\n"
  1046. "variable with __exit* (see linux/init.h) or "
  1047. "name the variable:\n",
  1048. fromsym, to, sec2annotation(tosec), tosym, to_p);
  1049. while (*s)
  1050. fprintf(stderr, "%s, ", *s++);
  1051. fprintf(stderr, "\n");
  1052. break;
  1053. }
  1054. case XXXINIT_TO_INIT:
  1055. case XXXEXIT_TO_EXIT:
  1056. fprintf(stderr,
  1057. "The %s %s%s%s references\n"
  1058. "a %s %s%s%s.\n"
  1059. "If %s is only used by %s then\n"
  1060. "annotate %s with a matching annotation.\n",
  1061. from, sec2annotation(fromsec), fromsym, from_p,
  1062. to, sec2annotation(tosec), tosym, to_p,
  1063. fromsym, tosym, fromsym);
  1064. break;
  1065. case INIT_TO_EXIT:
  1066. fprintf(stderr,
  1067. "The %s %s%s%s references\n"
  1068. "a %s %s%s%s.\n"
  1069. "This is often seen when error handling "
  1070. "in the init function\n"
  1071. "uses functionality in the exit path.\n"
  1072. "The fix is often to remove the %sannotation of\n"
  1073. "%s%s so it may be used outside an exit section.\n",
  1074. from, sec2annotation(fromsec), fromsym, from_p,
  1075. to, sec2annotation(tosec), tosym, to_p,
  1076. sec2annotation(tosec), tosym, to_p);
  1077. break;
  1078. case EXIT_TO_INIT:
  1079. fprintf(stderr,
  1080. "The %s %s%s%s references\n"
  1081. "a %s %s%s%s.\n"
  1082. "This is often seen when error handling "
  1083. "in the exit function\n"
  1084. "uses functionality in the init path.\n"
  1085. "The fix is often to remove the %sannotation of\n"
  1086. "%s%s so it may be used outside an init section.\n",
  1087. from, sec2annotation(fromsec), fromsym, from_p,
  1088. to, sec2annotation(tosec), tosym, to_p,
  1089. sec2annotation(tosec), tosym, to_p);
  1090. break;
  1091. case EXPORT_TO_INIT_EXIT:
  1092. fprintf(stderr,
  1093. "The symbol %s is exported and annotated %s\n"
  1094. "Fix this by removing the %sannotation of %s "
  1095. "or drop the export.\n",
  1096. tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
  1097. case NO_MISMATCH:
  1098. /* To get warnings on missing members */
  1099. break;
  1100. }
  1101. fprintf(stderr, "\n");
  1102. }
  1103. static void check_section_mismatch(const char *modname, struct elf_info *elf,
  1104. Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
  1105. {
  1106. const char *tosec;
  1107. enum mismatch mismatch;
  1108. tosec = sec_name(elf, sym->st_shndx);
  1109. mismatch = section_mismatch(fromsec, tosec);
  1110. if (mismatch != NO_MISMATCH) {
  1111. Elf_Sym *to;
  1112. Elf_Sym *from;
  1113. const char *tosym;
  1114. const char *fromsym;
  1115. from = find_elf_symbol2(elf, r->r_offset, fromsec);
  1116. fromsym = sym_name(elf, from);
  1117. to = find_elf_symbol(elf, r->r_addend, sym);
  1118. tosym = sym_name(elf, to);
  1119. /* check whitelist - we may ignore it */
  1120. if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
  1121. report_sec_mismatch(modname, mismatch,
  1122. fromsec, r->r_offset, fromsym,
  1123. is_function(from), tosec, tosym,
  1124. is_function(to));
  1125. }
  1126. }
  1127. }
  1128. static unsigned int *reloc_location(struct elf_info *elf,
  1129. Elf_Shdr *sechdr, Elf_Rela *r)
  1130. {
  1131. Elf_Shdr *sechdrs = elf->sechdrs;
  1132. int section = sechdr->sh_info;
  1133. return (void *)elf->hdr + sechdrs[section].sh_offset +
  1134. (r->r_offset - sechdrs[section].sh_addr);
  1135. }
  1136. static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1137. {
  1138. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1139. unsigned int *location = reloc_location(elf, sechdr, r);
  1140. switch (r_typ) {
  1141. case R_386_32:
  1142. r->r_addend = TO_NATIVE(*location);
  1143. break;
  1144. case R_386_PC32:
  1145. r->r_addend = TO_NATIVE(*location) + 4;
  1146. /* For CONFIG_RELOCATABLE=y */
  1147. if (elf->hdr->e_type == ET_EXEC)
  1148. r->r_addend += r->r_offset;
  1149. break;
  1150. }
  1151. return 0;
  1152. }
  1153. static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1154. {
  1155. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1156. switch (r_typ) {
  1157. case R_ARM_ABS32:
  1158. /* From ARM ABI: (S + A) | T */
  1159. r->r_addend = (int)(long)
  1160. (elf->symtab_start + ELF_R_SYM(r->r_info));
  1161. break;
  1162. case R_ARM_PC24:
  1163. /* From ARM ABI: ((S + A) | T) - P */
  1164. r->r_addend = (int)(long)(elf->hdr +
  1165. sechdr->sh_offset +
  1166. (r->r_offset - sechdr->sh_addr));
  1167. break;
  1168. default:
  1169. return 1;
  1170. }
  1171. return 0;
  1172. }
  1173. static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
  1174. {
  1175. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  1176. unsigned int *location = reloc_location(elf, sechdr, r);
  1177. unsigned int inst;
  1178. if (r_typ == R_MIPS_HI16)
  1179. return 1; /* skip this */
  1180. inst = TO_NATIVE(*location);
  1181. switch (r_typ) {
  1182. case R_MIPS_LO16:
  1183. r->r_addend = inst & 0xffff;
  1184. break;
  1185. case R_MIPS_26:
  1186. r->r_addend = (inst & 0x03ffffff) << 2;
  1187. break;
  1188. case R_MIPS_32:
  1189. r->r_addend = inst;
  1190. break;
  1191. }
  1192. return 0;
  1193. }
  1194. static void section_rela(const char *modname, struct elf_info *elf,
  1195. Elf_Shdr *sechdr)
  1196. {
  1197. Elf_Sym *sym;
  1198. Elf_Rela *rela;
  1199. Elf_Rela r;
  1200. unsigned int r_sym;
  1201. const char *fromsec;
  1202. Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
  1203. Elf_Rela *stop = (void *)start + sechdr->sh_size;
  1204. fromsec = sech_name(elf, sechdr);
  1205. fromsec += strlen(".rela");
  1206. /* if from section (name) is know good then skip it */
  1207. if (check_section(modname, fromsec))
  1208. return;
  1209. for (rela = start; rela < stop; rela++) {
  1210. r.r_offset = TO_NATIVE(rela->r_offset);
  1211. #if KERNEL_ELFCLASS == ELFCLASS64
  1212. if (elf->hdr->e_machine == EM_MIPS) {
  1213. unsigned int r_typ;
  1214. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  1215. r_sym = TO_NATIVE(r_sym);
  1216. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  1217. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1218. } else {
  1219. r.r_info = TO_NATIVE(rela->r_info);
  1220. r_sym = ELF_R_SYM(r.r_info);
  1221. }
  1222. #else
  1223. r.r_info = TO_NATIVE(rela->r_info);
  1224. r_sym = ELF_R_SYM(r.r_info);
  1225. #endif
  1226. r.r_addend = TO_NATIVE(rela->r_addend);
  1227. sym = elf->symtab_start + r_sym;
  1228. /* Skip special sections */
  1229. if (sym->st_shndx >= SHN_LORESERVE)
  1230. continue;
  1231. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1232. }
  1233. }
  1234. static void section_rel(const char *modname, struct elf_info *elf,
  1235. Elf_Shdr *sechdr)
  1236. {
  1237. Elf_Sym *sym;
  1238. Elf_Rel *rel;
  1239. Elf_Rela r;
  1240. unsigned int r_sym;
  1241. const char *fromsec;
  1242. Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
  1243. Elf_Rel *stop = (void *)start + sechdr->sh_size;
  1244. fromsec = sech_name(elf, sechdr);
  1245. fromsec += strlen(".rel");
  1246. /* if from section (name) is know good then skip it */
  1247. if (check_section(modname, fromsec))
  1248. return;
  1249. for (rel = start; rel < stop; rel++) {
  1250. r.r_offset = TO_NATIVE(rel->r_offset);
  1251. #if KERNEL_ELFCLASS == ELFCLASS64
  1252. if (elf->hdr->e_machine == EM_MIPS) {
  1253. unsigned int r_typ;
  1254. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  1255. r_sym = TO_NATIVE(r_sym);
  1256. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  1257. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  1258. } else {
  1259. r.r_info = TO_NATIVE(rel->r_info);
  1260. r_sym = ELF_R_SYM(r.r_info);
  1261. }
  1262. #else
  1263. r.r_info = TO_NATIVE(rel->r_info);
  1264. r_sym = ELF_R_SYM(r.r_info);
  1265. #endif
  1266. r.r_addend = 0;
  1267. switch (elf->hdr->e_machine) {
  1268. case EM_386:
  1269. if (addend_386_rel(elf, sechdr, &r))
  1270. continue;
  1271. break;
  1272. case EM_ARM:
  1273. if (addend_arm_rel(elf, sechdr, &r))
  1274. continue;
  1275. break;
  1276. case EM_MIPS:
  1277. if (addend_mips_rel(elf, sechdr, &r))
  1278. continue;
  1279. break;
  1280. }
  1281. sym = elf->symtab_start + r_sym;
  1282. /* Skip special sections */
  1283. if (sym->st_shndx >= SHN_LORESERVE)
  1284. continue;
  1285. check_section_mismatch(modname, elf, &r, sym, fromsec);
  1286. }
  1287. }
  1288. /**
  1289. * A module includes a number of sections that are discarded
  1290. * either when loaded or when used as built-in.
  1291. * For loaded modules all functions marked __init and all data
  1292. * marked __initdata will be discarded when the module has been intialized.
  1293. * Likewise for modules used built-in the sections marked __exit
  1294. * are discarded because __exit marked function are supposed to be called
  1295. * only when a moduel is unloaded which never happes for built-in modules.
  1296. * The check_sec_ref() function traverses all relocation records
  1297. * to find all references to a section that reference a section that will
  1298. * be discarded and warns about it.
  1299. **/
  1300. static void check_sec_ref(struct module *mod, const char *modname,
  1301. struct elf_info *elf)
  1302. {
  1303. int i;
  1304. Elf_Shdr *sechdrs = elf->sechdrs;
  1305. /* Walk through all sections */
  1306. for (i = 0; i < elf->hdr->e_shnum; i++) {
  1307. /* We want to process only relocation sections and not .init */
  1308. if (sechdrs[i].sh_type == SHT_RELA)
  1309. section_rela(modname, elf, &elf->sechdrs[i]);
  1310. else if (sechdrs[i].sh_type == SHT_REL)
  1311. section_rel(modname, elf, &elf->sechdrs[i]);
  1312. }
  1313. }
  1314. static void read_symbols(char *modname)
  1315. {
  1316. const char *symname;
  1317. char *version;
  1318. char *license;
  1319. struct module *mod;
  1320. struct elf_info info = { };
  1321. Elf_Sym *sym;
  1322. if (!parse_elf(&info, modname))
  1323. return;
  1324. mod = new_module(modname);
  1325. /* When there's no vmlinux, don't print warnings about
  1326. * unresolved symbols (since there'll be too many ;) */
  1327. if (is_vmlinux(modname)) {
  1328. have_vmlinux = 1;
  1329. mod->skip = 1;
  1330. }
  1331. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1332. while (license) {
  1333. if (license_is_gpl_compatible(license))
  1334. mod->gpl_compatible = 1;
  1335. else {
  1336. mod->gpl_compatible = 0;
  1337. break;
  1338. }
  1339. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1340. "license", license);
  1341. }
  1342. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1343. symname = info.strtab + sym->st_name;
  1344. handle_modversions(mod, &info, sym, symname);
  1345. handle_moddevtable(mod, &info, sym, symname);
  1346. }
  1347. if (!is_vmlinux(modname) ||
  1348. (is_vmlinux(modname) && vmlinux_section_warnings))
  1349. check_sec_ref(mod, modname, &info);
  1350. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1351. if (version)
  1352. maybe_frob_rcs_version(modname, version, info.modinfo,
  1353. version - (char *)info.hdr);
  1354. if (version || (all_versions && !is_vmlinux(modname)))
  1355. get_src_version(modname, mod->srcversion,
  1356. sizeof(mod->srcversion)-1);
  1357. parse_elf_finish(&info);
  1358. /* Our trick to get versioning for struct_module - it's
  1359. * never passed as an argument to an exported function, so
  1360. * the automatic versioning doesn't pick it up, but it's really
  1361. * important anyhow */
  1362. if (modversions)
  1363. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1364. }
  1365. #define SZ 500
  1366. /* We first write the generated file into memory using the
  1367. * following helper, then compare to the file on disk and
  1368. * only update the later if anything changed */
  1369. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1370. const char *fmt, ...)
  1371. {
  1372. char tmp[SZ];
  1373. int len;
  1374. va_list ap;
  1375. va_start(ap, fmt);
  1376. len = vsnprintf(tmp, SZ, fmt, ap);
  1377. buf_write(buf, tmp, len);
  1378. va_end(ap);
  1379. }
  1380. void buf_write(struct buffer *buf, const char *s, int len)
  1381. {
  1382. if (buf->size - buf->pos < len) {
  1383. buf->size += len + SZ;
  1384. buf->p = realloc(buf->p, buf->size);
  1385. }
  1386. strncpy(buf->p + buf->pos, s, len);
  1387. buf->pos += len;
  1388. }
  1389. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1390. {
  1391. const char *e = is_vmlinux(m) ?"":".ko";
  1392. switch (exp) {
  1393. case export_gpl:
  1394. fatal("modpost: GPL-incompatible module %s%s "
  1395. "uses GPL-only symbol '%s'\n", m, e, s);
  1396. break;
  1397. case export_unused_gpl:
  1398. fatal("modpost: GPL-incompatible module %s%s "
  1399. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1400. break;
  1401. case export_gpl_future:
  1402. warn("modpost: GPL-incompatible module %s%s "
  1403. "uses future GPL-only symbol '%s'\n", m, e, s);
  1404. break;
  1405. case export_plain:
  1406. case export_unused:
  1407. case export_unknown:
  1408. /* ignore */
  1409. break;
  1410. }
  1411. }
  1412. static void check_for_unused(enum export exp, const char *m, const char *s)
  1413. {
  1414. const char *e = is_vmlinux(m) ?"":".ko";
  1415. switch (exp) {
  1416. case export_unused:
  1417. case export_unused_gpl:
  1418. warn("modpost: module %s%s "
  1419. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1420. break;
  1421. default:
  1422. /* ignore */
  1423. break;
  1424. }
  1425. }
  1426. static void check_exports(struct module *mod)
  1427. {
  1428. struct symbol *s, *exp;
  1429. for (s = mod->unres; s; s = s->next) {
  1430. const char *basename;
  1431. exp = find_symbol(s->name);
  1432. if (!exp || exp->module == mod)
  1433. continue;
  1434. basename = strrchr(mod->name, '/');
  1435. if (basename)
  1436. basename++;
  1437. else
  1438. basename = mod->name;
  1439. if (!mod->gpl_compatible)
  1440. check_for_gpl_usage(exp->export, basename, exp->name);
  1441. check_for_unused(exp->export, basename, exp->name);
  1442. }
  1443. }
  1444. /**
  1445. * Header for the generated file
  1446. **/
  1447. static void add_header(struct buffer *b, struct module *mod)
  1448. {
  1449. buf_printf(b, "#include <linux/module.h>\n");
  1450. buf_printf(b, "#include <linux/vermagic.h>\n");
  1451. buf_printf(b, "#include <linux/compiler.h>\n");
  1452. buf_printf(b, "\n");
  1453. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1454. buf_printf(b, "\n");
  1455. buf_printf(b, "struct module __this_module\n");
  1456. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1457. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1458. if (mod->has_init)
  1459. buf_printf(b, " .init = init_module,\n");
  1460. if (mod->has_cleanup)
  1461. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1462. " .exit = cleanup_module,\n"
  1463. "#endif\n");
  1464. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1465. buf_printf(b, "};\n");
  1466. }
  1467. /**
  1468. * Record CRCs for unresolved symbols
  1469. **/
  1470. static int add_versions(struct buffer *b, struct module *mod)
  1471. {
  1472. struct symbol *s, *exp;
  1473. int err = 0;
  1474. for (s = mod->unres; s; s = s->next) {
  1475. exp = find_symbol(s->name);
  1476. if (!exp || exp->module == mod) {
  1477. if (have_vmlinux && !s->weak) {
  1478. if (warn_unresolved) {
  1479. warn("\"%s\" [%s.ko] undefined!\n",
  1480. s->name, mod->name);
  1481. } else {
  1482. merror("\"%s\" [%s.ko] undefined!\n",
  1483. s->name, mod->name);
  1484. err = 1;
  1485. }
  1486. }
  1487. continue;
  1488. }
  1489. s->module = exp->module;
  1490. s->crc_valid = exp->crc_valid;
  1491. s->crc = exp->crc;
  1492. }
  1493. if (!modversions)
  1494. return err;
  1495. buf_printf(b, "\n");
  1496. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1497. buf_printf(b, "__used\n");
  1498. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1499. for (s = mod->unres; s; s = s->next) {
  1500. if (!s->module)
  1501. continue;
  1502. if (!s->crc_valid) {
  1503. warn("\"%s\" [%s.ko] has no CRC!\n",
  1504. s->name, mod->name);
  1505. continue;
  1506. }
  1507. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1508. }
  1509. buf_printf(b, "};\n");
  1510. return err;
  1511. }
  1512. static void add_depends(struct buffer *b, struct module *mod,
  1513. struct module *modules)
  1514. {
  1515. struct symbol *s;
  1516. struct module *m;
  1517. int first = 1;
  1518. for (m = modules; m; m = m->next)
  1519. m->seen = is_vmlinux(m->name);
  1520. buf_printf(b, "\n");
  1521. buf_printf(b, "static const char __module_depends[]\n");
  1522. buf_printf(b, "__used\n");
  1523. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1524. buf_printf(b, "\"depends=");
  1525. for (s = mod->unres; s; s = s->next) {
  1526. const char *p;
  1527. if (!s->module)
  1528. continue;
  1529. if (s->module->seen)
  1530. continue;
  1531. s->module->seen = 1;
  1532. p = strrchr(s->module->name, '/');
  1533. if (p)
  1534. p++;
  1535. else
  1536. p = s->module->name;
  1537. buf_printf(b, "%s%s", first ? "" : ",", p);
  1538. first = 0;
  1539. }
  1540. buf_printf(b, "\";\n");
  1541. }
  1542. static void add_srcversion(struct buffer *b, struct module *mod)
  1543. {
  1544. if (mod->srcversion[0]) {
  1545. buf_printf(b, "\n");
  1546. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1547. mod->srcversion);
  1548. }
  1549. }
  1550. static void write_if_changed(struct buffer *b, const char *fname)
  1551. {
  1552. char *tmp;
  1553. FILE *file;
  1554. struct stat st;
  1555. file = fopen(fname, "r");
  1556. if (!file)
  1557. goto write;
  1558. if (fstat(fileno(file), &st) < 0)
  1559. goto close_write;
  1560. if (st.st_size != b->pos)
  1561. goto close_write;
  1562. tmp = NOFAIL(malloc(b->pos));
  1563. if (fread(tmp, 1, b->pos, file) != b->pos)
  1564. goto free_write;
  1565. if (memcmp(tmp, b->p, b->pos) != 0)
  1566. goto free_write;
  1567. free(tmp);
  1568. fclose(file);
  1569. return;
  1570. free_write:
  1571. free(tmp);
  1572. close_write:
  1573. fclose(file);
  1574. write:
  1575. file = fopen(fname, "w");
  1576. if (!file) {
  1577. perror(fname);
  1578. exit(1);
  1579. }
  1580. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1581. perror(fname);
  1582. exit(1);
  1583. }
  1584. fclose(file);
  1585. }
  1586. /* parse Module.symvers file. line format:
  1587. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1588. **/
  1589. static void read_dump(const char *fname, unsigned int kernel)
  1590. {
  1591. unsigned long size, pos = 0;
  1592. void *file = grab_file(fname, &size);
  1593. char *line;
  1594. if (!file)
  1595. /* No symbol versions, silently ignore */
  1596. return;
  1597. while ((line = get_next_line(&pos, file, size))) {
  1598. char *symname, *modname, *d, *export, *end;
  1599. unsigned int crc;
  1600. struct module *mod;
  1601. struct symbol *s;
  1602. if (!(symname = strchr(line, '\t')))
  1603. goto fail;
  1604. *symname++ = '\0';
  1605. if (!(modname = strchr(symname, '\t')))
  1606. goto fail;
  1607. *modname++ = '\0';
  1608. if ((export = strchr(modname, '\t')) != NULL)
  1609. *export++ = '\0';
  1610. if (export && ((end = strchr(export, '\t')) != NULL))
  1611. *end = '\0';
  1612. crc = strtoul(line, &d, 16);
  1613. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1614. goto fail;
  1615. mod = find_module(modname);
  1616. if (!mod) {
  1617. if (is_vmlinux(modname))
  1618. have_vmlinux = 1;
  1619. mod = new_module(NOFAIL(strdup(modname)));
  1620. mod->skip = 1;
  1621. }
  1622. s = sym_add_exported(symname, mod, export_no(export));
  1623. s->kernel = kernel;
  1624. s->preloaded = 1;
  1625. sym_update_crc(symname, mod, crc, export_no(export));
  1626. }
  1627. return;
  1628. fail:
  1629. fatal("parse error in symbol dump file\n");
  1630. }
  1631. /* For normal builds always dump all symbols.
  1632. * For external modules only dump symbols
  1633. * that are not read from kernel Module.symvers.
  1634. **/
  1635. static int dump_sym(struct symbol *sym)
  1636. {
  1637. if (!external_module)
  1638. return 1;
  1639. if (sym->vmlinux || sym->kernel)
  1640. return 0;
  1641. return 1;
  1642. }
  1643. static void write_dump(const char *fname)
  1644. {
  1645. struct buffer buf = { };
  1646. struct symbol *symbol;
  1647. int n;
  1648. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1649. symbol = symbolhash[n];
  1650. while (symbol) {
  1651. if (dump_sym(symbol))
  1652. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1653. symbol->crc, symbol->name,
  1654. symbol->module->name,
  1655. export_str(symbol->export));
  1656. symbol = symbol->next;
  1657. }
  1658. }
  1659. write_if_changed(&buf, fname);
  1660. }
  1661. int main(int argc, char **argv)
  1662. {
  1663. struct module *mod;
  1664. struct buffer buf = { };
  1665. char *kernel_read = NULL, *module_read = NULL;
  1666. char *dump_write = NULL;
  1667. int opt;
  1668. int err;
  1669. while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) {
  1670. switch (opt) {
  1671. case 'i':
  1672. kernel_read = optarg;
  1673. break;
  1674. case 'I':
  1675. module_read = optarg;
  1676. external_module = 1;
  1677. break;
  1678. case 'm':
  1679. modversions = 1;
  1680. break;
  1681. case 'o':
  1682. dump_write = optarg;
  1683. break;
  1684. case 'a':
  1685. all_versions = 1;
  1686. break;
  1687. case 's':
  1688. vmlinux_section_warnings = 0;
  1689. break;
  1690. case 'S':
  1691. sec_mismatch_verbose = 0;
  1692. break;
  1693. case 'w':
  1694. warn_unresolved = 1;
  1695. break;
  1696. default:
  1697. exit(1);
  1698. }
  1699. }
  1700. if (kernel_read)
  1701. read_dump(kernel_read, 1);
  1702. if (module_read)
  1703. read_dump(module_read, 0);
  1704. while (optind < argc)
  1705. read_symbols(argv[optind++]);
  1706. for (mod = modules; mod; mod = mod->next) {
  1707. if (mod->skip)
  1708. continue;
  1709. check_exports(mod);
  1710. }
  1711. err = 0;
  1712. for (mod = modules; mod; mod = mod->next) {
  1713. char fname[strlen(mod->name) + 10];
  1714. if (mod->skip)
  1715. continue;
  1716. buf.pos = 0;
  1717. add_header(&buf, mod);
  1718. err |= add_versions(&buf, mod);
  1719. add_depends(&buf, mod, modules);
  1720. add_moddevtable(&buf, mod);
  1721. add_srcversion(&buf, mod);
  1722. sprintf(fname, "%s.mod.c", mod->name);
  1723. write_if_changed(&buf, fname);
  1724. }
  1725. if (dump_write)
  1726. write_dump(dump_write);
  1727. if (sec_mismatch_count && !sec_mismatch_verbose)
  1728. warn("modpost: Found %d section mismatch(es).\n"
  1729. "To see full details build your kernel with:\n"
  1730. "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
  1731. sec_mismatch_count);
  1732. return err;
  1733. }