[PATCH] D28935: ELF: Fix ICF crash on absolute symbol relocations.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 19 19:57:24 PST 2017


pcc created this revision.

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.


https://reviews.llvm.org/D28935

Files:
  lld/ELF/ICF.cpp
  lld/test/ELF/Inputs/icf-absolute.s
  lld/test/ELF/icf-absolute.s


Index: lld/test/ELF/icf-absolute.s
===================================================================
--- /dev/null
+++ lld/test/ELF/icf-absolute.s
@@ -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
Index: lld/test/ELF/Inputs/icf-absolute.s
===================================================================
--- /dev/null
+++ lld/test/ELF/Inputs/icf-absolute.s
@@ -0,0 +1,3 @@
+.globl a1, a2
+a1 = 1
+a2 = 1
Index: lld/ELF/ICF.cpp
===================================================================
--- lld/ELF/ICF.cpp
+++ lld/ELF/ICF.cpp
@@ -245,14 +245,18 @@
     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)
       return false;
     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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28935.85094.patch
Type: text/x-patch
Size: 1609 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170120/ddeb11e0/attachment.bin>


More information about the llvm-commits mailing list