unsigned SectionKind : 3;
- // The next three bit fields are only used by InputSectionBase, but we
+ // The next four bit fields are only used by InputSectionBase, but we
// put them here so the struct packs better.
// The garbage collector sets sections' Live bits.
unsigned Bss : 1;
+ // True if this is a debuginfo section.
+ unsigned Debug : 1;
+
// Set for sections that should not be folded by ICF.
unsigned KeepUnique : 1;
uint64_t Entsize, uint64_t Alignment, uint32_t Type,
uint32_t Info, uint32_t Link)
: Name(Name), Repl(this), SectionKind(SectionKind), Live(false),
- Assigned(false), Bss(false), KeepUnique(false), Alignment(Alignment),
- Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) {}
+ Assigned(false), Bss(false), Debug(false), KeepUnique(false),
+ Alignment(Alignment), Flags(Flags), Entsize(Entsize), Type(Type),
+ Link(Link), Info(Info) {}
};
// This corresponds to a section of an input file.
void run();
private:
- void enqueue(InputSectionBase *Sec, uint64_t Offset);
+ void enqueue(InputSectionBase *Sec, uint64_t Offset, bool IsLSDA);
void markSymbol(Symbol *Sym);
template <class RelTy>
Offset += getAddend<ELFT>(Sec, Rel);
if (!IsLSDA || !(RelSec->Flags & SHF_EXECINSTR))
- enqueue(RelSec, Offset);
+ enqueue(RelSec, Offset, IsLSDA);
return;
}
SS->getFile().IsNeeded = true;
for (InputSectionBase *Sec : CNamedSections.lookup(Sym.getName()))
- enqueue(Sec, 0);
+ enqueue(Sec, 0, IsLSDA);
}
// The .eh_frame section is an unfortunate special case.
}
template <class ELFT>
-void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
+void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset,
+ bool IsLSDA) {
// Skip over discarded sections. This in theory shouldn't happen, because
// the ELF spec doesn't allow a relocation to point to a deduplicated
// COMDAT section directly. Unfortunately this happens in practice (e.g.
if (auto *MS = dyn_cast<MergeInputSection>(Sec))
MS->getSectionPiece(Offset)->Live = true;
+ InputSection *S = dyn_cast<InputSection>(Sec);
+ // LSDA does not count as "live code or data" in the object file.
+ // The section may already have been marked live for LSDA in which
+ // case we still need to mark the file.
+ if (S && !IsLSDA && Sec->File)
+ Sec->getFile<ELFT>()->HasLiveCodeOrData = true;
+
if (Sec->Live)
return;
- Sec->Live = true;
+ Sec->Live = true;
// Add input section to the queue.
- if (InputSection *S = dyn_cast<InputSection>(Sec))
+ if (S)
Queue.push_back(S);
}
template <class ELFT> void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
if (auto *D = dyn_cast_or_null<Defined>(Sym))
if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section))
- enqueue(IS, D->Value);
+ enqueue(IS, D->Value, false);
}
// This is the main function of the garbage collector.
continue;
if (isReserved(Sec) || Script->shouldKeep(Sec)) {
- enqueue(Sec, 0);
+ enqueue(Sec, 0, false);
} else if (isValidCIdentifier(Sec->Name)) {
CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
CNamedSections[Saver.save("__stop_" + Sec->Name)].push_back(Sec);
}
for (InputSectionBase *IS : Sec.DependentSections)
- enqueue(IS, 0);
+ enqueue(IS, 0, false);
}
}
// The -gc-sections option works only for SHF_ALLOC sections
// (sections that are memory-mapped at runtime). So we can
// unconditionally make non-SHF_ALLOC sections alive except
- // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
+ // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections and .debug sections.
//
// Usually, SHF_ALLOC sections are not removed even if they are
// unreachable through relocations because reachability is not
bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
- if (!IsAlloc && !IsLinkOrder && !IsRel)
+ if (!IsAlloc && !IsLinkOrder && !IsRel && !Sec->Debug)
Sec->Live = true;
}
// Follow the graph to mark all live sections.
MarkLive<ELFT>().run();
+ // Mark debug sections as live in any object file that has a live
+ // Regular or Merge section.
+ for (InputSectionBase *Sec : InputSections)
+ if (Sec->Debug && Sec->getFile<ELFT>()->HasLiveCodeOrData)
+ Sec->Live = true;
+
// Report garbage-collected sections.
if (Config->PrintGcSections)
for (InputSectionBase *Sec : InputSections)