Skip to content

Commit d063c7d

Browse files
author
George Rimar
committedFeb 20, 2019
[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
1 parent dee5846 commit d063c7d

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed
 

‎llvm/tools/yaml2obj/yaml2elf.cpp

+15-21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/MC/StringTableBuilder.h"
1818
#include "llvm/Object/ELFObjectFile.h"
1919
#include "llvm/ObjectYAML/ELFYAML.h"
20+
#include "llvm/Support/EndianStream.h"
2021
#include "llvm/Support/MemoryBuffer.h"
2122
#include "llvm/Support/WithColor.h"
2223
#include "llvm/Support/YAMLTraits.h"
@@ -549,25 +550,23 @@ template <class ELFT>
549550
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
550551
const ELFYAML::Group &Section,
551552
ContiguousBlobAccumulator &CBA) {
552-
typedef typename ELFT::Word Elf_Word;
553553
assert(Section.Type == llvm::ELF::SHT_GROUP &&
554554
"Section type is not SHT_GROUP");
555555

556-
SHeader.sh_entsize = sizeof(Elf_Word);
556+
SHeader.sh_entsize = 4;
557557
SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
558558

559-
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
559+
raw_ostream &OS =
560+
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
560561

561562
for (auto member : Section.Members) {
562-
Elf_Word SIdx;
563563
unsigned int sectionIndex = 0;
564564
if (member.sectionNameOrType == "GRP_COMDAT")
565565
sectionIndex = llvm::ELF::GRP_COMDAT;
566566
else if (!convertSectionIndex(SN2I, Section.Name, member.sectionNameOrType,
567567
sectionIndex))
568568
return false;
569-
SIdx = sectionIndex;
570-
OS.write((const char *)&SIdx, sizeof(SIdx));
569+
support::endian::write<uint32_t>(OS, sectionIndex, ELFT::TargetEndianness);
571570
}
572571
return true;
573572
}
@@ -576,17 +575,13 @@ template <class ELFT>
576575
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
577576
const ELFYAML::SymverSection &Section,
578577
ContiguousBlobAccumulator &CBA) {
579-
typedef typename ELFT::Half Elf_Half;
580-
581578
raw_ostream &OS =
582579
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
583-
for (uint16_t V : Section.Entries) {
584-
Elf_Half Version = (Elf_Half)V;
585-
OS.write((const char *)&Version, sizeof(Elf_Half));
586-
}
580+
for (uint16_t V : Section.Entries)
581+
support::endian::write<uint16_t>(OS, V, ELFT::TargetEndianness);
587582

588-
SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Half);
589-
SHeader.sh_entsize = sizeof(Elf_Half);
583+
SHeader.sh_entsize = 2;
584+
SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
590585
return true;
591586
}
592587

@@ -671,22 +666,21 @@ template <class ELFT>
671666
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
672667
const ELFYAML::DynamicSection &Section,
673668
ContiguousBlobAccumulator &CBA) {
674-
typedef typename ELFT::Addr Elf_Addr;
669+
typedef typename ELFT::uint uintX_t;
670+
675671
assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
676672
"Section type is not SHT_DYNAMIC");
677673

678-
SHeader.sh_size = 2 * sizeof(Elf_Addr) * Section.Entries.size();
674+
SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
679675
if (Section.EntSize)
680676
SHeader.sh_entsize = *Section.EntSize;
681677
else
682678
SHeader.sh_entsize = sizeof(Elf_Dyn);
683679

684-
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
680+
raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
685681
for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
686-
Elf_Addr Tag = (Elf_Addr)DE.Tag;
687-
OS.write((const char *)&Tag, sizeof(Elf_Addr));
688-
Elf_Addr Val = (Elf_Addr)DE.Val;
689-
OS.write((const char *)&Val, sizeof(Elf_Addr));
682+
support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
683+
support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
690684
}
691685
}
692686

0 commit comments

Comments
 (0)
Please sign in to comment.