[lld] db157d2 - [lld-macho] Follow-up to D77893
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Sat May 9 20:58:16 PDT 2020
Author: Jez Ng
Date: 2020-05-09T20:56:22-07:00
New Revision: db157d27337fcd13017051d125bf6e1439138da1
URL: https://github.com/llvm/llvm-project/commit/db157d27337fcd13017051d125bf6e1439138da1
DIFF: https://github.com/llvm/llvm-project/commit/db157d27337fcd13017051d125bf6e1439138da1.diff
LOG: [lld-macho] Follow-up to D77893
Summary:
1. Don't have isHidden() depend on isNeeded(). Whether a section is
hidden is orthogonal from whether it is needed: hidden sections will
never have a header regardless of whether they have a body. (I know we
override this method with return false for synthetic sections, but
regardless I think it's confusing to write it this way for non-synthetic
sections.)
2. Don't call writeTo() on unneeded sections. D78270 assumes that this
is true when implementing the stub helper section.
3. Filter out the unneeded sections early on to avoid having to deal
with them in multiple places.
4. Remove assumption in test that the referenced file has no other symbols.
(We should create separate input files for future tests to avoid such
issues.)
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D79460
Added:
Modified:
lld/MachO/OutputSection.h
lld/MachO/OutputSegment.cpp
lld/MachO/OutputSegment.h
lld/MachO/Writer.cpp
lld/test/MachO/section-merge.s
Removed:
################################################################################
diff --git a/lld/MachO/OutputSection.h b/lld/MachO/OutputSection.h
index 90f8a841a7f2..08dc41228dcd 100644
--- a/lld/MachO/OutputSection.h
+++ b/lld/MachO/OutputSection.h
@@ -36,8 +36,8 @@ class OutputSection {
// as-is so their file size is the same as their address space size.
virtual uint64_t getFileSize() const { return getSize(); }
- // Hidden sections omit header content, but body content is still present.
- virtual bool isHidden() const { return !this->isNeeded(); }
+ // Hidden sections omit header content, but body content may still be present.
+ virtual bool isHidden() const { return false; }
// Unneeded sections are omitted entirely (header and body).
virtual bool isNeeded() const { return true; }
diff --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp
index 1512e3f302f8..484d0b9da3ec 100644
--- a/lld/MachO/OutputSegment.cpp
+++ b/lld/MachO/OutputSegment.cpp
@@ -40,7 +40,7 @@ size_t OutputSegment::numNonHiddenSections() const {
size_t count = 0;
for (const OutputSegment::SectionMapEntry &i : sections) {
OutputSection *os = i.second;
- count += (os->isHidden() ? 0 : 1);
+ count += (!os->isHidden() ? 1 : 0);
}
return count;
}
@@ -70,6 +70,12 @@ void OutputSegment::sortOutputSections(OutputSegmentComparator *comparator) {
llvm::stable_sort(sections, *comparator->sectionComparator(this));
}
+void OutputSegment::removeUnneededSections() {
+ sections.remove_if([](const std::pair<StringRef, OutputSection *> &p) {
+ return !p.second->isNeeded();
+ });
+}
+
OutputSegmentComparator::OutputSegmentComparator() {
// This defines the order of segments and the sections within each segment.
// Segments that are not mentioned here will end up at defaultPosition;
@@ -138,9 +144,8 @@ void macho::sortOutputSegmentsAndSections() {
seg->sortOutputSections(&comparator);
for (auto &p : seg->getSections()) {
OutputSection *section = p.second;
- if (!section->isHidden()) {
+ if (!section->isHidden())
section->index = ++sectionIndex;
- }
}
}
}
diff --git a/lld/MachO/OutputSegment.h b/lld/MachO/OutputSegment.h
index 3a801430245c..332f38d2c259 100644
--- a/lld/MachO/OutputSegment.h
+++ b/lld/MachO/OutputSegment.h
@@ -51,6 +51,7 @@ class OutputSegment {
OutputSection *getOrCreateOutputSection(StringRef name);
void addOutputSection(OutputSection *os);
void sortOutputSections(OutputSegmentComparator *comparator);
+ void removeUnneededSections();
const SectionMap &getSections() const { return sections; }
size_t numNonHiddenSections() const;
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 80915528e11d..19a69c3bb734 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -114,7 +114,7 @@ class LCSegment : public LoadCommand {
c->maxprot = seg->maxProt;
c->initprot = seg->initProt;
- if (!seg->isNeeded())
+ if (seg->getSections().empty())
return;
c->vmaddr = seg->firstSection()->addr;
@@ -126,6 +126,7 @@ class LCSegment : public LoadCommand {
StringRef s = p.first;
OutputSection *section = p.second;
c->filesize += section->getFileSize();
+
if (section->isHidden())
continue;
@@ -283,10 +284,8 @@ void Writer::createLoadCommands() {
uint8_t segIndex = 0;
for (OutputSegment *seg : outputSegments) {
- if (seg->isNeeded()) {
- headerSection->addLoadCommand(make<LCSegment>(seg->name, seg));
- seg->index = segIndex++;
- }
+ headerSection->addLoadCommand(make<LCSegment>(seg->name, seg));
+ seg->index = segIndex++;
}
uint64_t dylibOrdinal = 1;
@@ -327,6 +326,17 @@ void Writer::createOutputSections() {
->getOrCreateOutputSection(isec->name)
->mergeInput(isec);
}
+
+ // Remove unneeded segments and sections.
+ // TODO: Avoid creating unneeded segments in the first place
+ for (auto it = outputSegments.begin(); it != outputSegments.end();) {
+ OutputSegment *seg = *it;
+ seg->removeUnneededSections();
+ if (!seg->isNeeded())
+ it = outputSegments.erase(it);
+ else
+ ++it;
+ }
}
void Writer::assignAddresses(OutputSegment *seg) {
diff --git a/lld/test/MachO/section-merge.s b/lld/test/MachO/section-merge.s
index bd3563718aa9..33e1eddd3044 100644
--- a/lld/test/MachO/section-merge.s
+++ b/lld/test/MachO/section-merge.s
@@ -23,9 +23,9 @@
# DATA: {{0*}}[[#%x,BASE:]] <_some_function>:
# DATA-NEXT: [[#BASE]]: 48 c7 c0 01 00 00 00 movq $1, %rax
# DATA-NEXT: [[#BASE + 0x7]]: c3 retq
-# DATA: {{0*}}[[#BASE + 0x8]] <_main>:
-# DATA-NEXT: [[#BASE + 0x8]]: 48 c7 c0 00 00 00 00 movq $0, %rax
-# DATA-NEXT: [[#BASE + 0xf]]: c3 retq
+# DATA: {{0*}}[[#%x,MAIN:]] <_main>:
+# DATA-NEXT: [[#MAIN]]: 48 c7 c0 00 00 00 00 movq $0, %rax
+# DATA-NEXT: [[#MAIN + 0x7]]: c3 retq
.section __TEXT,__text
.global _main
More information about the llvm-commits
mailing list