Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / scripts / mod / modpost.c
blob953697b76afbdc415a45d0f5725af4692e1f1c24
1 /* Postprocess module symbol versions
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006-2008 Sam Ravnborg
6 * Based in part on module-init-tools/depmod.c,file2alias
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
11 * Usage: modpost vmlinux module1.o module2.o ...
14 #define _GNU_SOURCE
15 #include <stdio.h>
16 #include <ctype.h>
17 #include "modpost.h"
18 #include "../../include/linux/license.h"
20 /* Are we using CONFIG_MODVERSIONS? */
21 int modversions = 0;
22 /* Warn about undefined symbols? (do so if we have vmlinux) */
23 int have_vmlinux = 0;
24 /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
25 static int all_versions = 0;
26 /* If we are modposting external module set to 1 */
27 static int external_module = 0;
28 /* Warn about section mismatch in vmlinux if set to 1 */
29 static int vmlinux_section_warnings = 1;
30 /* Only warn about unresolved symbols */
31 static int warn_unresolved = 0;
32 /* How a symbol is exported */
33 static int sec_mismatch_count = 0;
34 static int sec_mismatch_verbose = 1;
36 enum export {
37 export_plain, export_unused, export_gpl,
38 export_unused_gpl, export_gpl_future, export_unknown
41 #define PRINTF __attribute__ ((format (printf, 1, 2)))
43 PRINTF void fatal(const char *fmt, ...)
45 va_list arglist;
47 fprintf(stderr, "FATAL: ");
49 va_start(arglist, fmt);
50 vfprintf(stderr, fmt, arglist);
51 va_end(arglist);
53 exit(1);
56 PRINTF void warn(const char *fmt, ...)
58 va_list arglist;
60 fprintf(stderr, "WARNING: ");
62 va_start(arglist, fmt);
63 vfprintf(stderr, fmt, arglist);
64 va_end(arglist);
67 PRINTF void merror(const char *fmt, ...)
69 va_list arglist;
71 fprintf(stderr, "ERROR: ");
73 va_start(arglist, fmt);
74 vfprintf(stderr, fmt, arglist);
75 va_end(arglist);
78 static int is_vmlinux(const char *modname)
80 const char *myname;
82 myname = strrchr(modname, '/');
83 if (myname)
84 myname++;
85 else
86 myname = modname;
88 return (strcmp(myname, "vmlinux") == 0) ||
89 (strcmp(myname, "vmlinux.o") == 0);
92 void *do_nofail(void *ptr, const char *expr)
94 if (!ptr)
95 fatal("modpost: Memory allocation failure: %s.\n", expr);
97 return ptr;
100 /* A list of all modules we processed */
101 static struct module *modules;
103 static struct module *find_module(char *modname)
105 struct module *mod;
107 for (mod = modules; mod; mod = mod->next)
108 if (strcmp(mod->name, modname) == 0)
109 break;
110 return mod;
113 static struct module *new_module(char *modname)
115 struct module *mod;
116 char *p, *s;
118 mod = NOFAIL(malloc(sizeof(*mod)));
119 memset(mod, 0, sizeof(*mod));
120 p = NOFAIL(strdup(modname));
122 /* strip trailing .o */
123 s = strrchr(p, '.');
124 if (s != NULL)
125 if (strcmp(s, ".o") == 0)
126 *s = '\0';
128 /* add to list */
129 mod->name = p;
130 mod->gpl_compatible = -1;
131 mod->next = modules;
132 modules = mod;
134 return mod;
137 /* A hash of all exported symbols,
138 * struct symbol is also used for lists of unresolved symbols */
140 #define SYMBOL_HASH_SIZE 1024
142 struct symbol {
143 struct symbol *next;
144 struct module *module;
145 unsigned int crc;
146 int crc_valid;
147 unsigned int weak:1;
148 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
149 unsigned int kernel:1; /* 1 if symbol is from kernel
150 * (only for external modules) **/
151 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
152 enum export export; /* Type of export */
153 char name[0];
156 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
158 /* This is based on the hash agorithm from gdbm, via tdb */
159 static inline unsigned int tdb_hash(const char *name)
161 unsigned value; /* Used to compute the hash value. */
162 unsigned i; /* Used to cycle through random values. */
164 /* Set the initial value from the key size. */
165 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
166 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
168 return (1103515243 * value + 12345);
172 * Allocate a new symbols for use in the hash of exported symbols or
173 * the list of unresolved symbols per module
175 static struct symbol *alloc_symbol(const char *name, unsigned int weak,
176 struct symbol *next)
178 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
180 memset(s, 0, sizeof(*s));
181 strcpy(s->name, name);
182 s->weak = weak;
183 s->next = next;
184 return s;
187 /* For the hash of exported symbols */
188 static struct symbol *new_symbol(const char *name, struct module *module,
189 enum export export)
191 unsigned int hash;
192 struct symbol *new;
194 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
195 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
196 new->module = module;
197 new->export = export;
198 return new;
201 static struct symbol *find_symbol(const char *name)
203 struct symbol *s;
205 /* For our purposes, .foo matches foo. PPC64 needs this. */
206 if (name[0] == '.')
207 name++;
209 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
210 if (strcmp(s->name, name) == 0)
211 return s;
213 return NULL;
216 static struct {
217 const char *str;
218 enum export export;
219 } export_list[] = {
220 { .str = "EXPORT_SYMBOL", .export = export_plain },
221 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
222 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
223 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
224 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
225 { .str = "(unknown)", .export = export_unknown },
229 static const char *export_str(enum export ex)
231 return export_list[ex].str;
234 static enum export export_no(const char *s)
236 int i;
238 if (!s)
239 return export_unknown;
240 for (i = 0; export_list[i].export != export_unknown; i++) {
241 if (strcmp(export_list[i].str, s) == 0)
242 return export_list[i].export;
244 return export_unknown;
247 static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
249 if (sec == elf->export_sec)
250 return export_plain;
251 else if (sec == elf->export_unused_sec)
252 return export_unused;
253 else if (sec == elf->export_gpl_sec)
254 return export_gpl;
255 else if (sec == elf->export_unused_gpl_sec)
256 return export_unused_gpl;
257 else if (sec == elf->export_gpl_future_sec)
258 return export_gpl_future;
259 else
260 return export_unknown;
264 * Add an exported symbol - it may have already been added without a
265 * CRC, in this case just update the CRC
267 static struct symbol *sym_add_exported(const char *name, struct module *mod,
268 enum export export)
270 struct symbol *s = find_symbol(name);
272 if (!s) {
273 s = new_symbol(name, mod, export);
274 } else {
275 if (!s->preloaded) {
276 warn("%s: '%s' exported twice. Previous export "
277 "was in %s%s\n", mod->name, name,
278 s->module->name,
279 is_vmlinux(s->module->name) ?"":".ko");
280 } else {
281 /* In case Modules.symvers was out of date */
282 s->module = mod;
285 s->preloaded = 0;
286 s->vmlinux = is_vmlinux(mod->name);
287 s->kernel = 0;
288 s->export = export;
289 return s;
292 static void sym_update_crc(const char *name, struct module *mod,
293 unsigned int crc, enum export export)
295 struct symbol *s = find_symbol(name);
297 if (!s)
298 s = new_symbol(name, mod, export);
299 s->crc = crc;
300 s->crc_valid = 1;
303 void *grab_file(const char *filename, unsigned long *size)
305 struct stat st;
306 void *map;
307 int fd;
309 fd = open(filename, O_RDONLY);
310 if (fd < 0 || fstat(fd, &st) != 0)
311 return NULL;
313 *size = st.st_size;
314 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
315 close(fd);
317 if (map == MAP_FAILED)
318 return NULL;
319 return map;
323 * Return a copy of the next line in a mmap'ed file.
324 * spaces in the beginning of the line is trimmed away.
325 * Return a pointer to a static buffer.
327 char *get_next_line(unsigned long *pos, void *file, unsigned long size)
329 static char line[4096];
330 int skip = 1;
331 size_t len = 0;
332 signed char *p = (signed char *)file + *pos;
333 char *s = line;
335 for (; *pos < size ; (*pos)++) {
336 if (skip && isspace(*p)) {
337 p++;
338 continue;
340 skip = 0;
341 if (*p != '\n' && (*pos < size)) {
342 len++;
343 *s++ = *p++;
344 if (len > 4095)
345 break; /* Too long, stop */
346 } else {
347 /* End of string */
348 *s = '\0';
349 return line;
352 /* End of buffer */
353 return NULL;
356 void release_file(void *file, unsigned long size)
358 munmap(file, size);
361 static int parse_elf(struct elf_info *info, const char *filename)
363 unsigned int i;
364 Elf_Ehdr *hdr;
365 Elf_Shdr *sechdrs;
366 Elf_Sym *sym;
368 hdr = grab_file(filename, &info->size);
369 if (!hdr) {
370 perror(filename);
371 exit(1);
373 info->hdr = hdr;
374 if (info->size < sizeof(*hdr)) {
375 /* file too small, assume this is an empty .o file */
376 return 0;
378 /* Is this a valid ELF file? */
379 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
380 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
381 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
382 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
383 /* Not an ELF file - silently ignore it */
384 return 0;
386 /* Fix endianness in ELF header */
387 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
388 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
389 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
390 hdr->e_machine = TO_NATIVE(hdr->e_machine);
391 hdr->e_type = TO_NATIVE(hdr->e_type);
392 sechdrs = (void *)hdr + hdr->e_shoff;
393 info->sechdrs = sechdrs;
395 /* Check if file offset is correct */
396 if (hdr->e_shoff > info->size) {
397 fatal("section header offset=%lu in file '%s' is bigger than "
398 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
399 filename, info->size);
400 return 0;
403 /* Fix endianness in section headers */
404 for (i = 0; i < hdr->e_shnum; i++) {
405 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
406 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
407 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
408 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
409 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
410 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
411 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
413 /* Find symbol table. */
414 for (i = 1; i < hdr->e_shnum; i++) {
415 const char *secstrings
416 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
417 const char *secname;
419 if (sechdrs[i].sh_offset > info->size) {
420 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
421 "sizeof(*hrd)=%zu\n", filename,
422 (unsigned long)sechdrs[i].sh_offset,
423 sizeof(*hdr));
424 return 0;
426 secname = secstrings + sechdrs[i].sh_name;
427 if (strcmp(secname, ".modinfo") == 0) {
428 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
429 info->modinfo_len = sechdrs[i].sh_size;
430 } else if (strcmp(secname, "__ksymtab") == 0)
431 info->export_sec = i;
432 else if (strcmp(secname, "__ksymtab_unused") == 0)
433 info->export_unused_sec = i;
434 else if (strcmp(secname, "__ksymtab_gpl") == 0)
435 info->export_gpl_sec = i;
436 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
437 info->export_unused_gpl_sec = i;
438 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
439 info->export_gpl_future_sec = i;
440 else if (strcmp(secname, "__markers_strings") == 0)
441 info->markers_strings_sec = i;
443 if (sechdrs[i].sh_type != SHT_SYMTAB)
444 continue;
446 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
447 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
448 + sechdrs[i].sh_size;
449 info->strtab = (void *)hdr +
450 sechdrs[sechdrs[i].sh_link].sh_offset;
452 if (!info->symtab_start)
453 fatal("%s has no symtab?\n", filename);
455 /* Fix endianness in symbols */
456 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
457 sym->st_shndx = TO_NATIVE(sym->st_shndx);
458 sym->st_name = TO_NATIVE(sym->st_name);
459 sym->st_value = TO_NATIVE(sym->st_value);
460 sym->st_size = TO_NATIVE(sym->st_size);
462 return 1;
465 static void parse_elf_finish(struct elf_info *info)
467 release_file(info->hdr, info->size);
470 #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
471 #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
473 static void handle_modversions(struct module *mod, struct elf_info *info,
474 Elf_Sym *sym, const char *symname)
476 unsigned int crc;
477 enum export export = export_from_sec(info, sym->st_shndx);
479 switch (sym->st_shndx) {
480 case SHN_COMMON:
481 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
482 break;
483 case SHN_ABS:
484 /* CRC'd symbol */
485 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
486 crc = (unsigned int) sym->st_value;
487 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
488 export);
490 break;
491 case SHN_UNDEF:
492 /* undefined symbol */
493 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
494 ELF_ST_BIND(sym->st_info) != STB_WEAK)
495 break;
496 /* ignore global offset table */
497 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
498 break;
499 /* ignore __this_module, it will be resolved shortly */
500 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
501 break;
502 /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
503 #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
504 /* add compatibility with older glibc */
505 #ifndef STT_SPARC_REGISTER
506 #define STT_SPARC_REGISTER STT_REGISTER
507 #endif
508 if (info->hdr->e_machine == EM_SPARC ||
509 info->hdr->e_machine == EM_SPARCV9) {
510 /* Ignore register directives. */
511 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
512 break;
513 if (symname[0] == '.') {
514 char *munged = strdup(symname);
515 munged[0] = '_';
516 munged[1] = toupper(munged[1]);
517 symname = munged;
520 #endif
522 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
523 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
524 mod->unres =
525 alloc_symbol(symname +
526 strlen(MODULE_SYMBOL_PREFIX),
527 ELF_ST_BIND(sym->st_info) == STB_WEAK,
528 mod->unres);
530 break;
531 default:
532 /* All exported symbols */
533 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
534 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
535 export);
537 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
538 mod->has_init = 1;
539 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
540 mod->has_cleanup = 1;
541 break;
546 * Parse tag=value strings from .modinfo section
548 static char *next_string(char *string, unsigned long *secsize)
550 /* Skip non-zero chars */
551 while (string[0]) {
552 string++;
553 if ((*secsize)-- <= 1)
554 return NULL;
557 /* Skip any zero padding. */
558 while (!string[0]) {
559 string++;
560 if ((*secsize)-- <= 1)
561 return NULL;
563 return string;
566 static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
567 const char *tag, char *info)
569 char *p;
570 unsigned int taglen = strlen(tag);
571 unsigned long size = modinfo_len;
573 if (info) {
574 size -= info - (char *)modinfo;
575 modinfo = next_string(info, &size);
578 for (p = modinfo; p; p = next_string(p, &size)) {
579 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
580 return p + taglen + 1;
582 return NULL;
585 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
586 const char *tag)
589 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
593 * Test if string s ends in string sub
594 * return 0 if match
596 static int strrcmp(const char *s, const char *sub)
598 int slen, sublen;
600 if (!s || !sub)
601 return 1;
603 slen = strlen(s);
604 sublen = strlen(sub);
606 if ((slen == 0) || (sublen == 0))
607 return 1;
609 if (sublen > slen)
610 return 1;
612 return memcmp(s + slen - sublen, sub, sublen);
615 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
617 if (sym)
618 return elf->strtab + sym->st_name;
619 else
620 return "(unknown)";
623 static const char *sec_name(struct elf_info *elf, int shndx)
625 Elf_Shdr *sechdrs = elf->sechdrs;
626 return (void *)elf->hdr +
627 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
628 sechdrs[shndx].sh_name;
631 static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
633 return (void *)elf->hdr +
634 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
635 sechdr->sh_name;
638 /* if sym is empty or point to a string
639 * like ".[0-9]+" then return 1.
640 * This is the optional prefix added by ld to some sections
642 static int number_prefix(const char *sym)
644 if (*sym++ == '\0')
645 return 1;
646 if (*sym != '.')
647 return 0;
648 do {
649 char c = *sym++;
650 if (c < '0' || c > '9')
651 return 0;
652 } while (*sym);
653 return 1;
656 /* The pattern is an array of simple patterns.
657 * "foo" will match an exact string equal to "foo"
658 * "*foo" will match a string that ends with "foo"
659 * "foo*" will match a string that begins with "foo"
660 * "foo$" will match a string equal to "foo" or "foo.1"
661 * where the '1' can be any number including several digits.
662 * The $ syntax is for sections where ld append a dot number
663 * to make section name unique.
665 int match(const char *sym, const char * const pat[])
667 const char *p;
668 while (*pat) {
669 p = *pat++;
670 const char *endp = p + strlen(p) - 1;
672 /* "*foo" */
673 if (*p == '*') {
674 if (strrcmp(sym, p + 1) == 0)
675 return 1;
677 /* "foo*" */
678 else if (*endp == '*') {
679 if (strncmp(sym, p, strlen(p) - 1) == 0)
680 return 1;
682 /* "foo$" */
683 else if (*endp == '$') {
684 if (strncmp(sym, p, strlen(p) - 1) == 0) {
685 if (number_prefix(sym + strlen(p) - 1))
686 return 1;
689 /* no wildcards */
690 else {
691 if (strcmp(p, sym) == 0)
692 return 1;
695 /* no match */
696 return 0;
699 /* sections that we do not want to do full section mismatch check on */
700 static const char *section_white_list[] =
701 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
704 * Is this section one we do not want to check?
705 * This is often debug sections.
706 * If we are going to check this section then
707 * test if section name ends with a dot and a number.
708 * This is used to find sections where the linker have
709 * appended a dot-number to make the name unique.
710 * The cause of this is often a section specified in assembler
711 * without "ax" / "aw" and the same section used in .c
712 * code where gcc add these.
714 static int check_section(const char *modname, const char *sec)
716 const char *e = sec + strlen(sec) - 1;
717 if (match(sec, section_white_list))
718 return 1;
720 if (*e && isdigit(*e)) {
721 /* consume all digits */
722 while (*e && e != sec && isdigit(*e))
723 e--;
724 if (*e == '.') {
725 warn("%s (%s): unexpected section name.\n"
726 "The (.[number]+) following section name are "
727 "ld generated and not expected.\n"
728 "Did you forget to use \"ax\"/\"aw\" "
729 "in a .S file?\n"
730 "Note that for example <linux/init.h> contains\n"
731 "section definitions for use in .S files.\n\n",
732 modname, sec);
735 return 0;
740 #define ALL_INIT_DATA_SECTIONS \
741 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
742 #define ALL_EXIT_DATA_SECTIONS \
743 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
745 #define ALL_INIT_TEXT_SECTIONS \
746 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
747 #define ALL_EXIT_TEXT_SECTIONS \
748 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
750 #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
751 #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
753 #define DATA_SECTIONS ".data$", ".data.rel$"
754 #define TEXT_SECTIONS ".text$"
756 #define INIT_SECTIONS ".init.data$", ".init.text$"
757 #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
758 #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
759 #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
761 #define EXIT_SECTIONS ".exit.data$", ".exit.text$"
762 #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
763 #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
764 #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
766 /* init data sections */
767 static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
769 /* all init sections */
770 static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
772 /* All init and exit sections (code + data) */
773 static const char *init_exit_sections[] =
774 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
776 /* data section */
777 static const char *data_sections[] = { DATA_SECTIONS, NULL };
779 /* sections that may refer to an init/exit section with no warning */
780 static const char *initref_sections[] =
782 ".text.init.refok*",
783 ".exit.text.refok*",
784 ".data.init.refok*",
785 NULL
789 /* symbols in .data that may refer to init/exit sections */
790 static const char *symbol_white_list[] =
792 "*driver",
793 "*_template", /* scsi uses *_template a lot */
794 "*_timer", /* arm uses ops structures named _timer a lot */
795 "*_sht", /* scsi also used *_sht to some extent */
796 "*_ops",
797 "*_probe",
798 "*_probe_one",
799 "*_console",
800 NULL
803 static const char *head_sections[] = { ".head.text*", NULL };
804 static const char *linker_symbols[] =
805 { "__init_begin", "_sinittext", "_einittext", NULL };
807 enum mismatch {
808 NO_MISMATCH,
809 TEXT_TO_INIT,
810 DATA_TO_INIT,
811 TEXT_TO_EXIT,
812 DATA_TO_EXIT,
813 XXXINIT_TO_INIT,
814 XXXEXIT_TO_EXIT,
815 INIT_TO_EXIT,
816 EXIT_TO_INIT,
817 EXPORT_TO_INIT_EXIT,
820 struct sectioncheck {
821 const char *fromsec[20];
822 const char *tosec[20];
823 enum mismatch mismatch;
826 const struct sectioncheck sectioncheck[] = {
827 /* Do not reference init/exit code/data from
828 * normal code and data
831 .fromsec = { TEXT_SECTIONS, NULL },
832 .tosec = { ALL_INIT_SECTIONS, NULL },
833 .mismatch = TEXT_TO_INIT,
836 .fromsec = { DATA_SECTIONS, NULL },
837 .tosec = { ALL_INIT_SECTIONS, NULL },
838 .mismatch = DATA_TO_INIT,
841 .fromsec = { TEXT_SECTIONS, NULL },
842 .tosec = { ALL_EXIT_SECTIONS, NULL },
843 .mismatch = TEXT_TO_EXIT,
846 .fromsec = { DATA_SECTIONS, NULL },
847 .tosec = { ALL_EXIT_SECTIONS, NULL },
848 .mismatch = DATA_TO_EXIT,
850 /* Do not reference init code/data from devinit/cpuinit/meminit code/data */
852 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
853 .tosec = { INIT_SECTIONS, NULL },
854 .mismatch = XXXINIT_TO_INIT,
856 /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
858 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
859 .tosec = { EXIT_SECTIONS, NULL },
860 .mismatch = XXXEXIT_TO_EXIT,
862 /* Do not use exit code/data from init code */
864 .fromsec = { ALL_INIT_SECTIONS, NULL },
865 .tosec = { ALL_EXIT_SECTIONS, NULL },
866 .mismatch = INIT_TO_EXIT,
868 /* Do not use init code/data from exit code */
870 .fromsec = { ALL_EXIT_SECTIONS, NULL },
871 .tosec = { ALL_INIT_SECTIONS, NULL },
872 .mismatch = EXIT_TO_INIT,
874 /* Do not export init/exit functions or data */
876 .fromsec = { "__ksymtab*", NULL },
877 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
878 .mismatch = EXPORT_TO_INIT_EXIT
882 static int section_mismatch(const char *fromsec, const char *tosec)
884 int i;
885 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
886 const struct sectioncheck *check = &sectioncheck[0];
888 for (i = 0; i < elems; i++) {
889 if (match(fromsec, check->fromsec) &&
890 match(tosec, check->tosec))
891 return check->mismatch;
892 check++;
894 return NO_MISMATCH;
898 * Whitelist to allow certain references to pass with no warning.
900 * Pattern 0:
901 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
902 * The pattern is identified by:
903 * fromsec = .text.init.refok* | .data.init.refok*
905 * Pattern 1:
906 * If a module parameter is declared __initdata and permissions=0
907 * then this is legal despite the warning generated.
908 * We cannot see value of permissions here, so just ignore
909 * this pattern.
910 * The pattern is identified by:
911 * tosec = .init.data
912 * fromsec = .data*
913 * atsym =__param*
915 * Pattern 2:
916 * Many drivers utilise a *driver container with references to
917 * add, remove, probe functions etc.
918 * These functions may often be marked __init and we do not want to
919 * warn here.
920 * the pattern is identified by:
921 * tosec = init or exit section
922 * fromsec = data section
923 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
924 * *probe_one, *_console, *_timer
926 * Pattern 3:
927 * Whitelist all refereces from .text.head to .init.data
928 * Whitelist all refereces from .text.head to .init.text
930 * Pattern 4:
931 * Some symbols belong to init section but still it is ok to reference
932 * these from non-init sections as these symbols don't have any memory
933 * allocated for them and symbol address and value are same. So even
934 * if init section is freed, its ok to reference those symbols.
935 * For ex. symbols marking the init section boundaries.
936 * This pattern is identified by
937 * refsymname = __init_begin, _sinittext, _einittext
940 static int secref_whitelist(const char *fromsec, const char *fromsym,
941 const char *tosec, const char *tosym)
943 /* Check for pattern 0 */
944 if (match(fromsec, initref_sections))
945 return 0;
947 /* Check for pattern 1 */
948 if (match(tosec, init_data_sections) &&
949 match(fromsec, data_sections) &&
950 (strncmp(fromsym, "__param", strlen("__param")) == 0))
951 return 0;
953 /* Check for pattern 2 */
954 if (match(tosec, init_exit_sections) &&
955 match(fromsec, data_sections) &&
956 match(fromsym, symbol_white_list))
957 return 0;
959 /* Check for pattern 3 */
960 if (match(fromsec, head_sections) &&
961 match(tosec, init_sections))
962 return 0;
964 /* Check for pattern 4 */
965 if (match(tosym, linker_symbols))
966 return 0;
968 return 1;
972 * Find symbol based on relocation record info.
973 * In some cases the symbol supplied is a valid symbol so
974 * return refsym. If st_name != 0 we assume this is a valid symbol.
975 * In other cases the symbol needs to be looked up in the symbol table
976 * based on section and address.
977 * **/
978 static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
979 Elf_Sym *relsym)
981 Elf_Sym *sym;
982 Elf_Sym *near = NULL;
983 Elf64_Sword distance = 20;
984 Elf64_Sword d;
986 if (relsym->st_name != 0)
987 return relsym;
988 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
989 if (sym->st_shndx != relsym->st_shndx)
990 continue;
991 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
992 continue;
993 if (sym->st_value == addr)
994 return sym;
995 /* Find a symbol nearby - addr are maybe negative */
996 d = sym->st_value - addr;
997 if (d < 0)
998 d = addr - sym->st_value;
999 if (d < distance) {
1000 distance = d;
1001 near = sym;
1004 /* We need a close match */
1005 if (distance < 20)
1006 return near;
1007 else
1008 return NULL;
1011 static inline int is_arm_mapping_symbol(const char *str)
1013 return str[0] == '$' && strchr("atd", str[1])
1014 && (str[2] == '\0' || str[2] == '.');
1018 * If there's no name there, ignore it; likewise, ignore it if it's
1019 * one of the magic symbols emitted used by current ARM tools.
1021 * Otherwise if find_symbols_between() returns those symbols, they'll
1022 * fail the whitelist tests and cause lots of false alarms ... fixable
1023 * only by merging __exit and __init sections into __text, bloating
1024 * the kernel (which is especially evil on embedded platforms).
1026 static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1028 const char *name = elf->strtab + sym->st_name;
1030 if (!name || !strlen(name))
1031 return 0;
1032 return !is_arm_mapping_symbol(name);
1036 * Find symbols before or equal addr and after addr - in the section sec.
1037 * If we find two symbols with equal offset prefer one with a valid name.
1038 * The ELF format may have a better way to detect what type of symbol
1039 * it is, but this works for now.
1041 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1042 const char *sec)
1044 Elf_Sym *sym;
1045 Elf_Sym *near = NULL;
1046 Elf_Addr distance = ~0;
1048 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1049 const char *symsec;
1051 if (sym->st_shndx >= SHN_LORESERVE)
1052 continue;
1053 symsec = sec_name(elf, sym->st_shndx);
1054 if (strcmp(symsec, sec) != 0)
1055 continue;
1056 if (!is_valid_name(elf, sym))
1057 continue;
1058 if (sym->st_value <= addr) {
1059 if ((addr - sym->st_value) < distance) {
1060 distance = addr - sym->st_value;
1061 near = sym;
1062 } else if ((addr - sym->st_value) == distance) {
1063 near = sym;
1067 return near;
1071 * Convert a section name to the function/data attribute
1072 * .init.text => __init
1073 * .cpuinit.data => __cpudata
1074 * .memexitconst => __memconst
1075 * etc.
1077 static char *sec2annotation(const char *s)
1079 if (match(s, init_exit_sections)) {
1080 char *p = malloc(20);
1081 char *r = p;
1083 *p++ = '_';
1084 *p++ = '_';
1085 if (*s == '.')
1086 s++;
1087 while (*s && *s != '.')
1088 *p++ = *s++;
1089 *p = '\0';
1090 if (*s == '.')
1091 s++;
1092 if (strstr(s, "rodata") != NULL)
1093 strcat(p, "const ");
1094 else if (strstr(s, "data") != NULL)
1095 strcat(p, "data ");
1096 else
1097 strcat(p, " ");
1098 return r; /* we leak her but we do not care */
1099 } else {
1100 return "";
1104 static int is_function(Elf_Sym *sym)
1106 if (sym)
1107 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1108 else
1109 return -1;
1113 * Print a warning about a section mismatch.
1114 * Try to find symbols near it so user can find it.
1115 * Check whitelist before warning - it may be a false positive.
1117 static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
1118 const char *fromsec,
1119 unsigned long long fromaddr,
1120 const char *fromsym,
1121 int from_is_func,
1122 const char *tosec, const char *tosym,
1123 int to_is_func)
1125 const char *from, *from_p;
1126 const char *to, *to_p;
1128 switch (from_is_func) {
1129 case 0: from = "variable"; from_p = ""; break;
1130 case 1: from = "function"; from_p = "()"; break;
1131 default: from = "(unknown reference)"; from_p = ""; break;
1133 switch (to_is_func) {
1134 case 0: to = "variable"; to_p = ""; break;
1135 case 1: to = "function"; to_p = "()"; break;
1136 default: to = "(unknown reference)"; to_p = ""; break;
1139 sec_mismatch_count++;
1140 if (!sec_mismatch_verbose)
1141 return;
1143 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1144 "to the %s %s:%s%s\n",
1145 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1146 tosym, to_p);
1148 switch (mismatch) {
1149 case TEXT_TO_INIT:
1150 fprintf(stderr,
1151 "The function %s%s() references\n"
1152 "the %s %s%s%s.\n"
1153 "This is often because %s lacks a %s\n"
1154 "annotation or the annotation of %s is wrong.\n",
1155 sec2annotation(fromsec), fromsym,
1156 to, sec2annotation(tosec), tosym, to_p,
1157 fromsym, sec2annotation(tosec), tosym);
1158 break;
1159 case DATA_TO_INIT: {
1160 const char **s = symbol_white_list;
1161 fprintf(stderr,
1162 "The variable %s references\n"
1163 "the %s %s%s%s\n"
1164 "If the reference is valid then annotate the\n"
1165 "variable with __init* (see linux/init.h) "
1166 "or name the variable:\n",
1167 fromsym, to, sec2annotation(tosec), tosym, to_p);
1168 while (*s)
1169 fprintf(stderr, "%s, ", *s++);
1170 fprintf(stderr, "\n");
1171 break;
1173 case TEXT_TO_EXIT:
1174 fprintf(stderr,
1175 "The function %s() references a %s in an exit section.\n"
1176 "Often the %s %s%s has valid usage outside the exit section\n"
1177 "and the fix is to remove the %sannotation of %s.\n",
1178 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1179 break;
1180 case DATA_TO_EXIT: {
1181 const char **s = symbol_white_list;
1182 fprintf(stderr,
1183 "The variable %s references\n"
1184 "the %s %s%s%s\n"
1185 "If the reference is valid then annotate the\n"
1186 "variable with __exit* (see linux/init.h) or "
1187 "name the variable:\n",
1188 fromsym, to, sec2annotation(tosec), tosym, to_p);
1189 while (*s)
1190 fprintf(stderr, "%s, ", *s++);
1191 fprintf(stderr, "\n");
1192 break;
1194 case XXXINIT_TO_INIT:
1195 case XXXEXIT_TO_EXIT:
1196 fprintf(stderr,
1197 "The %s %s%s%s references\n"
1198 "a %s %s%s%s.\n"
1199 "If %s is only used by %s then\n"
1200 "annotate %s with a matching annotation.\n",
1201 from, sec2annotation(fromsec), fromsym, from_p,
1202 to, sec2annotation(tosec), tosym, to_p,
1203 <<<<<<< HEAD:scripts/mod/modpost.c
1204 fromsym, tosym, fromsym);
1205 =======
1206 tosym, fromsym, tosym);
1207 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:scripts/mod/modpost.c
1208 break;
1209 case INIT_TO_EXIT:
1210 fprintf(stderr,
1211 "The %s %s%s%s references\n"
1212 "a %s %s%s%s.\n"
1213 "This is often seen when error handling "
1214 "in the init function\n"
1215 "uses functionality in the exit path.\n"
1216 "The fix is often to remove the %sannotation of\n"
1217 "%s%s so it may be used outside an exit section.\n",
1218 from, sec2annotation(fromsec), fromsym, from_p,
1219 to, sec2annotation(tosec), tosym, to_p,
1220 sec2annotation(tosec), tosym, to_p);
1221 break;
1222 case EXIT_TO_INIT:
1223 fprintf(stderr,
1224 "The %s %s%s%s references\n"
1225 "a %s %s%s%s.\n"
1226 "This is often seen when error handling "
1227 "in the exit function\n"
1228 "uses functionality in the init path.\n"
1229 "The fix is often to remove the %sannotation of\n"
1230 "%s%s so it may be used outside an init section.\n",
1231 from, sec2annotation(fromsec), fromsym, from_p,
1232 to, sec2annotation(tosec), tosym, to_p,
1233 sec2annotation(tosec), tosym, to_p);
1234 break;
1235 case EXPORT_TO_INIT_EXIT:
1236 fprintf(stderr,
1237 "The symbol %s is exported and annotated %s\n"
1238 "Fix this by removing the %sannotation of %s "
1239 "or drop the export.\n",
1240 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1241 case NO_MISMATCH:
1242 /* To get warnings on missing members */
1243 break;
1245 fprintf(stderr, "\n");
1248 static void check_section_mismatch(const char *modname, struct elf_info *elf,
1249 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1251 const char *tosec;
1252 enum mismatch mismatch;
1254 tosec = sec_name(elf, sym->st_shndx);
1255 mismatch = section_mismatch(fromsec, tosec);
1256 if (mismatch != NO_MISMATCH) {
1257 Elf_Sym *to;
1258 Elf_Sym *from;
1259 const char *tosym;
1260 const char *fromsym;
1262 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1263 fromsym = sym_name(elf, from);
1264 to = find_elf_symbol(elf, r->r_addend, sym);
1265 tosym = sym_name(elf, to);
1267 /* check whitelist - we may ignore it */
1268 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
1269 report_sec_mismatch(modname, mismatch,
1270 fromsec, r->r_offset, fromsym,
1271 is_function(from), tosec, tosym,
1272 is_function(to));
1277 static unsigned int *reloc_location(struct elf_info *elf,
1278 Elf_Shdr *sechdr, Elf_Rela *r)
1280 Elf_Shdr *sechdrs = elf->sechdrs;
1281 int section = sechdr->sh_info;
1283 return (void *)elf->hdr + sechdrs[section].sh_offset +
1284 (r->r_offset - sechdrs[section].sh_addr);
1287 static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1289 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1290 unsigned int *location = reloc_location(elf, sechdr, r);
1292 switch (r_typ) {
1293 case R_386_32:
1294 r->r_addend = TO_NATIVE(*location);
1295 break;
1296 case R_386_PC32:
1297 r->r_addend = TO_NATIVE(*location) + 4;
1298 /* For CONFIG_RELOCATABLE=y */
1299 if (elf->hdr->e_type == ET_EXEC)
1300 r->r_addend += r->r_offset;
1301 break;
1303 return 0;
1306 static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1308 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1310 switch (r_typ) {
1311 case R_ARM_ABS32:
1312 /* From ARM ABI: (S + A) | T */
1313 r->r_addend = (int)(long)
1314 (elf->symtab_start + ELF_R_SYM(r->r_info));
1315 break;
1316 case R_ARM_PC24:
1317 /* From ARM ABI: ((S + A) | T) - P */
1318 r->r_addend = (int)(long)(elf->hdr +
1319 sechdr->sh_offset +
1320 (r->r_offset - sechdr->sh_addr));
1321 break;
1322 default:
1323 return 1;
1325 return 0;
1328 static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1330 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1331 unsigned int *location = reloc_location(elf, sechdr, r);
1332 unsigned int inst;
1334 if (r_typ == R_MIPS_HI16)
1335 return 1; /* skip this */
1336 inst = TO_NATIVE(*location);
1337 switch (r_typ) {
1338 case R_MIPS_LO16:
1339 r->r_addend = inst & 0xffff;
1340 break;
1341 case R_MIPS_26:
1342 r->r_addend = (inst & 0x03ffffff) << 2;
1343 break;
1344 case R_MIPS_32:
1345 r->r_addend = inst;
1346 break;
1348 return 0;
1351 static void section_rela(const char *modname, struct elf_info *elf,
1352 Elf_Shdr *sechdr)
1354 Elf_Sym *sym;
1355 Elf_Rela *rela;
1356 Elf_Rela r;
1357 unsigned int r_sym;
1358 const char *fromsec;
1360 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1361 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1363 fromsec = sech_name(elf, sechdr);
1364 fromsec += strlen(".rela");
1365 /* if from section (name) is know good then skip it */
1366 if (check_section(modname, fromsec))
1367 return;
1369 for (rela = start; rela < stop; rela++) {
1370 r.r_offset = TO_NATIVE(rela->r_offset);
1371 #if KERNEL_ELFCLASS == ELFCLASS64
1372 if (elf->hdr->e_machine == EM_MIPS) {
1373 unsigned int r_typ;
1374 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1375 r_sym = TO_NATIVE(r_sym);
1376 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1377 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1378 } else {
1379 r.r_info = TO_NATIVE(rela->r_info);
1380 r_sym = ELF_R_SYM(r.r_info);
1382 #else
1383 r.r_info = TO_NATIVE(rela->r_info);
1384 r_sym = ELF_R_SYM(r.r_info);
1385 #endif
1386 r.r_addend = TO_NATIVE(rela->r_addend);
1387 sym = elf->symtab_start + r_sym;
1388 /* Skip special sections */
1389 if (sym->st_shndx >= SHN_LORESERVE)
1390 continue;
1391 check_section_mismatch(modname, elf, &r, sym, fromsec);
1395 static void section_rel(const char *modname, struct elf_info *elf,
1396 Elf_Shdr *sechdr)
1398 Elf_Sym *sym;
1399 Elf_Rel *rel;
1400 Elf_Rela r;
1401 unsigned int r_sym;
1402 const char *fromsec;
1404 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1405 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1407 fromsec = sech_name(elf, sechdr);
1408 fromsec += strlen(".rel");
1409 /* if from section (name) is know good then skip it */
1410 if (check_section(modname, fromsec))
1411 return;
1413 for (rel = start; rel < stop; rel++) {
1414 r.r_offset = TO_NATIVE(rel->r_offset);
1415 #if KERNEL_ELFCLASS == ELFCLASS64
1416 if (elf->hdr->e_machine == EM_MIPS) {
1417 unsigned int r_typ;
1418 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1419 r_sym = TO_NATIVE(r_sym);
1420 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1421 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1422 } else {
1423 r.r_info = TO_NATIVE(rel->r_info);
1424 r_sym = ELF_R_SYM(r.r_info);
1426 #else
1427 r.r_info = TO_NATIVE(rel->r_info);
1428 r_sym = ELF_R_SYM(r.r_info);
1429 #endif
1430 r.r_addend = 0;
1431 switch (elf->hdr->e_machine) {
1432 case EM_386:
1433 if (addend_386_rel(elf, sechdr, &r))
1434 continue;
1435 break;
1436 case EM_ARM:
1437 if (addend_arm_rel(elf, sechdr, &r))
1438 continue;
1439 break;
1440 case EM_MIPS:
1441 if (addend_mips_rel(elf, sechdr, &r))
1442 continue;
1443 break;
1445 sym = elf->symtab_start + r_sym;
1446 /* Skip special sections */
1447 if (sym->st_shndx >= SHN_LORESERVE)
1448 continue;
1449 check_section_mismatch(modname, elf, &r, sym, fromsec);
1454 * A module includes a number of sections that are discarded
1455 * either when loaded or when used as built-in.
1456 * For loaded modules all functions marked __init and all data
1457 * marked __initdata will be discarded when the module has been intialized.
1458 * Likewise for modules used built-in the sections marked __exit
1459 * are discarded because __exit marked function are supposed to be called
1460 * only when a moduel is unloaded which never happes for built-in modules.
1461 * The check_sec_ref() function traverses all relocation records
1462 * to find all references to a section that reference a section that will
1463 * be discarded and warns about it.
1465 static void check_sec_ref(struct module *mod, const char *modname,
1466 struct elf_info *elf)
1468 int i;
1469 Elf_Shdr *sechdrs = elf->sechdrs;
1471 /* Walk through all sections */
1472 for (i = 0; i < elf->hdr->e_shnum; i++) {
1473 /* We want to process only relocation sections and not .init */
1474 if (sechdrs[i].sh_type == SHT_RELA)
1475 section_rela(modname, elf, &elf->sechdrs[i]);
1476 else if (sechdrs[i].sh_type == SHT_REL)
1477 section_rel(modname, elf, &elf->sechdrs[i]);
1481 static void get_markers(struct elf_info *info, struct module *mod)
1483 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1484 const char *strings = (const char *) info->hdr + sh->sh_offset;
1485 const Elf_Sym *sym, *first_sym, *last_sym;
1486 size_t n;
1488 if (!info->markers_strings_sec)
1489 return;
1492 * First count the strings. We look for all the symbols defined
1493 * in the __markers_strings section named __mstrtab_*. For
1494 * these local names, the compiler puts a random .NNN suffix on,
1495 * so the names don't correspond exactly.
1497 first_sym = last_sym = NULL;
1498 n = 0;
1499 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1500 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1501 sym->st_shndx == info->markers_strings_sec &&
1502 !strncmp(info->strtab + sym->st_name,
1503 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1504 if (first_sym == NULL)
1505 first_sym = sym;
1506 last_sym = sym;
1507 ++n;
1510 if (n == 0)
1511 return;
1514 * Now collect each name and format into a line for the output.
1515 * Lines look like:
1516 * marker_name vmlinux marker %s format %d
1517 * The format string after the second \t can use whitespace.
1519 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1520 mod->nmarkers = n;
1522 n = 0;
1523 for (sym = first_sym; sym <= last_sym; sym++)
1524 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1525 sym->st_shndx == info->markers_strings_sec &&
1526 !strncmp(info->strtab + sym->st_name,
1527 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1528 const char *name = strings + sym->st_value;
1529 const char *fmt = strchr(name, '\0') + 1;
1530 char *line = NULL;
1531 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1532 NOFAIL(line);
1533 mod->markers[n++] = line;
1537 static void read_symbols(char *modname)
1539 const char *symname;
1540 char *version;
1541 char *license;
1542 struct module *mod;
1543 struct elf_info info = { };
1544 Elf_Sym *sym;
1546 if (!parse_elf(&info, modname))
1547 return;
1549 mod = new_module(modname);
1551 /* When there's no vmlinux, don't print warnings about
1552 * unresolved symbols (since there'll be too many ;) */
1553 if (is_vmlinux(modname)) {
1554 have_vmlinux = 1;
1555 mod->skip = 1;
1558 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1559 while (license) {
1560 if (license_is_gpl_compatible(license))
1561 mod->gpl_compatible = 1;
1562 else {
1563 mod->gpl_compatible = 0;
1564 break;
1566 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1567 "license", license);
1570 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1571 symname = info.strtab + sym->st_name;
1573 handle_modversions(mod, &info, sym, symname);
1574 handle_moddevtable(mod, &info, sym, symname);
1576 if (!is_vmlinux(modname) ||
1577 (is_vmlinux(modname) && vmlinux_section_warnings))
1578 check_sec_ref(mod, modname, &info);
1580 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1581 if (version)
1582 maybe_frob_rcs_version(modname, version, info.modinfo,
1583 version - (char *)info.hdr);
1584 if (version || (all_versions && !is_vmlinux(modname)))
1585 get_src_version(modname, mod->srcversion,
1586 sizeof(mod->srcversion)-1);
1588 get_markers(&info, mod);
1590 parse_elf_finish(&info);
1592 /* Our trick to get versioning for struct_module - it's
1593 * never passed as an argument to an exported function, so
1594 * the automatic versioning doesn't pick it up, but it's really
1595 * important anyhow */
1596 if (modversions)
1597 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1600 #define SZ 500
1602 /* We first write the generated file into memory using the
1603 * following helper, then compare to the file on disk and
1604 * only update the later if anything changed */
1606 void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1607 const char *fmt, ...)
1609 char tmp[SZ];
1610 int len;
1611 va_list ap;
1613 va_start(ap, fmt);
1614 len = vsnprintf(tmp, SZ, fmt, ap);
1615 buf_write(buf, tmp, len);
1616 va_end(ap);
1619 void buf_write(struct buffer *buf, const char *s, int len)
1621 if (buf->size - buf->pos < len) {
1622 buf->size += len + SZ;
1623 buf->p = realloc(buf->p, buf->size);
1625 strncpy(buf->p + buf->pos, s, len);
1626 buf->pos += len;
1629 static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1631 const char *e = is_vmlinux(m) ?"":".ko";
1633 switch (exp) {
1634 case export_gpl:
1635 fatal("modpost: GPL-incompatible module %s%s "
1636 "uses GPL-only symbol '%s'\n", m, e, s);
1637 break;
1638 case export_unused_gpl:
1639 fatal("modpost: GPL-incompatible module %s%s "
1640 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1641 break;
1642 case export_gpl_future:
1643 warn("modpost: GPL-incompatible module %s%s "
1644 "uses future GPL-only symbol '%s'\n", m, e, s);
1645 break;
1646 case export_plain:
1647 case export_unused:
1648 case export_unknown:
1649 /* ignore */
1650 break;
1654 static void check_for_unused(enum export exp, const char *m, const char *s)
1656 const char *e = is_vmlinux(m) ?"":".ko";
1658 switch (exp) {
1659 case export_unused:
1660 case export_unused_gpl:
1661 warn("modpost: module %s%s "
1662 "uses symbol '%s' marked UNUSED\n", m, e, s);
1663 break;
1664 default:
1665 /* ignore */
1666 break;
1670 static void check_exports(struct module *mod)
1672 struct symbol *s, *exp;
1674 for (s = mod->unres; s; s = s->next) {
1675 const char *basename;
1676 exp = find_symbol(s->name);
1677 if (!exp || exp->module == mod)
1678 continue;
1679 basename = strrchr(mod->name, '/');
1680 if (basename)
1681 basename++;
1682 else
1683 basename = mod->name;
1684 if (!mod->gpl_compatible)
1685 check_for_gpl_usage(exp->export, basename, exp->name);
1686 check_for_unused(exp->export, basename, exp->name);
1691 * Header for the generated file
1693 static void add_header(struct buffer *b, struct module *mod)
1695 buf_printf(b, "#include <linux/module.h>\n");
1696 buf_printf(b, "#include <linux/vermagic.h>\n");
1697 buf_printf(b, "#include <linux/compiler.h>\n");
1698 buf_printf(b, "\n");
1699 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1700 buf_printf(b, "\n");
1701 buf_printf(b, "struct module __this_module\n");
1702 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1703 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1704 if (mod->has_init)
1705 buf_printf(b, " .init = init_module,\n");
1706 if (mod->has_cleanup)
1707 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1708 " .exit = cleanup_module,\n"
1709 "#endif\n");
1710 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
1711 buf_printf(b, "};\n");
1715 * Record CRCs for unresolved symbols
1717 static int add_versions(struct buffer *b, struct module *mod)
1719 struct symbol *s, *exp;
1720 int err = 0;
1722 for (s = mod->unres; s; s = s->next) {
1723 exp = find_symbol(s->name);
1724 if (!exp || exp->module == mod) {
1725 if (have_vmlinux && !s->weak) {
1726 if (warn_unresolved) {
1727 warn("\"%s\" [%s.ko] undefined!\n",
1728 s->name, mod->name);
1729 } else {
1730 merror("\"%s\" [%s.ko] undefined!\n",
1731 s->name, mod->name);
1732 err = 1;
1735 continue;
1737 s->module = exp->module;
1738 s->crc_valid = exp->crc_valid;
1739 s->crc = exp->crc;
1742 if (!modversions)
1743 return err;
1745 buf_printf(b, "\n");
1746 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1747 buf_printf(b, "__used\n");
1748 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1750 for (s = mod->unres; s; s = s->next) {
1751 if (!s->module)
1752 continue;
1753 if (!s->crc_valid) {
1754 warn("\"%s\" [%s.ko] has no CRC!\n",
1755 s->name, mod->name);
1756 continue;
1758 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1761 buf_printf(b, "};\n");
1763 return err;
1766 static void add_depends(struct buffer *b, struct module *mod,
1767 struct module *modules)
1769 struct symbol *s;
1770 struct module *m;
1771 int first = 1;
1773 for (m = modules; m; m = m->next)
1774 m->seen = is_vmlinux(m->name);
1776 buf_printf(b, "\n");
1777 buf_printf(b, "static const char __module_depends[]\n");
1778 buf_printf(b, "__used\n");
1779 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1780 buf_printf(b, "\"depends=");
1781 for (s = mod->unres; s; s = s->next) {
1782 const char *p;
1783 if (!s->module)
1784 continue;
1786 if (s->module->seen)
1787 continue;
1789 s->module->seen = 1;
1790 p = strrchr(s->module->name, '/');
1791 if (p)
1792 p++;
1793 else
1794 p = s->module->name;
1795 buf_printf(b, "%s%s", first ? "" : ",", p);
1796 first = 0;
1798 buf_printf(b, "\";\n");
1801 static void add_srcversion(struct buffer *b, struct module *mod)
1803 if (mod->srcversion[0]) {
1804 buf_printf(b, "\n");
1805 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1806 mod->srcversion);
1810 static void write_if_changed(struct buffer *b, const char *fname)
1812 char *tmp;
1813 FILE *file;
1814 struct stat st;
1816 file = fopen(fname, "r");
1817 if (!file)
1818 goto write;
1820 if (fstat(fileno(file), &st) < 0)
1821 goto close_write;
1823 if (st.st_size != b->pos)
1824 goto close_write;
1826 tmp = NOFAIL(malloc(b->pos));
1827 if (fread(tmp, 1, b->pos, file) != b->pos)
1828 goto free_write;
1830 if (memcmp(tmp, b->p, b->pos) != 0)
1831 goto free_write;
1833 free(tmp);
1834 fclose(file);
1835 return;
1837 free_write:
1838 free(tmp);
1839 close_write:
1840 fclose(file);
1841 write:
1842 file = fopen(fname, "w");
1843 if (!file) {
1844 perror(fname);
1845 exit(1);
1847 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1848 perror(fname);
1849 exit(1);
1851 fclose(file);
1854 /* parse Module.symvers file. line format:
1855 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
1857 static void read_dump(const char *fname, unsigned int kernel)
1859 unsigned long size, pos = 0;
1860 void *file = grab_file(fname, &size);
1861 char *line;
1863 if (!file)
1864 /* No symbol versions, silently ignore */
1865 return;
1867 while ((line = get_next_line(&pos, file, size))) {
1868 char *symname, *modname, *d, *export, *end;
1869 unsigned int crc;
1870 struct module *mod;
1871 struct symbol *s;
1873 if (!(symname = strchr(line, '\t')))
1874 goto fail;
1875 *symname++ = '\0';
1876 if (!(modname = strchr(symname, '\t')))
1877 goto fail;
1878 *modname++ = '\0';
1879 if ((export = strchr(modname, '\t')) != NULL)
1880 *export++ = '\0';
1881 if (export && ((end = strchr(export, '\t')) != NULL))
1882 *end = '\0';
1883 crc = strtoul(line, &d, 16);
1884 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1885 goto fail;
1886 mod = find_module(modname);
1887 if (!mod) {
1888 if (is_vmlinux(modname))
1889 have_vmlinux = 1;
1890 mod = new_module(NOFAIL(strdup(modname)));
1891 mod->skip = 1;
1893 s = sym_add_exported(symname, mod, export_no(export));
1894 s->kernel = kernel;
1895 s->preloaded = 1;
1896 sym_update_crc(symname, mod, crc, export_no(export));
1898 return;
1899 fail:
1900 fatal("parse error in symbol dump file\n");
1903 /* For normal builds always dump all symbols.
1904 * For external modules only dump symbols
1905 * that are not read from kernel Module.symvers.
1907 static int dump_sym(struct symbol *sym)
1909 if (!external_module)
1910 return 1;
1911 if (sym->vmlinux || sym->kernel)
1912 return 0;
1913 return 1;
1916 static void write_dump(const char *fname)
1918 struct buffer buf = { };
1919 struct symbol *symbol;
1920 int n;
1922 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1923 symbol = symbolhash[n];
1924 while (symbol) {
1925 if (dump_sym(symbol))
1926 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1927 symbol->crc, symbol->name,
1928 symbol->module->name,
1929 export_str(symbol->export));
1930 symbol = symbol->next;
1933 write_if_changed(&buf, fname);
1936 static void add_marker(struct module *mod, const char *name, const char *fmt)
1938 char *line = NULL;
1939 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1940 NOFAIL(line);
1942 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1943 sizeof mod->markers[0])));
1944 mod->markers[mod->nmarkers++] = line;
1947 static void read_markers(const char *fname)
1949 unsigned long size, pos = 0;
1950 void *file = grab_file(fname, &size);
1951 char *line;
1953 if (!file) /* No old markers, silently ignore */
1954 return;
1956 while ((line = get_next_line(&pos, file, size))) {
1957 char *marker, *modname, *fmt;
1958 struct module *mod;
1960 marker = line;
1961 modname = strchr(marker, '\t');
1962 if (!modname)
1963 goto fail;
1964 *modname++ = '\0';
1965 fmt = strchr(modname, '\t');
1966 if (!fmt)
1967 goto fail;
1968 *fmt++ = '\0';
1969 if (*marker == '\0' || *modname == '\0')
1970 goto fail;
1972 mod = find_module(modname);
1973 if (!mod) {
1974 if (is_vmlinux(modname))
1975 have_vmlinux = 1;
1976 mod = new_module(NOFAIL(strdup(modname)));
1977 mod->skip = 1;
1980 add_marker(mod, marker, fmt);
1982 return;
1983 fail:
1984 fatal("parse error in markers list file\n");
1987 static int compare_strings(const void *a, const void *b)
1989 return strcmp(*(const char **) a, *(const char **) b);
1992 static void write_markers(const char *fname)
1994 struct buffer buf = { };
1995 struct module *mod;
1996 size_t i;
1998 for (mod = modules; mod; mod = mod->next)
1999 if ((!external_module || !mod->skip) && mod->markers != NULL) {
2001 * Sort the strings so we can skip duplicates when
2002 * we write them out.
2004 qsort(mod->markers, mod->nmarkers,
2005 sizeof mod->markers[0], &compare_strings);
2006 for (i = 0; i < mod->nmarkers; ++i) {
2007 char *line = mod->markers[i];
2008 buf_write(&buf, line, strlen(line));
2009 while (i + 1 < mod->nmarkers &&
2010 !strcmp(mod->markers[i],
2011 mod->markers[i + 1]))
2012 free(mod->markers[i++]);
2013 free(mod->markers[i]);
2015 free(mod->markers);
2016 mod->markers = NULL;
2019 write_if_changed(&buf, fname);
2022 int main(int argc, char **argv)
2024 struct module *mod;
2025 struct buffer buf = { };
2026 char *kernel_read = NULL, *module_read = NULL;
2027 char *dump_write = NULL;
2028 char *markers_read = NULL;
2029 char *markers_write = NULL;
2030 int opt;
2031 int err;
2033 while ((opt = getopt(argc, argv, "i:I:msSo:awM:K:")) != -1) {
2034 switch (opt) {
2035 case 'i':
2036 kernel_read = optarg;
2037 break;
2038 case 'I':
2039 module_read = optarg;
2040 external_module = 1;
2041 break;
2042 case 'm':
2043 modversions = 1;
2044 break;
2045 case 'o':
2046 dump_write = optarg;
2047 break;
2048 case 'a':
2049 all_versions = 1;
2050 break;
2051 case 's':
2052 vmlinux_section_warnings = 0;
2053 break;
2054 case 'S':
2055 sec_mismatch_verbose = 0;
2056 break;
2057 case 'w':
2058 warn_unresolved = 1;
2059 break;
2060 case 'M':
2061 markers_write = optarg;
2062 break;
2063 case 'K':
2064 markers_read = optarg;
2065 break;
2066 default:
2067 exit(1);
2071 if (kernel_read)
2072 read_dump(kernel_read, 1);
2073 if (module_read)
2074 read_dump(module_read, 0);
2076 while (optind < argc)
2077 read_symbols(argv[optind++]);
2079 for (mod = modules; mod; mod = mod->next) {
2080 if (mod->skip)
2081 continue;
2082 check_exports(mod);
2085 err = 0;
2087 for (mod = modules; mod; mod = mod->next) {
2088 char fname[strlen(mod->name) + 10];
2090 if (mod->skip)
2091 continue;
2093 buf.pos = 0;
2095 add_header(&buf, mod);
2096 err |= add_versions(&buf, mod);
2097 add_depends(&buf, mod, modules);
2098 add_moddevtable(&buf, mod);
2099 add_srcversion(&buf, mod);
2101 sprintf(fname, "%s.mod.c", mod->name);
2102 write_if_changed(&buf, fname);
2105 if (dump_write)
2106 write_dump(dump_write);
2107 if (sec_mismatch_count && !sec_mismatch_verbose)
2108 warn("modpost: Found %d section mismatch(es).\n"
2109 "To see full details build your kernel with:\n"
2110 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2111 sec_mismatch_count);
2113 if (markers_read)
2114 read_markers(markers_read);
2116 if (markers_write)
2117 write_markers(markers_write);
2119 return err;