blob: a2ae5e2e756b0c380a476fcf18c21b8c291950ef [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"
Rafael Espindolaebd91932016-03-01 19:15:06 +000019#include "llvm/ObjectYAML/ELFYAML.h"
Sean Silvaf99309c2013-06-10 23:44:15 +000020#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) {
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000038 if (Align == 0)
39 Align = 1;
Sean Silvad93323f2013-06-22 00:47:43 +000040 uint64_t CurrentOffset = InitialOffset + OS.tell();
Rui Ueyamada00f2f2016-01-14 21:06:47 +000041 uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
Sean Silvad93323f2013-06-22 00:47:43 +000042 for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
43 OS.write('\0');
44 return AlignedOffset; // == CurrentOffset;
45 }
46
Sean Silva46dffff2013-06-13 22:20:01 +000047public:
Sean Silvabd3bc692013-06-20 19:11:41 +000048 ContiguousBlobAccumulator(uint64_t InitialOffset_)
49 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
Sean Silvad93323f2013-06-22 00:47:43 +000050 template <class Integer>
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +000051 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
Sean Silvad93323f2013-06-22 00:47:43 +000052 Offset = padToAlignment(Align);
53 return OS;
54 }
Sean Silva46dffff2013-06-13 22:20:01 +000055 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
56};
Sean Silva2a74f702013-06-15 00:31:46 +000057} // end anonymous namespace
Sean Silva46dffff2013-06-13 22:20:01 +000058
Simon Atanasyan35babf92014-04-06 09:02:55 +000059// Used to keep track of section and symbol names, so that in the YAML file
60// sections and symbols can be referenced by name instead of by index.
Sean Silva2a74f702013-06-15 00:31:46 +000061namespace {
Simon Atanasyan35babf92014-04-06 09:02:55 +000062class NameToIdxMap {
Sean Silvaa6423eb2013-06-15 00:25:26 +000063 StringMap<int> Map;
64public:
65 /// \returns true if name is already present in the map.
Simon Atanasyan35babf92014-04-06 09:02:55 +000066 bool addName(StringRef Name, unsigned i) {
David Blaikie5106ce72014-11-19 05:49:42 +000067 return !Map.insert(std::make_pair(Name, (int)i)).second;
Sean Silvaa6423eb2013-06-15 00:25:26 +000068 }
69 /// \returns true if name is not present in the map
Simon Atanasyan35babf92014-04-06 09:02:55 +000070 bool lookup(StringRef Name, unsigned &Idx) const {
71 StringMap<int>::const_iterator I = Map.find(Name);
Sean Silvaa6423eb2013-06-15 00:25:26 +000072 if (I == Map.end())
73 return true;
74 Idx = I->getValue();
75 return false;
76 }
77};
Sean Silva2a74f702013-06-15 00:31:46 +000078} // end anonymous namespace
Sean Silvaa6423eb2013-06-15 00:25:26 +000079
Sean Silva38205932013-06-13 22:19:48 +000080template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000081static size_t arrayDataSize(ArrayRef<T> A) {
82 return A.size() * sizeof(T);
Sean Silva38205932013-06-13 22:19:48 +000083}
84
85template <class T>
Will Dietz0b48c732013-10-12 21:29:16 +000086static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
87 OS.write((const char *)A.data(), arrayDataSize(A));
Sean Silva38205932013-06-13 22:19:48 +000088}
89
90template <class T>
91static void zero(T &Obj) {
92 memset(&Obj, 0, sizeof(Obj));
93}
94
Sean Silva08a75ae2013-06-20 19:11:44 +000095namespace {
96/// \brief "Single point of truth" for the ELF file construction.
97/// TODO: This class still has a ways to go before it is truly a "single
98/// point of truth".
99template <class ELFT>
100class ELFState {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000101 typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
102 typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
103 typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000104 typedef typename object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
105 typedef typename object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000106
Sean Silva08a75ae2013-06-20 19:11:44 +0000107 /// \brief The future ".strtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000108 StringTableBuilder DotStrtab{StringTableBuilder::ELF};
Sean Silva08a75ae2013-06-20 19:11:44 +0000109
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000110 /// \brief The future ".shstrtab" section.
Rafael Espindola21956e42015-10-23 21:48:05 +0000111 StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000112
Simon Atanasyan35babf92014-04-06 09:02:55 +0000113 NameToIdxMap SN2I;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000114 NameToIdxMap SymN2I;
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000115 const ELFYAML::Object &Doc;
Sean Silvac1c290b2013-06-20 20:59:34 +0000116
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000117 bool buildSectionIndex();
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000118 bool buildSymbolIndex(std::size_t &StartIndex,
119 const std::vector<ELFYAML::Symbol> &Symbols);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000120 void initELFHeader(Elf_Ehdr &Header);
121 bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
122 ContiguousBlobAccumulator &CBA);
123 void initSymtabSectionHeader(Elf_Shdr &SHeader,
124 ContiguousBlobAccumulator &CBA);
125 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
126 StringTableBuilder &STB,
127 ContiguousBlobAccumulator &CBA);
128 void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
129 std::vector<Elf_Sym> &Syms, unsigned SymbolBinding);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000130 void writeSectionContent(Elf_Shdr &SHeader,
131 const ELFYAML::RawContentSection &Section,
132 ContiguousBlobAccumulator &CBA);
133 bool writeSectionContent(Elf_Shdr &SHeader,
134 const ELFYAML::RelocationSection &Section,
135 ContiguousBlobAccumulator &CBA);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000136 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
137 ContiguousBlobAccumulator &CBA);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000138 bool writeSectionContent(Elf_Shdr &SHeader,
139 const ELFYAML::MipsABIFlags &Section,
140 ContiguousBlobAccumulator &CBA);
Sean Silva08a75ae2013-06-20 19:11:44 +0000141
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000142 // - SHT_NULL entry (placed first, i.e. 0'th entry)
143 // - symbol table (.symtab) (placed third to last)
144 // - string table (.strtab) (placed second to last)
145 // - section header string table (.shstrtab) (placed last)
146 unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; }
147 unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; }
148 unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
149 unsigned getSectionCount() const { return Doc.Sections.size() + 4; }
150
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000151 ELFState(const ELFYAML::Object &D) : Doc(D) {}
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000152
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000153public:
154 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
Sean Silva08a75ae2013-06-20 19:11:44 +0000155};
156} // end anonymous namespace
157
Sean Silva37e817c2013-06-21 00:33:01 +0000158template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000159void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
160 using namespace llvm::ELF;
161 zero(Header);
162 Header.e_ident[EI_MAG0] = 0x7f;
163 Header.e_ident[EI_MAG1] = 'E';
164 Header.e_ident[EI_MAG2] = 'L';
165 Header.e_ident[EI_MAG3] = 'F';
166 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
167 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
168 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
169 Header.e_ident[EI_VERSION] = EV_CURRENT;
170 Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
171 Header.e_ident[EI_ABIVERSION] = 0;
172 Header.e_type = Doc.Header.Type;
173 Header.e_machine = Doc.Header.Machine;
174 Header.e_version = EV_CURRENT;
175 Header.e_entry = Doc.Header.Entry;
176 Header.e_flags = Doc.Header.Flags;
177 Header.e_ehsize = sizeof(Elf_Ehdr);
178 Header.e_shentsize = sizeof(Elf_Shdr);
179 // Immediately following the ELF header.
180 Header.e_shoff = sizeof(Header);
181 Header.e_shnum = getSectionCount();
182 Header.e_shstrndx = getDotShStrTabSecNo();
183}
184
185template <class ELFT>
186bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
187 ContiguousBlobAccumulator &CBA) {
188 // Ensure SHN_UNDEF entry is present. An all-zero section header is a
189 // valid SHN_UNDEF entry since SHT_NULL == 0.
190 Elf_Shdr SHeader;
191 zero(SHeader);
192 SHeaders.push_back(SHeader);
193
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000194 for (const auto &Sec : Doc.Sections)
195 DotShStrtab.add(Sec->Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000196 DotShStrtab.finalize();
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000197
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000198 for (const auto &Sec : Doc.Sections) {
199 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000200 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000201 SHeader.sh_type = Sec->Type;
202 SHeader.sh_flags = Sec->Flags;
203 SHeader.sh_addr = Sec->Address;
204 SHeader.sh_addralign = Sec->AddressAlign;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000205
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000206 if (!Sec->Link.empty()) {
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000207 unsigned Index;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000208 if (SN2I.lookup(Sec->Link, Index)) {
209 errs() << "error: Unknown section referenced: '" << Sec->Link
210 << "' at YAML section '" << Sec->Name << "'.\n";
211 return false;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000212 }
213 SHeader.sh_link = Index;
214 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000215
216 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
217 writeSectionContent(SHeader, *S, CBA);
218 else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
219 if (S->Link.empty())
220 // For relocation section set link to .symtab by default.
221 SHeader.sh_link = getDotSymTabSecNo();
222
223 unsigned Index;
224 if (SN2I.lookup(S->Info, Index)) {
Michael J. Spencer572d7422015-08-27 23:11:20 +0000225 if (S->Info.getAsInteger(0, Index)) {
226 errs() << "error: Unknown section referenced: '" << S->Info
227 << "' at YAML section '" << S->Name << "'.\n";
228 return false;
229 }
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000230 }
231 SHeader.sh_info = Index;
232
233 if (!writeSectionContent(SHeader, *S, CBA))
234 return false;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000235 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
236 unsigned SymIdx;
237 if (SymN2I.lookup(S->Info, SymIdx)) {
238 errs() << "error: Unknown symbol referenced: '" << S->Info
239 << "' at YAML section '" << S->Name << "'.\n";
240 return false;
241 }
242 SHeader.sh_info = SymIdx;
243 if (!writeSectionContent(SHeader, *S, CBA))
244 return false;
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000245 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
246 if (!writeSectionContent(SHeader, *S, CBA))
247 return false;
Simon Atanasyan5db02762015-07-03 23:00:54 +0000248 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
249 SHeader.sh_entsize = 0;
250 SHeader.sh_size = S->Size;
251 // SHT_NOBITS section does not have content
252 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000253 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000254 } else
255 llvm_unreachable("Unknown section type");
256
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000257 SHeaders.push_back(SHeader);
258 }
259 return true;
260}
261
262template <class ELFT>
263void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
264 ContiguousBlobAccumulator &CBA) {
265 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000266 SHeader.sh_name = DotShStrtab.getOffset(".symtab");
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000267 SHeader.sh_type = ELF::SHT_SYMTAB;
268 SHeader.sh_link = getDotStrTabSecNo();
269 // One greater than symbol table index of the last local symbol.
270 SHeader.sh_info = Doc.Symbols.Local.size() + 1;
271 SHeader.sh_entsize = sizeof(Elf_Sym);
Simon Atanasyan3a120922015-07-09 18:23:02 +0000272 SHeader.sh_addralign = 8;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000273
274 std::vector<Elf_Sym> Syms;
275 {
276 // Ensure STN_UNDEF is present
277 Elf_Sym Sym;
278 zero(Sym);
279 Syms.push_back(Sym);
280 }
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000281
282 // Add symbol names to .strtab.
283 for (const auto &Sym : Doc.Symbols.Local)
284 DotStrtab.add(Sym.Name);
285 for (const auto &Sym : Doc.Symbols.Global)
286 DotStrtab.add(Sym.Name);
287 for (const auto &Sym : Doc.Symbols.Weak)
288 DotStrtab.add(Sym.Name);
Rafael Espindola21956e42015-10-23 21:48:05 +0000289 DotStrtab.finalize();
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000290
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000291 addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
292 addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
293 addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
294
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000295 writeArrayData(
296 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
297 makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000298 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
299}
300
301template <class ELFT>
302void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
303 StringTableBuilder &STB,
304 ContiguousBlobAccumulator &CBA) {
305 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000306 SHeader.sh_name = DotShStrtab.getOffset(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000307 SHeader.sh_type = ELF::SHT_STRTAB;
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000308 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)
309 << STB.data();
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000310 SHeader.sh_size = STB.data().size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000311 SHeader.sh_addralign = 1;
312}
313
314template <class ELFT>
315void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
316 std::vector<Elf_Sym> &Syms,
317 unsigned SymbolBinding) {
Simon Atanasyan048baca2014-03-14 06:53:30 +0000318 for (const auto &Sym : Symbols) {
Sean Silva6b083882013-06-18 23:14:03 +0000319 Elf_Sym Symbol;
320 zero(Symbol);
321 if (!Sym.Name.empty())
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000322 Symbol.st_name = DotStrtab.getOffset(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000323 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000324 if (!Sym.Section.empty()) {
325 unsigned Index;
Simon Atanasyan35babf92014-04-06 09:02:55 +0000326 if (SN2I.lookup(Sym.Section, Index)) {
Sean Silvac4afa6d2013-06-21 01:11:48 +0000327 errs() << "error: Unknown section referenced: '" << Sym.Section
328 << "' by YAML symbol " << Sym.Name << ".\n";
329 exit(1);
330 }
331 Symbol.st_shndx = Index;
332 } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000333 Symbol.st_value = Sym.Value;
Simon Atanasyan60e1a792014-11-06 22:46:24 +0000334 Symbol.st_other = Sym.Other;
Sean Silva05001b92013-06-20 20:59:47 +0000335 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000336 Syms.push_back(Symbol);
337 }
Sean Silvaaff51252013-06-21 00:27:50 +0000338}
339
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000340template <class ELFT>
341void
342ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
343 const ELFYAML::RawContentSection &Section,
344 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000345 assert(Section.Size >= Section.Content.binary_size() &&
346 "Section size and section content are inconsistent");
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000347 raw_ostream &OS =
348 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000349 Section.Content.writeAsBinary(OS);
350 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
351 OS.write(0);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000352 SHeader.sh_entsize = 0;
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000353 SHeader.sh_size = Section.Size;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000354}
355
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000356static bool isMips64EL(const ELFYAML::Object &Doc) {
357 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
358 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
359 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
360}
361
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000362template <class ELFT>
363bool
364ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
365 const ELFYAML::RelocationSection &Section,
366 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000367 assert((Section.Type == llvm::ELF::SHT_REL ||
368 Section.Type == llvm::ELF::SHT_RELA) &&
369 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000370
371 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
372 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
373 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
374
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000375 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000376
377 for (const auto &Rel : Section.Relocations) {
Adhemerval Zanella9f3dbff2015-04-22 15:26:43 +0000378 unsigned SymIdx = 0;
379 // Some special relocation, R_ARM_v4BX for instance, does not have
380 // an external reference. So it ignores the return value of lookup()
381 // here.
382 SymN2I.lookup(Rel.Symbol, SymIdx);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000383
384 if (IsRela) {
385 Elf_Rela REntry;
386 zero(REntry);
387 REntry.r_offset = Rel.Offset;
388 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000389 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000390 OS.write((const char *)&REntry, sizeof(REntry));
391 } else {
392 Elf_Rel REntry;
393 zero(REntry);
394 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000395 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000396 OS.write((const char *)&REntry, sizeof(REntry));
397 }
398 }
399 return true;
400}
401
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000402template <class ELFT>
403bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
404 const ELFYAML::Group &Section,
405 ContiguousBlobAccumulator &CBA) {
406 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000407 assert(Section.Type == llvm::ELF::SHT_GROUP &&
408 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000409
410 SHeader.sh_entsize = sizeof(Elf_Word);
411 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
412
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000413 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000414
415 for (auto member : Section.Members) {
416 Elf_Word SIdx;
417 unsigned int sectionIndex = 0;
418 if (member.sectionNameOrType == "GRP_COMDAT")
419 sectionIndex = llvm::ELF::GRP_COMDAT;
420 else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) {
421 errs() << "error: Unknown section referenced: '"
422 << member.sectionNameOrType << "' at YAML section' "
423 << Section.Name << "\n";
424 return false;
425 }
426 SIdx = sectionIndex;
427 OS.write((const char *)&SIdx, sizeof(SIdx));
428 }
429 return true;
430}
431
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000432template <class ELFT>
433bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
434 const ELFYAML::MipsABIFlags &Section,
435 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000436 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
437 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000438
439 object::Elf_Mips_ABIFlags<ELFT> Flags;
440 zero(Flags);
441 SHeader.sh_entsize = sizeof(Flags);
442 SHeader.sh_size = SHeader.sh_entsize;
443
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000444 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000445 Flags.version = Section.Version;
446 Flags.isa_level = Section.ISALevel;
447 Flags.isa_rev = Section.ISARevision;
448 Flags.gpr_size = Section.GPRSize;
449 Flags.cpr1_size = Section.CPR1Size;
450 Flags.cpr2_size = Section.CPR2Size;
451 Flags.fp_abi = Section.FpABI;
452 Flags.isa_ext = Section.ISAExtension;
453 Flags.ases = Section.ASEs;
454 Flags.flags1 = Section.Flags1;
455 Flags.flags2 = Section.Flags2;
456 OS.write((const char *)&Flags, sizeof(Flags));
457
458 return true;
459}
460
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000461template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
462 SN2I.addName(".symtab", getDotSymTabSecNo());
463 SN2I.addName(".strtab", getDotStrTabSecNo());
464 SN2I.addName(".shstrtab", getDotShStrTabSecNo());
Sean Silvaaff51252013-06-21 00:27:50 +0000465
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000466 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000467 StringRef Name = Doc.Sections[i]->Name;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000468 if (Name.empty())
469 continue;
470 // "+ 1" to take into account the SHT_NULL entry.
471 if (SN2I.addName(Name, i + 1)) {
472 errs() << "error: Repeated section name: '" << Name
473 << "' at YAML section number " << i << ".\n";
474 return false;
475 }
Sean Silvaaff51252013-06-21 00:27:50 +0000476 }
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000477 return true;
Sean Silva6b083882013-06-18 23:14:03 +0000478}
479
Sean Silvaf99309c2013-06-10 23:44:15 +0000480template <class ELFT>
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000481bool
482ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
483 const std::vector<ELFYAML::Symbol> &Symbols) {
484 for (const auto &Sym : Symbols) {
485 ++StartIndex;
486 if (Sym.Name.empty())
487 continue;
488 if (SymN2I.addName(Sym.Name, StartIndex)) {
489 errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n";
490 return false;
491 }
492 }
493 return true;
494}
495
496template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000497int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000498 ELFState<ELFT> State(Doc);
499 if (!State.buildSectionIndex())
500 return 1;
501
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000502 std::size_t StartSymIndex = 0;
503 if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
504 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
505 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
506 return 1;
507
Sean Silva38205932013-06-13 22:19:48 +0000508 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000509 State.initELFHeader(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000510
Sean Silva38205932013-06-13 22:19:48 +0000511 // TODO: Flesh out section header support.
512 // TODO: Program headers.
513
Sean Silva08a75ae2013-06-20 19:11:44 +0000514 // XXX: This offset is tightly coupled with the order that we write
515 // things to `OS`.
516 const size_t SectionContentBeginOffset =
517 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
518 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +0000519
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000520 // Doc might not contain .symtab, .strtab and .shstrtab sections,
521 // but we will emit them, so make sure to add them to ShStrTabSHeader.
522 State.DotShStrtab.add(".symtab");
523 State.DotShStrtab.add(".strtab");
524 State.DotShStrtab.add(".shstrtab");
525
Sean Silva38205932013-06-13 22:19:48 +0000526 std::vector<Elf_Shdr> SHeaders;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000527 if(!State.initSectionHeaders(SHeaders, CBA))
528 return 1;
Sean Silva38205932013-06-13 22:19:48 +0000529
Sean Silva82177572013-06-22 01:38:00 +0000530 // .symtab section.
531 Elf_Shdr SymtabSHeader;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000532 State.initSymtabSectionHeader(SymtabSHeader, CBA);
Sean Silva82177572013-06-22 01:38:00 +0000533 SHeaders.push_back(SymtabSHeader);
534
Sean Silva6b083882013-06-18 23:14:03 +0000535 // .strtab string table header.
Sean Silvac3131922013-06-18 21:37:50 +0000536 Elf_Shdr DotStrTabSHeader;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000537 State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
538 CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000539 SHeaders.push_back(DotStrTabSHeader);
Sean Silvac3131922013-06-18 21:37:50 +0000540
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000541 // .shstrtab string table header.
542 Elf_Shdr ShStrTabSHeader;
543 State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
544 CBA);
545 SHeaders.push_back(ShStrTabSHeader);
Sean Silvaf99309c2013-06-10 23:44:15 +0000546
547 OS.write((const char *)&Header, sizeof(Header));
Will Dietz0b48c732013-10-12 21:29:16 +0000548 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +0000549 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000550 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000551}
552
Sean Silva11caeba2013-06-22 01:03:35 +0000553static bool is64Bit(const ELFYAML::Object &Doc) {
554 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
555}
556
557static bool isLittleEndian(const ELFYAML::Object &Doc) {
558 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
559}
560
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000561int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
Sean Silvaf99309c2013-06-10 23:44:15 +0000562 ELFYAML::Object Doc;
563 YIn >> Doc;
564 if (YIn.error()) {
565 errs() << "yaml2obj: Failed to parse YAML file!\n";
566 return 1;
567 }
Sean Silva11caeba2013-06-22 01:03:35 +0000568 using object::ELFType;
Rafael Espindolaac729b42015-06-02 12:05:27 +0000569 typedef ELFType<support::little, true> LE64;
570 typedef ELFType<support::big, true> BE64;
571 typedef ELFType<support::little, false> LE32;
572 typedef ELFType<support::big, false> BE32;
Sean Silva11caeba2013-06-22 01:03:35 +0000573 if (is64Bit(Doc)) {
574 if (isLittleEndian(Doc))
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000575 return ELFState<LE64>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000576 else
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000577 return ELFState<BE64>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000578 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000579 if (isLittleEndian(Doc))
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000580 return ELFState<LE32>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000581 else
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000582 return ELFState<BE32>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000583 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000584}