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