From 62f7af712c0043e8bdb83e6c91bde43a11aab210 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Mon, 14 May 2018 18:36:51 +0000 Subject: [PATCH] COFF: Allow ICFing sections with different alignments. The combined section gets the maximum alignment of all sections. Differential Revision: https://reviews.llvm.org/D46786 llvm-svn: 332273 --- lld/COFF/Chunks.cpp | 1 + lld/COFF/ICF.cpp | 6 +++--- lld/test/COFF/icf-different-align.test | 13 +++++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp index 5f673f8..ab6160c 100644 --- a/lld/COFF/Chunks.cpp +++ b/lld/COFF/Chunks.cpp @@ -438,6 +438,7 @@ ArrayRef SectionChunk::getContents() const { } void SectionChunk::replace(SectionChunk *Other) { + Alignment = std::max(Alignment, Other->Alignment); Other->Repl = Repl; Other->Live = false; } diff --git a/lld/COFF/ICF.cpp b/lld/COFF/ICF.cpp index 78d667f..6297209 100644 --- a/lld/COFF/ICF.cpp +++ b/lld/COFF/ICF.cpp @@ -67,8 +67,8 @@ private: // Returns a hash value for S. uint32_t ICF::getHash(SectionChunk *C) { - return hash_combine(C->getOutputCharacteristics(), C->SectionName, C->Relocs.size(), - C->Alignment, uint32_t(C->Header->SizeOfRawData), + return hash_combine(C->getOutputCharacteristics(), C->SectionName, + C->Relocs.size(), uint32_t(C->Header->SizeOfRawData), C->Checksum, C->getContents()); } @@ -168,7 +168,7 @@ bool ICF::equalsConstant(const SectionChunk *A, const SectionChunk *B) { // Compare section attributes and contents. return A->getOutputCharacteristics() == B->getOutputCharacteristics() && - A->SectionName == B->SectionName && A->Alignment == B->Alignment && + A->SectionName == B->SectionName && A->Header->SizeOfRawData == B->Header->SizeOfRawData && A->Checksum == B->Checksum && A->getContents() == B->getContents() && assocEquals(A, B); diff --git a/lld/test/COFF/icf-different-align.test b/lld/test/COFF/icf-different-align.test index 3502ed3..0e2fce9 100644 --- a/lld/test/COFF/icf-different-align.test +++ b/lld/test/COFF/icf-different-align.test @@ -2,9 +2,14 @@ # RUN: lld-link /entry:foo /out:%t.exe /subsystem:console /include:bar \ # RUN: /verbose %t.obj > %t.log 2>&1 # RUN: FileCheck %s < %t.log +# RUN: llvm-objdump -s %t.exe | FileCheck --check-prefix=OBJDUMP %s -# CHECK-NOT: Selected foo -# CHECK-NOT: Removed bar +# CHECK: Selected foo +# CHECK: Removed bar + +# OBJDUMP: Contents of section .text: +# OBJDUMP-NEXT: 140001000 00cccccc cccccccc cccccccc cccccccc +# OBJDUMP-NEXT: 140001010 4883ec28 e8000000 004883c4 28c3 --- !COFF header: @@ -19,6 +24,10 @@ sections: Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] Alignment: 16 SectionData: 4883EC28E8000000004883C428C3 + - Name: '.text' + Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] + Alignment: 1 + SectionData: 00 symbols: - Name: '.text$mn' Value: 0 -- 2.7.4