diff options
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 6 | ||||
-rw-r--r-- | lld/ELF/LinkerScript.h | 3 | ||||
-rw-r--r-- | lld/ELF/Writer.cpp | 37 |
3 files changed, 29 insertions, 17 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 9dd7ba52be1..235fa254bec 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -958,7 +958,8 @@ void LinkerScript::assignAddresses( } // Creates program headers as instructed by PHDRS linker script command. -std::vector<PhdrEntry> LinkerScript::createPhdrs() { +std::vector<PhdrEntry> LinkerScript::createPhdrs( + ArrayRef<OutputSectionCommand *> OutputSectionCommands) { std::vector<PhdrEntry> Ret; // Process PHDRS and FILEHDR keywords because they are not @@ -979,7 +980,8 @@ std::vector<PhdrEntry> LinkerScript::createPhdrs() { } // Add output sections to program headers. - for (OutputSection *Sec : *OutputSections) { + for (OutputSectionCommand *Cmd : OutputSectionCommands) { + OutputSection *Sec = Cmd->Sec; if (!(Sec->Flags & SHF_ALLOC)) break; diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 1d4c736763f..8a5eb373c44 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -274,7 +274,8 @@ public: void adjustSectionsBeforeSorting(); void adjustSectionsAfterSorting(); - std::vector<PhdrEntry> createPhdrs(); + std::vector<PhdrEntry> + createPhdrs(ArrayRef<OutputSectionCommand *> OutputSectionCommands); bool ignoreInterpSection(); bool hasLMA(OutputSection *Sec); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index ad95a8acced..bb5fbc7ac8f 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1212,18 +1212,19 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() { for (BaseCommand *Base : Script->Opt.Commands) if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) OutputSectionCommands.push_back(Cmd); + clearOutputSections(); // Binary and relocatable output does not have PHDRS. // The headers have to be created before finalize as that can influence the // image base and the dynamic section on mips includes the image base. if (!Config->Relocatable && !Config->OFormatBinary) { - Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs(); + Phdrs = Script->hasPhdrsCommands() + ? Script->createPhdrs(OutputSectionCommands) + : createPhdrs(); addPtArmExid(Phdrs); Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size(); } - clearOutputSections(); - // Compute the size of .rela.dyn and .rela.plt early since we need // them to populate .dynamic. for (SyntheticSection *SS : {In<ELFT>::RelaDyn, In<ELFT>::RelaPlt}) @@ -1386,7 +1387,7 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders); // PT_INTERP must be the second entry if exists. - if (OutputSection *Sec = findSection(".interp")) + if (OutputSection *Sec = findSectionInScript(".interp")) AddHdr(PT_INTERP, Sec->getPhdrFlags())->add(Sec); // Add the first PT_LOAD segment for regular output sections. @@ -1397,7 +1398,8 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { Load->add(Out::ElfHeader); Load->add(Out::ProgramHeaders); - for (OutputSection *Sec : OutputSections) { + for (OutputSectionCommand *Cmd : OutputSectionCommands) { + OutputSection *Sec = Cmd->Sec; if (!(Sec->Flags & SHF_ALLOC)) break; if (!needsPtLoad(Sec)) @@ -1419,9 +1421,11 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { // Add a TLS segment if any. PhdrEntry TlsHdr(PT_TLS, PF_R); - for (OutputSection *Sec : OutputSections) + for (OutputSectionCommand *Cmd : OutputSectionCommands) { + OutputSection *Sec = Cmd->Sec; if (Sec->Flags & SHF_TLS) TlsHdr.add(Sec); + } if (TlsHdr.First) Ret.push_back(std::move(TlsHdr)); @@ -1433,9 +1437,11 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { // PT_GNU_RELRO includes all sections that should be marked as // read-only by dynamic linker after proccessing relocations. PhdrEntry RelRo(PT_GNU_RELRO, PF_R); - for (OutputSection *Sec : OutputSections) + for (OutputSectionCommand *Cmd : OutputSectionCommands) { + OutputSection *Sec = Cmd->Sec; if (needsPtLoad(Sec) && isRelroSection(Sec)) RelRo.add(Sec); + } if (RelRo.First) Ret.push_back(std::move(RelRo)); @@ -1447,7 +1453,7 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes // the dynamic linker fill the segment with random data. - if (OutputSection *Sec = findSection(".openbsd.randomdata")) + if (OutputSection *Sec = findSectionInScript(".openbsd.randomdata")) AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec); // PT_GNU_STACK is a special section to tell the loader to make the @@ -1470,7 +1476,8 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { // Create one PT_NOTE per a group of contiguous .note sections. PhdrEntry *Note = nullptr; - for (OutputSection *Sec : OutputSections) { + for (OutputSectionCommand *Cmd : OutputSectionCommands) { + OutputSection *Sec = Cmd->Sec; if (Sec->Type == SHT_NOTE) { if (!Note || Script->hasLMA(Sec)) Note = AddHdr(PT_NOTE, PF_R); @@ -1486,15 +1493,17 @@ template <class ELFT> void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry> &Phdrs) { if (Config->EMachine != EM_ARM) return; - auto I = std::find_if( - OutputSections.begin(), OutputSections.end(), - [](OutputSection *Sec) { return Sec->Type == SHT_ARM_EXIDX; }); - if (I == OutputSections.end()) + auto I = + std::find_if(OutputSectionCommands.begin(), OutputSectionCommands.end(), + [](OutputSectionCommand *Cmd) { + return Cmd->Sec->Type == SHT_ARM_EXIDX; + }); + if (I == OutputSectionCommands.end()) return; // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME PhdrEntry ARMExidx(PT_ARM_EXIDX, PF_R); - ARMExidx.add(*I); + ARMExidx.add((*I)->Sec); Phdrs.push_back(ARMExidx); } |