[yaml2obj] - Simplify implementation. NFCI.

Knowing about how types are declared for 32/64 bit platforms:
https://github.com/llvm-mirror/llvm/blob/master/include/llvm/BinaryFormat/ELF.h#L28

it is possible to simplify code that writes a binary a bit.
The patch does that.

Differential revision: https://reviews.llvm.org/D58441

llvm-svn: 354462
This commit is contained in:
George Rimar 2019-02-20 13:58:43 +00:00
parent dee5846b4a
commit d063c7d89b
1 changed files with 15 additions and 21 deletions

View File

@ -17,6 +17,7 @@
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/ObjectYAML/ELFYAML.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/YAMLTraits.h"
@ -549,25 +550,23 @@ template <class ELFT>
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::Group &Section,
ContiguousBlobAccumulator &CBA) {
typedef typename ELFT::Word Elf_Word;
assert(Section.Type == llvm::ELF::SHT_GROUP &&
"Section type is not SHT_GROUP");
SHeader.sh_entsize = sizeof(Elf_Word);
SHeader.sh_entsize = 4;
SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
raw_ostream &OS =
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
for (auto member : Section.Members) {
Elf_Word SIdx;
unsigned int sectionIndex = 0;
if (member.sectionNameOrType == "GRP_COMDAT")
sectionIndex = llvm::ELF::GRP_COMDAT;
else if (!convertSectionIndex(SN2I, Section.Name, member.sectionNameOrType,
sectionIndex))
return false;
SIdx = sectionIndex;
OS.write((const char *)&SIdx, sizeof(SIdx));
support::endian::write<uint32_t>(OS, sectionIndex, ELFT::TargetEndianness);
}
return true;
}
@ -576,17 +575,13 @@ template <class ELFT>
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::SymverSection &Section,
ContiguousBlobAccumulator &CBA) {
typedef typename ELFT::Half Elf_Half;
raw_ostream &OS =
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
for (uint16_t V : Section.Entries) {
Elf_Half Version = (Elf_Half)V;
OS.write((const char *)&Version, sizeof(Elf_Half));
}
for (uint16_t V : Section.Entries)
support::endian::write<uint16_t>(OS, V, ELFT::TargetEndianness);
SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Half);
SHeader.sh_entsize = sizeof(Elf_Half);
SHeader.sh_entsize = 2;
SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
return true;
}
@ -671,22 +666,21 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::DynamicSection &Section,
ContiguousBlobAccumulator &CBA) {
typedef typename ELFT::Addr Elf_Addr;
typedef typename ELFT::uint uintX_t;
assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
"Section type is not SHT_DYNAMIC");
SHeader.sh_size = 2 * sizeof(Elf_Addr) * Section.Entries.size();
SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
if (Section.EntSize)
SHeader.sh_entsize = *Section.EntSize;
else
SHeader.sh_entsize = sizeof(Elf_Dyn);
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
Elf_Addr Tag = (Elf_Addr)DE.Tag;
OS.write((const char *)&Tag, sizeof(Elf_Addr));
Elf_Addr Val = (Elf_Addr)DE.Val;
OS.write((const char *)&Val, sizeof(Elf_Addr));
support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
}
}