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