blob: 2cbc338035823889506bac88148a9227afcff289 [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"
Rafael Espindola97de4742014-07-03 02:01:39 +000017#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000018#include "llvm/Object/ELFObjectFile.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000019#include "llvm/Object/ELFYAML.h"
20#include "llvm/Support/ELF.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/raw_ostream.h"
24
25using namespace llvm;
26
Sean Silva46dffff2013-06-13 22:20:01 +000027// This class is used to build up a contiguous binary blob while keeping
28// track of an offset in the output (which notionally begins at
29// `InitialOffset`).
Sean Silva2a74f702013-06-15 00:31:46 +000030namespace {
Sean Silva46dffff2013-06-13 22:20:01 +000031class ContiguousBlobAccumulator {
32 const uint64_t InitialOffset;
Sean Silvabd3bc692013-06-20 19:11:41 +000033 SmallVector<char, 128> Buf;
Sean Silva46dffff2013-06-13 22:20:01 +000034 raw_svector_ostream OS;
35
Sean Silvad93323f2013-06-22 00:47:43 +000036 /// \returns The new offset.
37 uint64_t padToAlignment(unsigned Align) {
38 uint64_t CurrentOffset = InitialOffset + OS.tell();
39 uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
40 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
41 OS.write('\0');
42 return AlignedOffset; // == CurrentOffset;
43 }
44
Sean Silva46dffff2013-06-13 22:20:01 +000045public:
Sean Silvabd3bc692013-06-20 19:11:41 +000046 ContiguousBlobAccumulator(uint64_t InitialOffset_)
47 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000048 template <class Integer>
49 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
50 Offset = padToAlignment(Align);
51 return OS;
52 }
Sean Silva46dffff2013-06-13 22:20:01 +000053 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
54};
Sean Silva2a74f702013-06-15 00:31:46 +000055} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000056
Simon Atanasyan35babf92014-04-06 09:02:55 +000057// Used to keep track of section and symbol names, so that in the YAML file
58// sections and symbols can be referenced by name instead of by index.
Sean Silva2a74f702013-06-15 00:31:46 +000059namespace {
Simon Atanasyan35babf92014-04-06 09:02:55 +000060class NameToIdxMap {
Sean Silvaa6423eb2013-06-15 00:25:26 +000061 StringMap<int> Map;
62public:
63 /// \returns true if name is already present in the map.
Simon Atanasyan35babf92014-04-06 09:02:55 +000064 bool addName(StringRef Name, unsigned i) {
David Blaikie5106ce72014-11-19 05:49:42 +000065 return !Map.insert(std::make_pair(Name, (int)i)).second;
Sean Silvaa6423eb2013-06-15 00:25:26 +000066 }
67 /// \returns true if name is not present in the map
Simon Atanasyan35babf92014-04-06 09:02:55 +000068 bool lookup(StringRef Name, unsigned &Idx) const {
69 StringMap<int>::const_iterator I = Map.find(Name);
Sean Silvaa6423eb2013-06-15 00:25:26 +000070 if (I == Map.end())
71 return true;
72 Idx = I->getValue();
73 return false;
74 }
75};
Sean Silva2a74f702013-06-15 00:31:46 +000076} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +000077
Sean Silva38205932013-06-13 22:19:48 +000078template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000079static size_t arrayDataSize(ArrayRef<T> A) {
80 return A.size() * sizeof(T);
Sean Silva38205932013-06-13 22:19:48 +000081}
82
83template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000084static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
85 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva38205932013-06-13 22:19:48 +000086}
87
88template <class T>
89static void zero(T &Obj) {
90 memset(&Obj, 0, sizeof(Obj));
91}
92
Sean Silva08a75ae2013-06-20 19:11:44 +000093namespace {
94/// \brief "Single point of truth" for the ELF file construction.
95/// TODO: This class still has a ways to go before it is truly a "single
96/// point of truth".
97template <class ELFT>
98class ELFState {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +000099 typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
100 typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
101 typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000102 typedef typename object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
103 typedef typename object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000104
Sean Silva08a75ae2013-06-20 19:11:44 +0000105 /// \brief The future ".strtab" section.
106 StringTableBuilder DotStrtab;
Sean Silva08a75ae2013-06-20 19:11:44 +0000107
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000108 /// \brief The future ".shstrtab" section.
109 StringTableBuilder DotShStrtab;
110
Simon Atanasyan35babf92014-04-06 09:02:55 +0000111 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000112 NameToIdxMap SymN2I;
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000113 const ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000114
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000115 bool buildSectionIndex();
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000116 bool buildSymbolIndex(std::size_t &StartIndex,
117 const std::vector<ELFYAML::Symbol> &Symbols);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000118 void initELFHeader(Elf_Ehdr &Header);
119 bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
120 ContiguousBlobAccumulator &CBA);
121 void initSymtabSectionHeader(Elf_Shdr &SHeader,
122 ContiguousBlobAccumulator &CBA);
123 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
124 StringTableBuilder &STB,
125 ContiguousBlobAccumulator &CBA);
126 void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
127 std::vector<Elf_Sym> &Syms, unsigned SymbolBinding);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000128 void writeSectionContent(Elf_Shdr &SHeader,
129 const ELFYAML::RawContentSection &Section,
130 ContiguousBlobAccumulator &CBA);
131 bool writeSectionContent(Elf_Shdr &SHeader,
132 const ELFYAML::RelocationSection &Section,
133 ContiguousBlobAccumulator &CBA);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000134 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
135 ContiguousBlobAccumulator &CBA);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000136 bool writeSectionContent(Elf_Shdr &SHeader,
137 const ELFYAML::MipsABIFlags &Section,
138 ContiguousBlobAccumulator &CBA);
Sean Silva08a75ae2013-06-20 19:11:44 +0000139
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000140 // - SHT_NULL entry (placed first, i.e. 0'th entry)
141 // - symbol table (.symtab) (placed third to last)
142 // - string table (.strtab) (placed second to last)
143 // - section header string table (.shstrtab) (placed last)
144 unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; }
145 unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; }
146 unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
147 unsigned getSectionCount() const { return Doc.Sections.size() + 4; }
148
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000149 ELFState(const ELFYAML::Object &D) : Doc(D) {}
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000150
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000151public:
152 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
Sean Silva08a75ae2013-06-20 19:11:44 +0000153};
154} // end anonymous namespace
155
Sean Silva37e817c2013-06-21 00:33:01 +0000156template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000157void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
158 using namespace llvm::ELF;
159 zero(Header);
160 Header.e_ident[EI_MAG0] = 0x7f;
161 Header.e_ident[EI_MAG1] = 'E';
162 Header.e_ident[EI_MAG2] = 'L';
163 Header.e_ident[EI_MAG3] = 'F';
164 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
165 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
166 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
167 Header.e_ident[EI_VERSION] = EV_CURRENT;
168 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
169 Header.e_ident[EI_ABIVERSION] = 0;
170 Header.e_type = Doc.Header.Type;
171 Header.e_machine = Doc.Header.Machine;
172 Header.e_version = EV_CURRENT;
173 Header.e_entry = Doc.Header.Entry;
174 Header.e_flags = Doc.Header.Flags;
175 Header.e_ehsize = sizeof(Elf_Ehdr);
176 Header.e_shentsize = sizeof(Elf_Shdr);
177 // Immediately following the ELF header.
178 Header.e_shoff = sizeof(Header);
179 Header.e_shnum = getSectionCount();
180 Header.e_shstrndx = getDotShStrTabSecNo();
181}
182
183template <class ELFT>
184bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
185 ContiguousBlobAccumulator &CBA) {
186 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
187 // valid SHN_UNDEF entry since SHT_NULL == 0.
188 Elf_Shdr SHeader;
189 zero(SHeader);
190 SHeaders.push_back(SHeader);
191
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000192 for (const auto &Sec : Doc.Sections)
193 DotShStrtab.add(Sec->Name);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000194 DotShStrtab.finalize(StringTableBuilder::ELF);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000195
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000196 for (const auto &Sec : Doc.Sections) {
197 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000198 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000199 SHeader.sh_type = Sec->Type;
200 SHeader.sh_flags = Sec->Flags;
201 SHeader.sh_addr = Sec->Address;
202 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000203
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000204 if (!Sec->Link.empty()) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000205 unsigned Index;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000206 if (SN2I.lookup(Sec->Link, Index)) {
207 errs() << "error: Unknown section referenced: '" << Sec->Link
208 << "' at YAML section '" << Sec->Name << "'.\n";
209 return false;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000210 }
211 SHeader.sh_link = Index;
212 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000213
214 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
215 writeSectionContent(SHeader, *S, CBA);
216 else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
217 if (S->Link.empty())
218 // For relocation section set link to .symtab by default.
219 SHeader.sh_link = getDotSymTabSecNo();
220
221 unsigned Index;
222 if (SN2I.lookup(S->Info, Index)) {
223 errs() << "error: Unknown section referenced: '" << S->Info
224 << "' at YAML section '" << S->Name << "'.\n";
225 return false;
226 }
227 SHeader.sh_info = Index;
228
229 if (!writeSectionContent(SHeader, *S, CBA))
230 return false;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000231 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
232 unsigned SymIdx;
233 if (SymN2I.lookup(S->Info, SymIdx)) {
234 errs() << "error: Unknown symbol referenced: '" << S->Info
235 << "' at YAML section '" << S->Name << "'.\n";
236 return false;
237 }
238 SHeader.sh_info = SymIdx;
239 if (!writeSectionContent(SHeader, *S, CBA))
240 return false;
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000241 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
242 if (!writeSectionContent(SHeader, *S, CBA))
243 return false;
Simon Atanasyan5db02762015-07-03 23:00:54 +0000244 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
245 SHeader.sh_entsize = 0;
246 SHeader.sh_size = S->Size;
247 // SHT_NOBITS section does not have content
248 // so just to setup the section offset.
249 CBA.getOSAndAlignedOffset(SHeader.sh_offset);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000250 } else
251 llvm_unreachable("Unknown section type");
252
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000253 SHeaders.push_back(SHeader);
254 }
255 return true;
256}
257
258template <class ELFT>
259void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
260 ContiguousBlobAccumulator &CBA) {
261 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000262 SHeader.sh_name = DotShStrtab.getOffset(".symtab");
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000263 SHeader.sh_type = ELF::SHT_SYMTAB;
264 SHeader.sh_link = getDotStrTabSecNo();
265 // One greater than symbol table index of the last local symbol.
266 SHeader.sh_info = Doc.Symbols.Local.size() + 1;
267 SHeader.sh_entsize = sizeof(Elf_Sym);
268
269 std::vector<Elf_Sym> Syms;
270 {
271 // Ensure STN_UNDEF is present
272 Elf_Sym Sym;
273 zero(Sym);
274 Syms.push_back(Sym);
275 }
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000276
277 // Add symbol names to .strtab.
278 for (const auto &Sym : Doc.Symbols.Local)
279 DotStrtab.add(Sym.Name);
280 for (const auto &Sym : Doc.Symbols.Global)
281 DotStrtab.add(Sym.Name);
282 for (const auto &Sym : Doc.Symbols.Weak)
283 DotStrtab.add(Sym.Name);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000284 DotStrtab.finalize(StringTableBuilder::ELF);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000285
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000286 addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
287 addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
288 addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
289
290 writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
291 makeArrayRef(Syms));
292 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
293}
294
295template <class ELFT>
296void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
297 StringTableBuilder &STB,
298 ContiguousBlobAccumulator &CBA) {
299 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000300 SHeader.sh_name = DotShStrtab.getOffset(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000301 SHeader.sh_type = ELF::SHT_STRTAB;
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000302 CBA.getOSAndAlignedOffset(SHeader.sh_offset) << STB.data();
303 SHeader.sh_size = STB.data().size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000304 SHeader.sh_addralign = 1;
305}
306
307template <class ELFT>
308void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
309 std::vector<Elf_Sym> &Syms,
310 unsigned SymbolBinding) {
Simon Atanasyan048baca2014-03-14 06:53:30 +0000311 for (const auto &Sym : Symbols) {
Sean Silva6b083882013-06-18 23:14:03 +0000312 Elf_Sym Symbol;
313 zero(Symbol);
314 if (!Sym.Name.empty())
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000315 Symbol.st_name = DotStrtab.getOffset(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000316 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000317 if (!Sym.Section.empty()) {
318 unsigned Index;
Simon Atanasyan35babf92014-04-06 09:02:55 +0000319 if (SN2I.lookup(Sym.Section, Index)) {
Sean Silvac4afa6d2013-06-21 01:11:48 +0000320 errs() << "error: Unknown section referenced: '" << Sym.Section
321 << "' by YAML symbol " << Sym.Name << ".\n";
322 exit(1);
323 }
324 Symbol.st_shndx = Index;
325 } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000326 Symbol.st_value = Sym.Value;
Simon Atanasyan60e1a792014-11-06 22:46:24 +0000327 Symbol.st_other = Sym.Other;
Sean Silva05001b92013-06-20 20:59:47 +0000328 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000329 Syms.push_back(Symbol);
330 }
Sean Silvaaff51252013-06-21 00:27:50 +0000331}
332
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000333template <class ELFT>
334void
335ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
336 const ELFYAML::RawContentSection &Section,
337 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000338 assert(Section.Size >= Section.Content.binary_size() &&
339 "Section size and section content are inconsistent");
340 raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
341 Section.Content.writeAsBinary(OS);
342 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
343 OS.write(0);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000344 SHeader.sh_entsize = 0;
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000345 SHeader.sh_size = Section.Size;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000346}
347
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000348static bool isMips64EL(const ELFYAML::Object &Doc) {
349 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
350 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
351 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
352}
353
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000354template <class ELFT>
355bool
356ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
357 const ELFYAML::RelocationSection &Section,
358 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000359 assert((Section.Type == llvm::ELF::SHT_REL ||
360 Section.Type == llvm::ELF::SHT_RELA) &&
361 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000362
363 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
364 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
365 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
366
367 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
368
369 for (const auto &Rel : Section.Relocations) {
Adhemerval Zanella9f3dbff2015-04-22 15:26:43 +0000370 unsigned SymIdx = 0;
371 // Some special relocation, R_ARM_v4BX for instance, does not have
372 // an external reference. So it ignores the return value of lookup()
373 // here.
374 SymN2I.lookup(Rel.Symbol, SymIdx);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000375
376 if (IsRela) {
377 Elf_Rela REntry;
378 zero(REntry);
379 REntry.r_offset = Rel.Offset;
380 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000381 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000382 OS.write((const char *)&REntry, sizeof(REntry));
383 } else {
384 Elf_Rel REntry;
385 zero(REntry);
386 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000387 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000388 OS.write((const char *)&REntry, sizeof(REntry));
389 }
390 }
391 return true;
392}
393
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000394template <class ELFT>
395bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
396 const ELFYAML::Group &Section,
397 ContiguousBlobAccumulator &CBA) {
398 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000399 assert(Section.Type == llvm::ELF::SHT_GROUP &&
400 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000401
402 SHeader.sh_entsize = sizeof(Elf_Word);
403 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
404
405 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
406
407 for (auto member : Section.Members) {
408 Elf_Word SIdx;
409 unsigned int sectionIndex = 0;
410 if (member.sectionNameOrType == "GRP_COMDAT")
411 sectionIndex = llvm::ELF::GRP_COMDAT;
412 else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) {
413 errs() << "error: Unknown section referenced: '"
414 << member.sectionNameOrType << "' at YAML section' "
415 << Section.Name << "\n";
416 return false;
417 }
418 SIdx = sectionIndex;
419 OS.write((const char *)&SIdx, sizeof(SIdx));
420 }
421 return true;
422}
423
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000424template <class ELFT>
425bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
426 const ELFYAML::MipsABIFlags &Section,
427 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000428 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
429 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000430
431 object::Elf_Mips_ABIFlags<ELFT> Flags;
432 zero(Flags);
433 SHeader.sh_entsize = sizeof(Flags);
434 SHeader.sh_size = SHeader.sh_entsize;
435
436 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
437 Flags.version = Section.Version;
438 Flags.isa_level = Section.ISALevel;
439 Flags.isa_rev = Section.ISARevision;
440 Flags.gpr_size = Section.GPRSize;
441 Flags.cpr1_size = Section.CPR1Size;
442 Flags.cpr2_size = Section.CPR2Size;
443 Flags.fp_abi = Section.FpABI;
444 Flags.isa_ext = Section.ISAExtension;
445 Flags.ases = Section.ASEs;
446 Flags.flags1 = Section.Flags1;
447 Flags.flags2 = Section.Flags2;
448 OS.write((const char *)&Flags, sizeof(Flags));
449
450 return true;
451}
452
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000453template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
454 SN2I.addName(".symtab", getDotSymTabSecNo());
455 SN2I.addName(".strtab", getDotStrTabSecNo());
456 SN2I.addName(".shstrtab", getDotShStrTabSecNo());
Sean Silvaaff51252013-06-21 00:27:50 +0000457
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000458 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000459 StringRef Name = Doc.Sections[i]->Name;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000460 if (Name.empty())
461 continue;
462 // "+ 1" to take into account the SHT_NULL entry.
463 if (SN2I.addName(Name, i + 1)) {
464 errs() << "error: Repeated section name: '" << Name
465 << "' at YAML section number " << i << ".\n";
466 return false;
467 }
Sean Silvaaff51252013-06-21 00:27:50 +0000468 }
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000469 return true;
Sean Silva6b083882013-06-18 23:14:03 +0000470}
471
Sean Silvaf99309c2013-06-10 23:44:15 +0000472template <class ELFT>
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000473bool
474ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
475 const std::vector<ELFYAML::Symbol> &Symbols) {
476 for (const auto &Sym : Symbols) {
477 ++StartIndex;
478 if (Sym.Name.empty())
479 continue;
480 if (SymN2I.addName(Sym.Name, StartIndex)) {
481 errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n";
482 return false;
483 }
484 }
485 return true;
486}
487
488template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000489int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000490 ELFState<ELFT> State(Doc);
491 if (!State.buildSectionIndex())
492 return 1;
493
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000494 std::size_t StartSymIndex = 0;
495 if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
496 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
497 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
498 return 1;
499
Sean Silva38205932013-06-13 22:19:48 +0000500 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000501 State.initELFHeader(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000502
Sean Silva38205932013-06-13 22:19:48 +0000503 // TODO: Flesh out section header support.
504 // TODO: Program headers.
505
Sean Silva08a75ae2013-06-20 19:11:44 +0000506 // XXX: This offset is tightly coupled with the order that we write
507 // things to `OS`.
508 const size_t SectionContentBeginOffset =
509 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
510 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +0000511
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000512 // Doc might not contain .symtab, .strtab and .shstrtab sections,
513 // but we will emit them, so make sure to add them to ShStrTabSHeader.
514 State.DotShStrtab.add(".symtab");
515 State.DotShStrtab.add(".strtab");
516 State.DotShStrtab.add(".shstrtab");
517
Sean Silva38205932013-06-13 22:19:48 +0000518 std::vector<Elf_Shdr> SHeaders;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000519 if(!State.initSectionHeaders(SHeaders, CBA))
520 return 1;
Sean Silva38205932013-06-13 22:19:48 +0000521
Sean Silva82177572013-06-22 01:38:00 +0000522 // .symtab section.
523 Elf_Shdr SymtabSHeader;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000524 State.initSymtabSectionHeader(SymtabSHeader, CBA);
Sean Silva82177572013-06-22 01:38:00 +0000525 SHeaders.push_back(SymtabSHeader);
526
Sean Silva6b083882013-06-18 23:14:03 +0000527 // .strtab string table header.
Sean Silvac3131922013-06-18 21:37:50 +0000528 Elf_Shdr DotStrTabSHeader;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000529 State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
530 CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000531 SHeaders.push_back(DotStrTabSHeader);
Sean Silvac3131922013-06-18 21:37:50 +0000532
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000533 // .shstrtab string table header.
534 Elf_Shdr ShStrTabSHeader;
535 State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
536 CBA);
537 SHeaders.push_back(ShStrTabSHeader);
Sean Silvaf99309c2013-06-10 23:44:15 +0000538
539 OS.write((const char *)&Header, sizeof(Header));
Will Dietz0b48c732013-10-12 21:29:16 +0000540 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +0000541 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000542 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000543}
544
Sean Silva11caeba2013-06-22 01:03:35 +0000545static bool is64Bit(const ELFYAML::Object &Doc) {
546 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
547}
548
549static bool isLittleEndian(const ELFYAML::Object &Doc) {
550 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
551}
552
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000553int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
Sean Silvaf99309c2013-06-10 23:44:15 +0000554 ELFYAML::Object Doc;
555 YIn >> Doc;
556 if (YIn.error()) {
557 errs() << "yaml2obj: Failed to parse YAML file!\n";
558 return 1;
559 }
Sean Silva11caeba2013-06-22 01:03:35 +0000560 using object::ELFType;
Rafael Espindolaac729b42015-06-02 12:05:27 +0000561 typedef ELFType<support::little, true> LE64;
562 typedef ELFType<support::big, true> BE64;
563 typedef ELFType<support::little, false> LE32;
564 typedef ELFType<support::big, false> BE32;
Sean Silva11caeba2013-06-22 01:03:35 +0000565 if (is64Bit(Doc)) {
566 if (isLittleEndian(Doc))
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000567 return ELFState<LE64>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000568 else
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000569 return ELFState<BE64>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000570 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000571 if (isLittleEndian(Doc))
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000572 return ELFState<LE32>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000573 else
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000574 return ELFState<BE32>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000575 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000576}