47class ContiguousBlobAccumulator {
60 "reached the output size limit");
66 : InitialOffset(BaseOffset), MaxSize(
SizeLimit),
OS(Buf) {}
70 void writeBlobToStream(
raw_ostream &Out)
const { Out <<
OS.str(); }
72 Error takeLimitError() {
75 return std::move(ReachedLimitErr);
85 uint64_t PaddingSize = AlignedOffset - CurrentOffset;
86 if (!checkLimit(PaddingSize))
89 writeZeros(PaddingSize);
100 if (!checkLimit(
Bin.binary_size()))
102 Bin.writeAsBinary(OS,
N);
111 if (checkLimit(
Size))
115 void write(
unsigned char C) {
120 unsigned writeULEB128(
uint64_t Val) {
127 if (checkLimit(
sizeof(
T)))
128 support::endian::write<T>(OS, Val,
E);
131 void updateDataAt(
uint64_t Pos,
void *Data,
size_t Size) {
133 memcpy(&Buf[Pos - InitialOffset], Data,
Size);
145 return Map.insert({
Name, Ndx}).second;
160 assert(
false &&
"Expected section not found in index");
163 unsigned size()
const {
return Map.size(); }
178template <
class ELFT>
class ELFState {
198 StringRef SectionHeaderStringTableName =
".shstrtab";
203 NameToIdxMap DynSymN2I;
209 bool HasError =
false;
219 void buildSectionIndex();
220 void buildSymbolIndexes();
221 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
222 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
224 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
225 ContiguousBlobAccumulator &CBA);
226 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
227 ContiguousBlobAccumulator &CBA,
229 void initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
231 ContiguousBlobAccumulator &CBA,
233 void initDWARFSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
234 ContiguousBlobAccumulator &CBA,
236 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
237 std::vector<Elf_Shdr> &SHeaders);
239 std::vector<Fragment>
243 void finalizeStrings();
245 void writeSectionContent(Elf_Shdr &SHeader,
247 ContiguousBlobAccumulator &CBA);
248 void writeSectionContent(Elf_Shdr &SHeader,
250 ContiguousBlobAccumulator &CBA);
251 void writeSectionContent(Elf_Shdr &SHeader,
253 ContiguousBlobAccumulator &CBA);
254 void writeSectionContent(Elf_Shdr &SHeader,
256 ContiguousBlobAccumulator &CBA);
257 void writeSectionContent(Elf_Shdr &SHeader,
259 ContiguousBlobAccumulator &CBA);
260 void writeSectionContent(Elf_Shdr &SHeader,
262 ContiguousBlobAccumulator &CBA);
263 void writeSectionContent(Elf_Shdr &SHeader,
265 ContiguousBlobAccumulator &CBA);
266 void writeSectionContent(Elf_Shdr &SHeader,
268 ContiguousBlobAccumulator &CBA);
269 void writeSectionContent(Elf_Shdr &SHeader,
271 ContiguousBlobAccumulator &CBA);
272 void writeSectionContent(Elf_Shdr &SHeader,
274 ContiguousBlobAccumulator &CBA);
275 void writeSectionContent(Elf_Shdr &SHeader,
277 ContiguousBlobAccumulator &CBA);
278 void writeSectionContent(Elf_Shdr &SHeader,
280 ContiguousBlobAccumulator &CBA);
281 void writeSectionContent(Elf_Shdr &SHeader,
283 ContiguousBlobAccumulator &CBA);
284 void writeSectionContent(Elf_Shdr &SHeader,
286 ContiguousBlobAccumulator &CBA);
287 void writeSectionContent(Elf_Shdr &SHeader,
289 ContiguousBlobAccumulator &CBA);
290 void writeSectionContent(Elf_Shdr &SHeader,
292 ContiguousBlobAccumulator &CBA);
293 void writeSectionContent(Elf_Shdr &SHeader,
295 ContiguousBlobAccumulator &CBA);
296 void writeSectionContent(Elf_Shdr &SHeader,
298 ContiguousBlobAccumulator &CBA);
299 void writeSectionContent(Elf_Shdr &SHeader,
301 ContiguousBlobAccumulator &CBA);
302 void writeSectionContent(Elf_Shdr &SHeader,
304 ContiguousBlobAccumulator &CBA);
305 void writeSectionContent(Elf_Shdr &SHeader,
307 ContiguousBlobAccumulator &CBA);
309 void writeFill(
ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
319 std::optional<llvm::yaml::Hex64>
Offset);
330 return A.size() *
sizeof(
T);
337template <
class T>
static void zero(
T &Obj) { memset(&Obj, 0,
sizeof(Obj)); }
341 : Doc(
D), ErrHandler(EH) {
345 if (Doc.Header.SectionHeaderStringTable) {
346 SectionHeaderStringTableName = *Doc.Header.SectionHeaderStringTable;
347 if (*Doc.Header.SectionHeaderStringTable ==
".strtab")
348 ShStrtabStrings = &DotStrtab;
349 else if (*Doc.Header.SectionHeaderStringTable ==
".dynstr")
350 ShStrtabStrings = &DotDynstr;
354 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
356 if (Sections.empty() || Sections.front()->Type !=
ELF::SHT_NULL)
359 std::make_unique<ELFYAML::Section>(
360 ELFYAML::Chunk::ChunkKind::RawContent,
true));
364 for (
size_t I = 0;
I < Doc.Chunks.size(); ++
I) {
365 const std::unique_ptr<ELFYAML::Chunk> &
C = Doc.Chunks[
I];
368 if (
auto S = dyn_cast<ELFYAML::SectionHeaderTable>(
C.get())) {
370 reportError(
"multiple section header tables are not allowed");
378 if (
C->Name.empty()) {
385 if (!DocSections.
insert(
C->Name).second)
387 "' at YAML section/fill number " +
Twine(
I));
391 if (Doc.DynamicSymbols) {
392 if (SectionHeaderStringTableName ==
".dynsym")
393 reportError(
"cannot use '.dynsym' as the section header name table when "
394 "there are dynamic symbols");
395 ImplicitSections.
insert(
".dynsym");
396 ImplicitSections.
insert(
".dynstr");
399 if (SectionHeaderStringTableName ==
".symtab")
400 reportError(
"cannot use '.symtab' as the section header name table when "
401 "there are symbols");
402 ImplicitSections.
insert(
".symtab");
405 for (
StringRef DebugSecName : Doc.DWARF->getNonEmptySectionNames()) {
406 std::string SecName = (
"." + DebugSecName).str();
409 if (SectionHeaderStringTableName == SecName)
411 "' as the section header name table when it is needed for "
416 ImplicitSections.
insert(
".strtab");
417 if (!SecHdrTable || !SecHdrTable->
NoHeaders.value_or(
false))
418 ImplicitSections.
insert(SectionHeaderStringTableName);
422 for (
StringRef SecName : ImplicitSections) {
423 if (DocSections.
count(SecName))
426 std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
427 ELFYAML::Chunk::ChunkKind::RawContent,
true );
430 if (SecName == SectionHeaderStringTableName)
432 else if (SecName ==
".dynsym")
434 else if (SecName ==
".symtab")
444 if (Doc.Chunks.back().get() == SecHdrTable)
445 Doc.Chunks.insert(Doc.Chunks.end() - 1, std::move(Sec));
447 Doc.Chunks.push_back(std::move(Sec));
453 Doc.Chunks.push_back(
454 std::make_unique<ELFYAML::SectionHeaderTable>(
true));
463 Header.e_ident[
EI_MAG0] = 0x7f;
468 Header.e_ident[
EI_DATA] = Doc.Header.Data;
470 Header.e_ident[
EI_OSABI] = Doc.Header.OSABI;
472 Header.e_type = Doc.Header.Type;
474 if (Doc.Header.Machine)
475 Header.e_machine = *Doc.Header.Machine;
480 Header.e_entry = Doc.Header.Entry;
481 Header.e_flags = Doc.Header.Flags;
482 Header.e_ehsize =
sizeof(Elf_Ehdr);
484 if (Doc.Header.EPhOff)
485 Header.e_phoff = *Doc.Header.EPhOff;
486 else if (!Doc.ProgramHeaders.empty())
487 Header.e_phoff =
sizeof(Header);
491 if (Doc.Header.EPhEntSize)
492 Header.e_phentsize = *Doc.Header.EPhEntSize;
493 else if (!Doc.ProgramHeaders.empty())
494 Header.e_phentsize =
sizeof(Elf_Phdr);
496 Header.e_phentsize = 0;
498 if (Doc.Header.EPhNum)
499 Header.e_phnum = *Doc.Header.EPhNum;
500 else if (!Doc.ProgramHeaders.empty())
501 Header.e_phnum = Doc.ProgramHeaders.size();
505 Header.e_shentsize = Doc.Header.EShEntSize ? (
uint16_t)*Doc.Header.EShEntSize
509 Doc.getSectionHeaderTable();
511 if (Doc.Header.EShOff)
512 Header.e_shoff = *Doc.Header.EShOff;
513 else if (SectionHeaders.
Offset)
514 Header.e_shoff = *SectionHeaders.
Offset;
518 if (Doc.Header.EShNum)
519 Header.e_shnum = *Doc.Header.EShNum;
521 Header.e_shnum = SectionHeaders.
getNumHeaders(Doc.getSections().size());
523 if (Doc.Header.EShStrNdx)
524 Header.e_shstrndx = *Doc.Header.EShStrNdx;
525 else if (SectionHeaders.
Offset &&
526 !ExcludedSectionHeaders.count(SectionHeaderStringTableName))
527 Header.e_shstrndx = SN2I.get(SectionHeaderStringTableName);
529 Header.e_shstrndx = 0;
531 OS.
write((
const char *)&Header,
sizeof(Header));
535void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
538 for (
size_t I = 0,
E = Doc.Chunks.size();
I !=
E; ++
I) {
539 if (
auto S = dyn_cast<ELFYAML::Fill>(Doc.Chunks[
I].get()))
540 NameToFill[S->Name] = S;
541 NameToIndex[Doc.Chunks[
I]->Name] =
I + 1;
544 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
545 for (
size_t I = 0,
E = Doc.ProgramHeaders.size();
I !=
E; ++
I) {
549 Phdr.p_type = YamlPhdr.
Type;
550 Phdr.p_flags = YamlPhdr.
Flags;
551 Phdr.p_vaddr = YamlPhdr.
VAddr;
552 Phdr.p_paddr = YamlPhdr.
PAddr;
553 PHeaders.push_back(Phdr);
562 "' by the 'FirstSec' key of the program header with index " +
567 "' by the 'LastSec' key of the program header with index " +
574 ": the section index of " + *YamlPhdr.
FirstSec +
575 " is greater than the index of " + *YamlPhdr.
LastSec);
578 YamlPhdr.
Chunks.push_back(Doc.Chunks[
I - 1].get());
588 if (!SN2I.lookup(S,
Index) && !to_integer(S,
Index)) {
590 reportError(
"unknown section referenced: '" + S +
"' by YAML symbol '" +
593 reportError(
"unknown section referenced: '" + S +
"' by YAML section '" +
599 Doc.getSectionHeaderTable();
606 size_t FirstExcluded =
608 if (
Index > FirstExcluded) {
610 reportError(
"unable to link '" + LocSec +
"' to excluded section '" + S +
613 reportError(
"excluded section referenced: '" + S +
"' by symbol '" +
622 const NameToIdxMap &
SymMap = IsDynamic ? DynSymN2I : SymN2I;
627 reportError(
"unknown symbol referenced: '" + S +
"' by YAML section '" +
638 if (
From->ShAddrAlign)
639 To.sh_addralign = *
From->ShAddrAlign;
641 To.sh_flags = *
From->ShFlags;
643 To.sh_name = *
From->ShName;
645 To.sh_offset = *
From->ShOffset;
647 To.sh_size = *
From->ShSize;
649 To.sh_type = *
From->ShType;
653bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
657 if (Header.sh_offset)
660 if (SecName ==
".strtab")
661 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
662 else if (SecName ==
".dynstr")
663 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
664 else if (SecName == SectionHeaderStringTableName)
665 initStrtabSectionHeader(Header, SecName, *ShStrtabStrings, CBA, YAMLSec);
666 else if (SecName ==
".symtab")
667 initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
668 else if (SecName ==
".dynsym")
669 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
673 if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
675 initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
679 LocationCounter += Header.sh_size;
682 overrideFields<ELFT>(YAMLSec, Header);
692 std::string Ret =
Name.empty() ?
"" :
Name.str() +
' ';
707 return S.
substr(0, SuffixPos - 1);
714 if (ExcludedSectionHeaders.count(
Name))
716 return ShStrtabStrings->getOffset(
Name);
720 const std::optional<yaml::BinaryRef> &
Content,
721 const std::optional<llvm::yaml::Hex64> &
Size) {
722 size_t ContentSize = 0;
725 ContentSize =
Content->binary_size();
731 CBA.writeZeros(*
Size - ContentSize);
759void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
760 ContiguousBlobAccumulator &CBA) {
763 SHeaders.resize(Doc.getSections().size());
765 for (
const std::unique_ptr<ELFYAML::Chunk> &
D : Doc.Chunks) {
767 S->Offset = alignToOffset(CBA, 1, S->Offset);
769 LocationCounter += S->Size;
774 dyn_cast<ELFYAML::SectionHeaderTable>(
D.get())) {
775 if (S->NoHeaders.value_or(
false))
779 S->Offset = alignToOffset(CBA,
sizeof(
typename ELFT::uint),
782 S->Offset = alignToOffset(CBA, 1, S->Offset);
784 uint64_t Size = S->getNumHeaders(SHeaders.size()) *
sizeof(Elf_Shdr);
787 CBA.writeZeros(
Size);
788 LocationCounter +=
Size;
793 bool IsFirstUndefSection = Sec == Doc.getSections().front();
797 Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->
Name)];
799 SHeader.sh_link = toSectionIndex(*Sec->
Link, Sec->
Name);
803 if (!LinkSec.
empty() && !ExcludedSectionHeaders.count(LinkSec) &&
804 SN2I.lookup(LinkSec, Link))
805 SHeader.sh_link =
Link;
809 SHeader.sh_entsize = *Sec->
EntSize;
811 SHeader.sh_entsize = ELFYAML::getDefaultShEntSize<ELFT>(
818 if (initImplicitHeader(CBA, SHeader, Sec->
Name,
822 assert(Sec &&
"It can't be null unless it is an implicit section. But all "
823 "implicit sections should already have been handled above.");
827 SHeader.sh_type = Sec->
Type;
829 SHeader.sh_flags = *Sec->
Flags;
834 if (!IsFirstUndefSection || Sec->
Offset)
835 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->
Offset);
837 assignSectionAddress(SHeader, Sec);
839 if (IsFirstUndefSection) {
840 if (
auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
843 SHeader.sh_size = *RawSec->Size;
845 SHeader.sh_info = *RawSec->Info;
848 LocationCounter += SHeader.sh_size;
849 overrideFields<ELFT>(Sec, SHeader);
853 if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->
Content || Sec->
Size))
856 if (
auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
857 writeSectionContent(SHeader, *S, CBA);
858 }
else if (
auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
859 writeSectionContent(SHeader, *S, CBA);
860 }
else if (
auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
861 writeSectionContent(SHeader, *S, CBA);
862 }
else if (
auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
863 writeSectionContent(SHeader, *S, CBA);
864 }
else if (
auto S = dyn_cast<ELFYAML::GroupSection>(Sec)) {
865 writeSectionContent(SHeader, *S, CBA);
866 }
else if (
auto S = dyn_cast<ELFYAML::ARMIndexTableSection>(Sec)) {
867 writeSectionContent(SHeader, *S, CBA);
868 }
else if (
auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
869 writeSectionContent(SHeader, *S, CBA);
870 }
else if (
auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
871 writeSectionContent(SHeader, *S, CBA);
872 }
else if (
auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
873 writeSectionContent(SHeader, *S, CBA);
874 }
else if (
auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
875 writeSectionContent(SHeader, *S, CBA);
876 }
else if (
auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
877 writeSectionContent(SHeader, *S, CBA);
878 }
else if (
auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
879 writeSectionContent(SHeader, *S, CBA);
880 }
else if (
auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
881 writeSectionContent(SHeader, *S, CBA);
882 }
else if (
auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
883 writeSectionContent(SHeader, *S, CBA);
884 }
else if (
auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
885 writeSectionContent(SHeader, *S, CBA);
886 }
else if (
auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
887 writeSectionContent(SHeader, *S, CBA);
888 }
else if (
auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
889 writeSectionContent(SHeader, *S, CBA);
890 }
else if (
auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
891 writeSectionContent(SHeader, *S, CBA);
892 }
else if (
auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
893 writeSectionContent(SHeader, *S, CBA);
894 }
else if (
auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
895 writeSectionContent(SHeader, *S, CBA);
896 }
else if (
auto S = dyn_cast<ELFYAML::BBAddrMapSection>(Sec)) {
897 writeSectionContent(SHeader, *S, CBA);
902 LocationCounter += SHeader.sh_size;
905 overrideFields<ELFT>(Sec, SHeader);
910void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
912 if (YAMLSec && YAMLSec->
Address) {
913 SHeader.sh_addr = *YAMLSec->
Address;
914 LocationCounter = *YAMLSec->
Address;
926 alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
927 SHeader.sh_addr = LocationCounter;
931 for (
size_t I = 0;
I < Symbols.size(); ++
I)
934 return Symbols.size();
938std::vector<typename ELFT::Sym>
941 std::vector<Elf_Sym>
Ret;
942 Ret.resize(Symbols.size() + 1);
953 else if (!
Sym.Name.empty())
958 Symbol.st_shndx = toSectionIndex(*
Sym.Section,
"",
Sym.Name);
962 Symbol.st_value =
Sym.Value.value_or(yaml::Hex64(0));
964 Symbol.st_size =
Sym.Size.value_or(yaml::Hex64(0));
971void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
973 ContiguousBlobAccumulator &CBA,
976 bool IsStatic = STType == SymtabType::Static;
978 if (IsStatic && Doc.Symbols)
979 Symbols = *Doc.Symbols;
980 else if (!IsStatic && Doc.DynamicSymbols)
981 Symbols = *Doc.DynamicSymbols;
984 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
986 bool HasSymbolsDescription =
987 (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
988 if (HasSymbolsDescription) {
989 StringRef Property = (IsStatic ?
"`Symbols`" :
"`DynamicSymbols`");
991 reportError(
"cannot specify both `Content` and " + Property +
992 " for symbol table section '" + RawSec->
Name +
"'");
994 reportError(
"cannot specify both `Size` and " + Property +
995 " for symbol table section '" + RawSec->
Name +
"'");
1000 SHeader.sh_name = getSectionNameOffset(IsStatic ?
".symtab" :
".dynsym");
1003 SHeader.sh_type = YAMLSec->
Type;
1007 if (YAMLSec && YAMLSec->
Flags)
1008 SHeader.sh_flags = *YAMLSec->
Flags;
1014 SHeader.sh_info = (RawSec && RawSec->
Info) ? (
unsigned)(*RawSec->
Info)
1018 assignSectionAddress(SHeader, YAMLSec);
1020 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1021 RawSec ? RawSec->
Offset : std::nullopt);
1023 if (RawSec && (RawSec->
Content || RawSec->
Size)) {
1029 std::vector<Elf_Sym> Syms =
1030 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
1031 SHeader.sh_size = Syms.size() *
sizeof(Elf_Sym);
1032 CBA.write((
const char *)Syms.data(), SHeader.sh_size);
1035template <
class ELFT>
1036void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
1038 ContiguousBlobAccumulator &CBA,
1045 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1047 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1048 YAMLSec ? YAMLSec->
Offset : std::nullopt);
1050 if (RawSec && (RawSec->
Content || RawSec->
Size)) {
1055 SHeader.sh_size = STB.
getSize();
1058 if (RawSec && RawSec->
Info)
1059 SHeader.sh_info = *RawSec->
Info;
1061 if (YAMLSec && YAMLSec->
Flags)
1062 SHeader.sh_flags = *YAMLSec->
Flags;
1063 else if (
Name ==
".dynstr")
1066 assignSectionAddress(SHeader, YAMLSec);
1071 return Name.consume_front(
".") && DebugSecNames.
count(
Name);
1074template <
class ELFT>
1077 ContiguousBlobAccumulator &CBA) {
1088 if (
Error Err = EmitFunc(*
OS, DWARF))
1089 return std::move(Err);
1091 return CBA.tell() - BeginOffset;
1094template <
class ELFT>
1095void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
1096 ContiguousBlobAccumulator &CBA,
1101 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1102 YAMLSec ? YAMLSec->
Offset : std::nullopt);
1105 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1109 "' contents in the 'DWARF' entry and the 'Content' "
1110 "or 'Size' in the 'Sections' entry at the same time");
1113 emitDWARF<ELFT>(SHeader,
Name, *Doc.DWARF, CBA))
1114 SHeader.sh_size = *ShSizeOrErr;
1122 "entry or a RawContentSection");
1124 if (RawSec && RawSec->
Info)
1125 SHeader.sh_info = *RawSec->
Info;
1127 if (YAMLSec && YAMLSec->
Flags)
1128 SHeader.sh_flags = *YAMLSec->
Flags;
1129 else if (
Name ==
".debug_str")
1132 assignSectionAddress(SHeader, YAMLSec);
1135template <
class ELFT>
void ELFState<ELFT>::reportError(
const Twine &Msg) {
1140template <
class ELFT>
void ELFState<ELFT>::reportError(
Error Err) {
1146template <
class ELFT>
1147std::vector<Fragment>
1150 std::vector<Fragment>
Ret;
1159 const Elf_Shdr &
H = SHeaders[SN2I.get(S->
Name)];
1160 Ret.push_back({
H.sh_offset,
H.sh_size,
H.sh_type,
H.sh_addralign});
1165template <
class ELFT>
1166void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
1167 std::vector<Elf_Shdr> &SHeaders) {
1169 for (
auto &YamlPhdr : Doc.ProgramHeaders) {
1170 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
1171 std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
1173 return A.Offset <
B.Offset;
1175 reportError(
"sections in the program header with index " +
1176 Twine(PhdrIdx) +
" are not sorted by their file offset");
1179 if (!Fragments.empty() && *YamlPhdr.
Offset > Fragments.front().Offset)
1181 " must be less than or equal to the minimum file offset of "
1182 "all included sections (0x" +
1184 PHeader.p_offset = *YamlPhdr.
Offset;
1185 }
else if (!Fragments.empty()) {
1186 PHeader.p_offset = Fragments.front().Offset;
1191 PHeader.p_filesz = *YamlPhdr.
FileSize;
1192 }
else if (!Fragments.empty()) {
1193 uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
1198 FileSize += Fragments.back().Size;
1199 PHeader.p_filesz = FileSize;
1203 uint64_t MemOffset = PHeader.p_offset;
1204 for (
const Fragment &
F : Fragments)
1205 MemOffset = std::max(MemOffset,
F.Offset +
F.Size);
1208 : MemOffset - PHeader.p_offset;
1210 if (YamlPhdr.
Align) {
1211 PHeader.p_align = *YamlPhdr.
Align;
1216 PHeader.p_align = 1;
1217 for (
const Fragment &
F : Fragments)
1218 PHeader.p_align = std::max((
uint64_t)PHeader.p_align,
F.AddrAlign);
1229 return (isa<ELFYAML::Fill>(C) ||
1230 cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
1237template <
class ELFT>
1238void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1240 ContiguousBlobAccumulator &CBA) {
1244 SHeader.sh_size = *S.
Size;
1249 if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
1250 CBA.writeZeros(*S.
Size);
1253template <
class ELFT>
1254void ELFState<ELFT>::writeSectionContent(
1256 ContiguousBlobAccumulator &CBA) {
1258 SHeader.sh_info = *Section.Info;
1267template <
class ELFT>
1268void ELFState<ELFT>::writeSectionContent(
1270 ContiguousBlobAccumulator &CBA) {
1273 "Section type is not SHT_REL nor SHT_RELA");
1275 if (!
Section.RelocatableSec.empty())
1276 SHeader.sh_info = toSectionIndex(
Section.RelocatableSec,
Section.Name);
1283 const bool IsDynamic =
Section.Link && (*
Section.Link ==
".dynsym");
1289 REntry.r_offset = Rel.
Offset;
1290 REntry.r_addend = Rel.
Addend;
1292 CBA.write((
const char *)&REntry,
sizeof(REntry));
1296 REntry.r_offset = Rel.
Offset;
1298 CBA.write((
const char *)&REntry,
sizeof(REntry));
1302 SHeader.sh_size = (IsRela ?
sizeof(Elf_Rela) :
sizeof(Elf_Rel)) *
1306template <
class ELFT>
1307void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1309 ContiguousBlobAccumulator &CBA) {
1313 for (llvm::yaml::Hex64
E : *
Section.Entries) {
1314 if (!ELFT::Is64Bits &&
E > UINT32_MAX)
1317 CBA.write<uintX_t>(
E, ELFT::TargetEndianness);
1320 SHeader.sh_size =
sizeof(uintX_t) *
Section.Entries->size();
1323template <
class ELFT>
1324void ELFState<ELFT>::writeSectionContent(
1326 ContiguousBlobAccumulator &CBA) {
1336 CBA.write<
uint32_t>(
E, ELFT::TargetEndianness);
1337 SHeader.sh_size = Shndx.
Entries->size() * SHeader.sh_entsize;
1340template <
class ELFT>
1341void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1343 ContiguousBlobAccumulator &CBA) {
1345 "Section type is not SHT_GROUP");
1355 unsigned int SectionIndex = 0;
1356 if (
Member.sectionNameOrType ==
"GRP_COMDAT")
1359 SectionIndex = toSectionIndex(
Member.sectionNameOrType,
Section.Name);
1360 CBA.write<
uint32_t>(SectionIndex, ELFT::TargetEndianness);
1362 SHeader.sh_size = SHeader.sh_entsize *
Section.Members->size();
1365template <
class ELFT>
1366void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1368 ContiguousBlobAccumulator &CBA) {
1374 SHeader.sh_size =
Section.Entries->size() * SHeader.sh_entsize;
1377template <
class ELFT>
1378void ELFState<ELFT>::writeSectionContent(
1380 ContiguousBlobAccumulator &CBA) {
1385 CBA.write<uintX_t>(
E.Address, ELFT::TargetEndianness);
1386 SHeader.sh_size +=
sizeof(uintX_t) + CBA.writeULEB128(
E.Size);
1390template <
class ELFT>
1391void ELFState<ELFT>::writeSectionContent(
1393 ContiguousBlobAccumulator &CBA) {
1397 <<
"PGOAnalyses should not exist in SHT_LLVM_BB_ADDR_MAP when "
1398 "Entries does not exist";
1402 const std::vector<ELFYAML::PGOAnalysisMapEntry> *PGOAnalyses =
nullptr;
1406 "in SHT_LLVM_BB_ADDR_MAP";
1408 PGOAnalyses = &
Section.PGOAnalyses.value();
1416 <<
static_cast<int>(
E.Version)
1417 <<
"; encoding using the most recent version";
1420 SHeader.sh_size += 2;
1423 bool MultiBBRangeFeatureEnabled =
false;
1427 MultiBBRangeFeatureEnabled = FeatureOrErr->MultiBBRange;
1429 MultiBBRangeFeatureEnabled ||
1430 (
E.NumBBRanges.has_value() &&
E.NumBBRanges.value() != 1) ||
1431 (
E.BBRanges &&
E.BBRanges->size() != 1);
1432 if (MultiBBRange && !MultiBBRangeFeatureEnabled)
1434 <<
") does not support multiple BB ranges.";
1439 E.NumBBRanges.value_or(
E.BBRanges ?
E.BBRanges->size() : 0);
1440 SHeader.sh_size += CBA.writeULEB128(NumBBRanges);
1447 CBA.write<uintX_t>(BBR.
BaseAddress, ELFT::TargetEndianness);
1453 SHeader.sh_size +=
sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1460 SHeader.sh_size += CBA.writeULEB128(BBE.
ID);
1462 SHeader.sh_size += CBA.writeULEB128(BBE.
Size);
1463 SHeader.sh_size += CBA.writeULEB128(BBE.
Metadata);
1476 const auto &PGOBBEntries = PGOEntry.
PGOBBEntries.value();
1477 if (TotalNumBlocks != PGOBBEntries.size()) {
1479 "BBEntries in SHT_LLVM_BB_ADDR_MAP.\n"
1480 <<
"Mismatch on function with address: "
1481 <<
E.getFunctionAddress();
1485 for (
const auto &PGOBBE : PGOBBEntries) {
1487 SHeader.sh_size += CBA.writeULEB128(*PGOBBE.BBFreq);
1488 if (PGOBBE.Successors) {
1489 SHeader.sh_size += CBA.writeULEB128(PGOBBE.Successors->size());
1490 for (
const auto &[
ID,
BrProb] : *PGOBBE.Successors) {
1491 SHeader.sh_size += CBA.writeULEB128(
ID);
1492 SHeader.sh_size += CBA.writeULEB128(
BrProb);
1499template <
class ELFT>
1500void ELFState<ELFT>::writeSectionContent(
1502 ContiguousBlobAccumulator &CBA) {
1507 CBA.write(
LO.Key.data(),
LO.Key.size());
1509 CBA.write(
LO.Value.data(),
LO.Value.size());
1511 SHeader.sh_size += (
LO.Key.size() +
LO.Value.size() + 2);
1515template <
class ELFT>
1516void ELFState<ELFT>::writeSectionContent(
1518 ContiguousBlobAccumulator &CBA) {
1523 CBA.write(
Lib.data(),
Lib.size());
1525 SHeader.sh_size +=
Lib.size() + 1;
1529template <
class ELFT>
1531ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA,
uint64_t Align,
1532 std::optional<llvm::yaml::Hex64>
Offset) {
1533 uint64_t CurrentOffset = CBA.getOffset();
1540 return CurrentOffset;
1549 CBA.writeZeros(AlignedOffset - CurrentOffset);
1550 return AlignedOffset;
1553template <
class ELFT>
1554void ELFState<ELFT>::writeSectionContent(
1556 ContiguousBlobAccumulator &CBA) {
1561 CBA.write<
uint64_t>(
E.Weight, ELFT::TargetEndianness);
1566template <
class ELFT>
1567void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1569 ContiguousBlobAccumulator &CBA) {
1574 Section.NBucket.value_or(llvm::yaml::Hex64(
Section.Bucket->size())),
1575 ELFT::TargetEndianness);
1577 Section.NChain.value_or(llvm::yaml::Hex64(
Section.Chain->size())),
1578 ELFT::TargetEndianness);
1581 CBA.write<
uint32_t>(Val, ELFT::TargetEndianness);
1583 CBA.write<
uint32_t>(Val, ELFT::TargetEndianness);
1585 SHeader.sh_size = (2 +
Section.Bucket->size() +
Section.Chain->size()) * 4;
1588template <
class ELFT>
1589void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1591 ContiguousBlobAccumulator &CBA) {
1594 SHeader.sh_info = *
Section.Info;
1596 SHeader.sh_info =
Section.Entries->size();
1602 for (
size_t I = 0;
I <
Section.Entries->size(); ++
I) {
1606 VerDef.vd_version =
E.Version.value_or(1);
1607 VerDef.vd_flags =
E.Flags.value_or(0);
1608 VerDef.vd_ndx =
E.VersionNdx.value_or(0);
1609 VerDef.vd_hash =
E.Hash.value_or(0);
1610 VerDef.vd_aux =
sizeof(Elf_Verdef);
1611 VerDef.vd_cnt =
E.VerNames.size();
1612 if (
I ==
Section.Entries->size() - 1)
1616 sizeof(Elf_Verdef) +
E.VerNames.size() *
sizeof(Elf_Verdaux);
1617 CBA.write((
const char *)&VerDef,
sizeof(Elf_Verdef));
1619 for (
size_t J = 0; J <
E.VerNames.size(); ++J, ++AuxCnt) {
1620 Elf_Verdaux VernAux;
1621 VernAux.vda_name = DotDynstr.getOffset(
E.VerNames[J]);
1622 if (J ==
E.VerNames.size() - 1)
1623 VernAux.vda_next = 0;
1625 VernAux.vda_next =
sizeof(Elf_Verdaux);
1626 CBA.write((
const char *)&VernAux,
sizeof(Elf_Verdaux));
1630 SHeader.sh_size =
Section.Entries->size() *
sizeof(Elf_Verdef) +
1631 AuxCnt *
sizeof(Elf_Verdaux);
1634template <
class ELFT>
1635void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1637 ContiguousBlobAccumulator &CBA) {
1639 SHeader.sh_info = *
Section.Info;
1641 SHeader.sh_info =
Section.VerneedV->size();
1647 for (
size_t I = 0;
I <
Section.VerneedV->size(); ++
I) {
1650 Elf_Verneed VerNeed;
1651 VerNeed.vn_version = VE.
Version;
1652 VerNeed.vn_file = DotDynstr.getOffset(VE.
File);
1653 if (
I ==
Section.VerneedV->size() - 1)
1654 VerNeed.vn_next = 0;
1657 sizeof(Elf_Verneed) + VE.
AuxV.size() *
sizeof(Elf_Vernaux);
1658 VerNeed.vn_cnt = VE.
AuxV.size();
1659 VerNeed.vn_aux =
sizeof(Elf_Verneed);
1660 CBA.write((
const char *)&VerNeed,
sizeof(Elf_Verneed));
1662 for (
size_t J = 0; J < VE.
AuxV.size(); ++J, ++AuxCnt) {
1665 Elf_Vernaux VernAux;
1666 VernAux.vna_hash = VAuxE.
Hash;
1667 VernAux.vna_flags = VAuxE.
Flags;
1668 VernAux.vna_other = VAuxE.
Other;
1669 VernAux.vna_name = DotDynstr.getOffset(VAuxE.
Name);
1670 if (J == VE.
AuxV.size() - 1)
1671 VernAux.vna_next = 0;
1673 VernAux.vna_next =
sizeof(Elf_Vernaux);
1674 CBA.write((
const char *)&VernAux,
sizeof(Elf_Vernaux));
1678 SHeader.sh_size =
Section.VerneedV->size() *
sizeof(Elf_Verneed) +
1679 AuxCnt *
sizeof(Elf_Vernaux);
1682template <
class ELFT>
1683void ELFState<ELFT>::writeSectionContent(
1685 ContiguousBlobAccumulator &CBA) {
1690 CBA.write<
uint32_t>(
E.Offset, ELFT::TargetEndianness);
1691 CBA.write<
uint32_t>(
E.Value, ELFT::TargetEndianness);
1693 SHeader.sh_size =
Section.Entries->size() * 8;
1696template <
class ELFT>
1697void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1699 ContiguousBlobAccumulator &CBA) {
1701 "Section type is not SHT_MIPS_ABIFLAGS");
1705 SHeader.sh_size = SHeader.sh_entsize;
1718 CBA.write((
const char *)&Flags,
sizeof(Flags));
1721template <
class ELFT>
1722void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1724 ContiguousBlobAccumulator &CBA) {
1726 "Section type is not SHT_DYNAMIC");
1732 CBA.write<uintX_t>(DE.
Tag, ELFT::TargetEndianness);
1733 CBA.write<uintX_t>(DE.
Val, ELFT::TargetEndianness);
1735 SHeader.sh_size = 2 *
sizeof(uintX_t) *
Section.Entries->size();
1738template <
class ELFT>
1739void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1741 ContiguousBlobAccumulator &CBA) {
1747 CBA.writeULEB128(toSymbolIndex(
Sym,
Section.Name,
false));
1750template <
class ELFT>
1751void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1753 ContiguousBlobAccumulator &CBA) {
1760 if (
NE.Name.empty())
1761 CBA.write<
uint32_t>(0, ELFT::TargetEndianness);
1763 CBA.write<
uint32_t>(
NE.Name.size() + 1, ELFT::TargetEndianness);
1766 if (
NE.Desc.binary_size() == 0)
1767 CBA.write<
uint32_t>(0, ELFT::TargetEndianness);
1769 CBA.write<
uint32_t>(
NE.Desc.binary_size(), ELFT::TargetEndianness);
1772 CBA.write<
uint32_t>(
NE.Type, ELFT::TargetEndianness);
1775 if (!
NE.Name.empty()) {
1776 CBA.write(
NE.Name.data(),
NE.Name.size());
1778 CBA.padToAlignment(4);
1782 if (
NE.Desc.binary_size() != 0) {
1783 CBA.writeAsBinary(
NE.Desc);
1784 CBA.padToAlignment(4);
1788 SHeader.sh_size = CBA.tell() -
Offset;
1791template <
class ELFT>
1792void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1794 ContiguousBlobAccumulator &CBA) {
1806 CBA.write<
uint32_t>(*
Section.Header->NBuckets, ELFT::TargetEndianness);
1808 CBA.write<
uint32_t>(
Section.HashBuckets->size(), ELFT::TargetEndianness);
1816 if (
Section.Header->MaskWords)
1817 CBA.write<
uint32_t>(*
Section.Header->MaskWords, ELFT::TargetEndianness);
1819 CBA.write<
uint32_t>(
Section.BloomFilter->size(), ELFT::TargetEndianness);
1825 for (llvm::yaml::Hex64 Val : *
Section.BloomFilter)
1826 CBA.write<uintX_t>(Val, ELFT::TargetEndianness);
1829 for (llvm::yaml::Hex32 Val : *
Section.HashBuckets)
1830 CBA.write<
uint32_t>(Val, ELFT::TargetEndianness);
1833 for (llvm::yaml::Hex32 Val : *
Section.HashValues)
1834 CBA.write<
uint32_t>(Val, ELFT::TargetEndianness);
1836 SHeader.sh_size = 16 +
1837 Section.BloomFilter->size() *
sizeof(
typename ELFT::uint) +
1838 Section.HashBuckets->size() * 4 +
1839 Section.HashValues->size() * 4;
1842template <
class ELFT>
1844 ContiguousBlobAccumulator &CBA) {
1845 size_t PatternSize = Fill.
Pattern ? Fill.
Pattern->binary_size() : 0;
1847 CBA.writeZeros(Fill.
Size);
1853 for (; Written + PatternSize <= Fill.
Size; Written += PatternSize)
1854 CBA.writeAsBinary(*Fill.
Pattern);
1855 CBA.writeAsBinary(*Fill.
Pattern, Fill.
Size - Written);
1858template <
class ELFT>
1861 Doc.getSectionHeaderTable();
1871 if (!
Ret.try_emplace(Hdr.Name, ++SecNdx).second)
1872 reportError(
"repeated section name: '" + Hdr.Name +
1873 "' in the section header description");
1887 if (S == Doc.getSections().front())
1891 "' should be present in the 'Sections' or 'Excluded' lists");
1895 for (
const auto &It : Seen)
1896 reportError(
"section header contains undefined section '" + It.getKey() +
1901template <
class ELFT>
void ELFState<ELFT>::buildSectionIndex() {
1910 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
1912 Doc.getSectionHeaderTable();
1915 if (!ExcludedSectionHeaders.insert(Hdr.
Name).second)
1918 if (SectionHeaders.
NoHeaders.value_or(
false))
1920 if (!ExcludedSectionHeaders.insert(S->
Name).second)
1931 if (!ExcludedSectionHeaders.count(S->
Name))
1936template <
class ELFT>
void ELFState<ELFT>::buildSymbolIndexes() {
1938 for (
size_t I = 0, S =
V.size();
I < S; ++
I) {
1940 if (!
Sym.Name.empty() && !
Map.addName(
Sym.Name,
I + 1))
1946 Build(*Doc.Symbols, SymN2I);
1947 if (Doc.DynamicSymbols)
1948 Build(*Doc.DynamicSymbols, DynSymN2I);
1951template <
class ELFT>
void ELFState<ELFT>::finalizeStrings() {
1956 DotStrtab.finalize();
1959 if (Doc.DynamicSymbols)
1966 if (
auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
1967 if (VerNeed->VerneedV) {
1969 DotDynstr.add(VE.
File);
1971 DotDynstr.add(Aux.
Name);
1974 }
else if (
auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
1975 if (VerDef->Entries)
1978 DotDynstr.add(
Name);
1982 DotDynstr.finalize();
1986 if (ShStrtabStrings != &DotStrtab && ShStrtabStrings != &DotDynstr)
1987 ShStrtabStrings->finalize();
1990template <
class ELFT>
1993 ELFState<ELFT> State(Doc, EH);
1999 State.buildSectionIndex();
2000 State.buildSymbolIndexes();
2005 State.finalizeStrings();
2010 std::vector<Elf_Phdr> PHeaders;
2011 State.initProgramHeaders(PHeaders);
2015 const size_t SectionContentBeginOffset =
2021 ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
2023 std::vector<Elf_Shdr> SHeaders;
2024 State.initSectionHeaders(SHeaders, CBA);
2027 State.setProgramHeaderLayout(PHeaders, SHeaders);
2029 bool ReachedLimit = CBA.getOffset() > MaxSize;
2030 if (
Error E = CBA.takeLimitError()) {
2033 ReachedLimit =
true;
2038 "the desired output size is greater than permitted. Use the "
2039 "--max-size option to change the limit");
2044 State.writeELFHeader(
OS);
2049 CBA.updateDataAt(*SHT.
Offset, SHeaders.data(),
2052 CBA.writeBlobToStream(
OS);
2065 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
2066 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
2069 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
2070 return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH, MaxSize);
static Error reportError(StringRef Message)
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Common declarations for yaml2obj.
This file declares classes for handling the YAML representation of DWARF Debug Info.
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
static StringRef getDefaultLinkSec(unsigned SecType)
static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To)
static void writeArrayData(raw_ostream &OS, ArrayRef< T > A)
static bool isMips64EL(const ELFYAML::Object &Obj)
static size_t arrayDataSize(ArrayRef< T > A)
constexpr char SuffixStart
static bool shouldEmitDWARF(DWARFYAML::Data &DWARF, StringRef Name)
static uint64_t writeContent(ContiguousBlobAccumulator &CBA, const std::optional< yaml::BinaryRef > &Content, const std::optional< llvm::yaml::Hex64 > &Size)
Expected< uint64_t > emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name, const DWARFYAML::Data &DWARF, ContiguousBlobAccumulator &CBA)
static size_t findFirstNonGlobal(ArrayRef< ELFYAML::Symbol > Symbols)
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
This file declares classes for handling the YAML representation of ELF.
static cl::opt< unsigned > SizeLimit("eif-limit", cl::init(6), cl::Hidden, cl::desc("Size limit in Hexagon early if-conversion"))
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
StringSet - A set-like wrapper for the StringMap.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Allocate memory in an ever growing pool, as if by bump-pointer.
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...
Base class for error info classes.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
A vector that has set insertion semantics.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
A SetVector that performs no allocations if smaller than a certain size.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
empty - Check if the string is empty.
char back() const
back - Get the last character in the string.
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
StringRef copy(Allocator &A) const
static constexpr size_t npos
StringSet - A wrapper for StringMap that provides set-like functionality.
std::pair< typename Base::iterator, bool > insert(StringRef key)
Utility for building string tables with deduplicated suffixes.
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
void write(raw_ostream &OS) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Twine utohexstr(const uint64_t &Val)
The instances of the Type class are immutable: once they are created, they are never changed.
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
Specialized YAMLIO scalar type for representing a binary blob.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
std::function< Error(raw_ostream &, const Data &)> getDWARFEmitterByName(StringRef SecName)
std::string appendUniqueSuffix(StringRef Name, const Twine &Msg)
StringRef dropUniqueSuffix(StringRef S)
bool shouldAllocateFileSpace(ArrayRef< ProgramHeader > Phdrs, const NoBitsSection &S)
@ SHT_LLVM_CALL_GRAPH_PROFILE
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
bool yaml2elf(ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH, uint64_t MaxSize)
This is an optimization pass for GlobalISel generic memory operations.
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.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are are tuples (A,...
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
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...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
@ Dynamic
Denotes mode unknown at compile time.
OutputIt copy(R &&Range, OutputIt Out)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
void consumeError(Error Err)
Consume a Error without doing anything.
This struct is a compact representation of a valid (non-zero power of two) alignment.
SetVector< StringRef > getNonEmptySectionNames() const
llvm::yaml::Hex64 Metadata
llvm::yaml::Hex64 AddressOffset
std::optional< uint64_t > NumBlocks
llvm::yaml::Hex64 BaseAddress
std::optional< std::vector< BBEntry > > BBEntries
std::optional< llvm::yaml::Hex64 > Offset
std::optional< yaml::BinaryRef > Pattern
unsigned getMachine() const
const SectionHeaderTable & getSectionHeaderTable() const
std::vector< ProgramHeader > ProgramHeaders
std::optional< std::vector< PGOBBEntry > > PGOBBEntries
std::optional< uint64_t > FuncEntryCount
std::optional< llvm::yaml::Hex64 > Info
std::optional< StringRef > Symbol
std::optional< llvm::yaml::Hex64 > Address
std::optional< StringRef > Link
std::optional< llvm::yaml::Hex64 > Size
llvm::yaml::Hex64 AddressAlign
std::optional< ELF_SHF > Flags
std::optional< yaml::BinaryRef > Content
std::optional< llvm::yaml::Hex64 > EntSize
std::optional< std::vector< uint32_t > > Entries
std::vector< VernauxEntry > AuxV
static Expected< Features > decode(uint8_t Val)
Common declarations for yaml2obj.