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