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/DenseMap.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include "llvm/Object/ELFObjectFile.h"
20 #include "llvm/ObjectYAML/ELFYAML.h"
21 #include "llvm/ObjectYAML/yaml2obj.h"
22 #include "llvm/Support/EndianStream.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/WithColor.h"
26 #include "llvm/Support/YAMLTraits.h"
27 #include "llvm/Support/raw_ostream.h"
28 
29 using namespace llvm;
30 
31 // This class is used to build up a contiguous binary blob while keeping
32 // track of an offset in the output (which notionally begins at
33 // `InitialOffset`).
34 namespace {
35 class ContiguousBlobAccumulator {
36   const uint64_t InitialOffset;
37   SmallVector<char, 128> Buf;
38   raw_svector_ostream OS;
39 
40 public:
41   ContiguousBlobAccumulator(uint64_t InitialOffset_)
42       : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
43 
44   uint64_t getOffset() const { return InitialOffset + OS.tell(); }
45   raw_ostream &getOS() { return OS; }
46 
47   /// \returns The new offset.
48   uint64_t padToAlignment(unsigned Align) {
49     if (Align == 0)
50       Align = 1;
51     uint64_t CurrentOffset = getOffset();
52     uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
53     OS.write_zeros(AlignedOffset - CurrentOffset);
54     return AlignedOffset; // == CurrentOffset;
55   }
56 
57   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
58 };
59 
60 // Used to keep track of section and symbol names, so that in the YAML file
61 // sections and symbols can be referenced by name instead of by index.
62 class NameToIdxMap {
63   StringMap<unsigned> Map;
64 
65 public:
66   /// \Returns false if name is already present in the map.
67   bool addName(StringRef Name, unsigned Ndx) {
68     return Map.insert({Name, Ndx}).second;
69   }
70   /// \Returns false if name is not present in the map.
71   bool lookup(StringRef Name, unsigned &Idx) const {
72     auto I = Map.find(Name);
73     if (I == Map.end())
74       return false;
75     Idx = I->getValue();
76     return true;
77   }
78   /// Asserts if name is not present in the map.
79   unsigned get(StringRef Name) const {
80     unsigned Idx;
81     if (lookup(Name, Idx))
82       return Idx;
83     assert(false && "Expected section not found in index");
84     return 0;
85   }
86   unsigned size() const { return Map.size(); }
87 };
88 
89 namespace {
90 struct Fragment {
91   uint64_t Offset;
92   uint64_t Size;
93   uint32_t Type;
94   uint64_t AddrAlign;
95 };
96 } // namespace
97 
98 /// "Single point of truth" for the ELF file construction.
99 /// TODO: This class still has a ways to go before it is truly a "single
100 /// point of truth".
101 template <class ELFT> class ELFState {
102   typedef typename ELFT::Ehdr Elf_Ehdr;
103   typedef typename ELFT::Phdr Elf_Phdr;
104   typedef typename ELFT::Shdr Elf_Shdr;
105   typedef typename ELFT::Sym Elf_Sym;
106   typedef typename ELFT::Rel Elf_Rel;
107   typedef typename ELFT::Rela Elf_Rela;
108   typedef typename ELFT::Relr Elf_Relr;
109   typedef typename ELFT::Dyn Elf_Dyn;
110   typedef typename ELFT::uint uintX_t;
111 
112   enum class SymtabType { Static, Dynamic };
113 
114   /// The future ".strtab" section.
115   StringTableBuilder DotStrtab{StringTableBuilder::ELF};
116 
117   /// The future ".shstrtab" section.
118   StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
119 
120   /// The future ".dynstr" section.
121   StringTableBuilder DotDynstr{StringTableBuilder::ELF};
122 
123   NameToIdxMap SN2I;
124   NameToIdxMap SymN2I;
125   NameToIdxMap DynSymN2I;
126   ELFYAML::Object &Doc;
127 
128   uint64_t LocationCounter = 0;
129   bool HasError = false;
130   yaml::ErrorHandler ErrHandler;
131   void reportError(const Twine &Msg);
132 
133   std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
134                                     const StringTableBuilder &Strtab);
135   unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = "");
136   unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic);
137 
138   void buildSectionIndex();
139   void buildSymbolIndexes();
140   void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
141   bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
142                           StringRef SecName, ELFYAML::Section *YAMLSec);
143   void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
144                           ContiguousBlobAccumulator &CBA);
145   void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
146                                ContiguousBlobAccumulator &CBA,
147                                ELFYAML::Section *YAMLSec);
148   void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
149                                StringTableBuilder &STB,
150                                ContiguousBlobAccumulator &CBA,
151                                ELFYAML::Section *YAMLSec);
152   void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
153                               std::vector<Elf_Shdr> &SHeaders);
154 
155   std::vector<Fragment>
156   getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
157                    ArrayRef<typename ELFT::Shdr> SHeaders);
158 
159   void finalizeStrings();
160   void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS);
161   void writeSectionContent(Elf_Shdr &SHeader,
162                            const ELFYAML::RawContentSection &Section,
163                            ContiguousBlobAccumulator &CBA);
164   void writeSectionContent(Elf_Shdr &SHeader,
165                            const ELFYAML::RelocationSection &Section,
166                            ContiguousBlobAccumulator &CBA);
167   void writeSectionContent(Elf_Shdr &SHeader,
168                            const ELFYAML::RelrSection &Section,
169                            ContiguousBlobAccumulator &CBA);
170   void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
171                            ContiguousBlobAccumulator &CBA);
172   void writeSectionContent(Elf_Shdr &SHeader,
173                            const ELFYAML::SymtabShndxSection &Shndx,
174                            ContiguousBlobAccumulator &CBA);
175   void writeSectionContent(Elf_Shdr &SHeader,
176                            const ELFYAML::SymverSection &Section,
177                            ContiguousBlobAccumulator &CBA);
178   void writeSectionContent(Elf_Shdr &SHeader,
179                            const ELFYAML::VerneedSection &Section,
180                            ContiguousBlobAccumulator &CBA);
181   void writeSectionContent(Elf_Shdr &SHeader,
182                            const ELFYAML::VerdefSection &Section,
183                            ContiguousBlobAccumulator &CBA);
184   void writeSectionContent(Elf_Shdr &SHeader,
185                            const ELFYAML::MipsABIFlags &Section,
186                            ContiguousBlobAccumulator &CBA);
187   void writeSectionContent(Elf_Shdr &SHeader,
188                            const ELFYAML::DynamicSection &Section,
189                            ContiguousBlobAccumulator &CBA);
190   void writeSectionContent(Elf_Shdr &SHeader,
191                            const ELFYAML::StackSizesSection &Section,
192                            ContiguousBlobAccumulator &CBA);
193   void writeSectionContent(Elf_Shdr &SHeader,
194                            const ELFYAML::HashSection &Section,
195                            ContiguousBlobAccumulator &CBA);
196   void writeSectionContent(Elf_Shdr &SHeader,
197                            const ELFYAML::AddrsigSection &Section,
198                            ContiguousBlobAccumulator &CBA);
199   void writeSectionContent(Elf_Shdr &SHeader,
200                            const ELFYAML::NoteSection &Section,
201                            ContiguousBlobAccumulator &CBA);
202   void writeSectionContent(Elf_Shdr &SHeader,
203                            const ELFYAML::GnuHashSection &Section,
204                            ContiguousBlobAccumulator &CBA);
205   void writeSectionContent(Elf_Shdr &SHeader,
206                            const ELFYAML::LinkerOptionsSection &Section,
207                            ContiguousBlobAccumulator &CBA);
208   void writeSectionContent(Elf_Shdr &SHeader,
209                            const ELFYAML::DependentLibrariesSection &Section,
210                            ContiguousBlobAccumulator &CBA);
211   void writeSectionContent(Elf_Shdr &SHeader,
212                            const ELFYAML::CallGraphProfileSection &Section,
213                            ContiguousBlobAccumulator &CBA);
214 
215   void writeFill(ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
216 
217   ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
218 
219   void assignSectionAddress(Elf_Shdr &SHeader, ELFYAML::Section *YAMLSec);
220 
221   uint64_t alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
222                          llvm::Optional<llvm::yaml::Hex64> Offset);
223 
224 public:
225   static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
226                        yaml::ErrorHandler EH);
227 };
228 } // end anonymous namespace
229 
230 template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
231   return A.size() * sizeof(T);
232 }
233 
234 template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
235   OS.write((const char *)A.data(), arrayDataSize(A));
236 }
237 
238 template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
239 
240 template <class ELFT>
241 ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
242     : Doc(D), ErrHandler(EH) {
243   std::vector<ELFYAML::Section *> Sections = Doc.getSections();
244   StringSet<> DocSections;
245   for (const ELFYAML::Section *Sec : Sections)
246     if (!Sec->Name.empty())
247       DocSections.insert(Sec->Name);
248 
249   // Insert SHT_NULL section implicitly when it is not defined in YAML.
250   if (Sections.empty() || Sections.front()->Type != ELF::SHT_NULL)
251     Doc.Chunks.insert(
252         Doc.Chunks.begin(),
253         std::make_unique<ELFYAML::Section>(
254             ELFYAML::Chunk::ChunkKind::RawContent, /*IsImplicit=*/true));
255 
256   std::vector<StringRef> ImplicitSections;
257   if (Doc.DynamicSymbols)
258     ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
259   if (Doc.Symbols)
260     ImplicitSections.push_back(".symtab");
261   ImplicitSections.insert(ImplicitSections.end(), {".strtab", ".shstrtab"});
262 
263   // Insert placeholders for implicit sections that are not
264   // defined explicitly in YAML.
265   for (StringRef SecName : ImplicitSections) {
266     if (DocSections.count(SecName))
267       continue;
268 
269     std::unique_ptr<ELFYAML::Chunk> Sec = std::make_unique<ELFYAML::Section>(
270         ELFYAML::Chunk::ChunkKind::RawContent, true /*IsImplicit*/);
271     Sec->Name = SecName;
272     Doc.Chunks.push_back(std::move(Sec));
273   }
274 }
275 
276 template <class ELFT>
277 void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS) {
278   using namespace llvm::ELF;
279 
280   Elf_Ehdr Header;
281   zero(Header);
282   Header.e_ident[EI_MAG0] = 0x7f;
283   Header.e_ident[EI_MAG1] = 'E';
284   Header.e_ident[EI_MAG2] = 'L';
285   Header.e_ident[EI_MAG3] = 'F';
286   Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
287   Header.e_ident[EI_DATA] = Doc.Header.Data;
288   Header.e_ident[EI_VERSION] = EV_CURRENT;
289   Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
290   Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
291   Header.e_type = Doc.Header.Type;
292   Header.e_machine = Doc.Header.Machine;
293   Header.e_version = EV_CURRENT;
294   Header.e_entry = Doc.Header.Entry;
295   Header.e_phoff = Doc.ProgramHeaders.size() ? sizeof(Header) : 0;
296   Header.e_flags = Doc.Header.Flags;
297   Header.e_ehsize = sizeof(Elf_Ehdr);
298   Header.e_phentsize = Doc.ProgramHeaders.size() ? sizeof(Elf_Phdr) : 0;
299   Header.e_phnum = Doc.ProgramHeaders.size();
300 
301   Header.e_shentsize =
302       Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr);
303   // Align the start of the section header table, which is written after all
304   // other sections to the end of the file.
305   uint64_t SHOff =
306       alignToOffset(CBA, sizeof(typename ELFT::uint), /*Offset=*/None);
307   Header.e_shoff =
308       Doc.Header.SHOff ? typename ELFT::uint(*Doc.Header.SHOff) : SHOff;
309   Header.e_shnum =
310       Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.getSections().size();
311   Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
312                                           : SN2I.get(".shstrtab");
313 
314   OS.write((const char *)&Header, sizeof(Header));
315 }
316 
317 template <class ELFT>
318 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
319   for (const auto &YamlPhdr : Doc.ProgramHeaders) {
320     Elf_Phdr Phdr;
321     zero(Phdr);
322     Phdr.p_type = YamlPhdr.Type;
323     Phdr.p_flags = YamlPhdr.Flags;
324     Phdr.p_vaddr = YamlPhdr.VAddr;
325     Phdr.p_paddr = YamlPhdr.PAddr;
326     PHeaders.push_back(Phdr);
327   }
328 }
329 
330 template <class ELFT>
331 unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
332                                         StringRef LocSym) {
333   unsigned Index;
334   if (SN2I.lookup(S, Index) || to_integer(S, Index))
335     return Index;
336 
337   assert(LocSec.empty() || LocSym.empty());
338   if (!LocSym.empty())
339     reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
340                 LocSym + "'");
341   else
342     reportError("unknown section referenced: '" + S + "' by YAML section '" +
343                 LocSec + "'");
344   return 0;
345 }
346 
347 template <class ELFT>
348 unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec,
349                                        bool IsDynamic) {
350   const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
351   unsigned Index;
352   // Here we try to look up S in the symbol table. If it is not there,
353   // treat its value as a symbol index.
354   if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) {
355     reportError("unknown symbol referenced: '" + S + "' by YAML section '" +
356                 LocSec + "'");
357     return 0;
358   }
359   return Index;
360 }
361 
362 template <class ELFT>
363 static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) {
364   if (!From)
365     return;
366   if (From->ShFlags)
367     To.sh_flags = *From->ShFlags;
368   if (From->ShName)
369     To.sh_name = *From->ShName;
370   if (From->ShOffset)
371     To.sh_offset = *From->ShOffset;
372   if (From->ShSize)
373     To.sh_size = *From->ShSize;
374 }
375 
376 template <class ELFT>
377 bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
378                                         Elf_Shdr &Header, StringRef SecName,
379                                         ELFYAML::Section *YAMLSec) {
380   // Check if the header was already initialized.
381   if (Header.sh_offset)
382     return false;
383 
384   if (SecName == ".symtab")
385     initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
386   else if (SecName == ".strtab")
387     initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
388   else if (SecName == ".shstrtab")
389     initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec);
390   else if (SecName == ".dynsym")
391     initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
392   else if (SecName == ".dynstr")
393     initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
394   else
395     return false;
396 
397   LocationCounter += Header.sh_size;
398 
399   // Override section fields if requested.
400   overrideFields<ELFT>(YAMLSec, Header);
401   return true;
402 }
403 
404 StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
405   size_t SuffixPos = S.rfind(" [");
406   if (SuffixPos == StringRef::npos)
407     return S;
408   return S.substr(0, SuffixPos);
409 }
410 
411 template <class ELFT>
412 void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
413                                         ContiguousBlobAccumulator &CBA) {
414   // Ensure SHN_UNDEF entry is present. An all-zero section header is a
415   // valid SHN_UNDEF entry since SHT_NULL == 0.
416   SHeaders.resize(Doc.getSections().size());
417 
418   size_t SecNdx = -1;
419   for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) {
420     if (auto S = dyn_cast<ELFYAML::Fill>(D.get())) {
421       S->ShOffset = alignToOffset(CBA, /*Align=*/1, /*Offset=*/None);
422       writeFill(*S, CBA);
423       LocationCounter += S->Size;
424       continue;
425     }
426 
427     ++SecNdx;
428     ELFYAML::Section *Sec = cast<ELFYAML::Section>(D.get());
429     if (SecNdx == 0 && Sec->IsImplicit)
430       continue;
431 
432     // We have a few sections like string or symbol tables that are usually
433     // added implicitly to the end. However, if they are explicitly specified
434     // in the YAML, we need to write them here. This ensures the file offset
435     // remains correct.
436     Elf_Shdr &SHeader = SHeaders[SecNdx];
437     if (initImplicitHeader(CBA, SHeader, Sec->Name,
438                            Sec->IsImplicit ? nullptr : Sec))
439       continue;
440 
441     assert(Sec && "It can't be null unless it is an implicit section. But all "
442                   "implicit sections should already have been handled above.");
443 
444     SHeader.sh_name =
445         DotShStrtab.getOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
446     SHeader.sh_type = Sec->Type;
447     if (Sec->Flags)
448       SHeader.sh_flags = *Sec->Flags;
449     SHeader.sh_addralign = Sec->AddressAlign;
450 
451     // Set the offset for all sections, except the SHN_UNDEF section with index
452     // 0 when not explicitly requested.
453     bool IsFirstUndefSection = SecNdx == 0;
454     if (!IsFirstUndefSection || Sec->Offset)
455       SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->Offset);
456 
457     assignSectionAddress(SHeader, Sec);
458 
459     if (!Sec->Link.empty())
460       SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name);
461 
462     if (IsFirstUndefSection) {
463       if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
464         // We do not write any content for special SHN_UNDEF section.
465         if (RawSec->Size)
466           SHeader.sh_size = *RawSec->Size;
467         if (RawSec->Info)
468           SHeader.sh_info = *RawSec->Info;
469       }
470       if (Sec->EntSize)
471         SHeader.sh_entsize = *Sec->EntSize;
472     } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
473       writeSectionContent(SHeader, *S, CBA);
474     } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
475       writeSectionContent(SHeader, *S, CBA);
476     } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
477       writeSectionContent(SHeader, *S, CBA);
478     } else if (auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
479       writeSectionContent(SHeader, *S, CBA);
480     } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) {
481       writeSectionContent(SHeader, *S, CBA);
482     } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
483       writeSectionContent(SHeader, *S, CBA);
484     } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
485       // SHT_NOBITS sections do not have any content to write.
486       SHeader.sh_entsize = 0;
487       SHeader.sh_size = S->Size;
488     } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
489       writeSectionContent(SHeader, *S, CBA);
490     } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
491       writeSectionContent(SHeader, *S, CBA);
492     } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
493       writeSectionContent(SHeader, *S, CBA);
494     } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
495       writeSectionContent(SHeader, *S, CBA);
496     } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
497       writeSectionContent(SHeader, *S, CBA);
498     } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
499       writeSectionContent(SHeader, *S, CBA);
500     } else if (auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
501       writeSectionContent(SHeader, *S, CBA);
502     } else if (auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
503       writeSectionContent(SHeader, *S, CBA);
504     } else if (auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
505       writeSectionContent(SHeader, *S, CBA);
506     } else if (auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
507       writeSectionContent(SHeader, *S, CBA);
508     } else if (auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
509       writeSectionContent(SHeader, *S, CBA);
510     } else if (auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
511       writeSectionContent(SHeader, *S, CBA);
512     } else {
513       llvm_unreachable("Unknown section type");
514     }
515 
516     LocationCounter += SHeader.sh_size;
517 
518     // Override section fields if requested.
519     overrideFields<ELFT>(Sec, SHeader);
520   }
521 }
522 
523 template <class ELFT>
524 void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
525                                           ELFYAML::Section *YAMLSec) {
526   if (YAMLSec && YAMLSec->Address) {
527     SHeader.sh_addr = *YAMLSec->Address;
528     LocationCounter = *YAMLSec->Address;
529     return;
530   }
531 
532   // sh_addr represents the address in the memory image of a process. Sections
533   // in a relocatable object file or non-allocatable sections do not need
534   // sh_addr assignment.
535   if (Doc.Header.Type.value == ELF::ET_REL ||
536       !(SHeader.sh_flags & ELF::SHF_ALLOC))
537     return;
538 
539   LocationCounter =
540       alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
541   SHeader.sh_addr = LocationCounter;
542 }
543 
544 static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
545   for (size_t I = 0; I < Symbols.size(); ++I)
546     if (Symbols[I].Binding.value != ELF::STB_LOCAL)
547       return I;
548   return Symbols.size();
549 }
550 
551 static uint64_t writeContent(raw_ostream &OS,
552                              const Optional<yaml::BinaryRef> &Content,
553                              const Optional<llvm::yaml::Hex64> &Size) {
554   size_t ContentSize = 0;
555   if (Content) {
556     Content->writeAsBinary(OS);
557     ContentSize = Content->binary_size();
558   }
559 
560   if (!Size)
561     return ContentSize;
562 
563   OS.write_zeros(*Size - ContentSize);
564   return *Size;
565 }
566 
567 template <class ELFT>
568 std::vector<typename ELFT::Sym>
569 ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
570                              const StringTableBuilder &Strtab) {
571   std::vector<Elf_Sym> Ret;
572   Ret.resize(Symbols.size() + 1);
573 
574   size_t I = 0;
575   for (const ELFYAML::Symbol &Sym : Symbols) {
576     Elf_Sym &Symbol = Ret[++I];
577 
578     // If NameIndex, which contains the name offset, is explicitly specified, we
579     // use it. This is useful for preparing broken objects. Otherwise, we add
580     // the specified Name to the string table builder to get its offset.
581     if (Sym.StName)
582       Symbol.st_name = *Sym.StName;
583     else if (!Sym.Name.empty())
584       Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
585 
586     Symbol.setBindingAndType(Sym.Binding, Sym.Type);
587     if (!Sym.Section.empty())
588       Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name);
589     else if (Sym.Index)
590       Symbol.st_shndx = *Sym.Index;
591 
592     Symbol.st_value = Sym.Value;
593     Symbol.st_other = Sym.Other ? *Sym.Other : 0;
594     Symbol.st_size = Sym.Size;
595   }
596 
597   return Ret;
598 }
599 
600 template <class ELFT>
601 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
602                                              SymtabType STType,
603                                              ContiguousBlobAccumulator &CBA,
604                                              ELFYAML::Section *YAMLSec) {
605 
606   bool IsStatic = STType == SymtabType::Static;
607   ArrayRef<ELFYAML::Symbol> Symbols;
608   if (IsStatic && Doc.Symbols)
609     Symbols = *Doc.Symbols;
610   else if (!IsStatic && Doc.DynamicSymbols)
611     Symbols = *Doc.DynamicSymbols;
612 
613   ELFYAML::RawContentSection *RawSec =
614       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
615   if (RawSec && (RawSec->Content || RawSec->Size)) {
616     bool HasSymbolsDescription =
617         (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
618     if (HasSymbolsDescription) {
619       StringRef Property = (IsStatic ? "`Symbols`" : "`DynamicSymbols`");
620       if (RawSec->Content)
621         reportError("cannot specify both `Content` and " + Property +
622                     " for symbol table section '" + RawSec->Name + "'");
623       if (RawSec->Size)
624         reportError("cannot specify both `Size` and " + Property +
625                     " for symbol table section '" + RawSec->Name + "'");
626       return;
627     }
628   }
629 
630   zero(SHeader);
631   SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
632 
633   if (YAMLSec)
634     SHeader.sh_type = YAMLSec->Type;
635   else
636     SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
637 
638   if (RawSec && !RawSec->Link.empty()) {
639     // If the Link field is explicitly defined in the document,
640     // we should use it.
641     SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name);
642   } else {
643     // When we describe the .dynsym section in the document explicitly, it is
644     // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
645     // added implicitly and we should be able to leave the Link zeroed if
646     // .dynstr is not defined.
647     unsigned Link = 0;
648     if (IsStatic)
649       Link = SN2I.get(".strtab");
650     else
651       SN2I.lookup(".dynstr", Link);
652     SHeader.sh_link = Link;
653   }
654 
655   if (YAMLSec && YAMLSec->Flags)
656     SHeader.sh_flags = *YAMLSec->Flags;
657   else if (!IsStatic)
658     SHeader.sh_flags = ELF::SHF_ALLOC;
659 
660   // If the symbol table section is explicitly described in the YAML
661   // then we should set the fields requested.
662   SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
663                                              : findFirstNonGlobal(Symbols) + 1;
664   SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize)
665                            ? (uint64_t)(*YAMLSec->EntSize)
666                            : sizeof(Elf_Sym);
667   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
668 
669   assignSectionAddress(SHeader, YAMLSec);
670 
671   SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, /*Offset=*/None);
672   raw_ostream &OS = CBA.getOS();
673 
674   if (RawSec && (RawSec->Content || RawSec->Size)) {
675     assert(Symbols.empty());
676     SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size);
677     return;
678   }
679 
680   std::vector<Elf_Sym> Syms =
681       toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
682   writeArrayData(OS, makeArrayRef(Syms));
683   SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
684 }
685 
686 template <class ELFT>
687 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
688                                              StringTableBuilder &STB,
689                                              ContiguousBlobAccumulator &CBA,
690                                              ELFYAML::Section *YAMLSec) {
691   zero(SHeader);
692   SHeader.sh_name = DotShStrtab.getOffset(Name);
693   SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
694   SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
695 
696   ELFYAML::RawContentSection *RawSec =
697       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
698 
699   SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, /*Offset=*/None);
700   raw_ostream &OS = CBA.getOS();
701 
702   if (RawSec && (RawSec->Content || RawSec->Size)) {
703     SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size);
704   } else {
705     STB.write(OS);
706     SHeader.sh_size = STB.getSize();
707   }
708 
709   if (YAMLSec && YAMLSec->EntSize)
710     SHeader.sh_entsize = *YAMLSec->EntSize;
711 
712   if (RawSec && RawSec->Info)
713     SHeader.sh_info = *RawSec->Info;
714 
715   if (YAMLSec && YAMLSec->Flags)
716     SHeader.sh_flags = *YAMLSec->Flags;
717   else if (Name == ".dynstr")
718     SHeader.sh_flags = ELF::SHF_ALLOC;
719 
720   assignSectionAddress(SHeader, YAMLSec);
721 }
722 
723 template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
724   ErrHandler(Msg);
725   HasError = true;
726 }
727 
728 template <class ELFT>
729 std::vector<Fragment>
730 ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
731                                  ArrayRef<typename ELFT::Shdr> SHeaders) {
732   DenseMap<StringRef, ELFYAML::Fill *> NameToFill;
733   for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks)
734     if (auto S = dyn_cast<ELFYAML::Fill>(D.get()))
735       NameToFill[S->Name] = S;
736 
737   std::vector<Fragment> Ret;
738   for (const ELFYAML::SectionName &SecName : Phdr.Sections) {
739     unsigned Index;
740     if (SN2I.lookup(SecName.Section, Index)) {
741       const typename ELFT::Shdr &H = SHeaders[Index];
742       Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
743       continue;
744     }
745 
746     if (ELFYAML::Fill *Fill = NameToFill.lookup(SecName.Section)) {
747       Ret.push_back({Fill->ShOffset, Fill->Size, llvm::ELF::SHT_PROGBITS,
748                      /*ShAddrAlign=*/1});
749       continue;
750     }
751 
752     reportError("unknown section or fill referenced: '" + SecName.Section +
753                 "' by program header");
754   }
755 
756   return Ret;
757 }
758 
759 template <class ELFT>
760 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
761                                             std::vector<Elf_Shdr> &SHeaders) {
762   uint32_t PhdrIdx = 0;
763   for (auto &YamlPhdr : Doc.ProgramHeaders) {
764     Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
765     std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
766     if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) {
767           return A.Offset < B.Offset;
768         }))
769       reportError("sections in the program header with index " +
770                   Twine(PhdrIdx) + " are not sorted by their file offset");
771 
772     if (YamlPhdr.Offset) {
773       if (!Fragments.empty() && *YamlPhdr.Offset > Fragments.front().Offset)
774         reportError("'Offset' for segment with index " + Twine(PhdrIdx) +
775                     " must be less than or equal to the minimum file offset of "
776                     "all included sections (0x" +
777                     Twine::utohexstr(Fragments.front().Offset) + ")");
778       PHeader.p_offset = *YamlPhdr.Offset;
779     } else if (!Fragments.empty()) {
780       PHeader.p_offset = Fragments.front().Offset;
781     }
782 
783     // Set the file size if not set explicitly.
784     if (YamlPhdr.FileSize) {
785       PHeader.p_filesz = *YamlPhdr.FileSize;
786     } else if (!Fragments.empty()) {
787       uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
788       // SHT_NOBITS sections occupy no physical space in a file, we should not
789       // take their sizes into account when calculating the file size of a
790       // segment.
791       if (Fragments.back().Type != llvm::ELF::SHT_NOBITS)
792         FileSize += Fragments.back().Size;
793       PHeader.p_filesz = FileSize;
794     }
795 
796     // Find the maximum offset of the end of a section in order to set p_memsz.
797     uint64_t MemOffset = PHeader.p_offset;
798     for (const Fragment &F : Fragments)
799       MemOffset = std::max(MemOffset, F.Offset + F.Size);
800     // Set the memory size if not set explicitly.
801     PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
802                                        : MemOffset - PHeader.p_offset;
803 
804     if (YamlPhdr.Align) {
805       PHeader.p_align = *YamlPhdr.Align;
806     } else {
807       // Set the alignment of the segment to be the maximum alignment of the
808       // sections so that by default the segment has a valid and sensible
809       // alignment.
810       PHeader.p_align = 1;
811       for (const Fragment &F : Fragments)
812         PHeader.p_align = std::max((uint64_t)PHeader.p_align, F.AddrAlign);
813     }
814   }
815 }
816 
817 template <class ELFT>
818 void ELFState<ELFT>::writeSectionContent(
819     Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
820     ContiguousBlobAccumulator &CBA) {
821   SHeader.sh_size = writeContent(CBA.getOS(), Section.Content, Section.Size);
822 
823   if (Section.EntSize)
824     SHeader.sh_entsize = *Section.EntSize;
825 
826   if (Section.Info)
827     SHeader.sh_info = *Section.Info;
828 }
829 
830 static bool isMips64EL(const ELFYAML::Object &Doc) {
831   return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
832          Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
833          Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
834 }
835 
836 template <class ELFT>
837 void ELFState<ELFT>::writeSectionContent(
838     Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
839     ContiguousBlobAccumulator &CBA) {
840   assert((Section.Type == llvm::ELF::SHT_REL ||
841           Section.Type == llvm::ELF::SHT_RELA) &&
842          "Section type is not SHT_REL nor SHT_RELA");
843 
844   bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
845   if (Section.EntSize)
846     SHeader.sh_entsize = *Section.EntSize;
847   else
848     SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
849   SHeader.sh_size = (IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)) *
850                     Section.Relocations.size();
851 
852   // For relocation section set link to .symtab by default.
853   unsigned Link = 0;
854   if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
855     SHeader.sh_link = Link;
856 
857   if (!Section.RelocatableSec.empty())
858     SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
859 
860   raw_ostream &OS = CBA.getOS();
861   for (const auto &Rel : Section.Relocations) {
862     unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name,
863                                                  Section.Link == ".dynsym")
864                                  : 0;
865     if (IsRela) {
866       Elf_Rela REntry;
867       zero(REntry);
868       REntry.r_offset = Rel.Offset;
869       REntry.r_addend = Rel.Addend;
870       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
871       OS.write((const char *)&REntry, sizeof(REntry));
872     } else {
873       Elf_Rel REntry;
874       zero(REntry);
875       REntry.r_offset = Rel.Offset;
876       REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
877       OS.write((const char *)&REntry, sizeof(REntry));
878     }
879   }
880 }
881 
882 template <class ELFT>
883 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
884                                          const ELFYAML::RelrSection &Section,
885                                          ContiguousBlobAccumulator &CBA) {
886   SHeader.sh_entsize =
887       Section.EntSize ? uint64_t(*Section.EntSize) : sizeof(Elf_Relr);
888 
889   raw_ostream &OS = CBA.getOS();
890   if (Section.Content) {
891     SHeader.sh_size = writeContent(OS, Section.Content, None);
892     return;
893   }
894 
895   if (!Section.Entries)
896     return;
897 
898   for (llvm::yaml::Hex64 E : *Section.Entries) {
899     if (!ELFT::Is64Bits && E > UINT32_MAX)
900       reportError(Section.Name + ": the value is too large for 32-bits: 0x" +
901                   Twine::utohexstr(E));
902     support::endian::write<uintX_t>(OS, E, ELFT::TargetEndianness);
903   }
904 
905   SHeader.sh_size = sizeof(uintX_t) * Section.Entries->size();
906 }
907 
908 template <class ELFT>
909 void ELFState<ELFT>::writeSectionContent(
910     Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
911     ContiguousBlobAccumulator &CBA) {
912   for (uint32_t E : Shndx.Entries)
913     support::endian::write<uint32_t>(CBA.getOS(), E, ELFT::TargetEndianness);
914 
915   SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4;
916   SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize;
917 }
918 
919 template <class ELFT>
920 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
921                                          const ELFYAML::Group &Section,
922                                          ContiguousBlobAccumulator &CBA) {
923   assert(Section.Type == llvm::ELF::SHT_GROUP &&
924          "Section type is not SHT_GROUP");
925 
926   unsigned Link = 0;
927   if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
928     SHeader.sh_link = Link;
929 
930   SHeader.sh_entsize = 4;
931   SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
932 
933   if (Section.Signature)
934     SHeader.sh_info =
935         toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
936 
937   raw_ostream &OS = CBA.getOS();
938   for (const ELFYAML::SectionOrType &Member : Section.Members) {
939     unsigned int SectionIndex = 0;
940     if (Member.sectionNameOrType == "GRP_COMDAT")
941       SectionIndex = llvm::ELF::GRP_COMDAT;
942     else
943       SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
944     support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness);
945   }
946 }
947 
948 template <class ELFT>
949 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
950                                          const ELFYAML::SymverSection &Section,
951                                          ContiguousBlobAccumulator &CBA) {
952   raw_ostream &OS = CBA.getOS();
953   for (uint16_t Version : Section.Entries)
954     support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness);
955 
956   SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2;
957   SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize;
958 }
959 
960 template <class ELFT>
961 void ELFState<ELFT>::writeSectionContent(
962     Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section,
963     ContiguousBlobAccumulator &CBA) {
964   raw_ostream &OS = CBA.getOS();
965   if (Section.Content || Section.Size) {
966     SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
967     return;
968   }
969 
970   for (const ELFYAML::StackSizeEntry &E : *Section.Entries) {
971     support::endian::write<uintX_t>(OS, E.Address, ELFT::TargetEndianness);
972     SHeader.sh_size += sizeof(uintX_t) + encodeULEB128(E.Size, OS);
973   }
974 }
975 
976 template <class ELFT>
977 void ELFState<ELFT>::writeSectionContent(
978     Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section,
979     ContiguousBlobAccumulator &CBA) {
980   raw_ostream &OS = CBA.getOS();
981   if (Section.Content) {
982     SHeader.sh_size = writeContent(OS, Section.Content, None);
983     return;
984   }
985 
986   if (!Section.Options)
987     return;
988 
989   for (const ELFYAML::LinkerOption &LO : *Section.Options) {
990     OS.write(LO.Key.data(), LO.Key.size());
991     OS.write('\0');
992     OS.write(LO.Value.data(), LO.Value.size());
993     OS.write('\0');
994     SHeader.sh_size += (LO.Key.size() + LO.Value.size() + 2);
995   }
996 }
997 
998 template <class ELFT>
999 void ELFState<ELFT>::writeSectionContent(
1000     Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section,
1001     ContiguousBlobAccumulator &CBA) {
1002   raw_ostream &OS = CBA.getOS();
1003   if (Section.Content) {
1004     SHeader.sh_size = writeContent(OS, Section.Content, None);
1005     return;
1006   }
1007 
1008   if (!Section.Libs)
1009     return;
1010 
1011   for (StringRef Lib : *Section.Libs) {
1012     OS.write(Lib.data(), Lib.size());
1013     OS.write('\0');
1014     SHeader.sh_size += Lib.size() + 1;
1015   }
1016 }
1017 
1018 template <class ELFT>
1019 uint64_t
1020 ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
1021                               llvm::Optional<llvm::yaml::Hex64> Offset) {
1022   uint64_t CurrentOffset = CBA.getOffset();
1023   uint64_t AlignedOffset;
1024 
1025   if (Offset) {
1026     if ((uint64_t)*Offset < CurrentOffset) {
1027       reportError("the 'Offset' value (0x" +
1028                   Twine::utohexstr((uint64_t)*Offset) + ") goes backward");
1029       return CurrentOffset;
1030     }
1031 
1032     // We ignore an alignment when an explicit offset has been requested.
1033     AlignedOffset = *Offset;
1034   } else {
1035     AlignedOffset = alignTo(CurrentOffset, std::max(Align, (uint64_t)1));
1036   }
1037 
1038   CBA.getOS().write_zeros(AlignedOffset - CurrentOffset);
1039   return AlignedOffset;
1040 }
1041 
1042 template <class ELFT>
1043 void ELFState<ELFT>::writeSectionContent(
1044     Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
1045     ContiguousBlobAccumulator &CBA) {
1046   if (Section.EntSize)
1047     SHeader.sh_entsize = *Section.EntSize;
1048   else
1049     SHeader.sh_entsize = 16;
1050 
1051   unsigned Link = 0;
1052   if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
1053     SHeader.sh_link = Link;
1054 
1055   raw_ostream &OS = CBA.getOS();
1056   if (Section.Content) {
1057     SHeader.sh_size = writeContent(OS, Section.Content, None);
1058     return;
1059   }
1060 
1061   if (!Section.Entries)
1062     return;
1063 
1064   for (const ELFYAML::CallGraphEntry &E : *Section.Entries) {
1065     unsigned From = toSymbolIndex(E.From, Section.Name, /*IsDynamic=*/false);
1066     unsigned To = toSymbolIndex(E.To, Section.Name, /*IsDynamic=*/false);
1067 
1068     support::endian::write<uint32_t>(OS, From, ELFT::TargetEndianness);
1069     support::endian::write<uint32_t>(OS, To, ELFT::TargetEndianness);
1070     support::endian::write<uint64_t>(OS, E.Weight, ELFT::TargetEndianness);
1071     SHeader.sh_size += 16;
1072   }
1073 }
1074 
1075 template <class ELFT>
1076 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1077                                          const ELFYAML::HashSection &Section,
1078                                          ContiguousBlobAccumulator &CBA) {
1079   unsigned Link = 0;
1080   if (Section.Link.empty() && SN2I.lookup(".dynsym", Link))
1081     SHeader.sh_link = Link;
1082 
1083   raw_ostream &OS = CBA.getOS();
1084   if (Section.Content || Section.Size) {
1085     SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
1086     return;
1087   }
1088 
1089   support::endian::write<uint32_t>(
1090       OS, Section.NBucket.getValueOr(llvm::yaml::Hex64(Section.Bucket->size())),
1091       ELFT::TargetEndianness);
1092   support::endian::write<uint32_t>(
1093       OS, Section.NChain.getValueOr(llvm::yaml::Hex64(Section.Chain->size())),
1094       ELFT::TargetEndianness);
1095 
1096   for (uint32_t Val : *Section.Bucket)
1097     support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1098   for (uint32_t Val : *Section.Chain)
1099     support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1100 
1101   SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4;
1102 }
1103 
1104 template <class ELFT>
1105 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1106                                          const ELFYAML::VerdefSection &Section,
1107                                          ContiguousBlobAccumulator &CBA) {
1108   typedef typename ELFT::Verdef Elf_Verdef;
1109   typedef typename ELFT::Verdaux Elf_Verdaux;
1110 
1111   SHeader.sh_info = Section.Info;
1112 
1113   raw_ostream &OS = CBA.getOS();
1114   if (Section.Content) {
1115     SHeader.sh_size = writeContent(OS, Section.Content, None);
1116     return;
1117   }
1118 
1119   if (!Section.Entries)
1120     return;
1121 
1122   uint64_t AuxCnt = 0;
1123   for (size_t I = 0; I < Section.Entries->size(); ++I) {
1124     const ELFYAML::VerdefEntry &E = (*Section.Entries)[I];
1125 
1126     Elf_Verdef VerDef;
1127     VerDef.vd_version = E.Version;
1128     VerDef.vd_flags = E.Flags;
1129     VerDef.vd_ndx = E.VersionNdx;
1130     VerDef.vd_hash = E.Hash;
1131     VerDef.vd_aux = sizeof(Elf_Verdef);
1132     VerDef.vd_cnt = E.VerNames.size();
1133     if (I == Section.Entries->size() - 1)
1134       VerDef.vd_next = 0;
1135     else
1136       VerDef.vd_next =
1137           sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
1138     OS.write((const char *)&VerDef, sizeof(Elf_Verdef));
1139 
1140     for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
1141       Elf_Verdaux VernAux;
1142       VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
1143       if (J == E.VerNames.size() - 1)
1144         VernAux.vda_next = 0;
1145       else
1146         VernAux.vda_next = sizeof(Elf_Verdaux);
1147       OS.write((const char *)&VernAux, sizeof(Elf_Verdaux));
1148     }
1149   }
1150 
1151   SHeader.sh_size = Section.Entries->size() * sizeof(Elf_Verdef) +
1152                     AuxCnt * sizeof(Elf_Verdaux);
1153 }
1154 
1155 template <class ELFT>
1156 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1157                                          const ELFYAML::VerneedSection &Section,
1158                                          ContiguousBlobAccumulator &CBA) {
1159   typedef typename ELFT::Verneed Elf_Verneed;
1160   typedef typename ELFT::Vernaux Elf_Vernaux;
1161 
1162   SHeader.sh_info = Section.Info;
1163 
1164   raw_ostream &OS = CBA.getOS();
1165   if (Section.Content) {
1166     SHeader.sh_size = writeContent(OS, Section.Content, None);
1167     return;
1168   }
1169 
1170   if (!Section.VerneedV)
1171     return;
1172 
1173   uint64_t AuxCnt = 0;
1174   for (size_t I = 0; I < Section.VerneedV->size(); ++I) {
1175     const ELFYAML::VerneedEntry &VE = (*Section.VerneedV)[I];
1176 
1177     Elf_Verneed VerNeed;
1178     VerNeed.vn_version = VE.Version;
1179     VerNeed.vn_file = DotDynstr.getOffset(VE.File);
1180     if (I == Section.VerneedV->size() - 1)
1181       VerNeed.vn_next = 0;
1182     else
1183       VerNeed.vn_next =
1184           sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
1185     VerNeed.vn_cnt = VE.AuxV.size();
1186     VerNeed.vn_aux = sizeof(Elf_Verneed);
1187     OS.write((const char *)&VerNeed, sizeof(Elf_Verneed));
1188 
1189     for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
1190       const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
1191 
1192       Elf_Vernaux VernAux;
1193       VernAux.vna_hash = VAuxE.Hash;
1194       VernAux.vna_flags = VAuxE.Flags;
1195       VernAux.vna_other = VAuxE.Other;
1196       VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
1197       if (J == VE.AuxV.size() - 1)
1198         VernAux.vna_next = 0;
1199       else
1200         VernAux.vna_next = sizeof(Elf_Vernaux);
1201       OS.write((const char *)&VernAux, sizeof(Elf_Vernaux));
1202     }
1203   }
1204 
1205   SHeader.sh_size = Section.VerneedV->size() * sizeof(Elf_Verneed) +
1206                     AuxCnt * sizeof(Elf_Vernaux);
1207 }
1208 
1209 template <class ELFT>
1210 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1211                                          const ELFYAML::MipsABIFlags &Section,
1212                                          ContiguousBlobAccumulator &CBA) {
1213   assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
1214          "Section type is not SHT_MIPS_ABIFLAGS");
1215 
1216   object::Elf_Mips_ABIFlags<ELFT> Flags;
1217   zero(Flags);
1218   SHeader.sh_entsize = sizeof(Flags);
1219   SHeader.sh_size = SHeader.sh_entsize;
1220 
1221   Flags.version = Section.Version;
1222   Flags.isa_level = Section.ISALevel;
1223   Flags.isa_rev = Section.ISARevision;
1224   Flags.gpr_size = Section.GPRSize;
1225   Flags.cpr1_size = Section.CPR1Size;
1226   Flags.cpr2_size = Section.CPR2Size;
1227   Flags.fp_abi = Section.FpABI;
1228   Flags.isa_ext = Section.ISAExtension;
1229   Flags.ases = Section.ASEs;
1230   Flags.flags1 = Section.Flags1;
1231   Flags.flags2 = Section.Flags2;
1232   CBA.getOS().write((const char *)&Flags, sizeof(Flags));
1233 }
1234 
1235 template <class ELFT>
1236 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1237                                          const ELFYAML::DynamicSection &Section,
1238                                          ContiguousBlobAccumulator &CBA) {
1239   assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
1240          "Section type is not SHT_DYNAMIC");
1241 
1242   if (!Section.Entries.empty() && Section.Content)
1243     reportError("cannot specify both raw content and explicit entries "
1244                 "for dynamic section '" +
1245                 Section.Name + "'");
1246 
1247   if (Section.Content)
1248     SHeader.sh_size = Section.Content->binary_size();
1249   else
1250     SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size();
1251   if (Section.EntSize)
1252     SHeader.sh_entsize = *Section.EntSize;
1253   else
1254     SHeader.sh_entsize = sizeof(Elf_Dyn);
1255 
1256   raw_ostream &OS = CBA.getOS();
1257   for (const ELFYAML::DynamicEntry &DE : Section.Entries) {
1258     support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness);
1259     support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness);
1260   }
1261   if (Section.Content)
1262     Section.Content->writeAsBinary(OS);
1263 }
1264 
1265 template <class ELFT>
1266 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1267                                          const ELFYAML::AddrsigSection &Section,
1268                                          ContiguousBlobAccumulator &CBA) {
1269   unsigned Link = 0;
1270   if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
1271     SHeader.sh_link = Link;
1272 
1273   raw_ostream &OS = CBA.getOS();
1274   if (Section.Content || Section.Size) {
1275     SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
1276     return;
1277   }
1278 
1279   for (StringRef Sym : *Section.Symbols)
1280     SHeader.sh_size += encodeULEB128(
1281         toSymbolIndex(Sym, Section.Name, /*IsDynamic=*/false), OS);
1282 }
1283 
1284 template <class ELFT>
1285 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1286                                          const ELFYAML::NoteSection &Section,
1287                                          ContiguousBlobAccumulator &CBA) {
1288   raw_ostream &OS = CBA.getOS();
1289   uint64_t Offset = OS.tell();
1290   if (Section.Content || Section.Size) {
1291     SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
1292     return;
1293   }
1294 
1295   for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
1296     // Write name size.
1297     if (NE.Name.empty())
1298       support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness);
1299     else
1300       support::endian::write<uint32_t>(OS, NE.Name.size() + 1,
1301                                        ELFT::TargetEndianness);
1302 
1303     // Write description size.
1304     if (NE.Desc.binary_size() == 0)
1305       support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness);
1306     else
1307       support::endian::write<uint32_t>(OS, NE.Desc.binary_size(),
1308                                        ELFT::TargetEndianness);
1309 
1310     // Write type.
1311     support::endian::write<uint32_t>(OS, NE.Type, ELFT::TargetEndianness);
1312 
1313     // Write name, null terminator and padding.
1314     if (!NE.Name.empty()) {
1315       support::endian::write<uint8_t>(OS, arrayRefFromStringRef(NE.Name),
1316                                       ELFT::TargetEndianness);
1317       support::endian::write<uint8_t>(OS, 0, ELFT::TargetEndianness);
1318       CBA.padToAlignment(4);
1319     }
1320 
1321     // Write description and padding.
1322     if (NE.Desc.binary_size() != 0) {
1323       NE.Desc.writeAsBinary(OS);
1324       CBA.padToAlignment(4);
1325     }
1326   }
1327 
1328   SHeader.sh_size = OS.tell() - Offset;
1329 }
1330 
1331 template <class ELFT>
1332 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1333                                          const ELFYAML::GnuHashSection &Section,
1334                                          ContiguousBlobAccumulator &CBA) {
1335   unsigned Link = 0;
1336   if (Section.Link.empty() && SN2I.lookup(".dynsym", Link))
1337     SHeader.sh_link = Link;
1338 
1339   raw_ostream &OS = CBA.getOS();
1340   if (Section.Content) {
1341     SHeader.sh_size = writeContent(OS, Section.Content, None);
1342     return;
1343   }
1344 
1345   // We write the header first, starting with the hash buckets count. Normally
1346   // it is the number of entries in HashBuckets, but the "NBuckets" property can
1347   // be used to override this field, which is useful for producing broken
1348   // objects.
1349   if (Section.Header->NBuckets)
1350     support::endian::write<uint32_t>(OS, *Section.Header->NBuckets,
1351                                      ELFT::TargetEndianness);
1352   else
1353     support::endian::write<uint32_t>(OS, Section.HashBuckets->size(),
1354                                      ELFT::TargetEndianness);
1355 
1356   // Write the index of the first symbol in the dynamic symbol table accessible
1357   // via the hash table.
1358   support::endian::write<uint32_t>(OS, Section.Header->SymNdx,
1359                                    ELFT::TargetEndianness);
1360 
1361   // Write the number of words in the Bloom filter. As above, the "MaskWords"
1362   // property can be used to set this field to any value.
1363   if (Section.Header->MaskWords)
1364     support::endian::write<uint32_t>(OS, *Section.Header->MaskWords,
1365                                      ELFT::TargetEndianness);
1366   else
1367     support::endian::write<uint32_t>(OS, Section.BloomFilter->size(),
1368                                      ELFT::TargetEndianness);
1369 
1370   // Write the shift constant used by the Bloom filter.
1371   support::endian::write<uint32_t>(OS, Section.Header->Shift2,
1372                                    ELFT::TargetEndianness);
1373 
1374   // We've finished writing the header. Now write the Bloom filter.
1375   for (llvm::yaml::Hex64 Val : *Section.BloomFilter)
1376     support::endian::write<typename ELFT::uint>(OS, Val,
1377                                                 ELFT::TargetEndianness);
1378 
1379   // Write an array of hash buckets.
1380   for (llvm::yaml::Hex32 Val : *Section.HashBuckets)
1381     support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1382 
1383   // Write an array of hash values.
1384   for (llvm::yaml::Hex32 Val : *Section.HashValues)
1385     support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
1386 
1387   SHeader.sh_size = 16 /*Header size*/ +
1388                     Section.BloomFilter->size() * sizeof(typename ELFT::uint) +
1389                     Section.HashBuckets->size() * 4 +
1390                     Section.HashValues->size() * 4;
1391 }
1392 
1393 template <class ELFT>
1394 void ELFState<ELFT>::writeFill(ELFYAML::Fill &Fill,
1395                                ContiguousBlobAccumulator &CBA) {
1396   raw_ostream &OS = CBA.getOS();
1397   size_t PatternSize = Fill.Pattern ? Fill.Pattern->binary_size() : 0;
1398   if (!PatternSize) {
1399     OS.write_zeros(Fill.Size);
1400     return;
1401   }
1402 
1403   // Fill the content with the specified pattern.
1404   uint64_t Written = 0;
1405   for (; Written + PatternSize <= Fill.Size; Written += PatternSize)
1406     Fill.Pattern->writeAsBinary(OS);
1407   Fill.Pattern->writeAsBinary(OS, Fill.Size - Written);
1408 }
1409 
1410 template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
1411   size_t SecNdx = -1;
1412   StringSet<> Seen;
1413   for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
1414     const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
1415     bool IsSection = isa<ELFYAML::Section>(C.get());
1416     if (IsSection)
1417       ++SecNdx;
1418 
1419     if (C->Name.empty())
1420       continue;
1421 
1422     if (!Seen.insert(C->Name).second)
1423       reportError("repeated section/fill name: '" + C->Name +
1424                   "' at YAML section/fill number " + Twine(I));
1425     if (!IsSection || HasError)
1426       continue;
1427 
1428     if (!SN2I.addName(C->Name, SecNdx))
1429       llvm_unreachable("buildSectionIndex() failed");
1430     DotShStrtab.add(ELFYAML::dropUniqueSuffix(C->Name));
1431   }
1432 
1433   DotShStrtab.finalize();
1434 }
1435 
1436 template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
1437   auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) {
1438     for (size_t I = 0, S = V.size(); I < S; ++I) {
1439       const ELFYAML::Symbol &Sym = V[I];
1440       if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1))
1441         reportError("repeated symbol name: '" + Sym.Name + "'");
1442     }
1443   };
1444 
1445   if (Doc.Symbols)
1446     Build(*Doc.Symbols, SymN2I);
1447   if (Doc.DynamicSymbols)
1448     Build(*Doc.DynamicSymbols, DynSymN2I);
1449 }
1450 
1451 template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
1452   // Add the regular symbol names to .strtab section.
1453   if (Doc.Symbols)
1454     for (const ELFYAML::Symbol &Sym : *Doc.Symbols)
1455       DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1456   DotStrtab.finalize();
1457 
1458   // Add the dynamic symbol names to .dynstr section.
1459   if (Doc.DynamicSymbols)
1460     for (const ELFYAML::Symbol &Sym : *Doc.DynamicSymbols)
1461       DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1462 
1463   // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1464   // add strings to .dynstr section.
1465   for (const ELFYAML::Chunk *Sec : Doc.getSections()) {
1466     if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
1467       if (VerNeed->VerneedV) {
1468         for (const ELFYAML::VerneedEntry &VE : *VerNeed->VerneedV) {
1469           DotDynstr.add(VE.File);
1470           for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1471             DotDynstr.add(Aux.Name);
1472         }
1473       }
1474     } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
1475       if (VerDef->Entries)
1476         for (const ELFYAML::VerdefEntry &E : *VerDef->Entries)
1477           for (StringRef Name : E.VerNames)
1478             DotDynstr.add(Name);
1479     }
1480   }
1481 
1482   DotDynstr.finalize();
1483 }
1484 
1485 template <class ELFT>
1486 bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
1487                               yaml::ErrorHandler EH) {
1488   ELFState<ELFT> State(Doc, EH);
1489 
1490   // Finalize .strtab and .dynstr sections. We do that early because want to
1491   // finalize the string table builders before writing the content of the
1492   // sections that might want to use them.
1493   State.finalizeStrings();
1494 
1495   State.buildSectionIndex();
1496   if (State.HasError)
1497     return false;
1498 
1499   State.buildSymbolIndexes();
1500 
1501   std::vector<Elf_Phdr> PHeaders;
1502   State.initProgramHeaders(PHeaders);
1503 
1504   // XXX: This offset is tightly coupled with the order that we write
1505   // things to `OS`.
1506   const size_t SectionContentBeginOffset =
1507       sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
1508   ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
1509 
1510   std::vector<Elf_Shdr> SHeaders;
1511   State.initSectionHeaders(SHeaders, CBA);
1512 
1513   // Now we can decide segment offsets.
1514   State.setProgramHeaderLayout(PHeaders, SHeaders);
1515 
1516   if (State.HasError)
1517     return false;
1518 
1519   State.writeELFHeader(CBA, OS);
1520   writeArrayData(OS, makeArrayRef(PHeaders));
1521   CBA.writeBlobToStream(OS);
1522   writeArrayData(OS, makeArrayRef(SHeaders));
1523   return true;
1524 }
1525 
1526 namespace llvm {
1527 namespace yaml {
1528 
1529 bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
1530   bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1531   bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1532   if (Is64Bit) {
1533     if (IsLE)
1534       return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH);
1535     return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH);
1536   }
1537   if (IsLE)
1538     return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH);
1539   return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH);
1540 }
1541 
1542 } // namespace yaml
1543 } // namespace llvm
1544