blob: 235327bd0571d3f89cee2c2bfe96bd45cbb249a4 [file] [log] [blame]
Sean Silvaf99309c2013-06-10 23:44:15 +00001//===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief The ELF component of yaml2obj.
12///
13//===----------------------------------------------------------------------===//
14
15#include "yaml2obj.h"
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"
22
23using namespace llvm;
24
Sean Silva38205932013-06-13 22:19:48 +000025// 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.
28//
29// This class has a deliberately small interface, since a lot of
30// implementation variation is possible.
31//
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.
36class 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.
40 std::string Buf;
41public:
42 StringTableBuilder() {
43 Buf.push_back('\0');
44 }
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();
49 if (I != 0)
50 return I;
51 I = Buf.size();
52 Buf.append(S.begin(), S.end());
53 Buf.push_back('\0');
54 return I;
55 }
56 size_t size() const {
57 return Buf.size();
58 }
59 void writeToStream(raw_ostream &OS) {
60 OS.write(Buf.data(), Buf.size());
61 }
62};
63
64template <class T>
65static size_t vectorDataSize(const std::vector<T> &Vec) {
66 return Vec.size() * sizeof(T);
67}
68
69template <class T>
70static void writeVectorData(raw_ostream &OS, const std::vector<T> &Vec) {
71 OS.write((const char *)Vec.data(), vectorDataSize(Vec));
72}
73
74template <class T>
75static void zero(T &Obj) {
76 memset(&Obj, 0, sizeof(Obj));
77}
78
Sean Silvaf99309c2013-06-10 23:44:15 +000079template <class ELFT>
80static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Sean Silvaf99309c2013-06-10 23:44:15 +000081 using namespace llvm::ELF;
82 using namespace llvm::object;
Sean Silva38205932013-06-13 22:19:48 +000083 typedef typename ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
84 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
85
86 const ELFYAML::FileHeader &Hdr = Doc.Header;
87
88 Elf_Ehdr Header;
89 zero(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +000090 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;
Sean Silvaf99309c2013-06-10 23:44:15 +000097 Header.e_ident[EI_VERSION] = EV_CURRENT;
Sean Silvaf99309c2013-06-10 23:44:15 +000098 // 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;
Sean Silva38205932013-06-13 22:19:48 +0000106 Header.e_ehsize = sizeof(Elf_Ehdr);
Sean Silvaf99309c2013-06-10 23:44:15 +0000107
Sean Silva38205932013-06-13 22:19:48 +0000108 // TODO: Flesh out section header support.
109 // TODO: Program headers.
110
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) {
116 ELFYAML::Section S;
117 S.Type = SHT_NULL;
118 zero(S.Flags);
119 Sections.insert(Sections.begin(), S);
120 }
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();
125
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];
130 Elf_Shdr SHeader;
131 zero(SHeader);
132 SHeader.sh_name = StrTab.addString(Sec.Name);
133 SHeader.sh_type = Sec.Type;
134 SHeader.sh_flags = Sec.Flags;
135 SHeader.sh_addr = 0;
136 SHeader.sh_offset = 0;
137 SHeader.sh_size = 0;
138 SHeader.sh_link = 0;
139 SHeader.sh_info = 0;
140 SHeader.sh_addralign = 1;
141 SHeader.sh_entsize = 0;
142 SHeaders.push_back(SHeader);
143 }
144
145 // String table header.
146 Elf_Shdr StrTabSHeader;
147 zero(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;
Sean Silvaf99309c2013-06-10 23:44:15 +0000158
159 OS.write((const char *)&Header, sizeof(Header));
Sean Silva38205932013-06-13 22:19:48 +0000160 writeVectorData(OS, SHeaders);
161 OS.write((const char *)&StrTabSHeader, sizeof(StrTabSHeader));
162 StrTab.writeToStream(OS);
Sean Silvaf99309c2013-06-10 23:44:15 +0000163}
164
165int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
166 yaml::Input YIn(Buf->getBuffer());
167 ELFYAML::Object Doc;
168 YIn >> Doc;
169 if (YIn.error()) {
170 errs() << "yaml2obj: Failed to parse YAML file!\n";
171 return 1;
172 }
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);
176 else
177 writeELF<object::ELFType<support::big, 8, true> >(outs(), Doc);
178 } else {
179 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
180 writeELF<object::ELFType<support::little, 4, false> >(outs(), Doc);
181 else
182 writeELF<object::ELFType<support::big, 4, false> >(outs(), Doc);
183 }
184
185 return 0;
186}