diff options
author | Eric Christopher <echristo@gmail.com> | 2018-06-25 23:53:54 +0000 |
---|---|---|
committer | Eric Christopher <echristo@gmail.com> | 2018-06-25 23:53:54 +0000 |
commit | b7a52bb28a75615369369d2d88554b0df084d05a (patch) | |
tree | 8a819743bb1e0f0d16b6303a986eb5cebdd7f7f2 /llvm/lib/MC | |
parent | 802c31cc283e7cb85a9eddc19a82af9f0602077d (diff) | |
download | bcm5719-llvm-b7a52bb28a75615369369d2d88554b0df084d05a.tar.gz bcm5719-llvm-b7a52bb28a75615369369d2d88554b0df084d05a.zip |
Add a warning if someone attempts to add extra section flags to sections
with well defined semantics like .rodata.
llvm-svn: 335558
Diffstat (limited to 'llvm/lib/MC')
-rw-r--r-- | llvm/lib/MC/MCParser/ELFAsmParser.cpp | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp index dc5b0e0d031..93513a2c415 100644 --- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp +++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp @@ -481,6 +481,31 @@ static bool hasPrefix(StringRef SectionName, StringRef Prefix) { return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back(); } +// Return a set of section flags based on the section name that can then +// be augmented later, otherwise return 0 if we don't have any reasonable +// defaults. +static unsigned defaultSectionFlags(StringRef SectionName) { + + if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1") + return ELF::SHF_ALLOC; + + if (SectionName == ".fini" || SectionName == ".init" || + hasPrefix(SectionName, ".text.")) + return ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; + + if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" || + hasPrefix(SectionName, ".bss.") || + hasPrefix(SectionName, ".init_array.") || + hasPrefix(SectionName, ".fini_array.") || + hasPrefix(SectionName, ".preinit_array.")) + return ELF::SHF_ALLOC | ELF::SHF_WRITE; + + if (hasPrefix(SectionName, ".tdata.") || hasPrefix(SectionName, ".tbss.")) + return ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS; + + return 0; +} + bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { StringRef SectionName; @@ -490,27 +515,13 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { StringRef TypeName; int64_t Size = 0; StringRef GroupName; - unsigned Flags = 0; const MCExpr *Subsection = nullptr; bool UseLastGroup = false; MCSymbolELF *Associated = nullptr; int64_t UniqueID = ~0; - // Set the defaults first. - if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1") - Flags |= ELF::SHF_ALLOC; - else if (SectionName == ".fini" || SectionName == ".init" || - hasPrefix(SectionName, ".text.")) - Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; - else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" || - hasPrefix(SectionName, ".bss.") || - hasPrefix(SectionName, ".init_array.") || - hasPrefix(SectionName, ".fini_array.") || - hasPrefix(SectionName, ".preinit_array.")) - Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE; - else if (hasPrefix(SectionName, ".tdata.") || - hasPrefix(SectionName, ".tbss.")) - Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS; + // Set the default section flags first in case no others are given. + unsigned Flags = defaultSectionFlags(SectionName); if (getLexer().is(AsmToken::Comma)) { Lex(); @@ -538,6 +549,12 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { if (extraFlags == -1U) return TokError("unknown flag"); + + // If we found additional section flags on a known section then give a + // warning. + if (Flags && Flags != extraFlags) + Warning(loc, "setting incorrect section attributes for " + SectionName); + Flags |= extraFlags; bool Mergeable = Flags & ELF::SHF_MERGE; |