[llvm-branch-commits] [lld] 83716db - [ELF] -r: don't crash when a non-SHF_LINK_ORDER orphan is added before a SHF_LINK_ORDER orphan

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Oct 30 21:30:42 PDT 2020


Author: Fangrui Song
Date: 2020-10-31T00:29:52-04:00
New Revision: 83716db47f94391771bf469262ab4282679d8981

URL: https://github.com/llvm/llvm-project/commit/83716db47f94391771bf469262ab4282679d8981
DIFF: https://github.com/llvm/llvm-project/commit/83716db47f94391771bf469262ab4282679d8981.diff

LOG: [ELF] -r: don't crash when a non-SHF_LINK_ORDER orphan is added before a SHF_LINK_ORDER orphan

Fixes https://github.com/ClangBuiltLinux/linux/issues/1186

If a non-SHF_LINK_ORDER orphan is added first, `firstIsec->flags & SHF_LINK_ORDER`
will be zero and we currently assert when calling `getLinkOrderDep`.

Reviewed By: grimar

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

(cherry picked from commit ae73091f30245852817c5c0af050a5a731dee50a)

Added: 
    lld/test/ELF/linkorder-mixed2.s

Modified: 
    lld/ELF/LinkerScript.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 6de2cd65b973..7314b27659bb 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -679,8 +679,11 @@ addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
       auto *firstIsec = cast<InputSectionBase>(
           cast<InputSectionDescription>(sec->sectionCommands[0])
               ->sectionBases[0]);
-      if (firstIsec->getLinkOrderDep()->getOutputSection() !=
-          isec->getLinkOrderDep()->getOutputSection())
+      OutputSection *firstIsecOut =
+          firstIsec->flags & SHF_LINK_ORDER
+              ? firstIsec->getLinkOrderDep()->getOutputSection()
+              : nullptr;
+      if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
         continue;
     }
 

diff  --git a/lld/test/ELF/linkorder-mixed2.s b/lld/test/ELF/linkorder-mixed2.s
new file mode 100644
index 000000000000..26ab970676aa
--- /dev/null
+++ b/lld/test/ELF/linkorder-mixed2.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+## In a relocatable link, don't combine SHF_LINK_ORDER and non-SHF_LINK_ORDER
+## like we don't combine SHF_LINK_ORDER with 
diff erent linked-to sections
+## (see linkerscript/linkorder-linked-to.s).
+## Test we support adding a non-SHF_LINK_ORDER section as an orphan first.
+
+# RUN: llvm-mc -filetype=obj --triple=x86_64 %s -o %t.o
+
+# RUN: ld.lld -r %t.o -o %t.ro
+# RUN: llvm-readelf -x foo %t.ro | FileCheck %s
+
+# CHECK:      Hex dump of section 'foo':
+# CHECK-NEXT: 0x00000000 0100
+
+.section foo,"a"
+.byte 0
+
+.section .text,"ax", at progbits
+ret
+
+.section foo,"ao", at progbits,.text
+.byte 1


        


More information about the llvm-branch-commits mailing list