Skip to content

Commit

Permalink
[ELF] Simplify SectionBase::partition handling and make it live by de…
Browse files Browse the repository at this point in the history
…fault. NFC

Previously an InputSectionBase is dead (`partition==0`) by default.
SyntheticSection calls markLive and BssSection overrides that with markDead.

It is more natural to make InputSectionBase live by default and let
--gc-sections mark InputSectionBase dead.

When linking a Release build of clang:

* --no-gc-sections:, the removed `inputSections` loop decreases markLive time from 4ms to 1ms.
* --gc-sections: the extra `inputSections` loop increases markLive time from 0.181296s to 0.188526s.
  This is as of we lose the removing one `inputSections` loop optimization (4374824).
  I believe the loss can be mitigated if we refactor markLive.
  • Loading branch information
MaskRay committed Jan 30, 2022
1 parent a8a7bf9 commit 7cd0c45
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 12 deletions.
1 change: 0 additions & 1 deletion lld/ELF/Driver.cpp
Expand Up @@ -1828,7 +1828,6 @@ static void replaceCommonSymbols() {

auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
bss->file = s->file;
bss->markDead();
inputSections.push_back(bss);
s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
/*value=*/0, s->size, bss});
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/InputSection.h
Expand Up @@ -65,7 +65,7 @@ class SectionBase {
// The 1-indexed partition that this section is assigned to by the garbage
// collector, or 0 if this section is dead. Normally there is only one
// partition, so this will either be 0 or 1.
uint8_t partition;
uint8_t partition = 1;
elf::Partition &getPartition() const;

// These corresponds to the fields in Elf_Shdr.
Expand Down Expand Up @@ -96,8 +96,8 @@ class SectionBase {
uint32_t entsize, uint32_t alignment, uint32_t type,
uint32_t info, uint32_t link)
: name(name), sectionKind(sectionKind), bss(false), keepUnique(false),
partition(0), alignment(alignment), flags(flags), entsize(entsize),
type(type), link(link), info(info) {}
alignment(alignment), flags(flags), entsize(entsize), type(type),
link(link), info(info) {}
};

// This corresponds to a section of an input file.
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/MarkLive.cpp
Expand Up @@ -371,9 +371,6 @@ template <class ELFT> void elf::markLive() {
llvm::TimeTraceScope timeScope("markLive");
// If --gc-sections is not given, retain all input sections.
if (!config->gcSections) {
for (InputSectionBase *sec : inputSections)
sec->markLive();

// If a DSO defines a symbol referenced in a regular object, it is needed.
for (Symbol *sym : symtab->symbols())
if (auto *s = dyn_cast<SharedSymbol>(sym))
Expand All @@ -382,6 +379,9 @@ template <class ELFT> void elf::markLive() {
return;
}

for (InputSectionBase *sec : inputSections)
sec->markDead();

// Follow the graph to mark all live sections.
for (unsigned curPart = 1; curPart <= partitions.size(); ++curPart)
MarkLive<ELFT>(curPart).run();
Expand Down
4 changes: 1 addition & 3 deletions lld/ELF/SyntheticSections.h
Expand Up @@ -40,9 +40,7 @@ class SyntheticSection : public InputSection {
SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment,
StringRef name)
: InputSection(nullptr, flags, type, alignment, {}, name,
InputSectionBase::Synthetic) {
markLive();
}
InputSectionBase::Synthetic) {}

virtual ~SyntheticSection() = default;
virtual void writeTo(uint8_t *buf) = 0;
Expand Down
3 changes: 1 addition & 2 deletions lld/ELF/Writer.cpp
Expand Up @@ -315,8 +315,7 @@ template <class ELFT> void elf::createSyntheticSections() {
// If there is a SECTIONS command and a .data.rel.ro section name use name
// .data.rel.ro.bss so that we match in the .data.rel.ro output section.
// This makes sure our relro is contiguous.
bool hasDataRelRo =
script->hasSectionsCommand && findSection(".data.rel.ro", 0);
bool hasDataRelRo = script->hasSectionsCommand && findSection(".data.rel.ro");
in.bssRelRo = std::make_unique<BssSection>(
hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
add(*in.bssRelRo);
Expand Down

0 comments on commit 7cd0c45

Please sign in to comment.