blob: bc0cd3178137371b7dabca3fbc462bec6d18cf29 [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"
Will Dietz0b48c732013-10-12 21:29:16 +000016#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000017#include "llvm/Object/ELFObjectFile.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000018#include "llvm/Object/ELFYAML.h"
19#include "llvm/Support/ELF.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/YAMLTraits.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace llvm;
25
Sean Silva38205932013-06-13 22:19:48 +000026// There is similar code in yaml2coff, but with some slight COFF-specific
27// variations like different initial state. Might be able to deduplicate
28// some day, but also want to make sure that the Mach-O use case is served.
29//
30// This class has a deliberately small interface, since a lot of
31// implementation variation is possible.
32//
33// TODO: Use an ordered container with a suffix-based comparison in order
34// to deduplicate suffixes. std::map<> with a custom comparator is likely
35// to be the simplest implementation, but a suffix trie could be more
36// suitable for the job.
Sean Silva2a74f702013-06-15 00:31:46 +000037namespace {
Sean Silva38205932013-06-13 22:19:48 +000038class StringTableBuilder {
39 /// \brief Indices of strings currently present in `Buf`.
40 StringMap<unsigned> StringIndices;
41 /// \brief The contents of the string table as we build it.
42 std::string Buf;
43public:
44 StringTableBuilder() {
45 Buf.push_back('\0');
46 }
47 /// \returns Index of string in string table.
48 unsigned addString(StringRef S) {
49 StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S);
50 unsigned &I = Entry.getValue();
51 if (I != 0)
52 return I;
53 I = Buf.size();
54 Buf.append(S.begin(), S.end());
55 Buf.push_back('\0');
56 return I;
57 }
58 size_t size() const {
59 return Buf.size();
60 }
61 void writeToStream(raw_ostream &OS) {
62 OS.write(Buf.data(), Buf.size());
63 }
64};
Sean Silva2a74f702013-06-15 00:31:46 +000065} // end anonymous namespace
Sean Silva38205932013-06-13 22:19:48 +000066
Sean Silva46dffff2013-06-13 22:20:01 +000067// This class is used to build up a contiguous binary blob while keeping
68// track of an offset in the output (which notionally begins at
69// `InitialOffset`).
Sean Silva2a74f702013-06-15 00:31:46 +000070namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000071class ContiguousBlobAccumulator {
72 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000073 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000074 raw_svector_ostream OS;
75
Sean Silvad93323f2013-06-22 00:47:43 +000076 /// \returns The new offset.
77 uint64_t padToAlignment(unsigned Align) {
78 uint64_t CurrentOffset = InitialOffset + OS.tell();
79 uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
80 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
81 OS.write('\0');
82 return AlignedOffset; // == CurrentOffset;
83 }
84
Sean Silva46dffff2013-06-13 22:20:01 +000085public:
Sean Silvabd3bc692013-06-20 19:11:41 +000086 ContiguousBlobAccumulator(uint64_t InitialOffset_)
87 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000088 template <class Integer>
89 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
90 Offset = padToAlignment(Align);
91 return OS;
92 }
Sean Silva46dffff2013-06-13 22:20:01 +000093 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
94};
Sean Silva2a74f702013-06-15 00:31:46 +000095} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000096
Sean Silvaa6423eb2013-06-15 00:25:26 +000097// Used to keep track of section names, so that in the YAML file sections
98// can be referenced by name instead of by index.
Sean Silva2a74f702013-06-15 00:31:46 +000099namespace {
Sean Silvaa6423eb2013-06-15 00:25:26 +0000100class SectionNameToIdxMap {
101 StringMap<int> Map;
102public:
103 /// \returns true if name is already present in the map.
104 bool addName(StringRef SecName, unsigned i) {
105 StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1);
106 if (Entry.getValue() != -1)
107 return true;
108 Entry.setValue((int)i);
109 return false;
110 }
111 /// \returns true if name is not present in the map
112 bool lookupSection(StringRef SecName, unsigned &Idx) const {
113 StringMap<int>::const_iterator I = Map.find(SecName);
114 if (I == Map.end())
115 return true;
116 Idx = I->getValue();
117 return false;
118 }
119};
Sean Silva2a74f702013-06-15 00:31:46 +0000120} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +0000121
Sean Silva38205932013-06-13 22:19:48 +0000122template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +0000123static size_t arrayDataSize(ArrayRef<T> A) {
124 return A.size() * sizeof(T);
Sean Silva38205932013-06-13 22:19:48 +0000125}
126
127template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +0000128static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
129 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva38205932013-06-13 22:19:48 +0000130}
131
132template <class T>
133static void zero(T &Obj) {
134 memset(&Obj, 0, sizeof(Obj));
135}
136
Sean Silva85d3eeb2013-06-18 01:11:27 +0000137/// \brief Create a string table in `SHeader`, which we assume is already
138/// zero'd.
139template <class Elf_Shdr>
140static void createStringTableSectionHeader(Elf_Shdr &SHeader,
141 StringTableBuilder &STB,
142 ContiguousBlobAccumulator &CBA) {
143 SHeader.sh_type = ELF::SHT_STRTAB;
Sean Silvad93323f2013-06-22 00:47:43 +0000144 STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
Sean Silva85d3eeb2013-06-18 01:11:27 +0000145 SHeader.sh_size = STB.size();
Sean Silva85d3eeb2013-06-18 01:11:27 +0000146 SHeader.sh_addralign = 1;
147}
148
Sean Silva08a75ae2013-06-20 19:11:44 +0000149namespace {
150/// \brief "Single point of truth" for the ELF file construction.
151/// TODO: This class still has a ways to go before it is truly a "single
152/// point of truth".
153template <class ELFT>
154class ELFState {
155 /// \brief The future ".strtab" section.
156 StringTableBuilder DotStrtab;
157 /// \brief The section number of the ".strtab" section.
158 unsigned DotStrtabSecNo;
159 /// \brief The accumulated contents of all sections so far.
160 ContiguousBlobAccumulator &SectionContentAccum;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000161 typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
Sean Silva08a75ae2013-06-20 19:11:44 +0000162
Sean Silvac1c290b2013-06-20 20:59:34 +0000163 SectionNameToIdxMap &SN2I;
164
Sean Silva08a75ae2013-06-20 19:11:44 +0000165public:
Simon Atanasyan64dadc82014-03-14 06:53:21 +0000166 ELFState(ContiguousBlobAccumulator &Accum, unsigned DotStrtabSecNo_,
167 SectionNameToIdxMap &SN2I_)
Sean Silva08a75ae2013-06-20 19:11:44 +0000168 : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_),
Simon Atanasyan64dadc82014-03-14 06:53:21 +0000169 SectionContentAccum(Accum), SN2I(SN2I_) {}
Sean Silva08a75ae2013-06-20 19:11:44 +0000170
171 unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; }
172 StringTableBuilder &getStringTable() { return DotStrtab; }
173 ContiguousBlobAccumulator &getSectionContentAccum() {
174 return SectionContentAccum;
175 }
Sean Silvac1c290b2013-06-20 20:59:34 +0000176 SectionNameToIdxMap &getSN2I() { return SN2I; }
Sean Silva08a75ae2013-06-20 19:11:44 +0000177};
178} // end anonymous namespace
179
Sean Silvaaff51252013-06-21 00:27:50 +0000180// FIXME: At this point it is fairly clear that we need to refactor these
181// static functions into methods of a class sharing some typedefs. These
182// ELF type names are insane.
Sean Silva37e817c2013-06-21 00:33:01 +0000183template <class ELFT>
184static void
185addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, ELFState<ELFT> &State,
Michael J. Spencer126973b2013-08-08 22:27:13 +0000186 std::vector<typename object::ELFFile<ELFT>::Elf_Sym> &Syms,
Sean Silva37e817c2013-06-21 00:33:01 +0000187 unsigned SymbolBinding) {
Michael J. Spencer126973b2013-08-08 22:27:13 +0000188 typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Simon Atanasyan048baca2014-03-14 06:53:30 +0000189 for (const auto &Sym : Symbols) {
Sean Silva6b083882013-06-18 23:14:03 +0000190 Elf_Sym Symbol;
191 zero(Symbol);
192 if (!Sym.Name.empty())
Sean Silva08a75ae2013-06-20 19:11:44 +0000193 Symbol.st_name = State.getStringTable().addString(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000194 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000195 if (!Sym.Section.empty()) {
196 unsigned Index;
197 if (State.getSN2I().lookupSection(Sym.Section, Index)) {
198 errs() << "error: Unknown section referenced: '" << Sym.Section
199 << "' by YAML symbol " << Sym.Name << ".\n";
200 exit(1);
201 }
202 Symbol.st_shndx = Index;
203 } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000204 Symbol.st_value = Sym.Value;
205 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000206 Syms.push_back(Symbol);
207 }
Sean Silvaaff51252013-06-21 00:27:50 +0000208}
209
210template <class ELFT>
Michael J. Spencer126973b2013-08-08 22:27:13 +0000211static void
212handleSymtabSectionHeader(const ELFYAML::LocalGlobalWeakSymbols &Symbols,
213 ELFState<ELFT> &State,
214 typename object::ELFFile<ELFT>::Elf_Shdr &SHeader) {
Sean Silvaaff51252013-06-21 00:27:50 +0000215
Michael J. Spencer126973b2013-08-08 22:27:13 +0000216 typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Sean Silva82177572013-06-22 01:38:00 +0000217 SHeader.sh_type = ELF::SHT_SYMTAB;
Sean Silvaaff51252013-06-21 00:27:50 +0000218 SHeader.sh_link = State.getDotStrTabSecNo();
219 // One greater than symbol table index of the last local symbol.
Sean Silva7a0c3a62013-06-22 01:37:55 +0000220 SHeader.sh_info = Symbols.Local.size() + 1;
Sean Silvaaff51252013-06-21 00:27:50 +0000221 SHeader.sh_entsize = sizeof(Elf_Sym);
222
223 std::vector<Elf_Sym> Syms;
224 {
225 // Ensure STN_UNDEF is present
226 Elf_Sym Sym;
227 zero(Sym);
228 Syms.push_back(Sym);
229 }
Sean Silva7a0c3a62013-06-22 01:37:55 +0000230 addSymbols(Symbols.Local, State, Syms, ELF::STB_LOCAL);
231 addSymbols(Symbols.Global, State, Syms, ELF::STB_GLOBAL);
232 addSymbols(Symbols.Weak, State, Syms, ELF::STB_WEAK);
Sean Silva6b083882013-06-18 23:14:03 +0000233
Sean Silva08a75ae2013-06-20 19:11:44 +0000234 ContiguousBlobAccumulator &CBA = State.getSectionContentAccum();
Will Dietz0b48c732013-10-12 21:29:16 +0000235 writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
236 makeArrayRef(Syms));
237 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
Sean Silva6b083882013-06-18 23:14:03 +0000238}
239
Sean Silvaf99309c2013-06-10 23:44:15 +0000240template <class ELFT>
Sean Silva415d93f2013-06-17 20:14:59 +0000241static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Sean Silvaf99309c2013-06-10 23:44:15 +0000242 using namespace llvm::ELF;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000243 typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
244 typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Sean Silva38205932013-06-13 22:19:48 +0000245
246 const ELFYAML::FileHeader &Hdr = Doc.Header;
247
248 Elf_Ehdr Header;
249 zero(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000250 Header.e_ident[EI_MAG0] = 0x7f;
251 Header.e_ident[EI_MAG1] = 'E';
252 Header.e_ident[EI_MAG2] = 'L';
253 Header.e_ident[EI_MAG3] = 'F';
254 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
255 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
256 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
Sean Silvaf99309c2013-06-10 23:44:15 +0000257 Header.e_ident[EI_VERSION] = EV_CURRENT;
Sean Silvab3a013a2013-06-19 01:10:58 +0000258 Header.e_ident[EI_OSABI] = Hdr.OSABI;
Sean Silvaf99309c2013-06-10 23:44:15 +0000259 Header.e_ident[EI_ABIVERSION] = 0;
260 Header.e_type = Hdr.Type;
261 Header.e_machine = Hdr.Machine;
262 Header.e_version = EV_CURRENT;
263 Header.e_entry = Hdr.Entry;
Sean Silva38205932013-06-13 22:19:48 +0000264 Header.e_ehsize = sizeof(Elf_Ehdr);
Sean Silvaf99309c2013-06-10 23:44:15 +0000265
Sean Silva38205932013-06-13 22:19:48 +0000266 // TODO: Flesh out section header support.
267 // TODO: Program headers.
268
269 Header.e_shentsize = sizeof(Elf_Shdr);
270 // Immediately following the ELF header.
271 Header.e_shoff = sizeof(Header);
Sean Silvabdf19862013-06-18 23:37:23 +0000272 const std::vector<ELFYAML::Section> &Sections = Doc.Sections;
Sean Silva82177572013-06-22 01:38:00 +0000273 // "+ 4" for
Sean Silvabdf19862013-06-18 23:37:23 +0000274 // - SHT_NULL entry (placed first, i.e. 0'th entry)
Sean Silva82177572013-06-22 01:38:00 +0000275 // - symbol table (.symtab) (placed third to last)
Sean Silvabdf19862013-06-18 23:37:23 +0000276 // - string table (.strtab) (placed second to last)
277 // - section header string table. (placed last)
Sean Silva82177572013-06-22 01:38:00 +0000278 Header.e_shnum = Sections.size() + 4;
Sean Silva38205932013-06-13 22:19:48 +0000279 // Place section header string table last.
Sean Silvabdf19862013-06-18 23:37:23 +0000280 Header.e_shstrndx = Header.e_shnum - 1;
281 const unsigned DotStrtabSecNo = Header.e_shnum - 2;
Sean Silva38205932013-06-13 22:19:48 +0000282
Sean Silva08a75ae2013-06-20 19:11:44 +0000283 // XXX: This offset is tightly coupled with the order that we write
284 // things to `OS`.
285 const size_t SectionContentBeginOffset =
286 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
287 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvaa6423eb2013-06-15 00:25:26 +0000288 SectionNameToIdxMap SN2I;
289 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
290 StringRef Name = Sections[i].Name;
291 if (Name.empty())
292 continue;
Sean Silvabdf19862013-06-18 23:37:23 +0000293 // "+ 1" to take into account the SHT_NULL entry.
294 if (SN2I.addName(Name, i + 1)) {
Sean Silvaa6423eb2013-06-15 00:25:26 +0000295 errs() << "error: Repeated section name: '" << Name
296 << "' at YAML section number " << i << ".\n";
Sean Silva415d93f2013-06-17 20:14:59 +0000297 return 1;
Sean Silvaa6423eb2013-06-15 00:25:26 +0000298 }
299 }
300
Simon Atanasyan64dadc82014-03-14 06:53:21 +0000301 ELFState<ELFT> State(CBA, DotStrtabSecNo, SN2I);
Sean Silvac1c290b2013-06-20 20:59:34 +0000302
Sean Silvafde4ab02013-06-18 01:11:24 +0000303 StringTableBuilder SHStrTab;
Sean Silva38205932013-06-13 22:19:48 +0000304 std::vector<Elf_Shdr> SHeaders;
Sean Silvabdf19862013-06-18 23:37:23 +0000305 {
306 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
307 // valid SHN_UNDEF entry since SHT_NULL == 0.
308 Elf_Shdr SHdr;
309 zero(SHdr);
310 SHeaders.push_back(SHdr);
311 }
Simon Atanasyan048baca2014-03-14 06:53:30 +0000312 for (const auto &Sec : Sections) {
Sean Silva38205932013-06-13 22:19:48 +0000313 Elf_Shdr SHeader;
314 zero(SHeader);
Sean Silvafde4ab02013-06-18 01:11:24 +0000315 SHeader.sh_name = SHStrTab.addString(Sec.Name);
Sean Silva38205932013-06-13 22:19:48 +0000316 SHeader.sh_type = Sec.Type;
317 SHeader.sh_flags = Sec.Flags;
Sean Silvaf4bfced2013-06-13 22:19:54 +0000318 SHeader.sh_addr = Sec.Address;
Sean Silva46dffff2013-06-13 22:20:01 +0000319
Sean Silvad93323f2013-06-22 00:47:43 +0000320 Sec.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
Sean Silva46dffff2013-06-13 22:20:01 +0000321 SHeader.sh_size = Sec.Content.binary_size();
Sean Silva46dffff2013-06-13 22:20:01 +0000322
Sean Silvaa6423eb2013-06-15 00:25:26 +0000323 if (!Sec.Link.empty()) {
324 unsigned Index;
325 if (SN2I.lookupSection(Sec.Link, Index)) {
326 errs() << "error: Unknown section referenced: '" << Sec.Link
Simon Atanasyan4d70d2a2014-03-14 06:53:25 +0000327 << "' at YAML section '" << Sec.Name << "'.\n";
Sean Silva415d93f2013-06-17 20:14:59 +0000328 return 1;
Sean Silvaa6423eb2013-06-15 00:25:26 +0000329 }
330 SHeader.sh_link = Index;
331 }
Sean Silva38205932013-06-13 22:19:48 +0000332 SHeader.sh_info = 0;
Sean Silva0a409cf2013-06-14 00:38:02 +0000333 SHeader.sh_addralign = Sec.AddressAlign;
Sean Silva38205932013-06-13 22:19:48 +0000334 SHeader.sh_entsize = 0;
335 SHeaders.push_back(SHeader);
336 }
337
Sean Silva82177572013-06-22 01:38:00 +0000338 // .symtab section.
339 Elf_Shdr SymtabSHeader;
340 zero(SymtabSHeader);
341 SymtabSHeader.sh_name = SHStrTab.addString(StringRef(".symtab"));
342 handleSymtabSectionHeader<ELFT>(Doc.Symbols, State, SymtabSHeader);
343 SHeaders.push_back(SymtabSHeader);
344
Sean Silva6b083882013-06-18 23:14:03 +0000345 // .strtab string table header.
Sean Silvac3131922013-06-18 21:37:50 +0000346 Elf_Shdr DotStrTabSHeader;
347 zero(DotStrTabSHeader);
348 DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab"));
Sean Silva08a75ae2013-06-20 19:11:44 +0000349 createStringTableSectionHeader(DotStrTabSHeader, State.getStringTable(), CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000350 SHeaders.push_back(DotStrTabSHeader);
Sean Silvac3131922013-06-18 21:37:50 +0000351
Sean Silvafde4ab02013-06-18 01:11:24 +0000352 // Section header string table header.
353 Elf_Shdr SHStrTabSHeader;
354 zero(SHStrTabSHeader);
Simon Atanasyana3130a42014-03-14 06:53:16 +0000355 SHStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".shstrtab"));
Sean Silva85d3eeb2013-06-18 01:11:27 +0000356 createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000357 SHeaders.push_back(SHStrTabSHeader);
Sean Silvaf99309c2013-06-10 23:44:15 +0000358
359 OS.write((const char *)&Header, sizeof(Header));
Will Dietz0b48c732013-10-12 21:29:16 +0000360 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +0000361 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000362 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000363}
364
Sean Silva11caeba2013-06-22 01:03:35 +0000365static bool is64Bit(const ELFYAML::Object &Doc) {
366 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
367}
368
369static bool isLittleEndian(const ELFYAML::Object &Doc) {
370 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
371}
372
Sean Silvaf99309c2013-06-10 23:44:15 +0000373int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
374 yaml::Input YIn(Buf->getBuffer());
375 ELFYAML::Object Doc;
376 YIn >> Doc;
377 if (YIn.error()) {
378 errs() << "yaml2obj: Failed to parse YAML file!\n";
379 return 1;
380 }
Sean Silva11caeba2013-06-22 01:03:35 +0000381 using object::ELFType;
382 typedef ELFType<support::little, 8, true> LE64;
383 typedef ELFType<support::big, 8, true> BE64;
384 typedef ELFType<support::little, 4, false> LE32;
385 typedef ELFType<support::big, 4, false> BE32;
386 if (is64Bit(Doc)) {
387 if (isLittleEndian(Doc))
388 return writeELF<LE64>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000389 else
Sean Silva11caeba2013-06-22 01:03:35 +0000390 return writeELF<BE64>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000391 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000392 if (isLittleEndian(Doc))
393 return writeELF<LE32>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000394 else
Sean Silva11caeba2013-06-22 01:03:35 +0000395 return writeELF<BE32>(outs(), Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000396 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000397}