From db00b61860b30203428cee0cda9f45f55ee280c3 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 1 Feb 2017 22:42:17 +0000 Subject: Simplify createPhdrs. NFC. Instead of creating multiple PHDRs in a single loop, this patch runs one for loop for each PHDR type. I think this improves code readability. llvm-svn: 293832 --- lld/ELF/Writer.cpp | 58 +++++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 10ec3f096f5..09992cad7d0 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1193,31 +1193,18 @@ template std::vector Writer::createPhdrs() { }; // The first phdr entry is PT_PHDR which describes the program header itself. - PhdrEntry &Hdr = *AddHdr(PT_PHDR, PF_R); - Hdr.add(Out::ProgramHeaders); + AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders); // PT_INTERP must be the second entry if exists. - if (OutputSectionBase *Sec = findSection(".interp")) { - PhdrEntry &Hdr = *AddHdr(PT_INTERP, Sec->getPhdrFlags()); - Hdr.add(Sec); - } + if (OutputSectionBase *Sec = findSection(".interp")) + AddHdr(PT_INTERP, Sec->getPhdrFlags())->add(Sec); // Add the first PT_LOAD segment for regular output sections. uintX_t Flags = computeFlags(PF_R); PhdrEntry *Load = AddHdr(PT_LOAD, Flags); - - PhdrEntry TlsHdr(PT_TLS, PF_R); - PhdrEntry RelRo(PT_GNU_RELRO, PF_R); for (OutputSectionBase *Sec : OutputSections) { if (!(Sec->Flags & SHF_ALLOC)) break; - - // If we meet TLS section then we create TLS header - // and put all TLS sections inside for further use when - // assign addresses. - if (Sec->Flags & SHF_TLS) - TlsHdr.add(Sec); - if (!needsPtLoad(Sec)) continue; @@ -1233,48 +1220,47 @@ template std::vector Writer::createPhdrs() { } Load->add(Sec); - - if (isRelroSection(Sec)) - RelRo.add(Sec); } - // Add the TLS segment unless it's empty. + // Add a TLS segment if any. + PhdrEntry TlsHdr(PT_TLS, PF_R); + for (OutputSectionBase *Sec : OutputSections) + if (Sec->Flags & SHF_TLS) + TlsHdr.add(Sec); if (TlsHdr.First) Ret.push_back(std::move(TlsHdr)); // Add an entry for .dynamic. - if (In::DynSymTab) { - PhdrEntry &H = - *AddHdr(PT_DYNAMIC, In::Dynamic->OutSec->getPhdrFlags()); - H.add(In::Dynamic->OutSec); - } + if (In::DynSymTab) + AddHdr(PT_DYNAMIC, In::Dynamic->OutSec->getPhdrFlags()) + ->add(In::Dynamic->OutSec); // 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 (OutputSectionBase *Sec : OutputSections) + if (needsPtLoad(Sec) && isRelroSection(Sec)) + RelRo.add(Sec); if (RelRo.First) Ret.push_back(std::move(RelRo)); // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr. - if (!Out::EhFrame->empty() && In::EhFrameHdr) { - PhdrEntry &Hdr = - *AddHdr(PT_GNU_EH_FRAME, In::EhFrameHdr->OutSec->getPhdrFlags()); - Hdr.add(In::EhFrameHdr->OutSec); - } + if (!Out::EhFrame->empty() && In::EhFrameHdr) + AddHdr(PT_GNU_EH_FRAME, In::EhFrameHdr->OutSec->getPhdrFlags()) + ->add(In::EhFrameHdr->OutSec); // PT_OPENBSD_RANDOMIZE specifies the location and size of a part of the // memory image of the program that must be filled with random data before any // code in the object is executed. - if (OutputSectionBase *Sec = findSection(".openbsd.randomdata")) { - PhdrEntry &Hdr = *AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags()); - Hdr.add(Sec); - } + if (OutputSectionBase *Sec = findSection(".openbsd.randomdata")) + AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec); // PT_GNU_STACK is a special section to tell the loader to make the // pages for the stack non-executable. if (!Config->ZExecstack) { - PhdrEntry &Hdr = *AddHdr(PT_GNU_STACK, PF_R | PF_W); + PhdrEntry *Hdr = AddHdr(PT_GNU_STACK, PF_R | PF_W); if (Config->ZStackSize != uint64_t(-1)) - Hdr.p_memsz = Config->ZStackSize; + Hdr->p_memsz = Config->ZStackSize; } // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable -- cgit v1.2.3