1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief The ELF component of yaml2obj.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/MC/StringTableBuilder.h"
18 #include "llvm/Object/ELFObjectFile.h"
19 #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"
27 // 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
31 class ContiguousBlobAccumulator {
32 const uint64_t InitialOffset;
33 SmallVector<char, 128> Buf;
34 raw_svector_ostream OS;
36 /// \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)
42 return AlignedOffset; // == CurrentOffset;
46 ContiguousBlobAccumulator(uint64_t InitialOffset_)
47 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
48 template <class Integer>
49 raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
50 Offset = padToAlignment(Align);
53 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
55 } // end anonymous namespace
57 // 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.
63 /// \returns true if name is already present in the map.
64 bool addName(StringRef Name, unsigned i) {
65 return !Map.insert(std::make_pair(Name, (int)i)).second;
67 /// \returns true if name is not present in the map
68 bool lookup(StringRef Name, unsigned &Idx) const {
69 StringMap<int>::const_iterator I = Map.find(Name);
76 } // end anonymous namespace
79 static size_t arrayDataSize(ArrayRef<T> A) {
80 return A.size() * sizeof(T);
84 static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
85 OS.write((const char *)A.data(), arrayDataSize(A));
89 static void zero(T &Obj) {
90 memset(&Obj, 0, sizeof(Obj));
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
99 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;
102 typedef typename object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
103 typedef typename object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
105 /// \brief The future ".strtab" section.
106 StringTableBuilder DotStrtab;
108 /// \brief The future ".shstrtab" section.
109 StringTableBuilder DotShStrtab;
113 const ELFYAML::Object &Doc;
115 bool buildSectionIndex();
116 bool buildSymbolIndex(std::size_t &StartIndex,
117 const std::vector<ELFYAML::Symbol> &Symbols);
118 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);
128 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);
134 bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
135 ContiguousBlobAccumulator &CBA);
136 bool writeSectionContent(Elf_Shdr &SHeader,
137 const ELFYAML::MipsABIFlags &Section,
138 ContiguousBlobAccumulator &CBA);
140 // - 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; }
149 ELFState(const ELFYAML::Object &D) : Doc(D) {}
152 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
154 } // end anonymous namespace
156 template <class ELFT>
157 void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
158 using namespace llvm::ELF;
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();
183 template <class ELFT>
184 bool 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.
190 SHeaders.push_back(SHeader);
192 for (const auto &Sec : Doc.Sections)
193 DotShStrtab.add(Sec->Name);
194 DotShStrtab.finalize(StringTableBuilder::ELF);
196 for (const auto &Sec : Doc.Sections) {
198 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
199 SHeader.sh_type = Sec->Type;
200 SHeader.sh_flags = Sec->Flags;
201 SHeader.sh_addr = Sec->Address;
202 SHeader.sh_addralign = Sec->AddressAlign;
204 if (!Sec->Link.empty()) {
206 if (SN2I.lookup(Sec->Link, Index)) {
207 errs() << "error: Unknown section referenced: '" << Sec->Link
208 << "' at YAML section '" << Sec->Name << "'.\n";
211 SHeader.sh_link = Index;
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())) {
218 // For relocation section set link to .symtab by default.
219 SHeader.sh_link = getDotSymTabSecNo();
222 if (SN2I.lookup(S->Info, Index)) {
223 errs() << "error: Unknown section referenced: '" << S->Info
224 << "' at YAML section '" << S->Name << "'.\n";
227 SHeader.sh_info = Index;
229 if (!writeSectionContent(SHeader, *S, CBA))
231 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
233 if (SymN2I.lookup(S->Info, SymIdx)) {
234 errs() << "error: Unknown symbol referenced: '" << S->Info
235 << "' at YAML section '" << S->Name << "'.\n";
238 SHeader.sh_info = SymIdx;
239 if (!writeSectionContent(SHeader, *S, CBA))
241 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
242 if (!writeSectionContent(SHeader, *S, CBA))
245 llvm_unreachable("Unknown section type");
247 SHeaders.push_back(SHeader);
252 template <class ELFT>
253 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
254 ContiguousBlobAccumulator &CBA) {
256 SHeader.sh_name = DotShStrtab.getOffset(".symtab");
257 SHeader.sh_type = ELF::SHT_SYMTAB;
258 SHeader.sh_link = getDotStrTabSecNo();
259 // One greater than symbol table index of the last local symbol.
260 SHeader.sh_info = Doc.Symbols.Local.size() + 1;
261 SHeader.sh_entsize = sizeof(Elf_Sym);
263 std::vector<Elf_Sym> Syms;
265 // Ensure STN_UNDEF is present
271 // Add symbol names to .strtab.
272 for (const auto &Sym : Doc.Symbols.Local)
273 DotStrtab.add(Sym.Name);
274 for (const auto &Sym : Doc.Symbols.Global)
275 DotStrtab.add(Sym.Name);
276 for (const auto &Sym : Doc.Symbols.Weak)
277 DotStrtab.add(Sym.Name);
278 DotStrtab.finalize(StringTableBuilder::ELF);
280 addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
281 addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
282 addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
284 writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
286 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
289 template <class ELFT>
290 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
291 StringTableBuilder &STB,
292 ContiguousBlobAccumulator &CBA) {
294 SHeader.sh_name = DotShStrtab.getOffset(Name);
295 SHeader.sh_type = ELF::SHT_STRTAB;
296 CBA.getOSAndAlignedOffset(SHeader.sh_offset) << STB.data();
297 SHeader.sh_size = STB.data().size();
298 SHeader.sh_addralign = 1;
301 template <class ELFT>
302 void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
303 std::vector<Elf_Sym> &Syms,
304 unsigned SymbolBinding) {
305 for (const auto &Sym : Symbols) {
308 if (!Sym.Name.empty())
309 Symbol.st_name = DotStrtab.getOffset(Sym.Name);
310 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
311 if (!Sym.Section.empty()) {
313 if (SN2I.lookup(Sym.Section, Index)) {
314 errs() << "error: Unknown section referenced: '" << Sym.Section
315 << "' by YAML symbol " << Sym.Name << ".\n";
318 Symbol.st_shndx = Index;
319 } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
320 Symbol.st_value = Sym.Value;
321 Symbol.st_other = Sym.Other;
322 Symbol.st_size = Sym.Size;
323 Syms.push_back(Symbol);
327 template <class ELFT>
329 ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
330 const ELFYAML::RawContentSection &Section,
331 ContiguousBlobAccumulator &CBA) {
332 assert(Section.Size >= Section.Content.binary_size() &&
333 "Section size and section content are inconsistent");
334 raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
335 Section.Content.writeAsBinary(OS);
336 for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
338 SHeader.sh_entsize = 0;
339 SHeader.sh_size = Section.Size;
342 static bool isMips64EL(const ELFYAML::Object &Doc) {
343 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
344 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
345 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
348 template <class ELFT>
350 ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
351 const ELFYAML::RelocationSection &Section,
352 ContiguousBlobAccumulator &CBA) {
353 if (Section.Type != llvm::ELF::SHT_REL &&
354 Section.Type != llvm::ELF::SHT_RELA) {
355 errs() << "error: Invalid relocation section type.\n";
359 bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
360 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
361 SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
363 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
365 for (const auto &Rel : Section.Relocations) {
367 // Some special relocation, R_ARM_v4BX for instance, does not have
368 // an external reference. So it ignores the return value of lookup()
370 SymN2I.lookup(Rel.Symbol, SymIdx);
375 REntry.r_offset = Rel.Offset;
376 REntry.r_addend = Rel.Addend;
377 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
378 OS.write((const char *)&REntry, sizeof(REntry));
382 REntry.r_offset = Rel.Offset;
383 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
384 OS.write((const char *)&REntry, sizeof(REntry));
390 template <class ELFT>
391 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
392 const ELFYAML::Group &Section,
393 ContiguousBlobAccumulator &CBA) {
394 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
395 if (Section.Type != llvm::ELF::SHT_GROUP) {
396 errs() << "error: Invalid section type.\n";
400 SHeader.sh_entsize = sizeof(Elf_Word);
401 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
403 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
405 for (auto member : Section.Members) {
407 unsigned int sectionIndex = 0;
408 if (member.sectionNameOrType == "GRP_COMDAT")
409 sectionIndex = llvm::ELF::GRP_COMDAT;
410 else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) {
411 errs() << "error: Unknown section referenced: '"
412 << member.sectionNameOrType << "' at YAML section' "
413 << Section.Name << "\n";
417 OS.write((const char *)&SIdx, sizeof(SIdx));
422 template <class ELFT>
423 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
424 const ELFYAML::MipsABIFlags &Section,
425 ContiguousBlobAccumulator &CBA) {
426 if (Section.Type != llvm::ELF::SHT_MIPS_ABIFLAGS) {
427 errs() << "error: Invalid section type.\n";
431 object::Elf_Mips_ABIFlags<ELFT> Flags;
433 SHeader.sh_entsize = sizeof(Flags);
434 SHeader.sh_size = SHeader.sh_entsize;
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));
453 template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
454 SN2I.addName(".symtab", getDotSymTabSecNo());
455 SN2I.addName(".strtab", getDotStrTabSecNo());
456 SN2I.addName(".shstrtab", getDotShStrTabSecNo());
458 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
459 StringRef Name = Doc.Sections[i]->Name;
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";
472 template <class ELFT>
474 ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
475 const std::vector<ELFYAML::Symbol> &Symbols) {
476 for (const auto &Sym : Symbols) {
478 if (Sym.Name.empty())
480 if (SymN2I.addName(Sym.Name, StartIndex)) {
481 errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n";
488 template <class ELFT>
489 int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
490 ELFState<ELFT> State(Doc);
491 if (!State.buildSectionIndex())
494 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))
501 State.initELFHeader(Header);
503 // TODO: Flesh out section header support.
504 // TODO: Program headers.
506 // XXX: This offset is tightly coupled with the order that we write
508 const size_t SectionContentBeginOffset =
509 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
510 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
512 // 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");
518 std::vector<Elf_Shdr> SHeaders;
519 if(!State.initSectionHeaders(SHeaders, CBA))
523 Elf_Shdr SymtabSHeader;
524 State.initSymtabSectionHeader(SymtabSHeader, CBA);
525 SHeaders.push_back(SymtabSHeader);
527 // .strtab string table header.
528 Elf_Shdr DotStrTabSHeader;
529 State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
531 SHeaders.push_back(DotStrTabSHeader);
533 // .shstrtab string table header.
534 Elf_Shdr ShStrTabSHeader;
535 State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
537 SHeaders.push_back(ShStrTabSHeader);
539 OS.write((const char *)&Header, sizeof(Header));
540 writeArrayData(OS, makeArrayRef(SHeaders));
541 CBA.writeBlobToStream(OS);
545 static bool is64Bit(const ELFYAML::Object &Doc) {
546 return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
549 static bool isLittleEndian(const ELFYAML::Object &Doc) {
550 return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
553 int yaml2elf(yaml::Input &YIn, raw_ostream &Out) {
557 errs() << "yaml2obj: Failed to parse YAML file!\n";
560 using object::ELFType;
561 typedef ELFType<support::little, 8, true> LE64;
562 typedef ELFType<support::big, 8, true> BE64;
563 typedef ELFType<support::little, 4, false> LE32;
564 typedef ELFType<support::big, 4, false> BE32;
566 if (isLittleEndian(Doc))
567 return ELFState<LE64>::writeELF(Out, Doc);
569 return ELFState<BE64>::writeELF(Out, Doc);
571 if (isLittleEndian(Doc))
572 return ELFState<LE32>::writeELF(Out, Doc);
574 return ELFState<BE32>::writeELF(Out, Doc);