Commit b614a697 authored by Anders Kaseorg's avatar Anders Kaseorg Committed by Sam Ravnborg

kbuild, modpost: Check the section flags, to catch missing "ax"/"aw"

When you put
  .section ".foo"
in an assembly file instead of
  .section "foo", "ax"
, one of the possible symptoms is that modpost will see an
ld-generated section name ".foo.1" in section_rel() or section_rela().
But this heuristic has two problems: it will miss a bad section that
has no relocations, and it will incorrectly flag many gcc-generated
sections as bad when compiling with -ffunction-sections
-fdata-sections.

On mips it fixes a lot of bogus warnings with gcc 4.4.0 lije this one:
WARNING: crypto/cryptd.o (.text.T.349): unexpected section name.

So instead of checking whether the section name matches a particular
pattern, we directly check for a missing SHF_ALLOC in the section
flags.
Signed-off-by: default avatarAnders Kaseorg <andersk@mit.edu>
Tested-by: default avatarRalf Baechle <ralf@linux-mips.org>
Signed-off-by: default avatarSam Ravnborg <sam@ravnborg.org>
parent c993971f
...@@ -716,41 +716,27 @@ int match(const char *sym, const char * const pat[]) ...@@ -716,41 +716,27 @@ int match(const char *sym, const char * const pat[])
/* sections that we do not want to do full section mismatch check on */ /* sections that we do not want to do full section mismatch check on */
static const char *section_white_list[] = static const char *section_white_list[] =
{ ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL }; { ".comment", ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
/* /*
* Is this section one we do not want to check? * This is used to find sections missing the SHF_ALLOC flag.
* This is often debug sections.
* If we are going to check this section then
* test if section name ends with a dot and a number.
* This is used to find sections where the linker have
* appended a dot-number to make the name unique.
* The cause of this is often a section specified in assembler * The cause of this is often a section specified in assembler
* without "ax" / "aw" and the same section used in .c * without "ax" / "aw".
* code where gcc add these.
*/ */
static int check_section(const char *modname, const char *sec) static void check_section(const char *modname, struct elf_info *elf,
Elf_Shdr *sechdr)
{ {
const char *e = sec + strlen(sec) - 1; const char *sec = sech_name(elf, sechdr);
if (match(sec, section_white_list))
return 1;
if (*e && isdigit(*e)) { if (sechdr->sh_type == SHT_PROGBITS &&
/* consume all digits */ !(sechdr->sh_flags & SHF_ALLOC) &&
while (*e && e != sec && isdigit(*e)) !match(sec, section_white_list)) {
e--; warn("%s (%s): unexpected non-allocatable section.\n"
if (*e == '.' && !strstr(sec, ".linkonce")) { "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
warn("%s (%s): unexpected section name.\n"
"The (.[number]+) following section name are "
"ld generated and not expected.\n"
"Did you forget to use \"ax\"/\"aw\" "
"in a .S file?\n"
"Note that for example <linux/init.h> contains\n" "Note that for example <linux/init.h> contains\n"
"section definitions for use in .S files.\n\n", "section definitions for use in .S files.\n\n",
modname, sec); modname, sec);
} }
}
return 0;
} }
...@@ -1358,7 +1344,7 @@ static void section_rela(const char *modname, struct elf_info *elf, ...@@ -1358,7 +1344,7 @@ static void section_rela(const char *modname, struct elf_info *elf,
fromsec = sech_name(elf, sechdr); fromsec = sech_name(elf, sechdr);
fromsec += strlen(".rela"); fromsec += strlen(".rela");
/* if from section (name) is know good then skip it */ /* if from section (name) is know good then skip it */
if (check_section(modname, fromsec)) if (match(fromsec, section_white_list))
return; return;
for (rela = start; rela < stop; rela++) { for (rela = start; rela < stop; rela++) {
...@@ -1402,7 +1388,7 @@ static void section_rel(const char *modname, struct elf_info *elf, ...@@ -1402,7 +1388,7 @@ static void section_rel(const char *modname, struct elf_info *elf,
fromsec = sech_name(elf, sechdr); fromsec = sech_name(elf, sechdr);
fromsec += strlen(".rel"); fromsec += strlen(".rel");
/* if from section (name) is know good then skip it */ /* if from section (name) is know good then skip it */
if (check_section(modname, fromsec)) if (match(fromsec, section_white_list))
return; return;
for (rel = start; rel < stop; rel++) { for (rel = start; rel < stop; rel++) {
...@@ -1465,6 +1451,7 @@ static void check_sec_ref(struct module *mod, const char *modname, ...@@ -1465,6 +1451,7 @@ static void check_sec_ref(struct module *mod, const char *modname,
/* Walk through all sections */ /* Walk through all sections */
for (i = 0; i < elf->hdr->e_shnum; i++) { for (i = 0; i < elf->hdr->e_shnum; i++) {
check_section(modname, elf, &elf->sechdrs[i]);
/* We want to process only relocation sections and not .init */ /* We want to process only relocation sections and not .init */
if (sechdrs[i].sh_type == SHT_RELA) if (sechdrs[i].sh_type == SHT_RELA)
section_rela(modname, elf, &elf->sechdrs[i]); section_rela(modname, elf, &elf->sechdrs[i]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment