Skip to content

Commit 83716db

Browse files
MaskRaytstellar
authored andcommitted
[ELF] -r: don't crash when a non-SHF_LINK_ORDER orphan is added before a SHF_LINK_ORDER orphan
Fixes ClangBuiltLinux/linux#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 ae73091)
1 parent 1ff84a0 commit 83716db

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,11 @@ addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
679679
auto *firstIsec = cast<InputSectionBase>(
680680
cast<InputSectionDescription>(sec->sectionCommands[0])
681681
->sectionBases[0]);
682-
if (firstIsec->getLinkOrderDep()->getOutputSection() !=
683-
isec->getLinkOrderDep()->getOutputSection())
682+
OutputSection *firstIsecOut =
683+
firstIsec->flags & SHF_LINK_ORDER
684+
? firstIsec->getLinkOrderDep()->getOutputSection()
685+
: nullptr;
686+
if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
684687
continue;
685688
}
686689

lld/test/ELF/linkorder-mixed2.s

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# REQUIRES: x86
2+
## In a relocatable link, don't combine SHF_LINK_ORDER and non-SHF_LINK_ORDER
3+
## like we don't combine SHF_LINK_ORDER with different linked-to sections
4+
## (see linkerscript/linkorder-linked-to.s).
5+
## Test we support adding a non-SHF_LINK_ORDER section as an orphan first.
6+
7+
# RUN: llvm-mc -filetype=obj --triple=x86_64 %s -o %t.o
8+
9+
# RUN: ld.lld -r %t.o -o %t.ro
10+
# RUN: llvm-readelf -x foo %t.ro | FileCheck %s
11+
12+
# CHECK: Hex dump of section 'foo':
13+
# CHECK-NEXT: 0x00000000 0100
14+
15+
.section foo,"a"
16+
.byte 0
17+
18+
.section .text,"ax",@progbits
19+
ret
20+
21+
.section foo,"ao",@progbits,.text
22+
.byte 1

0 commit comments

Comments
 (0)