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