blob: d3b25ecf09bf2bdb8566ee7bde0cd72f96b5815f [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"
Michael J. Spencer126973b2013-08-08 22:27:13 +000016#include "llvm/Object/ELFObjectFile.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000017#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.
Sean Silva2a74f702013-06-15 00:31:46 +000036namespace {
Sean Silva38205932013-06-13 22:19:48 +000037class StringTableBuilder {
38 /// \brief Indices of strings currently present in `Buf`.
39 StringMap<unsigned> StringIndices;
40 /// \brief The contents of the string table as we build it.
41 std::string Buf;
42public:
43 StringTableBuilder() {
44 Buf.push_back('\0');
45 }
46 /// \returns Index of string in string table.
47 unsigned addString(StringRef S) {
48 StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S);
49 unsigned &I = Entry.getValue();
50 if (I != 0)
51 return I;
52 I = Buf.size();
53 Buf.append(S.begin(), S.end());
54 Buf.push_back('\0');
55 return I;
56 }
57 size_t size() const {
58 return Buf.size();
59 }
60 void writeToStream(raw_ostream &OS) {
61 OS.write(Buf.data(), Buf.size());
62 }
63};
Sean Silva2a74f702013-06-15 00:31:46 +000064} // end anonymous namespace
Sean Silva38205932013-06-13 22:19:48 +000065
Sean Silva46dffff2013-06-13 22:20:01 +000066// This class is used to build up a contiguous binary blob while keeping
67// track of an offset in the output (which notionally begins at
68// `InitialOffset`).
Sean Silva2a74f702013-06-15 00:31:46 +000069namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000070class ContiguousBlobAccumulator {
71 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000072 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000073 raw_svector_ostream OS;
74
Sean Silvad93323f2013-06-22 00:47:43 +000075 /// \returns The new offset.
76 uint64_t padToAlignment(unsigned Align) {
77 uint64_t CurrentOffset = InitialOffset + OS.tell();
78 uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
79 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
80 OS.write('\0');
81 return AlignedOffset; // == CurrentOffset;
82 }
83
Sean Silva46dffff2013-06-13 22:20:01 +000084public:
Sean Silvabd3bc692013-06-20 19:11:41 +000085 ContiguousBlobAccumulator(uint64_t InitialOffset_)
86 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000087 template <class Integer>
88 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
89 Offset = padToAlignment(Align);
90 return OS;
91 }
Sean Silva46dffff2013-06-13 22:20:01 +000092 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
93};
Sean Silva2a74f702013-06-15 00:31:46 +000094} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000095
Sean Silvaa6423eb2013-06-15 00:25:26 +000096// Used to keep track of section names, so that in the YAML file sections
97// can be referenced by name instead of by index.
Sean Silva2a74f702013-06-15 00:31:46 +000098namespace {
Sean Silvaa6423eb2013-06-15 00:25:26 +000099class SectionNameToIdxMap {
100 StringMap<int> Map;
101public:
102 /// \returns true if name is already present in the map.
103 bool addName(StringRef SecName, unsigned i) {
104 StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1);
105 if (Entry.getValue() != -1)
106 return true;
107 Entry.setValue((int)i);
108 return false;
109 }
110 /// \returns true if name is not present in the map
111 bool lookupSection(StringRef SecName, unsigned &Idx) const {
112 StringMap<int>::const_iterator I = Map.find(SecName);
113 if (I == Map.end())
114 return true;
115 Idx = I->getValue();
116 return false;
117 }
118};
Sean Silva2a74f702013-06-15 00:31:46 +0000119} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +0000120
Sean Silva38205932013-06-13 22:19:48 +0000121template <class T>
122static size_t vectorDataSize(const std::vector<T> &Vec) {
123 return Vec.size() * sizeof(T);
124}
125
126template <class T>
127static void writeVectorData(raw_ostream &OS, const std::vector<T> &Vec) {
128 OS.write((const char *)Vec.data(), vectorDataSize(Vec));
129}
130
131template <class T>
132static void zero(T &Obj) {
133 memset(&Obj, 0, sizeof(Obj));
134}
135
Sean Silva85d3eeb2013-06-18 01:11:27 +0000136/// \brief Create a string table in `SHeader`, which we assume is already
137/// zero'd.
138template <class Elf_Shdr>
139static void createStringTableSectionHeader(Elf_Shdr &SHeader,
140 StringTableBuilder &STB,
141 ContiguousBlobAccumulator &CBA) {
142 SHeader.sh_type = ELF::SHT_STRTAB;
Sean Silvad93323f2013-06-22 00:47:43 +0000143 STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
Sean Silva85d3eeb2013-06-18 01:11:27 +0000144 SHeader.sh_size = STB.size();
Sean Silva85d3eeb2013-06-18 01:11:27 +0000145 SHeader.sh_addralign = 1;
146}
147
Sean Silva08a75ae2013-06-20 19:11:44 +0000148namespace {
149/// \brief "Single point of truth" for the ELF file construction.
150/// TODO: This class still has a ways to go before it is truly a "single
151/// point of truth".
152template <class ELFT>
153class ELFState {
154 /// \brief The future ".strtab" section.
155 StringTableBuilder DotStrtab;
156 /// \brief The section number of the ".strtab" section.
157 unsigned DotStrtabSecNo;
158 /// \brief The accumulated contents of all sections so far.
159 ContiguousBlobAccumulator &SectionContentAccum;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000160 typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
Sean Silva08a75ae2013-06-20 19:11:44 +0000161 /// \brief The ELF file header.
162 Elf_Ehdr &Header;
163
Sean Silvac1c290b2013-06-20 20:59:34 +0000164 SectionNameToIdxMap &SN2I;
165
Sean Silva08a75ae2013-06-20 19:11:44 +0000166public:
167
168 ELFState(Elf_Ehdr &Header_, ContiguousBlobAccumulator &Accum,
Sean Silvac1c290b2013-06-20 20:59:34 +0000169 unsigned DotStrtabSecNo_, SectionNameToIdxMap &SN2I_)
Sean Silva08a75ae2013-06-20 19:11:44 +0000170 : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_),
Sean Silvac1c290b2013-06-20 20:59:34 +0000171 SectionContentAccum(Accum), Header(Header_), SN2I(SN2I_) {}
Sean Silva08a75ae2013-06-20 19:11:44 +0000172
173 unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; }
174 StringTableBuilder &getStringTable() { return DotStrtab; }
175 ContiguousBlobAccumulator &getSectionContentAccum() {
176 return SectionContentAccum;
177 }
Sean Silvac1c290b2013-06-20 20:59:34 +0000178 SectionNameToIdxMap &getSN2I() { return SN2I; }
Sean Silva08a75ae2013-06-20 19:11:44 +0000179};
180} // end anonymous namespace
181
Sean Silvaaff51252013-06-21 00:27:50 +0000182// FIXME: At this point it is fairly clear that we need to refactor these
183// static functions into methods of a class sharing some typedefs. These
184// ELF type names are insane.
Sean Silva37e817c2013-06-21 00:33:01 +0000185template <class ELFT>
186static void
187addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, ELFState<ELFT> &State,
Michael J. Spencer126973b2013-08-08 22:27:13 +0000188 std::vector<typename object::ELFFile<ELFT>::Elf_Sym> &Syms,
Sean Silva37e817c2013-06-21 00:33:01 +0000189 unsigned SymbolBinding) {
Michael J. Spencer126973b2013-08-08 22:27:13 +0000190 typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Sean Silvaaff51252013-06-21 00:27:50 +0000191 for (unsigned i = 0, e = Symbols.size(); i != e; ++i) {
192 const ELFYAML::Symbol &Sym = Symbols[i];
Sean Silva6b083882013-06-18 23:14:03 +0000193 Elf_Sym Symbol;
194 zero(Symbol);
195 if (!Sym.Name.empty())
Sean Silva08a75ae2013-06-20 19:11:44 +0000196 Symbol.st_name = State.getStringTable().addString(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000197 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000198 if (!Sym.Section.empty()) {
199 unsigned Index;
200 if (State.getSN2I().lookupSection(Sym.Section, Index)) {
201 errs() << "error: Unknown section referenced: '" << Sym.Section
202 << "' by YAML symbol " << Sym.Name << ".\n";
203 exit(1);
204 }
205 Symbol.st_shndx = Index;
206 } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000207 Symbol.st_value = Sym.Value;
208 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000209 Syms.push_back(Symbol);
210 }
Sean Silvaaff51252013-06-21 00:27:50 +0000211}
212
213template <class ELFT>
Michael J. Spencer126973b2013-08-08 22:27:13 +0000214static void
215handleSymtabSectionHeader(const ELFYAML::LocalGlobalWeakSymbols &Symbols,
216 ELFState<ELFT> &State,
217 typename object::ELFFile<ELFT>::Elf_Shdr &SHeader) {
Sean Silvaaff51252013-06-21 00:27:50 +0000218
Michael J. Spencer126973b2013-08-08 22:27:13 +0000219 typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Sean Silva82177572013-06-22 01:38:00 +0000220 SHeader.sh_type = ELF::SHT_SYMTAB;
Sean Silvaaff51252013-06-21 00:27:50 +0000221 SHeader.sh_link = State.getDotStrTabSecNo();
222 // One greater than symbol table index of the last local symbol.
Sean Silva7a0c3a62013-06-22 01:37:55 +0000223 SHeader.sh_info = Symbols.Local.size() + 1;
Sean Silvaaff51252013-06-21 00:27:50 +0000224 SHeader.sh_entsize = sizeof(Elf_Sym);
225
226 std::vector<Elf_Sym> Syms;
227 {
228 // Ensure STN_UNDEF is present
229 Elf_Sym Sym;
230 zero(Sym);
231 Syms.push_back(Sym);
232 }
Sean Silva7a0c3a62013-06-22 01:37:55 +0000233 addSymbols(Symbols.Local, State, Syms, ELF::STB_LOCAL);
234 addSymbols(Symbols.Global, State, Syms, ELF::STB_GLOBAL);
235 addSymbols(Symbols.Weak, State, Syms, ELF::STB_WEAK);
Sean Silva6b083882013-06-18 23:14:03 +0000236
Sean Silva08a75ae2013-06-20 19:11:44 +0000237 ContiguousBlobAccumulator &CBA = State.getSectionContentAccum();
Sean Silvad93323f2013-06-22 00:47:43 +0000238 writeVectorData(CBA.getOSAndAlignedOffset(SHeader.sh_offset), Syms);
Sean Silva6b083882013-06-18 23:14:03 +0000239 SHeader.sh_size = vectorDataSize(Syms);
Sean Silva6b083882013-06-18 23:14:03 +0000240}
241
Sean Silvaf99309c2013-06-10 23:44:15 +0000242template <class ELFT>
Sean Silva415d93f2013-06-17 20:14:59 +0000243static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Sean Silvaf99309c2013-06-10 23:44:15 +0000244 using namespace llvm::ELF;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000245 typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
246 typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Sean Silva38205932013-06-13 22:19:48 +0000247
248 const ELFYAML::FileHeader &Hdr = Doc.Header;
249
250 Elf_Ehdr Header;
251 zero(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000252 Header.e_ident[EI_MAG0] = 0x7f;
253 Header.e_ident[EI_MAG1] = 'E';
254 Header.e_ident[EI_MAG2] = 'L';
255 Header.e_ident[EI_MAG3] = 'F';
256 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
257 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
258 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
Sean Silvaf99309c2013-06-10 23:44:15 +0000259 Header.e_ident[EI_VERSION] = EV_CURRENT;
Sean Silvab3a013a2013-06-19 01:10:58 +0000260 Header.e_ident[EI_OSABI] = Hdr.OSABI;
Sean Silvaf99309c2013-06-10 23:44:15 +0000261 Header.e_ident[EI_ABIVERSION] = 0;
262 Header.e_type = Hdr.Type;
263 Header.e_machine = Hdr.Machine;
264 Header.e_version = EV_CURRENT;
265 Header.e_entry = Hdr.Entry;
Sean Silva38205932013-06-13 22:19:48 +0000266 Header.e_ehsize = sizeof(Elf_Ehdr);
Sean Silvaf99309c2013-06-10 23:44:15 +0000267
Sean Silva38205932013-06-13 22:19:48 +0000268 // TODO: Flesh out section header support.
269 // TODO: Program headers.
270
271 Header.e_shentsize = sizeof(Elf_Shdr);
272 // Immediately following the ELF header.
273 Header.e_shoff = sizeof(Header);
Sean Silvabdf19862013-06-18 23:37:23 +0000274 const std::vector<ELFYAML::Section> &Sections = Doc.Sections;
Sean Silva82177572013-06-22 01:38:00 +0000275 // "+ 4" for
Sean Silvabdf19862013-06-18 23:37:23 +0000276 // - SHT_NULL entry (placed first, i.e. 0'th entry)
Sean Silva82177572013-06-22 01:38:00 +0000277 // - symbol table (.symtab) (placed third to last)
Sean Silvabdf19862013-06-18 23:37:23 +0000278 // - string table (.strtab) (placed second to last)
279 // - section header string table. (placed last)
Sean Silva82177572013-06-22 01:38:00 +0000280 Header.e_shnum = Sections.size() + 4;
Sean Silva38205932013-06-13 22:19:48 +0000281 // Place section header string table last.
Sean Silvabdf19862013-06-18 23:37:23 +0000282 Header.e_shstrndx = Header.e_shnum - 1;
283 const unsigned DotStrtabSecNo = Header.e_shnum - 2;
Sean Silva38205932013-06-13 22:19:48 +0000284
Sean Silva08a75ae2013-06-20 19:11:44 +0000285 // XXX: This offset is tightly coupled with the order that we write
286 // things to `OS`.
287 const size_t SectionContentBeginOffset =
288 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
289 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvaa6423eb2013-06-15 00:25:26 +0000290 SectionNameToIdxMap SN2I;
291 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
292 StringRef Name = Sections[i].Name;
293 if (Name.empty())
294 continue;
Sean Silvabdf19862013-06-18 23:37:23 +0000295 // "+ 1" to take into account the SHT_NULL entry.
296 if (SN2I.addName(Name, i + 1)) {
Sean Silvaa6423eb2013-06-15 00:25:26 +0000297 errs() << "error: Repeated section name: '" << Name
298 << "' at YAML section number " << i << ".\n";
Sean Silva415d93f2013-06-17 20:14:59 +0000299 return 1;
Sean Silvaa6423eb2013-06-15 00:25:26 +0000300 }
301 }
302
Sean Silvac1c290b2013-06-20 20:59:34 +0000303 ELFState<ELFT> State(Header, CBA, DotStrtabSecNo, SN2I);
304
Sean Silvafde4ab02013-06-18 01:11:24 +0000305 StringTableBuilder SHStrTab;
Sean Silva38205932013-06-13 22:19:48 +0000306 std::vector<Elf_Shdr> SHeaders;
Sean Silvabdf19862013-06-18 23:37:23 +0000307 {
308 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
309 // valid SHN_UNDEF entry since SHT_NULL == 0.
310 Elf_Shdr SHdr;
311 zero(SHdr);
312 SHeaders.push_back(SHdr);
313 }
Sean Silva38205932013-06-13 22:19:48 +0000314 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
315 const ELFYAML::Section &Sec = Sections[i];
316 Elf_Shdr SHeader;
317 zero(SHeader);
Sean Silvafde4ab02013-06-18 01:11:24 +0000318 SHeader.sh_name = SHStrTab.addString(Sec.Name);
Sean Silva38205932013-06-13 22:19:48 +0000319 SHeader.sh_type = Sec.Type;
320 SHeader.sh_flags = Sec.Flags;
Sean Silvaf4bfced2013-06-13 22:19:54 +0000321 SHeader.sh_addr = Sec.Address;
Sean Silva46dffff2013-06-13 22:20:01 +0000322
Sean Silvad93323f2013-06-22 00:47:43 +0000323 Sec.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
Sean Silva46dffff2013-06-13 22:20:01 +0000324 SHeader.sh_size = Sec.Content.binary_size();
Sean Silva46dffff2013-06-13 22:20:01 +0000325
Sean Silvaa6423eb2013-06-15 00:25:26 +0000326 if (!Sec.Link.empty()) {
327 unsigned Index;
328 if (SN2I.lookupSection(Sec.Link, Index)) {
329 errs() << "error: Unknown section referenced: '" << Sec.Link
330 << "' at YAML section number " << i << ".\n";
Sean Silva415d93f2013-06-17 20:14:59 +0000331 return 1;
Sean Silvaa6423eb2013-06-15 00:25:26 +0000332 }
333 SHeader.sh_link = Index;
334 }
Sean Silva38205932013-06-13 22:19:48 +0000335 SHeader.sh_info = 0;
Sean Silva0a409cf2013-06-14 00:38:02 +0000336 SHeader.sh_addralign = Sec.AddressAlign;
Sean Silva38205932013-06-13 22:19:48 +0000337 SHeader.sh_entsize = 0;
338 SHeaders.push_back(SHeader);
339 }
340
Sean Silva82177572013-06-22 01:38:00 +0000341 // .symtab section.
342 Elf_Shdr SymtabSHeader;
343 zero(SymtabSHeader);
344 SymtabSHeader.sh_name = SHStrTab.addString(StringRef(".symtab"));
345 handleSymtabSectionHeader<ELFT>(Doc.Symbols, State, SymtabSHeader);
346 SHeaders.push_back(SymtabSHeader);
347
Sean Silva6b083882013-06-18 23:14:03 +0000348 // .strtab string table header.
Sean Silvac3131922013-06-18 21:37:50 +0000349 Elf_Shdr DotStrTabSHeader;
350 zero(DotStrTabSHeader);
351 DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab"));
Sean Silva08a75ae2013-06-20 19:11:44 +0000352 createStringTableSectionHeader(DotStrTabSHeader, State.getStringTable(), CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000353 SHeaders.push_back(DotStrTabSHeader);
Sean Silvac3131922013-06-18 21:37:50 +0000354
Sean Silvafde4ab02013-06-18 01:11:24 +0000355 // Section header string table header.
356 Elf_Shdr SHStrTabSHeader;
357 zero(SHStrTabSHeader);
Sean Silva85d3eeb2013-06-18 01:11:27 +0000358 createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000359 SHeaders.push_back(SHStrTabSHeader);
Sean Silvaf99309c2013-06-10 23:44:15 +0000360
361 OS.write((const char *)&Header, sizeof(Header));
Sean Silva38205932013-06-13 22:19:48 +0000362 writeVectorData(OS, SHeaders);
Sean Silva46dffff2013-06-13 22:20:01 +0000363 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000364 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000365}
366
Sean Silva11caeba2013-06-22 01:03:35 +0000367static bool is64Bit(const ELFYAML::Object &Doc) {
368 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
369}
370
371static bool isLittleEndian(const ELFYAML::Object &Doc) {
372 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
373}
374
Sean Silvaf99309c2013-06-10 23:44:15 +0000375int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
376 yaml::Input YIn(Buf->getBuffer());
377 ELFYAML::Object Doc;
378 YIn >> Doc;
379 if (YIn.error()) {
380 errs() << "yaml2obj: Failed to parse YAML file!\n";
381 return 1;
382 }
Sean Silva11caeba2013-06-22 01:03:35 +0000383 using object::ELFType;
384 typedef ELFType<support::little, 8, true> LE64;
385 typedef ELFType<support::big, 8, true> BE64;
386 typedef ELFType<support::little, 4, false> LE32;
387 typedef ELFType<support::big, 4, false> BE32;
388 if (is64Bit(Doc)) {
389 if (isLittleEndian(Doc))
390 return writeELF<LE64>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000391 else
Sean Silva11caeba2013-06-22 01:03:35 +0000392 return writeELF<BE64>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000393 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000394 if (isLittleEndian(Doc))
395 return writeELF<LE32>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000396 else
Sean Silva11caeba2013-06-22 01:03:35 +0000397 return writeELF<BE32>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000398 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000399}