From 4376cffb5703410561136132a7799ee4535678fc Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 23 Mar 2018 17:19:18 +0000 Subject: [PATCH] Add a minimal fix for PR36878. When looking for the output section and the output offset the expectation was that the caller had looked at Repl. That works fine for InputSections, but in the case of MergeInputSections the caller doesn't have the section that is actually replaced. The original testcase was failing because getOutputSection was returning null. The slightly extended testcase also checks that getOffset also checks Repl. I will send a refactoring separetelly. llvm-svn: 328332 --- lld/ELF/InputSection.cpp | 6 +++--- lld/test/ELF/icf-merged-sections.s | 33 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 lld/test/ELF/icf-merged-sections.s diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 713a781206a6..9814de9f2789 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -156,7 +156,7 @@ uint64_t SectionBase::getOffset(uint64_t Offset) const { case Merge: const MergeInputSection *MS = cast(this); if (InputSection *IS = MS->getParent()) - return IS->OutSecOff + MS->getOffset(Offset); + return cast(IS->Repl)->OutSecOff + MS->getOffset(Offset); return MS->getOffset(Offset); } llvm_unreachable("invalid section kind"); @@ -165,14 +165,14 @@ uint64_t SectionBase::getOffset(uint64_t Offset) const { OutputSection *SectionBase::getOutputSection() { InputSection *Sec; if (auto *IS = dyn_cast(this)) - return IS->getParent(); + Sec = IS; else if (auto *MS = dyn_cast(this)) Sec = MS->getParent(); else if (auto *EH = dyn_cast(this)) Sec = EH->getParent(); else return cast(this); - return Sec ? Sec->getParent() : nullptr; + return Sec ? cast(Sec->Repl)->getParent() : nullptr; } // Decompress section contents if required. Note that this function diff --git a/lld/test/ELF/icf-merged-sections.s b/lld/test/ELF/icf-merged-sections.s new file mode 100644 index 000000000000..e29bd4938d70 --- /dev/null +++ b/lld/test/ELF/icf-merged-sections.s @@ -0,0 +1,33 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: ld.lld %t.o -o %t --icf=all --ignore-data-address-equality --print-icf-sections | FileCheck %s --check-prefix ICF +# RUN: llvm-objdump -s -d -print-imm-hex %t | FileCheck %s + +# ICF: selected section :(.rodata) +# ICF-NEXT: removing identical section :(.rodata) + +# CHECK: {{^}}.text: +# CHECK-NEXT: movq 0x[[ADDR:[0-9a-f]+]], %rax +# CHECK-NEXT: movq 0x[[ADDR]], %rax +# CHECK: Contents of section .rodata: +# CHECK-NEXT: 2a000000 00000000 67452301 10325476 + +.section .rodata, "a" + .quad 42 + +.section .rodata.cst4,"aM",@progbits,4 +rodata4: + .long 0x01234567 + .long 0x76543210 + .long 0x01234567 + .long 0x76543210 + +.section .rodata.cst8,"aM",@progbits,8 +rodata8: + .long 0x01234567 + .long 0x76543210 + +.section .text,"ax" + movq rodata4, %rax + movq rodata8, %rax