diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 4d7e489c87e8..94e7eccb4849 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -159,11 +159,12 @@ void LinkerDriver::createFiles(opt::InputArgList &Args) { Config->Undefined.push_back(Arg->getValue()); for (auto *Arg : Args.filtered(OPT_z)) { - if (Arg->getValue() == StringRef("nodelete")) + StringRef S = Arg->getValue(); + if (S == "nodelete") Config->ZNodelete = true; - else if (Arg->getValue() == StringRef("now")) + else if (S == "now") Config->ZNow = true; - else if (Arg->getValue() == StringRef("origin")) + else if (S == "origin") Config->ZOrigin = true; } diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index c04147a4c74c..88b8ff4c9d7b 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -349,9 +349,23 @@ template void DynamicSection::finalize() { ++NumEntries; // DT_INIT if (FiniSym) ++NumEntries; // DT_FINI - if (Config->Bsymbolic || Config->ZNow || Config->ZOrigin) + + if (Config->Bsymbolic) + DtFlags |= DF_SYMBOLIC; + if (Config->ZNodelete) + DtFlags1 |= DF_1_NODELETE; + if (Config->ZNow) { + DtFlags |= DF_BIND_NOW; + DtFlags1 |= DF_1_NOW; + } + if (Config->ZOrigin) { + DtFlags |= DF_ORIGIN; + DtFlags1 |= DF_1_ORIGIN; + } + + if (DtFlags) ++NumEntries; // DT_FLAGS - if (Config->ZNodelete || Config->ZNow || Config->ZOrigin) + if (DtFlags1) ++NumEntries; // DT_FLAGS_1 ++NumEntries; // DT_NULL @@ -428,26 +442,10 @@ template void DynamicSection::writeTo(uint8_t *Buf) { WritePtr(DT_INIT, getSymVA(*InitSym)); if (FiniSym) WritePtr(DT_FINI, getSymVA(*FiniSym)); - - uint32_t Flags = 0; - if (Config->Bsymbolic) - Flags |= DF_SYMBOLIC; - if (Config->ZNow) - Flags |= DF_BIND_NOW; - if (Config->ZOrigin) - Flags |= DF_ORIGIN; - if (Flags) - WriteVal(DT_FLAGS, Flags); - Flags = 0; - if (Config->ZNodelete) - Flags |= DF_1_NODELETE; - if (Config->ZNow) - Flags |= DF_1_NOW; - if (Config->ZOrigin) - Flags |= DF_1_ORIGIN; - if (Flags) - WriteVal(DT_FLAGS_1, Flags); - + if (DtFlags) + WriteVal(DT_FLAGS, DtFlags); + if (DtFlags1) + WriteVal(DT_FLAGS_1, DtFlags1); WriteVal(DT_NULL, 0); } diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index 65d4604cbe43..d15ffe65c167 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -305,6 +305,8 @@ private: SymbolTable &SymTab; const ELFSymbolBody *InitSym = nullptr; const ELFSymbolBody *FiniSym = nullptr; + uint32_t DtFlags = 0; + uint32_t DtFlags1 = 0; }; // All output sections that are hadnled by the linker specially are