diff --git a/llvm/tools/yaml2obj/yaml2elf.cpp b/llvm/tools/yaml2obj/yaml2elf.cpp index 1f83656a606b..e3246bc29412 100644 --- a/llvm/tools/yaml2obj/yaml2elf.cpp +++ b/llvm/tools/yaml2obj/yaml2elf.cpp @@ -325,6 +325,8 @@ void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader, SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym"); SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM; SHeader.sh_link = IsStatic ? getDotStrTabSecNo() : getDotDynStrSecNo(); + if (!IsStatic) + SHeader.sh_flags |= ELF::SHF_ALLOC; // One greater than symbol table index of the last local symbol. const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols; @@ -378,6 +380,9 @@ void ELFState::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, unsigned SecNdx = getDotDynStrSecNo() - 1; if (SecNdx < Doc.Sections.size()) SHeader.sh_addr = Doc.Sections[SecNdx]->Address; + + // We assume that .dynstr is always allocatable. + SHeader.sh_flags |= ELF::SHF_ALLOC; } } @@ -899,19 +904,17 @@ int ELFState::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { SHeaders.push_back({}); // Initialize the implicit sections - auto Index = State.SN2I.get(".symtab"); - State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Static, CBA); - Index = State.SN2I.get(".strtab"); - State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA); - Index = State.SN2I.get(".shstrtab"); - State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA); + State.initSymtabSectionHeader(SHeaders[State.SN2I.get(".symtab")], + SymtabType::Static, CBA); + State.initStrtabSectionHeader(SHeaders[State.SN2I.get(".strtab")], ".strtab", + State.DotStrtab, CBA); + State.initStrtabSectionHeader(SHeaders[State.SN2I.get(".shstrtab")], + ".shstrtab", State.DotShStrtab, CBA); if (!Doc.DynamicSymbols.empty()) { - Index = State.SN2I.get(".dynsym"); - State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Dynamic, CBA); - SHeaders[Index].sh_flags |= ELF::SHF_ALLOC; - Index = State.SN2I.get(".dynstr"); - State.initStrtabSectionHeader(SHeaders[Index], ".dynstr", State.DotDynstr, CBA); - SHeaders[Index].sh_flags |= ELF::SHF_ALLOC; + State.initSymtabSectionHeader(SHeaders[State.SN2I.get(".dynsym")], + SymtabType::Dynamic, CBA); + State.initStrtabSectionHeader(SHeaders[State.SN2I.get(".dynstr")], + ".dynstr", State.DotDynstr, CBA); } // Now we can decide segment offsets