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