44class ContiguousBlobAccumulator {
57 "reached the output size limit");
63 : InitialOffset(BaseOffset), MaxSize(
SizeLimit),
OS(Buf) {}
67 void writeBlobToStream(
raw_ostream &Out)
const { Out <<
OS.str(); }
69 Error takeLimitError() {
72 return std::move(ReachedLimitErr);
82 uint64_t PaddingSize = AlignedOffset - CurrentOffset;
83 if (!checkLimit(PaddingSize))
86 writeZeros(PaddingSize);
97 if (!checkLimit(
Bin.binary_size()))
99 Bin.writeAsBinary(OS,
N);
108 if (checkLimit(
Size))
112 void write(
unsigned char C) {
117 unsigned writeULEB128(
uint64_t Val) {
123 unsigned writeSLEB128(int64_t Val) {
130 if (checkLimit(
sizeof(
T)))
131 support::endian::write<T>(OS, Val, E);
134 void updateDataAt(
uint64_t Pos,
void *Data,
size_t Size) {
136 memcpy(&Buf[Pos - InitialOffset], Data,
Size);
148 return Map.insert({
Name, Ndx}).second;
163 assert(
false &&
"Expected section not found in index");
166 unsigned size()
const {
return Map.size(); }
181template <
class ELFT>
class ELFState {
201 StringRef SectionHeaderStringTableName =
".shstrtab";
206 NameToIdxMap DynSymN2I;
212 bool HasError =
false;
222 void buildSectionIndex();
223 void buildSymbolIndexes();
224 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
225 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header,
227 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
228 ContiguousBlobAccumulator &CBA);
229 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
230 ContiguousBlobAccumulator &CBA,
232 void initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
234 ContiguousBlobAccumulator &CBA,
236 void initDWARFSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
237 ContiguousBlobAccumulator &CBA,
239 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
240 std::vector<Elf_Shdr> &SHeaders);
242 std::vector<Fragment>
246 void finalizeStrings();
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);
308 void writeSectionContent(Elf_Shdr &SHeader,
310 ContiguousBlobAccumulator &CBA);
312 void writeFill(
ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA);
322 std::optional<llvm::yaml::Hex64>
Offset);
333 return A.size() *
sizeof(
T);
340template <
class T>
static void zero(
T &Obj) { memset(&Obj, 0,
sizeof(Obj)); }
344 : Doc(
D), ErrHandler(EH) {
348 if (Doc.Header.SectionHeaderStringTable) {
349 SectionHeaderStringTableName = *Doc.Header.SectionHeaderStringTable;
350 if (*Doc.Header.SectionHeaderStringTable ==
".strtab")
351 ShStrtabStrings = &DotStrtab;
352 else if (*Doc.Header.SectionHeaderStringTable ==
".dynstr")
353 ShStrtabStrings = &DotDynstr;
357 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
359 if (Sections.empty() || Sections.front()->Type !=
ELF::SHT_NULL)
362 std::make_unique<ELFYAML::Section>(
363 ELFYAML::Chunk::ChunkKind::RawContent,
true));
367 for (
size_t I = 0;
I < Doc.Chunks.size(); ++
I) {
368 const std::unique_ptr<ELFYAML::Chunk> &
C = Doc.Chunks[
I];
371 if (
auto S = dyn_cast<ELFYAML::SectionHeaderTable>(
C.get())) {
373 reportError(
"multiple section header tables are not allowed");
381 if (
C->Name.empty()) {
388 if (!DocSections.
insert(
C->Name).second)
390 "' at YAML section/fill number " +
Twine(
I));
394 if (Doc.DynamicSymbols) {
395 if (SectionHeaderStringTableName ==
".dynsym")
396 reportError(
"cannot use '.dynsym' as the section header name table when "
397 "there are dynamic symbols");
398 ImplicitSections.
insert(
".dynsym");
399 ImplicitSections.
insert(
".dynstr");
402 if (SectionHeaderStringTableName ==
".symtab")
403 reportError(
"cannot use '.symtab' as the section header name table when "
404 "there are symbols");
405 ImplicitSections.
insert(
".symtab");
408 for (
StringRef DebugSecName : Doc.DWARF->getNonEmptySectionNames()) {
409 std::string SecName = (
"." + DebugSecName).str();
412 if (SectionHeaderStringTableName == SecName)
414 "' as the section header name table when it is needed for "
419 ImplicitSections.
insert(
".strtab");
420 if (!SecHdrTable || !SecHdrTable->
NoHeaders.value_or(
false))
421 ImplicitSections.
insert(SectionHeaderStringTableName);
425 for (
StringRef SecName : ImplicitSections) {
426 if (DocSections.
count(SecName))
429 std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
430 ELFYAML::Chunk::ChunkKind::RawContent,
true );
433 if (SecName == SectionHeaderStringTableName)
435 else if (SecName ==
".dynsym")
437 else if (SecName ==
".symtab")
447 if (Doc.Chunks.back().get() == SecHdrTable)
448 Doc.Chunks.insert(Doc.Chunks.end() - 1, std::move(Sec));
450 Doc.Chunks.push_back(std::move(Sec));
456 Doc.Chunks.push_back(
457 std::make_unique<ELFYAML::SectionHeaderTable>(
true));
466 Header.e_ident[
EI_MAG0] = 0x7f;
471 Header.e_ident[
EI_DATA] = Doc.Header.Data;
473 Header.e_ident[
EI_OSABI] = Doc.Header.OSABI;
475 Header.e_type = Doc.Header.Type;
477 if (Doc.Header.Machine)
478 Header.e_machine = *Doc.Header.Machine;
483 Header.e_entry = Doc.Header.Entry;
484 Header.e_flags = Doc.Header.Flags;
485 Header.e_ehsize =
sizeof(Elf_Ehdr);
487 if (Doc.Header.EPhOff)
488 Header.e_phoff = *Doc.Header.EPhOff;
489 else if (!Doc.ProgramHeaders.empty())
490 Header.e_phoff =
sizeof(Header);
494 if (Doc.Header.EPhEntSize)
495 Header.e_phentsize = *Doc.Header.EPhEntSize;
496 else if (!Doc.ProgramHeaders.empty())
497 Header.e_phentsize =
sizeof(Elf_Phdr);
499 Header.e_phentsize = 0;
501 if (Doc.Header.EPhNum)
502 Header.e_phnum = *Doc.Header.EPhNum;
503 else if (!Doc.ProgramHeaders.empty())
504 Header.e_phnum = Doc.ProgramHeaders.size();
508 Header.e_shentsize = Doc.Header.EShEntSize ? (
uint16_t)*Doc.Header.EShEntSize
512 Doc.getSectionHeaderTable();
514 if (Doc.Header.EShOff)
515 Header.e_shoff = *Doc.Header.EShOff;
516 else if (SectionHeaders.
Offset)
517 Header.e_shoff = *SectionHeaders.
Offset;
521 if (Doc.Header.EShNum)
522 Header.e_shnum = *Doc.Header.EShNum;
524 Header.e_shnum = SectionHeaders.
getNumHeaders(Doc.getSections().size());
526 if (Doc.Header.EShStrNdx)
527 Header.e_shstrndx = *Doc.Header.EShStrNdx;
528 else if (SectionHeaders.
Offset &&
529 !ExcludedSectionHeaders.count(SectionHeaderStringTableName))
530 Header.e_shstrndx = SN2I.get(SectionHeaderStringTableName);
532 Header.e_shstrndx = 0;
534 OS.
write((
const char *)&Header,
sizeof(Header));
538void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
540 for (
size_t I = 0, E = Doc.Chunks.size();
I != E; ++
I) {
541 NameToIndex[Doc.Chunks[
I]->Name] =
I + 1;
544 for (
size_t I = 0, E = Doc.ProgramHeaders.size();
I != E; ++
I) {
548 Phdr.p_type = YamlPhdr.
Type;
549 Phdr.p_flags = YamlPhdr.
Flags;
550 Phdr.p_vaddr = YamlPhdr.
VAddr;
551 Phdr.p_paddr = YamlPhdr.
PAddr;
552 PHeaders.push_back(Phdr);
561 "' by the 'FirstSec' key of the program header with index " +
566 "' by the 'LastSec' key of the program header with index " +
573 ": the section index of " + *YamlPhdr.
FirstSec +
574 " is greater than the index of " + *YamlPhdr.
LastSec);
577 YamlPhdr.
Chunks.push_back(Doc.Chunks[
I - 1].get());
587 if (!SN2I.lookup(S, Index) && !to_integer(S, Index)) {
589 reportError(
"unknown section referenced: '" + S +
"' by YAML symbol '" +
592 reportError(
"unknown section referenced: '" + S +
"' by YAML section '" +
598 Doc.getSectionHeaderTable();
605 size_t FirstExcluded =
607 if (Index > FirstExcluded) {
609 reportError(
"unable to link '" + LocSec +
"' to excluded section '" + S +
612 reportError(
"excluded section referenced: '" + S +
"' by symbol '" +
621 const NameToIdxMap &
SymMap = IsDynamic ? DynSymN2I : SymN2I;
625 if (!
SymMap.lookup(S, Index) && !to_integer(S, Index)) {
626 reportError(
"unknown symbol referenced: '" + S +
"' by YAML section '" +
637 if (
From->ShAddrAlign)
638 To.sh_addralign = *
From->ShAddrAlign;
640 To.sh_flags = *
From->ShFlags;
642 To.sh_name = *
From->ShName;
644 To.sh_offset = *
From->ShOffset;
646 To.sh_size = *
From->ShSize;
648 To.sh_type = *
From->ShType;
652bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
656 if (Header.sh_offset)
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);
672 if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
674 initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
678 LocationCounter += Header.sh_size;
681 overrideFields<ELFT>(YAMLSec, Header);
691 std::string Ret =
Name.empty() ?
"" :
Name.str() +
' ';
706 return S.
substr(0, SuffixPos - 1);
713 if (ExcludedSectionHeaders.count(
Name))
715 return ShStrtabStrings->getOffset(
Name);
719 const std::optional<yaml::BinaryRef> &
Content,
720 const std::optional<llvm::yaml::Hex64> &
Size) {
721 size_t ContentSize = 0;
724 ContentSize =
Content->binary_size();
730 CBA.writeZeros(*
Size - ContentSize);
758void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
759 ContiguousBlobAccumulator &CBA) {
762 SHeaders.resize(Doc.getSections().size());
764 for (
const std::unique_ptr<ELFYAML::Chunk> &
D : Doc.Chunks) {
766 S->Offset = alignToOffset(CBA, 1, S->Offset);
768 LocationCounter += S->Size;
773 dyn_cast<ELFYAML::SectionHeaderTable>(
D.get())) {
774 if (S->NoHeaders.value_or(
false))
778 S->Offset = alignToOffset(CBA,
sizeof(
typename ELFT::uint),
781 S->Offset = alignToOffset(CBA, 1, S->Offset);
783 uint64_t Size = S->getNumHeaders(SHeaders.size()) *
sizeof(Elf_Shdr);
786 CBA.writeZeros(
Size);
787 LocationCounter +=
Size;
792 bool IsFirstUndefSection = Sec == Doc.getSections().front();
796 Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->
Name)];
798 SHeader.sh_link = toSectionIndex(*Sec->
Link, Sec->
Name);
802 if (!LinkSec.
empty() && !ExcludedSectionHeaders.count(LinkSec) &&
803 SN2I.lookup(LinkSec, Link))
804 SHeader.sh_link =
Link;
808 SHeader.sh_entsize = *Sec->
EntSize;
810 SHeader.sh_entsize = ELFYAML::getDefaultShEntSize<ELFT>(
817 if (initImplicitHeader(CBA, SHeader, Sec->
Name,
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.");
826 SHeader.sh_type = Sec->
Type;
828 SHeader.sh_flags = *Sec->
Flags;
833 if (!IsFirstUndefSection || Sec->
Offset)
834 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->
Offset);
836 assignSectionAddress(SHeader, Sec);
838 if (IsFirstUndefSection) {
839 if (
auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
842 SHeader.sh_size = *RawSec->Size;
844 SHeader.sh_info = *RawSec->Info;
847 LocationCounter += SHeader.sh_size;
848 overrideFields<ELFT>(Sec, SHeader);
852 if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->
Content || Sec->
Size))
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);
901 LocationCounter += SHeader.sh_size;
904 overrideFields<ELFT>(Sec, SHeader);
909void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
911 if (YAMLSec && YAMLSec->
Address) {
912 SHeader.sh_addr = *YAMLSec->
Address;
913 LocationCounter = *YAMLSec->
Address;
925 alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
926 SHeader.sh_addr = LocationCounter;
930 for (
size_t I = 0;
I < Symbols.size(); ++
I)
933 return Symbols.size();
937std::vector<typename ELFT::Sym>
940 std::vector<Elf_Sym>
Ret;
941 Ret.resize(Symbols.size() + 1);
952 else if (!
Sym.Name.empty())
957 Symbol.st_shndx = toSectionIndex(*
Sym.Section,
"",
Sym.Name);
961 Symbol.st_value =
Sym.Value.value_or(yaml::Hex64(0));
963 Symbol.st_size =
Sym.Size.value_or(yaml::Hex64(0));
970void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
972 ContiguousBlobAccumulator &CBA,
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;
983 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
985 bool HasSymbolsDescription =
986 (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
987 if (HasSymbolsDescription) {
988 StringRef Property = (IsStatic ?
"`Symbols`" :
"`DynamicSymbols`");
990 reportError(
"cannot specify both `Content` and " + Property +
991 " for symbol table section '" + RawSec->
Name +
"'");
993 reportError(
"cannot specify both `Size` and " + Property +
994 " for symbol table section '" + RawSec->
Name +
"'");
999 SHeader.sh_name = getSectionNameOffset(IsStatic ?
".symtab" :
".dynsym");
1002 SHeader.sh_type = YAMLSec->
Type;
1006 if (YAMLSec && YAMLSec->
Flags)
1007 SHeader.sh_flags = *YAMLSec->
Flags;
1013 SHeader.sh_info = (RawSec && RawSec->
Info) ? (
unsigned)(*RawSec->
Info)
1017 assignSectionAddress(SHeader, YAMLSec);
1019 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1020 RawSec ? RawSec->
Offset : std::nullopt);
1022 if (RawSec && (RawSec->
Content || RawSec->
Size)) {
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);
1034template <
class ELFT>
1035void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
1037 ContiguousBlobAccumulator &CBA,
1044 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1046 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1047 YAMLSec ? YAMLSec->
Offset : std::nullopt);
1049 if (RawSec && (RawSec->
Content || RawSec->
Size)) {
1054 SHeader.sh_size = STB.
getSize();
1057 if (RawSec && RawSec->
Info)
1058 SHeader.sh_info = *RawSec->
Info;
1060 if (YAMLSec && YAMLSec->
Flags)
1061 SHeader.sh_flags = *YAMLSec->
Flags;
1062 else if (
Name ==
".dynstr")
1065 assignSectionAddress(SHeader, YAMLSec);
1070 return Name.consume_front(
".") && DebugSecNames.
count(
Name);
1073template <
class ELFT>
1076 ContiguousBlobAccumulator &CBA) {
1087 if (
Error Err = EmitFunc(*
OS, DWARF))
1088 return std::move(Err);
1090 return CBA.tell() - BeginOffset;
1093template <
class ELFT>
1094void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
1095 ContiguousBlobAccumulator &CBA,
1100 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1101 YAMLSec ? YAMLSec->
Offset : std::nullopt);
1104 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1108 "' contents in the 'DWARF' entry and the 'Content' "
1109 "or 'Size' in the 'Sections' entry at the same time");
1112 emitDWARF<ELFT>(SHeader,
Name, *Doc.DWARF, CBA))
1113 SHeader.sh_size = *ShSizeOrErr;
1121 "entry or a RawContentSection");
1123 if (RawSec && RawSec->
Info)
1124 SHeader.sh_info = *RawSec->
Info;
1126 if (YAMLSec && YAMLSec->
Flags)
1127 SHeader.sh_flags = *YAMLSec->
Flags;
1128 else if (
Name ==
".debug_str")
1131 assignSectionAddress(SHeader, YAMLSec);
1134template <
class ELFT>
void ELFState<ELFT>::reportError(
const Twine &Msg) {
1139template <
class ELFT>
void ELFState<ELFT>::reportError(
Error Err) {
1145template <
class ELFT>
1146std::vector<Fragment>
1149 std::vector<Fragment>
Ret;
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});
1164template <
class ELFT>
1165void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
1166 std::vector<Elf_Shdr> &SHeaders) {
1168 for (
auto &YamlPhdr : Doc.ProgramHeaders) {
1169 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
1170 std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
1172 return A.Offset <
B.Offset;
1174 reportError(
"sections in the program header with index " +
1175 Twine(PhdrIdx) +
" are not sorted by their file offset");
1178 if (!Fragments.empty() && *YamlPhdr.
Offset > Fragments.front().Offset)
1180 " must be less than or equal to the minimum file offset of "
1181 "all included sections (0x" +
1183 PHeader.p_offset = *YamlPhdr.
Offset;
1184 }
else if (!Fragments.empty()) {
1185 PHeader.p_offset = Fragments.front().Offset;
1190 PHeader.p_filesz = *YamlPhdr.
FileSize;
1191 }
else if (!Fragments.empty()) {
1192 uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
1197 FileSize += Fragments.back().Size;
1198 PHeader.p_filesz = FileSize;
1202 uint64_t MemOffset = PHeader.p_offset;
1203 for (
const Fragment &
F : Fragments)
1204 MemOffset = std::max(MemOffset,
F.Offset +
F.Size);
1207 : MemOffset - PHeader.p_offset;
1209 if (YamlPhdr.
Align) {
1210 PHeader.p_align = *YamlPhdr.
Align;
1215 PHeader.p_align = 1;
1216 for (
const Fragment &
F : Fragments)
1217 PHeader.p_align = std::max((
uint64_t)PHeader.p_align,
F.AddrAlign);
1228 return (isa<ELFYAML::Fill>(C) ||
1229 cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
1236template <
class ELFT>
1237void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1239 ContiguousBlobAccumulator &CBA) {
1243 SHeader.sh_size = *S.
Size;
1248 if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
1249 CBA.writeZeros(*S.
Size);
1252template <
class ELFT>
1253void ELFState<ELFT>::writeSectionContent(
1255 ContiguousBlobAccumulator &CBA) {
1257 SHeader.sh_info = *Section.Info;
1266template <
class ELFT>
1267void ELFState<ELFT>::writeSectionContent(
1269 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 typename ELFT::uint OffsetMask = 8,
Offset = 0, Addend = 0;
1285 uint64_t CurrentOffset = CBA.getOffset();
1288 OffsetMask |= Rel.
Offset;
1294 const bool IsDynamic =
Section.Link && (*
Section.Link ==
".dynsym");
1302 (
static_cast<typename ELFT::uint
>(Rel.
Offset) -
Offset) >> Shift;
1305 DeltaOffset * 8 + (SymIdx != CurSymIdx) + (
Type != Rel.
Type ? 2 : 0) +
1306 (Addend !=
static_cast<typename ELFT::uint
>(Rel.
Addend) ? 4 : 0);
1307 if (DeltaOffset < 0x10) {
1310 CBA.write(
B | 0x80);
1311 CBA.writeULEB128(DeltaOffset >> 4);
1316 std::make_signed_t<typename ELFT::uint>(CurSymIdx - SymIdx));
1320 CBA.writeSLEB128(
static_cast<int32_t
>(Rel.
Type -
Type));
1325 std::make_signed_t<typename ELFT::uint>(Rel.
Addend - Addend));
1328 }
else if (IsRela) {
1331 REntry.r_offset = Rel.
Offset;
1332 REntry.r_addend = Rel.
Addend;
1334 CBA.write((
const char *)&REntry,
sizeof(REntry));
1338 REntry.r_offset = Rel.
Offset;
1340 CBA.write((
const char *)&REntry,
sizeof(REntry));
1344 SHeader.sh_size = CBA.getOffset() - CurrentOffset;
1347template <
class ELFT>
1348void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1350 ContiguousBlobAccumulator &CBA) {
1354 for (llvm::yaml::Hex64 E : *
Section.Entries) {
1355 if (!ELFT::Is64Bits && E > UINT32_MAX)
1358 CBA.write<uintX_t>(E, ELFT::Endianness);
1361 SHeader.sh_size =
sizeof(uintX_t) *
Section.Entries->size();
1364template <
class ELFT>
1365void ELFState<ELFT>::writeSectionContent(
1367 ContiguousBlobAccumulator &CBA) {
1377 CBA.write<
uint32_t>(E, ELFT::Endianness);
1378 SHeader.sh_size = Shndx.
Entries->size() * SHeader.sh_entsize;
1381template <
class ELFT>
1382void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1384 ContiguousBlobAccumulator &CBA) {
1386 "Section type is not SHT_GROUP");
1396 unsigned int SectionIndex = 0;
1397 if (
Member.sectionNameOrType ==
"GRP_COMDAT")
1400 SectionIndex = toSectionIndex(
Member.sectionNameOrType,
Section.Name);
1401 CBA.write<
uint32_t>(SectionIndex, ELFT::Endianness);
1403 SHeader.sh_size = SHeader.sh_entsize *
Section.Members->size();
1406template <
class ELFT>
1407void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1409 ContiguousBlobAccumulator &CBA) {
1415 SHeader.sh_size =
Section.Entries->size() * SHeader.sh_entsize;
1418template <
class ELFT>
1419void ELFState<ELFT>::writeSectionContent(
1421 ContiguousBlobAccumulator &CBA) {
1426 CBA.write<uintX_t>(E.
Address, ELFT::Endianness);
1427 SHeader.sh_size +=
sizeof(uintX_t) + CBA.writeULEB128(E.
Size);
1431template <
class ELFT>
1432void ELFState<ELFT>::writeSectionContent(
1434 ContiguousBlobAccumulator &CBA) {
1438 <<
"PGOAnalyses should not exist in SHT_LLVM_BB_ADDR_MAP when "
1439 "Entries does not exist";
1443 const std::vector<ELFYAML::PGOAnalysisMapEntry> *PGOAnalyses =
nullptr;
1447 "in SHT_LLVM_BB_ADDR_MAP";
1449 PGOAnalyses = &
Section.PGOAnalyses.value();
1457 <<
static_cast<int>(E.Version)
1458 <<
"; encoding using the most recent version";
1459 CBA.
write(E.Version);
1460 CBA.
write(E.Feature);
1461 SHeader.sh_size += 2;
1464 bool MultiBBRangeFeatureEnabled =
false;
1468 MultiBBRangeFeatureEnabled = FeatureOrErr->MultiBBRange;
1470 MultiBBRangeFeatureEnabled ||
1471 (E.NumBBRanges.has_value() && E.NumBBRanges.value() != 1) ||
1472 (E.BBRanges && E.BBRanges->size() != 1);
1473 if (MultiBBRange && !MultiBBRangeFeatureEnabled)
1475 <<
") does not support multiple BB ranges.";
1480 E.NumBBRanges.value_or(E.BBRanges ? E.BBRanges->size() : 0);
1481 SHeader.sh_size += CBA.writeULEB128(NumBBRanges);
1488 CBA.write<uintX_t>(BBR.
BaseAddress, ELFT::Endianness);
1494 SHeader.sh_size +=
sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1496 if (!BBR.
BBEntries || FeatureOrErr->OmitBBEntries)
1501 SHeader.sh_size += CBA.writeULEB128(BBE.
ID);
1503 SHeader.sh_size += CBA.writeULEB128(BBE.
Size);
1504 SHeader.sh_size += CBA.writeULEB128(BBE.
Metadata);
1517 const auto &PGOBBEntries = PGOEntry.
PGOBBEntries.value();
1518 if (TotalNumBlocks != PGOBBEntries.size()) {
1520 "BBEntries in SHT_LLVM_BB_ADDR_MAP.\n"
1521 <<
"Mismatch on function with address: "
1522 << E.getFunctionAddress();
1526 for (
const auto &PGOBBE : PGOBBEntries) {
1528 SHeader.sh_size += CBA.writeULEB128(*PGOBBE.BBFreq);
1529 if (PGOBBE.Successors) {
1530 SHeader.sh_size += CBA.writeULEB128(PGOBBE.Successors->size());
1531 for (
const auto &[
ID,
BrProb] : *PGOBBE.Successors) {
1532 SHeader.sh_size += CBA.writeULEB128(
ID);
1533 SHeader.sh_size += CBA.writeULEB128(
BrProb);
1540template <
class ELFT>
1541void ELFState<ELFT>::writeSectionContent(
1543 ContiguousBlobAccumulator &CBA) {
1548 CBA.write(
LO.Key.data(),
LO.Key.size());
1550 CBA.write(
LO.Value.data(),
LO.Value.size());
1552 SHeader.sh_size += (
LO.Key.size() +
LO.Value.size() + 2);
1556template <
class ELFT>
1557void ELFState<ELFT>::writeSectionContent(
1559 ContiguousBlobAccumulator &CBA) {
1564 CBA.write(
Lib.data(),
Lib.size());
1566 SHeader.sh_size +=
Lib.size() + 1;
1570template <
class ELFT>
1572ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA,
uint64_t Align,
1573 std::optional<llvm::yaml::Hex64>
Offset) {
1574 uint64_t CurrentOffset = CBA.getOffset();
1581 return CurrentOffset;
1590 CBA.writeZeros(AlignedOffset - CurrentOffset);
1591 return AlignedOffset;
1594template <
class ELFT>
1595void ELFState<ELFT>::writeSectionContent(
1597 ContiguousBlobAccumulator &CBA) {
1607template <
class ELFT>
1608void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1610 ContiguousBlobAccumulator &CBA) {
1615 Section.NBucket.value_or(llvm::yaml::Hex64(
Section.Bucket->size())),
1618 Section.NChain.value_or(llvm::yaml::Hex64(
Section.Chain->size())),
1622 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1624 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1626 SHeader.sh_size = (2 +
Section.Bucket->size() +
Section.Chain->size()) * 4;
1629template <
class ELFT>
1630void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1632 ContiguousBlobAccumulator &CBA) {
1635 SHeader.sh_info = *
Section.Info;
1637 SHeader.sh_info =
Section.Entries->size();
1643 for (
size_t I = 0;
I <
Section.Entries->size(); ++
I) {
1647 VerDef.vd_version = E.
Version.value_or(1);
1648 VerDef.vd_flags = E.
Flags.value_or(0);
1650 VerDef.vd_hash = E.
Hash.value_or(0);
1651 VerDef.vd_aux = E.
VDAux.value_or(
sizeof(Elf_Verdef));
1653 if (
I ==
Section.Entries->size() - 1)
1657 sizeof(Elf_Verdef) + E.
VerNames.size() *
sizeof(Elf_Verdaux);
1658 CBA.write((
const char *)&VerDef,
sizeof(Elf_Verdef));
1660 for (
size_t J = 0; J < E.
VerNames.size(); ++J, ++AuxCnt) {
1661 Elf_Verdaux VerdAux;
1662 VerdAux.vda_name = DotDynstr.getOffset(E.
VerNames[J]);
1664 VerdAux.vda_next = 0;
1666 VerdAux.vda_next =
sizeof(Elf_Verdaux);
1667 CBA.write((
const char *)&VerdAux,
sizeof(Elf_Verdaux));
1671 SHeader.sh_size =
Section.Entries->size() *
sizeof(Elf_Verdef) +
1672 AuxCnt *
sizeof(Elf_Verdaux);
1675template <
class ELFT>
1676void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1678 ContiguousBlobAccumulator &CBA) {
1680 SHeader.sh_info = *
Section.Info;
1682 SHeader.sh_info =
Section.VerneedV->size();
1688 for (
size_t I = 0;
I <
Section.VerneedV->size(); ++
I) {
1691 Elf_Verneed VerNeed;
1692 VerNeed.vn_version = VE.
Version;
1693 VerNeed.vn_file = DotDynstr.getOffset(VE.
File);
1694 if (
I ==
Section.VerneedV->size() - 1)
1695 VerNeed.vn_next = 0;
1698 sizeof(Elf_Verneed) + VE.
AuxV.size() *
sizeof(Elf_Vernaux);
1699 VerNeed.vn_cnt = VE.
AuxV.size();
1700 VerNeed.vn_aux =
sizeof(Elf_Verneed);
1701 CBA.write((
const char *)&VerNeed,
sizeof(Elf_Verneed));
1703 for (
size_t J = 0; J < VE.
AuxV.size(); ++J, ++AuxCnt) {
1706 Elf_Vernaux VernAux;
1707 VernAux.vna_hash = VAuxE.
Hash;
1708 VernAux.vna_flags = VAuxE.
Flags;
1709 VernAux.vna_other = VAuxE.
Other;
1710 VernAux.vna_name = DotDynstr.getOffset(VAuxE.
Name);
1711 if (J == VE.
AuxV.size() - 1)
1712 VernAux.vna_next = 0;
1714 VernAux.vna_next =
sizeof(Elf_Vernaux);
1715 CBA.write((
const char *)&VernAux,
sizeof(Elf_Vernaux));
1719 SHeader.sh_size =
Section.VerneedV->size() *
sizeof(Elf_Verneed) +
1720 AuxCnt *
sizeof(Elf_Vernaux);
1723template <
class ELFT>
1724void ELFState<ELFT>::writeSectionContent(
1726 ContiguousBlobAccumulator &CBA) {
1734 SHeader.sh_size =
Section.Entries->size() * 8;
1737template <
class ELFT>
1738void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1740 ContiguousBlobAccumulator &CBA) {
1742 "Section type is not SHT_MIPS_ABIFLAGS");
1746 SHeader.sh_size = SHeader.sh_entsize;
1759 CBA.write((
const char *)&Flags,
sizeof(Flags));
1762template <
class ELFT>
1763void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1765 ContiguousBlobAccumulator &CBA) {
1767 "Section type is not SHT_DYNAMIC");
1773 CBA.write<uintX_t>(DE.
Tag, ELFT::Endianness);
1774 CBA.write<uintX_t>(DE.
Val, ELFT::Endianness);
1776 SHeader.sh_size = 2 *
sizeof(uintX_t) *
Section.Entries->size();
1779template <
class ELFT>
1780void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1782 ContiguousBlobAccumulator &CBA) {
1788 CBA.writeULEB128(toSymbolIndex(
Sym,
Section.Name,
false));
1791template <
class ELFT>
1792void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1794 ContiguousBlobAccumulator &CBA) {
1799 switch (
Section.AddressAlign) {
1813 if (CBA.getOffset() !=
alignTo(CBA.getOffset(),
Align)) {
1823 if (
NE.Name.empty())
1824 CBA.write<
uint32_t>(0, ELFT::Endianness);
1826 CBA.write<
uint32_t>(
NE.Name.size() + 1, ELFT::Endianness);
1829 if (
NE.Desc.binary_size() == 0)
1830 CBA.write<
uint32_t>(0, ELFT::Endianness);
1832 CBA.write<
uint32_t>(
NE.Desc.binary_size(), ELFT::Endianness);
1835 CBA.write<
uint32_t>(
NE.Type, ELFT::Endianness);
1838 if (!
NE.Name.empty()) {
1839 CBA.write(
NE.Name.data(),
NE.Name.size());
1844 if (
NE.Desc.binary_size() != 0) {
1845 CBA.padToAlignment(
Align);
1846 CBA.writeAsBinary(
NE.Desc);
1849 CBA.padToAlignment(
Align);
1852 SHeader.sh_size = CBA.tell() -
Offset;
1855template <
class ELFT>
1856void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1858 ContiguousBlobAccumulator &CBA) {
1880 if (
Section.Header->MaskWords)
1889 for (llvm::yaml::Hex64 Val : *
Section.BloomFilter)
1890 CBA.write<uintX_t>(Val, ELFT::Endianness);
1893 for (llvm::yaml::Hex32 Val : *
Section.HashBuckets)
1894 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1897 for (llvm::yaml::Hex32 Val : *
Section.HashValues)
1898 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1900 SHeader.sh_size = 16 +
1901 Section.BloomFilter->size() *
sizeof(
typename ELFT::uint) +
1902 Section.HashBuckets->size() * 4 +
1903 Section.HashValues->size() * 4;
1906template <
class ELFT>
1908 ContiguousBlobAccumulator &CBA) {
1909 size_t PatternSize = Fill.
Pattern ? Fill.
Pattern->binary_size() : 0;
1911 CBA.writeZeros(Fill.
Size);
1917 for (; Written + PatternSize <= Fill.
Size; Written += PatternSize)
1918 CBA.writeAsBinary(*Fill.
Pattern);
1919 CBA.writeAsBinary(*Fill.
Pattern, Fill.
Size - Written);
1922template <
class ELFT>
1925 Doc.getSectionHeaderTable();
1935 if (!
Ret.try_emplace(Hdr.Name, ++SecNdx).second)
1936 reportError(
"repeated section name: '" + Hdr.Name +
1937 "' in the section header description");
1951 if (S == Doc.getSections().front())
1955 "' should be present in the 'Sections' or 'Excluded' lists");
1959 for (
const auto &It : Seen)
1960 reportError(
"section header contains undefined section '" + It.getKey() +
1965template <
class ELFT>
void ELFState<ELFT>::buildSectionIndex() {
1974 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
1976 Doc.getSectionHeaderTable();
1979 if (!ExcludedSectionHeaders.insert(Hdr.
Name).second)
1982 if (SectionHeaders.
NoHeaders.value_or(
false))
1984 if (!ExcludedSectionHeaders.insert(S->
Name).second)
1992 if (!SN2I.addName(S->
Name, Index))
1995 if (!ExcludedSectionHeaders.count(S->
Name))
2000template <
class ELFT>
void ELFState<ELFT>::buildSymbolIndexes() {
2002 for (
size_t I = 0, S =
V.size();
I < S; ++
I) {
2004 if (!
Sym.Name.empty() && !
Map.addName(
Sym.Name,
I + 1))
2010 Build(*Doc.Symbols, SymN2I);
2011 if (Doc.DynamicSymbols)
2012 Build(*Doc.DynamicSymbols, DynSymN2I);
2015template <
class ELFT>
void ELFState<ELFT>::finalizeStrings() {
2020 DotStrtab.finalize();
2023 if (Doc.DynamicSymbols)
2030 if (
auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
2031 if (VerNeed->VerneedV) {
2033 DotDynstr.add(VE.
File);
2035 DotDynstr.add(Aux.
Name);
2038 }
else if (
auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
2039 if (VerDef->Entries)
2042 DotDynstr.add(
Name);
2046 DotDynstr.finalize();
2050 if (ShStrtabStrings != &DotStrtab && ShStrtabStrings != &DotDynstr)
2051 ShStrtabStrings->finalize();
2054template <
class ELFT>
2057 ELFState<ELFT> State(Doc, EH);
2063 State.buildSectionIndex();
2064 State.buildSymbolIndexes();
2069 State.finalizeStrings();
2074 std::vector<Elf_Phdr> PHeaders;
2075 State.initProgramHeaders(PHeaders);
2079 const size_t SectionContentBeginOffset =
2085 ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
2087 std::vector<Elf_Shdr> SHeaders;
2088 State.initSectionHeaders(SHeaders, CBA);
2091 State.setProgramHeaderLayout(PHeaders, SHeaders);
2093 bool ReachedLimit = CBA.getOffset() > MaxSize;
2094 if (
Error E = CBA.takeLimitError()) {
2097 ReachedLimit =
true;
2102 "the desired output size is greater than permitted. Use the "
2103 "--max-size option to change the limit");
2108 State.writeELFHeader(
OS);
2113 CBA.updateDataAt(*SHT.
Offset, SHeaders.data(),
2116 CBA.writeBlobToStream(
OS);
2129 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
2130 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
2133 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
2134 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")
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
constexpr unsigned CREL_HDR_ADDEND
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 tuples (A, B,...
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.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
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)
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
const char * toString(DWARFSectionKind Kind)
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
llvm::yaml::Hex64 Address
std::optional< std::vector< uint32_t > > Entries
std::optional< uint16_t > Flags
std::optional< uint16_t > VDAux
std::vector< StringRef > VerNames
std::optional< uint16_t > VersionNdx
std::optional< uint16_t > Version
std::optional< uint32_t > Hash
std::vector< VernauxEntry > AuxV
static Expected< Features > decode(uint8_t Val)
Common declarations for yaml2obj.