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