diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index cf8fd1dfc3137..cf7c1c228b433 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -878,8 +878,8 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, // to work. In a full implementation we would merge all attribute // sections. if (in.attributes == nullptr) { - in.attributes = make(*this, sec, name); - return in.attributes; + in.attributes = std::make_unique(*this, sec, name); + return in.attributes.get(); } return &InputSection::discarded; } @@ -900,8 +900,8 @@ InputSectionBase *ObjFile::createInputSection(uint32_t idx, // standard extensions to enable. In a full implementation we would merge // all attribute sections. if (in.attributes == nullptr) { - in.attributes = make(*this, sec, name); - return in.attributes; + in.attributes = std::make_unique(*this, sec, name); + return in.attributes.get(); } return &InputSection::discarded; } diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 999bb94d6416c..a362dd3809d09 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -561,7 +561,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd, } void LinkerScript::discard(InputSectionBase &s) { - if (&s == in.shStrTab || &s == mainPart->relrDyn) + if (&s == in.shStrTab.get() || &s == mainPart->relrDyn) error("discarding " + s.name + " section is not allowed"); // You can discard .hash and .gnu.hash sections by linker scripts. Since diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index c4438be0cb593..d6f7d0a935c8e 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -870,7 +870,7 @@ static void addGotEntry(Symbol &sym) { // If preemptible, emit a GLOB_DAT relocation. if (sym.isPreemptible) { - mainPart->relaDyn->addReloc({target->gotRel, in.got, off, + mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off, DynamicReloc::AgainstSymbol, sym, 0, R_ABS}); return; } @@ -1205,8 +1205,8 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c, in.got->relocations.push_back( {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym}); else - mainPart->relaDyn->addReloc( - {target->tlsModuleIndexRel, in.got, in.got->getTlsIndexOff()}); + mainPart->relaDyn->addReloc({target->tlsModuleIndexRel, in.got.get(), + in.got->getTlsIndexOff()}); } c.relocations.push_back({expr, type, offset, addend, &sym}); return 1; @@ -1592,7 +1592,7 @@ static bool handleNonPreemptibleIfunc(Symbol &sym) { if (sym.hasDirectReloc) { // Change the value to the IPLT and redirect all references to it. auto &d = cast(sym); - d.section = in.iplt; + d.section = in.iplt.get(); d.value = sym.pltIndex * target->ipltEntrySize; d.size = 0; // It's important to set the symbol type here so that dynamic loaders diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index def06da2bcbc9..047546e28ed0b 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1636,11 +1636,11 @@ void RelocationBaseSection::finalizeContents() { else getParent()->link = 0; - if (in.relaPlt == this && in.gotPlt->getParent()) { + if (in.relaPlt.get() == this && in.gotPlt->getParent()) { getParent()->flags |= ELF::SHF_INFO_LINK; getParent()->info = in.gotPlt->getParent()->sectionIndex; } - if (in.relaIplt == this && in.igotPlt->getParent()) { + if (in.relaIplt.get() == this && in.igotPlt->getParent()) { getParent()->flags |= ELF::SHF_INFO_LINK; getParent()->info = in.igotPlt->getParent()->sectionIndex; } @@ -3801,8 +3801,9 @@ void PartitionIndexSection::writeTo(uint8_t *buf) { write32(buf, mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va); write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4)); - SyntheticSection *next = - i == partitions.size() - 1 ? in.partEnd : partitions[i + 1].elfHeader; + SyntheticSection *next = i == partitions.size() - 1 + ? in.partEnd.get() + : partitions[i + 1].elfHeader; write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA()); va += 12; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index e49f2e3245360..15b5657f80372 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1235,27 +1235,27 @@ inline Partition &SectionBase::getPartition() const { // Linker generated sections which can be used as inputs and are not specific to // a partition. struct InStruct { - InputSection *attributes; - BssSection *bss; - BssSection *bssRelRo; - GotSection *got; - GotPltSection *gotPlt; - IgotPltSection *igotPlt; - PPC64LongBranchTargetSection *ppc64LongBranchTarget; - MipsGotSection *mipsGot; - MipsRldMapSection *mipsRldMap; - SyntheticSection *partEnd; - SyntheticSection *partIndex; - PltSection *plt; - IpltSection *iplt; - PPC32Got2Section *ppc32Got2; - IBTPltSection *ibtPlt; - RelocationBaseSection *relaPlt; - RelocationBaseSection *relaIplt; - StringTableSection *shStrTab; - StringTableSection *strTab; - SymbolTableBaseSection *symTab; - SymtabShndxSection *symTabShndx; + std::unique_ptr attributes; + std::unique_ptr bss; + std::unique_ptr bssRelRo; + std::unique_ptr got; + std::unique_ptr gotPlt; + std::unique_ptr igotPlt; + std::unique_ptr ppc64LongBranchTarget; + std::unique_ptr mipsGot; + std::unique_ptr mipsRldMap; + std::unique_ptr partEnd; + std::unique_ptr partIndex; + std::unique_ptr plt; + std::unique_ptr iplt; + std::unique_ptr ppc32Got2; + std::unique_ptr ibtPlt; + std::unique_ptr relaPlt; + std::unique_ptr relaIplt; + std::unique_ptr shStrTab; + std::unique_ptr strTab; + std::unique_ptr symTab; + std::unique_ptr symTabShndx; }; extern InStruct in; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index bce524811b84d..15cb7603524e8 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -299,18 +299,18 @@ template void elf::createSyntheticSections() { auto add = [](SyntheticSection &sec) { inputSections.push_back(&sec); }; - in.shStrTab = make(".shstrtab", false); + in.shStrTab = std::make_unique(".shstrtab", false); Out::programHeaders = make("", 0, SHF_ALLOC); Out::programHeaders->alignment = config->wordsize; if (config->strip != StripPolicy::All) { - in.strTab = make(".strtab", false); - in.symTab = make>(*in.strTab); - in.symTabShndx = make(); + in.strTab = std::make_unique(".strtab", false); + in.symTab = std::make_unique>(*in.strTab); + in.symTabShndx = std::make_unique(); } - in.bss = make(".bss", 0, 1); + in.bss = std::make_unique(".bss", 0, 1); add(*in.bss); // If there is a SECTIONS command and a .data.rel.ro section name use name @@ -318,14 +318,14 @@ template void elf::createSyntheticSections() { // This makes sure our relro is contiguous. bool hasDataRelRo = script->hasSectionsCommand && findSection(".data.rel.ro", 0); - in.bssRelRo = - make(hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1); + in.bssRelRo = std::make_unique( + hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1); add(*in.bssRelRo); // Add MIPS-specific sections. if (config->emachine == EM_MIPS) { if (!config->shared && config->hasDynSymTab) { - in.mipsRldMap = make(); + in.mipsRldMap = std::make_unique(); add(*in.mipsRldMap); } if (auto *sec = MipsAbiFlagsSection::create()) @@ -422,13 +422,14 @@ template void elf::createSyntheticSections() { // Create the partition end marker. This needs to be in partition number 255 // so that it is sorted after all other partitions. It also has other // special handling (see createPhdrs() and combineEhSections()). - in.partEnd = make(".part.end", config->maxPageSize, 1); + in.partEnd = + std::make_unique(".part.end", config->maxPageSize, 1); in.partEnd->partition = 255; add(*in.partEnd); - in.partIndex = make(); - addOptionalRegular("__part_index_begin", in.partIndex, 0); - addOptionalRegular("__part_index_end", in.partIndex, + in.partIndex = std::make_unique(); + addOptionalRegular("__part_index_begin", in.partIndex.get(), 0); + addOptionalRegular("__part_index_end", in.partIndex.get(), in.partIndex->getSize()); add(*in.partIndex); } @@ -436,26 +437,26 @@ template void elf::createSyntheticSections() { // Add .got. MIPS' .got is so different from the other archs, // it has its own class. if (config->emachine == EM_MIPS) { - in.mipsGot = make(); + in.mipsGot = std::make_unique(); add(*in.mipsGot); } else { - in.got = make(); + in.got = std::make_unique(); add(*in.got); } if (config->emachine == EM_PPC) { - in.ppc32Got2 = make(); + in.ppc32Got2 = std::make_unique(); add(*in.ppc32Got2); } if (config->emachine == EM_PPC64) { - in.ppc64LongBranchTarget = make(); + in.ppc64LongBranchTarget = std::make_unique(); add(*in.ppc64LongBranchTarget); } - in.gotPlt = make(); + in.gotPlt = std::make_unique(); add(*in.gotPlt); - in.igotPlt = make(); + in.igotPlt = std::make_unique(); add(*in.igotPlt); // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat @@ -472,7 +473,7 @@ template void elf::createSyntheticSections() { // We always need to add rel[a].plt to output if it has entries. // Even for static linking it can contain R_[*]_IRELATIVE relocations. - in.relaPlt = make>( + in.relaPlt = std::make_unique>( config->isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false); add(*in.relaPlt); @@ -482,21 +483,23 @@ template void elf::createSyntheticSections() { // that would cause a section type mismatch. However, because the Android // dynamic loader reads .rel.plt after .rel.dyn, we can get the desired // behaviour by placing the iplt section in .rel.plt. - in.relaIplt = make>( + in.relaIplt = std::make_unique>( config->androidPackDynRelocs ? in.relaPlt->name : relaDynName, /*sort=*/false); add(*in.relaIplt); if ((config->emachine == EM_386 || config->emachine == EM_X86_64) && (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) { - in.ibtPlt = make(); + in.ibtPlt = std::make_unique(); add(*in.ibtPlt); } - in.plt = config->emachine == EM_PPC ? make() - : make(); + if (config->emachine == EM_PPC) + in.plt = std::make_unique(); + else + in.plt = std::make_unique(); add(*in.plt); - in.iplt = make(); + in.iplt = std::make_unique(); add(*in.iplt); if (config->andFeatures) @@ -1066,17 +1069,17 @@ template void Writer::setReservedSymbolSections() { if (ElfSym::globalOffsetTable) { // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually // to the start of the .got or .got.plt section. - InputSection *gotSection = in.gotPlt; + InputSection *sec = in.gotPlt.get(); if (!target->gotBaseSymInGotPlt) - gotSection = in.mipsGot ? cast(in.mipsGot) - : cast(in.got); - ElfSym::globalOffsetTable->section = gotSection; + sec = in.mipsGot.get() ? cast(in.mipsGot.get()) + : cast(in.got.get()); + ElfSym::globalOffsetTable->section = sec; } // .rela_iplt_{start,end} mark the start and the end of in.relaIplt. if (ElfSym::relaIpltStart && in.relaIplt->isNeeded()) { - ElfSym::relaIpltStart->section = in.relaIplt; - ElfSym::relaIpltEnd->section = in.relaIplt; + ElfSym::relaIpltStart->section = in.relaIplt.get(); + ElfSym::relaIpltEnd->section = in.relaIplt.get(); ElfSym::relaIpltEnd->value = in.relaIplt->getSize(); } @@ -1644,6 +1647,9 @@ static void finalizeSynthetic(SyntheticSection *sec) { sec->finalizeContents(); } } +template static void finalizeSynthetic(std::unique_ptr &sec) { + finalizeSynthetic(sec.get()); +} // We need to generate and finalize the content that depends on the address of // InputSections. As the generation of the content may also alter InputSection