blob: ccf1bc82865a45b8ddf213c7dcb34fd28b25307d [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// The ELF component of yaml2obj.
Sean Silvaf99309c2013-06-10 23:44:15 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "yaml2obj.h"
Will Dietz0b48c732013-10-12 21:29:16 +000016#include "llvm/ADT/ArrayRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/ELF.h"
Rafael Espindola97de4742014-07-03 02:01:39 +000018#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000019#include "llvm/Object/ELFObjectFile.h"
Rafael Espindolaebd91932016-03-01 19:15:06 +000020#include "llvm/ObjectYAML/ELFYAML.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000021#include "llvm/Support/MemoryBuffer.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000022#include "llvm/Support/WithColor.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000023#include "llvm/Support/YAMLTraits.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace llvm;
27
Sean Silva46dffff2013-06-13 22:20:01 +000028// This class is used to build up a contiguous binary blob while keeping
29// track of an offset in the output (which notionally begins at
30// `InitialOffset`).
Sean Silva2a74f702013-06-15 00:31:46 +000031namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000032class ContiguousBlobAccumulator {
33 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000034 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000035 raw_svector_ostream OS;
36
Sean Silvad93323f2013-06-22 00:47:43 +000037 /// \returns The new offset.
38 uint64_t padToAlignment(unsigned Align) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000039 if (Align == 0)
40 Align = 1;
Sean Silvad93323f2013-06-22 00:47:43 +000041 uint64_t CurrentOffset = InitialOffset + OS.tell();
Rui Ueyamada00f2f2016-01-14 21:06:47 +000042 uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
Sean Silvad93323f2013-06-22 00:47:43 +000043 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
44 OS.write('\0');
45 return AlignedOffset; // == CurrentOffset;
46 }
47
Sean Silva46dffff2013-06-13 22:20:01 +000048public:
Sean Silvabd3bc692013-06-20 19:11:41 +000049 ContiguousBlobAccumulator(uint64_t InitialOffset_)
50 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000051 template <class Integer>
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000052 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
Sean Silvad93323f2013-06-22 00:47:43 +000053 Offset = padToAlignment(Align);
54 return OS;
55 }
Sean Silva46dffff2013-06-13 22:20:01 +000056 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
57};
Sean Silva2a74f702013-06-15 00:31:46 +000058} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000059
Simon Atanasyan35babf92014-04-06 09:02:55 +000060// Used to keep track of section and symbol names, so that in the YAML file
61// sections and symbols can be referenced by name instead of by index.
Sean Silva2a74f702013-06-15 00:31:46 +000062namespace {
Simon Atanasyan35babf92014-04-06 09:02:55 +000063class NameToIdxMap {
Sean Silvaa6423eb2013-06-15 00:25:26 +000064 StringMap<int> Map;
65public:
66 /// \returns true if name is already present in the map.
Simon Atanasyan35babf92014-04-06 09:02:55 +000067 bool addName(StringRef Name, unsigned i) {
David Blaikie5106ce72014-11-19 05:49:42 +000068 return !Map.insert(std::make_pair(Name, (int)i)).second;
Sean Silvaa6423eb2013-06-15 00:25:26 +000069 }
70 /// \returns true if name is not present in the map
Simon Atanasyan35babf92014-04-06 09:02:55 +000071 bool lookup(StringRef Name, unsigned &Idx) const {
72 StringMap<int>::const_iterator I = Map.find(Name);
Sean Silvaa6423eb2013-06-15 00:25:26 +000073 if (I == Map.end())
74 return true;
75 Idx = I->getValue();
76 return false;
77 }
Dave Lee17307d92017-11-09 14:53:43 +000078 /// asserts if name is not present in the map
79 unsigned get(StringRef Name) const {
80 unsigned Idx = 0;
81 auto missing = lookup(Name, Idx);
82 (void)missing;
83 assert(!missing && "Expected section not found in index");
84 return Idx;
85 }
86 unsigned size() const { return Map.size(); }
Sean Silvaa6423eb2013-06-15 00:25:26 +000087};
Sean Silva2a74f702013-06-15 00:31:46 +000088} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +000089
Sean Silva38205932013-06-13 22:19:48 +000090template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000091static size_t arrayDataSize(ArrayRef<T> A) {
92 return A.size() * sizeof(T);
Sean Silva38205932013-06-13 22:19:48 +000093}
94
95template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000096static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
97 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva38205932013-06-13 22:19:48 +000098}
99
100template <class T>
101static void zero(T &Obj) {
102 memset(&Obj, 0, sizeof(Obj));
103}
104
Sean Silva08a75ae2013-06-20 19:11:44 +0000105namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000106/// "Single point of truth" for the ELF file construction.
Sean Silva08a75ae2013-06-20 19:11:44 +0000107/// TODO: This class still has a ways to go before it is truly a "single
108/// point of truth".
109template <class ELFT>
110class ELFState {
Rui Ueyama478d6352018-01-12 02:28:31 +0000111 typedef typename ELFT::Ehdr Elf_Ehdr;
112 typedef typename ELFT::Phdr Elf_Phdr;
113 typedef typename ELFT::Shdr Elf_Shdr;
114 typedef typename ELFT::Sym Elf_Sym;
115 typedef typename ELFT::Rel Elf_Rel;
116 typedef typename ELFT::Rela Elf_Rela;
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000117 typedef typename ELFT::Relr Elf_Relr;
Paul Semel1dbbfba2018-07-23 18:49:04 +0000118 typedef typename ELFT::Dyn Elf_Dyn;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000119
Dave Lee67b49662017-11-16 18:10:15 +0000120 enum class SymtabType { Static, Dynamic };
121
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000122 /// The future ".strtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000123 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva08a75ae2013-06-20 19:11:44 +0000124
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000125 /// The future ".shstrtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000126 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000127
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000128 /// The future ".dynstr" section.
Dave Lee67b49662017-11-16 18:10:15 +0000129 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
130
Simon Atanasyan35babf92014-04-06 09:02:55 +0000131 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000132 NameToIdxMap SymN2I;
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000133 const ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000134
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000135 bool buildSectionIndex();
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000136 bool buildSymbolIndex(std::size_t &StartIndex,
137 const std::vector<ELFYAML::Symbol> &Symbols);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000138 void initELFHeader(Elf_Ehdr &Header);
Petr Hosekeb04da32017-07-19 20:38:46 +0000139 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000140 bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
141 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000142 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000143 ContiguousBlobAccumulator &CBA);
144 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
145 StringTableBuilder &STB,
146 ContiguousBlobAccumulator &CBA);
Petr Hosekeb04da32017-07-19 20:38:46 +0000147 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
148 std::vector<Elf_Shdr> &SHeaders);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000149 void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
Dave Lee67b49662017-11-16 18:10:15 +0000150 std::vector<Elf_Sym> &Syms, unsigned SymbolBinding,
151 const StringTableBuilder &Strtab);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000152 void writeSectionContent(Elf_Shdr &SHeader,
153 const ELFYAML::RawContentSection &Section,
154 ContiguousBlobAccumulator &CBA);
155 bool writeSectionContent(Elf_Shdr &SHeader,
156 const ELFYAML::RelocationSection &Section,
157 ContiguousBlobAccumulator &CBA);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000158 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
159 ContiguousBlobAccumulator &CBA);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000160 bool writeSectionContent(Elf_Shdr &SHeader,
161 const ELFYAML::MipsABIFlags &Section,
162 ContiguousBlobAccumulator &CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000163 bool hasDynamicSymbols() const;
164 SmallVector<const char *, 5> implicitSectionNames() const;
Sean Silva08a75ae2013-06-20 19:11:44 +0000165
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000166 // - SHT_NULL entry (placed first, i.e. 0'th entry)
Dave Lee67b49662017-11-16 18:10:15 +0000167 // - symbol table (.symtab) (defaults to after last yaml section)
168 // - string table (.strtab) (defaults to after .symtab)
169 // - section header string table (.shstrtab) (defaults to after .strtab)
170 // - dynamic symbol table (.dynsym) (defaults to after .shstrtab)
171 // - dynamic string table (.dynstr) (defaults to after .dynsym)
Dave Lee17307d92017-11-09 14:53:43 +0000172 unsigned getDotSymTabSecNo() const { return SN2I.get(".symtab"); }
173 unsigned getDotStrTabSecNo() const { return SN2I.get(".strtab"); }
174 unsigned getDotShStrTabSecNo() const { return SN2I.get(".shstrtab"); }
Dave Lee67b49662017-11-16 18:10:15 +0000175 unsigned getDotDynSymSecNo() const { return SN2I.get(".dynsym"); }
176 unsigned getDotDynStrSecNo() const { return SN2I.get(".dynstr"); }
Dave Lee17307d92017-11-09 14:53:43 +0000177 unsigned getSectionCount() const { return SN2I.size() + 1; }
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000178
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000179 ELFState(const ELFYAML::Object &D) : Doc(D) {}
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000180
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000181public:
182 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
Sean Silva08a75ae2013-06-20 19:11:44 +0000183};
184} // end anonymous namespace
185
Sean Silva37e817c2013-06-21 00:33:01 +0000186template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000187void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
188 using namespace llvm::ELF;
189 zero(Header);
190 Header.e_ident[EI_MAG0] = 0x7f;
191 Header.e_ident[EI_MAG1] = 'E';
192 Header.e_ident[EI_MAG2] = 'L';
193 Header.e_ident[EI_MAG3] = 'F';
194 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
195 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
196 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
197 Header.e_ident[EI_VERSION] = EV_CURRENT;
198 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
199 Header.e_ident[EI_ABIVERSION] = 0;
200 Header.e_type = Doc.Header.Type;
201 Header.e_machine = Doc.Header.Machine;
202 Header.e_version = EV_CURRENT;
203 Header.e_entry = Doc.Header.Entry;
Petr Hosekeb04da32017-07-19 20:38:46 +0000204 Header.e_phoff = sizeof(Header);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000205 Header.e_flags = Doc.Header.Flags;
206 Header.e_ehsize = sizeof(Elf_Ehdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000207 Header.e_phentsize = sizeof(Elf_Phdr);
208 Header.e_phnum = Doc.ProgramHeaders.size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000209 Header.e_shentsize = sizeof(Elf_Shdr);
Petr Hosekeb04da32017-07-19 20:38:46 +0000210 // Immediately following the ELF header and program headers.
211 Header.e_shoff =
212 sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000213 Header.e_shnum = getSectionCount();
214 Header.e_shstrndx = getDotShStrTabSecNo();
215}
216
217template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000218void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
219 for (const auto &YamlPhdr : Doc.ProgramHeaders) {
220 Elf_Phdr Phdr;
221 Phdr.p_type = YamlPhdr.Type;
222 Phdr.p_flags = YamlPhdr.Flags;
223 Phdr.p_vaddr = YamlPhdr.VAddr;
224 Phdr.p_paddr = YamlPhdr.PAddr;
225 PHeaders.push_back(Phdr);
226 }
227}
228
229template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000230bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
231 ContiguousBlobAccumulator &CBA) {
232 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
233 // valid SHN_UNDEF entry since SHT_NULL == 0.
234 Elf_Shdr SHeader;
235 zero(SHeader);
236 SHeaders.push_back(SHeader);
237
238 for (const auto &Sec : Doc.Sections) {
239 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000240 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000241 SHeader.sh_type = Sec->Type;
242 SHeader.sh_flags = Sec->Flags;
243 SHeader.sh_addr = Sec->Address;
244 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000245
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000246 if (!Sec->Link.empty()) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000247 unsigned Index;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000248 if (SN2I.lookup(Sec->Link, Index)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000249 WithColor::error() << "Unknown section referenced: '" << Sec->Link
250 << "' at YAML section '" << Sec->Name << "'.\n";
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000251 return false;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000252 }
253 SHeader.sh_link = Index;
254 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000255
256 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
257 writeSectionContent(SHeader, *S, CBA);
258 else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
259 if (S->Link.empty())
260 // For relocation section set link to .symtab by default.
261 SHeader.sh_link = getDotSymTabSecNo();
262
263 unsigned Index;
264 if (SN2I.lookup(S->Info, Index)) {
Michael J. Spencer572d7422015-08-27 23:11:20 +0000265 if (S->Info.getAsInteger(0, Index)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000266 WithColor::error() << "Unknown section referenced: '" << S->Info
267 << "' at YAML section '" << S->Name << "'.\n";
Michael J. Spencer572d7422015-08-27 23:11:20 +0000268 return false;
269 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000270 }
271 SHeader.sh_info = Index;
272
273 if (!writeSectionContent(SHeader, *S, CBA))
274 return false;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000275 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
276 unsigned SymIdx;
277 if (SymN2I.lookup(S->Info, SymIdx)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000278 WithColor::error() << "Unknown symbol referenced: '" << S->Info
279 << "' at YAML section '" << S->Name << "'.\n";
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000280 return false;
281 }
282 SHeader.sh_info = SymIdx;
283 if (!writeSectionContent(SHeader, *S, CBA))
284 return false;
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000285 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
286 if (!writeSectionContent(SHeader, *S, CBA))
287 return false;
Simon Atanasyan5db02762015-07-03 23:00:54 +0000288 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
289 SHeader.sh_entsize = 0;
290 SHeader.sh_size = S->Size;
291 // SHT_NOBITS section does not have content
292 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000293 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000294 } else
295 llvm_unreachable("Unknown section type");
296
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000297 SHeaders.push_back(SHeader);
298 }
299 return true;
300}
301
302template <class ELFT>
303void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
Dave Lee67b49662017-11-16 18:10:15 +0000304 SymtabType STType,
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000305 ContiguousBlobAccumulator &CBA) {
306 zero(SHeader);
Dave Lee67b49662017-11-16 18:10:15 +0000307 bool IsStatic = STType == SymtabType::Static;
308 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
309 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
310 SHeader.sh_link = IsStatic ? getDotStrTabSecNo() : getDotDynStrSecNo();
311 const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
312 auto &Strtab = IsStatic ? DotStrtab : DotDynstr;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000313 // One greater than symbol table index of the last local symbol.
Dave Lee67b49662017-11-16 18:10:15 +0000314 SHeader.sh_info = Symbols.Local.size() + 1;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000315 SHeader.sh_entsize = sizeof(Elf_Sym);
Simon Atanasyan3a120922015-07-09 18:23:02 +0000316 SHeader.sh_addralign = 8;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000317
318 std::vector<Elf_Sym> Syms;
319 {
320 // Ensure STN_UNDEF is present
321 Elf_Sym Sym;
322 zero(Sym);
323 Syms.push_back(Sym);
324 }
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000325
Dave Lee67b49662017-11-16 18:10:15 +0000326 // Add symbol names to .strtab or .dynstr.
327 for (const auto &Sym : Symbols.Local)
328 Strtab.add(Sym.Name);
329 for (const auto &Sym : Symbols.Global)
330 Strtab.add(Sym.Name);
331 for (const auto &Sym : Symbols.Weak)
332 Strtab.add(Sym.Name);
333 Strtab.finalize();
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000334
Dave Lee67b49662017-11-16 18:10:15 +0000335 addSymbols(Symbols.Local, Syms, ELF::STB_LOCAL, Strtab);
336 addSymbols(Symbols.Global, Syms, ELF::STB_GLOBAL, Strtab);
337 addSymbols(Symbols.Weak, Syms, ELF::STB_WEAK, Strtab);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000338
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000339 writeArrayData(
340 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
341 makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000342 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
343}
344
345template <class ELFT>
346void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
347 StringTableBuilder &STB,
348 ContiguousBlobAccumulator &CBA) {
349 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000350 SHeader.sh_name = DotShStrtab.getOffset(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000351 SHeader.sh_type = ELF::SHT_STRTAB;
Rafael Espindola39751af2016-10-04 22:43:25 +0000352 STB.write(CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign));
353 SHeader.sh_size = STB.getSize();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000354 SHeader.sh_addralign = 1;
355}
356
357template <class ELFT>
Petr Hosekeb04da32017-07-19 20:38:46 +0000358void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
359 std::vector<Elf_Shdr> &SHeaders) {
360 uint32_t PhdrIdx = 0;
361 for (auto &YamlPhdr : Doc.ProgramHeaders) {
362 auto &PHeader = PHeaders[PhdrIdx++];
363
364 if (YamlPhdr.Sections.size())
365 PHeader.p_offset = UINT32_MAX;
366 else
367 PHeader.p_offset = 0;
368
369 // Find the minimum offset for the program header.
370 for (auto SecName : YamlPhdr.Sections) {
371 uint32_t Index = 0;
372 SN2I.lookup(SecName.Section, Index);
373 const auto &SHeader = SHeaders[Index];
374 PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset);
375 }
376
377 // Find the maximum offset of the end of a section in order to set p_filesz.
378 PHeader.p_filesz = 0;
379 for (auto SecName : YamlPhdr.Sections) {
380 uint32_t Index = 0;
381 SN2I.lookup(SecName.Section, Index);
382 const auto &SHeader = SHeaders[Index];
383 uint64_t EndOfSection;
384 if (SHeader.sh_type == llvm::ELF::SHT_NOBITS)
385 EndOfSection = SHeader.sh_offset;
386 else
387 EndOfSection = SHeader.sh_offset + SHeader.sh_size;
388 uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
389 EndOfSegment = std::max(EndOfSegment, EndOfSection);
390 PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
391 }
392
393 // Find the memory size by adding the size of sections at the end of the
394 // segment. These should be empty (size of zero) and NOBITS sections.
395 PHeader.p_memsz = PHeader.p_filesz;
396 for (auto SecName : YamlPhdr.Sections) {
397 uint32_t Index = 0;
398 SN2I.lookup(SecName.Section, Index);
399 const auto &SHeader = SHeaders[Index];
400 if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz)
401 PHeader.p_memsz += SHeader.sh_size;
402 }
403
404 // Set the alignment of the segment to be the same as the maximum alignment
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000405 // of the sections with the same offset so that by default the segment
Petr Hosekeb04da32017-07-19 20:38:46 +0000406 // has a valid and sensible alignment.
Jake Ehrlich03aeeb092017-11-01 23:14:48 +0000407 if (YamlPhdr.Align) {
408 PHeader.p_align = *YamlPhdr.Align;
409 } else {
410 PHeader.p_align = 1;
411 for (auto SecName : YamlPhdr.Sections) {
412 uint32_t Index = 0;
413 SN2I.lookup(SecName.Section, Index);
414 const auto &SHeader = SHeaders[Index];
415 if (SHeader.sh_offset == PHeader.p_offset)
416 PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign);
417 }
Petr Hosekeb04da32017-07-19 20:38:46 +0000418 }
419 }
420}
421
422template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000423void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
424 std::vector<Elf_Sym> &Syms,
Dave Lee67b49662017-11-16 18:10:15 +0000425 unsigned SymbolBinding,
426 const StringTableBuilder &Strtab) {
Simon Atanasyan048baca2014-03-14 06:53:30 +0000427 for (const auto &Sym : Symbols) {
Sean Silva6b083882013-06-18 23:14:03 +0000428 Elf_Sym Symbol;
429 zero(Symbol);
430 if (!Sym.Name.empty())
Dave Lee67b49662017-11-16 18:10:15 +0000431 Symbol.st_name = Strtab.getOffset(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000432 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000433 if (!Sym.Section.empty()) {
434 unsigned Index;
Simon Atanasyan35babf92014-04-06 09:02:55 +0000435 if (SN2I.lookup(Sym.Section, Index)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000436 WithColor::error() << "Unknown section referenced: '" << Sym.Section
437 << "' by YAML symbol " << Sym.Name << ".\n";
Sean Silvac4afa6d2013-06-21 01:11:48 +0000438 exit(1);
439 }
440 Symbol.st_shndx = Index;
Petr Hosek5c469a32017-09-07 20:44:16 +0000441 } else if (Sym.Index) {
442 Symbol.st_shndx = *Sym.Index;
443 }
444 // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000445 Symbol.st_value = Sym.Value;
Simon Atanasyan60e1a792014-11-06 22:46:24 +0000446 Symbol.st_other = Sym.Other;
Sean Silva05001b92013-06-20 20:59:47 +0000447 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000448 Syms.push_back(Symbol);
449 }
Sean Silvaaff51252013-06-21 00:27:50 +0000450}
451
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000452template <class ELFT>
453void
454ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
455 const ELFYAML::RawContentSection &Section,
456 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000457 assert(Section.Size >= Section.Content.binary_size() &&
458 "Section size and section content are inconsistent");
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000459 raw_ostream &OS =
460 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000461 Section.Content.writeAsBinary(OS);
462 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
463 OS.write(0);
George Rimar65a68282018-08-07 08:11:38 +0000464 if (Section.EntSize)
465 SHeader.sh_entsize = *Section.EntSize;
466 else if (Section.Type == llvm::ELF::SHT_RELR)
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000467 SHeader.sh_entsize = sizeof(Elf_Relr);
Paul Semel1dbbfba2018-07-23 18:49:04 +0000468 else if (Section.Type == llvm::ELF::SHT_DYNAMIC)
469 SHeader.sh_entsize = sizeof(Elf_Dyn);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000470 else
471 SHeader.sh_entsize = 0;
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000472 SHeader.sh_size = Section.Size;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000473}
474
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000475static bool isMips64EL(const ELFYAML::Object &Doc) {
476 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
477 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
478 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
479}
480
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000481template <class ELFT>
482bool
483ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
484 const ELFYAML::RelocationSection &Section,
485 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000486 assert((Section.Type == llvm::ELF::SHT_REL ||
487 Section.Type == llvm::ELF::SHT_RELA) &&
488 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000489
490 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
491 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
492 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
493
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000494 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000495
496 for (const auto &Rel : Section.Relocations) {
Adhemerval Zanella9f3dbff2015-04-22 15:26:43 +0000497 unsigned SymIdx = 0;
498 // Some special relocation, R_ARM_v4BX for instance, does not have
499 // an external reference. So it ignores the return value of lookup()
500 // here.
Petr Hosek5aa80f12017-08-30 23:13:31 +0000501 if (Rel.Symbol)
502 SymN2I.lookup(*Rel.Symbol, SymIdx);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000503
504 if (IsRela) {
505 Elf_Rela REntry;
506 zero(REntry);
507 REntry.r_offset = Rel.Offset;
508 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000509 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000510 OS.write((const char *)&REntry, sizeof(REntry));
511 } else {
512 Elf_Rel REntry;
513 zero(REntry);
514 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000515 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000516 OS.write((const char *)&REntry, sizeof(REntry));
517 }
518 }
519 return true;
520}
521
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000522template <class ELFT>
523bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
524 const ELFYAML::Group &Section,
525 ContiguousBlobAccumulator &CBA) {
Rui Ueyama478d6352018-01-12 02:28:31 +0000526 typedef typename ELFT::Word Elf_Word;
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000527 assert(Section.Type == llvm::ELF::SHT_GROUP &&
528 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000529
530 SHeader.sh_entsize = sizeof(Elf_Word);
531 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
532
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000533 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000534
535 for (auto member : Section.Members) {
536 Elf_Word SIdx;
537 unsigned int sectionIndex = 0;
538 if (member.sectionNameOrType == "GRP_COMDAT")
539 sectionIndex = llvm::ELF::GRP_COMDAT;
George Rimar5290af82018-08-15 11:43:00 +0000540 else if (SN2I.lookup(member.sectionNameOrType, sectionIndex) &&
541 !to_integer(member.sectionNameOrType, sectionIndex)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000542 WithColor::error() << "Unknown section referenced: '"
543 << member.sectionNameOrType << "' at YAML section' "
544 << Section.Name << "\n";
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000545 return false;
546 }
547 SIdx = sectionIndex;
548 OS.write((const char *)&SIdx, sizeof(SIdx));
549 }
550 return true;
551}
552
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000553template <class ELFT>
554bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
555 const ELFYAML::MipsABIFlags &Section,
556 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000557 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
558 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000559
560 object::Elf_Mips_ABIFlags<ELFT> Flags;
561 zero(Flags);
562 SHeader.sh_entsize = sizeof(Flags);
563 SHeader.sh_size = SHeader.sh_entsize;
564
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000565 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000566 Flags.version = Section.Version;
567 Flags.isa_level = Section.ISALevel;
568 Flags.isa_rev = Section.ISARevision;
569 Flags.gpr_size = Section.GPRSize;
570 Flags.cpr1_size = Section.CPR1Size;
571 Flags.cpr2_size = Section.CPR2Size;
572 Flags.fp_abi = Section.FpABI;
573 Flags.isa_ext = Section.ISAExtension;
574 Flags.ases = Section.ASEs;
575 Flags.flags1 = Section.Flags1;
576 Flags.flags2 = Section.Flags2;
577 OS.write((const char *)&Flags, sizeof(Flags));
578
579 return true;
580}
581
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000582template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000583 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000584 StringRef Name = Doc.Sections[i]->Name;
Dave Lee17307d92017-11-09 14:53:43 +0000585 DotShStrtab.add(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000586 // "+ 1" to take into account the SHT_NULL entry.
587 if (SN2I.addName(Name, i + 1)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000588 WithColor::error() << "Repeated section name: '" << Name
589 << "' at YAML section number " << i << ".\n";
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000590 return false;
591 }
Sean Silvaaff51252013-06-21 00:27:50 +0000592 }
Dave Lee17307d92017-11-09 14:53:43 +0000593
594 auto SecNo = 1 + Doc.Sections.size();
595 // Add special sections after input sections, if necessary.
Dave Lee67b49662017-11-16 18:10:15 +0000596 for (const auto &Name : implicitSectionNames())
Dave Lee17307d92017-11-09 14:53:43 +0000597 if (!SN2I.addName(Name, SecNo)) {
598 // Account for this section, since it wasn't in the Doc
599 ++SecNo;
600 DotShStrtab.add(Name);
601 }
602
603 DotShStrtab.finalize();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000604 return true;
Sean Silva6b083882013-06-18 23:14:03 +0000605}
606
Sean Silvaf99309c2013-06-10 23:44:15 +0000607template <class ELFT>
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000608bool
609ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
610 const std::vector<ELFYAML::Symbol> &Symbols) {
611 for (const auto &Sym : Symbols) {
612 ++StartIndex;
613 if (Sym.Name.empty())
614 continue;
615 if (SymN2I.addName(Sym.Name, StartIndex)) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000616 WithColor::error() << "Repeated symbol name: '" << Sym.Name << "'.\n";
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000617 return false;
618 }
619 }
620 return true;
621}
622
623template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000624int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000625 ELFState<ELFT> State(Doc);
626 if (!State.buildSectionIndex())
627 return 1;
628
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000629 std::size_t StartSymIndex = 0;
630 if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
631 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
632 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
633 return 1;
634
Sean Silva38205932013-06-13 22:19:48 +0000635 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000636 State.initELFHeader(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000637
Sean Silva38205932013-06-13 22:19:48 +0000638 // TODO: Flesh out section header support.
Petr Hosekeb04da32017-07-19 20:38:46 +0000639
640 std::vector<Elf_Phdr> PHeaders;
641 State.initProgramHeaders(PHeaders);
Sean Silva38205932013-06-13 22:19:48 +0000642
Sean Silva08a75ae2013-06-20 19:11:44 +0000643 // XXX: This offset is tightly coupled with the order that we write
644 // things to `OS`.
Petr Hosekeb04da32017-07-19 20:38:46 +0000645 const size_t SectionContentBeginOffset = Header.e_ehsize +
646 Header.e_phentsize * Header.e_phnum +
647 Header.e_shentsize * Header.e_shnum;
Sean Silva08a75ae2013-06-20 19:11:44 +0000648 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +0000649
Sean Silva38205932013-06-13 22:19:48 +0000650 std::vector<Elf_Shdr> SHeaders;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000651 if(!State.initSectionHeaders(SHeaders, CBA))
652 return 1;
Sean Silva38205932013-06-13 22:19:48 +0000653
Dave Lee17307d92017-11-09 14:53:43 +0000654 // Populate SHeaders with implicit sections not present in the Doc
Dave Lee67b49662017-11-16 18:10:15 +0000655 for (const auto &Name : State.implicitSectionNames())
Dave Lee17307d92017-11-09 14:53:43 +0000656 if (State.SN2I.get(Name) >= SHeaders.size())
657 SHeaders.push_back({});
Sean Silva82177572013-06-22 01:38:00 +0000658
Dave Lee17307d92017-11-09 14:53:43 +0000659 // Initialize the implicit sections
660 auto Index = State.SN2I.get(".symtab");
Dave Lee67b49662017-11-16 18:10:15 +0000661 State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Static, CBA);
Dave Lee17307d92017-11-09 14:53:43 +0000662 Index = State.SN2I.get(".strtab");
663 State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA);
664 Index = State.SN2I.get(".shstrtab");
665 State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA);
Dave Lee67b49662017-11-16 18:10:15 +0000666 if (State.hasDynamicSymbols()) {
667 Index = State.SN2I.get(".dynsym");
668 State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Dynamic, CBA);
669 SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
670 Index = State.SN2I.get(".dynstr");
671 State.initStrtabSectionHeader(SHeaders[Index], ".dynstr", State.DotDynstr, CBA);
672 SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
673 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000674
Petr Hosekeb04da32017-07-19 20:38:46 +0000675 // Now we can decide segment offsets
676 State.setProgramHeaderLayout(PHeaders, SHeaders);
677
Sean Silvaf99309c2013-06-10 23:44:15 +0000678 OS.write((const char *)&Header, sizeof(Header));
Petr Hosekeb04da32017-07-19 20:38:46 +0000679 writeArrayData(OS, makeArrayRef(PHeaders));
Will Dietz0b48c732013-10-12 21:29:16 +0000680 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +0000681 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000682 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000683}
684
Dave Lee67b49662017-11-16 18:10:15 +0000685template <class ELFT> bool ELFState<ELFT>::hasDynamicSymbols() const {
686 return Doc.DynamicSymbols.Global.size() > 0 ||
687 Doc.DynamicSymbols.Weak.size() > 0 ||
688 Doc.DynamicSymbols.Local.size() > 0;
689}
690
691template <class ELFT> SmallVector<const char *, 5> ELFState<ELFT>::implicitSectionNames() const {
692 if (!hasDynamicSymbols())
693 return {".symtab", ".strtab", ".shstrtab"};
694 return {".symtab", ".strtab", ".shstrtab", ".dynsym", ".dynstr"};
695}
696
Sean Silva11caeba2013-06-22 01:03:35 +0000697static bool is64Bit(const ELFYAML::Object &Doc) {
698 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
699}
700
701static bool isLittleEndian(const ELFYAML::Object &Doc) {
702 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
703}
704
Chris Bieneman8ff0c112016-06-27 19:53:53 +0000705int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
Sean Silva11caeba2013-06-22 01:03:35 +0000706 if (is64Bit(Doc)) {
707 if (isLittleEndian(Doc))
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000708 return ELFState<object::ELF64LE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000709 else
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000710 return ELFState<object::ELF64BE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000711 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000712 if (isLittleEndian(Doc))
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000713 return ELFState<object::ELF32LE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000714 else
Rui Ueyama1b31eb92018-01-12 01:40:32 +0000715 return ELFState<object::ELF32BE>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000716 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000717}