Skip to content

Commit 4d54a4b

Browse files
committedOct 10, 2017
[LLD] Fix findOrphanPos to consistently ignore "dead" OutputSection's
When findOrphanPos does the reverse search to find the OutputSection preceding the orphan's insertion point, look for a live OutputSection and ignore "dead" OutputSection's. This matches the behaviour of the forward search performed earlier in this function. Added test which without the above fix fails as a result of an orphan executable section being incorrectly placed in a non-executable segment. Differential Review: https://reviews.llvm.org/D38690 llvm-svn: 315292
1 parent 2b132eb commit 4d54a4b

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed
 

‎lld/ELF/Writer.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1031,9 +1031,11 @@ findOrphanPos(std::vector<BaseCommand *>::iterator B,
10311031
Sec->SortRank < CurSec->SortRank)
10321032
break;
10331033
}
1034-
auto J = std::find_if(
1035-
llvm::make_reverse_iterator(I), llvm::make_reverse_iterator(B),
1036-
[](BaseCommand *Cmd) { return isa<OutputSection>(Cmd); });
1034+
auto J = std::find_if(llvm::make_reverse_iterator(I),
1035+
llvm::make_reverse_iterator(B), [](BaseCommand *Cmd) {
1036+
auto *OS = dyn_cast<OutputSection>(Cmd);
1037+
return OS && OS->Live;
1038+
});
10371039
I = J.base();
10381040

10391041
// As a special case, if the orphan section is the last section, put
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
3+
# RUN: echo "PHDRS { \
4+
# RUN: exec PT_LOAD FLAGS(0x4 | 0x1); \
5+
# RUN: rw PT_LOAD FLAGS(0x4 | 0x2); \
6+
# RUN: } \
7+
# RUN: SECTIONS { \
8+
# RUN: .text : { *(.text) } :exec \
9+
# RUN: .empty : { *(.empty) } :rw \
10+
# RUN: .rw : { *(.rw) } \
11+
# RUN: }" > %t.script
12+
# RUN: ld.lld -o %t --script %t.script %t.o
13+
# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
14+
15+
## Check that the orphan section is placed correctly and belongs to
16+
## the correct segment.
17+
18+
# CHECK: Section Headers
19+
# CHECK: .text
20+
# CHECK-NEXT: .orphan
21+
# CHECK-NEXT: .rw
22+
23+
# CHECK: Segment Sections
24+
# CHECK-NEXT: .text .orphan
25+
# CHECK-NEXT: .rw
26+
27+
.section .text, "ax"
28+
ret
29+
30+
.section .rw, "aw"
31+
.quad 0
32+
33+
.section .orphan, "ax"
34+
ret

0 commit comments

Comments
 (0)
Please sign in to comment.