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