modpost.c 33 KB

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