Skip to content

Commit 0bb825d

Browse files
committedOct 1, 2019
ELF: Add .interp synthetic sections first in createSyntheticSections().
Our .interp section is not a SyntheticSection. As a result, it terminates the loop in removeUnusedSyntheticSections(). This has at least two consequences: - The synthetic .bss and .bss.rel.ro sections are always present in dynamically linked executables, even when they are not needed. - The synthetic .ARM.exidx (and possibly other) sections are always present in partitions other than the last one, even when not needed. .ARM.exidx in particular is problematic because it assumes that its list of code sections is non-empty in getLinkOrderDep(), which can lead to a crash if the partition does not have any code sections. Fix these problems by moving the creation of the .interp sections to the top of createSyntheticSections(). While here, make the code a little less error-prone by changing the add() lambdas to take a SyntheticSection instead of an InputSectionBase. Differential Revision: https://reviews.llvm.org/D68256 llvm-svn: 373347
1 parent e536800 commit 0bb825d

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed
 

‎lld/ELF/SyntheticSections.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,8 @@ InputSection *elf::createInterpSection() {
257257
StringRef s = saver.save(config->dynamicLinker);
258258
ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
259259

260-
auto *sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents,
261-
".interp");
262-
sec->markLive();
263-
return sec;
260+
return make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents,
261+
".interp");
264262
}
265263

266264
Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,

‎lld/ELF/Writer.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,18 @@ template <class ELFT> void elf::createSyntheticSections() {
314314
// you can call lld::elf::main more than once as a library.
315315
memset(&Out::first, 0, sizeof(Out));
316316

317-
auto add = [](InputSectionBase *sec) { inputSections.push_back(sec); };
317+
// Add the .interp section first because it is not a SyntheticSection.
318+
// The removeUnusedSyntheticSections() function relies on the
319+
// SyntheticSections coming last.
320+
if (needsInterpSection()) {
321+
for (size_t i = 1; i <= partitions.size(); ++i) {
322+
InputSection *sec = createInterpSection();
323+
sec->partition = i;
324+
inputSections.push_back(sec);
325+
}
326+
}
327+
328+
auto add = [](SyntheticSection *sec) { inputSections.push_back(sec); };
318329

319330
in.shStrTab = make<StringTableSection>(".shstrtab", false);
320331

@@ -356,7 +367,7 @@ template <class ELFT> void elf::createSyntheticSections() {
356367
StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";
357368

358369
for (Partition &part : partitions) {
359-
auto add = [&](InputSectionBase *sec) {
370+
auto add = [&](SyntheticSection *sec) {
360371
sec->partition = part.getNumber();
361372
inputSections.push_back(sec);
362373
};
@@ -384,9 +395,6 @@ template <class ELFT> void elf::createSyntheticSections() {
384395
part.relaDyn =
385396
make<RelocationSection<ELFT>>(relaDynName, config->zCombreloc);
386397

387-
if (needsInterpSection())
388-
add(createInterpSection());
389-
390398
if (config->hasDynSymTab) {
391399
part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
392400
add(part.dynSymTab);

‎lld/test/ELF/Inputs/shared.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.global bar
2-
.type bar, @function
2+
.type bar, %function
33
bar:
44

55
.global bar2
6-
.type bar2, @function
6+
.type bar2, %function
77
bar2:
88

99
.global zed

‎lld/test/ELF/dynamic-linker.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
55

66
# RUN: ld.lld --dynamic-linker foo %t.o %t.so -o %t
7-
# RUN: llvm-readelf -program-headers %t | FileCheck %s
7+
# RUN: llvm-readelf --program-headers --section-headers %t | FileCheck --implicit-check-not=.bss %s
88

99
# RUN: ld.lld --dynamic-linker=foo %t.o %t.so -o %t
10-
# RUN: llvm-readelf -program-headers %t | FileCheck %s
10+
# RUN: llvm-readelf --program-headers --section-headers %t | FileCheck --implicit-check-not=.bss %s
1111

1212
# CHECK: [Requesting program interpreter: foo]
1313

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Test that we don't create a .ARM.exidx for the main partition.
2+
## Previously we were doing so, which is unnecessary and led to a crash.
3+
4+
# RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/shared.s -o %t1.o
5+
# RUN: ld.lld -shared %t1.o -o %t.so
6+
# RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
7+
8+
# RUN: ld.lld -shared --gc-sections --dynamic-linker foo %t.o %t.so -o %t
9+
# RUN: llvm-readelf --section-headers %t | FileCheck %s
10+
11+
# CHECK: .ARM.exidx
12+
# CHECK-NOT: .ARM.exidx
13+
14+
.section .llvm_sympart,"",%llvm_sympart
15+
.asciz "part1"
16+
.4byte p1
17+
18+
.section .text.p1,"ax",%progbits
19+
.globl p1
20+
p1:
21+
.fnstart
22+
bx lr
23+
.cantunwind
24+
.fnend

0 commit comments

Comments
 (0)
Please sign in to comment.