aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/mod/modpost.c
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/mod/modpost.c')
-rw-r--r--scripts/mod/modpost.c1210
1 files changed, 715 insertions, 495 deletions
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 93ac52adb498..f8efc93eb700 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -2,7 +2,7 @@
2 * 2 *
3 * Copyright 2003 Kai Germaschewski 3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation 4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006 Sam Ravnborg 5 * Copyright 2006-2008 Sam Ravnborg
6 * Based in part on module-init-tools/depmod.c,file2alias 6 * Based in part on module-init-tools/depmod.c,file2alias
7 * 7 *
8 * This software may be used and distributed according to the terms 8 * This software may be used and distributed according to the terms
@@ -28,12 +28,17 @@ static int vmlinux_section_warnings = 1;
28/* Only warn about unresolved symbols */ 28/* Only warn about unresolved symbols */
29static int warn_unresolved = 0; 29static int warn_unresolved = 0;
30/* How a symbol is exported */ 30/* How a symbol is exported */
31static int sec_mismatch_count = 0;
32static int sec_mismatch_verbose = 1;
33
31enum export { 34enum export {
32 export_plain, export_unused, export_gpl, 35 export_plain, export_unused, export_gpl,
33 export_unused_gpl, export_gpl_future, export_unknown 36 export_unused_gpl, export_gpl_future, export_unknown
34}; 37};
35 38
36void fatal(const char *fmt, ...) 39#define PRINTF __attribute__ ((format (printf, 1, 2)))
40
41PRINTF void fatal(const char *fmt, ...)
37{ 42{
38 va_list arglist; 43 va_list arglist;
39 44
@@ -46,7 +51,7 @@ void fatal(const char *fmt, ...)
46 exit(1); 51 exit(1);
47} 52}
48 53
49void warn(const char *fmt, ...) 54PRINTF void warn(const char *fmt, ...)
50{ 55{
51 va_list arglist; 56 va_list arglist;
52 57
@@ -57,7 +62,7 @@ void warn(const char *fmt, ...)
57 va_end(arglist); 62 va_end(arglist);
58} 63}
59 64
60void merror(const char *fmt, ...) 65PRINTF void merror(const char *fmt, ...)
61{ 66{
62 va_list arglist; 67 va_list arglist;
63 68
@@ -72,7 +77,8 @@ static int is_vmlinux(const char *modname)
72{ 77{
73 const char *myname; 78 const char *myname;
74 79
75 if ((myname = strrchr(modname, '/'))) 80 myname = strrchr(modname, '/');
81 if (myname)
76 myname++; 82 myname++;
77 else 83 else
78 myname = modname; 84 myname = modname;
@@ -83,14 +89,13 @@ static int is_vmlinux(const char *modname)
83 89
84void *do_nofail(void *ptr, const char *expr) 90void *do_nofail(void *ptr, const char *expr)
85{ 91{
86 if (!ptr) { 92 if (!ptr)
87 fatal("modpost: Memory allocation failure: %s.\n", expr); 93 fatal("modpost: Memory allocation failure: %s.\n", expr);
88 } 94
89 return ptr; 95 return ptr;
90} 96}
91 97
92/* A list of all modules we processed */ 98/* A list of all modules we processed */
93
94static struct module *modules; 99static struct module *modules;
95 100
96static struct module *find_module(char *modname) 101static struct module *find_module(char *modname)
@@ -113,7 +118,8 @@ static struct module *new_module(char *modname)
113 p = NOFAIL(strdup(modname)); 118 p = NOFAIL(strdup(modname));
114 119
115 /* strip trailing .o */ 120 /* strip trailing .o */
116 if ((s = strrchr(p, '.')) != NULL) 121 s = strrchr(p, '.');
122 if (s != NULL)
117 if (strcmp(s, ".o") == 0) 123 if (strcmp(s, ".o") == 0)
118 *s = '\0'; 124 *s = '\0';
119 125
@@ -154,7 +160,7 @@ static inline unsigned int tdb_hash(const char *name)
154 unsigned i; /* Used to cycle through random values. */ 160 unsigned i; /* Used to cycle through random values. */
155 161
156 /* Set the initial value from the key size. */ 162 /* Set the initial value from the key size. */
157 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) 163 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
158 value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); 164 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
159 165
160 return (1103515243 * value + 12345); 166 return (1103515243 * value + 12345);
@@ -198,7 +204,7 @@ static struct symbol *find_symbol(const char *name)
198 if (name[0] == '.') 204 if (name[0] == '.')
199 name++; 205 name++;
200 206
201 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) { 207 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
202 if (strcmp(s->name, name) == 0) 208 if (strcmp(s->name, name) == 0)
203 return s; 209 return s;
204 } 210 }
@@ -223,9 +229,10 @@ static const char *export_str(enum export ex)
223 return export_list[ex].str; 229 return export_list[ex].str;
224} 230}
225 231
226static enum export export_no(const char * s) 232static enum export export_no(const char *s)
227{ 233{
228 int i; 234 int i;
235
229 if (!s) 236 if (!s)
230 return export_unknown; 237 return export_unknown;
231 for (i = 0; export_list[i].export != export_unknown; i++) { 238 for (i = 0; export_list[i].export != export_unknown; i++) {
@@ -315,7 +322,7 @@ void *grab_file(const char *filename, unsigned long *size)
315 * spaces in the beginning of the line is trimmed away. 322 * spaces in the beginning of the line is trimmed away.
316 * Return a pointer to a static buffer. 323 * Return a pointer to a static buffer.
317 **/ 324 **/
318char* get_next_line(unsigned long *pos, void *file, unsigned long size) 325char *get_next_line(unsigned long *pos, void *file, unsigned long size)
319{ 326{
320 static char line[4096]; 327 static char line[4096];
321 int skip = 1; 328 int skip = 1;
@@ -323,8 +330,7 @@ char* get_next_line(unsigned long *pos, void *file, unsigned long size)
323 signed char *p = (signed char *)file + *pos; 330 signed char *p = (signed char *)file + *pos;
324 char *s = line; 331 char *s = line;
325 332
326 for (; *pos < size ; (*pos)++) 333 for (; *pos < size ; (*pos)++) {
327 {
328 if (skip && isspace(*p)) { 334 if (skip && isspace(*p)) {
329 p++; 335 p++;
330 continue; 336 continue;
@@ -386,7 +392,9 @@ static int parse_elf(struct elf_info *info, const char *filename)
386 392
387 /* Check if file offset is correct */ 393 /* Check if file offset is correct */
388 if (hdr->e_shoff > info->size) { 394 if (hdr->e_shoff > info->size) {
389 fatal("section header offset=%u in file '%s' is bigger then filesize=%lu\n", hdr->e_shoff, filename, info->size); 395 fatal("section header offset=%lu in file '%s' is bigger than "
396 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
397 filename, info->size);
390 return 0; 398 return 0;
391 } 399 }
392 400
@@ -407,7 +415,10 @@ static int parse_elf(struct elf_info *info, const char *filename)
407 const char *secname; 415 const char *secname;
408 416
409 if (sechdrs[i].sh_offset > info->size) { 417 if (sechdrs[i].sh_offset > info->size) {
410 fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr)); 418 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
419 "sizeof(*hrd)=%zu\n", filename,
420 (unsigned long)sechdrs[i].sh_offset,
421 sizeof(*hdr));
411 return 0; 422 return 0;
412 } 423 }
413 secname = secstrings + sechdrs[i].sh_name; 424 secname = secstrings + sechdrs[i].sh_name;
@@ -434,9 +445,9 @@ static int parse_elf(struct elf_info *info, const char *filename)
434 info->strtab = (void *)hdr + 445 info->strtab = (void *)hdr +
435 sechdrs[sechdrs[i].sh_link].sh_offset; 446 sechdrs[sechdrs[i].sh_link].sh_offset;
436 } 447 }
437 if (!info->symtab_start) { 448 if (!info->symtab_start)
438 fatal("%s has no symtab?\n", filename); 449 fatal("%s has no symtab?\n", filename);
439 } 450
440 /* Fix endianness in symbols */ 451 /* Fix endianness in symbols */
441 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { 452 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
442 sym->st_shndx = TO_NATIVE(sym->st_shndx); 453 sym->st_shndx = TO_NATIVE(sym->st_shndx);
@@ -505,11 +516,13 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
505#endif 516#endif
506 517
507 if (memcmp(symname, MODULE_SYMBOL_PREFIX, 518 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
508 strlen(MODULE_SYMBOL_PREFIX)) == 0) 519 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
509 mod->unres = alloc_symbol(symname + 520 mod->unres =
510 strlen(MODULE_SYMBOL_PREFIX), 521 alloc_symbol(symname +
511 ELF_ST_BIND(sym->st_info) == STB_WEAK, 522 strlen(MODULE_SYMBOL_PREFIX),
512 mod->unres); 523 ELF_ST_BIND(sym->st_info) == STB_WEAK,
524 mod->unres);
525 }
513 break; 526 break;
514 default: 527 default:
515 /* All exported symbols */ 528 /* All exported symbols */
@@ -578,69 +591,303 @@ static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
578 **/ 591 **/
579static int strrcmp(const char *s, const char *sub) 592static int strrcmp(const char *s, const char *sub)
580{ 593{
581 int slen, sublen; 594 int slen, sublen;
582 595
583 if (!s || !sub) 596 if (!s || !sub)
584 return 1; 597 return 1;
585 598
586 slen = strlen(s); 599 slen = strlen(s);
587 sublen = strlen(sub); 600 sublen = strlen(sub);
588 601
589 if ((slen == 0) || (sublen == 0)) 602 if ((slen == 0) || (sublen == 0))
590 return 1; 603 return 1;
591 604
592 if (sublen > slen) 605 if (sublen > slen)
593 return 1; 606 return 1;
594 607
595 return memcmp(s + slen - sublen, sub, sublen); 608 return memcmp(s + slen - sublen, sub, sublen);
596} 609}
597 610
598/* 611static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
599 * Functions used only during module init is marked __init and is stored in 612{
600 * a .init.text section. Likewise data is marked __initdata and stored in 613 if (sym)
601 * a .init.data section. 614 return elf->strtab + sym->st_name;
602 * If this section is one of these sections return 1 615 else
603 * See include/linux/init.h for the details 616 return "";
617}
618
619static const char *sec_name(struct elf_info *elf, int shndx)
620{
621 Elf_Shdr *sechdrs = elf->sechdrs;
622 return (void *)elf->hdr +
623 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
624 sechdrs[shndx].sh_name;
625}
626
627static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
628{
629 return (void *)elf->hdr +
630 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
631 sechdr->sh_name;
632}
633
634/* if sym is empty or point to a string
635 * like ".[0-9]+" then return 1.
636 * This is the optional prefix added by ld to some sections
604 */ 637 */
605static int init_section(const char *name) 638static int number_prefix(const char *sym)
606{ 639{
607 if (strcmp(name, ".init") == 0) 640 if (*sym++ == '\0')
608 return 1;
609 if (strncmp(name, ".init.", strlen(".init.")) == 0)
610 return 1; 641 return 1;
642 if (*sym != '.')
643 return 0;
644 do {
645 char c = *sym++;
646 if (c < '0' || c > '9')
647 return 0;
648 } while (*sym);
649 return 1;
650}
651
652/* The pattern is an array of simple patterns.
653 * "foo" will match an exact string equal to "foo"
654 * "*foo" will match a string that ends with "foo"
655 * "foo*" will match a string that begins with "foo"
656 * "foo$" will match a string equal to "foo" or "foo.1"
657 * where the '1' can be any number including several digits.
658 * The $ syntax is for sections where ld append a dot number
659 * to make section name unique.
660 */
661int match(const char *sym, const char * const pat[])
662{
663 const char *p;
664 while (*pat) {
665 p = *pat++;
666 const char *endp = p + strlen(p) - 1;
667
668 /* "*foo" */
669 if (*p == '*') {
670 if (strrcmp(sym, p + 1) == 0)
671 return 1;
672 }
673 /* "foo*" */
674 else if (*endp == '*') {
675 if (strncmp(sym, p, strlen(p) - 1) == 0)
676 return 1;
677 }
678 /* "foo$" */
679 else if (*endp == '$') {
680 if (strncmp(sym, p, strlen(p) - 1) == 0) {
681 if (number_prefix(sym + strlen(p) - 1))
682 return 1;
683 }
684 }
685 /* no wildcards */
686 else {
687 if (strcmp(p, sym) == 0)
688 return 1;
689 }
690 }
691 /* no match */
611 return 0; 692 return 0;
612} 693}
613 694
695/* sections that we do not want to do full section mismatch check on */
696static const char *section_white_list[] =
697 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
698
614/* 699/*
615 * Functions used only during module exit is marked __exit and is stored in 700 * Is this section one we do not want to check?
616 * a .exit.text section. Likewise data is marked __exitdata and stored in 701 * This is often debug sections.
617 * a .exit.data section. 702 * If we are going to check this section then
618 * If this section is one of these sections return 1 703 * test if section name ends with a dot and a number.
619 * See include/linux/init.h for the details 704 * This is used to find sections where the linker have
620 **/ 705 * appended a dot-number to make the name unique.
621static int exit_section(const char *name) 706 * The cause of this is often a section specified in assembler
707 * without "ax" / "aw" and the same section used in .c
708 * code where gcc add these.
709 */
710static int check_section(const char *modname, const char *sec)
622{ 711{
623 if (strcmp(name, ".exit.text") == 0) 712 const char *e = sec + strlen(sec) - 1;
624 return 1; 713 if (match(sec, section_white_list))
625 if (strcmp(name, ".exit.data") == 0)
626 return 1; 714 return 1;
627 return 0;
628 715
716 if (*e && isdigit(*e)) {
717 /* consume all digits */
718 while (*e && e != sec && isdigit(*e))
719 e--;
720 if (*e == '.') {
721 warn("%s (%s): unexpected section name.\n"
722 "The (.[number]+) following section name are "
723 "ld generated and not expected.\n"
724 "Did you forget to use \"ax\"/\"aw\" "
725 "in a .S file?\n"
726 "Note that for example <linux/init.h> contains\n"
727 "section definitions for use in .S files.\n\n",
728 modname, sec);
729 }
730 }
731 return 0;
629} 732}
630 733
631/* 734
632 * Data sections are named like this: 735
633 * .data | .data.rel | .data.rel.* 736#define ALL_INIT_DATA_SECTIONS \
634 * Return 1 if the specified section is a data section 737 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
738#define ALL_EXIT_DATA_SECTIONS \
739 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
740
741#define ALL_INIT_TEXT_SECTIONS \
742 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
743#define ALL_EXIT_TEXT_SECTIONS \
744 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
745
746#define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
747#define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
748
749#define DATA_SECTIONS ".data$", ".data.rel$"
750#define TEXT_SECTIONS ".text$"
751
752#define INIT_SECTIONS ".init.data$", ".init.text$"
753#define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
754#define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
755#define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
756
757#define EXIT_SECTIONS ".exit.data$", ".exit.text$"
758#define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
759#define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
760#define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
761
762/* init data sections */
763static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
764
765/* all init sections */
766static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
767
768/* All init and exit sections (code + data) */
769static const char *init_exit_sections[] =
770 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
771
772/* data section */
773static const char *data_sections[] = { DATA_SECTIONS, NULL };
774
775/* sections that may refer to an init/exit section with no warning */
776static const char *initref_sections[] =
777{
778 ".text.init.refok*",
779 ".exit.text.refok*",
780 ".data.init.refok*",
781 NULL
782};
783
784
785/* symbols in .data that may refer to init/exit sections */
786static const char *symbol_white_list[] =
787{
788 "*driver",
789 "*_template", /* scsi uses *_template a lot */
790 "*_timer", /* arm uses ops structures named _timer a lot */
791 "*_sht", /* scsi also used *_sht to some extent */
792 "*_ops",
793 "*_probe",
794 "*_probe_one",
795 "*_console",
796 NULL
797};
798
799static const char *head_sections[] = { ".head.text*", NULL };
800static const char *linker_symbols[] =
801 { "__init_begin", "_sinittext", "_einittext", NULL };
802
803enum mismatch {
804 NO_MISMATCH,
805 TEXT_TO_INIT,
806 DATA_TO_INIT,
807 TEXT_TO_EXIT,
808 DATA_TO_EXIT,
809 XXXINIT_TO_INIT,
810 XXXEXIT_TO_EXIT,
811 INIT_TO_EXIT,
812 EXIT_TO_INIT,
813 EXPORT_TO_INIT_EXIT,
814};
815
816struct sectioncheck {
817 const char *fromsec[20];
818 const char *tosec[20];
819 enum mismatch mismatch;
820};
821
822const struct sectioncheck sectioncheck[] = {
823/* Do not reference init/exit code/data from
824 * normal code and data
635 */ 825 */
636static int data_section(const char *name)
637{ 826{
638 if ((strcmp(name, ".data") == 0) || 827 .fromsec = { TEXT_SECTIONS, NULL },
639 (strcmp(name, ".data.rel") == 0) || 828 .tosec = { ALL_INIT_SECTIONS, NULL },
640 (strncmp(name, ".data.rel.", strlen(".data.rel.")) == 0)) 829 .mismatch = TEXT_TO_INIT,
641 return 1; 830},
642 else 831{
643 return 0; 832 .fromsec = { DATA_SECTIONS, NULL },
833 .tosec = { ALL_INIT_SECTIONS, NULL },
834 .mismatch = DATA_TO_INIT,
835},
836{
837 .fromsec = { TEXT_SECTIONS, NULL },
838 .tosec = { ALL_EXIT_SECTIONS, NULL },
839 .mismatch = TEXT_TO_EXIT,
840},
841{
842 .fromsec = { DATA_SECTIONS, NULL },
843 .tosec = { ALL_EXIT_SECTIONS, NULL },
844 .mismatch = DATA_TO_EXIT,
845},
846/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
847{
848 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
849 .tosec = { INIT_SECTIONS, NULL },
850 .mismatch = XXXINIT_TO_INIT,
851},
852/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
853{
854 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
855 .tosec = { EXIT_SECTIONS, NULL },
856 .mismatch = XXXEXIT_TO_EXIT,
857},
858/* Do not use exit code/data from init code */
859{
860 .fromsec = { ALL_INIT_SECTIONS, NULL },
861 .tosec = { ALL_EXIT_SECTIONS, NULL },
862 .mismatch = INIT_TO_EXIT,
863},
864/* Do not use init code/data from exit code */
865{
866 .fromsec = { ALL_EXIT_SECTIONS, NULL },
867 .tosec = { ALL_INIT_SECTIONS, NULL },
868 .mismatch = EXIT_TO_INIT,
869},
870/* Do not export init/exit functions or data */
871{
872 .fromsec = { "__ksymtab*", NULL },
873 .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL },
874 .mismatch = EXPORT_TO_INIT_EXIT
875}
876};
877
878static int section_mismatch(const char *fromsec, const char *tosec)
879{
880 int i;
881 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
882 const struct sectioncheck *check = &sectioncheck[0];
883
884 for (i = 0; i < elems; i++) {
885 if (match(fromsec, check->fromsec) &&
886 match(tosec, check->tosec))
887 return check->mismatch;
888 check++;
889 }
890 return NO_MISMATCH;
644} 891}
645 892
646/** 893/**
@@ -669,7 +916,8 @@ static int data_section(const char *name)
669 * the pattern is identified by: 916 * the pattern is identified by:
670 * tosec = init or exit section 917 * tosec = init or exit section
671 * fromsec = data section 918 * fromsec = data section
672 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer 919 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
920 * *probe_one, *_console, *_timer
673 * 921 *
674 * Pattern 3: 922 * Pattern 3:
675 * Whitelist all refereces from .text.head to .init.data 923 * Whitelist all refereces from .text.head to .init.data
@@ -684,77 +932,36 @@ static int data_section(const char *name)
684 * This pattern is identified by 932 * This pattern is identified by
685 * refsymname = __init_begin, _sinittext, _einittext 933 * refsymname = __init_begin, _sinittext, _einittext
686 * 934 *
687 * Pattern 5:
688 * Xtensa uses literal sections for constants that are accessed PC-relative.
689 * Literal sections may safely reference their text sections.
690 * (Note that the name for the literal section omits any trailing '.text')
691 * tosec = <section>[.text]
692 * fromsec = <section>.literal
693 **/ 935 **/
694static int secref_whitelist(const char *modname, const char *tosec, 936static int secref_whitelist(const char *fromsec, const char *fromsym,
695 const char *fromsec, const char *atsym, 937 const char *tosec, const char *tosym)
696 const char *refsymname)
697{ 938{
698 int len;
699 const char **s;
700 const char *pat2sym[] = {
701 "driver",
702 "_template", /* scsi uses *_template a lot */
703 "_timer", /* arm uses ops structures named _timer a lot */
704 "_sht", /* scsi also used *_sht to some extent */
705 "_ops",
706 "_probe",
707 "_probe_one",
708 "_console",
709 NULL
710 };
711
712 const char *pat3refsym[] = {
713 "__init_begin",
714 "_sinittext",
715 "_einittext",
716 NULL
717 };
718
719 /* Check for pattern 0 */ 939 /* Check for pattern 0 */
720 if ((strncmp(fromsec, ".text.init.refok", strlen(".text.init.refok")) == 0) || 940 if (match(fromsec, initref_sections))
721 (strncmp(fromsec, ".exit.text.refok", strlen(".exit.text.refok")) == 0) || 941 return 0;
722 (strncmp(fromsec, ".data.init.refok", strlen(".data.init.refok")) == 0))
723 return 1;
724 942
725 /* Check for pattern 1 */ 943 /* Check for pattern 1 */
726 if ((strcmp(tosec, ".init.data") == 0) && 944 if (match(tosec, init_data_sections) &&
727 (strncmp(fromsec, ".data", strlen(".data")) == 0) && 945 match(fromsec, data_sections) &&
728 (strncmp(atsym, "__param", strlen("__param")) == 0)) 946 (strncmp(fromsym, "__param", strlen("__param")) == 0))
729 return 1; 947 return 0;
730 948
731 /* Check for pattern 2 */ 949 /* Check for pattern 2 */
732 if ((init_section(tosec) || exit_section(tosec)) && data_section(fromsec)) 950 if (match(tosec, init_exit_sections) &&
733 for (s = pat2sym; *s; s++) 951 match(fromsec, data_sections) &&
734 if (strrcmp(atsym, *s) == 0) 952 match(fromsym, symbol_white_list))
735 return 1; 953 return 0;
736 954
737 /* Check for pattern 3 */ 955 /* Check for pattern 3 */
738 if ((strcmp(fromsec, ".text.head") == 0) && 956 if (match(fromsec, head_sections) &&
739 ((strcmp(tosec, ".init.data") == 0) || 957 match(tosec, init_sections))
740 (strcmp(tosec, ".init.text") == 0))) 958 return 0;
741 return 1;
742 959
743 /* Check for pattern 4 */ 960 /* Check for pattern 4 */
744 for (s = pat3refsym; *s; s++) 961 if (match(tosym, linker_symbols))
745 if (strcmp(refsymname, *s) == 0) 962 return 0;
746 return 1;
747
748 /* Check for pattern 5 */
749 if (strrcmp(tosec, ".text") == 0)
750 len = strlen(tosec) - strlen(".text");
751 else
752 len = strlen(tosec);
753 if ((strncmp(tosec, fromsec, len) == 0) && (strlen(fromsec) > len) &&
754 (strcmp(fromsec + len, ".literal") == 0))
755 return 1;
756 963
757 return 0; 964 return 1;
758} 965}
759 966
760/** 967/**
@@ -764,10 +971,13 @@ static int secref_whitelist(const char *modname, const char *tosec,
764 * In other cases the symbol needs to be looked up in the symbol table 971 * In other cases the symbol needs to be looked up in the symbol table
765 * based on section and address. 972 * based on section and address.
766 * **/ 973 * **/
767static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, 974static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
768 Elf_Sym *relsym) 975 Elf_Sym *relsym)
769{ 976{
770 Elf_Sym *sym; 977 Elf_Sym *sym;
978 Elf_Sym *near = NULL;
979 Elf64_Sword distance = 20;
980 Elf64_Sword d;
771 981
772 if (relsym->st_name != 0) 982 if (relsym->st_name != 0)
773 return relsym; 983 return relsym;
@@ -778,8 +988,20 @@ static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
778 continue; 988 continue;
779 if (sym->st_value == addr) 989 if (sym->st_value == addr)
780 return sym; 990 return sym;
991 /* Find a symbol nearby - addr are maybe negative */
992 d = sym->st_value - addr;
993 if (d < 0)
994 d = addr - sym->st_value;
995 if (d < distance) {
996 distance = d;
997 near = sym;
998 }
781 } 999 }
782 return NULL; 1000 /* We need a close match */
1001 if (distance < 20)
1002 return near;
1003 else
1004 return NULL;
783} 1005}
784 1006
785static inline int is_arm_mapping_symbol(const char *str) 1007static inline int is_arm_mapping_symbol(const char *str)
@@ -812,121 +1034,245 @@ static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
812 * The ELF format may have a better way to detect what type of symbol 1034 * The ELF format may have a better way to detect what type of symbol
813 * it is, but this works for now. 1035 * it is, but this works for now.
814 **/ 1036 **/
815static void find_symbols_between(struct elf_info *elf, Elf_Addr addr, 1037static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
816 const char *sec, 1038 const char *sec)
817 Elf_Sym **before, Elf_Sym **after)
818{ 1039{
819 Elf_Sym *sym; 1040 Elf_Sym *sym;
820 Elf_Ehdr *hdr = elf->hdr; 1041 Elf_Sym *near = NULL;
821 Elf_Addr beforediff = ~0; 1042 Elf_Addr distance = ~0;
822 Elf_Addr afterdiff = ~0;
823 const char *secstrings = (void *)hdr +
824 elf->sechdrs[hdr->e_shstrndx].sh_offset;
825
826 *before = NULL;
827 *after = NULL;
828 1043
829 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { 1044 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
830 const char *symsec; 1045 const char *symsec;
831 1046
832 if (sym->st_shndx >= SHN_LORESERVE) 1047 if (sym->st_shndx >= SHN_LORESERVE)
833 continue; 1048 continue;
834 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; 1049 symsec = sec_name(elf, sym->st_shndx);
835 if (strcmp(symsec, sec) != 0) 1050 if (strcmp(symsec, sec) != 0)
836 continue; 1051 continue;
837 if (!is_valid_name(elf, sym)) 1052 if (!is_valid_name(elf, sym))
838 continue; 1053 continue;
839 if (sym->st_value <= addr) { 1054 if (sym->st_value <= addr) {
840 if ((addr - sym->st_value) < beforediff) { 1055 if ((addr - sym->st_value) < distance) {
841 beforediff = addr - sym->st_value; 1056 distance = addr - sym->st_value;
842 *before = sym; 1057 near = sym;
843 } 1058 } else if ((addr - sym->st_value) == distance) {
844 else if ((addr - sym->st_value) == beforediff) { 1059 near = sym;
845 *before = sym;
846 } 1060 }
847 } 1061 }
1062 }
1063 return near;
1064}
1065
1066/*
1067 * Convert a section name to the function/data attribute
1068 * .init.text => __init
1069 * .cpuinit.data => __cpudata
1070 * .memexitconst => __memconst
1071 * etc.
1072*/
1073static char *sec2annotation(const char *s)
1074{
1075 if (match(s, init_exit_sections)) {
1076 char *p = malloc(20);
1077 char *r = p;
1078
1079 *p++ = '_';
1080 *p++ = '_';
1081 if (*s == '.')
1082 s++;
1083 while (*s && *s != '.')
1084 *p++ = *s++;
1085 *p = '\0';
1086 if (*s == '.')
1087 s++;
1088 if (strstr(s, "rodata") != NULL)
1089 strcat(p, "const ");
1090 else if (strstr(s, "data") != NULL)
1091 strcat(p, "data ");
848 else 1092 else
849 { 1093 strcat(p, " ");
850 if ((sym->st_value - addr) < afterdiff) { 1094 return r; /* we leak her but we do not care */
851 afterdiff = sym->st_value - addr; 1095 } else {
852 *after = sym; 1096 return "";
853 }
854 else if ((sym->st_value - addr) == afterdiff) {
855 *after = sym;
856 }
857 }
858 } 1097 }
859} 1098}
860 1099
861/** 1100static int is_function(Elf_Sym *sym)
1101{
1102 if (sym)
1103 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1104 else
1105 return 0;
1106}
1107
1108/*
862 * Print a warning about a section mismatch. 1109 * Print a warning about a section mismatch.
863 * Try to find symbols near it so user can find it. 1110 * Try to find symbols near it so user can find it.
864 * Check whitelist before warning - it may be a false positive. 1111 * Check whitelist before warning - it may be a false positive.
865 **/ 1112 */
866static void warn_sec_mismatch(const char *modname, const char *fromsec, 1113static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
867 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r) 1114 const char *fromsec,
1115 unsigned long long fromaddr,
1116 const char *fromsym,
1117 int from_is_func,
1118 const char *tosec, const char *tosym,
1119 int to_is_func)
868{ 1120{
869 const char *refsymname = ""; 1121 const char *from, *from_p;
870 Elf_Sym *before, *after; 1122 const char *to, *to_p;
871 Elf_Sym *refsym; 1123 from = from_is_func ? "function" : "variable";
872 Elf_Ehdr *hdr = elf->hdr; 1124 from_p = from_is_func ? "()" : "";
873 Elf_Shdr *sechdrs = elf->sechdrs; 1125 to = to_is_func ? "function" : "variable";
874 const char *secstrings = (void *)hdr + 1126 to_p = to_is_func ? "()" : "";
875 sechdrs[hdr->e_shstrndx].sh_offset; 1127
876 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name; 1128 fprintf(stderr, "WARNING: %s(%s+0x%llx): Section mismatch in"
877 1129 " reference from the %s %s%s to the %s %s:%s%s\n",
878 find_symbols_between(elf, r.r_offset, fromsec, &before, &after); 1130 modname, fromsec, fromaddr, from, fromsym, from_p,
879 1131 to, tosec, tosym, to_p);
880 refsym = find_elf_symbol(elf, r.r_addend, sym); 1132
881 if (refsym && strlen(elf->strtab + refsym->st_name)) 1133 sec_mismatch_count++;
882 refsymname = elf->strtab + refsym->st_name; 1134 if (!sec_mismatch_verbose)
883
884 /* check whitelist - we may ignore it */
885 if (secref_whitelist(modname, secname, fromsec,
886 before ? elf->strtab + before->st_name : "",
887 refsymname))
888 return; 1135 return;
889 1136
890 if (before && after) { 1137 switch (mismatch) {
891 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " 1138 case TEXT_TO_INIT:
892 "(between '%s' and '%s')\n", 1139 fprintf(stderr,
893 modname, fromsec, (unsigned long long)r.r_offset, 1140 "The function %s %s() references\n"
894 secname, refsymname, 1141 "the %s %s%s%s.\n"
895 elf->strtab + before->st_name, 1142 "This is often because %s lacks a %s\n"
896 elf->strtab + after->st_name); 1143 "annotation or the annotation of %s is wrong.\n",
897 } else if (before) { 1144 sec2annotation(fromsec), fromsym,
898 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " 1145 to, sec2annotation(tosec), tosym, to_p,
899 "(after '%s')\n", 1146 fromsym, sec2annotation(tosec), tosym);
900 modname, fromsec, (unsigned long long)r.r_offset, 1147 break;
901 secname, refsymname, 1148 case DATA_TO_INIT: {
902 elf->strtab + before->st_name); 1149 const char **s = symbol_white_list;
903 } else if (after) { 1150 fprintf(stderr,
904 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " 1151 "The variable %s references\n"
905 "before '%s' (at offset -0x%llx)\n", 1152 "the %s %s%s%s\n"
906 modname, fromsec, (unsigned long long)r.r_offset, 1153 "If the reference is valid then annotate the\n"
907 secname, refsymname, 1154 "variable with __init* (see linux/init.h) "
908 elf->strtab + after->st_name); 1155 "or name the variable:\n",
909 } else { 1156 fromsym, to, sec2annotation(tosec), tosym, to_p);
910 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n", 1157 while (*s)
911 modname, fromsec, (unsigned long long)r.r_offset, 1158 fprintf(stderr, "%s, ", *s++);
912 secname, refsymname); 1159 fprintf(stderr, "\n");
1160 break;
1161 }
1162 case TEXT_TO_EXIT:
1163 fprintf(stderr,
1164 "The function %s() references a %s in an exit section.\n"
1165 "Often the %s %s%s has valid usage outside the exit section\n"
1166 "and the fix is to remove the %sannotation of %s.\n",
1167 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1168 break;
1169 case DATA_TO_EXIT: {
1170 const char **s = symbol_white_list;
1171 fprintf(stderr,
1172 "The variable %s references\n"
1173 "the %s %s%s%s\n"
1174 "If the reference is valid then annotate the\n"
1175 "variable with __exit* (see linux/init.h) or "
1176 "name the variable:\n",
1177 fromsym, to, sec2annotation(tosec), tosym, to_p);
1178 while (*s)
1179 fprintf(stderr, "%s, ", *s++);
1180 fprintf(stderr, "\n");
1181 break;
1182 }
1183 case XXXINIT_TO_INIT:
1184 case XXXEXIT_TO_EXIT:
1185 fprintf(stderr,
1186 "The %s %s%s%s references\n"
1187 "a %s %s%s%s.\n"
1188 "If %s is only used by %s then\n"
1189 "annotate %s with a matching annotation.\n",
1190 from, sec2annotation(fromsec), fromsym, from_p,
1191 to, sec2annotation(tosec), tosym, to_p,
1192 fromsym, tosym, fromsym);
1193 break;
1194 case INIT_TO_EXIT:
1195 fprintf(stderr,
1196 "The %s %s%s%s references\n"
1197 "a %s %s%s%s.\n"
1198 "This is often seen when error handling "
1199 "in the init function\n"
1200 "uses functionality in the exit path.\n"
1201 "The fix is often to remove the %sannotation of\n"
1202 "%s%s so it may be used outside an exit section.\n",
1203 from, sec2annotation(fromsec), fromsym, from_p,
1204 to, sec2annotation(tosec), tosym, to_p,
1205 sec2annotation(tosec), tosym, to_p);
1206 break;
1207 case EXIT_TO_INIT:
1208 fprintf(stderr,
1209 "The %s %s%s%s references\n"
1210 "a %s %s%s%s.\n"
1211 "This is often seen when error handling "
1212 "in the exit function\n"
1213 "uses functionality in the init path.\n"
1214 "The fix is often to remove the %sannotation of\n"
1215 "%s%s so it may be used outside an init section.\n",
1216 from, sec2annotation(fromsec), fromsym, from_p,
1217 to, sec2annotation(tosec), tosym, to_p,
1218 sec2annotation(tosec), tosym, to_p);
1219 break;
1220 case EXPORT_TO_INIT_EXIT:
1221 fprintf(stderr,
1222 "The symbol %s is exported and annotated %s\n"
1223 "Fix this by removing the %sannotation of %s "
1224 "or drop the export.\n",
1225 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1226 case NO_MISMATCH:
1227 /* To get warnings on missing members */
1228 break;
1229 }
1230 fprintf(stderr, "\n");
1231}
1232
1233static void check_section_mismatch(const char *modname, struct elf_info *elf,
1234 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1235{
1236 const char *tosec;
1237 enum mismatch mismatch;
1238
1239 tosec = sec_name(elf, sym->st_shndx);
1240 mismatch = section_mismatch(fromsec, tosec);
1241 if (mismatch != NO_MISMATCH) {
1242 Elf_Sym *to;
1243 Elf_Sym *from;
1244 const char *tosym;
1245 const char *fromsym;
1246
1247 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1248 fromsym = sym_name(elf, from);
1249 to = find_elf_symbol(elf, r->r_addend, sym);
1250 tosym = sym_name(elf, to);
1251
1252 /* check whitelist - we may ignore it */
1253 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
1254 report_sec_mismatch(modname, mismatch,
1255 fromsec, r->r_offset, fromsym,
1256 is_function(from), tosec, tosym,
1257 is_function(to));
1258 }
913 } 1259 }
914} 1260}
915 1261
916static unsigned int *reloc_location(struct elf_info *elf, 1262static unsigned int *reloc_location(struct elf_info *elf,
917 int rsection, Elf_Rela *r) 1263 Elf_Shdr *sechdr, Elf_Rela *r)
918{ 1264{
919 Elf_Shdr *sechdrs = elf->sechdrs; 1265 Elf_Shdr *sechdrs = elf->sechdrs;
920 int section = sechdrs[rsection].sh_info; 1266 int section = sechdr->sh_info;
921 1267
922 return (void *)elf->hdr + sechdrs[section].sh_offset + 1268 return (void *)elf->hdr + sechdrs[section].sh_offset +
923 (r->r_offset - sechdrs[section].sh_addr); 1269 (r->r_offset - sechdrs[section].sh_addr);
924} 1270}
925 1271
926static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r) 1272static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
927{ 1273{
928 unsigned int r_typ = ELF_R_TYPE(r->r_info); 1274 unsigned int r_typ = ELF_R_TYPE(r->r_info);
929 unsigned int *location = reloc_location(elf, rsection, r); 1275 unsigned int *location = reloc_location(elf, sechdr, r);
930 1276
931 switch (r_typ) { 1277 switch (r_typ) {
932 case R_386_32: 1278 case R_386_32:
@@ -942,19 +1288,21 @@ static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
942 return 0; 1288 return 0;
943} 1289}
944 1290
945static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r) 1291static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
946{ 1292{
947 unsigned int r_typ = ELF_R_TYPE(r->r_info); 1293 unsigned int r_typ = ELF_R_TYPE(r->r_info);
948 1294
949 switch (r_typ) { 1295 switch (r_typ) {
950 case R_ARM_ABS32: 1296 case R_ARM_ABS32:
951 /* From ARM ABI: (S + A) | T */ 1297 /* From ARM ABI: (S + A) | T */
952 r->r_addend = (int)(long)(elf->symtab_start + ELF_R_SYM(r->r_info)); 1298 r->r_addend = (int)(long)
1299 (elf->symtab_start + ELF_R_SYM(r->r_info));
953 break; 1300 break;
954 case R_ARM_PC24: 1301 case R_ARM_PC24:
955 /* From ARM ABI: ((S + A) | T) - P */ 1302 /* From ARM ABI: ((S + A) | T) - P */
956 r->r_addend = (int)(long)(elf->hdr + elf->sechdrs[rsection].sh_offset + 1303 r->r_addend = (int)(long)(elf->hdr +
957 (r->r_offset - elf->sechdrs[rsection].sh_addr)); 1304 sechdr->sh_offset +
1305 (r->r_offset - sechdr->sh_addr));
958 break; 1306 break;
959 default: 1307 default:
960 return 1; 1308 return 1;
@@ -962,10 +1310,10 @@ static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
962 return 0; 1310 return 0;
963} 1311}
964 1312
965static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r) 1313static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
966{ 1314{
967 unsigned int r_typ = ELF_R_TYPE(r->r_info); 1315 unsigned int r_typ = ELF_R_TYPE(r->r_info);
968 unsigned int *location = reloc_location(elf, rsection, r); 1316 unsigned int *location = reloc_location(elf, sechdr, r);
969 unsigned int inst; 1317 unsigned int inst;
970 1318
971 if (r_typ == R_MIPS_HI16) 1319 if (r_typ == R_MIPS_HI16)
@@ -985,6 +1333,108 @@ static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
985 return 0; 1333 return 0;
986} 1334}
987 1335
1336static void section_rela(const char *modname, struct elf_info *elf,
1337 Elf_Shdr *sechdr)
1338{
1339 Elf_Sym *sym;
1340 Elf_Rela *rela;
1341 Elf_Rela r;
1342 unsigned int r_sym;
1343 const char *fromsec;
1344
1345 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1346 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1347
1348 fromsec = sech_name(elf, sechdr);
1349 fromsec += strlen(".rela");
1350 /* if from section (name) is know good then skip it */
1351 if (check_section(modname, fromsec))
1352 return;
1353
1354 for (rela = start; rela < stop; rela++) {
1355 r.r_offset = TO_NATIVE(rela->r_offset);
1356#if KERNEL_ELFCLASS == ELFCLASS64
1357 if (elf->hdr->e_machine == EM_MIPS) {
1358 unsigned int r_typ;
1359 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1360 r_sym = TO_NATIVE(r_sym);
1361 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1362 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1363 } else {
1364 r.r_info = TO_NATIVE(rela->r_info);
1365 r_sym = ELF_R_SYM(r.r_info);
1366 }
1367#else
1368 r.r_info = TO_NATIVE(rela->r_info);
1369 r_sym = ELF_R_SYM(r.r_info);
1370#endif
1371 r.r_addend = TO_NATIVE(rela->r_addend);
1372 sym = elf->symtab_start + r_sym;
1373 /* Skip special sections */
1374 if (sym->st_shndx >= SHN_LORESERVE)
1375 continue;
1376 check_section_mismatch(modname, elf, &r, sym, fromsec);
1377 }
1378}
1379
1380static void section_rel(const char *modname, struct elf_info *elf,
1381 Elf_Shdr *sechdr)
1382{
1383 Elf_Sym *sym;
1384 Elf_Rel *rel;
1385 Elf_Rela r;
1386 unsigned int r_sym;
1387 const char *fromsec;
1388
1389 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1390 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1391
1392 fromsec = sech_name(elf, sechdr);
1393 fromsec += strlen(".rel");
1394 /* if from section (name) is know good then skip it */
1395 if (check_section(modname, fromsec))
1396 return;
1397
1398 for (rel = start; rel < stop; rel++) {
1399 r.r_offset = TO_NATIVE(rel->r_offset);
1400#if KERNEL_ELFCLASS == ELFCLASS64
1401 if (elf->hdr->e_machine == EM_MIPS) {
1402 unsigned int r_typ;
1403 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1404 r_sym = TO_NATIVE(r_sym);
1405 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1406 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1407 } else {
1408 r.r_info = TO_NATIVE(rel->r_info);
1409 r_sym = ELF_R_SYM(r.r_info);
1410 }
1411#else
1412 r.r_info = TO_NATIVE(rel->r_info);
1413 r_sym = ELF_R_SYM(r.r_info);
1414#endif
1415 r.r_addend = 0;
1416 switch (elf->hdr->e_machine) {
1417 case EM_386:
1418 if (addend_386_rel(elf, sechdr, &r))
1419 continue;
1420 break;
1421 case EM_ARM:
1422 if (addend_arm_rel(elf, sechdr, &r))
1423 continue;
1424 break;
1425 case EM_MIPS:
1426 if (addend_mips_rel(elf, sechdr, &r))
1427 continue;
1428 break;
1429 }
1430 sym = elf->symtab_start + r_sym;
1431 /* Skip special sections */
1432 if (sym->st_shndx >= SHN_LORESERVE)
1433 continue;
1434 check_section_mismatch(modname, elf, &r, sym, fromsec);
1435 }
1436}
1437
988/** 1438/**
989 * A module includes a number of sections that are discarded 1439 * A module includes a number of sections that are discarded
990 * either when loaded or when used as built-in. 1440 * either when loaded or when used as built-in.
@@ -998,257 +1448,21 @@ static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
998 * be discarded and warns about it. 1448 * be discarded and warns about it.
999 **/ 1449 **/
1000static void check_sec_ref(struct module *mod, const char *modname, 1450static void check_sec_ref(struct module *mod, const char *modname,
1001 struct elf_info *elf, 1451 struct elf_info *elf)
1002 int section(const char*),
1003 int section_ref_ok(const char *))
1004{ 1452{
1005 int i; 1453 int i;
1006 Elf_Sym *sym;
1007 Elf_Ehdr *hdr = elf->hdr;
1008 Elf_Shdr *sechdrs = elf->sechdrs; 1454 Elf_Shdr *sechdrs = elf->sechdrs;
1009 const char *secstrings = (void *)hdr +
1010 sechdrs[hdr->e_shstrndx].sh_offset;
1011 1455
1012 /* Walk through all sections */ 1456 /* Walk through all sections */
1013 for (i = 0; i < hdr->e_shnum; i++) { 1457 for (i = 0; i < elf->hdr->e_shnum; i++) {
1014 const char *name = secstrings + sechdrs[i].sh_name;
1015 const char *secname;
1016 Elf_Rela r;
1017 unsigned int r_sym;
1018 /* We want to process only relocation sections and not .init */ 1458 /* We want to process only relocation sections and not .init */
1019 if (sechdrs[i].sh_type == SHT_RELA) { 1459 if (sechdrs[i].sh_type == SHT_RELA)
1020 Elf_Rela *rela; 1460 section_rela(modname, elf, &elf->sechdrs[i]);
1021 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset; 1461 else if (sechdrs[i].sh_type == SHT_REL)
1022 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size; 1462 section_rel(modname, elf, &elf->sechdrs[i]);
1023 name += strlen(".rela");
1024 if (section_ref_ok(name))
1025 continue;
1026
1027 for (rela = start; rela < stop; rela++) {
1028 r.r_offset = TO_NATIVE(rela->r_offset);
1029#if KERNEL_ELFCLASS == ELFCLASS64
1030 if (hdr->e_machine == EM_MIPS) {
1031 unsigned int r_typ;
1032 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1033 r_sym = TO_NATIVE(r_sym);
1034 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1035 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1036 } else {
1037 r.r_info = TO_NATIVE(rela->r_info);
1038 r_sym = ELF_R_SYM(r.r_info);
1039 }
1040#else
1041 r.r_info = TO_NATIVE(rela->r_info);
1042 r_sym = ELF_R_SYM(r.r_info);
1043#endif
1044 r.r_addend = TO_NATIVE(rela->r_addend);
1045 sym = elf->symtab_start + r_sym;
1046 /* Skip special sections */
1047 if (sym->st_shndx >= SHN_LORESERVE)
1048 continue;
1049
1050 secname = secstrings +
1051 sechdrs[sym->st_shndx].sh_name;
1052 if (section(secname))
1053 warn_sec_mismatch(modname, name,
1054 elf, sym, r);
1055 }
1056 } else if (sechdrs[i].sh_type == SHT_REL) {
1057 Elf_Rel *rel;
1058 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
1059 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
1060 name += strlen(".rel");
1061 if (section_ref_ok(name))
1062 continue;
1063
1064 for (rel = start; rel < stop; rel++) {
1065 r.r_offset = TO_NATIVE(rel->r_offset);
1066#if KERNEL_ELFCLASS == ELFCLASS64
1067 if (hdr->e_machine == EM_MIPS) {
1068 unsigned int r_typ;
1069 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1070 r_sym = TO_NATIVE(r_sym);
1071 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1072 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1073 } else {
1074 r.r_info = TO_NATIVE(rel->r_info);
1075 r_sym = ELF_R_SYM(r.r_info);
1076 }
1077#else
1078 r.r_info = TO_NATIVE(rel->r_info);
1079 r_sym = ELF_R_SYM(r.r_info);
1080#endif
1081 r.r_addend = 0;
1082 switch (hdr->e_machine) {
1083 case EM_386:
1084 if (addend_386_rel(elf, i, &r))
1085 continue;
1086 break;
1087 case EM_ARM:
1088 if(addend_arm_rel(elf, i, &r))
1089 continue;
1090 break;
1091 case EM_MIPS:
1092 if (addend_mips_rel(elf, i, &r))
1093 continue;
1094 break;
1095 }
1096 sym = elf->symtab_start + r_sym;
1097 /* Skip special sections */
1098 if (sym->st_shndx >= SHN_LORESERVE)
1099 continue;
1100
1101 secname = secstrings +
1102 sechdrs[sym->st_shndx].sh_name;
1103 if (section(secname))
1104 warn_sec_mismatch(modname, name,
1105 elf, sym, r);
1106 }
1107 }
1108 } 1463 }
1109} 1464}
1110 1465
1111/*
1112 * Identify sections from which references to either a
1113 * .init or a .exit section is OK.
1114 *
1115 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1116 * For our future {in}sanity, add a comment that this is the ppc .opd
1117 * section, not the ia64 .opd section.
1118 * ia64 .opd should not point to discarded sections.
1119 * [.rodata] like for .init.text we ignore .rodata references -same reason
1120 */
1121static int initexit_section_ref_ok(const char *name)
1122{
1123 const char **s;
1124 /* Absolute section names */
1125 const char *namelist1[] = {
1126 "__bug_table", /* used by powerpc for BUG() */
1127 "__ex_table",
1128 ".altinstructions",
1129 ".cranges", /* used by sh64 */
1130 ".fixup",
1131 ".machvec", /* ia64 + powerpc uses these */
1132 ".machine.desc",
1133 ".opd", /* See comment [OPD] */
1134 "__dbe_table",
1135 ".parainstructions",
1136 ".pdr",
1137 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
1138 ".smp_locks",
1139 ".stab",
1140 ".m68k_fixup",
1141 ".xt.prop", /* xtensa informational section */
1142 ".xt.lit", /* xtensa informational section */
1143 NULL
1144 };
1145 /* Start of section names */
1146 const char *namelist2[] = {
1147 ".debug",
1148 ".eh_frame",
1149 ".note", /* ignore ELF notes - may contain anything */
1150 ".got", /* powerpc - global offset table */
1151 ".toc", /* powerpc - table of contents */
1152 NULL
1153 };
1154 /* part of section name */
1155 const char *namelist3 [] = {
1156 ".unwind", /* Sample: IA_64.unwind.exit.text */
1157 NULL
1158 };
1159
1160 for (s = namelist1; *s; s++)
1161 if (strcmp(*s, name) == 0)
1162 return 1;
1163 for (s = namelist2; *s; s++)
1164 if (strncmp(*s, name, strlen(*s)) == 0)
1165 return 1;
1166 for (s = namelist3; *s; s++)
1167 if (strstr(name, *s) != NULL)
1168 return 1;
1169 return 0;
1170}
1171
1172
1173/*
1174 * Identify sections from which references to a .init section is OK.
1175 *
1176 * Unfortunately references to read only data that referenced .init
1177 * sections had to be excluded. Almost all of these are false
1178 * positives, they are created by gcc. The downside of excluding rodata
1179 * is that there really are some user references from rodata to
1180 * init code, e.g. drivers/video/vgacon.c:
1181 *
1182 * const struct consw vga_con = {
1183 * con_startup: vgacon_startup,
1184 *
1185 * where vgacon_startup is __init. If you want to wade through the false
1186 * positives, take out the check for rodata.
1187 */
1188static int init_section_ref_ok(const char *name)
1189{
1190 const char **s;
1191 /* Absolute section names */
1192 const char *namelist1[] = {
1193 "__dbe_table", /* MIPS generate these */
1194 "__ftr_fixup", /* powerpc cpu feature fixup */
1195 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
1196 "__param",
1197 ".data.rel.ro", /* used by parisc64 */
1198 ".init",
1199 ".text.lock",
1200 NULL
1201 };
1202 /* Start of section names */
1203 const char *namelist2[] = {
1204 ".init.",
1205 ".pci_fixup",
1206 ".rodata",
1207 NULL
1208 };
1209
1210 if (initexit_section_ref_ok(name))
1211 return 1;
1212
1213 for (s = namelist1; *s; s++)
1214 if (strcmp(*s, name) == 0)
1215 return 1;
1216 for (s = namelist2; *s; s++)
1217 if (strncmp(*s, name, strlen(*s)) == 0)
1218 return 1;
1219
1220 /* If section name ends with ".init" we allow references
1221 * as is the case with .initcallN.init, .early_param.init, .taglist.init etc
1222 */
1223 if (strrcmp(name, ".init") == 0)
1224 return 1;
1225 return 0;
1226}
1227
1228/*
1229 * Identify sections from which references to a .exit section is OK.
1230 */
1231static int exit_section_ref_ok(const char *name)
1232{
1233 const char **s;
1234 /* Absolute section names */
1235 const char *namelist1[] = {
1236 ".exit.data",
1237 ".exit.text",
1238 ".exitcall.exit",
1239 ".rodata",
1240 NULL
1241 };
1242
1243 if (initexit_section_ref_ok(name))
1244 return 1;
1245
1246 for (s = namelist1; *s; s++)
1247 if (strcmp(*s, name) == 0)
1248 return 1;
1249 return 0;
1250}
1251
1252static void read_symbols(char *modname) 1466static void read_symbols(char *modname)
1253{ 1467{
1254 const char *symname; 1468 const char *symname;
@@ -1288,10 +1502,9 @@ static void read_symbols(char *modname)
1288 handle_modversions(mod, &info, sym, symname); 1502 handle_modversions(mod, &info, sym, symname);
1289 handle_moddevtable(mod, &info, sym, symname); 1503 handle_moddevtable(mod, &info, sym, symname);
1290 } 1504 }
1291 if (is_vmlinux(modname) && vmlinux_section_warnings) { 1505 if (!is_vmlinux(modname) ||
1292 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok); 1506 (is_vmlinux(modname) && vmlinux_section_warnings))
1293 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok); 1507 check_sec_ref(mod, modname, &info);
1294 }
1295 1508
1296 version = get_modinfo(info.modinfo, info.modinfo_len, "version"); 1509 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1297 if (version) 1510 if (version)
@@ -1365,7 +1578,7 @@ static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1365 } 1578 }
1366} 1579}
1367 1580
1368static void check_for_unused(enum export exp, const char* m, const char* s) 1581static void check_for_unused(enum export exp, const char *m, const char *s)
1369{ 1582{
1370 const char *e = is_vmlinux(m) ?"":".ko"; 1583 const char *e = is_vmlinux(m) ?"":".ko";
1371 1584
@@ -1398,7 +1611,7 @@ static void check_exports(struct module *mod)
1398 if (!mod->gpl_compatible) 1611 if (!mod->gpl_compatible)
1399 check_for_gpl_usage(exp->export, basename, exp->name); 1612 check_for_gpl_usage(exp->export, basename, exp->name);
1400 check_for_unused(exp->export, basename, exp->name); 1613 check_for_unused(exp->export, basename, exp->name);
1401 } 1614 }
1402} 1615}
1403 1616
1404/** 1617/**
@@ -1458,13 +1671,12 @@ static int add_versions(struct buffer *b, struct module *mod)
1458 1671
1459 buf_printf(b, "\n"); 1672 buf_printf(b, "\n");
1460 buf_printf(b, "static const struct modversion_info ____versions[]\n"); 1673 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1461 buf_printf(b, "__attribute_used__\n"); 1674 buf_printf(b, "__used\n");
1462 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); 1675 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1463 1676
1464 for (s = mod->unres; s; s = s->next) { 1677 for (s = mod->unres; s; s = s->next) {
1465 if (!s->module) { 1678 if (!s->module)
1466 continue; 1679 continue;
1467 }
1468 if (!s->crc_valid) { 1680 if (!s->crc_valid) {
1469 warn("\"%s\" [%s.ko] has no CRC!\n", 1681 warn("\"%s\" [%s.ko] has no CRC!\n",
1470 s->name, mod->name); 1682 s->name, mod->name);
@@ -1485,13 +1697,12 @@ static void add_depends(struct buffer *b, struct module *mod,
1485 struct module *m; 1697 struct module *m;
1486 int first = 1; 1698 int first = 1;
1487 1699
1488 for (m = modules; m; m = m->next) { 1700 for (m = modules; m; m = m->next)
1489 m->seen = is_vmlinux(m->name); 1701 m->seen = is_vmlinux(m->name);
1490 }
1491 1702
1492 buf_printf(b, "\n"); 1703 buf_printf(b, "\n");
1493 buf_printf(b, "static const char __module_depends[]\n"); 1704 buf_printf(b, "static const char __module_depends[]\n");
1494 buf_printf(b, "__attribute_used__\n"); 1705 buf_printf(b, "__used\n");
1495 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); 1706 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1496 buf_printf(b, "\"depends="); 1707 buf_printf(b, "\"depends=");
1497 for (s = mod->unres; s; s = s->next) { 1708 for (s = mod->unres; s; s = s->next) {
@@ -1503,7 +1714,8 @@ static void add_depends(struct buffer *b, struct module *mod,
1503 continue; 1714 continue;
1504 1715
1505 s->module->seen = 1; 1716 s->module->seen = 1;
1506 if ((p = strrchr(s->module->name, '/')) != NULL) 1717 p = strrchr(s->module->name, '/');
1718 if (p)
1507 p++; 1719 p++;
1508 else 1720 else
1509 p = s->module->name; 1721 p = s->module->name;
@@ -1575,7 +1787,7 @@ static void read_dump(const char *fname, unsigned int kernel)
1575 void *file = grab_file(fname, &size); 1787 void *file = grab_file(fname, &size);
1576 char *line; 1788 char *line;
1577 1789
1578 if (!file) 1790 if (!file)
1579 /* No symbol versions, silently ignore */ 1791 /* No symbol versions, silently ignore */
1580 return; 1792 return;
1581 1793
@@ -1598,11 +1810,10 @@ static void read_dump(const char *fname, unsigned int kernel)
1598 crc = strtoul(line, &d, 16); 1810 crc = strtoul(line, &d, 16);
1599 if (*symname == '\0' || *modname == '\0' || *d != '\0') 1811 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1600 goto fail; 1812 goto fail;
1601 1813 mod = find_module(modname);
1602 if (!(mod = find_module(modname))) { 1814 if (!mod) {
1603 if (is_vmlinux(modname)) { 1815 if (is_vmlinux(modname))
1604 have_vmlinux = 1; 1816 have_vmlinux = 1;
1605 }
1606 mod = new_module(NOFAIL(strdup(modname))); 1817 mod = new_module(NOFAIL(strdup(modname)));
1607 mod->skip = 1; 1818 mod->skip = 1;
1608 } 1819 }
@@ -1653,38 +1864,40 @@ int main(int argc, char **argv)
1653{ 1864{
1654 struct module *mod; 1865 struct module *mod;
1655 struct buffer buf = { }; 1866 struct buffer buf = { };
1656 char fname[SZ];
1657 char *kernel_read = NULL, *module_read = NULL; 1867 char *kernel_read = NULL, *module_read = NULL;
1658 char *dump_write = NULL; 1868 char *dump_write = NULL;
1659 int opt; 1869 int opt;
1660 int err; 1870 int err;
1661 1871
1662 while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) { 1872 while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) {
1663 switch(opt) { 1873 switch (opt) {
1664 case 'i': 1874 case 'i':
1665 kernel_read = optarg; 1875 kernel_read = optarg;
1666 break; 1876 break;
1667 case 'I': 1877 case 'I':
1668 module_read = optarg; 1878 module_read = optarg;
1669 external_module = 1; 1879 external_module = 1;
1670 break; 1880 break;
1671 case 'm': 1881 case 'm':
1672 modversions = 1; 1882 modversions = 1;
1673 break; 1883 break;
1674 case 'o': 1884 case 'o':
1675 dump_write = optarg; 1885 dump_write = optarg;
1676 break; 1886 break;
1677 case 'a': 1887 case 'a':
1678 all_versions = 1; 1888 all_versions = 1;
1679 break; 1889 break;
1680 case 's': 1890 case 's':
1681 vmlinux_section_warnings = 0; 1891 vmlinux_section_warnings = 0;
1682 break; 1892 break;
1683 case 'w': 1893 case 'S':
1684 warn_unresolved = 1; 1894 sec_mismatch_verbose = 0;
1685 break; 1895 break;
1686 default: 1896 case 'w':
1687 exit(1); 1897 warn_unresolved = 1;
1898 break;
1899 default:
1900 exit(1);
1688 } 1901 }
1689 } 1902 }
1690 1903
@@ -1693,9 +1906,8 @@ int main(int argc, char **argv)
1693 if (module_read) 1906 if (module_read)
1694 read_dump(module_read, 0); 1907 read_dump(module_read, 0);
1695 1908
1696 while (optind < argc) { 1909 while (optind < argc)
1697 read_symbols(argv[optind++]); 1910 read_symbols(argv[optind++]);
1698 }
1699 1911
1700 for (mod = modules; mod; mod = mod->next) { 1912 for (mod = modules; mod; mod = mod->next) {
1701 if (mod->skip) 1913 if (mod->skip)
@@ -1706,6 +1918,8 @@ int main(int argc, char **argv)
1706 err = 0; 1918 err = 0;
1707 1919
1708 for (mod = modules; mod; mod = mod->next) { 1920 for (mod = modules; mod; mod = mod->next) {
1921 char fname[strlen(mod->name) + 10];
1922
1709 if (mod->skip) 1923 if (mod->skip)
1710 continue; 1924 continue;
1711 1925
@@ -1723,6 +1937,12 @@ int main(int argc, char **argv)
1723 1937
1724 if (dump_write) 1938 if (dump_write)
1725 write_dump(dump_write); 1939 write_dump(dump_write);
1940 if (sec_mismatch_count && !sec_mismatch_verbose)
1941 fprintf(stderr, "modpost: Found %d section mismatch(es).\n"
1942 "To see additional details select \"Enable full "
1943 "Section mismatch analysis\"\n"
1944 "in the Kernel Hacking menu "
1945 "(CONFIG_SECTION_MISMATCH).\n", sec_mismatch_count);
1726 1946
1727 return err; 1947 return err;
1728} 1948}