[lld] r292578 - ELF: Fix ICF crash on absolute symbol relocations.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 19 20:58:12 PST 2017


Author: pcc
Date: Thu Jan 19 22:58:12 2017
New Revision: 292578

URL: http://llvm.org/viewvc/llvm-project?rev=292578&view=rev
Log:
ELF: Fix ICF crash on absolute symbol relocations.

If two sections contained relocations to absolute symbols with the same
value we would crash when trying to access their sections. Add a check that
both symbols point to sections before accessing their sections, and treat
absolute symbols as equal if their values are equal.

Differential Revision: https://reviews.llvm.org/D28935

Added:
    lld/trunk/test/ELF/Inputs/icf-absolute.s
    lld/trunk/test/ELF/icf-absolute.s
Modified:
    lld/trunk/ELF/ICF.cpp

Modified: lld/trunk/ELF/ICF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ICF.cpp?rev=292578&r1=292577&r2=292578&view=diff
==============================================================================
--- lld/trunk/ELF/ICF.cpp (original)
+++ lld/trunk/ELF/ICF.cpp Thu Jan 19 22:58:12 2017
@@ -245,7 +245,6 @@ bool ICF<ELFT>::variableEq(const InputSe
     if (&SA == &SB)
       return true;
 
-    // Or, the two sections must be in the same equivalence class.
     auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA);
     auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB);
     if (!DA || !DB)
@@ -253,6 +252,11 @@ bool ICF<ELFT>::variableEq(const InputSe
     if (DA->Value != DB->Value)
       return false;
 
+    // Either both symbols must be absolute...
+    if (!DA->Section || !DB->Section)
+      return !DA->Section && !DB->Section;
+
+    // Or the two sections must be in the same equivalence class.
     auto *X = dyn_cast<InputSection<ELFT>>(DA->Section);
     auto *Y = dyn_cast<InputSection<ELFT>>(DB->Section);
     if (!X || !Y)

Added: lld/trunk/test/ELF/Inputs/icf-absolute.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/icf-absolute.s?rev=292578&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/icf-absolute.s (added)
+++ lld/trunk/test/ELF/Inputs/icf-absolute.s Thu Jan 19 22:58:12 2017
@@ -0,0 +1,3 @@
+.globl a1, a2
+a1 = 1
+a2 = 1

Added: lld/trunk/test/ELF/icf-absolute.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/icf-absolute.s?rev=292578&view=auto
==============================================================================
--- lld/trunk/test/ELF/icf-absolute.s (added)
+++ lld/trunk/test/ELF/icf-absolute.s Thu Jan 19 22:58:12 2017
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-absolute.s -o %t2
+# RUN: ld.lld %t %t2 -o %t3 --icf=all --verbose | FileCheck %s
+
+# CHECK: selected .text.f1
+# CHECK:   removed .text.f2
+
+.globl _start, f1, f2
+_start:
+  ret
+
+.section .text.f1, "ax"
+f1:
+  .byte a1
+
+.section .text.f2, "ax"
+f2:
+  .byte a2




More information about the llvm-commits mailing list