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