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

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 26 17:21:34 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, psmith.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.
MaskRay requested review of this revision.

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`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90200

Files:
  lld/ELF/LinkerScript.cpp
  lld/test/ELF/linkorder-mixed2.s


Index: lld/test/ELF/linkorder-mixed2.s
===================================================================
--- /dev/null
+++ lld/test/ELF/linkorder-mixed2.s
@@ -0,0 +1,21 @@
+# 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 different 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: 0100
+
+.section foo,"a"
+.byte 0
+
+.section .text,"ax", at progbits
+ret
+
+.section foo,"ao", at progbits,.text
+.byte 1
Index: lld/ELF/LinkerScript.cpp
===================================================================
--- lld/ELF/LinkerScript.cpp
+++ lld/ELF/LinkerScript.cpp
@@ -690,8 +690,11 @@
       auto *firstIsec = cast<InputSectionBase>(
           cast<InputSectionDescription>(sec->sectionCommands[0])
               ->sectionBases[0]);
-      if (firstIsec->getLinkOrderDep()->getOutputSection() !=
-          isec->getLinkOrderDep()->getOutputSection())
+      auto *firstIsecOut =
+          firstIsec->flags & SHF_LINK_ORDER
+              ? firstIsec->getLinkOrderDep()->getOutputSection()
+              : nullptr;
+      if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
         continue;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90200.300835.patch
Type: text/x-patch
Size: 1452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201027/e74f3b6d/attachment.bin>


More information about the llvm-commits mailing list