1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// The ELF component of yaml2obj.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/MC/StringTableBuilder.h"
18 #include "llvm/Object/ELFObjectFile.h"
19 #include "llvm/ObjectYAML/ELFYAML.h"
20 #include "llvm/ObjectYAML/yaml2obj.h"
21 #include "llvm/Support/EndianStream.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/WithColor.h"
24 #include "llvm/Support/YAMLTraits.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 using namespace llvm;
28 
29 // This class is used to build up a contiguous binary blob while keeping
30 // track of an offset in the output (which notionally begins at
31 // `InitialOffset`).
32 namespace {
33 class ContiguousBlobAccumulator {
34   const uint64_t InitialOffset;
35   SmallVector<char, 128> Buf;
36   raw_svector_ostream OS;
37 
38   /// \returns The new offset.
39   uint64_t padToAlignment(unsigned Align) {
40     if (Align == 0)
41       Align = 1;
42     uint64_t CurrentOffset = InitialOffset + OS.tell();
43     uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
44     OS.write_zeros(AlignedOffset - CurrentOffset);
45     return AlignedOffset; // == CurrentOffset;
46   }
47 
48 public:
49   ContiguousBlobAccumulator(uint64_t InitialOffset_)
50       : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
51   template <class Integer>
52   raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
53     Offset = padToAlignment(Align);
54     return OS;
55   }
56   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
57 };
58 
59 // 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.
61 class NameToIdxMap {
62   StringMap<unsigned> Map;
63 
64 public:
65   /// \Returns false if name is already present in the map.
66   bool addName(StringRef Name, unsigned Ndx) {
67     return Map.insert({Name, Ndx}).second;
68   }
69   /// \Returns false if name is not present in the map.
70   bool lookup(StringRef Name, unsigned &Idx) const {
71     auto I = Map.find(Name);
72     if (I == Map.end())
73       return false;
74     Idx = I->getValue();
75     return true;
76   }
77   /// Asserts if name is not present in the map.
78   unsigned get(StringRef Name) const {
79     unsigned Idx;
80     if (lookup(Name, Idx))
81       return Idx;
82     assert(false && "Expected section not found in index");
83     return 0;
84   }
85   unsigned size() const { return Map.size(); }
86 };
87 
88 /// "Single point of truth" for the ELF file construction.
89 /// TODO: This class still has a ways to go before it is truly a "single
90 /// point of truth".
91 template <class ELFT> class ELFState {
92   typedef typename ELFT::Ehdr Elf_Ehdr;
93   typedef typename ELFT::Phdr Elf_Phdr;
94   typedef typename ELFT::Shdr Elf_Shdr;
95   typedef typename ELFT::Sym Elf_Sym;
96   typedef typename ELFT::Rel Elf_Rel;
97   typedef typename ELFT::Rela Elf_Rela;
98   typedef typename ELFT::Relr Elf_Relr;
99   typedef typename ELFT::Dyn Elf_Dyn;
100 
101   enum class SymtabType { Static, Dynamic };
102 
103   /// The future ".strtab" section.
104   StringTableBuilder DotStrtab{StringTableBuilder::ELF};
105 
106   /// The future ".shstrtab" section.
107   StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
108 
109   /// The future ".dynstr" section.
110   StringTableBuilder DotDynstr{StringTableBuilder::ELF};
111 
112   NameToIdxMap SN2I;
113   NameToIdxMap SymN2I;
114   ELFYAML::Object &Doc;
115 
116   bool buildSectionIndex();
117   bool buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols);
118   void initELFHeader(Elf_Ehdr &Header);
119   void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
120   bool initImplicitHeader(ELFState<ELFT> &State, ContiguousBlobAccumulator &CBA,
121                           Elf_Shdr &Header, StringRef SecName,
122                           ELFYAML::Section *YAMLSec);
123   bool initSectionHeaders(ELFState<ELFT> &State,
124                           std::vector<Elf_Shdr> &SHeaders,
125                           ContiguousBlobAccumulator &CBA);
126   void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
127                                ContiguousBlobAccumulator &CBA,
128                                ELFYAML::Section *YAMLSec);
129   void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
130                                StringTableBuilder &STB,
131                                ContiguousBlobAccumulator &CBA,
132                                ELFYAML::Section *YAMLSec);
133   void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
134                               std::vector<Elf_Shdr> &SHeaders);
135   bool writeSectionContent(Elf_Shdr &SHeader,
136                            const ELFYAML::RawContentSection &Section,
137                            ContiguousBlobAccumulator &CBA);
138   bool writeSectionContent(Elf_Shdr &SHeader,
139                            const ELFYAML::RelocationSection &Section,
140                            ContiguousBlobAccumulator &CBA);
141   bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
142                            ContiguousBlobAccumulator &CBA);
143   bool writeSectionContent(Elf_Shdr &SHeader,
144                            const ELFYAML::SymverSection &Section,
145                            ContiguousBlobAccumulator &CBA);
146   bool writeSectionContent(Elf_Shdr &SHeader,
147                            const ELFYAML::VerneedSection &Section,
148                            ContiguousBlobAccumulator &CBA);
149   bool writeSectionContent(Elf_Shdr &SHeader,
150                            const ELFYAML::VerdefSection &Section,
151                            ContiguousBlobAccumulator &CBA);
152   bool writeSectionContent(Elf_Shdr &SHeader,
153                            const ELFYAML::MipsABIFlags &Section,
154                            ContiguousBlobAccumulator &CBA);
155   bool writeSectionContent(Elf_Shdr &SHeader,
156                            const ELFYAML::DynamicSection &Section,
157                            ContiguousBlobAccumulator &CBA);
158   ELFState(ELFYAML::Object &D);
159 
160 public:
161   static int writeELF(raw_ostream &OS, ELFYAML::Object &Doc);
162 
163 private:
164   void finalizeStrings();
165 };
166 } // end anonymous namespace
167 
168 template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
169   return A.size() * sizeof(T);
170 }
171 
172 template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
173   OS.write((const char *)A.data(), arrayDataSize(A));
174 }
175 
176 template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
177 
178 template <class ELFT> ELFState<ELFT>::ELFState(ELFYAML::Object &D) : Doc(D) {
179   StringSet<> DocSections;
180   for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
181     if (!D->Name.empty())
182       DocSections.insert(D->Name);
183 
184   // Insert SHT_NULL section implicitly when it is not defined in YAML.
185   if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
186     Doc.Sections.insert(
187         Doc.Sections.begin(),
188         llvm::make_unique<ELFYAML::Section>(
189             ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
190 
191   std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
192   if (!Doc.DynamicSymbols.empty())
193     ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
194 
195   // Insert placeholders for implicit sections that are not
196   // defined explicitly in YAML.
197   for (StringRef SecName : ImplicitSections) {
198     if (DocSections.count(SecName))
199       continue;
200 
201     std::unique_ptr<ELFYAML::Section> Sec = llvm::make_unique<ELFYAML::Section>(
202         ELFYAML::Section::SectionKind::RawContent, true /*IsImplicit*/);
203     Sec->Name = SecName;
204     Doc.Sections.push_back(std::move(Sec));
205   }
206 }
207 
208 template <class ELFT> void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
209   using namespace llvm::ELF;
210   zero(Header);
211   Header.e_ident[EI_MAG0] = 0x7f;
212   Header.e_ident[EI_MAG1] = 'E';
213   Header.e_ident[EI_MAG2] = 'L';
214   Header.e_ident[EI_MAG3] = 'F';
215   Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
216   Header.e_ident[EI_DATA] = Doc.Header.Data;
217   Header.e_ident[EI_VERSION] = EV_CURRENT;
218   Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
219   Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
220   Header.e_type = Doc.Header.Type;
221   Header.e_machine = Doc.Header.Machine;
222   Header.e_version = EV_CURRENT;
223   Header.e_entry = Doc.Header.Entry;
224   Header.e_phoff = sizeof(Header);
225   Header.e_flags = Doc.Header.Flags;
226   Header.e_ehsize = sizeof(Elf_Ehdr);
227   Header.e_phentsize = sizeof(Elf_Phdr);
228   Header.e_phnum = Doc.ProgramHeaders.size();
229 
230   Header.e_shentsize =
231       Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
232   // Immediately following the ELF header and program headers.
233   Header.e_shoff =
234       Doc.Header.SHOffset
235           ? (typename ELFT::uint)(*Doc.Header.SHOffset)
236           : sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
237   Header.e_shnum =
238       Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size();
239   Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
240                                           : SN2I.get(".shstrtab");
241 }
242 
243 template <class ELFT>
244 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
245   for (const auto &YamlPhdr : Doc.ProgramHeaders) {
246     Elf_Phdr Phdr;
247     Phdr.p_type = YamlPhdr.Type;
248     Phdr.p_flags = YamlPhdr.Flags;
249     Phdr.p_vaddr = YamlPhdr.VAddr;
250     Phdr.p_paddr = YamlPhdr.PAddr;
251     PHeaders.push_back(Phdr);
252   }
253 }
254 
255 static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
256                                 StringRef IndexSrc, unsigned &IndexDest) {
257   if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
258     WithColor::error() << "Unknown section referenced: '" << IndexSrc
259                        << "' at YAML section '" << SecName << "'.\n";
260     return false;
261   }
262   return true;
263 }
264 
265 template <class ELFT>
266 bool ELFState<ELFT>::initImplicitHeader(ELFState<ELFT> &State,
267                                         ContiguousBlobAccumulator &CBA,
268                                         Elf_Shdr &Header, StringRef SecName,
269                                         ELFYAML::Section *YAMLSec) {
270   // Check if the header was already initialized.
271   if (Header.sh_offset)
272     return false;
273 
274   if (SecName == ".symtab")
275     State.initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
276   else if (SecName == ".strtab")
277     State.initStrtabSectionHeader(Header, SecName, State.DotStrtab, CBA,
278                                   YAMLSec);
279   else if (SecName == ".shstrtab")
280     State.initStrtabSectionHeader(Header, SecName, State.DotShStrtab, CBA,
281                                   YAMLSec);
282 
283   else if (SecName == ".dynsym")
284     State.initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
285   else if (SecName == ".dynstr")
286     State.initStrtabSectionHeader(Header, SecName, State.DotDynstr, CBA,
287                                   YAMLSec);
288   else
289     return false;
290 
291   // Override the sh_offset/sh_size fields if requested.
292   if (YAMLSec) {
293     if (YAMLSec->ShOffset)
294       Header.sh_offset = *YAMLSec->ShOffset;
295     if (YAMLSec->ShSize)
296       Header.sh_size = *YAMLSec->ShSize;
297   }
298 
299   return true;
300 }
301 
302 static StringRef dropUniqueSuffix(StringRef S) {
303   size_t SuffixPos = S.rfind(" [");
304   if (SuffixPos == StringRef::npos)
305     return S;
306   return S.substr(0, SuffixPos);
307 }
308 
309 template <class ELFT>
310 bool ELFState<ELFT>::initSectionHeaders(ELFState<ELFT> &State,
311                                         std::vector<Elf_Shdr> &SHeaders,
312                                         ContiguousBlobAccumulator &CBA) {
313   // Ensure SHN_UNDEF entry is present. An all-zero section header is a
314   // valid SHN_UNDEF entry since SHT_NULL == 0.
315   SHeaders.resize(Doc.Sections.size());
316 
317   for (size_t I = 0; I < Doc.Sections.size(); ++I) {
318     ELFYAML::Section *Sec = Doc.Sections[I].get();
319     if (I == 0 && Sec->IsImplicit)
320       continue;
321 
322     // We have a few sections like string or symbol tables that are usually
323     // added implicitly to the end. However, if they are explicitly specified
324     // in the YAML, we need to write them here. This ensures the file offset
325     // remains correct.
326     Elf_Shdr &SHeader = SHeaders[I];
327     if (initImplicitHeader(State, CBA, SHeader, Sec->Name,
328                            Sec->IsImplicit ? nullptr : Sec))
329       continue;
330 
331     assert(Sec && "It can't be null unless it is an implicit section. But all "
332                   "implicit sections should already have been handled above.");
333 
334     SHeader.sh_name = DotShStrtab.getOffset(dropUniqueSuffix(Sec->Name));
335     SHeader.sh_type = Sec->Type;
336     if (Sec->Flags)
337       SHeader.sh_flags = *Sec->Flags;
338     SHeader.sh_addr = Sec->Address;
339     SHeader.sh_addralign = Sec->AddressAlign;
340 
341     if (!Sec->Link.empty()) {
342       unsigned Index;
343       if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
344         return false;
345       SHeader.sh_link = Index;
346     }
347 
348     if (I == 0) {
349       if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
350         // We do not write any content for special SHN_UNDEF section.
351         if (RawSec->Size)
352           SHeader.sh_size = *RawSec->Size;
353         if (RawSec->Info)
354           SHeader.sh_info = *RawSec->Info;
355       }
356       if (Sec->EntSize)
357         SHeader.sh_entsize = *Sec->EntSize;
358     } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
359       if (!writeSectionContent(SHeader, *S, CBA))
360         return false;
361     } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
362       if (!writeSectionContent(SHeader, *S, CBA))
363         return false;
364     } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) {
365       if (!writeSectionContent(SHeader, *S, CBA))
366         return false;
367     } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
368       if (!writeSectionContent(SHeader, *S, CBA))
369         return false;
370     } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
371       SHeader.sh_entsize = 0;
372       SHeader.sh_size = S->Size;
373       // SHT_NOBITS section does not have content
374       // so just to setup the section offset.
375       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
376     } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
377       if (!writeSectionContent(SHeader, *S, CBA))
378         return false;
379     } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
380       if (!writeSectionContent(SHeader, *S, CBA))
381         return false;
382     } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
383       if (!writeSectionContent(SHeader, *S, CBA))
384         return false;
385     } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
386       if (!writeSectionContent(SHeader, *S, CBA))
387         return false;
388     } else
389       llvm_unreachable("Unknown section type");
390 
391     // Override the sh_offset/sh_size fields if requested.
392     if (Sec) {
393       if (Sec->ShOffset)
394         SHeader.sh_offset = *Sec->ShOffset;
395       if (Sec->ShSize)
396         SHeader.sh_size = *Sec->ShSize;
397     }
398   }
399 
400   return true;
401 }
402 
403 static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
404   for (size_t I = 0; I < Symbols.size(); ++I)
405     if (Symbols[I].Binding.value != ELF::STB_LOCAL)
406       return I;
407   return Symbols.size();
408 }
409 
410 static uint64_t writeRawSectionData(raw_ostream &OS,
411                                     const ELFYAML::RawContentSection &RawSec) {
412   size_t ContentSize = 0;
413   if (RawSec.Content) {
414     RawSec.Content->writeAsBinary(OS);
415     ContentSize = RawSec.Content->binary_size();
416   }
417 
418   if (!RawSec.Size)
419     return ContentSize;
420 
421   OS.write_zeros(*RawSec.Size - ContentSize);
422   return *RawSec.Size;
423 }
424 
425 template <class ELFT>
426 static std::vector<typename ELFT::Sym>
427 toELFSymbols(NameToIdxMap &SN2I, ArrayRef<ELFYAML::Symbol> Symbols,
428              const StringTableBuilder &Strtab) {
429   using Elf_Sym = typename ELFT::Sym;
430 
431   std::vector<Elf_Sym> Ret;
432   Ret.resize(Symbols.size() + 1);
433 
434   size_t I = 0;
435   for (const auto &Sym : Symbols) {
436     Elf_Sym &Symbol = Ret[++I];
437 
438     // If NameIndex, which contains the name offset, is explicitly specified, we
439     // use it. This is useful for preparing broken objects. Otherwise, we add
440     // the specified Name to the string table builder to get its offset.
441     if (Sym.NameIndex)
442       Symbol.st_name = *Sym.NameIndex;
443     else if (!Sym.Name.empty())
444       Symbol.st_name = Strtab.getOffset(dropUniqueSuffix(Sym.Name));
445 
446     Symbol.setBindingAndType(Sym.Binding, Sym.Type);
447     if (!Sym.Section.empty()) {
448       unsigned Index;
449       if (!SN2I.lookup(Sym.Section, Index)) {
450         WithColor::error() << "Unknown section referenced: '" << Sym.Section
451                            << "' by YAML symbol " << Sym.Name << ".\n";
452         exit(1);
453       }
454       Symbol.st_shndx = Index;
455     } else if (Sym.Index) {
456       Symbol.st_shndx = *Sym.Index;
457     }
458     // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
459     Symbol.st_value = Sym.Value;
460     Symbol.st_other = Sym.Other;
461     Symbol.st_size = Sym.Size;
462   }
463 
464   return Ret;
465 }
466 
467 template <class ELFT>
468 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
469                                              SymtabType STType,
470                                              ContiguousBlobAccumulator &CBA,
471                                              ELFYAML::Section *YAMLSec) {
472 
473   bool IsStatic = STType == SymtabType::Static;
474   const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
475 
476   ELFYAML::RawContentSection *RawSec =
477       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
478   if (RawSec && !Symbols.empty() && (RawSec->Content || RawSec->Size)) {
479     if (RawSec->Content)
480       WithColor::error() << "Cannot specify both `Content` and " +
481                                 (IsStatic ? Twine("`Symbols`")
482                                           : Twine("`DynamicSymbols`")) +
483                                 " for symbol table section '"
484                          << RawSec->Name << "'.\n";
485     if (RawSec->Size)
486       WithColor::error() << "Cannot specify both `Size` and " +
487                                 (IsStatic ? Twine("`Symbols`")
488                                           : Twine("`DynamicSymbols`")) +
489                                 " for symbol table section '"
490                          << RawSec->Name << "'.\n";
491     exit(1);
492   }
493 
494   zero(SHeader);
495   SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
496 
497   if (YAMLSec)
498     SHeader.sh_type = YAMLSec->Type;
499   else
500     SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
501 
502   if (RawSec && !RawSec->Link.empty()) {
503     // If the Link field is explicitly defined in the document,
504     // we should use it.
505     unsigned Index;
506     if (!convertSectionIndex(SN2I, RawSec->Name, RawSec->Link, Index))
507       return;
508     SHeader.sh_link = Index;
509   } else {
510     // When we describe the .dynsym section in the document explicitly, it is
511     // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
512     // added implicitly and we should be able to leave the Link zeroed if
513     // .dynstr is not defined.
514     unsigned Link = 0;
515     if (IsStatic)
516       Link = SN2I.get(".strtab");
517     else
518       SN2I.lookup(".dynstr", Link);
519     SHeader.sh_link = Link;
520   }
521 
522   if (YAMLSec && YAMLSec->Flags)
523     SHeader.sh_flags = *YAMLSec->Flags;
524   else if (!IsStatic)
525     SHeader.sh_flags = ELF::SHF_ALLOC;
526 
527   // If the symbol table section is explicitly described in the YAML
528   // then we should set the fields requested.
529   SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
530                                              : findFirstNonGlobal(Symbols) + 1;
531   SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
532                            ? (uint64_t)(*YAMLSec->EntSize)
533                            : sizeof(Elf_Sym);
534   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
535   SHeader.sh_addr = YAMLSec ? (uint64_t)YAMLSec->Address : 0;
536 
537   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
538   if (RawSec && (RawSec->Content || RawSec->Size)) {
539     assert(Symbols.empty());
540     SHeader.sh_size = writeRawSectionData(OS, *RawSec);
541     return;
542   }
543 
544   std::vector<Elf_Sym> Syms =
545       toELFSymbols<ELFT>(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr);
546   writeArrayData(OS, makeArrayRef(Syms));
547   SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
548 }
549 
550 template <class ELFT>
551 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
552                                              StringTableBuilder &STB,
553                                              ContiguousBlobAccumulator &CBA,
554                                              ELFYAML::Section *YAMLSec) {
555   zero(SHeader);
556   SHeader.sh_name = DotShStrtab.getOffset(Name);
557   SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
558   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
559 
560   ELFYAML::RawContentSection *RawSec =
561       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
562 
563   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
564   if (RawSec && (RawSec->Content || RawSec->Size)) {
565     SHeader.sh_size = writeRawSectionData(OS, *RawSec);
566   } else {
567     STB.write(OS);
568     SHeader.sh_size = STB.getSize();
569   }
570 
571   if (YAMLSec && YAMLSec->EntSize)
572     SHeader.sh_entsize = *YAMLSec->EntSize;
573 
574   if (RawSec && RawSec->Info)
575     SHeader.sh_info = *RawSec->Info;
576 
577   if (YAMLSec && YAMLSec->Flags)
578     SHeader.sh_flags = *YAMLSec->Flags;
579   else if (Name == ".dynstr")
580     SHeader.sh_flags = ELF::SHF_ALLOC;
581 
582   // If the section is explicitly described in the YAML
583   // then we want to use its section address.
584   if (YAMLSec)
585     SHeader.sh_addr = YAMLSec->Address;
586 }
587 
588 template <class ELFT>
589 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
590                                             std::vector<Elf_Shdr> &SHeaders) {
591   uint32_t PhdrIdx = 0;
592   for (auto &YamlPhdr : Doc.ProgramHeaders) {
593     Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
594 
595     std::vector<Elf_Shdr *> Sections;
596     for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
597       unsigned Index;
598       if (!SN2I.lookup(SecName.Section, Index)) {
599         WithColor::error() << "Unknown section referenced: '" << SecName.Section
600                            << "' by program header.\n";
601         exit(1);
602       }
603       Sections.push_back(&SHeaders[Index]);
604     }
605 
606     if (YamlPhdr.Offset) {
607       PHeader.p_offset = *YamlPhdr.Offset;
608     } else {
609       if (YamlPhdr.Sections.size())
610         PHeader.p_offset = UINT32_MAX;
611       else
612         PHeader.p_offset = 0;
613 
614       // Find the minimum offset for the program header.
615       for (Elf_Shdr *SHeader : Sections)
616         PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
617     }
618 
619     // Find the maximum offset of the end of a section in order to set p_filesz,
620     // if not set explicitly.
621     if (YamlPhdr.FileSize) {
622       PHeader.p_filesz = *YamlPhdr.FileSize;
623     } else {
624       PHeader.p_filesz = 0;
625       for (Elf_Shdr *SHeader : Sections) {
626         uint64_t EndOfSection;
627         if (SHeader->sh_type == llvm::ELF::SHT_NOBITS)
628           EndOfSection = SHeader->sh_offset;
629         else
630           EndOfSection = SHeader->sh_offset + SHeader->sh_size;
631         uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
632         EndOfSegment = std::max(EndOfSegment, EndOfSection);
633         PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
634       }
635     }
636 
637     // If not set explicitly, find the memory size by adding the size of
638     // sections at the end of the segment. These should be empty (size of zero)
639     // and NOBITS sections.
640     if (YamlPhdr.MemSize) {
641       PHeader.p_memsz = *YamlPhdr.MemSize;
642     } else {
643       PHeader.p_memsz = PHeader.p_filesz;
644       for (Elf_Shdr *SHeader : Sections)
645         if (SHeader->sh_offset == PHeader.p_offset + PHeader.p_filesz)
646           PHeader.p_memsz += SHeader->sh_size;
647     }
648 
649     // Set the alignment of the segment to be the same as the maximum alignment
650     // of the sections with the same offset so that by default the segment
651     // has a valid and sensible alignment.
652     if (YamlPhdr.Align) {
653       PHeader.p_align = *YamlPhdr.Align;
654     } else {
655       PHeader.p_align = 1;
656       for (Elf_Shdr *SHeader : Sections)
657         if (SHeader->sh_offset == PHeader.p_offset)
658           PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign);
659     }
660   }
661 }
662 
663 template <class ELFT>
664 bool ELFState<ELFT>::writeSectionContent(
665     Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
666     ContiguousBlobAccumulator &CBA) {
667   raw_ostream &OS =
668       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
669   SHeader.sh_size = writeRawSectionData(OS, Section);
670 
671   if (Section.EntSize)
672     SHeader.sh_entsize = *Section.EntSize;
673   else if (Section.Type == llvm::ELF::SHT_RELR)
674     SHeader.sh_entsize = sizeof(Elf_Relr);
675   else
676     SHeader.sh_entsize = 0;
677 
678   if (Section.Info)
679     SHeader.sh_info = *Section.Info;
680 
681   return true;
682 }
683 
684 static bool isMips64EL(const ELFYAML::Object &Doc) {
685   return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
686          Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
687          Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
688 }
689 
690 template <class ELFT>
691 bool ELFState<ELFT>::writeSectionContent(
692     Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
693     ContiguousBlobAccumulator &CBA) {
694   assert((Section.Type == llvm::ELF::SHT_REL ||
695           Section.Type == llvm::ELF::SHT_RELA) &&
696          "Section type is not SHT_REL nor SHT_RELA");
697 
698   bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
699   SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
700   SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
701 
702   // For relocation section set link to .symtab by default.
703   if (Section.Link.empty())
704     SHeader.sh_link = SN2I.get(".symtab");
705 
706   unsigned Index = 0;
707   if (!Section.RelocatableSec.empty() &&
708       !convertSectionIndex(SN2I, Section.Name, Section.RelocatableSec, Index))
709     return false;
710   SHeader.sh_info = Index;
711 
712   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
713 
714   for (const auto &Rel : Section.Relocations) {
715     unsigned SymIdx = 0;
716     // If a relocation references a symbol, try to look one up in the symbol
717     // table. If it is not there, treat the value as a symbol index.
718     if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) &&
719         !to_integer(*Rel.Symbol, SymIdx)) {
720       WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
721                          << "' at YAML section '" << Section.Name << "'.\n";
722       return false;
723     }
724 
725     if (IsRela) {
726       Elf_Rela REntry;
727       zero(REntry);
728       REntry.r_offset = Rel.Offset;
729       REntry.r_addend = Rel.Addend;
730       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
731       OS.write((const char *)&REntry, sizeof(REntry));
732     } else {
733       Elf_Rel REntry;
734       zero(REntry);
735       REntry.r_offset = Rel.Offset;
736       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
737       OS.write((const char *)&REntry, sizeof(REntry));
738     }
739   }
740   return true;
741 }
742 
743 template <class ELFT>
744 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
745                                          const ELFYAML::Group &Section,
746                                          ContiguousBlobAccumulator &CBA) {
747   assert(Section.Type == llvm::ELF::SHT_GROUP &&
748          "Section type is not SHT_GROUP");
749 
750   SHeader.sh_entsize = 4;
751   SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
752 
753   unsigned SymIdx;
754   if (!SymN2I.lookup(Section.Signature, SymIdx) &&
755       !to_integer(Section.Signature, SymIdx)) {
756     WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
757                        << "' at YAML section '" << Section.Name << "'.\n";
758     return false;
759   }
760   SHeader.sh_info = SymIdx;
761 
762   raw_ostream &OS =
763       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
764 
765   for (const ELFYAML::SectionOrType &Member : Section.Members) {
766     unsigned int SectionIndex = 0;
767     if (Member.sectionNameOrType == "GRP_COMDAT")
768       SectionIndex = llvm::ELF::GRP_COMDAT;
769     else if (!convertSectionIndex(SN2I, Section.Name, Member.sectionNameOrType,
770                                   SectionIndex))
771       return false;
772     support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
773   }
774   return true;
775 }
776 
777 template <class ELFT>
778 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
779                                          const ELFYAML::SymverSection &Section,
780                                          ContiguousBlobAccumulator &CBA) {
781   raw_ostream &OS =
782       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
783   for (uint16_t Version : Section.Entries)
784     support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
785 
786   SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
787   SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
788   return true;
789 }
790 
791 template <class ELFT>
792 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
793                                          const ELFYAML::VerdefSection &Section,
794                                          ContiguousBlobAccumulator &CBA) {
795   typedef typename ELFT::Verdef Elf_Verdef;
796   typedef typename ELFT::Verdaux Elf_Verdaux;
797   raw_ostream &OS =
798       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
799 
800   uint64_t AuxCnt = 0;
801   for (size_t I = 0; I < Section.Entries.size(); ++I) {
802     const ELFYAML::VerdefEntry &E = Section.Entries[I];
803 
804     Elf_Verdef VerDef;
805     VerDef.vd_version = E.Version;
806     VerDef.vd_flags = E.Flags;
807     VerDef.vd_ndx = E.VersionNdx;
808     VerDef.vd_hash = E.Hash;
809     VerDef.vd_aux = sizeof(Elf_Verdef);
810     VerDef.vd_cnt = E.VerNames.size();
811     if (I == Section.Entries.size() - 1)
812       VerDef.vd_next = 0;
813     else
814       VerDef.vd_next =
815           sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
816     OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
817 
818     for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
819       Elf_Verdaux VernAux;
820       VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
821       if (J == E.VerNames.size() - 1)
822         VernAux.vda_next = 0;
823       else
824         VernAux.vda_next = sizeof(Elf_Verdaux);
825       OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
826     }
827   }
828 
829   SHeader.sh_size = Section.Entries.size() * sizeof(Elf_Verdef) +
830                     AuxCnt * sizeof(Elf_Verdaux);
831   SHeader.sh_info = Section.Info;
832 
833   return true;
834 }
835 
836 template <class ELFT>
837 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
838                                          const ELFYAML::VerneedSection &Section,
839                                          ContiguousBlobAccumulator &CBA) {
840   typedef typename ELFT::Verneed Elf_Verneed;
841   typedef typename ELFT::Vernaux Elf_Vernaux;
842 
843   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
844 
845   uint64_t AuxCnt = 0;
846   for (size_t I = 0; I < Section.VerneedV.size(); ++I) {
847     const ELFYAML::VerneedEntry &VE = Section.VerneedV[I];
848 
849     Elf_Verneed VerNeed;
850     VerNeed.vn_version = VE.Version;
851     VerNeed.vn_file = DotDynstr.getOffset(VE.File);
852     if (I == Section.VerneedV.size() - 1)
853       VerNeed.vn_next = 0;
854     else
855       VerNeed.vn_next =
856           sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
857     VerNeed.vn_cnt = VE.AuxV.size();
858     VerNeed.vn_aux = sizeof(Elf_Verneed);
859     OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
860 
861     for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
862       const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
863 
864       Elf_Vernaux VernAux;
865       VernAux.vna_hash = VAuxE.Hash;
866       VernAux.vna_flags = VAuxE.Flags;
867       VernAux.vna_other = VAuxE.Other;
868       VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
869       if (J == VE.AuxV.size() - 1)
870         VernAux.vna_next = 0;
871       else
872         VernAux.vna_next = sizeof(Elf_Vernaux);
873       OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
874     }
875   }
876 
877   SHeader.sh_size = Section.VerneedV.size() * sizeof(Elf_Verneed) +
878                     AuxCnt * sizeof(Elf_Vernaux);
879   SHeader.sh_info = Section.Info;
880 
881   return true;
882 }
883 
884 template <class ELFT>
885 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
886                                          const ELFYAML::MipsABIFlags &Section,
887                                          ContiguousBlobAccumulator &CBA) {
888   assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
889          "Section type is not SHT_MIPS_ABIFLAGS");
890 
891   object::Elf_Mips_ABIFlags<ELFT> Flags;
892   zero(Flags);
893   SHeader.sh_entsize = sizeof(Flags);
894   SHeader.sh_size = SHeader.sh_entsize;
895 
896   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
897   Flags.version = Section.Version;
898   Flags.isa_level = Section.ISALevel;
899   Flags.isa_rev = Section.ISARevision;
900   Flags.gpr_size = Section.GPRSize;
901   Flags.cpr1_size = Section.CPR1Size;
902   Flags.cpr2_size = Section.CPR2Size;
903   Flags.fp_abi = Section.FpABI;
904   Flags.isa_ext = Section.ISAExtension;
905   Flags.ases = Section.ASEs;
906   Flags.flags1 = Section.Flags1;
907   Flags.flags2 = Section.Flags2;
908   OS.write((const char *)&Flags, sizeof(Flags));
909 
910   return true;
911 }
912 
913 template <class ELFT>
914 bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
915                                          const ELFYAML::DynamicSection &Section,
916                                          ContiguousBlobAccumulator &CBA) {
917   typedef typename ELFT::uint uintX_t;
918 
919   assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
920          "Section type is not SHT_DYNAMIC");
921 
922   if (!Section.Entries.empty() && Section.Content) {
923     WithColor::error()
924         << "Cannot specify both raw content and explicit entries "
925            "for dynamic section '"
926         << Section.Name << "'.\n";
927     return false;
928   }
929 
930   if (Section.Content)
931     SHeader.sh_size = Section.Content->binary_size();
932   else
933     SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
934   if (Section.EntSize)
935     SHeader.sh_entsize = *Section.EntSize;
936   else
937     SHeader.sh_entsize = sizeof(Elf_Dyn);
938 
939   raw_ostream &OS =
940       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
941   for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
942     support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
943     support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
944   }
945   if (Section.Content)
946     Section.Content->writeAsBinary(OS);
947 
948   return true;
949 }
950 
951 template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
952   for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) {
953     StringRef Name = Doc.Sections[I]->Name;
954     if (Name.empty())
955       continue;
956 
957     DotShStrtab.add(dropUniqueSuffix(Name));
958     if (!SN2I.addName(Name, I)) {
959       WithColor::error() << "Repeated section name: '" << Name
960                          << "' at YAML section number " << I << ".\n";
961       return false;
962     }
963   }
964 
965   DotShStrtab.finalize();
966   return true;
967 }
968 
969 template <class ELFT>
970 bool ELFState<ELFT>::buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols) {
971   bool GlobalSymbolSeen = false;
972   std::size_t I = 0;
973   for (const auto &Sym : Symbols) {
974     ++I;
975 
976     StringRef Name = Sym.Name;
977     if (Sym.Binding.value == ELF::STB_LOCAL && GlobalSymbolSeen) {
978       WithColor::error() << "Local symbol '" + Name +
979                                 "' after global in Symbols list.\n";
980       return false;
981     }
982     if (Sym.Binding.value != ELF::STB_LOCAL)
983       GlobalSymbolSeen = true;
984 
985     if (!Name.empty() && !SymN2I.addName(Name, I)) {
986       WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
987       return false;
988     }
989   }
990   return true;
991 }
992 
993 template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
994   // Add the regular symbol names to .strtab section.
995   for (const ELFYAML::Symbol &Sym : Doc.Symbols)
996     DotStrtab.add(dropUniqueSuffix(Sym.Name));
997   DotStrtab.finalize();
998 
999   // Add the dynamic symbol names to .dynstr section.
1000   for (const ELFYAML::Symbol &Sym : Doc.DynamicSymbols)
1001     DotDynstr.add(dropUniqueSuffix(Sym.Name));
1002 
1003   // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1004   // add strings to .dynstr section.
1005   for (const std::unique_ptr<ELFYAML::Section> &Sec : Doc.Sections) {
1006     if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec.get())) {
1007       for (const ELFYAML::VerneedEntry &VE : VerNeed->VerneedV) {
1008         DotDynstr.add(VE.File);
1009         for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1010           DotDynstr.add(Aux.Name);
1011       }
1012     } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec.get())) {
1013       for (const ELFYAML::VerdefEntry &E : VerDef->Entries)
1014         for (StringRef Name : E.VerNames)
1015           DotDynstr.add(Name);
1016     }
1017   }
1018 
1019   DotDynstr.finalize();
1020 }
1021 
1022 template <class ELFT>
1023 int ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc) {
1024   ELFState<ELFT> State(Doc);
1025 
1026   // Finalize .strtab and .dynstr sections. We do that early because want to
1027   // finalize the string table builders before writing the content of the
1028   // sections that might want to use them.
1029   State.finalizeStrings();
1030 
1031   if (!State.buildSectionIndex())
1032     return 1;
1033 
1034   if (!State.buildSymbolIndex(Doc.Symbols))
1035     return 1;
1036 
1037   Elf_Ehdr Header;
1038   State.initELFHeader(Header);
1039 
1040   // TODO: Flesh out section header support.
1041 
1042   std::vector<Elf_Phdr> PHeaders;
1043   State.initProgramHeaders(PHeaders);
1044 
1045   // XXX: This offset is tightly coupled with the order that we write
1046   // things to `OS`.
1047   const size_t SectionContentBeginOffset = Header.e_ehsize +
1048                                            Header.e_phentsize * Header.e_phnum +
1049                                            Header.e_shentsize * Header.e_shnum;
1050   ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
1051 
1052   std::vector<Elf_Shdr> SHeaders;
1053   if (!State.initSectionHeaders(State, SHeaders, CBA))
1054     return 1;
1055 
1056   // Now we can decide segment offsets
1057   State.setProgramHeaderLayout(PHeaders, SHeaders);
1058 
1059   OS.write((const char *)&Header, sizeof(Header));
1060   writeArrayData(OS, makeArrayRef(PHeaders));
1061   writeArrayData(OS, makeArrayRef(SHeaders));
1062   CBA.writeBlobToStream(OS);
1063   return 0;
1064 }
1065 
1066 namespace llvm {
1067 namespace yaml {
1068 
1069 int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
1070   bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1071   bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1072   if (Is64Bit) {
1073     if (IsLE)
1074       return ELFState<object::ELF64LE>::writeELF(Out, Doc);
1075     return ELFState<object::ELF64BE>::writeELF(Out, Doc);
1076   }
1077   if (IsLE)
1078     return ELFState<object::ELF32LE>::writeELF(Out, Doc);
1079   return ELFState<object::ELF32BE>::writeELF(Out, Doc);
1080 }
1081 
1082 } // namespace yaml
1083 } // namespace llvm
1084