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/SetVector.h"
17#include "llvm/ADT/StringSet.h"
18#include "llvm/BinaryFormat/ELF.h"
19#include "llvm/MC/StringTableBuilder.h"
20#include "llvm/Object/ELFObjectFile.h"
21#include "llvm/Object/ELFTypes.h"
22#include "llvm/ObjectYAML/DWARFEmitter.h"
23#include "llvm/ObjectYAML/DWARFYAML.h"
24#include "llvm/ObjectYAML/ELFYAML.h"
25#include "llvm/ObjectYAML/yaml2obj.h"
26#include "llvm/Support/EndianStream.h"
27#include "llvm/Support/Errc.h"
28#include "llvm/Support/Error.h"
29#include "llvm/Support/LEB128.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/WithColor.h"
32#include "llvm/Support/YAMLTraits.h"
33#include "llvm/Support/raw_ostream.h"
34#include <optional>
35
36using namespace llvm;
37
38// This class is used to build up a contiguous binary blob while keeping
39// track of an offset in the output (which notionally begins at
40// `InitialOffset`).
41// The blob might be limited to an arbitrary size. All attempts to write data
42// are ignored and the error condition is remembered once the limit is reached.
43// Such an approach allows us to simplify the code by delaying error reporting
44// and doing it at a convenient time.
45namespace {
46class ContiguousBlobAccumulator {
47  const uint64_t InitialOffset;
48  const uint64_t MaxSize;
49
50  SmallVector<char, 128> Buf;
51  raw_svector_ostream OS;
52  Error ReachedLimitErr = Error::success();
53
54  bool checkLimit(uint64_t Size) {
55    if (!ReachedLimitErr && getOffset() + Size <= MaxSize)
56      return true;
57    if (!ReachedLimitErr)
58      ReachedLimitErr = createStringError(errc::invalid_argument,
59                                          "reached the output size limit");
60    return false;
61  }
62
63public:
64  ContiguousBlobAccumulator(uint64_t BaseOffset, uint64_t SizeLimit)
65      : InitialOffset(BaseOffset), MaxSize(SizeLimit), OS(Buf) {}
66
67  uint64_t tell() const { return OS.tell(); }
68  uint64_t getOffset() const { return InitialOffset + OS.tell(); }
69  void writeBlobToStream(raw_ostream &Out) const { Out << OS.str(); }
70
71  Error takeLimitError() {
72    // Request to write 0 bytes to check we did not reach the limit.
73    checkLimit(0);
74    return std::move(ReachedLimitErr);
75  }
76
77  /// \returns The new offset.
78  uint64_t padToAlignment(unsigned Align) {
79    uint64_t CurrentOffset = getOffset();
80    if (ReachedLimitErr)
81      return CurrentOffset;
82
83    uint64_t AlignedOffset = alignTo(CurrentOffset, Align == 0 ? 1 : Align);
84    uint64_t PaddingSize = AlignedOffset - CurrentOffset;
85    if (!checkLimit(PaddingSize))
86      return CurrentOffset;
87
88    writeZeros(PaddingSize);
89    return AlignedOffset;
90  }
91
92  raw_ostream *getRawOS(uint64_t Size) {
93    if (checkLimit(Size))
94      return &OS;
95    return nullptr;
96  }
97
98  void writeAsBinary(const yaml::BinaryRef &Bin, uint64_t N = UINT64_MAX) {
99    if (!checkLimit(Bin.binary_size()))
100      return;
101    Bin.writeAsBinary(OS, N);
102  }
103
104  void writeZeros(uint64_t Num) {
105    if (checkLimit(Num))
106      OS.write_zeros(Num);
107  }
108
109  void write(const char *Ptr, size_t Size) {
110    if (checkLimit(Size))
111      OS.write(Ptr, Size);
112  }
113
114  void write(unsigned char C) {
115    if (checkLimit(1))
116      OS.write(C);
117  }
118
119  unsigned writeULEB128(uint64_t Val) {
120    if (!checkLimit(sizeof(uint64_t)))
121      return 0;
122    return encodeULEB128(Val, OS);
123  }
124
125  template <typename T> void write(T Val, support::endianness E) {
126    if (checkLimit(sizeof(T)))
127      support::endian::write<T>(OS, Val, E);
128  }
129
130  void updateDataAt(uint64_t Pos, void *Data, size_t Size) {
131    assert(Pos >= InitialOffset && Pos + Size <= getOffset());
132    memcpy(&Buf[Pos - InitialOffset], Data, Size);
133  }
134};
135
136// Used to keep track of section and symbol names, so that in the YAML file
137// sections and symbols can be referenced by name instead of by index.
138class NameToIdxMap {
139  StringMap<unsigned> Map;
140
141public:
142  /// \Returns false if name is already present in the map.
143  bool addName(StringRef Name, unsigned Ndx) {
144    return Map.insert({Name, Ndx}).second;
145  }
146  /// \Returns false if name is not present in the map.
147  bool lookup(StringRef Name, unsigned &Idx) const {
148    auto I = Map.find(Name);
149    if (I == Map.end())
150      return false;
151    Idx = I->getValue();
152    return true;
153  }
154  /// Asserts if name is not present in the map.
155  unsigned get(StringRef Name) const {
156    unsigned Idx;
157    if (lookup(Name, Idx))
158      return Idx;
159    assert(false && "Expected section not found in index");
160    return 0;
161  }
162  unsigned size() const { return Map.size(); }
163};
164
165namespace {
166struct Fragment {
167  uint64_t Offset;
168  uint64_t Size;
169  uint32_t Type;
170  uint64_t AddrAlign;
171};
172} // namespace
173
174/// "Single point of truth" for the ELF file construction.
175/// TODO: This class still has a ways to go before it is truly a "single
176/// point of truth".
177template <class ELFT> class ELFState {
178  LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
179
180  enum class SymtabType { Static, Dynamic };
181
182  /// The future symbol table string section.
183  StringTableBuilder DotStrtab{StringTableBuilder::ELF};
184
185  /// The future section header string table section, if a unique string table
186  /// is needed. Don't reference this variable direectly: use the
187  /// ShStrtabStrings member instead.
188  StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
189
190  /// The future dynamic symbol string section.
191  StringTableBuilder DotDynstr{StringTableBuilder::ELF};
192
193  /// The name of the section header string table section. If it is .strtab or
194  /// .dynstr, the section header strings will be written to the same string
195  /// table as the static/dynamic symbols respectively. Otherwise a dedicated
196  /// section will be created with that name.
197  StringRef SectionHeaderStringTableName = ".shstrtab";
198  StringTableBuilder *ShStrtabStrings = &DotShStrtab;
199
200  NameToIdxMap SN2I;
201  NameToIdxMap SymN2I;
202  NameToIdxMap DynSymN2I;
203  ELFYAML::Object &Doc;
204
205  StringSet<> ExcludedSectionHeaders;
206
207  uint64_t LocationCounter = 0;
208  bool HasError = false;
209  yaml::ErrorHandler ErrHandler;
210  void reportError(const Twine &Msg);
211  void reportError(Error Err);
212
213  std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
214                                    const StringTableBuilder &Strtab);
215  unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = "");
216  unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic);
217
218  void buildSectionIndex();
219  void buildSymbolIndexes();
220  void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
221  bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
222                          StringRef SecName, ELFYAML::Section *YAMLSec);
223  void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
224                          ContiguousBlobAccumulator &CBA);
225  void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
226                               ContiguousBlobAccumulator &CBA,
227                               ELFYAML::Section *YAMLSec);
228  void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
229                               StringTableBuilder &STB,
230                               ContiguousBlobAccumulator &CBA,
231                               ELFYAML::Section *YAMLSec);
232  void initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
233                              ContiguousBlobAccumulator &CBA,
234                              ELFYAML::Section *YAMLSec);
235  void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
236                              std::vector<Elf_Shdr> &SHeaders);
237
238  std::vector<Fragment>
239  getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
240                   ArrayRef<typename ELFT::Shdr> SHeaders);
241
242  void finalizeStrings();
243  void writeELFHeader(raw_ostream &OS);
244  void writeSectionContent(Elf_Shdr &SHeader,
245                           const ELFYAML::NoBitsSection &Section,
246                           ContiguousBlobAccumulator &CBA);
247  void writeSectionContent(Elf_Shdr &SHeader,
248                           const ELFYAML::RawContentSection &Section,
249                           ContiguousBlobAccumulator &CBA);
250  void writeSectionContent(Elf_Shdr &SHeader,
251                           const ELFYAML::RelocationSection &Section,
252                           ContiguousBlobAccumulator &CBA);
253  void writeSectionContent(Elf_Shdr &SHeader,
254                           const ELFYAML::RelrSection &Section,
255                           ContiguousBlobAccumulator &CBA);
256  void writeSectionContent(Elf_Shdr &SHeader,
257                           const ELFYAML::GroupSection &Group,
258                           ContiguousBlobAccumulator &CBA);
259  void writeSectionContent(Elf_Shdr &SHeader,
260                           const ELFYAML::SymtabShndxSection &Shndx,
261                           ContiguousBlobAccumulator &CBA);
262  void writeSectionContent(Elf_Shdr &SHeader,
263                           const ELFYAML::SymverSection &Section,
264                           ContiguousBlobAccumulator &CBA);
265  void writeSectionContent(Elf_Shdr &SHeader,
266                           const ELFYAML::VerneedSection &Section,
267                           ContiguousBlobAccumulator &CBA);
268  void writeSectionContent(Elf_Shdr &SHeader,
269                           const ELFYAML::VerdefSection &Section,
270                           ContiguousBlobAccumulator &CBA);
271  void writeSectionContent(Elf_Shdr &SHeader,
272                           const ELFYAML::ARMIndexTableSection &Section,
273                           ContiguousBlobAccumulator &CBA);
274  void writeSectionContent(Elf_Shdr &SHeader,
275                           const ELFYAML::MipsABIFlags &Section,
276                           ContiguousBlobAccumulator &CBA);
277  void writeSectionContent(Elf_Shdr &SHeader,
278                           const ELFYAML::DynamicSection &Section,
279                           ContiguousBlobAccumulator &CBA);
280  void writeSectionContent(Elf_Shdr &SHeader,
281                           const ELFYAML::StackSizesSection &Section,
282                           ContiguousBlobAccumulator &CBA);
283  void writeSectionContent(Elf_Shdr &SHeader,
284                           const ELFYAML::BBAddrMapSection &Section,
285                           ContiguousBlobAccumulator &CBA);
286  void writeSectionContent(Elf_Shdr &SHeader,
287                           const ELFYAML::HashSection &Section,
288                           ContiguousBlobAccumulator &CBA);
289  void writeSectionContent(Elf_Shdr &SHeader,
290                           const ELFYAML::AddrsigSection &Section,
291                           ContiguousBlobAccumulator &CBA);
292  void writeSectionContent(Elf_Shdr &SHeader,
293                           const ELFYAML::NoteSection &Section,
294                           ContiguousBlobAccumulator &CBA);
295  void writeSectionContent(Elf_Shdr &SHeader,
296                           const ELFYAML::GnuHashSection &Section,
297                           ContiguousBlobAccumulator &CBA);
298  void writeSectionContent(Elf_Shdr &SHeader,
299                           const ELFYAML::LinkerOptionsSection &Section,
300                           ContiguousBlobAccumulator &CBA);
301  void writeSectionContent(Elf_Shdr &SHeader,
302                           const ELFYAML::DependentLibrariesSection &Section,
303                           ContiguousBlobAccumulator &CBA);
304  void writeSectionContent(Elf_Shdr &SHeader,
305                           const ELFYAML::CallGraphProfileSection &Section,
306                           ContiguousBlobAccumulator &CBA);
307
308  void writeFill(ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
309
310  ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH);
311
312  void assignSectionAddress(Elf_Shdr &SHeader, ELFYAML::Section *YAMLSec);
313
314  DenseMap<StringRef, size_t> buildSectionHeaderReorderMap();
315
316  BumpPtrAllocator StringAlloc;
317  uint64_t alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
318                         std::optional<llvm::yaml::Hex64> Offset);
319
320  uint64_t getSectionNameOffset(StringRef Name);
321
322public:
323  static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
324                       yaml::ErrorHandler EH, uint64_t MaxSize);
325};
326} // end anonymous namespace
327
328template <class T> static size_t arrayDataSize(ArrayRef<T> A) {
329  return A.size() * sizeof(T);
330}
331
332template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
333  OS.write((const char *)A.data(), arrayDataSize(A));
334}
335
336template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); }
337
338template <class ELFT>
339ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
340    : Doc(D), ErrHandler(EH) {
341  // The input may explicitly request to store the section header table strings
342  // in the same string table as dynamic or static symbol names. Set the
343  // ShStrtabStrings member accordingly.
344  if (Doc.Header.SectionHeaderStringTable) {
345    SectionHeaderStringTableName = *Doc.Header.SectionHeaderStringTable;
346    if (*Doc.Header.SectionHeaderStringTable == ".strtab")
347      ShStrtabStrings = &DotStrtab;
348    else if (*Doc.Header.SectionHeaderStringTable == ".dynstr")
349      ShStrtabStrings = &DotDynstr;
350    // Otherwise, the unique table will be used.
351  }
352
353  std::vector<ELFYAML::Section *> Sections = Doc.getSections();
354  // Insert SHT_NULL section implicitly when it is not defined in YAML.
355  if (Sections.empty() || Sections.front()->Type != ELF::SHT_NULL)
356    Doc.Chunks.insert(
357        Doc.Chunks.begin(),
358        std::make_unique<ELFYAML::Section>(
359            ELFYAML::Chunk::ChunkKind::RawContent, /*IsImplicit=*/true));
360
361  StringSet<> DocSections;
362  ELFYAML::SectionHeaderTable *SecHdrTable = nullptr;
363  for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
364    const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
365
366    // We might have an explicit section header table declaration.
367    if (auto S = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) {
368      if (SecHdrTable)
369        reportError("multiple section header tables are not allowed");
370      SecHdrTable = S;
371      continue;
372    }
373
374    // We add a technical suffix for each unnamed section/fill. It does not
375    // affect the output, but allows us to map them by name in the code and
376    // report better error messages.
377    if (C->Name.empty()) {
378      std::string NewName = ELFYAML::appendUniqueSuffix(
379          /*Name=*/"", "index " + Twine(I));
380      C->Name = StringRef(NewName).copy(StringAlloc);
381      assert(ELFYAML::dropUniqueSuffix(C->Name).empty());
382    }
383
384    if (!DocSections.insert(C->Name).second)
385      reportError("repeated section/fill name: '" + C->Name +
386                  "' at YAML section/fill number " + Twine(I));
387  }
388
389  SmallSetVector<StringRef, 8> ImplicitSections;
390  if (Doc.DynamicSymbols) {
391    if (SectionHeaderStringTableName == ".dynsym")
392      reportError("cannot use '.dynsym' as the section header name table when "
393                  "there are dynamic symbols");
394    ImplicitSections.insert(".dynsym");
395    ImplicitSections.insert(".dynstr");
396  }
397  if (Doc.Symbols) {
398    if (SectionHeaderStringTableName == ".symtab")
399      reportError("cannot use '.symtab' as the section header name table when "
400                  "there are symbols");
401    ImplicitSections.insert(".symtab");
402  }
403  if (Doc.DWARF)
404    for (StringRef DebugSecName : Doc.DWARF->getNonEmptySectionNames()) {
405      std::string SecName = ("." + DebugSecName).str();
406      // TODO: For .debug_str it should be possible to share the string table,
407      // in the same manner as the symbol string tables.
408      if (SectionHeaderStringTableName == SecName)
409        reportError("cannot use '" + SecName +
410                    "' as the section header name table when it is needed for "
411                    "DWARF output");
412      ImplicitSections.insert(StringRef(SecName).copy(StringAlloc));
413    }
414  // TODO: Only create the .strtab here if any symbols have been requested.
415  ImplicitSections.insert(".strtab");
416  if (!SecHdrTable || !SecHdrTable->NoHeaders.value_or(false))
417    ImplicitSections.insert(SectionHeaderStringTableName);
418
419  // Insert placeholders for implicit sections that are not
420  // defined explicitly in YAML.
421  for (StringRef SecName : ImplicitSections) {
422    if (DocSections.count(SecName))
423      continue;
424
425    std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
426        ELFYAML::Chunk::ChunkKind::RawContent, true /*IsImplicit*/);
427    Sec->Name = SecName;
428
429    if (SecName == SectionHeaderStringTableName)
430      Sec->Type = ELF::SHT_STRTAB;
431    else if (SecName == ".dynsym")
432      Sec->Type = ELF::SHT_DYNSYM;
433    else if (SecName == ".symtab")
434      Sec->Type = ELF::SHT_SYMTAB;
435    else
436      Sec->Type = ELF::SHT_STRTAB;
437
438    // When the section header table is explicitly defined at the end of the
439    // sections list, it is reasonable to assume that the user wants to reorder
440    // section headers, but still wants to place the section header table after
441    // all sections, like it normally happens. In this case we want to insert
442    // other implicit sections right before the section header table.
443    if (Doc.Chunks.back().get() == SecHdrTable)
444      Doc.Chunks.insert(Doc.Chunks.end() - 1, std::move(Sec));
445    else
446      Doc.Chunks.push_back(std::move(Sec));
447  }
448
449  // Insert the section header table implicitly at the end, when it is not
450  // explicitly defined.
451  if (!SecHdrTable)
452    Doc.Chunks.push_back(
453        std::make_unique<ELFYAML::SectionHeaderTable>(/*IsImplicit=*/true));
454}
455
456template <class ELFT>
457void ELFState<ELFT>::writeELFHeader(raw_ostream &OS) {
458  using namespace llvm::ELF;
459
460  Elf_Ehdr Header;
461  zero(Header);
462  Header.e_ident[EI_MAG0] = 0x7f;
463  Header.e_ident[EI_MAG1] = 'E';
464  Header.e_ident[EI_MAG2] = 'L';
465  Header.e_ident[EI_MAG3] = 'F';
466  Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
467  Header.e_ident[EI_DATA] = Doc.Header.Data;
468  Header.e_ident[EI_VERSION] = EV_CURRENT;
469  Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
470  Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion;
471  Header.e_type = Doc.Header.Type;
472
473  if (Doc.Header.Machine)
474    Header.e_machine = *Doc.Header.Machine;
475  else
476    Header.e_machine = EM_NONE;
477
478  Header.e_version = EV_CURRENT;
479  Header.e_entry = Doc.Header.Entry;
480  Header.e_flags = Doc.Header.Flags;
481  Header.e_ehsize = sizeof(Elf_Ehdr);
482
483  if (Doc.Header.EPhOff)
484    Header.e_phoff = *Doc.Header.EPhOff;
485  else if (!Doc.ProgramHeaders.empty())
486    Header.e_phoff = sizeof(Header);
487  else
488    Header.e_phoff = 0;
489
490  if (Doc.Header.EPhEntSize)
491    Header.e_phentsize = *Doc.Header.EPhEntSize;
492  else if (!Doc.ProgramHeaders.empty())
493    Header.e_phentsize = sizeof(Elf_Phdr);
494  else
495    Header.e_phentsize = 0;
496
497  if (Doc.Header.EPhNum)
498    Header.e_phnum = *Doc.Header.EPhNum;
499  else if (!Doc.ProgramHeaders.empty())
500    Header.e_phnum = Doc.ProgramHeaders.size();
501  else
502    Header.e_phnum = 0;
503
504  Header.e_shentsize = Doc.Header.EShEntSize ? (uint16_t)*Doc.Header.EShEntSize
505                                             : sizeof(Elf_Shdr);
506
507  const ELFYAML::SectionHeaderTable &SectionHeaders =
508      Doc.getSectionHeaderTable();
509
510  if (Doc.Header.EShOff)
511    Header.e_shoff = *Doc.Header.EShOff;
512  else if (SectionHeaders.Offset)
513    Header.e_shoff = *SectionHeaders.Offset;
514  else
515    Header.e_shoff = 0;
516
517  if (Doc.Header.EShNum)
518    Header.e_shnum = *Doc.Header.EShNum;
519  else
520    Header.e_shnum = SectionHeaders.getNumHeaders(Doc.getSections().size());
521
522  if (Doc.Header.EShStrNdx)
523    Header.e_shstrndx = *Doc.Header.EShStrNdx;
524  else if (SectionHeaders.Offset &&
525           !ExcludedSectionHeaders.count(SectionHeaderStringTableName))
526    Header.e_shstrndx = SN2I.get(SectionHeaderStringTableName);
527  else
528    Header.e_shstrndx = 0;
529
530  OS.write((const char *)&Header, sizeof(Header));
531}
532
533template <class ELFT>
534void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
535  DenseMap<StringRef, ELFYAML::Fill *> NameToFill;
536  DenseMap<StringRef, size_t> NameToIndex;
537  for (size_t I = 0, E = Doc.Chunks.size(); I != E; ++I) {
538    if (auto S = dyn_cast<ELFYAML::Fill>(Doc.Chunks[I].get()))
539      NameToFill[S->Name] = S;
540    NameToIndex[Doc.Chunks[I]->Name] = I + 1;
541  }
542
543  std::vector<ELFYAML::Section *> Sections = Doc.getSections();
544  for (size_t I = 0, E = Doc.ProgramHeaders.size(); I != E; ++I) {
545    ELFYAML::ProgramHeader &YamlPhdr = Doc.ProgramHeaders[I];
546    Elf_Phdr Phdr;
547    zero(Phdr);
548    Phdr.p_type = YamlPhdr.Type;
549    Phdr.p_flags = YamlPhdr.Flags;
550    Phdr.p_vaddr = YamlPhdr.VAddr;
551    Phdr.p_paddr = YamlPhdr.PAddr;
552    PHeaders.push_back(Phdr);
553
554    if (!YamlPhdr.FirstSec && !YamlPhdr.LastSec)
555      continue;
556
557    // Get the index of the section, or 0 in the case when the section doesn't exist.
558    size_t First = NameToIndex[*YamlPhdr.FirstSec];
559    if (!First)
560      reportError("unknown section or fill referenced: '" + *YamlPhdr.FirstSec +
561                  "' by the 'FirstSec' key of the program header with index " +
562                  Twine(I));
563    size_t Last = NameToIndex[*YamlPhdr.LastSec];
564    if (!Last)
565      reportError("unknown section or fill referenced: '" + *YamlPhdr.LastSec +
566                  "' by the 'LastSec' key of the program header with index " +
567                  Twine(I));
568    if (!First || !Last)
569      continue;
570
571    if (First > Last)
572      reportError("program header with index " + Twine(I) +
573                  ": the section index of " + *YamlPhdr.FirstSec +
574                  " is greater than the index of " + *YamlPhdr.LastSec);
575
576    for (size_t I = First; I <= Last; ++I)
577      YamlPhdr.Chunks.push_back(Doc.Chunks[I - 1].get());
578  }
579}
580
581template <class ELFT>
582unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
583                                        StringRef LocSym) {
584  assert(LocSec.empty() || LocSym.empty());
585
586  unsigned Index;
587  if (!SN2I.lookup(S, Index) && !to_integer(S, Index)) {
588    if (!LocSym.empty())
589      reportError("unknown section referenced: '" + S + "' by YAML symbol '" +
590                  LocSym + "'");
591    else
592      reportError("unknown section referenced: '" + S + "' by YAML section '" +
593                  LocSec + "'");
594    return 0;
595  }
596
597  const ELFYAML::SectionHeaderTable &SectionHeaders =
598      Doc.getSectionHeaderTable();
599  if (SectionHeaders.IsImplicit ||
600      (SectionHeaders.NoHeaders && !*SectionHeaders.NoHeaders) ||
601      SectionHeaders.isDefault())
602    return Index;
603
604  assert(!SectionHeaders.NoHeaders.value_or(false) || !SectionHeaders.Sections);
605  size_t FirstExcluded =
606      SectionHeaders.Sections ? SectionHeaders.Sections->size() : 0;
607  if (Index > FirstExcluded) {
608    if (LocSym.empty())
609      reportError("unable to link '" + LocSec + "' to excluded section '" + S +
610                  "'");
611    else
612      reportError("excluded section referenced: '" + S + "'  by symbol '" +
613                  LocSym + "'");
614  }
615  return Index;
616}
617
618template <class ELFT>
619unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec,
620                                       bool IsDynamic) {
621  const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I;
622  unsigned Index;
623  // Here we try to look up S in the symbol table. If it is not there,
624  // treat its value as a symbol index.
625  if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) {
626    reportError("unknown symbol referenced: '" + S + "' by YAML section '" +
627                LocSec + "'");
628    return 0;
629  }
630  return Index;
631}
632
633template <class ELFT>
634static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) {
635  if (!From)
636    return;
637  if (From->ShAddrAlign)
638    To.sh_addralign = *From->ShAddrAlign;
639  if (From->ShFlags)
640    To.sh_flags = *From->ShFlags;
641  if (From->ShName)
642    To.sh_name = *From->ShName;
643  if (From->ShOffset)
644    To.sh_offset = *From->ShOffset;
645  if (From->ShSize)
646    To.sh_size = *From->ShSize;
647  if (From->ShType)
648    To.sh_type = *From->ShType;
649}
650
651template <class ELFT>
652bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
653                                        Elf_Shdr &Header, StringRef SecName,
654                                        ELFYAML::Section *YAMLSec) {
655  // Check if the header was already initialized.
656  if (Header.sh_offset)
657    return false;
658
659  if (SecName == ".strtab")
660    initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
661  else if (SecName == ".dynstr")
662    initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
663  else if (SecName == SectionHeaderStringTableName)
664    initStrtabSectionHeader(Header, SecName, *ShStrtabStrings, CBA, YAMLSec);
665  else if (SecName == ".symtab")
666    initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
667  else if (SecName == ".dynsym")
668    initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
669  else if (SecName.startswith(".debug_")) {
670    // If a ".debug_*" section's type is a preserved one, e.g., SHT_DYNAMIC, we
671    // will not treat it as a debug section.
672    if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
673      return false;
674    initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
675  } else
676    return false;
677
678  LocationCounter += Header.sh_size;
679
680  // Override section fields if requested.
681  overrideFields<ELFT>(YAMLSec, Header);
682  return true;
683}
684
685constexpr char SuffixStart = '(';
686constexpr char SuffixEnd = ')';
687
688std::string llvm::ELFYAML::appendUniqueSuffix(StringRef Name,
689                                              const Twine &Msg) {
690  // Do not add a space when a Name is empty.
691  std::string Ret = Name.empty() ? "" : Name.str() + ' ';
692  return Ret + (Twine(SuffixStart) + Msg + Twine(SuffixEnd)).str();
693}
694
695StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) {
696  if (S.empty() || S.back() != SuffixEnd)
697    return S;
698
699  // A special case for empty names. See appendUniqueSuffix() above.
700  size_t SuffixPos = S.rfind(SuffixStart);
701  if (SuffixPos == 0)
702    return "";
703
704  if (SuffixPos == StringRef::npos || S[SuffixPos - 1] != ' ')
705    return S;
706  return S.substr(0, SuffixPos - 1);
707}
708
709template <class ELFT>
710uint64_t ELFState<ELFT>::getSectionNameOffset(StringRef Name) {
711  // If a section is excluded from section headers, we do not save its name in
712  // the string table.
713  if (ExcludedSectionHeaders.count(Name))
714    return 0;
715  return ShStrtabStrings->getOffset(Name);
716}
717
718static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
719                             const std::optional<yaml::BinaryRef> &Content,
720                             const std::optional<llvm::yaml::Hex64> &Size) {
721  size_t ContentSize = 0;
722  if (Content) {
723    CBA.writeAsBinary(*Content);
724    ContentSize = Content->binary_size();
725  }
726
727  if (!Size)
728    return ContentSize;
729
730  CBA.writeZeros(*Size - ContentSize);
731  return *Size;
732}
733
734static StringRef getDefaultLinkSec(unsigned SecType) {
735  switch (SecType) {
736  case ELF::SHT_REL:
737  case ELF::SHT_RELA:
738  case ELF::SHT_GROUP:
739  case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
740  case ELF::SHT_LLVM_ADDRSIG:
741    return ".symtab";
742  case ELF::SHT_GNU_versym:
743  case ELF::SHT_HASH:
744  case ELF::SHT_GNU_HASH:
745    return ".dynsym";
746  case ELF::SHT_DYNSYM:
747  case ELF::SHT_GNU_verdef:
748  case ELF::SHT_GNU_verneed:
749    return ".dynstr";
750  case ELF::SHT_SYMTAB:
751    return ".strtab";
752  default:
753    return "";
754  }
755}
756
757template <class ELFT>
758void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
759                                        ContiguousBlobAccumulator &CBA) {
760  // Ensure SHN_UNDEF entry is present. An all-zero section header is a
761  // valid SHN_UNDEF entry since SHT_NULL == 0.
762  SHeaders.resize(Doc.getSections().size());
763
764  for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) {
765    if (ELFYAML::Fill *S = dyn_cast<ELFYAML::Fill>(D.get())) {
766      S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
767      writeFill(*S, CBA);
768      LocationCounter += S->Size;
769      continue;
770    }
771
772    if (ELFYAML::SectionHeaderTable *S =
773            dyn_cast<ELFYAML::SectionHeaderTable>(D.get())) {
774      if (S->NoHeaders.value_or(false))
775        continue;
776
777      if (!S->Offset)
778        S->Offset = alignToOffset(CBA, sizeof(typename ELFT::uint),
779                                  /*Offset=*/std::nullopt);
780      else
781        S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
782
783      uint64_t Size = S->getNumHeaders(SHeaders.size()) * sizeof(Elf_Shdr);
784      // The full section header information might be not available here, so
785      // fill the space with zeroes as a placeholder.
786      CBA.writeZeros(Size);
787      LocationCounter += Size;
788      continue;
789    }
790
791    ELFYAML::Section *Sec = cast<ELFYAML::Section>(D.get());
792    bool IsFirstUndefSection = Sec == Doc.getSections().front();
793    if (IsFirstUndefSection && Sec->IsImplicit)
794      continue;
795
796    Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)];
797    if (Sec->Link) {
798      SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
799    } else {
800      StringRef LinkSec = getDefaultLinkSec(Sec->Type);
801      unsigned Link = 0;
802      if (!LinkSec.empty() && !ExcludedSectionHeaders.count(LinkSec) &&
803          SN2I.lookup(LinkSec, Link))
804        SHeader.sh_link = Link;
805    }
806
807    if (Sec->EntSize)
808      SHeader.sh_entsize = *Sec->EntSize;
809    else
810      SHeader.sh_entsize = ELFYAML::getDefaultShEntSize<ELFT>(
811          Doc.Header.Machine.value_or(ELF::EM_NONE), Sec->Type, Sec->Name);
812
813    // We have a few sections like string or symbol tables that are usually
814    // added implicitly to the end. However, if they are explicitly specified
815    // in the YAML, we need to write them here. This ensures the file offset
816    // remains correct.
817    if (initImplicitHeader(CBA, SHeader, Sec->Name,
818                           Sec->IsImplicit ? nullptr : Sec))
819      continue;
820
821    assert(Sec && "It can't be null unless it is an implicit section. But all "
822                  "implicit sections should already have been handled above.");
823
824    SHeader.sh_name =
825        getSectionNameOffset(ELFYAML::dropUniqueSuffix(Sec->Name));
826    SHeader.sh_type = Sec->Type;
827    if (Sec->Flags)
828      SHeader.sh_flags = *Sec->Flags;
829    SHeader.sh_addralign = Sec->AddressAlign;
830
831    // Set the offset for all sections, except the SHN_UNDEF section with index
832    // 0 when not explicitly requested.
833    if (!IsFirstUndefSection || Sec->Offset)
834      SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->Offset);
835
836    assignSectionAddress(SHeader, Sec);
837
838    if (IsFirstUndefSection) {
839      if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
840        // We do not write any content for special SHN_UNDEF section.
841        if (RawSec->Size)
842          SHeader.sh_size = *RawSec->Size;
843        if (RawSec->Info)
844          SHeader.sh_info = *RawSec->Info;
845      }
846
847      LocationCounter += SHeader.sh_size;
848      overrideFields<ELFT>(Sec, SHeader);
849      continue;
850    }
851
852    if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->Content || Sec->Size))
853      SHeader.sh_size = writeContent(CBA, Sec->Content, Sec->Size);
854
855    if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
856      writeSectionContent(SHeader, *S, CBA);
857    } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
858      writeSectionContent(SHeader, *S, CBA);
859    } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
860      writeSectionContent(SHeader, *S, CBA);
861    } else if (auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
862      writeSectionContent(SHeader, *S, CBA);
863    } else if (auto S = dyn_cast<ELFYAML::GroupSection>(Sec)) {
864      writeSectionContent(SHeader, *S, CBA);
865    } else if (auto S = dyn_cast<ELFYAML::ARMIndexTableSection>(Sec)) {
866      writeSectionContent(SHeader, *S, CBA);
867    } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
868      writeSectionContent(SHeader, *S, CBA);
869    } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
870      writeSectionContent(SHeader, *S, CBA);
871    } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
872      writeSectionContent(SHeader, *S, CBA);
873    } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
874      writeSectionContent(SHeader, *S, CBA);
875    } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
876      writeSectionContent(SHeader, *S, CBA);
877    } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
878      writeSectionContent(SHeader, *S, CBA);
879    } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
880      writeSectionContent(SHeader, *S, CBA);
881    } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
882      writeSectionContent(SHeader, *S, CBA);
883    } else if (auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
884      writeSectionContent(SHeader, *S, CBA);
885    } else if (auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
886      writeSectionContent(SHeader, *S, CBA);
887    } else if (auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
888      writeSectionContent(SHeader, *S, CBA);
889    } else if (auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
890      writeSectionContent(SHeader, *S, CBA);
891    } else if (auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
892      writeSectionContent(SHeader, *S, CBA);
893    } else if (auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
894      writeSectionContent(SHeader, *S, CBA);
895    } else if (auto S = dyn_cast<ELFYAML::BBAddrMapSection>(Sec)) {
896      writeSectionContent(SHeader, *S, CBA);
897    } else {
898      llvm_unreachable("Unknown section type");
899    }
900
901    LocationCounter += SHeader.sh_size;
902
903    // Override section fields if requested.
904    overrideFields<ELFT>(Sec, SHeader);
905  }
906}
907
908template <class ELFT>
909void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
910                                          ELFYAML::Section *YAMLSec) {
911  if (YAMLSec && YAMLSec->Address) {
912    SHeader.sh_addr = *YAMLSec->Address;
913    LocationCounter = *YAMLSec->Address;
914    return;
915  }
916
917  // sh_addr represents the address in the memory image of a process. Sections
918  // in a relocatable object file or non-allocatable sections do not need
919  // sh_addr assignment.
920  if (Doc.Header.Type.value == ELF::ET_REL ||
921      !(SHeader.sh_flags & ELF::SHF_ALLOC))
922    return;
923
924  LocationCounter =
925      alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
926  SHeader.sh_addr = LocationCounter;
927}
928
929static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) {
930  for (size_t I = 0; I < Symbols.size(); ++I)
931    if (Symbols[I].Binding.value != ELF::STB_LOCAL)
932      return I;
933  return Symbols.size();
934}
935
936template <class ELFT>
937std::vector<typename ELFT::Sym>
938ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
939                             const StringTableBuilder &Strtab) {
940  std::vector<Elf_Sym> Ret;
941  Ret.resize(Symbols.size() + 1);
942
943  size_t I = 0;
944  for (const ELFYAML::Symbol &Sym : Symbols) {
945    Elf_Sym &Symbol = Ret[++I];
946
947    // If NameIndex, which contains the name offset, is explicitly specified, we
948    // use it. This is useful for preparing broken objects. Otherwise, we add
949    // the specified Name to the string table builder to get its offset.
950    if (Sym.StName)
951      Symbol.st_name = *Sym.StName;
952    else if (!Sym.Name.empty())
953      Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
954
955    Symbol.setBindingAndType(Sym.Binding, Sym.Type);
956    if (Sym.Section)
957      Symbol.st_shndx = toSectionIndex(*Sym.Section, "", Sym.Name);
958    else if (Sym.Index)
959      Symbol.st_shndx = *Sym.Index;
960
961    Symbol.st_value = Sym.Value.value_or(yaml::Hex64(0));
962    Symbol.st_other = Sym.Other ? *Sym.Other : 0;
963    Symbol.st_size = Sym.Size.value_or(yaml::Hex64(0));
964  }
965
966  return Ret;
967}
968
969template <class ELFT>
970void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
971                                             SymtabType STType,
972                                             ContiguousBlobAccumulator &CBA,
973                                             ELFYAML::Section *YAMLSec) {
974
975  bool IsStatic = STType == SymtabType::Static;
976  ArrayRef<ELFYAML::Symbol> Symbols;
977  if (IsStatic && Doc.Symbols)
978    Symbols = *Doc.Symbols;
979  else if (!IsStatic && Doc.DynamicSymbols)
980    Symbols = *Doc.DynamicSymbols;
981
982  ELFYAML::RawContentSection *RawSec =
983      dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
984  if (RawSec && (RawSec->Content || RawSec->Size)) {
985    bool HasSymbolsDescription =
986        (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
987    if (HasSymbolsDescription) {
988      StringRef Property = (IsStatic ? "`Symbols`" : "`DynamicSymbols`");
989      if (RawSec->Content)
990        reportError("cannot specify both `Content` and " + Property +
991                    " for symbol table section '" + RawSec->Name + "'");
992      if (RawSec->Size)
993        reportError("cannot specify both `Size` and " + Property +
994                    " for symbol table section '" + RawSec->Name + "'");
995      return;
996    }
997  }
998
999  SHeader.sh_name = getSectionNameOffset(IsStatic ? ".symtab" : ".dynsym");
1000
1001  if (YAMLSec)
1002    SHeader.sh_type = YAMLSec->Type;
1003  else
1004    SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
1005
1006  if (YAMLSec && YAMLSec->Flags)
1007    SHeader.sh_flags = *YAMLSec->Flags;
1008  else if (!IsStatic)
1009    SHeader.sh_flags = ELF::SHF_ALLOC;
1010
1011  // If the symbol table section is explicitly described in the YAML
1012  // then we should set the fields requested.
1013  SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info)
1014                                             : findFirstNonGlobal(Symbols) + 1;
1015  SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8;
1016
1017  assignSectionAddress(SHeader, YAMLSec);
1018
1019  SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1020                                    RawSec ? RawSec->Offset : std::nullopt);
1021
1022  if (RawSec && (RawSec->Content || RawSec->Size)) {
1023    assert(Symbols.empty());
1024    SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1025    return;
1026  }
1027
1028  std::vector<Elf_Sym> Syms =
1029      toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
1030  SHeader.sh_size = Syms.size() * sizeof(Elf_Sym);
1031  CBA.write((const char *)Syms.data(), SHeader.sh_size);
1032}
1033
1034template <class ELFT>
1035void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1036                                             StringTableBuilder &STB,
1037                                             ContiguousBlobAccumulator &CBA,
1038                                             ELFYAML::Section *YAMLSec) {
1039  SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
1040  SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
1041  SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
1042
1043  ELFYAML::RawContentSection *RawSec =
1044      dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1045
1046  SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1047                                    YAMLSec ? YAMLSec->Offset : std::nullopt);
1048
1049  if (RawSec && (RawSec->Content || RawSec->Size)) {
1050    SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1051  } else {
1052    if (raw_ostream *OS = CBA.getRawOS(STB.getSize()))
1053      STB.write(*OS);
1054    SHeader.sh_size = STB.getSize();
1055  }
1056
1057  if (RawSec && RawSec->Info)
1058    SHeader.sh_info = *RawSec->Info;
1059
1060  if (YAMLSec && YAMLSec->Flags)
1061    SHeader.sh_flags = *YAMLSec->Flags;
1062  else if (Name == ".dynstr")
1063    SHeader.sh_flags = ELF::SHF_ALLOC;
1064
1065  assignSectionAddress(SHeader, YAMLSec);
1066}
1067
1068static bool shouldEmitDWARF(DWARFYAML::Data &DWARF, StringRef Name) {
1069  SetVector<StringRef> DebugSecNames = DWARF.getNonEmptySectionNames();
1070  return Name.consume_front(".") && DebugSecNames.count(Name);
1071}
1072
1073template <class ELFT>
1074Expected<uint64_t> emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name,
1075                             const DWARFYAML::Data &DWARF,
1076                             ContiguousBlobAccumulator &CBA) {
1077  // We are unable to predict the size of debug data, so we request to write 0
1078  // bytes. This should always return us an output stream unless CBA is already
1079  // in an error state.
1080  raw_ostream *OS = CBA.getRawOS(0);
1081  if (!OS)
1082    return 0;
1083
1084  uint64_t BeginOffset = CBA.tell();
1085
1086  auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Name.substr(1));
1087  if (Error Err = EmitFunc(*OS, DWARF))
1088    return std::move(Err);
1089
1090  return CBA.tell() - BeginOffset;
1091}
1092
1093template <class ELFT>
1094void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1095                                            ContiguousBlobAccumulator &CBA,
1096                                            ELFYAML::Section *YAMLSec) {
1097  SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
1098  SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_PROGBITS;
1099  SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
1100  SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1101                                    YAMLSec ? YAMLSec->Offset : std::nullopt);
1102
1103  ELFYAML::RawContentSection *RawSec =
1104      dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1105  if (Doc.DWARF && shouldEmitDWARF(*Doc.DWARF, Name)) {
1106    if (RawSec && (RawSec->Content || RawSec->Size))
1107      reportError("cannot specify section '" + Name +
1108                  "' contents in the 'DWARF' entry and the 'Content' "
1109                  "or 'Size' in the 'Sections' entry at the same time");
1110    else {
1111      if (Expected<uint64_t> ShSizeOrErr =
1112              emitDWARF<ELFT>(SHeader, Name, *Doc.DWARF, CBA))
1113        SHeader.sh_size = *ShSizeOrErr;
1114      else
1115        reportError(ShSizeOrErr.takeError());
1116    }
1117  } else if (RawSec)
1118    SHeader.sh_size = writeContent(CBA, RawSec->Content, RawSec->Size);
1119  else
1120    llvm_unreachable("debug sections can only be initialized via the 'DWARF' "
1121                     "entry or a RawContentSection");
1122
1123  if (RawSec && RawSec->Info)
1124    SHeader.sh_info = *RawSec->Info;
1125
1126  if (YAMLSec && YAMLSec->Flags)
1127    SHeader.sh_flags = *YAMLSec->Flags;
1128  else if (Name == ".debug_str")
1129    SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
1130
1131  assignSectionAddress(SHeader, YAMLSec);
1132}
1133
1134template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) {
1135  ErrHandler(Msg);
1136  HasError = true;
1137}
1138
1139template <class ELFT> void ELFState<ELFT>::reportError(Error Err) {
1140  handleAllErrors(std::move(Err), [&](const ErrorInfoBase &Err) {
1141    reportError(Err.message());
1142  });
1143}
1144
1145template <class ELFT>
1146std::vector<Fragment>
1147ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
1148                                 ArrayRef<Elf_Shdr> SHeaders) {
1149  std::vector<Fragment> Ret;
1150  for (const ELFYAML::Chunk *C : Phdr.Chunks) {
1151    if (const ELFYAML::Fill *F = dyn_cast<ELFYAML::Fill>(C)) {
1152      Ret.push_back({*F->Offset, F->Size, llvm::ELF::SHT_PROGBITS,
1153                     /*ShAddrAlign=*/1});
1154      continue;
1155    }
1156
1157    const ELFYAML::Section *S = cast<ELFYAML::Section>(C);
1158    const Elf_Shdr &H = SHeaders[SN2I.get(S->Name)];
1159    Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign});
1160  }
1161  return Ret;
1162}
1163
1164template <class ELFT>
1165void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
1166                                            std::vector<Elf_Shdr> &SHeaders) {
1167  uint32_t PhdrIdx = 0;
1168  for (auto &YamlPhdr : Doc.ProgramHeaders) {
1169    Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
1170    std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
1171    if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) {
1172          return A.Offset < B.Offset;
1173        }))
1174      reportError("sections in the program header with index " +
1175                  Twine(PhdrIdx) + " are not sorted by their file offset");
1176
1177    if (YamlPhdr.Offset) {
1178      if (!Fragments.empty() && *YamlPhdr.Offset > Fragments.front().Offset)
1179        reportError("'Offset' for segment with index " + Twine(PhdrIdx) +
1180                    " must be less than or equal to the minimum file offset of "
1181                    "all included sections (0x" +
1182                    Twine::utohexstr(Fragments.front().Offset) + ")");
1183      PHeader.p_offset = *YamlPhdr.Offset;
1184    } else if (!Fragments.empty()) {
1185      PHeader.p_offset = Fragments.front().Offset;
1186    }
1187
1188    // Set the file size if not set explicitly.
1189    if (YamlPhdr.FileSize) {
1190      PHeader.p_filesz = *YamlPhdr.FileSize;
1191    } else if (!Fragments.empty()) {
1192      uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
1193      // SHT_NOBITS sections occupy no physical space in a file, we should not
1194      // take their sizes into account when calculating the file size of a
1195      // segment.
1196      if (Fragments.back().Type != llvm::ELF::SHT_NOBITS)
1197        FileSize += Fragments.back().Size;
1198      PHeader.p_filesz = FileSize;
1199    }
1200
1201    // Find the maximum offset of the end of a section in order to set p_memsz.
1202    uint64_t MemOffset = PHeader.p_offset;
1203    for (const Fragment &F : Fragments)
1204      MemOffset = std::max(MemOffset, F.Offset + F.Size);
1205    // Set the memory size if not set explicitly.
1206    PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
1207                                       : MemOffset - PHeader.p_offset;
1208
1209    if (YamlPhdr.Align) {
1210      PHeader.p_align = *YamlPhdr.Align;
1211    } else {
1212      // Set the alignment of the segment to be the maximum alignment of the
1213      // sections so that by default the segment has a valid and sensible
1214      // alignment.
1215      PHeader.p_align = 1;
1216      for (const Fragment &F : Fragments)
1217        PHeader.p_align = std::max((uint64_t)PHeader.p_align, F.AddrAlign);
1218    }
1219  }
1220}
1221
1222bool llvm::ELFYAML::shouldAllocateFileSpace(
1223    ArrayRef<ELFYAML::ProgramHeader> Phdrs, const ELFYAML::NoBitsSection &S) {
1224  for (const ELFYAML::ProgramHeader &PH : Phdrs) {
1225    auto It = llvm::find_if(
1226        PH.Chunks, [&](ELFYAML::Chunk *C) { return C->Name == S.Name; });
1227    if (std::any_of(It, PH.Chunks.end(), [](ELFYAML::Chunk *C) {
1228          return (isa<ELFYAML::Fill>(C) ||
1229                  cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
1230        }))
1231      return true;
1232  }
1233  return false;
1234}
1235
1236template <class ELFT>
1237void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1238                                         const ELFYAML::NoBitsSection &S,
1239                                         ContiguousBlobAccumulator &CBA) {
1240  if (!S.Size)
1241    return;
1242
1243  SHeader.sh_size = *S.Size;
1244
1245  // When a nobits section is followed by a non-nobits section or fill
1246  // in the same segment, we allocate the file space for it. This behavior
1247  // matches linkers.
1248  if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
1249    CBA.writeZeros(*S.Size);
1250}
1251
1252template <class ELFT>
1253void ELFState<ELFT>::writeSectionContent(
1254    Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
1255    ContiguousBlobAccumulator &CBA) {
1256  if (Section.Info)
1257    SHeader.sh_info = *Section.Info;
1258}
1259
1260static bool isMips64EL(const ELFYAML::Object &Obj) {
1261  return Obj.getMachine() == llvm::ELF::EM_MIPS &&
1262         Obj.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
1263         Obj.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1264}
1265
1266template <class ELFT>
1267void ELFState<ELFT>::writeSectionContent(
1268    Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section,
1269    ContiguousBlobAccumulator &CBA) {
1270  assert((Section.Type == llvm::ELF::SHT_REL ||
1271          Section.Type == llvm::ELF::SHT_RELA) &&
1272         "Section type is not SHT_REL nor SHT_RELA");
1273
1274  if (!Section.RelocatableSec.empty())
1275    SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
1276
1277  if (!Section.Relocations)
1278    return;
1279
1280  const bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
1281  for (const ELFYAML::Relocation &Rel : *Section.Relocations) {
1282    const bool IsDynamic = Section.Link && (*Section.Link == ".dynsym");
1283    unsigned SymIdx =
1284        Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, IsDynamic) : 0;
1285    if (IsRela) {
1286      Elf_Rela REntry;
1287      zero(REntry);
1288      REntry.r_offset = Rel.Offset;
1289      REntry.r_addend = Rel.Addend;
1290      REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
1291      CBA.write((const char *)&REntry, sizeof(REntry));
1292    } else {
1293      Elf_Rel REntry;
1294      zero(REntry);
1295      REntry.r_offset = Rel.Offset;
1296      REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
1297      CBA.write((const char *)&REntry, sizeof(REntry));
1298    }
1299  }
1300
1301  SHeader.sh_size = (IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)) *
1302                    Section.Relocations->size();
1303}
1304
1305template <class ELFT>
1306void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1307                                         const ELFYAML::RelrSection &Section,
1308                                         ContiguousBlobAccumulator &CBA) {
1309  if (!Section.Entries)
1310    return;
1311
1312  for (llvm::yaml::Hex64 E : *Section.Entries) {
1313    if (!ELFT::Is64Bits && E > UINT32_MAX)
1314      reportError(Section.Name + ": the value is too large for 32-bits: 0x" +
1315                  Twine::utohexstr(E));
1316    CBA.write<uintX_t>(E, ELFT::TargetEndianness);
1317  }
1318
1319  SHeader.sh_size = sizeof(uintX_t) * Section.Entries->size();
1320}
1321
1322template <class ELFT>
1323void ELFState<ELFT>::writeSectionContent(
1324    Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx,
1325    ContiguousBlobAccumulator &CBA) {
1326  if (Shndx.Content || Shndx.Size) {
1327    SHeader.sh_size = writeContent(CBA, Shndx.Content, Shndx.Size);
1328    return;
1329  }
1330
1331  if (!Shndx.Entries)
1332    return;
1333
1334  for (uint32_t E : *Shndx.Entries)
1335    CBA.write<uint32_t>(E, ELFT::TargetEndianness);
1336  SHeader.sh_size = Shndx.Entries->size() * SHeader.sh_entsize;
1337}
1338
1339template <class ELFT>
1340void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1341                                         const ELFYAML::GroupSection &Section,
1342                                         ContiguousBlobAccumulator &CBA) {
1343  assert(Section.Type == llvm::ELF::SHT_GROUP &&
1344         "Section type is not SHT_GROUP");
1345
1346  if (Section.Signature)
1347    SHeader.sh_info =
1348        toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
1349
1350  if (!Section.Members)
1351    return;
1352
1353  for (const ELFYAML::SectionOrType &Member : *Section.Members) {
1354    unsigned int SectionIndex = 0;
1355    if (Member.sectionNameOrType == "GRP_COMDAT")
1356      SectionIndex = llvm::ELF::GRP_COMDAT;
1357    else
1358      SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name);
1359    CBA.write<uint32_t>(SectionIndex, ELFT::TargetEndianness);
1360  }
1361  SHeader.sh_size = SHeader.sh_entsize * Section.Members->size();
1362}
1363
1364template <class ELFT>
1365void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1366                                         const ELFYAML::SymverSection &Section,
1367                                         ContiguousBlobAccumulator &CBA) {
1368  if (!Section.Entries)
1369    return;
1370
1371  for (uint16_t Version : *Section.Entries)
1372    CBA.write<uint16_t>(Version, ELFT::TargetEndianness);
1373  SHeader.sh_size = Section.Entries->size() * SHeader.sh_entsize;
1374}
1375
1376template <class ELFT>
1377void ELFState<ELFT>::writeSectionContent(
1378    Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section,
1379    ContiguousBlobAccumulator &CBA) {
1380  if (!Section.Entries)
1381    return;
1382
1383  for (const ELFYAML::StackSizeEntry &E : *Section.Entries) {
1384    CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1385    SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(E.Size);
1386  }
1387}
1388
1389template <class ELFT>
1390void ELFState<ELFT>::writeSectionContent(
1391    Elf_Shdr &SHeader, const ELFYAML::BBAddrMapSection &Section,
1392    ContiguousBlobAccumulator &CBA) {
1393  if (!Section.Entries)
1394    return;
1395
1396  for (const ELFYAML::BBAddrMapEntry &E : *Section.Entries) {
1397    // Write version and feature values.
1398    if (Section.Type == llvm::ELF::SHT_LLVM_BB_ADDR_MAP) {
1399      if (E.Version > 2)
1400        WithColor::warning() << "unsupported SHT_LLVM_BB_ADDR_MAP version: "
1401                             << static_cast<int>(E.Version)
1402                             << "; encoding using the most recent version";
1403      CBA.write(E.Version);
1404      CBA.write(E.Feature);
1405      SHeader.sh_size += 2;
1406    }
1407    // Write the address of the function.
1408    CBA.write<uintX_t>(E.Address, ELFT::TargetEndianness);
1409    // Write number of BBEntries (number of basic blocks in the function). This
1410    // is overridden by the 'NumBlocks' YAML field when specified.
1411    uint64_t NumBlocks =
1412        E.NumBlocks.value_or(E.BBEntries ? E.BBEntries->size() : 0);
1413    SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1414    // Write all BBEntries.
1415    if (!E.BBEntries)
1416      continue;
1417    for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *E.BBEntries) {
1418      if (Section.Type == llvm::ELF::SHT_LLVM_BB_ADDR_MAP && E.Version > 1)
1419        SHeader.sh_size += CBA.writeULEB128(BBE.ID);
1420      SHeader.sh_size += CBA.writeULEB128(BBE.AddressOffset) +
1421                         CBA.writeULEB128(BBE.Size) +
1422                         CBA.writeULEB128(BBE.Metadata);
1423    }
1424  }
1425}
1426
1427template <class ELFT>
1428void ELFState<ELFT>::writeSectionContent(
1429    Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section,
1430    ContiguousBlobAccumulator &CBA) {
1431  if (!Section.Options)
1432    return;
1433
1434  for (const ELFYAML::LinkerOption &LO : *Section.Options) {
1435    CBA.write(LO.Key.data(), LO.Key.size());
1436    CBA.write('\0');
1437    CBA.write(LO.Value.data(), LO.Value.size());
1438    CBA.write('\0');
1439    SHeader.sh_size += (LO.Key.size() + LO.Value.size() + 2);
1440  }
1441}
1442
1443template <class ELFT>
1444void ELFState<ELFT>::writeSectionContent(
1445    Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section,
1446    ContiguousBlobAccumulator &CBA) {
1447  if (!Section.Libs)
1448    return;
1449
1450  for (StringRef Lib : *Section.Libs) {
1451    CBA.write(Lib.data(), Lib.size());
1452    CBA.write('\0');
1453    SHeader.sh_size += Lib.size() + 1;
1454  }
1455}
1456
1457template <class ELFT>
1458uint64_t
1459ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align,
1460                              std::optional<llvm::yaml::Hex64> Offset) {
1461  uint64_t CurrentOffset = CBA.getOffset();
1462  uint64_t AlignedOffset;
1463
1464  if (Offset) {
1465    if ((uint64_t)*Offset < CurrentOffset) {
1466      reportError("the 'Offset' value (0x" +
1467                  Twine::utohexstr((uint64_t)*Offset) + ") goes backward");
1468      return CurrentOffset;
1469    }
1470
1471    // We ignore an alignment when an explicit offset has been requested.
1472    AlignedOffset = *Offset;
1473  } else {
1474    AlignedOffset = alignTo(CurrentOffset, std::max(Align, (uint64_t)1));
1475  }
1476
1477  CBA.writeZeros(AlignedOffset - CurrentOffset);
1478  return AlignedOffset;
1479}
1480
1481template <class ELFT>
1482void ELFState<ELFT>::writeSectionContent(
1483    Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
1484    ContiguousBlobAccumulator &CBA) {
1485  if (!Section.Entries)
1486    return;
1487
1488  for (const ELFYAML::CallGraphEntryWeight &E : *Section.Entries) {
1489    CBA.write<uint64_t>(E.Weight, ELFT::TargetEndianness);
1490    SHeader.sh_size += sizeof(object::Elf_CGProfile_Impl<ELFT>);
1491  }
1492}
1493
1494template <class ELFT>
1495void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1496                                         const ELFYAML::HashSection &Section,
1497                                         ContiguousBlobAccumulator &CBA) {
1498  if (!Section.Bucket)
1499    return;
1500
1501  CBA.write<uint32_t>(
1502      Section.NBucket.value_or(llvm::yaml::Hex64(Section.Bucket->size())),
1503      ELFT::TargetEndianness);
1504  CBA.write<uint32_t>(
1505      Section.NChain.value_or(llvm::yaml::Hex64(Section.Chain->size())),
1506      ELFT::TargetEndianness);
1507
1508  for (uint32_t Val : *Section.Bucket)
1509    CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1510  for (uint32_t Val : *Section.Chain)
1511    CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1512
1513  SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4;
1514}
1515
1516template <class ELFT>
1517void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1518                                         const ELFYAML::VerdefSection &Section,
1519                                         ContiguousBlobAccumulator &CBA) {
1520
1521  if (Section.Info)
1522    SHeader.sh_info = *Section.Info;
1523  else if (Section.Entries)
1524    SHeader.sh_info = Section.Entries->size();
1525
1526  if (!Section.Entries)
1527    return;
1528
1529  uint64_t AuxCnt = 0;
1530  for (size_t I = 0; I < Section.Entries->size(); ++I) {
1531    const ELFYAML::VerdefEntry &E = (*Section.Entries)[I];
1532
1533    Elf_Verdef VerDef;
1534    VerDef.vd_version = E.Version.value_or(1);
1535    VerDef.vd_flags = E.Flags.value_or(0);
1536    VerDef.vd_ndx = E.VersionNdx.value_or(0);
1537    VerDef.vd_hash = E.Hash.value_or(0);
1538    VerDef.vd_aux = sizeof(Elf_Verdef);
1539    VerDef.vd_cnt = E.VerNames.size();
1540    if (I == Section.Entries->size() - 1)
1541      VerDef.vd_next = 0;
1542    else
1543      VerDef.vd_next =
1544          sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux);
1545    CBA.write((const char *)&VerDef, sizeof(Elf_Verdef));
1546
1547    for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) {
1548      Elf_Verdaux VernAux;
1549      VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]);
1550      if (J == E.VerNames.size() - 1)
1551        VernAux.vda_next = 0;
1552      else
1553        VernAux.vda_next = sizeof(Elf_Verdaux);
1554      CBA.write((const char *)&VernAux, sizeof(Elf_Verdaux));
1555    }
1556  }
1557
1558  SHeader.sh_size = Section.Entries->size() * sizeof(Elf_Verdef) +
1559                    AuxCnt * sizeof(Elf_Verdaux);
1560}
1561
1562template <class ELFT>
1563void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1564                                         const ELFYAML::VerneedSection &Section,
1565                                         ContiguousBlobAccumulator &CBA) {
1566  if (Section.Info)
1567    SHeader.sh_info = *Section.Info;
1568  else if (Section.VerneedV)
1569    SHeader.sh_info = Section.VerneedV->size();
1570
1571  if (!Section.VerneedV)
1572    return;
1573
1574  uint64_t AuxCnt = 0;
1575  for (size_t I = 0; I < Section.VerneedV->size(); ++I) {
1576    const ELFYAML::VerneedEntry &VE = (*Section.VerneedV)[I];
1577
1578    Elf_Verneed VerNeed;
1579    VerNeed.vn_version = VE.Version;
1580    VerNeed.vn_file = DotDynstr.getOffset(VE.File);
1581    if (I == Section.VerneedV->size() - 1)
1582      VerNeed.vn_next = 0;
1583    else
1584      VerNeed.vn_next =
1585          sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux);
1586    VerNeed.vn_cnt = VE.AuxV.size();
1587    VerNeed.vn_aux = sizeof(Elf_Verneed);
1588    CBA.write((const char *)&VerNeed, sizeof(Elf_Verneed));
1589
1590    for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) {
1591      const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J];
1592
1593      Elf_Vernaux VernAux;
1594      VernAux.vna_hash = VAuxE.Hash;
1595      VernAux.vna_flags = VAuxE.Flags;
1596      VernAux.vna_other = VAuxE.Other;
1597      VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name);
1598      if (J == VE.AuxV.size() - 1)
1599        VernAux.vna_next = 0;
1600      else
1601        VernAux.vna_next = sizeof(Elf_Vernaux);
1602      CBA.write((const char *)&VernAux, sizeof(Elf_Vernaux));
1603    }
1604  }
1605
1606  SHeader.sh_size = Section.VerneedV->size() * sizeof(Elf_Verneed) +
1607                    AuxCnt * sizeof(Elf_Vernaux);
1608}
1609
1610template <class ELFT>
1611void ELFState<ELFT>::writeSectionContent(
1612    Elf_Shdr &SHeader, const ELFYAML::ARMIndexTableSection &Section,
1613    ContiguousBlobAccumulator &CBA) {
1614  if (!Section.Entries)
1615    return;
1616
1617  for (const ELFYAML::ARMIndexTableEntry &E : *Section.Entries) {
1618    CBA.write<uint32_t>(E.Offset, ELFT::TargetEndianness);
1619    CBA.write<uint32_t>(E.Value, ELFT::TargetEndianness);
1620  }
1621  SHeader.sh_size = Section.Entries->size() * 8;
1622}
1623
1624template <class ELFT>
1625void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1626                                         const ELFYAML::MipsABIFlags &Section,
1627                                         ContiguousBlobAccumulator &CBA) {
1628  assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
1629         "Section type is not SHT_MIPS_ABIFLAGS");
1630
1631  object::Elf_Mips_ABIFlags<ELFT> Flags;
1632  zero(Flags);
1633  SHeader.sh_size = SHeader.sh_entsize;
1634
1635  Flags.version = Section.Version;
1636  Flags.isa_level = Section.ISALevel;
1637  Flags.isa_rev = Section.ISARevision;
1638  Flags.gpr_size = Section.GPRSize;
1639  Flags.cpr1_size = Section.CPR1Size;
1640  Flags.cpr2_size = Section.CPR2Size;
1641  Flags.fp_abi = Section.FpABI;
1642  Flags.isa_ext = Section.ISAExtension;
1643  Flags.ases = Section.ASEs;
1644  Flags.flags1 = Section.Flags1;
1645  Flags.flags2 = Section.Flags2;
1646  CBA.write((const char *)&Flags, sizeof(Flags));
1647}
1648
1649template <class ELFT>
1650void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1651                                         const ELFYAML::DynamicSection &Section,
1652                                         ContiguousBlobAccumulator &CBA) {
1653  assert(Section.Type == llvm::ELF::SHT_DYNAMIC &&
1654         "Section type is not SHT_DYNAMIC");
1655
1656  if (!Section.Entries)
1657    return;
1658
1659  for (const ELFYAML::DynamicEntry &DE : *Section.Entries) {
1660    CBA.write<uintX_t>(DE.Tag, ELFT::TargetEndianness);
1661    CBA.write<uintX_t>(DE.Val, ELFT::TargetEndianness);
1662  }
1663  SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries->size();
1664}
1665
1666template <class ELFT>
1667void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1668                                         const ELFYAML::AddrsigSection &Section,
1669                                         ContiguousBlobAccumulator &CBA) {
1670  if (!Section.Symbols)
1671    return;
1672
1673  for (StringRef Sym : *Section.Symbols)
1674    SHeader.sh_size +=
1675        CBA.writeULEB128(toSymbolIndex(Sym, Section.Name, /*IsDynamic=*/false));
1676}
1677
1678template <class ELFT>
1679void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1680                                         const ELFYAML::NoteSection &Section,
1681                                         ContiguousBlobAccumulator &CBA) {
1682  if (!Section.Notes)
1683    return;
1684
1685  uint64_t Offset = CBA.tell();
1686  for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
1687    // Write name size.
1688    if (NE.Name.empty())
1689      CBA.write<uint32_t>(0, ELFT::TargetEndianness);
1690    else
1691      CBA.write<uint32_t>(NE.Name.size() + 1, ELFT::TargetEndianness);
1692
1693    // Write description size.
1694    if (NE.Desc.binary_size() == 0)
1695      CBA.write<uint32_t>(0, ELFT::TargetEndianness);
1696    else
1697      CBA.write<uint32_t>(NE.Desc.binary_size(), ELFT::TargetEndianness);
1698
1699    // Write type.
1700    CBA.write<uint32_t>(NE.Type, ELFT::TargetEndianness);
1701
1702    // Write name, null terminator and padding.
1703    if (!NE.Name.empty()) {
1704      CBA.write(NE.Name.data(), NE.Name.size());
1705      CBA.write('\0');
1706      CBA.padToAlignment(4);
1707    }
1708
1709    // Write description and padding.
1710    if (NE.Desc.binary_size() != 0) {
1711      CBA.writeAsBinary(NE.Desc);
1712      CBA.padToAlignment(4);
1713    }
1714  }
1715
1716  SHeader.sh_size = CBA.tell() - Offset;
1717}
1718
1719template <class ELFT>
1720void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1721                                         const ELFYAML::GnuHashSection &Section,
1722                                         ContiguousBlobAccumulator &CBA) {
1723  if (!Section.HashBuckets)
1724    return;
1725
1726  if (!Section.Header)
1727    return;
1728
1729  // We write the header first, starting with the hash buckets count. Normally
1730  // it is the number of entries in HashBuckets, but the "NBuckets" property can
1731  // be used to override this field, which is useful for producing broken
1732  // objects.
1733  if (Section.Header->NBuckets)
1734    CBA.write<uint32_t>(*Section.Header->NBuckets, ELFT::TargetEndianness);
1735  else
1736    CBA.write<uint32_t>(Section.HashBuckets->size(), ELFT::TargetEndianness);
1737
1738  // Write the index of the first symbol in the dynamic symbol table accessible
1739  // via the hash table.
1740  CBA.write<uint32_t>(Section.Header->SymNdx, ELFT::TargetEndianness);
1741
1742  // Write the number of words in the Bloom filter. As above, the "MaskWords"
1743  // property can be used to set this field to any value.
1744  if (Section.Header->MaskWords)
1745    CBA.write<uint32_t>(*Section.Header->MaskWords, ELFT::TargetEndianness);
1746  else
1747    CBA.write<uint32_t>(Section.BloomFilter->size(), ELFT::TargetEndianness);
1748
1749  // Write the shift constant used by the Bloom filter.
1750  CBA.write<uint32_t>(Section.Header->Shift2, ELFT::TargetEndianness);
1751
1752  // We've finished writing the header. Now write the Bloom filter.
1753  for (llvm::yaml::Hex64 Val : *Section.BloomFilter)
1754    CBA.write<uintX_t>(Val, ELFT::TargetEndianness);
1755
1756  // Write an array of hash buckets.
1757  for (llvm::yaml::Hex32 Val : *Section.HashBuckets)
1758    CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1759
1760  // Write an array of hash values.
1761  for (llvm::yaml::Hex32 Val : *Section.HashValues)
1762    CBA.write<uint32_t>(Val, ELFT::TargetEndianness);
1763
1764  SHeader.sh_size = 16 /*Header size*/ +
1765                    Section.BloomFilter->size() * sizeof(typename ELFT::uint) +
1766                    Section.HashBuckets->size() * 4 +
1767                    Section.HashValues->size() * 4;
1768}
1769
1770template <class ELFT>
1771void ELFState<ELFT>::writeFill(ELFYAML::Fill &Fill,
1772                               ContiguousBlobAccumulator &CBA) {
1773  size_t PatternSize = Fill.Pattern ? Fill.Pattern->binary_size() : 0;
1774  if (!PatternSize) {
1775    CBA.writeZeros(Fill.Size);
1776    return;
1777  }
1778
1779  // Fill the content with the specified pattern.
1780  uint64_t Written = 0;
1781  for (; Written + PatternSize <= Fill.Size; Written += PatternSize)
1782    CBA.writeAsBinary(*Fill.Pattern);
1783  CBA.writeAsBinary(*Fill.Pattern, Fill.Size - Written);
1784}
1785
1786template <class ELFT>
1787DenseMap<StringRef, size_t> ELFState<ELFT>::buildSectionHeaderReorderMap() {
1788  const ELFYAML::SectionHeaderTable &SectionHeaders =
1789      Doc.getSectionHeaderTable();
1790  if (SectionHeaders.IsImplicit || SectionHeaders.NoHeaders ||
1791      SectionHeaders.isDefault())
1792    return DenseMap<StringRef, size_t>();
1793
1794  DenseMap<StringRef, size_t> Ret;
1795  size_t SecNdx = 0;
1796  StringSet<> Seen;
1797
1798  auto AddSection = [&](const ELFYAML::SectionHeader &Hdr) {
1799    if (!Ret.try_emplace(Hdr.Name, ++SecNdx).second)
1800      reportError("repeated section name: '" + Hdr.Name +
1801                  "' in the section header description");
1802    Seen.insert(Hdr.Name);
1803  };
1804
1805  if (SectionHeaders.Sections)
1806    for (const ELFYAML::SectionHeader &Hdr : *SectionHeaders.Sections)
1807      AddSection(Hdr);
1808
1809  if (SectionHeaders.Excluded)
1810    for (const ELFYAML::SectionHeader &Hdr : *SectionHeaders.Excluded)
1811      AddSection(Hdr);
1812
1813  for (const ELFYAML::Section *S : Doc.getSections()) {
1814    // Ignore special first SHT_NULL section.
1815    if (S == Doc.getSections().front())
1816      continue;
1817    if (!Seen.count(S->Name))
1818      reportError("section '" + S->Name +
1819                  "' should be present in the 'Sections' or 'Excluded' lists");
1820    Seen.erase(S->Name);
1821  }
1822
1823  for (const auto &It : Seen)
1824    reportError("section header contains undefined section '" + It.getKey() +
1825                "'");
1826  return Ret;
1827}
1828
1829template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
1830  // A YAML description can have an explicit section header declaration that
1831  // allows to change the order of section headers.
1832  DenseMap<StringRef, size_t> ReorderMap = buildSectionHeaderReorderMap();
1833
1834  if (HasError)
1835    return;
1836
1837  // Build excluded section headers map.
1838  std::vector<ELFYAML::Section *> Sections = Doc.getSections();
1839  const ELFYAML::SectionHeaderTable &SectionHeaders =
1840      Doc.getSectionHeaderTable();
1841  if (SectionHeaders.Excluded)
1842    for (const ELFYAML::SectionHeader &Hdr : *SectionHeaders.Excluded)
1843      if (!ExcludedSectionHeaders.insert(Hdr.Name).second)
1844        llvm_unreachable("buildSectionIndex() failed");
1845
1846  if (SectionHeaders.NoHeaders.value_or(false))
1847    for (const ELFYAML::Section *S : Sections)
1848      if (!ExcludedSectionHeaders.insert(S->Name).second)
1849        llvm_unreachable("buildSectionIndex() failed");
1850
1851  size_t SecNdx = -1;
1852  for (const ELFYAML::Section *S : Sections) {
1853    ++SecNdx;
1854
1855    size_t Index = ReorderMap.empty() ? SecNdx : ReorderMap.lookup(S->Name);
1856    if (!SN2I.addName(S->Name, Index))
1857      llvm_unreachable("buildSectionIndex() failed");
1858
1859    if (!ExcludedSectionHeaders.count(S->Name))
1860      ShStrtabStrings->add(ELFYAML::dropUniqueSuffix(S->Name));
1861  }
1862}
1863
1864template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
1865  auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) {
1866    for (size_t I = 0, S = V.size(); I < S; ++I) {
1867      const ELFYAML::Symbol &Sym = V[I];
1868      if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1))
1869        reportError("repeated symbol name: '" + Sym.Name + "'");
1870    }
1871  };
1872
1873  if (Doc.Symbols)
1874    Build(*Doc.Symbols, SymN2I);
1875  if (Doc.DynamicSymbols)
1876    Build(*Doc.DynamicSymbols, DynSymN2I);
1877}
1878
1879template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
1880  // Add the regular symbol names to .strtab section.
1881  if (Doc.Symbols)
1882    for (const ELFYAML::Symbol &Sym : *Doc.Symbols)
1883      DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1884  DotStrtab.finalize();
1885
1886  // Add the dynamic symbol names to .dynstr section.
1887  if (Doc.DynamicSymbols)
1888    for (const ELFYAML::Symbol &Sym : *Doc.DynamicSymbols)
1889      DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name));
1890
1891  // SHT_GNU_verdef and SHT_GNU_verneed sections might also
1892  // add strings to .dynstr section.
1893  for (const ELFYAML::Chunk *Sec : Doc.getSections()) {
1894    if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
1895      if (VerNeed->VerneedV) {
1896        for (const ELFYAML::VerneedEntry &VE : *VerNeed->VerneedV) {
1897          DotDynstr.add(VE.File);
1898          for (const ELFYAML::VernauxEntry &Aux : VE.AuxV)
1899            DotDynstr.add(Aux.Name);
1900        }
1901      }
1902    } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
1903      if (VerDef->Entries)
1904        for (const ELFYAML::VerdefEntry &E : *VerDef->Entries)
1905          for (StringRef Name : E.VerNames)
1906            DotDynstr.add(Name);
1907    }
1908  }
1909
1910  DotDynstr.finalize();
1911
1912  // Don't finalize the section header string table a second time if it has
1913  // already been finalized due to being one of the symbol string tables.
1914  if (ShStrtabStrings != &DotStrtab && ShStrtabStrings != &DotDynstr)
1915    ShStrtabStrings->finalize();
1916}
1917
1918template <class ELFT>
1919bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
1920                              yaml::ErrorHandler EH, uint64_t MaxSize) {
1921  ELFState<ELFT> State(Doc, EH);
1922  if (State.HasError)
1923    return false;
1924
1925  // Build the section index, which adds sections to the section header string
1926  // table first, so that we can finalize the section header string table.
1927  State.buildSectionIndex();
1928  State.buildSymbolIndexes();
1929
1930  // Finalize section header string table and the .strtab and .dynstr sections.
1931  // We do this early because we want to finalize the string table builders
1932  // before writing the content of the sections that might want to use them.
1933  State.finalizeStrings();
1934
1935  if (State.HasError)
1936    return false;
1937
1938  std::vector<Elf_Phdr> PHeaders;
1939  State.initProgramHeaders(PHeaders);
1940
1941  // XXX: This offset is tightly coupled with the order that we write
1942  // things to `OS`.
1943  const size_t SectionContentBeginOffset =
1944      sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
1945  // It is quite easy to accidentally create output with yaml2obj that is larger
1946  // than intended, for example, due to an issue in the YAML description.
1947  // We limit the maximum allowed output size, but also provide a command line
1948  // option to change this limitation.
1949  ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
1950
1951  std::vector<Elf_Shdr> SHeaders;
1952  State.initSectionHeaders(SHeaders, CBA);
1953
1954  // Now we can decide segment offsets.
1955  State.setProgramHeaderLayout(PHeaders, SHeaders);
1956
1957  bool ReachedLimit = CBA.getOffset() > MaxSize;
1958  if (Error E = CBA.takeLimitError()) {
1959    // We report a custom error message instead below.
1960    consumeError(std::move(E));
1961    ReachedLimit = true;
1962  }
1963
1964  if (ReachedLimit)
1965    State.reportError(
1966        "the desired output size is greater than permitted. Use the "
1967        "--max-size option to change the limit");
1968
1969  if (State.HasError)
1970    return false;
1971
1972  State.writeELFHeader(OS);
1973  writeArrayData(OS, ArrayRef(PHeaders));
1974
1975  const ELFYAML::SectionHeaderTable &SHT = Doc.getSectionHeaderTable();
1976  if (!SHT.NoHeaders.value_or(false))
1977    CBA.updateDataAt(*SHT.Offset, SHeaders.data(),
1978                     SHT.getNumHeaders(SHeaders.size()) * sizeof(Elf_Shdr));
1979
1980  CBA.writeBlobToStream(OS);
1981  return true;
1982}
1983
1984namespace llvm {
1985namespace yaml {
1986
1987bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH,
1988              uint64_t MaxSize) {
1989  bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1990  bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1991  if (Is64Bit) {
1992    if (IsLE)
1993      return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
1994    return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
1995  }
1996  if (IsLE)
1997    return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
1998  return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH, MaxSize);
1999}
2000
2001} // namespace yaml
2002} // namespace llvm
2003