kbuild: use warn()/fatal() consistent in modpost
[pandora-kernel.git] / scripts / mod / modpost.c
1 /* Postprocess module symbol versions
2  *
3  * Copyright 2003       Kai Germaschewski
4  * Copyright 2002-2004  Rusty Russell, IBM Corporation
5  *
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
14 #include <ctype.h>
15 #include "modpost.h"
16
17 /* Are we using CONFIG_MODVERSIONS? */
18 int modversions = 0;
19 /* Warn about undefined symbols? (do so if we have vmlinux) */
20 int have_vmlinux = 0;
21 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
22 static int all_versions = 0;
23
24 void
25 fatal(const char *fmt, ...)
26 {
27         va_list arglist;
28
29         fprintf(stderr, "FATAL: ");
30
31         va_start(arglist, fmt);
32         vfprintf(stderr, fmt, arglist);
33         va_end(arglist);
34
35         exit(1);
36 }
37
38 void
39 warn(const char *fmt, ...)
40 {
41         va_list arglist;
42
43         fprintf(stderr, "WARNING: ");
44
45         va_start(arglist, fmt);
46         vfprintf(stderr, fmt, arglist);
47         va_end(arglist);
48 }
49
50 void *do_nofail(void *ptr, const char *expr)
51 {
52         if (!ptr) {
53                 fatal("modpost: Memory allocation failure: %s.\n", expr);
54         }
55         return ptr;
56 }
57
58 /* A list of all modules we processed */
59
60 static struct module *modules;
61
62 struct module *
63 find_module(char *modname)
64 {
65         struct module *mod;
66
67         for (mod = modules; mod; mod = mod->next)
68                 if (strcmp(mod->name, modname) == 0)
69                         break;
70         return mod;
71 }
72
73 struct module *
74 new_module(char *modname)
75 {
76         struct module *mod;
77         char *p, *s;
78         
79         mod = NOFAIL(malloc(sizeof(*mod)));
80         memset(mod, 0, sizeof(*mod));
81         p = NOFAIL(strdup(modname));
82
83         /* strip trailing .o */
84         if ((s = strrchr(p, '.')) != NULL)
85                 if (strcmp(s, ".o") == 0)
86                         *s = '\0';
87
88         /* add to list */
89         mod->name = p;
90         mod->next = modules;
91         modules = mod;
92
93         return mod;
94 }
95
96 /* A hash of all exported symbols,
97  * struct symbol is also used for lists of unresolved symbols */
98
99 #define SYMBOL_HASH_SIZE 1024
100
101 struct symbol {
102         struct symbol *next;
103         struct module *module;
104         unsigned int crc;
105         int crc_valid;
106         unsigned int weak:1;
107         char name[0];
108 };
109
110 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
111
112 /* This is based on the hash agorithm from gdbm, via tdb */
113 static inline unsigned int tdb_hash(const char *name)
114 {
115         unsigned value; /* Used to compute the hash value.  */
116         unsigned   i;   /* Used to cycle through random values. */
117
118         /* Set the initial value from the key size. */
119         for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
120                 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
121
122         return (1103515243 * value + 12345);
123 }
124
125 /* Allocate a new symbols for use in the hash of exported symbols or
126  * the list of unresolved symbols per module */
127
128 struct symbol *
129 alloc_symbol(const char *name, unsigned int weak, struct symbol *next)
130 {
131         struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
132
133         memset(s, 0, sizeof(*s));
134         strcpy(s->name, name);
135         s->weak = weak;
136         s->next = next;
137         return s;
138 }
139
140 /* For the hash of exported symbols */
141
142 void
143 new_symbol(const char *name, struct module *module, unsigned int *crc)
144 {
145         unsigned int hash;
146         struct symbol *new;
147
148         hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
149         new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
150         new->module = module;
151         if (crc) {
152                 new->crc = *crc;
153                 new->crc_valid = 1;
154         }
155 }
156
157 struct symbol *
158 find_symbol(const char *name)
159 {
160         struct symbol *s;
161
162         /* For our purposes, .foo matches foo.  PPC64 needs this. */
163         if (name[0] == '.')
164                 name++;
165
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
173 /* Add an exported symbol - it may have already been added without a
174  * CRC, in this case just update the CRC */
175 void
176 add_exported_symbol(const char *name, struct module *module, unsigned int *crc)
177 {
178         struct symbol *s = find_symbol(name);
179
180         if (!s) {
181                 new_symbol(name, module, crc);
182                 return;
183         }
184         if (crc) {
185                 s->crc = *crc;
186                 s->crc_valid = 1;
187         }
188 }
189
190 void *
191 grab_file(const char *filename, unsigned long *size)
192 {
193         struct stat st;
194         void *map;
195         int fd;
196
197         fd = open(filename, O_RDONLY);
198         if (fd < 0 || fstat(fd, &st) != 0)
199                 return NULL;
200
201         *size = st.st_size;
202         map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
203         close(fd);
204
205         if (map == MAP_FAILED)
206                 return NULL;
207         return map;
208 }
209
210 /*
211    Return a copy of the next line in a mmap'ed file.
212    spaces in the beginning of the line is trimmed away.
213    Return a pointer to a static buffer.
214 */
215 char*
216 get_next_line(unsigned long *pos, void *file, unsigned long size)
217 {
218         static char line[4096];
219         int skip = 1;
220         size_t len = 0;
221         signed char *p = (signed char *)file + *pos;
222         char *s = line;
223
224         for (; *pos < size ; (*pos)++)
225         {
226                 if (skip && isspace(*p)) {
227                         p++;
228                         continue;
229                 }
230                 skip = 0;
231                 if (*p != '\n' && (*pos < size)) {
232                         len++;
233                         *s++ = *p++;
234                         if (len > 4095)
235                                 break; /* Too long, stop */
236                 } else {
237                         /* End of string */
238                         *s = '\0';
239                         return line;
240                 }
241         }
242         /* End of buffer */
243         return NULL;
244 }
245
246 void
247 release_file(void *file, unsigned long size)
248 {
249         munmap(file, size);
250 }
251
252 void
253 parse_elf(struct elf_info *info, const char *filename)
254 {
255         unsigned int i;
256         Elf_Ehdr *hdr = info->hdr;
257         Elf_Shdr *sechdrs;
258         Elf_Sym  *sym;
259
260         hdr = grab_file(filename, &info->size);
261         if (!hdr) {
262                 perror(filename);
263                 abort();
264         }
265         info->hdr = hdr;
266         if (info->size < sizeof(*hdr))
267                 goto truncated;
268
269         /* Fix endianness in ELF header */
270         hdr->e_shoff    = TO_NATIVE(hdr->e_shoff);
271         hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
272         hdr->e_shnum    = TO_NATIVE(hdr->e_shnum);
273         hdr->e_machine  = TO_NATIVE(hdr->e_machine);
274         sechdrs = (void *)hdr + hdr->e_shoff;
275         info->sechdrs = sechdrs;
276
277         /* Fix endianness in section headers */
278         for (i = 0; i < hdr->e_shnum; i++) {
279                 sechdrs[i].sh_type   = TO_NATIVE(sechdrs[i].sh_type);
280                 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
281                 sechdrs[i].sh_size   = TO_NATIVE(sechdrs[i].sh_size);
282                 sechdrs[i].sh_link   = TO_NATIVE(sechdrs[i].sh_link);
283                 sechdrs[i].sh_name   = TO_NATIVE(sechdrs[i].sh_name);
284         }
285         /* Find symbol table. */
286         for (i = 1; i < hdr->e_shnum; i++) {
287                 const char *secstrings
288                         = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
289
290                 if (sechdrs[i].sh_offset > info->size)
291                         goto truncated;
292                 if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) {
293                         info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
294                         info->modinfo_len = sechdrs[i].sh_size;
295                 }
296                 if (sechdrs[i].sh_type != SHT_SYMTAB)
297                         continue;
298
299                 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
300                 info->symtab_stop  = (void *)hdr + sechdrs[i].sh_offset 
301                                                  + sechdrs[i].sh_size;
302                 info->strtab       = (void *)hdr + 
303                                      sechdrs[sechdrs[i].sh_link].sh_offset;
304         }
305         if (!info->symtab_start) {
306                 fatal("%s has no symtab?\n", filename);
307         }
308         /* Fix endianness in symbols */
309         for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
310                 sym->st_shndx = TO_NATIVE(sym->st_shndx);
311                 sym->st_name  = TO_NATIVE(sym->st_name);
312                 sym->st_value = TO_NATIVE(sym->st_value);
313                 sym->st_size  = TO_NATIVE(sym->st_size);
314         }
315         return;
316
317  truncated:
318         fatal("%s is truncated.\n", filename);
319 }
320
321 void
322 parse_elf_finish(struct elf_info *info)
323 {
324         release_file(info->hdr, info->size);
325 }
326
327 #define CRC_PFX     "__crc_"
328 #define KSYMTAB_PFX "__ksymtab_"
329
330 void
331 handle_modversions(struct module *mod, struct elf_info *info,
332                    Elf_Sym *sym, const char *symname)
333 {
334         unsigned int crc;
335
336         switch (sym->st_shndx) {
337         case SHN_COMMON:
338                 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
339                 break;
340         case SHN_ABS:
341                 /* CRC'd symbol */
342                 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
343                         crc = (unsigned int) sym->st_value;
344                         add_exported_symbol(symname + strlen(CRC_PFX),
345                                             mod, &crc);
346                 }
347                 break;
348         case SHN_UNDEF:
349                 /* undefined symbol */
350                 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
351                     ELF_ST_BIND(sym->st_info) != STB_WEAK)
352                         break;
353                 /* ignore global offset table */
354                 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
355                         break;
356                 /* ignore __this_module, it will be resolved shortly */
357                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
358                         break;
359 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
360 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
361 /* add compatibility with older glibc */
362 #ifndef STT_SPARC_REGISTER
363 #define STT_SPARC_REGISTER STT_REGISTER
364 #endif
365                 if (info->hdr->e_machine == EM_SPARC ||
366                     info->hdr->e_machine == EM_SPARCV9) {
367                         /* Ignore register directives. */
368                         if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
369                                 break;
370                         if (symname[0] == '.') {
371                                 char *munged = strdup(symname);
372                                 munged[0] = '_';
373                                 munged[1] = toupper(munged[1]);
374                                 symname = munged;
375                         }
376                 }
377 #endif
378                 
379                 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
380                            strlen(MODULE_SYMBOL_PREFIX)) == 0)
381                         mod->unres = alloc_symbol(symname +
382                                                   strlen(MODULE_SYMBOL_PREFIX),
383                                                   ELF_ST_BIND(sym->st_info) == STB_WEAK,
384                                                   mod->unres);
385                 break;
386         default:
387                 /* All exported symbols */
388                 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
389                         add_exported_symbol(symname + strlen(KSYMTAB_PFX),
390                                             mod, NULL);
391                 }
392                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
393                         mod->has_init = 1;
394                 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
395                         mod->has_cleanup = 1;
396                 break;
397         }
398 }
399
400 int
401 is_vmlinux(const char *modname)
402 {
403         const char *myname;
404
405         if ((myname = strrchr(modname, '/')))
406                 myname++;
407         else
408                 myname = modname;
409
410         return strcmp(myname, "vmlinux") == 0;
411 }
412
413 /* Parse tag=value strings from .modinfo section */
414 static char *next_string(char *string, unsigned long *secsize)
415 {
416         /* Skip non-zero chars */
417         while (string[0]) {
418                 string++;
419                 if ((*secsize)-- <= 1)
420                         return NULL;
421         }
422
423         /* Skip any zero padding. */
424         while (!string[0]) {
425                 string++;
426                 if ((*secsize)-- <= 1)
427                         return NULL;
428         }
429         return string;
430 }
431
432 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
433                          const char *tag)
434 {
435         char *p;
436         unsigned int taglen = strlen(tag);
437         unsigned long size = modinfo_len;
438
439         for (p = modinfo; p; p = next_string(p, &size)) {
440                 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
441                         return p + taglen + 1;
442         }
443         return NULL;
444 }
445
446 void
447 read_symbols(char *modname)
448 {
449         const char *symname;
450         char *version;
451         struct module *mod;
452         struct elf_info info = { };
453         Elf_Sym *sym;
454
455         parse_elf(&info, modname);
456
457         mod = new_module(modname);
458
459         /* When there's no vmlinux, don't print warnings about
460          * unresolved symbols (since there'll be too many ;) */
461         if (is_vmlinux(modname)) {
462                 unsigned int fake_crc = 0;
463                 have_vmlinux = 1;
464                 add_exported_symbol("struct_module", mod, &fake_crc);
465                 mod->skip = 1;
466         }
467
468         for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
469                 symname = info.strtab + sym->st_name;
470
471                 handle_modversions(mod, &info, sym, symname);
472                 handle_moddevtable(mod, &info, sym, symname);
473         }
474
475         version = get_modinfo(info.modinfo, info.modinfo_len, "version");
476         if (version)
477                 maybe_frob_rcs_version(modname, version, info.modinfo,
478                                        version - (char *)info.hdr);
479         if (version || (all_versions && !is_vmlinux(modname)))
480                 get_src_version(modname, mod->srcversion,
481                                 sizeof(mod->srcversion)-1);
482
483         parse_elf_finish(&info);
484
485         /* Our trick to get versioning for struct_module - it's
486          * never passed as an argument to an exported function, so
487          * the automatic versioning doesn't pick it up, but it's really
488          * important anyhow */
489         if (modversions)
490                 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
491 }
492
493 #define SZ 500
494
495 /* We first write the generated file into memory using the
496  * following helper, then compare to the file on disk and
497  * only update the later if anything changed */
498
499 void __attribute__((format(printf, 2, 3)))
500 buf_printf(struct buffer *buf, const char *fmt, ...)
501 {
502         char tmp[SZ];
503         int len;
504         va_list ap;
505         
506         va_start(ap, fmt);
507         len = vsnprintf(tmp, SZ, fmt, ap);
508         if (buf->size - buf->pos < len + 1) {
509                 buf->size += 128;
510                 buf->p = realloc(buf->p, buf->size);
511         }
512         strncpy(buf->p + buf->pos, tmp, len + 1);
513         buf->pos += len;
514         va_end(ap);
515 }
516
517 void
518 buf_write(struct buffer *buf, const char *s, int len)
519 {
520         if (buf->size - buf->pos < len) {
521                 buf->size += len;
522                 buf->p = realloc(buf->p, buf->size);
523         }
524         strncpy(buf->p + buf->pos, s, len);
525         buf->pos += len;
526 }
527
528 /* Header for the generated file */
529
530 void
531 add_header(struct buffer *b, struct module *mod)
532 {
533         buf_printf(b, "#include <linux/module.h>\n");
534         buf_printf(b, "#include <linux/vermagic.h>\n");
535         buf_printf(b, "#include <linux/compiler.h>\n");
536         buf_printf(b, "\n");
537         buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
538         buf_printf(b, "\n");
539         buf_printf(b, "struct module __this_module\n");
540         buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
541         buf_printf(b, " .name = KBUILD_MODNAME,\n");
542         if (mod->has_init)
543                 buf_printf(b, " .init = init_module,\n");
544         if (mod->has_cleanup)
545                 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
546                               " .exit = cleanup_module,\n"
547                               "#endif\n");
548         buf_printf(b, "};\n");
549 }
550
551 /* Record CRCs for unresolved symbols */
552
553 void
554 add_versions(struct buffer *b, struct module *mod)
555 {
556         struct symbol *s, *exp;
557
558         for (s = mod->unres; s; s = s->next) {
559                 exp = find_symbol(s->name);
560                 if (!exp || exp->module == mod) {
561                         if (have_vmlinux && !s->weak)
562                                 warn("\"%s\" [%s.ko] undefined!\n",
563                                      s->name, mod->name);
564                         continue;
565                 }
566                 s->module = exp->module;
567                 s->crc_valid = exp->crc_valid;
568                 s->crc = exp->crc;
569         }
570
571         if (!modversions)
572                 return;
573
574         buf_printf(b, "\n");
575         buf_printf(b, "static const struct modversion_info ____versions[]\n");
576         buf_printf(b, "__attribute_used__\n");
577         buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
578
579         for (s = mod->unres; s; s = s->next) {
580                 if (!s->module) {
581                         continue;
582                 }
583                 if (!s->crc_valid) {
584                         warn("\"%s\" [%s.ko] has no CRC!\n",
585                                 s->name, mod->name);
586                         continue;
587                 }
588                 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
589         }
590
591         buf_printf(b, "};\n");
592 }
593
594 void
595 add_depends(struct buffer *b, struct module *mod, struct module *modules)
596 {
597         struct symbol *s;
598         struct module *m;
599         int first = 1;
600
601         for (m = modules; m; m = m->next) {
602                 m->seen = is_vmlinux(m->name);
603         }
604
605         buf_printf(b, "\n");
606         buf_printf(b, "static const char __module_depends[]\n");
607         buf_printf(b, "__attribute_used__\n");
608         buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
609         buf_printf(b, "\"depends=");
610         for (s = mod->unres; s; s = s->next) {
611                 if (!s->module)
612                         continue;
613
614                 if (s->module->seen)
615                         continue;
616
617                 s->module->seen = 1;
618                 buf_printf(b, "%s%s", first ? "" : ",",
619                            strrchr(s->module->name, '/') + 1);
620                 first = 0;
621         }
622         buf_printf(b, "\";\n");
623 }
624
625 void
626 add_srcversion(struct buffer *b, struct module *mod)
627 {
628         if (mod->srcversion[0]) {
629                 buf_printf(b, "\n");
630                 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
631                            mod->srcversion);
632         }
633 }
634
635 void
636 write_if_changed(struct buffer *b, const char *fname)
637 {
638         char *tmp;
639         FILE *file;
640         struct stat st;
641
642         file = fopen(fname, "r");
643         if (!file)
644                 goto write;
645
646         if (fstat(fileno(file), &st) < 0)
647                 goto close_write;
648
649         if (st.st_size != b->pos)
650                 goto close_write;
651
652         tmp = NOFAIL(malloc(b->pos));
653         if (fread(tmp, 1, b->pos, file) != b->pos)
654                 goto free_write;
655
656         if (memcmp(tmp, b->p, b->pos) != 0)
657                 goto free_write;
658
659         free(tmp);
660         fclose(file);
661         return;
662
663  free_write:
664         free(tmp);
665  close_write:
666         fclose(file);
667  write:
668         file = fopen(fname, "w");
669         if (!file) {
670                 perror(fname);
671                 exit(1);
672         }
673         if (fwrite(b->p, 1, b->pos, file) != b->pos) {
674                 perror(fname);
675                 exit(1);
676         }
677         fclose(file);
678 }
679
680 void
681 read_dump(const char *fname)
682 {
683         unsigned long size, pos = 0;
684         void *file = grab_file(fname, &size);
685         char *line;
686
687         if (!file)
688                 /* No symbol versions, silently ignore */
689                 return;
690
691         while ((line = get_next_line(&pos, file, size))) {
692                 char *symname, *modname, *d;
693                 unsigned int crc;
694                 struct module *mod;
695
696                 if (!(symname = strchr(line, '\t')))
697                         goto fail;
698                 *symname++ = '\0';
699                 if (!(modname = strchr(symname, '\t')))
700                         goto fail;
701                 *modname++ = '\0';
702                 if (strchr(modname, '\t'))
703                         goto fail;
704                 crc = strtoul(line, &d, 16);
705                 if (*symname == '\0' || *modname == '\0' || *d != '\0')
706                         goto fail;
707
708                 if (!(mod = find_module(modname))) {
709                         if (is_vmlinux(modname)) {
710                                 have_vmlinux = 1;
711                         }
712                         mod = new_module(NOFAIL(strdup(modname)));
713                         mod->skip = 1;
714                 }
715                 add_exported_symbol(symname, mod, &crc);
716         }
717         return;
718 fail:
719         fatal("parse error in symbol dump file\n");
720 }
721
722 void
723 write_dump(const char *fname)
724 {
725         struct buffer buf = { };
726         struct symbol *symbol;
727         int n;
728
729         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
730                 symbol = symbolhash[n];
731                 while (symbol) {
732                         symbol = symbol->next;
733                 }
734         }
735
736         for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
737                 symbol = symbolhash[n];
738                 while (symbol) {
739                         buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc,
740                                 symbol->name, symbol->module->name);
741                         symbol = symbol->next;
742                 }
743         }
744         write_if_changed(&buf, fname);
745 }
746
747 int
748 main(int argc, char **argv)
749 {
750         struct module *mod;
751         struct buffer buf = { };
752         char fname[SZ];
753         char *dump_read = NULL, *dump_write = NULL;
754         int opt;
755
756         while ((opt = getopt(argc, argv, "i:mo:a")) != -1) {
757                 switch(opt) {
758                         case 'i':
759                                 dump_read = optarg;
760                                 break;
761                         case 'm':
762                                 modversions = 1;
763                                 break;
764                         case 'o':
765                                 dump_write = optarg;
766                                 break;
767                         case 'a':
768                                 all_versions = 1;
769                                 break;
770                         default:
771                                 exit(1);
772                 }
773         }
774
775         if (dump_read)
776                 read_dump(dump_read);
777
778         while (optind < argc) {
779                 read_symbols(argv[optind++]);
780         }
781
782         for (mod = modules; mod; mod = mod->next) {
783                 if (mod->skip)
784                         continue;
785
786                 buf.pos = 0;
787
788                 add_header(&buf, mod);
789                 add_versions(&buf, mod);
790                 add_depends(&buf, mod, modules);
791                 add_moddevtable(&buf, mod);
792                 add_srcversion(&buf, mod);
793
794                 sprintf(fname, "%s.mod.c", mod->name);
795                 write_if_changed(&buf, fname);
796         }
797
798         if (dump_write)
799                 write_dump(dump_write);
800
801         return 0;
802 }
803