[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.

llvm-svn: 359443
This commit is contained in:
George Rimar 2019-04-29 11:54:10 +00:00
parent a6159a226e
commit 4828811a2d
1 changed files with 15 additions and 12 deletions

View File

@ -325,6 +325,8 @@ void ELFState<ELFT>::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<ELFT>::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<ELFT>::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