From d2098745a4b5c5af9056d5fd68233a8edb035be3 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Mon, 29 Apr 2019 11:54:10 +0000 Subject: [PATCH] [yaml2obj] - Cleanup and simplify the code. NFCI. The current code has the following problems: `initSymtabSectionHeader` and `initStrtabSectionHeader` method names saying us they are going to initialize the section headers. Though for a few cases sh_flags field is initialized outside of them. It does not look clean. This patch moves initialization of the sh_flags inside these methods. Also, it removes an excessive variable, what together with the above change hopefully makes the code a bit more readable. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359443 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/yaml2obj/yaml2elf.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tools/yaml2obj/yaml2elf.cpp b/tools/yaml2obj/yaml2elf.cpp index 1f83656a606..e3246bc2941 100644 --- a/tools/yaml2obj/yaml2elf.cpp +++ b/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 -- 2.11.4.GIT