blob: 4940e837367a287ef8e15dfb5d5ef19f0f43ef1a [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) {
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();
41 uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
42 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.
108 StringTableBuilder DotStrtab;
Sean Silva08a75ae2013-06-20 19:11:44 +0000109
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000110 /// \brief The future ".shstrtab" section.
111 StringTableBuilder DotShStrtab;
112
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);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000196 DotShStrtab.finalize(StringTableBuilder::ELF);
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)) {
225 errs() << "error: Unknown section referenced: '" << S->Info
226 << "' at YAML section '" << S->Name << "'.\n";
227 return false;
228 }
229 SHeader.sh_info = Index;
230
231 if (!writeSectionContent(SHeader, *S, CBA))
232 return false;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000233 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
234 unsigned SymIdx;
235 if (SymN2I.lookup(S->Info, SymIdx)) {
236 errs() << "error: Unknown symbol referenced: '" << S->Info
237 << "' at YAML section '" << S->Name << "'.\n";
238 return false;
239 }
240 SHeader.sh_info = SymIdx;
241 if (!writeSectionContent(SHeader, *S, CBA))
242 return false;
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000243 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
244 if (!writeSectionContent(SHeader, *S, CBA))
245 return false;
Simon Atanasyan5db02762015-07-03 23:00:54 +0000246 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
247 SHeader.sh_entsize = 0;
248 SHeader.sh_size = S->Size;
249 // SHT_NOBITS section does not have content
250 // so just to setup the section offset.
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000251 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000252 } else
253 llvm_unreachable("Unknown section type");
254
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000255 SHeaders.push_back(SHeader);
256 }
257 return true;
258}
259
260template <class ELFT>
261void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
262 ContiguousBlobAccumulator &CBA) {
263 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000264 SHeader.sh_name = DotShStrtab.getOffset(".symtab");
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000265 SHeader.sh_type = ELF::SHT_SYMTAB;
266 SHeader.sh_link = getDotStrTabSecNo();
267 // One greater than symbol table index of the last local symbol.
268 SHeader.sh_info = Doc.Symbols.Local.size() + 1;
269 SHeader.sh_entsize = sizeof(Elf_Sym);
270
271 std::vector<Elf_Sym> Syms;
272 {
273 // Ensure STN_UNDEF is present
274 Elf_Sym Sym;
275 zero(Sym);
276 Syms.push_back(Sym);
277 }
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000278
279 // Add symbol names to .strtab.
280 for (const auto &Sym : Doc.Symbols.Local)
281 DotStrtab.add(Sym.Name);
282 for (const auto &Sym : Doc.Symbols.Global)
283 DotStrtab.add(Sym.Name);
284 for (const auto &Sym : Doc.Symbols.Weak)
285 DotStrtab.add(Sym.Name);
Hans Wennborgf26bfc12014-09-29 22:43:20 +0000286 DotStrtab.finalize(StringTableBuilder::ELF);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000287
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000288 addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
289 addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
290 addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
291
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000292 writeArrayData(
293 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
294 makeArrayRef(Syms));
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000295 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
296}
297
298template <class ELFT>
299void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
300 StringTableBuilder &STB,
301 ContiguousBlobAccumulator &CBA) {
302 zero(SHeader);
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000303 SHeader.sh_name = DotShStrtab.getOffset(Name);
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000304 SHeader.sh_type = ELF::SHT_STRTAB;
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000305 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign)
306 << STB.data();
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000307 SHeader.sh_size = STB.data().size();
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000308 SHeader.sh_addralign = 1;
309}
310
311template <class ELFT>
312void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
313 std::vector<Elf_Sym> &Syms,
314 unsigned SymbolBinding) {
Simon Atanasyan048baca2014-03-14 06:53:30 +0000315 for (const auto &Sym : Symbols) {
Sean Silva6b083882013-06-18 23:14:03 +0000316 Elf_Sym Symbol;
317 zero(Symbol);
318 if (!Sym.Name.empty())
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000319 Symbol.st_name = DotStrtab.getOffset(Sym.Name);
Sean Silvaaff51252013-06-21 00:27:50 +0000320 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
Sean Silvac4afa6d2013-06-21 01:11:48 +0000321 if (!Sym.Section.empty()) {
322 unsigned Index;
Simon Atanasyan35babf92014-04-06 09:02:55 +0000323 if (SN2I.lookup(Sym.Section, Index)) {
Sean Silvac4afa6d2013-06-21 01:11:48 +0000324 errs() << "error: Unknown section referenced: '" << Sym.Section
325 << "' by YAML symbol " << Sym.Name << ".\n";
326 exit(1);
327 }
328 Symbol.st_shndx = Index;
329 } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
Sean Silva05001b92013-06-20 20:59:47 +0000330 Symbol.st_value = Sym.Value;
Simon Atanasyan60e1a792014-11-06 22:46:24 +0000331 Symbol.st_other = Sym.Other;
Sean Silva05001b92013-06-20 20:59:47 +0000332 Symbol.st_size = Sym.Size;
Sean Silva6b083882013-06-18 23:14:03 +0000333 Syms.push_back(Symbol);
334 }
Sean Silvaaff51252013-06-21 00:27:50 +0000335}
336
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000337template <class ELFT>
338void
339ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
340 const ELFYAML::RawContentSection &Section,
341 ContiguousBlobAccumulator &CBA) {
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000342 assert(Section.Size >= Section.Content.binary_size() &&
343 "Section size and section content are inconsistent");
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000344 raw_ostream &OS =
345 CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000346 Section.Content.writeAsBinary(OS);
347 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
348 OS.write(0);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000349 SHeader.sh_entsize = 0;
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000350 SHeader.sh_size = Section.Size;
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000351}
352
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000353static bool isMips64EL(const ELFYAML::Object &Doc) {
354 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
355 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
356 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
357}
358
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000359template <class ELFT>
360bool
361ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
362 const ELFYAML::RelocationSection &Section,
363 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000364 assert((Section.Type == llvm::ELF::SHT_REL ||
365 Section.Type == llvm::ELF::SHT_RELA) &&
366 "Section type is not SHT_REL nor SHT_RELA");
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000367
368 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
369 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
370 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
371
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000372 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000373
374 for (const auto &Rel : Section.Relocations) {
Adhemerval Zanella9f3dbff2015-04-22 15:26:43 +0000375 unsigned SymIdx = 0;
376 // Some special relocation, R_ARM_v4BX for instance, does not have
377 // an external reference. So it ignores the return value of lookup()
378 // here.
379 SymN2I.lookup(Rel.Symbol, SymIdx);
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000380
381 if (IsRela) {
382 Elf_Rela REntry;
383 zero(REntry);
384 REntry.r_offset = Rel.Offset;
385 REntry.r_addend = Rel.Addend;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000386 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000387 OS.write((const char *)&REntry, sizeof(REntry));
388 } else {
389 Elf_Rel REntry;
390 zero(REntry);
391 REntry.r_offset = Rel.Offset;
Simon Atanasyan5d19c672015-01-25 13:29:25 +0000392 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000393 OS.write((const char *)&REntry, sizeof(REntry));
394 }
395 }
396 return true;
397}
398
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000399template <class ELFT>
400bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
401 const ELFYAML::Group &Section,
402 ContiguousBlobAccumulator &CBA) {
403 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000404 assert(Section.Type == llvm::ELF::SHT_GROUP &&
405 "Section type is not SHT_GROUP");
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000406
407 SHeader.sh_entsize = sizeof(Elf_Word);
408 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
409
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000410 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000411
412 for (auto member : Section.Members) {
413 Elf_Word SIdx;
414 unsigned int sectionIndex = 0;
415 if (member.sectionNameOrType == "GRP_COMDAT")
416 sectionIndex = llvm::ELF::GRP_COMDAT;
417 else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) {
418 errs() << "error: Unknown section referenced: '"
419 << member.sectionNameOrType << "' at YAML section' "
420 << Section.Name << "\n";
421 return false;
422 }
423 SIdx = sectionIndex;
424 OS.write((const char *)&SIdx, sizeof(SIdx));
425 }
426 return true;
427}
428
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000429template <class ELFT>
430bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
431 const ELFYAML::MipsABIFlags &Section,
432 ContiguousBlobAccumulator &CBA) {
Simon Atanasyan8eb9d1b2015-05-08 07:05:04 +0000433 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
434 "Section type is not SHT_MIPS_ABIFLAGS");
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000435
436 object::Elf_Mips_ABIFlags<ELFT> Flags;
437 zero(Flags);
438 SHeader.sh_entsize = sizeof(Flags);
439 SHeader.sh_size = SHeader.sh_entsize;
440
Simon Atanasyan22c4c9e2015-07-08 10:12:40 +0000441 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000442 Flags.version = Section.Version;
443 Flags.isa_level = Section.ISALevel;
444 Flags.isa_rev = Section.ISARevision;
445 Flags.gpr_size = Section.GPRSize;
446 Flags.cpr1_size = Section.CPR1Size;
447 Flags.cpr2_size = Section.CPR2Size;
448 Flags.fp_abi = Section.FpABI;
449 Flags.isa_ext = Section.ISAExtension;
450 Flags.ases = Section.ASEs;
451 Flags.flags1 = Section.Flags1;
452 Flags.flags2 = Section.Flags2;
453 OS.write((const char *)&Flags, sizeof(Flags));
454
455 return true;
456}
457
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000458template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
459 SN2I.addName(".symtab", getDotSymTabSecNo());
460 SN2I.addName(".strtab", getDotStrTabSecNo());
461 SN2I.addName(".shstrtab", getDotShStrTabSecNo());
Sean Silvaaff51252013-06-21 00:27:50 +0000462
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000463 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000464 StringRef Name = Doc.Sections[i]->Name;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000465 if (Name.empty())
466 continue;
467 // "+ 1" to take into account the SHT_NULL entry.
468 if (SN2I.addName(Name, i + 1)) {
469 errs() << "error: Repeated section name: '" << Name
470 << "' at YAML section number " << i << ".\n";
471 return false;
472 }
Sean Silvaaff51252013-06-21 00:27:50 +0000473 }
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000474 return true;
Sean Silva6b083882013-06-18 23:14:03 +0000475}
476
Sean Silvaf99309c2013-06-10 23:44:15 +0000477template <class ELFT>
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000478bool
479ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
480 const std::vector<ELFYAML::Symbol> &Symbols) {
481 for (const auto &Sym : Symbols) {
482 ++StartIndex;
483 if (Sym.Name.empty())
484 continue;
485 if (SymN2I.addName(Sym.Name, StartIndex)) {
486 errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n";
487 return false;
488 }
489 }
490 return true;
491}
492
493template <class ELFT>
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000494int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Simon Atanasyan220c54a2014-04-02 16:34:40 +0000495 ELFState<ELFT> State(Doc);
496 if (!State.buildSectionIndex())
497 return 1;
498
Simon Atanasyan42ac0dd2014-04-11 04:13:39 +0000499 std::size_t StartSymIndex = 0;
500 if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
501 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
502 !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
503 return 1;
504
Sean Silva38205932013-06-13 22:19:48 +0000505 Elf_Ehdr Header;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000506 State.initELFHeader(Header);
Sean Silvaf99309c2013-06-10 23:44:15 +0000507
Sean Silva38205932013-06-13 22:19:48 +0000508 // TODO: Flesh out section header support.
509 // TODO: Program headers.
510
Sean Silva08a75ae2013-06-20 19:11:44 +0000511 // XXX: This offset is tightly coupled with the order that we write
512 // things to `OS`.
513 const size_t SectionContentBeginOffset =
514 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
515 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
Sean Silvac1c290b2013-06-20 20:59:34 +0000516
Hans Wennborg59f0cba2014-04-30 19:38:09 +0000517 // Doc might not contain .symtab, .strtab and .shstrtab sections,
518 // but we will emit them, so make sure to add them to ShStrTabSHeader.
519 State.DotShStrtab.add(".symtab");
520 State.DotShStrtab.add(".strtab");
521 State.DotShStrtab.add(".shstrtab");
522
Sean Silva38205932013-06-13 22:19:48 +0000523 std::vector<Elf_Shdr> SHeaders;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000524 if(!State.initSectionHeaders(SHeaders, CBA))
525 return 1;
Sean Silva38205932013-06-13 22:19:48 +0000526
Sean Silva82177572013-06-22 01:38:00 +0000527 // .symtab section.
528 Elf_Shdr SymtabSHeader;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000529 State.initSymtabSectionHeader(SymtabSHeader, CBA);
Sean Silva82177572013-06-22 01:38:00 +0000530 SHeaders.push_back(SymtabSHeader);
531
Sean Silva6b083882013-06-18 23:14:03 +0000532 // .strtab string table header.
Sean Silvac3131922013-06-18 21:37:50 +0000533 Elf_Shdr DotStrTabSHeader;
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000534 State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
535 CBA);
Sean Silva7d617222013-06-22 01:06:12 +0000536 SHeaders.push_back(DotStrTabSHeader);
Sean Silvac3131922013-06-18 21:37:50 +0000537
Simon Atanasyan3ee21b02014-04-02 16:34:54 +0000538 // .shstrtab string table header.
539 Elf_Shdr ShStrTabSHeader;
540 State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
541 CBA);
542 SHeaders.push_back(ShStrTabSHeader);
Sean Silvaf99309c2013-06-10 23:44:15 +0000543
544 OS.write((const char *)&Header, sizeof(Header));
Will Dietz0b48c732013-10-12 21:29:16 +0000545 writeArrayData(OS, makeArrayRef(SHeaders));
Sean Silva46dffff2013-06-13 22:20:01 +0000546 CBA.writeBlobToStream(OS);
Sean Silva415d93f2013-06-17 20:14:59 +0000547 return 0;
Sean Silvaf99309c2013-06-10 23:44:15 +0000548}
549
Sean Silva11caeba2013-06-22 01:03:35 +0000550static bool is64Bit(const ELFYAML::Object &Doc) {
551 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
552}
553
554static bool isLittleEndian(const ELFYAML::Object &Doc) {
555 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
556}
557
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000558int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
Sean Silvaf99309c2013-06-10 23:44:15 +0000559 ELFYAML::Object Doc;
560 YIn >> Doc;
561 if (YIn.error()) {
562 errs() << "yaml2obj: Failed to parse YAML file!\n";
563 return 1;
564 }
Sean Silva11caeba2013-06-22 01:03:35 +0000565 using object::ELFType;
Rafael Espindolaac729b42015-06-02 12:05:27 +0000566 typedef ELFType<support::little, true> LE64;
567 typedef ELFType<support::big, true> BE64;
568 typedef ELFType<support::little, false> LE32;
569 typedef ELFType<support::big, false> BE32;
Sean Silva11caeba2013-06-22 01:03:35 +0000570 if (is64Bit(Doc)) {
571 if (isLittleEndian(Doc))
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000572 return ELFState<LE64>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000573 else
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000574 return ELFState<BE64>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000575 } else {
Sean Silva11caeba2013-06-22 01:03:35 +0000576 if (isLittleEndian(Doc))
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000577 return ELFState<LE32>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000578 else
Simon Atanasyan73e047e2014-05-15 16:14:02 +0000579 return ELFState<BE32>::writeELF(Out, Doc);
Sean Silvaf99309c2013-06-10 23:44:15 +0000580 }
Sean Silvaf99309c2013-06-10 23:44:15 +0000581}