diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 1b5446c3dc81..e788f215229e 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -848,7 +848,7 @@ template void LinkerDriver::link(opt::InputArgList &Args) { if (S && S != &InputSection::Discarded) Symtab.Sections.push_back(S); for (BinaryFile *F : Symtab.getBinaryFiles()) - for (InputSectionData *S : F->getSections()) + for (InputSectionBase *S : F->getSections()) Symtab.Sections.push_back(cast>(S)); // Do size optimizations: garbage collection and identical code folding. diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 78bc2cc0efdb..12e3c1e5e64b 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -322,10 +322,10 @@ public: explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {} static bool classof(const InputFile *F) { return F->kind() == BinaryKind; } template void parse(); - ArrayRef getSections() const { return Sections; } + ArrayRef getSections() const { return Sections; } private: - std::vector Sections; + std::vector Sections; }; InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index d3a2213d2411..09443e98e9f4 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -53,10 +53,10 @@ InputSectionBase::InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Link, uint32_t Info, uint64_t Addralign, ArrayRef Data, StringRef Name, Kind SectionKind) - : InputSectionData(SectionKind, Name, Data, - !Config->GcSections || !(Flags & SHF_ALLOC)), - File(File), Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), - Info(Info), Repl(this) { + : File(File), Data(Data), Name(Name), SectionKind(SectionKind), + Live(!Config->GcSections || !(Flags & SHF_ALLOC)), Assigned(false), + Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info), + Repl(this) { NumRelocations = 0; AreRelocsRela = false; @@ -198,7 +198,7 @@ InputSection::InputSection(elf::ObjectFile *F, : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {} template -bool InputSection::classof(const InputSectionData *S) { +bool InputSection::classof(const InputSectionBase *S) { return S->kind() == InputSectionBase::Regular || S->kind() == InputSectionBase::Synthetic; } @@ -583,7 +583,7 @@ EhInputSection::EhInputSection(elf::ObjectFile *F, } template -bool EhInputSection::classof(const InputSectionData *S) { +bool EhInputSection::classof(const InputSectionBase *S) { return S->kind() == InputSectionBase::EHFrame; } @@ -711,7 +711,7 @@ template void MergeInputSection::splitIntoPieces() { } template -bool MergeInputSection::classof(const InputSectionData *S) { +bool MergeInputSection::classof(const InputSectionBase *S) { return S->kind() == InputSectionBase::Merge; } diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 0846d64f9e41..9e5b09070cf0 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -33,50 +33,28 @@ template class ObjectFile; template class OutputSection; class OutputSectionBase; -// We need non-template input section class to store symbol layout -// in linker script parser structures, where we do not have ELFT -// template parameter. For each scripted output section symbol we -// store pointer to preceding InputSectionData object or nullptr, -// if symbol should be placed at the very beginning of the output -// section -class InputSectionData { +// This corresponds to a section of an input file. +class InputSectionBase { public: enum Kind { Regular, EHFrame, Merge, Synthetic, }; - // The garbage collector sets sections' Live bits. - // If GC is disabled, all sections are considered live by default. - InputSectionData(Kind SectionKind, StringRef Name, ArrayRef Data, - bool Live) - : SectionKind(SectionKind), Live(Live), Assigned(false), Name(Name), - Data(Data) {} - -private: - unsigned SectionKind : 3; - -public: Kind kind() const { return (Kind)SectionKind; } - - unsigned Live : 1; // for garbage collection - unsigned Assigned : 1; // for linker script - uint32_t Alignment; - StringRef Name; - ArrayRef Data; - - template llvm::ArrayRef getDataAs() const { - size_t S = Data.size(); - assert(S % sizeof(T) == 0); - return llvm::makeArrayRef((const T *)Data.data(), S / sizeof(T)); - } - - std::vector Relocations; -}; - -// This corresponds to a section of an input file. -class InputSectionBase : public InputSectionData { -public: // The file this section is from. InputFile *File; + ArrayRef Data; + + StringRef Name; + + unsigned SectionKind : 3; + + // The garbage collector sets sections' Live bits. + // If GC is disabled, all sections are considered live by default. + unsigned Live : 1; // for garbage collection + unsigned Assigned : 1; // for linker script + + uint32_t Alignment; + // These corresponds to the fields in Elf_Shdr. uint64_t Flags; uint64_t Offset = 0; @@ -86,7 +64,7 @@ public: uint32_t Info; InputSectionBase() - : InputSectionData(Regular, "", ArrayRef(), false), Repl(this) { + : SectionKind(Regular), Live(false), Assigned(false), Repl(this) { NumRelocations = 0; AreRelocsRela = false; } @@ -153,6 +131,14 @@ public: template std::string getLocation(uint64_t Offset); template void relocate(uint8_t *Buf, uint8_t *BufEnd); + + std::vector Relocations; + + template llvm::ArrayRef getDataAs() const { + size_t S = Data.size(); + assert(S % sizeof(T) == 0); + return llvm::makeArrayRef((const T *)Data.data(), S / sizeof(T)); + } }; // SectionPiece represents a piece of splittable section contents. @@ -179,7 +165,7 @@ template class MergeInputSection : public InputSectionBase { public: MergeInputSection(ObjectFile *F, const Elf_Shdr *Header, StringRef Name); - static bool classof(const InputSectionData *S); + static bool classof(const InputSectionBase *S); void splitIntoPieces(); // Mark the piece at a given offset live. Used by GC. @@ -233,11 +219,11 @@ private: }; struct EhSectionPiece : public SectionPiece { - EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size, + EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size, unsigned FirstRelocation) : SectionPiece(Off, false), ID(ID), Size(Size), FirstRelocation(FirstRelocation) {} - InputSectionData *ID; + InputSectionBase *ID; uint32_t Size; uint32_t size() const { return Size; } @@ -251,7 +237,7 @@ public: typedef typename ELFT::Shdr Elf_Shdr; typedef typename ELFT::uint uintX_t; EhInputSection(ObjectFile *F, const Elf_Shdr *Header, StringRef Name); - static bool classof(const InputSectionData *S); + static bool classof(const InputSectionBase *S); void split(); template void split(ArrayRef Rels); @@ -267,13 +253,11 @@ template class InputSection : public InputSectionBase { typedef typename ELFT::Rel Elf_Rel; typedef typename ELFT::Sym Elf_Sym; typedef typename ELFT::uint uintX_t; - typedef InputSectionData::Kind Kind; public: InputSection(); InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign, - ArrayRef Data, StringRef Name, - Kind K = InputSectionData::Regular); + ArrayRef Data, StringRef Name, Kind K = Regular); InputSection(ObjectFile *F, const Elf_Shdr *Header, StringRef Name); static InputSection Discarded; @@ -286,7 +270,7 @@ public: // to. The writer sets a value. uint64_t OutSecOff = 0; - static bool classof(const InputSectionData *S); + static bool classof(const InputSectionBase *S); InputSectionBase *getRelocatedSection(); diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index fa603828c54f..143324dc1dc8 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -196,22 +196,22 @@ template bool LinkerScript::shouldKeep(InputSectionBase *S) { return false; } -static bool comparePriority(InputSectionData *A, InputSectionData *B) { +static bool comparePriority(InputSectionBase *A, InputSectionBase *B) { return getPriority(A->Name) < getPriority(B->Name); } -static bool compareName(InputSectionData *A, InputSectionData *B) { +static bool compareName(InputSectionBase *A, InputSectionBase *B) { return A->Name < B->Name; } -static bool compareAlignment(InputSectionData *A, InputSectionData *B) { +static bool compareAlignment(InputSectionBase *A, InputSectionBase *B) { // ">" is not a mistake. Larger alignments are placed before smaller // alignments in order to reduce the amount of padding necessary. // This is compatible with GNU. return A->Alignment > B->Alignment; } -static std::function +static std::function getComparator(SortSectionPolicy K) { switch (K) { case SortSectionPolicy::Alignment: @@ -230,7 +230,7 @@ static bool matchConstraints(ArrayRef Sections, ConstraintKind Kind) { if (Kind == ConstraintKind::NoConstraint) return true; - bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) { + bool IsRW = llvm::any_of(Sections, [=](InputSectionBase *Sec2) { auto *Sec = static_cast(Sec2); return Sec->Flags & SHF_WRITE; }); @@ -238,7 +238,7 @@ static bool matchConstraints(ArrayRef Sections, (!IsRW && Kind == ConstraintKind::ReadOnly); } -static void sortSections(InputSectionData **Begin, InputSectionData **End, +static void sortSections(InputSectionBase **Begin, InputSectionBase **End, SortSectionPolicy K) { if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) std::stable_sort(Begin, End, getComparator(K)); @@ -281,8 +281,8 @@ void LinkerScript::computeInputSections(InputSectionDescription *I) { // --sort-section is handled as an inner SORT command. // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. // 4. If no SORT command is given, sort according to --sort-section. - InputSectionData **Begin = I->Sections.data() + SizeBefore; - InputSectionData **End = I->Sections.data() + I->Sections.size(); + InputSectionBase **Begin = I->Sections.data() + SizeBefore; + InputSectionBase **End = I->Sections.data() + I->Sections.size(); if (Pat.SortOuter != SortSectionPolicy::None) { if (Pat.SortInner == SortSectionPolicy::Default) sortSections(Begin, End, Config->SortSection); @@ -313,7 +313,7 @@ LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) { if (!Cmd) continue; computeInputSections(Cmd); - for (InputSectionData *S : Cmd->Sections) + for (InputSectionBase *S : Cmd->Sections) Ret.push_back(static_cast(S)); } @@ -492,7 +492,7 @@ template void LinkerScript::process(BaseCommand &Base) { // calculates and assigns the offsets for each section and also // updates the output section size. auto &ICmd = cast(Base); - for (InputSectionData *ID : ICmd.Sections) { + for (InputSectionBase *ID : ICmd.Sections) { // We tentatively added all synthetic sections at the beginning and removed // empty ones afterwards (because there is no way to know whether they were // going be empty or not other than actually running linker scripts.) diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 3735355a8dc2..6893ad628bf6 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -35,7 +35,7 @@ class InputSectionBase; template class InputSection; class OutputSectionBase; template class OutputSectionFactory; -class InputSectionData; +class InputSectionBase; // This represents an expression in the linker script. // ScriptParser::readExpr reads an expression and returns an Expr. @@ -159,7 +159,7 @@ struct InputSectionDescription : BaseCommand { // will be associated with this InputSectionDescription. std::vector SectionPatterns; - std::vector Sections; + std::vector Sections; }; // Represents an ASSERT(). @@ -307,7 +307,7 @@ private: void output(InputSection *Sec); void process(BaseCommand &Base); llvm::DenseSet AlreadyOutputOS; - llvm::DenseSet AlreadyOutputIS; + llvm::DenseSet AlreadyOutputIS; }; // Variable template is a C++14 feature, so we can't template diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp index c341049fb68b..9f128f66f24e 100644 --- a/lld/ELF/MapFile.cpp +++ b/lld/ELF/MapFile.cpp @@ -107,7 +107,7 @@ static void writeMapFile2(raw_fd_ostream &OS, OS << '\n'; StringRef PrevName = ""; - Sec->forEachInputSection([&](InputSectionData *S) { + Sec->forEachInputSection([&](InputSectionBase *S) { if (const auto *IS = dyn_cast>(S)) writeInputSection(OS, IS, PrevName); }); diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 3b3cb806cd44..c563e9358960 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -89,8 +89,8 @@ template static bool compareByFilePosition(InputSection *A, InputSection *B) { // Synthetic doesn't have link order dependecy, stable_sort will keep it last - if (A->kind() == InputSectionData::Synthetic || - B->kind() == InputSectionData::Synthetic) + if (A->kind() == InputSectionBase::Synthetic || + B->kind() == InputSectionBase::Synthetic) return false; auto *LA = cast>(A->template getLinkOrderDep()); auto *LB = cast>(B->template getLinkOrderDep()); @@ -131,7 +131,7 @@ template void OutputSection::finalize() { } template -void OutputSection::addSection(InputSectionData *C) { +void OutputSection::addSection(InputSectionBase *C) { assert(C->Live); auto *S = cast>(C); Sections.push_back(S); @@ -145,7 +145,7 @@ void OutputSection::addSection(InputSectionData *C) { template void OutputSection::forEachInputSection( - std::function F) { + std::function F) { for (InputSection *S : Sections) F(S); } @@ -163,7 +163,7 @@ template void OutputSection::assignOffsets() { } template -void OutputSection::sort(std::function Order) { +void OutputSection::sort(std::function Order) { typedef std::pair *> Pair; auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; @@ -184,7 +184,7 @@ void OutputSection::sort(std::function Order) { // For more detail, read the section of the GCC's manual about init_priority. template void OutputSection::sortInitFini() { // Sort sections by priority. - sort([](InputSectionData *S) { return getPriority(S->Name); }); + sort([](InputSectionBase *S) { return getPriority(S->Name); }); } // Returns true if S matches /Filename.?\.o$/. @@ -277,7 +277,7 @@ EhOutputSection::EhOutputSection() template void EhOutputSection::forEachInputSection( - std::function F) { + std::function F) { for (EhInputSection *S : Sections) F(S); } @@ -366,7 +366,7 @@ void EhOutputSection::addSectionAux(EhInputSection *Sec, } template -void EhOutputSection::addSection(InputSectionData *C) { +void EhOutputSection::addSection(InputSectionBase *C) { auto *Sec = cast>(C); Sec->OutSec = this; this->updateAlignment(Sec->Alignment); diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index f2198b410615..a3064418ecb5 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -53,7 +53,7 @@ public: template void writeHeaderTo(typename ELFT::Shdr *SHdr); StringRef getName() const { return Name; } - virtual void addSection(InputSectionData *C) {} + virtual void addSection(InputSectionBase *C) {} virtual Kind getKind() const { return Base; } static bool classof(const OutputSectionBase *B) { return B->getKind() == Base; @@ -81,7 +81,7 @@ public: OutputSectionBase *FirstInPtLoad = nullptr; virtual void finalize() {} - virtual void forEachInputSection(std::function F) {} + virtual void forEachInputSection(std::function F) {} virtual void assignOffsets() {} virtual void writeTo(uint8_t *Buf) {} virtual ~OutputSectionBase() = default; @@ -111,13 +111,13 @@ public: typedef typename ELFT::Rela Elf_Rela; typedef typename ELFT::uint uintX_t; OutputSection(StringRef Name, uint32_t Type, uintX_t Flags); - void addSection(InputSectionData *C) override; - void sort(std::function Order); + void addSection(InputSectionBase *C) override; + void sort(std::function Order); void sortInitFini(); void sortCtorsDtors(); void writeTo(uint8_t *Buf) override; void finalize() override; - void forEachInputSection(std::function F) override; + void forEachInputSection(std::function F) override; void assignOffsets() override; Kind getKind() const override { return Regular; } static bool classof(const OutputSectionBase *B) { @@ -146,9 +146,9 @@ public: void writeTo(uint8_t *Buf) override; void finalize() override; bool empty() const { return Sections.empty(); } - void forEachInputSection(std::function F) override; + void forEachInputSection(std::function F) override; - void addSection(InputSectionData *S) override; + void addSection(InputSectionBase *S) override; Kind getKind() const override { return EHFrame; } static bool classof(const OutputSectionBase *B) { return B->getKind() == EHFrame; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 7c6425f3ab48..f8d5dce4d5a6 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -40,7 +40,7 @@ public: SyntheticSection(uintX_t Flags, uint32_t Type, uintX_t Addralign, StringRef Name) : InputSection(Flags, Type, Addralign, {}, Name, - InputSectionData::Synthetic) { + InputSectionBase::Synthetic) { this->Live = true; } @@ -54,8 +54,8 @@ public: return this->OutSec ? this->OutSec->Addr + this->OutSecOff : 0; } - static bool classof(const InputSectionData *D) { - return D->kind() == InputSectionData::Synthetic; + static bool classof(const InputSectionBase *D) { + return D->kind() == InputSectionBase::Synthetic; } }; diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index 7d2433072f46..08ef75d16658 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -60,7 +60,7 @@ static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); } static void or32be(uint8_t *P, int32_t V) { write32be(P, read32be(P) | V); } template static std::string getErrorLoc(uint8_t *Loc) { - for (InputSectionData *D : Symtab::X->Sections) { + for (InputSectionBase *D : Symtab::X->Sections) { auto *IS = dyn_cast_or_null>(D); if (!IS || !IS->OutSec) continue; @@ -226,8 +226,8 @@ public: void writePltHeader(uint8_t *Buf) const override; void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, int32_t Index, unsigned RelOff) const override; - void addPltSymbols(InputSectionData *IS, uint64_t Off) const override; - void addPltHeaderSymbols(InputSectionData *ISD) const override; + void addPltSymbols(InputSectionBase *IS, uint64_t Off) const override; + void addPltHeaderSymbols(InputSectionBase *ISD) const override; bool needsThunk(RelExpr Expr, uint32_t RelocType, const InputFile *File, const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; @@ -1758,7 +1758,7 @@ void ARMTargetInfo::writePltHeader(uint8_t *Buf) const { write32le(Buf + 16, GotPlt - L1 - 8); } -void ARMTargetInfo::addPltHeaderSymbols(InputSectionData *ISD) const { +void ARMTargetInfo::addPltHeaderSymbols(InputSectionBase *ISD) const { auto *IS = cast>(ISD); addSyntheticLocal("$a", STT_NOTYPE, 0, 0, IS); addSyntheticLocal("$d", STT_NOTYPE, 16, 0, IS); @@ -1781,7 +1781,7 @@ void ARMTargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, write32le(Buf + 12, GotEntryAddr - L1 - 8); } -void ARMTargetInfo::addPltSymbols(InputSectionData *ISD, uint64_t Off) const { +void ARMTargetInfo::addPltSymbols(InputSectionBase *ISD, uint64_t Off) const { auto *IS = cast>(ISD); addSyntheticLocal("$a", STT_NOTYPE, Off, 0, IS); addSyntheticLocal("$d", STT_NOTYPE, Off + 12, 0, IS); diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index c5f5d61e02f0..02a2384fb8a7 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -41,8 +41,8 @@ public: virtual void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, int32_t Index, unsigned RelOff) const {} - virtual void addPltHeaderSymbols(InputSectionData *IS) const {} - virtual void addPltSymbols(InputSectionData *IS, uint64_t Off) const {} + virtual void addPltHeaderSymbols(InputSectionBase *IS) const {} + virtual void addPltSymbols(InputSectionBase *IS, uint64_t Off) const {} // Returns true if a relocation only uses the low bits of a value such that // all those bits are in in the same page. For example, if the relocation // only uses the low 12 bits in a system with 4k pages. If this is true, the diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index f4568ebf26e3..51253f14f551 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -526,8 +526,8 @@ template void Writer::addSectionSymbols() { // Create one STT_SECTION symbol for each output section we might // have a relocation with. for (OutputSectionBase *Sec : OutputSections) { - InputSectionData *First = nullptr; - Sec->forEachInputSection([&](InputSectionData *D) { + InputSectionBase *First = nullptr; + Sec->forEachInputSection([&](InputSectionBase *D) { if (!First) First = D; }); @@ -885,7 +885,7 @@ static void sortBySymbolsOrder(ArrayRef OutputSections) { SymbolOrder.insert({S, Priority++}); // Build a map from sections to their priorities. - DenseMap SectionOrder; + DenseMap SectionOrder; for (elf::ObjectFile *File : Symtab::X->getObjectFiles()) { for (SymbolBody *Body : File->getSymbols()) { auto *D = dyn_cast>(Body); @@ -899,7 +899,7 @@ static void sortBySymbolsOrder(ArrayRef OutputSections) { // Sort sections by priority. for (OutputSectionBase *Base : OutputSections) if (auto *Sec = dyn_cast>(Base)) - Sec->sort([&](InputSectionData *S) { return SectionOrder.lookup(S); }); + Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); }); } template