1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief The ELF component of yaml2obj.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Object/ELF.h"
17 #include "llvm/Object/ELFYAML.h"
18 #include "llvm/Support/ELF.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/YAMLTraits.h"
21 #include "llvm/Support/raw_ostream.h"
25 // There is similar code in yaml2coff, but with some slight COFF-specific
26 // variations like different initial state. Might be able to deduplicate
27 // some day, but also want to make sure that the Mach-O use case is served.
29 // This class has a deliberately small interface, since a lot of
30 // implementation variation is possible.
32 // TODO: Use an ordered container with a suffix-based comparison in order
33 // to deduplicate suffixes. std::map<> with a custom comparator is likely
34 // to be the simplest implementation, but a suffix trie could be more
35 // suitable for the job.
36 class StringTableBuilder {
37 /// \brief Indices of strings currently present in `Buf`.
38 StringMap<unsigned> StringIndices;
39 /// \brief The contents of the string table as we build it.
42 StringTableBuilder() {
45 /// \returns Index of string in string table.
46 unsigned addString(StringRef S) {
47 StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S);
48 unsigned &I = Entry.getValue();
52 Buf.append(S.begin(), S.end());
59 void writeToStream(raw_ostream &OS) {
60 OS.write(Buf.data(), Buf.size());
65 static size_t vectorDataSize(const std::vector<T> &Vec) {
66 return Vec.size() * sizeof(T);
70 static void writeVectorData(raw_ostream &OS, const std::vector<T> &Vec) {
71 OS.write((const char *)Vec.data(), vectorDataSize(Vec));
75 static void zero(T &Obj) {
76 memset(&Obj, 0, sizeof(Obj));
80 static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
81 using namespace llvm::ELF;
82 using namespace llvm::object;
83 typedef typename ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
84 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
86 const ELFYAML::FileHeader &Hdr = Doc.Header;
90 Header.e_ident[EI_MAG0] = 0x7f;
91 Header.e_ident[EI_MAG1] = 'E';
92 Header.e_ident[EI_MAG2] = 'L';
93 Header.e_ident[EI_MAG3] = 'F';
94 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
95 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
96 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
97 Header.e_ident[EI_VERSION] = EV_CURRENT;
98 // TODO: Implement ELF_ELFOSABI enum.
99 Header.e_ident[EI_OSABI] = ELFOSABI_NONE;
100 // TODO: Implement ELF_ABIVERSION enum.
101 Header.e_ident[EI_ABIVERSION] = 0;
102 Header.e_type = Hdr.Type;
103 Header.e_machine = Hdr.Machine;
104 Header.e_version = EV_CURRENT;
105 Header.e_entry = Hdr.Entry;
106 Header.e_ehsize = sizeof(Elf_Ehdr);
108 // TODO: Flesh out section header support.
109 // TODO: Program headers.
111 Header.e_shentsize = sizeof(Elf_Shdr);
112 // Immediately following the ELF header.
113 Header.e_shoff = sizeof(Header);
114 std::vector<ELFYAML::Section> Sections = Doc.Sections;
115 if (Sections.empty() || Sections.front().Type != SHT_NULL) {
119 Sections.insert(Sections.begin(), S);
121 // "+ 1" for string table.
122 Header.e_shnum = Sections.size() + 1;
123 // Place section header string table last.
124 Header.e_shstrndx = Sections.size();
126 StringTableBuilder StrTab;
127 std::vector<Elf_Shdr> SHeaders;
128 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
129 const ELFYAML::Section &Sec = Sections[i];
132 SHeader.sh_name = StrTab.addString(Sec.Name);
133 SHeader.sh_type = Sec.Type;
134 SHeader.sh_flags = Sec.Flags;
135 SHeader.sh_addr = Sec.Address;
136 SHeader.sh_offset = 0;
140 SHeader.sh_addralign = 1;
141 SHeader.sh_entsize = 0;
142 SHeaders.push_back(SHeader);
145 // String table header.
146 Elf_Shdr StrTabSHeader;
148 StrTabSHeader.sh_name = 0;
149 StrTabSHeader.sh_type = SHT_STRTAB;
150 StrTabSHeader.sh_flags = 0;
151 StrTabSHeader.sh_addr = 0;
152 StrTabSHeader.sh_offset = Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
153 StrTabSHeader.sh_size = StrTab.size();
154 StrTabSHeader.sh_link = 0;
155 StrTabSHeader.sh_info = 0;
156 StrTabSHeader.sh_addralign = 1;
157 StrTabSHeader.sh_entsize = 0;
159 OS.write((const char *)&Header, sizeof(Header));
160 writeVectorData(OS, SHeaders);
161 OS.write((const char *)&StrTabSHeader, sizeof(StrTabSHeader));
162 StrTab.writeToStream(OS);
165 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
166 yaml::Input YIn(Buf->getBuffer());
170 errs() << "yaml2obj: Failed to parse YAML file!\n";
173 if (Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
174 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
175 writeELF<object::ELFType<support::little, 8, true> >(outs(), Doc);
177 writeELF<object::ELFType<support::big, 8, true> >(outs(), Doc);
179 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
180 writeELF<object::ELFType<support::little, 4, false> >(outs(), Doc);
182 writeELF<object::ELFType<support::big, 4, false> >(outs(), Doc);