From a51acc7631fec5bc371d996d7cea45e8a3caee95 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 6 Jun 2014 21:40:16 +0000 Subject: [PATCH] MC: prevent early DCE of empty sections Prevent the early elimination of sections in the object writer. There may be references to the section itself by other symbols, which may potentially not be possible to resolve. ML (Visual Studio's Macro Assembler) also seems to retain empty sections. The elimination of symbols and sections which are unused should really occur at the link phase. This will not cause any change in the resulting binary, simply in the generated object files. The adjustments to the other unit tests account for the fluctuating section index caused by the appearance of sections which were previously discarded. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210373 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/WinCOFFObjectWriter.cpp | 10 +-- test/MC/COFF/alias.s | 7 ++- test/MC/COFF/basic-coff-64.s | 4 +- test/MC/COFF/basic-coff.s | 4 +- test/MC/COFF/early-dce.s | 16 +++++ test/MC/COFF/linkonce.s | 28 ++++----- test/MC/COFF/section-comdat.s | 80 ++++++++++++------------ test/MC/COFF/section-name-encoding.s | 10 +-- test/MC/COFF/seh.s | 4 +- test/MC/COFF/symbol-fragment-offset-64.s | 4 +- test/MC/COFF/symbol-fragment-offset.s | 4 +- 11 files changed, 92 insertions(+), 79 deletions(-) create mode 100644 test/MC/COFF/early-dce.s diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index 6f9c73b9cc5..2d4b75858a5 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -833,13 +833,9 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm, DenseMap SectionIndices; for (auto & Section : Sections) { - if (Layout.getSectionAddressSize(Section->MCData) > 0) { - size_t Number = ++Header.NumberOfSections; - SectionIndices[Section.get()] = Number; - MakeSectionReal(*Section, Number); - } else { - Section->Number = -1; - } + size_t Number = ++Header.NumberOfSections; + SectionIndices[Section.get()] = Number; + MakeSectionReal(*Section, Number); } Header.NumberOfSymbols = 0; diff --git a/test/MC/COFF/alias.s b/test/MC/COFF/alias.s index dc4f65acf74..eb5398a4ed2 100644 --- a/test/MC/COFF/alias.s +++ b/test/MC/COFF/alias.s @@ -1,4 +1,5 @@ -// RUN: llvm-mc -filetype=obj -triple i686-pc-win32 %s -o - | llvm-readobj -t -r | FileCheck %s +// RUN: llvm-mc -filetype=obj -triple i686-pc-win32 %s -o - \ +// RUN: | llvm-readobj -t -r | FileCheck %s local1: external_aliased_to_local = local1 @@ -36,7 +37,7 @@ weak_aliased_to_external = external2 // CHECK-NEXT: AuxSymbolCount: 1 // CHECK: } // CHECK: Symbol { -// CHECK-NEXT: Name: local1 +// CHECK: Name: local1 // CHECK-NEXT: Value: 0 // CHECK-NEXT: Section: .text (1) // CHECK-NEXT: BaseType: Null (0x0) @@ -89,7 +90,7 @@ weak_aliased_to_external = external2 // CHECK-NEXT: StorageClass: WeakExternal (0x69) // CHECK-NEXT: AuxSymbolCount: 1 // CHECK-NEXT: AuxWeakExternal { -// CHECK-NEXT: Linked: external2 (9) +// CHECK-NEXT: Linked: external2 (13) // CHECK-NEXT: Search: Library (0x2) // CHECK-NEXT: Unused: (00 00 00 00 00 00 00 00 00 00) // CHECK-NEXT: } diff --git a/test/MC/COFF/basic-coff-64.s b/test/MC/COFF/basic-coff-64.s index 89d17452d0d..38a9e578a4c 100644 --- a/test/MC/COFF/basic-coff-64.s +++ b/test/MC/COFF/basic-coff-64.s @@ -25,10 +25,10 @@ _main: # @main // CHECK: ImageFileHeader { // CHECK: Machine: IMAGE_FILE_MACHINE_AMD64 -// CHECK: SectionCount: 2 +// CHECK: SectionCount: 3 // CHECK: TimeDateStamp: {{[0-9]+}} // CHECK: PointerToSymbolTable: 0x{{[0-9A-F]+}} -// CHECK: SymbolCount: 6 +// CHECK: SymbolCount: 8 // CHECK: OptionalHeaderSize: 0 // CHECK: Characteristics [ (0x0) // CHECK: ] diff --git a/test/MC/COFF/basic-coff.s b/test/MC/COFF/basic-coff.s index 9b299707a13..38bfa6d1014 100644 --- a/test/MC/COFF/basic-coff.s +++ b/test/MC/COFF/basic-coff.s @@ -25,10 +25,10 @@ L_.str: # @.str // CHECK: ImageFileHeader { // CHECK: Machine: IMAGE_FILE_MACHINE_I386 -// CHECK: SectionCount: 2 +// CHECK: SectionCount: 3 // CHECK: TimeDateStamp: {{[0-9]+}} // CHECK: PointerToSymbolTable: 0x{{[0-9A-F]+}} -// CHECK: SymbolCount: 6 +// CHECK: SymbolCount: 8 // CHECK: OptionalHeaderSize: 0 // CHECK: Characteristics [ (0x0) // CHECK: ] diff --git a/test/MC/COFF/early-dce.s b/test/MC/COFF/early-dce.s new file mode 100644 index 00000000000..ec1a9bda76f --- /dev/null +++ b/test/MC/COFF/early-dce.s @@ -0,0 +1,16 @@ +# RUN: llvm-mc -triple i686-windows -g -filetype obj -o - %s \ +# RUN: | llvm-readobj -s -t | FileCheck %s + + .section .rdata + + .align 8 + .global data +data: + .quad 0 + +# CHECK: Sections [ +# CHECK: Section { +# CHECK: Name: .text +# CHECK: } +# CHECK: ] + diff --git a/test/MC/COFF/linkonce.s b/test/MC/COFF/linkonce.s index b6e81ad4b62..f2e350645bf 100644 --- a/test/MC/COFF/linkonce.s +++ b/test/MC/COFF/linkonce.s @@ -91,41 +91,41 @@ // CHECK: Symbols [ // CHECK: Symbol { // CHECK: Name: s1 -// CHECK: Section: s1 (1) +// CHECK: Section: s1 (4) // CHECK: AuxSectionDef { -// CHECK: Number: 1 +// CHECK: Number: 4 // CHECK: Selection: Any (0x2) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: s2 -// CHECK: Section: s2 (2) +// CHECK: Section: s2 (5) // CHECK: AuxSectionDef { -// CHECK: Number: 2 +// CHECK: Number: 5 // CHECK: Selection: NoDuplicates (0x1) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: s3 -// CHECK: Section: s3 (3) +// CHECK: Section: s3 (6) // CHECK: AuxSectionDef { -// CHECK: Number: 3 +// CHECK: Number: 6 // CHECK: Selection: Any (0x2) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: s4 -// CHECK: Section: s4 (4) +// CHECK: Section: s4 (7) // CHECK: AuxSectionDef { -// CHECK: Number: 4 +// CHECK: Number: 7 // CHECK: Selection: SameSize (0x3) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: s5 -// CHECK: Section: s5 (5) +// CHECK: Section: s5 (8) // CHECK: AuxSectionDef { -// CHECK: Number: 5 +// CHECK: Number: 8 // CHECK: Selection: ExactMatch (0x4) // CHECK: } // CHECK: } @@ -134,17 +134,17 @@ // CHECK: } // CHECK: Symbol { // CHECK: Name: s7 -// CHECK: Section: s7 (7) +// CHECK: Section: s7 (10) // CHECK: AuxSectionDef { -// CHECK: Number: 7 +// CHECK: Number: 10 // CHECK: Selection: Largest (0x6) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: s8 -// CHECK: Section: s8 (8) +// CHECK: Section: s8 (11) // CHECK: AuxSectionDef { -// CHECK: Number: 8 +// CHECK: Number: 11 // CHECK: Selection: Newest (0x7) // CHECK: } // CHECK: } diff --git a/test/MC/COFF/section-comdat.s b/test/MC/COFF/section-comdat.s index a0ea7d0928d..8a7de1a1d91 100644 --- a/test/MC/COFF/section-comdat.s +++ b/test/MC/COFF/section-comdat.s @@ -41,43 +41,22 @@ Symbol7: // CHECK: Sections [ // CHECK: Section { -// CHECK: Number: 1 +// CHECK: Number: 4 // CHECK: Name: assocSec // CHECK: Characteristics [ // CHECK: IMAGE_SCN_LNK_COMDAT // CHECK: ] // CHECK: } // CHECK: Section { -// CHECK: Number: 2 -// CHECK: Name: secName -// CHECK: Characteristics [ -// CHECK: IMAGE_SCN_LNK_COMDAT -// CHECK: ] -// CHECK: } -// CHECK: Section { -// CHECK: Number: 3 -// CHECK: Name: secName -// CHECK: Characteristics [ -// CHECK: IMAGE_SCN_LNK_COMDAT -// CHECK: ] -// CHECK: } -// CHECK: Section { -// CHECK: Number: 4 -// CHECK: Name: SecName -// CHECK: Characteristics [ -// CHECK: IMAGE_SCN_LNK_COMDAT -// CHECK: ] -// CHECK: } -// CHECK: Section { // CHECK: Number: 5 -// CHECK: Name: SecName +// CHECK: Name: secName // CHECK: Characteristics [ // CHECK: IMAGE_SCN_LNK_COMDAT // CHECK: ] // CHECK: } // CHECK: Section { // CHECK: Number: 6 -// CHECK: Name: SecName +// CHECK: Name: secName // CHECK: Characteristics [ // CHECK: IMAGE_SCN_LNK_COMDAT // CHECK: ] @@ -96,11 +75,32 @@ Symbol7: // CHECK: IMAGE_SCN_LNK_COMDAT // CHECK: ] // CHECK: } +// CHECK: Section { +// CHECK: Number: 9 +// CHECK: Name: SecName +// CHECK: Characteristics [ +// CHECK: IMAGE_SCN_LNK_COMDAT +// CHECK: ] +// CHECK: } +// CHECK: Section { +// CHECK: Number: 10 +// CHECK: Name: SecName +// CHECK: Characteristics [ +// CHECK: IMAGE_SCN_LNK_COMDAT +// CHECK: ] +// CHECK: } +// CHECK: Section { +// CHECK: Number: 11 +// CHECK: Name: SecName +// CHECK: Characteristics [ +// CHECK: IMAGE_SCN_LNK_COMDAT +// CHECK: ] +// CHECK: } // CHECK: ] // CHECK: Symbols [ // CHECK: Symbol { // CHECK: Name: assocSec -// CHECK: Section: assocSec (1) +// CHECK: Section: assocSec (4) // CHECK: AuxSectionDef { // CHECK: Selection: Any // CHECK: } @@ -111,40 +111,40 @@ Symbol7: // CHECK: } // CHECK: Symbol { // CHECK: Name: secName -// CHECK: Section: secName (2) +// CHECK: Section: secName (5) // CHECK: AuxSectionDef { // CHECK: Selection: Any // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol1 -// CHECK: Section: secName (2) +// CHECK: Section: secName (5) // CHECK: } // CHECK: Symbol { // CHECK: Name: secName -// CHECK: Section: secName (3) +// CHECK: Section: secName (6) // CHECK: AuxSectionDef { // CHECK: Selection: NoDuplicates // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol2 -// CHECK: Section: secName (3) +// CHECK: Section: secName (6) // CHECK: } // CHECK: Symbol { // CHECK: Name: SecName -// CHECK: Section: SecName (4) +// CHECK: Section: SecName (7) // CHECK: AuxSectionDef { // CHECK: Selection: SameSize // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol3 -// CHECK: Section: SecName (4) +// CHECK: Section: SecName (7) // CHECK: } // CHECK: Symbol { // CHECK: Name: SecName -// CHECK: Section: SecName (5) +// CHECK: Section: SecName (8) // CHECK: AuxSymbolCount: 1 // CHECK: AuxSectionDef { // CHECK: Selection: ExactMatch @@ -152,40 +152,40 @@ Symbol7: // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol4 -// CHECK: Section: SecName (5) +// CHECK: Section: SecName (8) // CHECK: } // CHECK: Symbol { // CHECK: Name: SecName -// CHECK: Section: SecName (6) +// CHECK: Section: SecName (9) // CHECK: AuxSectionDef { // CHECK: Selection: Associative -// CHECK: AssocSection: assocSec (1) +// CHECK: AssocSection: assocSec (4) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: SecName -// CHECK: Section: SecName (7) +// CHECK: Section: SecName (10) // CHECK: AuxSectionDef { // CHECK: Selection: Largest // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol6 -// CHECK: Section: SecName (7) +// CHECK: Section: SecName (10) // CHECK: } // CHECK: Symbol { // CHECK: Name: SecName -// CHECK: Section: SecName (8) +// CHECK: Section: SecName (11) // CHECK: AuxSectionDef { // CHECK: Selection: Newest (0x7) // CHECK: } // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol7 -// CHECK: Section: SecName (8) +// CHECK: Section: SecName (11) // CHECK: } // CHECK: Symbol { // CHECK: Name: Symbol5 -// CHECK: Section: SecName (6) +// CHECK: Section: SecName (9) // CHECK: } // CHECK: ] diff --git a/test/MC/COFF/section-name-encoding.s b/test/MC/COFF/section-name-encoding.s index 74cd490bd3c..7edd6d7446d 100644 --- a/test/MC/COFF/section-name-encoding.s +++ b/test/MC/COFF/section-name-encoding.s @@ -10,11 +10,11 @@ // Raw encoding // CHECK: Section { -// CHECK: Number: 1 +// CHECK: Number: 4 // CHECK: Name: s (73 00 00 00 00 00 00 00) // CHECK: } // CHECK: Section { -// CHECK: Number: 2 +// CHECK: Number: 5 // CHECK: Name: s1234567 (73 31 32 33 34 35 36 37) // CHECK: } .section s; .long 1 @@ -25,7 +25,7 @@ // /4 // CHECK: Section { -// CHECK: Number: 3 +// CHECK: Number: 6 // CHECK: Name: s12345678 (2F 34 00 00 00 00 00 00) // CHECK: } .section s12345678; .long 1 @@ -57,7 +57,7 @@ pad_sections aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa // "s12345678\0" # of pad sections // // CHECK: Section { -// CHECK: Number: 9 +// CHECK: Number: 12 // CHECK: Name: seven_digit (2F 31 30 30 30 30 32 39) // CHECK: } .section seven_digit; .long 1 @@ -82,7 +82,7 @@ pad_sections_ex aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa // "2F 2F 41 41 6D 4A 61 34" is "//AAmJa4", which decodes to "0 0 38 9 26 56". // // CHECK: Section { -// CHECK: Number: 15 +// CHECK: Number: 18 // CHECK: Name: double_slash (2F 2F 41 41 6D 4A 61 34) // CHECK: } .section double_slash; .long 1 diff --git a/test/MC/COFF/seh.s b/test/MC/COFF/seh.s index 72d42f4b2ea..cd884b4fa00 100644 --- a/test/MC/COFF/seh.s +++ b/test/MC/COFF/seh.s @@ -35,13 +35,13 @@ // CHECK-NEXT: ] // CHECK-NEXT: Relocations [ -// CHECK-NEXT: Section (2) .xdata { +// CHECK-NEXT: Section (4) .xdata { // CHECK-NEXT: 0x14 IMAGE_REL_AMD64_ADDR32NB __C_specific_handler // CHECK-NEXT: 0x20 IMAGE_REL_AMD64_ADDR32NB func // CHECK-NEXT: 0x24 IMAGE_REL_AMD64_ADDR32NB func // CHECK-NEXT: 0x28 IMAGE_REL_AMD64_ADDR32NB .xdata // CHECK-NEXT: } -// CHECK-NEXT: Section (3) .pdata { +// CHECK-NEXT: Section (5) .pdata { // CHECK-NEXT: 0x0 IMAGE_REL_AMD64_ADDR32NB func // CHECK-NEXT: 0x4 IMAGE_REL_AMD64_ADDR32NB func // CHECK-NEXT: 0x8 IMAGE_REL_AMD64_ADDR32NB .xdata diff --git a/test/MC/COFF/symbol-fragment-offset-64.s b/test/MC/COFF/symbol-fragment-offset-64.s index b8244709aa7..deac88869b2 100644 --- a/test/MC/COFF/symbol-fragment-offset-64.s +++ b/test/MC/COFF/symbol-fragment-offset-64.s @@ -36,10 +36,10 @@ _main: # @main // CHECK: { // CHECK: Machine: IMAGE_FILE_MACHINE_AMD64 -// CHECK: SectionCount: 2 +// CHECK: SectionCount: 3 // CHECK: TimeDateStamp: {{[0-9]+}} // CHECK: PointerToSymbolTable: 0x{{[0-9A-F]+}} -// CHECK: SymbolCount: 7 +// CHECK: SymbolCount: 9 // CHECK: OptionalHeaderSize: 0 // CHECK: Characteristics [ (0x0) // CHECK: ] diff --git a/test/MC/COFF/symbol-fragment-offset.s b/test/MC/COFF/symbol-fragment-offset.s index 71b1703972a..b09c5af1b61 100644 --- a/test/MC/COFF/symbol-fragment-offset.s +++ b/test/MC/COFF/symbol-fragment-offset.s @@ -36,10 +36,10 @@ L_.str2: // CHECK: { // CHECK: Machine: IMAGE_FILE_MACHINE_I386 (0x14C) -// CHECK: SectionCount: 2 +// CHECK: SectionCount: 3 // CHECK: TimeDateStamp: {{[0-9]+}} // CHECK: PointerToSymbolTable: 0x{{[0-9A-F]+}} -// CHECK: SymbolCount: 7 +// CHECK: SymbolCount: 9 // CHECK: OptionalHeaderSize: 0 // CHECK: Characteristics [ (0x0) // CHECK: ]