[lld] r328332 - Add a minimal fix for PR36878.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 23 10:19:18 PDT 2018


Author: rafael
Date: Fri Mar 23 10:19:18 2018
New Revision: 328332

URL: http://llvm.org/viewvc/llvm-project?rev=328332&view=rev
Log:
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.

Added:
    lld/trunk/test/ELF/icf-merged-sections.s
Modified:
    lld/trunk/ELF/InputSection.cpp

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=328332&r1=328331&r2=328332&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Fri Mar 23 10:19:18 2018
@@ -156,7 +156,7 @@ uint64_t SectionBase::getOffset(uint64_t
   case Merge:
     const MergeInputSection *MS = cast<MergeInputSection>(this);
     if (InputSection *IS = MS->getParent())
-      return IS->OutSecOff + MS->getOffset(Offset);
+      return cast<InputSection>(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
 OutputSection *SectionBase::getOutputSection() {
   InputSection *Sec;
   if (auto *IS = dyn_cast<InputSection>(this))
-    return IS->getParent();
+    Sec = IS;
   else if (auto *MS = dyn_cast<MergeInputSection>(this))
     Sec = MS->getParent();
   else if (auto *EH = dyn_cast<EhInputSection>(this))
     Sec = EH->getParent();
   else
     return cast<OutputSection>(this);
-  return Sec ? Sec->getParent() : nullptr;
+  return Sec ? cast<InputSection>(Sec->Repl)->getParent() : nullptr;
 }
 
 // Decompress section contents if required. Note that this function

Added: lld/trunk/test/ELF/icf-merged-sections.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/icf-merged-sections.s?rev=328332&view=auto
==============================================================================
--- lld/trunk/test/ELF/icf-merged-sections.s (added)
+++ lld/trunk/test/ELF/icf-merged-sections.s Fri Mar 23 10:19:18 2018
@@ -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 <internal>:(.rodata)
+# ICF-NEXT: removing identical section <internal>:(.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", at progbits,4
+rodata4:
+  .long 0x01234567
+  .long 0x76543210
+  .long 0x01234567
+  .long 0x76543210
+
+.section .rodata.cst8,"aM", at progbits,8
+rodata8:
+  .long 0x01234567
+  .long 0x76543210
+
+.section .text,"ax"
+  movq rodata4, %rax
+  movq rodata8, %rax




More information about the llvm-commits mailing list