- StringTableBuilder &getStringTable() { return DotStrtab; }
- SectionNameToIdxMap &getSN2I() { return SN2I; }
-
- bool buildSectionIndex() {
- SN2I.addName(".symtab", getDotSymTabSecNo());
- SN2I.addName(".strtab", getDotStrTabSecNo());
- SN2I.addName(".shstrtab", getDotShStrTabSecNo());
-
- for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
- StringRef Name = Doc.Sections[i].Name;
- if (Name.empty())
- continue;
- // "+ 1" to take into account the SHT_NULL entry.
- if (SN2I.addName(Name, i + 1)) {
- errs() << "error: Repeated section name: '" << Name
- << "' at YAML section number " << i << ".\n";
+ ELFState(const ELFYAML::Object &D) : Doc(D) {}
+
+public:
+ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
+};
+} // end anonymous namespace
+
+template <class ELFT>
+void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
+ using namespace llvm::ELF;
+ zero(Header);
+ Header.e_ident[EI_MAG0] = 0x7f;
+ Header.e_ident[EI_MAG1] = 'E';
+ Header.e_ident[EI_MAG2] = 'L';
+ Header.e_ident[EI_MAG3] = 'F';
+ Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
+ bool IsLittleEndian = ELFT::TargetEndianness == support::little;
+ Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
+ Header.e_ident[EI_VERSION] = EV_CURRENT;
+ Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
+ Header.e_ident[EI_ABIVERSION] = 0;
+ Header.e_type = Doc.Header.Type;
+ Header.e_machine = Doc.Header.Machine;
+ Header.e_version = EV_CURRENT;
+ Header.e_entry = Doc.Header.Entry;
+ Header.e_flags = Doc.Header.Flags;
+ Header.e_ehsize = sizeof(Elf_Ehdr);
+ Header.e_shentsize = sizeof(Elf_Shdr);
+ // Immediately following the ELF header.
+ Header.e_shoff = sizeof(Header);
+ Header.e_shnum = getSectionCount();
+ Header.e_shstrndx = getDotShStrTabSecNo();
+}
+
+template <class ELFT>
+bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
+ ContiguousBlobAccumulator &CBA) {
+ // Ensure SHN_UNDEF entry is present. An all-zero section header is a
+ // valid SHN_UNDEF entry since SHT_NULL == 0.
+ Elf_Shdr SHeader;
+ zero(SHeader);
+ SHeaders.push_back(SHeader);
+
+ for (const auto &Sec : Doc.Sections)
+ DotShStrtab.add(Sec->Name);
+ DotShStrtab.finalize(StringTableBuilder::ELF);
+
+ for (const auto &Sec : Doc.Sections) {
+ zero(SHeader);
+ SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
+ SHeader.sh_type = Sec->Type;
+ SHeader.sh_flags = Sec->Flags;
+ SHeader.sh_addr = Sec->Address;
+ SHeader.sh_addralign = Sec->AddressAlign;
+
+ if (!Sec->Link.empty()) {
+ unsigned Index;
+ if (SN2I.lookup(Sec->Link, Index)) {
+ errs() << "error: Unknown section referenced: '" << Sec->Link
+ << "' at YAML section '" << Sec->Name << "'.\n";