blob: 5e23bc6f0850cf763d11a2abedf52139d2896858 [file] [log] [blame]
Sean Silva5918b7a2013-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 Prantl26b584c2018-05-01 15:54:18 +000011/// The ELF component of yaml2obj.
Sean Silva5918b7a2013-06-10 23:44:15 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "yaml2obj.h"
Will Dietz6dd78932013-10-12 21:29:16 +000016#include "llvm/ADT/ArrayRef.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/ELF.h"
Rafael Espindola7413fef2014-07-03 02:01:39 +000018#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencer081a1942013-08-08 22:27:13 +000019#include "llvm/Object/ELFObjectFile.h"
Rafael Espindola18903ff2016-03-01 19:15:06 +000020#include "llvm/ObjectYAML/ELFYAML.h"
Sean Silva5918b7a2013-06-10 23:44:15 +000021#include "llvm/Support/MemoryBuffer.h"
Jonas Devlieghereae6875e2018-04-21 21:11:59 +000022#include "llvm/Support/WithColor.h"
Sean Silva5918b7a2013-06-10 23:44:15 +000023#include "llvm/Support/YAMLTraits.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace llvm;
27
Sean Silva2a7e79a2013-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 Silvaf3f35232013-06-15 00:31:46 +000031namespace {
Sean Silva2a7e79a2013-06-13 22:20:01 +000032class ContiguousBlobAccumulator {
33 const uint64_t InitialOffset;
Sean Silvab14f9722013-06-20 19:11:41 +000034 SmallVector<char, 128> Buf;
Sean Silva2a7e79a2013-06-13 22:20:01 +000035 raw_svector_ostream OS;
36
Sean Silvae7668842013-06-22 00:47:43 +000037 /// \returns The new offset.
38 uint64_t padToAlignment(unsigned Align) {
Simon Atanasyan080d7a82015-07-08 10:12:40 +000039 if (Align == 0)
40 Align = 1;
Sean Silvae7668842013-06-22 00:47:43 +000041 uint64_t CurrentOffset = InitialOffset + OS.tell();
Rui Ueyama3edb0ec2016-01-14 21:06:47 +000042 uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
Sean Silvae7668842013-06-22 00:47:43 +000043 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
44 OS.write('\0');
45 return AlignedOffset; // == CurrentOffset;
46 }
47
Sean Silva2a7e79a2013-06-13 22:20:01 +000048public:
Sean Silvab14f9722013-06-20 19:11:41 +000049 ContiguousBlobAccumulator(uint64_t InitialOffset_)
50 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvae7668842013-06-22 00:47:43 +000051 template <class Integer>
Simon Atanasyan080d7a82015-07-08 10:12:40 +000052 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
Sean Silvae7668842013-06-22 00:47:43 +000053 Offset = padToAlignment(Align);
54 return OS;
55 }
Sean Silva2a7e79a2013-06-13 22:20:01 +000056 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
57};
Sean Silvaf3f35232013-06-15 00:31:46 +000058} // end anonymous namespace
Sean Silva2a7e79a2013-06-13 22:20:01 +000059
Simon Atanasyancde97ea2014-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 Silvaf3f35232013-06-15 00:31:46 +000062namespace {
Simon Atanasyancde97ea2014-04-06 09:02:55 +000063class NameToIdxMap {
Sean Silvafe57e342013-06-15 00:25:26 +000064 StringMap<int> Map;
65public:
66 /// \returns true if name is already present in the map.
Simon Atanasyancde97ea2014-04-06 09:02:55 +000067 bool addName(StringRef Name, unsigned i) {
David Blaikie1d4f28c2014-11-19 05:49:42 +000068 return !Map.insert(std::make_pair(Name, (int)i)).second;
Sean Silvafe57e342013-06-15 00:25:26 +000069 }
70 /// \returns true if name is not present in the map
Simon Atanasyancde97ea2014-04-06 09:02:55 +000071 bool lookup(StringRef Name, unsigned &Idx) const {
72 StringMap<int>::const_iterator I = Map.find(Name);
Sean Silvafe57e342013-06-15 00:25:26 +000073 if (I == Map.end())
74 return true;
75 Idx = I->getValue();
76 return false;
77 }
Dave Lee9a5be1f2017-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 Silvafe57e342013-06-15 00:25:26 +000087};
Sean Silvaf3f35232013-06-15 00:31:46 +000088} // end anonymous namespace
Sean Silvafe57e342013-06-15 00:25:26 +000089
Sean Silva274264c2013-06-13 22:19:48 +000090template <class T>
Will Dietz6dd78932013-10-12 21:29:16 +000091static size_t arrayDataSize(ArrayRef<T> A) {
92 return A.size() * sizeof(T);
Sean Silva274264c2013-06-13 22:19:48 +000093}
94
95template <class T>
Will Dietz6dd78932013-10-12 21:29:16 +000096static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
97 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva274264c2013-06-13 22:19:48 +000098}
99
100template <class T>
101static void zero(T &Obj) {
102 memset(&Obj, 0, sizeof(Obj));
103}
104
Sean Silva0382b302013-06-20 19:11:44 +0000105namespace {
Adrian Prantl26b584c2018-05-01 15:54:18 +0000106/// "Single point of truth" for the ELF file construction.
Sean Silva0382b302013-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 Ueyama5a286a02018-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 Ehrlicheefdbb42018-06-28 21:07:34 +0000117 typedef typename ELFT::Relr Elf_Relr;
Paul Semelf3f578d2018-07-23 18:49:04 +0000118 typedef typename ELFT::Dyn Elf_Dyn;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000119
Dave Lee9c5b7992017-11-16 18:10:15 +0000120 enum class SymtabType { Static, Dynamic };
121
Adrian Prantl26b584c2018-05-01 15:54:18 +0000122 /// The future ".strtab" section.
Rafael Espindola715bcca2015-10-23 21:48:05 +0000123 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva0382b302013-06-20 19:11:44 +0000124
Adrian Prantl26b584c2018-05-01 15:54:18 +0000125 /// The future ".shstrtab" section.
Rafael Espindola715bcca2015-10-23 21:48:05 +0000126 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000127
Adrian Prantl26b584c2018-05-01 15:54:18 +0000128 /// The future ".dynstr" section.
Dave Lee9c5b7992017-11-16 18:10:15 +0000129 StringTableBuilder DotDynstr{StringTableBuilder::ELF};
130
Simon Atanasyancde97ea2014-04-06 09:02:55 +0000131 NameToIdxMap SN2I;
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000132 NameToIdxMap SymN2I;
Simon Atanasyan38ac43b12014-04-02 16:34:40 +0000133 const ELFYAML::Object &Doc;
Sean Silvac18f66e2013-06-20 20:59:34 +0000134
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000135 bool buildSectionIndex();
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000136 bool buildSymbolIndex(std::size_t &StartIndex,
137 const std::vector<ELFYAML::Symbol> &Symbols);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000138 void initELFHeader(Elf_Ehdr &Header);
Petr Hoseke7726ca2017-07-19 20:38:46 +0000139 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000140 bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
141 ContiguousBlobAccumulator &CBA);
Dave Lee9c5b7992017-11-16 18:10:15 +0000142 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000143 ContiguousBlobAccumulator &CBA);
144 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
145 StringTableBuilder &STB,
146 ContiguousBlobAccumulator &CBA);
Petr Hoseke7726ca2017-07-19 20:38:46 +0000147 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
148 std::vector<Elf_Shdr> &SHeaders);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000149 void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
Dave Lee9c5b7992017-11-16 18:10:15 +0000150 std::vector<Elf_Sym> &Syms, unsigned SymbolBinding,
151 const StringTableBuilder &Strtab);
Simon Atanasyaneb0c9092014-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 Easwaran58721742015-02-21 04:28:26 +0000158 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
159 ContiguousBlobAccumulator &CBA);
Simon Atanasyanbd58bdb2015-05-07 15:40:48 +0000160 bool writeSectionContent(Elf_Shdr &SHeader,
161 const ELFYAML::MipsABIFlags &Section,
162 ContiguousBlobAccumulator &CBA);
Dave Lee9c5b7992017-11-16 18:10:15 +0000163 bool hasDynamicSymbols() const;
164 SmallVector<const char *, 5> implicitSectionNames() const;
Sean Silva0382b302013-06-20 19:11:44 +0000165
Simon Atanasyan38ac43b12014-04-02 16:34:40 +0000166 // - SHT_NULL entry (placed first, i.e. 0'th entry)
Dave Lee9c5b7992017-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 Lee9a5be1f2017-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 Lee9c5b7992017-11-16 18:10:15 +0000175 unsigned getDotDynSymSecNo() const { return SN2I.get(".dynsym"); }
176 unsigned getDotDynStrSecNo() const { return SN2I.get(".dynstr"); }
Dave Lee9a5be1f2017-11-09 14:53:43 +0000177 unsigned getSectionCount() const { return SN2I.size() + 1; }
Simon Atanasyan38ac43b12014-04-02 16:34:40 +0000178
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000179 ELFState(const ELFYAML::Object &D) : Doc(D) {}
Simon Atanasyan38ac43b12014-04-02 16:34:40 +0000180
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000181public:
182 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
Sean Silva0382b302013-06-20 19:11:44 +0000183};
184} // end anonymous namespace
185
Sean Silva552d7cd2013-06-21 00:33:01 +0000186template <class ELFT>
Simon Atanasyanbe2ec9b2014-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;
George Rimar64002dd2018-12-20 10:43:49 +0000199 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000200 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 Hoseke7726ca2017-07-19 20:38:46 +0000204 Header.e_phoff = sizeof(Header);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000205 Header.e_flags = Doc.Header.Flags;
206 Header.e_ehsize = sizeof(Elf_Ehdr);
Petr Hoseke7726ca2017-07-19 20:38:46 +0000207 Header.e_phentsize = sizeof(Elf_Phdr);
208 Header.e_phnum = Doc.ProgramHeaders.size();
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000209 Header.e_shentsize = sizeof(Elf_Shdr);
Petr Hoseke7726ca2017-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 Atanasyanbe2ec9b2014-04-02 16:34:54 +0000213 Header.e_shnum = getSectionCount();
214 Header.e_shstrndx = getDotShStrTabSecNo();
215}
216
217template <class ELFT>
Petr Hoseke7726ca2017-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
Xing GUO53a9c2e2018-12-04 14:27:51 +0000229static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
230 StringRef IndexSrc, unsigned &IndexDest) {
231 if (SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
232 WithColor::error() << "Unknown section referenced: '" << IndexSrc
233 << "' at YAML section '" << SecName << "'.\n";
234 return false;
235 }
236 return true;
237}
238
Petr Hoseke7726ca2017-07-19 20:38:46 +0000239template <class ELFT>
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000240bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
241 ContiguousBlobAccumulator &CBA) {
242 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
243 // valid SHN_UNDEF entry since SHT_NULL == 0.
244 Elf_Shdr SHeader;
245 zero(SHeader);
246 SHeaders.push_back(SHeader);
247
248 for (const auto &Sec : Doc.Sections) {
249 zero(SHeader);
Hans Wennborg14d1db92014-04-30 19:38:09 +0000250 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000251 SHeader.sh_type = Sec->Type;
252 SHeader.sh_flags = Sec->Flags;
253 SHeader.sh_addr = Sec->Address;
254 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000255
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000256 if (!Sec->Link.empty()) {
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000257 unsigned Index;
Xing GUO53a9c2e2018-12-04 14:27:51 +0000258 if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000259 return false;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000260 SHeader.sh_link = Index;
261 }
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000262
263 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
264 writeSectionContent(SHeader, *S, CBA);
265 else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
266 if (S->Link.empty())
267 // For relocation section set link to .symtab by default.
268 SHeader.sh_link = getDotSymTabSecNo();
269
270 unsigned Index;
Xing GUO53a9c2e2018-12-04 14:27:51 +0000271 if (!convertSectionIndex(SN2I, S->Name, S->Info, Index))
George Rimard9d67112018-08-16 12:23:22 +0000272 return false;
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000273 SHeader.sh_info = Index;
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000274 if (!writeSectionContent(SHeader, *S, CBA))
275 return false;
Shankar Easwaran58721742015-02-21 04:28:26 +0000276 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
277 unsigned SymIdx;
George Rimar312924a2018-08-15 13:55:22 +0000278 if (SymN2I.lookup(S->Info, SymIdx) && !to_integer(S->Info, SymIdx)) {
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000279 WithColor::error() << "Unknown symbol referenced: '" << S->Info
280 << "' at YAML section '" << S->Name << "'.\n";
Shankar Easwaran58721742015-02-21 04:28:26 +0000281 return false;
282 }
283 SHeader.sh_info = SymIdx;
284 if (!writeSectionContent(SHeader, *S, CBA))
285 return false;
Simon Atanasyanbd58bdb2015-05-07 15:40:48 +0000286 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
287 if (!writeSectionContent(SHeader, *S, CBA))
288 return false;
Simon Atanasyanafc03402015-07-03 23:00:54 +0000289 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
290 SHeader.sh_entsize = 0;
291 SHeader.sh_size = S->Size;
292 // SHT_NOBITS section does not have content
293 // so just to setup the section offset.
Simon Atanasyan080d7a82015-07-08 10:12:40 +0000294 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000295 } else
296 llvm_unreachable("Unknown section type");
297
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000298 SHeaders.push_back(SHeader);
299 }
300 return true;
301}
302
303template <class ELFT>
304void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
Dave Lee9c5b7992017-11-16 18:10:15 +0000305 SymtabType STType,
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000306 ContiguousBlobAccumulator &CBA) {
307 zero(SHeader);
Dave Lee9c5b7992017-11-16 18:10:15 +0000308 bool IsStatic = STType == SymtabType::Static;
309 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
310 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
311 SHeader.sh_link = IsStatic ? getDotStrTabSecNo() : getDotDynStrSecNo();
312 const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
313 auto &Strtab = IsStatic ? DotStrtab : DotDynstr;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000314 // One greater than symbol table index of the last local symbol.
Dave Lee9c5b7992017-11-16 18:10:15 +0000315 SHeader.sh_info = Symbols.Local.size() + 1;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000316 SHeader.sh_entsize = sizeof(Elf_Sym);
Simon Atanasyanba8d9482015-07-09 18:23:02 +0000317 SHeader.sh_addralign = 8;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000318
319 std::vector<Elf_Sym> Syms;
320 {
321 // Ensure STN_UNDEF is present
322 Elf_Sym Sym;
323 zero(Sym);
324 Syms.push_back(Sym);
325 }
Hans Wennborg14d1db92014-04-30 19:38:09 +0000326
Dave Lee9c5b7992017-11-16 18:10:15 +0000327 // Add symbol names to .strtab or .dynstr.
328 for (const auto &Sym : Symbols.Local)
329 Strtab.add(Sym.Name);
330 for (const auto &Sym : Symbols.Global)
331 Strtab.add(Sym.Name);
332 for (const auto &Sym : Symbols.Weak)
333 Strtab.add(Sym.Name);
334 Strtab.finalize();
Hans Wennborg14d1db92014-04-30 19:38:09 +0000335
Dave Lee9c5b7992017-11-16 18:10:15 +0000336 addSymbols(Symbols.Local, Syms, ELF::STB_LOCAL, Strtab);
337 addSymbols(Symbols.Global, Syms, ELF::STB_GLOBAL, Strtab);
338 addSymbols(Symbols.Weak, Syms, ELF::STB_WEAK, Strtab);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000339
Simon Atanasyan080d7a82015-07-08 10:12:40 +0000340 writeArrayData(
341 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
342 makeArrayRef(Syms));
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000343 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
344}
345
346template <class ELFT>
347void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
348 StringTableBuilder &STB,
349 ContiguousBlobAccumulator &CBA) {
350 zero(SHeader);
Hans Wennborg14d1db92014-04-30 19:38:09 +0000351 SHeader.sh_name = DotShStrtab.getOffset(Name);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000352 SHeader.sh_type = ELF::SHT_STRTAB;
Rafael Espindola2638e452016-10-04 22:43:25 +0000353 STB.write(CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign));
354 SHeader.sh_size = STB.getSize();
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000355 SHeader.sh_addralign = 1;
356}
357
358template <class ELFT>
Petr Hoseke7726ca2017-07-19 20:38:46 +0000359void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
360 std::vector<Elf_Shdr> &SHeaders) {
361 uint32_t PhdrIdx = 0;
362 for (auto &YamlPhdr : Doc.ProgramHeaders) {
363 auto &PHeader = PHeaders[PhdrIdx++];
364
365 if (YamlPhdr.Sections.size())
366 PHeader.p_offset = UINT32_MAX;
367 else
368 PHeader.p_offset = 0;
369
370 // Find the minimum offset for the program header.
371 for (auto SecName : YamlPhdr.Sections) {
372 uint32_t Index = 0;
373 SN2I.lookup(SecName.Section, Index);
374 const auto &SHeader = SHeaders[Index];
375 PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset);
376 }
377
378 // Find the maximum offset of the end of a section in order to set p_filesz.
379 PHeader.p_filesz = 0;
380 for (auto SecName : YamlPhdr.Sections) {
381 uint32_t Index = 0;
382 SN2I.lookup(SecName.Section, Index);
383 const auto &SHeader = SHeaders[Index];
384 uint64_t EndOfSection;
385 if (SHeader.sh_type == llvm::ELF::SHT_NOBITS)
386 EndOfSection = SHeader.sh_offset;
387 else
388 EndOfSection = SHeader.sh_offset + SHeader.sh_size;
389 uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
390 EndOfSegment = std::max(EndOfSegment, EndOfSection);
391 PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
392 }
393
394 // Find the memory size by adding the size of sections at the end of the
395 // segment. These should be empty (size of zero) and NOBITS sections.
396 PHeader.p_memsz = PHeader.p_filesz;
397 for (auto SecName : YamlPhdr.Sections) {
398 uint32_t Index = 0;
399 SN2I.lookup(SecName.Section, Index);
400 const auto &SHeader = SHeaders[Index];
401 if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz)
402 PHeader.p_memsz += SHeader.sh_size;
403 }
404
405 // Set the alignment of the segment to be the same as the maximum alignment
Jake Ehrlich9ae2da62017-11-01 23:14:48 +0000406 // of the sections with the same offset so that by default the segment
Petr Hoseke7726ca2017-07-19 20:38:46 +0000407 // has a valid and sensible alignment.
Jake Ehrlich9ae2da62017-11-01 23:14:48 +0000408 if (YamlPhdr.Align) {
409 PHeader.p_align = *YamlPhdr.Align;
410 } else {
411 PHeader.p_align = 1;
412 for (auto SecName : YamlPhdr.Sections) {
413 uint32_t Index = 0;
414 SN2I.lookup(SecName.Section, Index);
415 const auto &SHeader = SHeaders[Index];
416 if (SHeader.sh_offset == PHeader.p_offset)
417 PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign);
418 }
Petr Hoseke7726ca2017-07-19 20:38:46 +0000419 }
420 }
421}
422
423template <class ELFT>
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000424void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
425 std::vector<Elf_Sym> &Syms,
Dave Lee9c5b7992017-11-16 18:10:15 +0000426 unsigned SymbolBinding,
427 const StringTableBuilder &Strtab) {
Simon Atanasyan74f42fd2014-03-14 06:53:30 +0000428 for (const auto &Sym : Symbols) {
Sean Silvaafcf60f2013-06-18 23:14:03 +0000429 Elf_Sym Symbol;
430 zero(Symbol);
431 if (!Sym.Name.empty())
Dave Lee9c5b7992017-11-16 18:10:15 +0000432 Symbol.st_name = Strtab.getOffset(Sym.Name);
Sean Silva4235ba32013-06-21 00:27:50 +0000433 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silva326c1932013-06-21 01:11:48 +0000434 if (!Sym.Section.empty()) {
435 unsigned Index;
Simon Atanasyancde97ea2014-04-06 09:02:55 +0000436 if (SN2I.lookup(Sym.Section, Index)) {
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000437 WithColor::error() << "Unknown section referenced: '" << Sym.Section
438 << "' by YAML symbol " << Sym.Name << ".\n";
Sean Silva326c1932013-06-21 01:11:48 +0000439 exit(1);
440 }
441 Symbol.st_shndx = Index;
Petr Hosek746c7782017-09-07 20:44:16 +0000442 } else if (Sym.Index) {
443 Symbol.st_shndx = *Sym.Index;
444 }
445 // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silvae38f6402013-06-20 20:59:47 +0000446 Symbol.st_value = Sym.Value;
Simon Atanasyan848edb12014-11-06 22:46:24 +0000447 Symbol.st_other = Sym.Other;
Sean Silvae38f6402013-06-20 20:59:47 +0000448 Symbol.st_size = Sym.Size;
Sean Silvaafcf60f2013-06-18 23:14:03 +0000449 Syms.push_back(Symbol);
450 }
Sean Silva4235ba32013-06-21 00:27:50 +0000451}
452
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000453template <class ELFT>
454void
455ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
456 const ELFYAML::RawContentSection &Section,
457 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan19ab8f82014-05-16 16:01:00 +0000458 assert(Section.Size >= Section.Content.binary_size() &&
459 "Section size and section content are inconsistent");
Simon Atanasyan080d7a82015-07-08 10:12:40 +0000460 raw_ostream &OS =
461 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan19ab8f82014-05-16 16:01:00 +0000462 Section.Content.writeAsBinary(OS);
463 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
464 OS.write(0);
George Rimarb40956fe2018-08-07 08:11:38 +0000465 if (Section.EntSize)
466 SHeader.sh_entsize = *Section.EntSize;
467 else if (Section.Type == llvm::ELF::SHT_RELR)
Jake Ehrlicheefdbb42018-06-28 21:07:34 +0000468 SHeader.sh_entsize = sizeof(Elf_Relr);
Paul Semelf3f578d2018-07-23 18:49:04 +0000469 else if (Section.Type == llvm::ELF::SHT_DYNAMIC)
470 SHeader.sh_entsize = sizeof(Elf_Dyn);
Jake Ehrlicheefdbb42018-06-28 21:07:34 +0000471 else
472 SHeader.sh_entsize = 0;
Simon Atanasyan19ab8f82014-05-16 16:01:00 +0000473 SHeader.sh_size = Section.Size;
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000474}
475
Simon Atanasyan09a46072015-01-25 13:29:25 +0000476static bool isMips64EL(const ELFYAML::Object &Doc) {
477 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
478 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
479 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
480}
481
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000482template <class ELFT>
483bool
484ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
485 const ELFYAML::RelocationSection &Section,
486 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb1e75952015-05-08 07:05:04 +0000487 assert((Section.Type == llvm::ELF::SHT_REL ||
488 Section.Type == llvm::ELF::SHT_RELA) &&
489 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000490
491 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
492 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
493 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
494
Simon Atanasyan080d7a82015-07-08 10:12:40 +0000495 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000496
497 for (const auto &Rel : Section.Relocations) {
Adhemerval Zanellac728e852015-04-22 15:26:43 +0000498 unsigned SymIdx = 0;
499 // Some special relocation, R_ARM_v4BX for instance, does not have
500 // an external reference. So it ignores the return value of lookup()
501 // here.
Petr Hosek82311332017-08-30 23:13:31 +0000502 if (Rel.Symbol)
503 SymN2I.lookup(*Rel.Symbol, SymIdx);
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000504
505 if (IsRela) {
506 Elf_Rela REntry;
507 zero(REntry);
508 REntry.r_offset = Rel.Offset;
509 REntry.r_addend = Rel.Addend;
Simon Atanasyan09a46072015-01-25 13:29:25 +0000510 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000511 OS.write((const char *)&REntry, sizeof(REntry));
512 } else {
513 Elf_Rel REntry;
514 zero(REntry);
515 REntry.r_offset = Rel.Offset;
Simon Atanasyan09a46072015-01-25 13:29:25 +0000516 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000517 OS.write((const char *)&REntry, sizeof(REntry));
518 }
519 }
520 return true;
521}
522
Shankar Easwaran58721742015-02-21 04:28:26 +0000523template <class ELFT>
524bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
525 const ELFYAML::Group &Section,
526 ContiguousBlobAccumulator &CBA) {
Rui Ueyama5a286a02018-01-12 02:28:31 +0000527 typedef typename ELFT::Word Elf_Word;
Simon Atanasyanb1e75952015-05-08 07:05:04 +0000528 assert(Section.Type == llvm::ELF::SHT_GROUP &&
529 "Section type is not SHT_GROUP");
Shankar Easwaran58721742015-02-21 04:28:26 +0000530
531 SHeader.sh_entsize = sizeof(Elf_Word);
532 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
533
Simon Atanasyan080d7a82015-07-08 10:12:40 +0000534 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran58721742015-02-21 04:28:26 +0000535
536 for (auto member : Section.Members) {
537 Elf_Word SIdx;
538 unsigned int sectionIndex = 0;
539 if (member.sectionNameOrType == "GRP_COMDAT")
540 sectionIndex = llvm::ELF::GRP_COMDAT;
Xing GUO53a9c2e2018-12-04 14:27:51 +0000541 else if (!convertSectionIndex(SN2I, Section.Name, member.sectionNameOrType,
542 sectionIndex))
Shankar Easwaran58721742015-02-21 04:28:26 +0000543 return false;
Shankar Easwaran58721742015-02-21 04:28:26 +0000544 SIdx = sectionIndex;
545 OS.write((const char *)&SIdx, sizeof(SIdx));
546 }
547 return true;
548}
549
Simon Atanasyanbd58bdb2015-05-07 15:40:48 +0000550template <class ELFT>
551bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
552 const ELFYAML::MipsABIFlags &Section,
553 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb1e75952015-05-08 07:05:04 +0000554 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
555 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyanbd58bdb2015-05-07 15:40:48 +0000556
557 object::Elf_Mips_ABIFlags<ELFT> Flags;
558 zero(Flags);
559 SHeader.sh_entsize = sizeof(Flags);
560 SHeader.sh_size = SHeader.sh_entsize;
561
Simon Atanasyan080d7a82015-07-08 10:12:40 +0000562 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyanbd58bdb2015-05-07 15:40:48 +0000563 Flags.version = Section.Version;
564 Flags.isa_level = Section.ISALevel;
565 Flags.isa_rev = Section.ISARevision;
566 Flags.gpr_size = Section.GPRSize;
567 Flags.cpr1_size = Section.CPR1Size;
568 Flags.cpr2_size = Section.CPR2Size;
569 Flags.fp_abi = Section.FpABI;
570 Flags.isa_ext = Section.ISAExtension;
571 Flags.ases = Section.ASEs;
572 Flags.flags1 = Section.Flags1;
573 Flags.flags2 = Section.Flags2;
574 OS.write((const char *)&Flags, sizeof(Flags));
575
576 return true;
577}
578
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000579template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000580 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000581 StringRef Name = Doc.Sections[i]->Name;
Dave Lee9a5be1f2017-11-09 14:53:43 +0000582 DotShStrtab.add(Name);
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000583 // "+ 1" to take into account the SHT_NULL entry.
584 if (SN2I.addName(Name, i + 1)) {
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000585 WithColor::error() << "Repeated section name: '" << Name
586 << "' at YAML section number " << i << ".\n";
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000587 return false;
588 }
Sean Silva4235ba32013-06-21 00:27:50 +0000589 }
Dave Lee9a5be1f2017-11-09 14:53:43 +0000590
591 auto SecNo = 1 + Doc.Sections.size();
592 // Add special sections after input sections, if necessary.
Dave Lee9c5b7992017-11-16 18:10:15 +0000593 for (const auto &Name : implicitSectionNames())
Dave Lee9a5be1f2017-11-09 14:53:43 +0000594 if (!SN2I.addName(Name, SecNo)) {
595 // Account for this section, since it wasn't in the Doc
596 ++SecNo;
597 DotShStrtab.add(Name);
598 }
599
600 DotShStrtab.finalize();
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000601 return true;
Sean Silvaafcf60f2013-06-18 23:14:03 +0000602}
603
Sean Silva5918b7a2013-06-10 23:44:15 +0000604template <class ELFT>
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000605bool
606ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
607 const std::vector<ELFYAML::Symbol> &Symbols) {
608 for (const auto &Sym : Symbols) {
609 ++StartIndex;
610 if (Sym.Name.empty())
611 continue;
612 if (SymN2I.addName(Sym.Name, StartIndex)) {
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000613 WithColor::error() << "Repeated symbol name: '" << Sym.Name << "'.\n";
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000614 return false;
615 }
616 }
617 return true;
618}
619
620template <class ELFT>
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000621int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Simon Atanasyan38ac43b12014-04-02 16:34:40 +0000622 ELFState<ELFT> State(Doc);
623 if (!State.buildSectionIndex())
624 return 1;
625
Simon Atanasyaneb0c9092014-04-11 04:13:39 +0000626 std::size_t StartSymIndex = 0;
627 if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
628 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
629 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
630 return 1;
631
Sean Silva274264c2013-06-13 22:19:48 +0000632 Elf_Ehdr Header;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000633 State.initELFHeader(Header);
Sean Silva5918b7a2013-06-10 23:44:15 +0000634
Sean Silva274264c2013-06-13 22:19:48 +0000635 // TODO: Flesh out section header support.
Petr Hoseke7726ca2017-07-19 20:38:46 +0000636
637 std::vector<Elf_Phdr> PHeaders;
638 State.initProgramHeaders(PHeaders);
Sean Silva274264c2013-06-13 22:19:48 +0000639
Sean Silva0382b302013-06-20 19:11:44 +0000640 // XXX: This offset is tightly coupled with the order that we write
641 // things to `OS`.
Petr Hoseke7726ca2017-07-19 20:38:46 +0000642 const size_t SectionContentBeginOffset = Header.e_ehsize +
643 Header.e_phentsize * Header.e_phnum +
644 Header.e_shentsize * Header.e_shnum;
Sean Silva0382b302013-06-20 19:11:44 +0000645 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac18f66e2013-06-20 20:59:34 +0000646
Sean Silva274264c2013-06-13 22:19:48 +0000647 std::vector<Elf_Shdr> SHeaders;
Simon Atanasyanbe2ec9b2014-04-02 16:34:54 +0000648 if(!State.initSectionHeaders(SHeaders, CBA))
649 return 1;
Sean Silva274264c2013-06-13 22:19:48 +0000650
Dave Lee9a5be1f2017-11-09 14:53:43 +0000651 // Populate SHeaders with implicit sections not present in the Doc
Dave Lee9c5b7992017-11-16 18:10:15 +0000652 for (const auto &Name : State.implicitSectionNames())
Dave Lee9a5be1f2017-11-09 14:53:43 +0000653 if (State.SN2I.get(Name) >= SHeaders.size())
654 SHeaders.push_back({});
Sean Silva068463b2013-06-22 01:38:00 +0000655
Dave Lee9a5be1f2017-11-09 14:53:43 +0000656 // Initialize the implicit sections
657 auto Index = State.SN2I.get(".symtab");
Dave Lee9c5b7992017-11-16 18:10:15 +0000658 State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Static, CBA);
Dave Lee9a5be1f2017-11-09 14:53:43 +0000659 Index = State.SN2I.get(".strtab");
660 State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA);
661 Index = State.SN2I.get(".shstrtab");
662 State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA);
Dave Lee9c5b7992017-11-16 18:10:15 +0000663 if (State.hasDynamicSymbols()) {
664 Index = State.SN2I.get(".dynsym");
665 State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Dynamic, CBA);
666 SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
667 Index = State.SN2I.get(".dynstr");
668 State.initStrtabSectionHeader(SHeaders[Index], ".dynstr", State.DotDynstr, CBA);
669 SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
670 }
Sean Silva5918b7a2013-06-10 23:44:15 +0000671
Petr Hoseke7726ca2017-07-19 20:38:46 +0000672 // Now we can decide segment offsets
673 State.setProgramHeaderLayout(PHeaders, SHeaders);
674
Sean Silva5918b7a2013-06-10 23:44:15 +0000675 OS.write((const char *)&Header, sizeof(Header));
Petr Hoseke7726ca2017-07-19 20:38:46 +0000676 writeArrayData(OS, makeArrayRef(PHeaders));
Will Dietz6dd78932013-10-12 21:29:16 +0000677 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva2a7e79a2013-06-13 22:20:01 +0000678 CBA.writeBlobToStream(OS);
Sean Silva4b548ec2013-06-17 20:14:59 +0000679 return 0;
Sean Silva5918b7a2013-06-10 23:44:15 +0000680}
681
Dave Lee9c5b7992017-11-16 18:10:15 +0000682template <class ELFT> bool ELFState<ELFT>::hasDynamicSymbols() const {
683 return Doc.DynamicSymbols.Global.size() > 0 ||
684 Doc.DynamicSymbols.Weak.size() > 0 ||
685 Doc.DynamicSymbols.Local.size() > 0;
686}
687
Xing GUOa4079af2018-12-07 11:04:22 +0000688template <class ELFT>
689SmallVector<const char *, 5> ELFState<ELFT>::implicitSectionNames() const {
Dave Lee9c5b7992017-11-16 18:10:15 +0000690 if (!hasDynamicSymbols())
691 return {".symtab", ".strtab", ".shstrtab"};
692 return {".symtab", ".strtab", ".shstrtab", ".dynsym", ".dynstr"};
693}
694
Sean Silvaefc78982013-06-22 01:03:35 +0000695static bool is64Bit(const ELFYAML::Object &Doc) {
696 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
697}
698
699static bool isLittleEndian(const ELFYAML::Object &Doc) {
700 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
701}
702
Chris Bieneman9cf90e62016-06-27 19:53:53 +0000703int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
Sean Silvaefc78982013-06-22 01:03:35 +0000704 if (is64Bit(Doc)) {
705 if (isLittleEndian(Doc))
Rui Ueyama0a759402018-01-12 01:40:32 +0000706 return ELFState<object::ELF64LE>::writeELF(Out, Doc);
Sean Silva5918b7a2013-06-10 23:44:15 +0000707 else
Rui Ueyama0a759402018-01-12 01:40:32 +0000708 return ELFState<object::ELF64BE>::writeELF(Out, Doc);
Sean Silva5918b7a2013-06-10 23:44:15 +0000709 } else {
Sean Silvaefc78982013-06-22 01:03:35 +0000710 if (isLittleEndian(Doc))
Rui Ueyama0a759402018-01-12 01:40:32 +0000711 return ELFState<object::ELF32LE>::writeELF(Out, Doc);
Sean Silva5918b7a2013-06-10 23:44:15 +0000712 else
Rui Ueyama0a759402018-01-12 01:40:32 +0000713 return ELFState<object::ELF32BE>::writeELF(Out, Doc);
Sean Silva5918b7a2013-06-10 23:44:15 +0000714 }
Sean Silva5918b7a2013-06-10 23:44:15 +0000715}