Skip to content

Commit 75af9da

Browse files
committed
[MC][ELF] Error for sh_type, sh_flags or sh_entsize change
Heads-up message: https://lists.llvm.org/pipermail/llvm-dev/2020-February/139390.html GNU as started to emit warnings for changed sh_type or sh_flags in 2000. GNU as>=2.35 will emit errors for most sh_type/sh_flags change, and error for entsize change. Some cases remain warnings for legacy reasons: .section .init_array,"ax", @progbits .section .init_array,"ax", @init_array # And some obscure sh_flags changes (OS/Processor specific flags) The rationale of a diagnostic (warning or error) is that sh_type, sh_flags or sh_entsize changes usually indicate user errors. The values are taken from the first .section directive. Successive directives are ignored. We just try to be rigid and emit errors for all sh_type/sh_flags/sh_entsize change. A possible improvement in the future is to reuse llvm-readobj/ELFDumper.cpp:getSectionTypeString so that we can name the type in the diagnostics. Reviewed By: psmith Differential Revision: https://reviews.llvm.org/D73999
1 parent 266877a commit 75af9da

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,20 +634,29 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
634634
}
635635
}
636636

637-
MCSection *ELFSection = getContext().getELFSection(
637+
MCSectionELF *Section = getContext().getELFSection(
638638
SectionName, Type, Flags, Size, GroupName, UniqueID, LinkedToSym);
639-
getStreamer().SwitchSection(ELFSection, Subsection);
639+
getStreamer().SwitchSection(Section, Subsection);
640+
if (Section->getType() != Type)
641+
Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
642+
utohexstr(Section->getType()));
643+
if (Section->getFlags() != Flags)
644+
Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
645+
utohexstr(Section->getFlags()));
646+
if (Section->getEntrySize() != Size)
647+
Error(loc, "changed section entsize for " + SectionName +
648+
", expected: " + Twine(Section->getEntrySize()));
640649

641650
if (getContext().getGenDwarfForAssembly()) {
642-
bool InsertResult = getContext().addGenDwarfSection(ELFSection);
651+
bool InsertResult = getContext().addGenDwarfSection(Section);
643652
if (InsertResult) {
644653
if (getContext().getDwarfVersion() <= 2)
645654
Warning(loc, "DWARF2 only supports one section per compilation unit");
646655

647-
if (!ELFSection->getBeginSymbol()) {
656+
if (!Section->getBeginSymbol()) {
648657
MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
649658
getStreamer().emitLabel(SectionStartSymbol);
650-
ELFSection->setBeginSymbol(SectionStartSymbol);
659+
Section->setBeginSymbol(SectionStartSymbol);
651660
}
652661
}
653662
}

llvm/test/MC/ELF/exclude-debug-dwo.s

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
# CHECK: .debug_loc.dwo {{.*}} E
1111
# CHECK: .debug_str_offsets.dwo {{.*}} E
1212

13-
.section .debug_info.dwo
13+
.section .debug_info.dwo,"e"
1414
nop
1515

16-
.section .debug_types.dwo
16+
.section .debug_types.dwo,"e"
1717
nop
1818

19-
.section .debug_abbrev.dwo
19+
.section .debug_abbrev.dwo,"e"
2020
nop
2121

22-
.section .debug_str.dwo
22+
.section .debug_str.dwo,"MSe",@progbits,1
2323
nop
2424

25-
.section .debug_line.dwo
25+
.section .debug_line.dwo,"e"
2626
nop
2727

28-
.section .debug_loc.dwo
28+
.section .debug_loc.dwo,"e"
2929
nop
3030

31-
.section .debug_str_offsets.dwo
31+
.section .debug_str_offsets.dwo,"e"
3232
nop
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# RUN: not llvm-mc -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
2+
3+
foo:
4+
.section .foo,"aM",@progbits,1
5+
6+
# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section entsize for .foo, expected: 1
7+
.section .foo,"aM",@progbits,4
8+
9+
# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section entsize for .foo, expected: 1
10+
.pushsection .foo,"aM",@progbits,4
11+
12+
.pushsection .foo,"aM",@progbits,1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# RUN: not llvm-mc -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
2+
3+
foo:
4+
.section .foo,"ax",@progbits
5+
6+
# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section flags for .foo, expected: 0x6
7+
.section .foo,"awx",@progbits
8+
9+
# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section flags for .foo, expected: 0x6
10+
.pushsection .foo,"a",@progbits
11+
12+
.pushsection .foo,"ax",@progbits
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RUN: not llvm-mc -triple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
2+
3+
.section .foo,"a",@progbits
4+
5+
# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section type for .foo, expected: 0x1
6+
.section .foo,"a",@init_array
7+
8+
# CHECK: {{.*}}.s:[[# @LINE+1]]:1: error: changed section type for .foo, expected: 0x1
9+
.pushsection .foo,"a",@nobits
10+
11+
.pushsection .foo,"a",@progbits

0 commit comments

Comments
 (0)