From ae73091f30245852817c5c0af050a5a731dee50a Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 27 Oct 2020 09:30:10 -0700 Subject: [PATCH] [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 --- lld/ELF/LinkerScript.cpp | 7 +++++-- lld/test/ELF/linkorder-mixed2.s | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 lld/test/ELF/linkorder-mixed2.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index ba51a8b..9d612ae 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -690,8 +690,11 @@ addInputSec(StringMap> &map, auto *firstIsec = cast( cast(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 0000000..26ab970 --- /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 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: Hex dump of section 'foo': +# CHECK-NEXT: 0x00000000 0100 + +.section foo,"a" +.byte 0 + +.section .text,"ax",@progbits +ret + +.section foo,"ao",@progbits,.text +.byte 1 -- 2.7.4