llvm.org GIT mirror llvm / 2ff622e
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354462 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 7 months ago
1 changed file(s) with 16 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
1616 #include "llvm/MC/StringTableBuilder.h"
1717 #include "llvm/Object/ELFObjectFile.h"
1818 #include "llvm/ObjectYAML/ELFYAML.h"
19 #include "llvm/Support/EndianStream.h"
1920 #include "llvm/Support/MemoryBuffer.h"
2021 #include "llvm/Support/WithColor.h"
2122 #include "llvm/Support/YAMLTraits.h"
548549 bool ELFState::writeSectionContent(Elf_Shdr &SHeader,
549550 const ELFYAML::Group &Section,
550551 ContiguousBlobAccumulator &CBA) {
551 typedef typename ELFT::Word Elf_Word;
552552 assert(Section.Type == llvm::ELF::SHT_GROUP &&
553553 "Section type is not SHT_GROUP");
554554
555 SHeader.sh_entsize = sizeof(Elf_Word);
555 SHeader.sh_entsize = 4;
556556 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
557557
558 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
558 raw_ostream &OS =
559 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
559560
560561 for (auto member : Section.Members) {
561 Elf_Word SIdx;
562562 unsigned int sectionIndex = 0;
563563 if (member.sectionNameOrType == "GRP_COMDAT")
564564 sectionIndex = llvm::ELF::GRP_COMDAT;
565565 else if (!convertSectionIndex(SN2I, Section.Name, member.sectionNameOrType,
566566 sectionIndex))
567567 return false;
568 SIdx = sectionIndex;
569 OS.write((const char *)&SIdx, sizeof(SIdx));
568 support::endian::write(OS, sectionIndex, ELFT::TargetEndianness);
570569 }
571570 return true;
572571 }
575574 bool ELFState::writeSectionContent(Elf_Shdr &SHeader,
576575 const ELFYAML::SymverSection &Section,
577576 ContiguousBlobAccumulator &CBA) {
578 typedef typename ELFT::Half Elf_Half;
579
580577 raw_ostream &OS =
581578 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
582 for (uint16_t V : Section.Entries) {
583 Elf_Half Version = (Elf_Half)V;
584 OS.write((const char *)&Version, sizeof(Elf_Half));
585 }
586
587 SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Half);
588 SHeader.sh_entsize = sizeof(Elf_Half);
579 for (uint16_t V : Section.Entries)
580 support::endian::write(OS, V, ELFT::TargetEndianness);
581
582 SHeader.sh_entsize = 2;
583 SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
589584 return true;
590585 }
591586
670665 void ELFState::writeSectionContent(Elf_Shdr &SHeader,
671666 const ELFYAML::DynamicSection &Section,
672667 ContiguousBlobAccumulator &CBA) {
673 typedef typename ELFT::Addr Elf_Addr;
668 typedef typename ELFT::uint uintX_t;
669
674670 assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
675671 "Section type is not SHT_DYNAMIC");
676672
677 SHeader.sh_size = 2 * sizeof(Elf_Addr) * Section.Entries.size();
673 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
678674 if (Section.EntSize)
679675 SHeader.sh_entsize = *Section.EntSize;
680676 else
681677 SHeader.sh_entsize = sizeof(Elf_Dyn);
682678
683 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
679 raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
684680 for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
685 Elf_Addr Tag = (Elf_Addr)DE.Tag;
686 OS.write((const char *)&Tag, sizeof(Elf_Addr));
687 Elf_Addr Val = (Elf_Addr)DE.Val;
688 OS.write((const char *)&Val, sizeof(Elf_Addr));
681 support::endian::write(OS, DE.Tag, ELFT::TargetEndianness);
682 support::endian::write(OS, DE.Val, ELFT::TargetEndianness);
689683 }
690684 }
691685