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) {
541 for (
size_t I = 0, E = Doc.Chunks.size();
I != E; ++
I) {
542 if (
auto S = dyn_cast<ELFYAML::Fill>(Doc.Chunks[
I].get()))
543 NameToFill[S->Name] = S;
544 NameToIndex[Doc.Chunks[
I]->Name] =
I + 1;
547 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
548 for (
size_t I = 0, E = Doc.ProgramHeaders.size();
I != E; ++
I) {
552 Phdr.p_type = YamlPhdr.
Type;
553 Phdr.p_flags = YamlPhdr.
Flags;
554 Phdr.p_vaddr = YamlPhdr.
VAddr;
555 Phdr.p_paddr = YamlPhdr.
PAddr;
556 PHeaders.push_back(Phdr);
565 "' by the 'FirstSec' key of the program header with index " +
570 "' by the 'LastSec' key of the program header with index " +
577 ": the section index of " + *YamlPhdr.
FirstSec +
578 " is greater than the index of " + *YamlPhdr.
LastSec);
581 YamlPhdr.
Chunks.push_back(Doc.Chunks[
I - 1].get());
591 if (!SN2I.lookup(S, Index) && !to_integer(S, Index)) {
593 reportError(
"unknown section referenced: '" + S +
"' by YAML symbol '" +
596 reportError(
"unknown section referenced: '" + S +
"' by YAML section '" +
602 Doc.getSectionHeaderTable();
609 size_t FirstExcluded =
611 if (Index > FirstExcluded) {
613 reportError(
"unable to link '" + LocSec +
"' to excluded section '" + S +
616 reportError(
"excluded section referenced: '" + S +
"' by symbol '" +
625 const NameToIdxMap &
SymMap = IsDynamic ? DynSymN2I : SymN2I;
629 if (!
SymMap.lookup(S, Index) && !to_integer(S, Index)) {
630 reportError(
"unknown symbol referenced: '" + S +
"' by YAML section '" +
641 if (
From->ShAddrAlign)
642 To.sh_addralign = *
From->ShAddrAlign;
644 To.sh_flags = *
From->ShFlags;
646 To.sh_name = *
From->ShName;
648 To.sh_offset = *
From->ShOffset;
650 To.sh_size = *
From->ShSize;
652 To.sh_type = *
From->ShType;
656bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA,
660 if (Header.sh_offset)
663 if (SecName ==
".strtab")
664 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec);
665 else if (SecName ==
".dynstr")
666 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec);
667 else if (SecName == SectionHeaderStringTableName)
668 initStrtabSectionHeader(Header, SecName, *ShStrtabStrings, CBA, YAMLSec);
669 else if (SecName ==
".symtab")
670 initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec);
671 else if (SecName ==
".dynsym")
672 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec);
676 if (YAMLSec && !isa<ELFYAML::RawContentSection>(YAMLSec))
678 initDWARFSectionHeader(Header, SecName, CBA, YAMLSec);
682 LocationCounter += Header.sh_size;
685 overrideFields<ELFT>(YAMLSec, Header);
695 std::string Ret =
Name.empty() ?
"" :
Name.str() +
' ';
710 return S.
substr(0, SuffixPos - 1);
717 if (ExcludedSectionHeaders.count(
Name))
719 return ShStrtabStrings->getOffset(
Name);
723 const std::optional<yaml::BinaryRef> &
Content,
724 const std::optional<llvm::yaml::Hex64> &
Size) {
725 size_t ContentSize = 0;
728 ContentSize =
Content->binary_size();
734 CBA.writeZeros(*
Size - ContentSize);
762void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
763 ContiguousBlobAccumulator &CBA) {
766 SHeaders.resize(Doc.getSections().size());
768 for (
const std::unique_ptr<ELFYAML::Chunk> &
D : Doc.Chunks) {
770 S->Offset = alignToOffset(CBA, 1, S->Offset);
772 LocationCounter += S->Size;
777 dyn_cast<ELFYAML::SectionHeaderTable>(
D.get())) {
778 if (S->NoHeaders.value_or(
false))
782 S->Offset = alignToOffset(CBA,
sizeof(
typename ELFT::uint),
785 S->Offset = alignToOffset(CBA, 1, S->Offset);
787 uint64_t Size = S->getNumHeaders(SHeaders.size()) *
sizeof(Elf_Shdr);
790 CBA.writeZeros(
Size);
791 LocationCounter +=
Size;
796 bool IsFirstUndefSection = Sec == Doc.getSections().front();
800 Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->
Name)];
802 SHeader.sh_link = toSectionIndex(*Sec->
Link, Sec->
Name);
806 if (!LinkSec.
empty() && !ExcludedSectionHeaders.count(LinkSec) &&
807 SN2I.lookup(LinkSec, Link))
808 SHeader.sh_link =
Link;
812 SHeader.sh_entsize = *Sec->
EntSize;
814 SHeader.sh_entsize = ELFYAML::getDefaultShEntSize<ELFT>(
821 if (initImplicitHeader(CBA, SHeader, Sec->
Name,
825 assert(Sec &&
"It can't be null unless it is an implicit section. But all "
826 "implicit sections should already have been handled above.");
830 SHeader.sh_type = Sec->
Type;
832 SHeader.sh_flags = *Sec->
Flags;
837 if (!IsFirstUndefSection || Sec->
Offset)
838 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->
Offset);
840 assignSectionAddress(SHeader, Sec);
842 if (IsFirstUndefSection) {
843 if (
auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
846 SHeader.sh_size = *RawSec->Size;
848 SHeader.sh_info = *RawSec->Info;
851 LocationCounter += SHeader.sh_size;
852 overrideFields<ELFT>(Sec, SHeader);
856 if (!isa<ELFYAML::NoBitsSection>(Sec) && (Sec->
Content || Sec->
Size))
859 if (
auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
860 writeSectionContent(SHeader, *S, CBA);
861 }
else if (
auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) {
862 writeSectionContent(SHeader, *S, CBA);
863 }
else if (
auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
864 writeSectionContent(SHeader, *S, CBA);
865 }
else if (
auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) {
866 writeSectionContent(SHeader, *S, CBA);
867 }
else if (
auto S = dyn_cast<ELFYAML::GroupSection>(Sec)) {
868 writeSectionContent(SHeader, *S, CBA);
869 }
else if (
auto S = dyn_cast<ELFYAML::ARMIndexTableSection>(Sec)) {
870 writeSectionContent(SHeader, *S, CBA);
871 }
else if (
auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) {
872 writeSectionContent(SHeader, *S, CBA);
873 }
else if (
auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) {
874 writeSectionContent(SHeader, *S, CBA);
875 }
else if (
auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) {
876 writeSectionContent(SHeader, *S, CBA);
877 }
else if (
auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) {
878 writeSectionContent(SHeader, *S, CBA);
879 }
else if (
auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
880 writeSectionContent(SHeader, *S, CBA);
881 }
else if (
auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
882 writeSectionContent(SHeader, *S, CBA);
883 }
else if (
auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) {
884 writeSectionContent(SHeader, *S, CBA);
885 }
else if (
auto S = dyn_cast<ELFYAML::HashSection>(Sec)) {
886 writeSectionContent(SHeader, *S, CBA);
887 }
else if (
auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) {
888 writeSectionContent(SHeader, *S, CBA);
889 }
else if (
auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) {
890 writeSectionContent(SHeader, *S, CBA);
891 }
else if (
auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) {
892 writeSectionContent(SHeader, *S, CBA);
893 }
else if (
auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) {
894 writeSectionContent(SHeader, *S, CBA);
895 }
else if (
auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) {
896 writeSectionContent(SHeader, *S, CBA);
897 }
else if (
auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) {
898 writeSectionContent(SHeader, *S, CBA);
899 }
else if (
auto S = dyn_cast<ELFYAML::BBAddrMapSection>(Sec)) {
900 writeSectionContent(SHeader, *S, CBA);
905 LocationCounter += SHeader.sh_size;
908 overrideFields<ELFT>(Sec, SHeader);
913void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader,
915 if (YAMLSec && YAMLSec->
Address) {
916 SHeader.sh_addr = *YAMLSec->
Address;
917 LocationCounter = *YAMLSec->
Address;
929 alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1);
930 SHeader.sh_addr = LocationCounter;
934 for (
size_t I = 0;
I < Symbols.size(); ++
I)
937 return Symbols.size();
941std::vector<typename ELFT::Sym>
944 std::vector<Elf_Sym>
Ret;
945 Ret.resize(Symbols.size() + 1);
956 else if (!
Sym.Name.empty())
961 Symbol.st_shndx = toSectionIndex(*
Sym.Section,
"",
Sym.Name);
965 Symbol.st_value =
Sym.Value.value_or(yaml::Hex64(0));
967 Symbol.st_size =
Sym.Size.value_or(yaml::Hex64(0));
974void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
976 ContiguousBlobAccumulator &CBA,
979 bool IsStatic = STType == SymtabType::Static;
981 if (IsStatic && Doc.Symbols)
982 Symbols = *Doc.Symbols;
983 else if (!IsStatic && Doc.DynamicSymbols)
984 Symbols = *Doc.DynamicSymbols;
987 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
989 bool HasSymbolsDescription =
990 (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols);
991 if (HasSymbolsDescription) {
992 StringRef Property = (IsStatic ?
"`Symbols`" :
"`DynamicSymbols`");
994 reportError(
"cannot specify both `Content` and " + Property +
995 " for symbol table section '" + RawSec->
Name +
"'");
997 reportError(
"cannot specify both `Size` and " + Property +
998 " for symbol table section '" + RawSec->
Name +
"'");
1003 SHeader.sh_name = getSectionNameOffset(IsStatic ?
".symtab" :
".dynsym");
1006 SHeader.sh_type = YAMLSec->
Type;
1010 if (YAMLSec && YAMLSec->
Flags)
1011 SHeader.sh_flags = *YAMLSec->
Flags;
1017 SHeader.sh_info = (RawSec && RawSec->
Info) ? (
unsigned)(*RawSec->
Info)
1021 assignSectionAddress(SHeader, YAMLSec);
1023 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1024 RawSec ? RawSec->
Offset : std::nullopt);
1026 if (RawSec && (RawSec->
Content || RawSec->
Size)) {
1032 std::vector<Elf_Sym> Syms =
1033 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr);
1034 SHeader.sh_size = Syms.size() *
sizeof(Elf_Sym);
1035 CBA.write((
const char *)Syms.data(), SHeader.sh_size);
1038template <
class ELFT>
1039void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
1041 ContiguousBlobAccumulator &CBA,
1048 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1050 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1051 YAMLSec ? YAMLSec->
Offset : std::nullopt);
1053 if (RawSec && (RawSec->
Content || RawSec->
Size)) {
1058 SHeader.sh_size = STB.
getSize();
1061 if (RawSec && RawSec->
Info)
1062 SHeader.sh_info = *RawSec->
Info;
1064 if (YAMLSec && YAMLSec->
Flags)
1065 SHeader.sh_flags = *YAMLSec->
Flags;
1066 else if (
Name ==
".dynstr")
1069 assignSectionAddress(SHeader, YAMLSec);
1074 return Name.consume_front(
".") && DebugSecNames.
count(
Name);
1077template <
class ELFT>
1080 ContiguousBlobAccumulator &CBA) {
1091 if (
Error Err = EmitFunc(*
OS, DWARF))
1092 return std::move(Err);
1094 return CBA.tell() - BeginOffset;
1097template <
class ELFT>
1098void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader,
StringRef Name,
1099 ContiguousBlobAccumulator &CBA,
1104 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign,
1105 YAMLSec ? YAMLSec->
Offset : std::nullopt);
1108 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
1112 "' contents in the 'DWARF' entry and the 'Content' "
1113 "or 'Size' in the 'Sections' entry at the same time");
1116 emitDWARF<ELFT>(SHeader,
Name, *Doc.DWARF, CBA))
1117 SHeader.sh_size = *ShSizeOrErr;
1125 "entry or a RawContentSection");
1127 if (RawSec && RawSec->
Info)
1128 SHeader.sh_info = *RawSec->
Info;
1130 if (YAMLSec && YAMLSec->
Flags)
1131 SHeader.sh_flags = *YAMLSec->
Flags;
1132 else if (
Name ==
".debug_str")
1135 assignSectionAddress(SHeader, YAMLSec);
1138template <
class ELFT>
void ELFState<ELFT>::reportError(
const Twine &Msg) {
1143template <
class ELFT>
void ELFState<ELFT>::reportError(
Error Err) {
1149template <
class ELFT>
1150std::vector<Fragment>
1153 std::vector<Fragment>
Ret;
1162 const Elf_Shdr &
H = SHeaders[SN2I.get(S->
Name)];
1163 Ret.push_back({
H.sh_offset,
H.sh_size,
H.sh_type,
H.sh_addralign});
1168template <
class ELFT>
1169void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
1170 std::vector<Elf_Shdr> &SHeaders) {
1172 for (
auto &YamlPhdr : Doc.ProgramHeaders) {
1173 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
1174 std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders);
1176 return A.Offset <
B.Offset;
1178 reportError(
"sections in the program header with index " +
1179 Twine(PhdrIdx) +
" are not sorted by their file offset");
1182 if (!Fragments.empty() && *YamlPhdr.
Offset > Fragments.front().Offset)
1184 " must be less than or equal to the minimum file offset of "
1185 "all included sections (0x" +
1187 PHeader.p_offset = *YamlPhdr.
Offset;
1188 }
else if (!Fragments.empty()) {
1189 PHeader.p_offset = Fragments.front().Offset;
1194 PHeader.p_filesz = *YamlPhdr.
FileSize;
1195 }
else if (!Fragments.empty()) {
1196 uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset;
1201 FileSize += Fragments.back().Size;
1202 PHeader.p_filesz = FileSize;
1206 uint64_t MemOffset = PHeader.p_offset;
1207 for (
const Fragment &
F : Fragments)
1208 MemOffset = std::max(MemOffset,
F.Offset +
F.Size);
1211 : MemOffset - PHeader.p_offset;
1213 if (YamlPhdr.
Align) {
1214 PHeader.p_align = *YamlPhdr.
Align;
1219 PHeader.p_align = 1;
1220 for (
const Fragment &
F : Fragments)
1221 PHeader.p_align = std::max((
uint64_t)PHeader.p_align,
F.AddrAlign);
1232 return (isa<ELFYAML::Fill>(C) ||
1233 cast<ELFYAML::Section>(C)->Type != ELF::SHT_NOBITS);
1240template <
class ELFT>
1241void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1243 ContiguousBlobAccumulator &CBA) {
1247 SHeader.sh_size = *S.
Size;
1252 if (shouldAllocateFileSpace(Doc.ProgramHeaders, S))
1253 CBA.writeZeros(*S.
Size);
1256template <
class ELFT>
1257void ELFState<ELFT>::writeSectionContent(
1259 ContiguousBlobAccumulator &CBA) {
1261 SHeader.sh_info = *Section.Info;
1270template <
class ELFT>
1271void ELFState<ELFT>::writeSectionContent(
1273 ContiguousBlobAccumulator &CBA) {
1277 "Section type is not SHT_REL nor SHT_RELA");
1279 if (!
Section.RelocatableSec.empty())
1280 SHeader.sh_info = toSectionIndex(
Section.RelocatableSec,
Section.Name);
1287 typename ELFT::uint OffsetMask = 8,
Offset = 0, Addend = 0;
1289 uint64_t CurrentOffset = CBA.getOffset();
1292 OffsetMask |= Rel.
Offset;
1298 const bool IsDynamic =
Section.Link && (*
Section.Link ==
".dynsym");
1306 (
static_cast<typename ELFT::uint
>(Rel.
Offset) -
Offset) >> Shift;
1309 DeltaOffset * 8 + (SymIdx != CurSymIdx) + (
Type != Rel.
Type ? 2 : 0) +
1310 (Addend !=
static_cast<typename ELFT::uint
>(Rel.
Addend) ? 4 : 0);
1311 if (DeltaOffset < 0x10) {
1314 CBA.write(
B | 0x80);
1315 CBA.writeULEB128(DeltaOffset >> 4);
1320 std::make_signed_t<typename ELFT::uint>(CurSymIdx - SymIdx));
1324 CBA.writeSLEB128(
static_cast<int32_t
>(Rel.
Type -
Type));
1329 std::make_signed_t<typename ELFT::uint>(Rel.
Addend - Addend));
1332 }
else if (IsRela) {
1335 REntry.r_offset = Rel.
Offset;
1336 REntry.r_addend = Rel.
Addend;
1338 CBA.write((
const char *)&REntry,
sizeof(REntry));
1342 REntry.r_offset = Rel.
Offset;
1344 CBA.write((
const char *)&REntry,
sizeof(REntry));
1348 SHeader.sh_size = CBA.getOffset() - CurrentOffset;
1351template <
class ELFT>
1352void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1354 ContiguousBlobAccumulator &CBA) {
1358 for (llvm::yaml::Hex64 E : *
Section.Entries) {
1359 if (!ELFT::Is64Bits && E > UINT32_MAX)
1362 CBA.write<uintX_t>(E, ELFT::Endianness);
1365 SHeader.sh_size =
sizeof(uintX_t) *
Section.Entries->size();
1368template <
class ELFT>
1369void ELFState<ELFT>::writeSectionContent(
1371 ContiguousBlobAccumulator &CBA) {
1381 CBA.write<
uint32_t>(E, ELFT::Endianness);
1382 SHeader.sh_size = Shndx.
Entries->size() * SHeader.sh_entsize;
1385template <
class ELFT>
1386void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1388 ContiguousBlobAccumulator &CBA) {
1390 "Section type is not SHT_GROUP");
1400 unsigned int SectionIndex = 0;
1401 if (
Member.sectionNameOrType ==
"GRP_COMDAT")
1404 SectionIndex = toSectionIndex(
Member.sectionNameOrType,
Section.Name);
1405 CBA.write<
uint32_t>(SectionIndex, ELFT::Endianness);
1407 SHeader.sh_size = SHeader.sh_entsize *
Section.Members->size();
1410template <
class ELFT>
1411void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1413 ContiguousBlobAccumulator &CBA) {
1419 SHeader.sh_size =
Section.Entries->size() * SHeader.sh_entsize;
1422template <
class ELFT>
1423void ELFState<ELFT>::writeSectionContent(
1425 ContiguousBlobAccumulator &CBA) {
1430 CBA.write<uintX_t>(E.
Address, ELFT::Endianness);
1431 SHeader.sh_size +=
sizeof(uintX_t) + CBA.writeULEB128(E.
Size);
1435template <
class ELFT>
1436void ELFState<ELFT>::writeSectionContent(
1438 ContiguousBlobAccumulator &CBA) {
1442 <<
"PGOAnalyses should not exist in SHT_LLVM_BB_ADDR_MAP when "
1443 "Entries does not exist";
1447 const std::vector<ELFYAML::PGOAnalysisMapEntry> *PGOAnalyses =
nullptr;
1451 "in SHT_LLVM_BB_ADDR_MAP";
1453 PGOAnalyses = &
Section.PGOAnalyses.value();
1461 <<
static_cast<int>(E.Version)
1462 <<
"; encoding using the most recent version";
1463 CBA.
write(E.Version);
1464 CBA.
write(E.Feature);
1465 SHeader.sh_size += 2;
1468 bool MultiBBRangeFeatureEnabled =
false;
1472 MultiBBRangeFeatureEnabled = FeatureOrErr->MultiBBRange;
1474 MultiBBRangeFeatureEnabled ||
1475 (E.NumBBRanges.has_value() && E.NumBBRanges.value() != 1) ||
1476 (E.BBRanges && E.BBRanges->size() != 1);
1477 if (MultiBBRange && !MultiBBRangeFeatureEnabled)
1479 <<
") does not support multiple BB ranges.";
1484 E.NumBBRanges.value_or(E.BBRanges ? E.BBRanges->size() : 0);
1485 SHeader.sh_size += CBA.writeULEB128(NumBBRanges);
1492 CBA.write<uintX_t>(BBR.
BaseAddress, ELFT::Endianness);
1498 SHeader.sh_size +=
sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
1500 if (!BBR.
BBEntries || FeatureOrErr->OmitBBEntries)
1505 SHeader.sh_size += CBA.writeULEB128(BBE.
ID);
1507 SHeader.sh_size += CBA.writeULEB128(BBE.
Size);
1508 SHeader.sh_size += CBA.writeULEB128(BBE.
Metadata);
1521 const auto &PGOBBEntries = PGOEntry.
PGOBBEntries.value();
1522 if (TotalNumBlocks != PGOBBEntries.size()) {
1524 "BBEntries in SHT_LLVM_BB_ADDR_MAP.\n"
1525 <<
"Mismatch on function with address: "
1526 << E.getFunctionAddress();
1530 for (
const auto &PGOBBE : PGOBBEntries) {
1532 SHeader.sh_size += CBA.writeULEB128(*PGOBBE.BBFreq);
1533 if (PGOBBE.Successors) {
1534 SHeader.sh_size += CBA.writeULEB128(PGOBBE.Successors->size());
1535 for (
const auto &[
ID,
BrProb] : *PGOBBE.Successors) {
1536 SHeader.sh_size += CBA.writeULEB128(
ID);
1537 SHeader.sh_size += CBA.writeULEB128(
BrProb);
1544template <
class ELFT>
1545void ELFState<ELFT>::writeSectionContent(
1547 ContiguousBlobAccumulator &CBA) {
1552 CBA.write(
LO.Key.data(),
LO.Key.size());
1554 CBA.write(
LO.Value.data(),
LO.Value.size());
1556 SHeader.sh_size += (
LO.Key.size() +
LO.Value.size() + 2);
1560template <
class ELFT>
1561void ELFState<ELFT>::writeSectionContent(
1563 ContiguousBlobAccumulator &CBA) {
1568 CBA.write(
Lib.data(),
Lib.size());
1570 SHeader.sh_size +=
Lib.size() + 1;
1574template <
class ELFT>
1576ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA,
uint64_t Align,
1577 std::optional<llvm::yaml::Hex64>
Offset) {
1578 uint64_t CurrentOffset = CBA.getOffset();
1585 return CurrentOffset;
1594 CBA.writeZeros(AlignedOffset - CurrentOffset);
1595 return AlignedOffset;
1598template <
class ELFT>
1599void ELFState<ELFT>::writeSectionContent(
1601 ContiguousBlobAccumulator &CBA) {
1611template <
class ELFT>
1612void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1614 ContiguousBlobAccumulator &CBA) {
1619 Section.NBucket.value_or(llvm::yaml::Hex64(
Section.Bucket->size())),
1622 Section.NChain.value_or(llvm::yaml::Hex64(
Section.Chain->size())),
1626 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1628 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1630 SHeader.sh_size = (2 +
Section.Bucket->size() +
Section.Chain->size()) * 4;
1633template <
class ELFT>
1634void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1636 ContiguousBlobAccumulator &CBA) {
1639 SHeader.sh_info = *
Section.Info;
1641 SHeader.sh_info =
Section.Entries->size();
1647 for (
size_t I = 0;
I <
Section.Entries->size(); ++
I) {
1651 VerDef.vd_version = E.
Version.value_or(1);
1652 VerDef.vd_flags = E.
Flags.value_or(0);
1654 VerDef.vd_hash = E.
Hash.value_or(0);
1655 VerDef.vd_aux = E.
VDAux.value_or(
sizeof(Elf_Verdef));
1657 if (
I ==
Section.Entries->size() - 1)
1661 sizeof(Elf_Verdef) + E.
VerNames.size() *
sizeof(Elf_Verdaux);
1662 CBA.write((
const char *)&VerDef,
sizeof(Elf_Verdef));
1664 for (
size_t J = 0; J < E.
VerNames.size(); ++J, ++AuxCnt) {
1665 Elf_Verdaux VerdAux;
1666 VerdAux.vda_name = DotDynstr.getOffset(E.
VerNames[J]);
1668 VerdAux.vda_next = 0;
1670 VerdAux.vda_next =
sizeof(Elf_Verdaux);
1671 CBA.write((
const char *)&VerdAux,
sizeof(Elf_Verdaux));
1675 SHeader.sh_size =
Section.Entries->size() *
sizeof(Elf_Verdef) +
1676 AuxCnt *
sizeof(Elf_Verdaux);
1679template <
class ELFT>
1680void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1682 ContiguousBlobAccumulator &CBA) {
1684 SHeader.sh_info = *
Section.Info;
1686 SHeader.sh_info =
Section.VerneedV->size();
1692 for (
size_t I = 0;
I <
Section.VerneedV->size(); ++
I) {
1695 Elf_Verneed VerNeed;
1696 VerNeed.vn_version = VE.
Version;
1697 VerNeed.vn_file = DotDynstr.getOffset(VE.
File);
1698 if (
I ==
Section.VerneedV->size() - 1)
1699 VerNeed.vn_next = 0;
1702 sizeof(Elf_Verneed) + VE.
AuxV.size() *
sizeof(Elf_Vernaux);
1703 VerNeed.vn_cnt = VE.
AuxV.size();
1704 VerNeed.vn_aux =
sizeof(Elf_Verneed);
1705 CBA.write((
const char *)&VerNeed,
sizeof(Elf_Verneed));
1707 for (
size_t J = 0; J < VE.
AuxV.size(); ++J, ++AuxCnt) {
1710 Elf_Vernaux VernAux;
1711 VernAux.vna_hash = VAuxE.
Hash;
1712 VernAux.vna_flags = VAuxE.
Flags;
1713 VernAux.vna_other = VAuxE.
Other;
1714 VernAux.vna_name = DotDynstr.getOffset(VAuxE.
Name);
1715 if (J == VE.
AuxV.size() - 1)
1716 VernAux.vna_next = 0;
1718 VernAux.vna_next =
sizeof(Elf_Vernaux);
1719 CBA.write((
const char *)&VernAux,
sizeof(Elf_Vernaux));
1723 SHeader.sh_size =
Section.VerneedV->size() *
sizeof(Elf_Verneed) +
1724 AuxCnt *
sizeof(Elf_Vernaux);
1727template <
class ELFT>
1728void ELFState<ELFT>::writeSectionContent(
1730 ContiguousBlobAccumulator &CBA) {
1738 SHeader.sh_size =
Section.Entries->size() * 8;
1741template <
class ELFT>
1742void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1744 ContiguousBlobAccumulator &CBA) {
1746 "Section type is not SHT_MIPS_ABIFLAGS");
1750 SHeader.sh_size = SHeader.sh_entsize;
1763 CBA.write((
const char *)&Flags,
sizeof(Flags));
1766template <
class ELFT>
1767void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1769 ContiguousBlobAccumulator &CBA) {
1771 "Section type is not SHT_DYNAMIC");
1777 CBA.write<uintX_t>(DE.
Tag, ELFT::Endianness);
1778 CBA.write<uintX_t>(DE.
Val, ELFT::Endianness);
1780 SHeader.sh_size = 2 *
sizeof(uintX_t) *
Section.Entries->size();
1783template <
class ELFT>
1784void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1786 ContiguousBlobAccumulator &CBA) {
1792 CBA.writeULEB128(toSymbolIndex(
Sym,
Section.Name,
false));
1795template <
class ELFT>
1796void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1798 ContiguousBlobAccumulator &CBA) {
1803 switch (
Section.AddressAlign) {
1817 if (CBA.getOffset() !=
alignTo(CBA.getOffset(),
Align)) {
1827 if (
NE.Name.empty())
1828 CBA.write<
uint32_t>(0, ELFT::Endianness);
1830 CBA.write<
uint32_t>(
NE.Name.size() + 1, ELFT::Endianness);
1833 if (
NE.Desc.binary_size() == 0)
1834 CBA.write<
uint32_t>(0, ELFT::Endianness);
1836 CBA.write<
uint32_t>(
NE.Desc.binary_size(), ELFT::Endianness);
1839 CBA.write<
uint32_t>(
NE.Type, ELFT::Endianness);
1842 if (!
NE.Name.empty()) {
1843 CBA.write(
NE.Name.data(),
NE.Name.size());
1848 if (
NE.Desc.binary_size() != 0) {
1849 CBA.padToAlignment(
Align);
1850 CBA.writeAsBinary(
NE.Desc);
1853 CBA.padToAlignment(
Align);
1856 SHeader.sh_size = CBA.tell() -
Offset;
1859template <
class ELFT>
1860void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1862 ContiguousBlobAccumulator &CBA) {
1884 if (
Section.Header->MaskWords)
1893 for (llvm::yaml::Hex64 Val : *
Section.BloomFilter)
1894 CBA.write<uintX_t>(Val, ELFT::Endianness);
1897 for (llvm::yaml::Hex32 Val : *
Section.HashBuckets)
1898 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1901 for (llvm::yaml::Hex32 Val : *
Section.HashValues)
1902 CBA.write<
uint32_t>(Val, ELFT::Endianness);
1904 SHeader.sh_size = 16 +
1905 Section.BloomFilter->size() *
sizeof(
typename ELFT::uint) +
1906 Section.HashBuckets->size() * 4 +
1907 Section.HashValues->size() * 4;
1910template <
class ELFT>
1912 ContiguousBlobAccumulator &CBA) {
1913 size_t PatternSize = Fill.
Pattern ? Fill.
Pattern->binary_size() : 0;
1915 CBA.writeZeros(Fill.
Size);
1921 for (; Written + PatternSize <= Fill.
Size; Written += PatternSize)
1922 CBA.writeAsBinary(*Fill.
Pattern);
1923 CBA.writeAsBinary(*Fill.
Pattern, Fill.
Size - Written);
1926template <
class ELFT>
1929 Doc.getSectionHeaderTable();
1939 if (!
Ret.try_emplace(Hdr.Name, ++SecNdx).second)
1940 reportError(
"repeated section name: '" + Hdr.Name +
1941 "' in the section header description");
1955 if (S == Doc.getSections().front())
1959 "' should be present in the 'Sections' or 'Excluded' lists");
1963 for (
const auto &It : Seen)
1964 reportError(
"section header contains undefined section '" + It.getKey() +
1969template <
class ELFT>
void ELFState<ELFT>::buildSectionIndex() {
1978 std::vector<ELFYAML::Section *> Sections = Doc.getSections();
1980 Doc.getSectionHeaderTable();
1983 if (!ExcludedSectionHeaders.insert(Hdr.
Name).second)
1986 if (SectionHeaders.
NoHeaders.value_or(
false))
1988 if (!ExcludedSectionHeaders.insert(S->
Name).second)
1996 if (!SN2I.addName(S->
Name, Index))
1999 if (!ExcludedSectionHeaders.count(S->
Name))
2004template <
class ELFT>
void ELFState<ELFT>::buildSymbolIndexes() {
2006 for (
size_t I = 0, S =
V.size();
I < S; ++
I) {
2008 if (!
Sym.Name.empty() && !
Map.addName(
Sym.Name,
I + 1))
2014 Build(*Doc.Symbols, SymN2I);
2015 if (Doc.DynamicSymbols)
2016 Build(*Doc.DynamicSymbols, DynSymN2I);
2019template <
class ELFT>
void ELFState<ELFT>::finalizeStrings() {
2024 DotStrtab.finalize();
2027 if (Doc.DynamicSymbols)
2034 if (
auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) {
2035 if (VerNeed->VerneedV) {
2037 DotDynstr.add(VE.
File);
2039 DotDynstr.add(Aux.
Name);
2042 }
else if (
auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) {
2043 if (VerDef->Entries)
2046 DotDynstr.add(
Name);
2050 DotDynstr.finalize();
2054 if (ShStrtabStrings != &DotStrtab && ShStrtabStrings != &DotDynstr)
2055 ShStrtabStrings->finalize();
2058template <
class ELFT>
2061 ELFState<ELFT> State(Doc, EH);
2067 State.buildSectionIndex();
2068 State.buildSymbolIndexes();
2073 State.finalizeStrings();
2078 std::vector<Elf_Phdr> PHeaders;
2079 State.initProgramHeaders(PHeaders);
2083 const size_t SectionContentBeginOffset =
2089 ContiguousBlobAccumulator CBA(SectionContentBeginOffset, MaxSize);
2091 std::vector<Elf_Shdr> SHeaders;
2092 State.initSectionHeaders(SHeaders, CBA);
2095 State.setProgramHeaderLayout(PHeaders, SHeaders);
2097 bool ReachedLimit = CBA.getOffset() > MaxSize;
2098 if (
Error E = CBA.takeLimitError()) {
2101 ReachedLimit =
true;
2106 "the desired output size is greater than permitted. Use the "
2107 "--max-size option to change the limit");
2112 State.writeELFHeader(
OS);
2117 CBA.updateDataAt(*SHT.
Offset, SHeaders.data(),
2120 CBA.writeBlobToStream(
OS);
2133 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH, MaxSize);
2134 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH, MaxSize);
2137 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH, MaxSize);
2138 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.