29 if (DwarfVersion <= 4)
41 while (AbbrevData.
getULEB128(&Offset) != AbbrCode) {
45 AbbrevData.
getU8(&Offset);
56 if (
Form == dwarf::DW_FORM_string)
57 return InfoData.
getCStr(&InfoOffset);
60 case dwarf::DW_FORM_strx1:
61 StrIndex = InfoData.
getU8(&InfoOffset);
63 case dwarf::DW_FORM_strx2:
64 StrIndex = InfoData.
getU16(&InfoOffset);
66 case dwarf::DW_FORM_strx3:
67 StrIndex = InfoData.
getU24(&InfoOffset);
69 case dwarf::DW_FORM_strx4:
70 StrIndex = InfoData.
getU32(&InfoOffset);
72 case dwarf::DW_FORM_strx:
73 case dwarf::DW_FORM_GNU_str_index:
77 return make_error<DWPError>(
78 "string field must be encoded with one of the following: "
79 "DW_FORM_string, DW_FORM_strx, DW_FORM_strx1, DW_FORM_strx2, "
80 "DW_FORM_strx3, DW_FORM_strx4, or DW_FORM_GNU_str_index.");
83 uint64_t StrOffsetsOffset = 4 * StrIndex;
88 return StrData.
getCStr(&StrOffset);
96 if (Header.
Version >= 5 && Header.
UnitType != dwarf::DW_UT_split_compile)
97 return make_error<DWPError>(
98 std::string(
"unit type DW_UT_split_compile type not found in "
99 "debug_info header. Unexpected unit type 0x" +
100 utostr(Header.
UnitType) +
" found"));
108 if (Tag != dwarf::DW_TAG_compile_unit)
109 return make_error<DWPError>(
"top level DIE is not a compile unit");
111 AbbrevData.
getU8(&AbbrevOffset);
119 case dwarf::DW_AT_name: {
121 Form, InfoData, Offset, StrOffsets, Str, Header.
Version);
127 case dwarf::DW_AT_GNU_dwo_name:
128 case dwarf::DW_AT_dwo_name: {
130 Form, InfoData, Offset, StrOffsets, Str, Header.
Version);
136 case dwarf::DW_AT_GNU_dwo_id:
141 Form, InfoData, &Offset,
146 return make_error<DWPError>(
"compile unit missing dwo_id");
167 return Index + DW_SECT_INFO;
173 const auto *Off = Entry.getContribution(
Kind);
176 return Section.substr(Off->Offset, Off->Length);
184 uint32_t &TypesOffset,
unsigned TypesContributionIndex) {
187 auto *
I =
E.getContributions();
190 auto P = TypeIndexEntries.
insert(std::make_pair(
E.getSignature(), TUEntry));
193 auto &Entry =
P.first->second;
195 Entry.Contributions[0] = {};
201 C.Offset +=
I->Offset;
202 C.Length =
I->Length;
205 auto &
C = Entry.Contributions[TypesContributionIndex];
209 C.Offset = TypesOffset;
210 TypesOffset +=
C.Length;
216 MCSection *OutputTypes,
const std::vector<StringRef> &TypesSections,
222 while (
Data.isValidOffset(Offset)) {
225 Entry.Contributions[0] = {};
227 C.Offset = TypesOffset;
228 auto PrevOffset = Offset;
230 C.Length =
Data.getU32(&Offset) + 4;
232 Data.getU16(&Offset);
233 Data.getU32(&Offset);
235 auto Signature =
Data.getU64(&Offset);
236 Offset = PrevOffset +
C.Length;
238 auto P = TypeIndexEntries.
insert(std::make_pair(Signature, Entry));
242 Out.
emitBytes(Types.substr(PrevOffset,
C.Length));
243 TypesOffset +=
C.Length;
250 std::string Text =
"\'";
253 if (!DWPName.
empty()) {
255 if (!DWOName.
empty()) {
268 return make_error<DWPError>(
269 (
"failure while decompressing compressed section: '" +
Name +
"', " +
285 UncompressedSections.emplace_back();
286 if (
Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
290 Contents = UncompressedSections.
back();
304 return make_error<DWPError>(
"cannot parse compile unit length: " +
308 return make_error<DWPError>(
309 "compile unit exceeds .debug_info section range: " +
310 utostr(Offset + Header.
Length) +
" >= " + utostr(InfoData.
size()));
315 return make_error<DWPError>(
"cannot parse compile unit version: " +
322 MinHeaderLength = 16;
327 if (Header.
Length < MinHeaderLength) {
328 return make_error<DWPError>(
"unit length is too small: expected at least " +
329 utostr(MinHeaderLength) +
" got " +
330 utostr(Header.
Length) +
".");
337 if (Header.
UnitType == dwarf::DW_UT_split_type) {
339 MinHeaderLength += 4;
340 if (Header.
Length < MinHeaderLength)
341 return make_error<DWPError>(
"type unit is missing type offset");
361 if (CurStrSection.
empty() || CurStrOffsetSection.
empty())
369 while (
const char *
S =
Data.getCStr(&LocalOffset)) {
370 OffsetRemapping[PrevOffset] =
371 Strings.getOffset(
S, LocalOffset - PrevOffset);
372 PrevOffset = LocalOffset;
383 assert(
HeaderSize <= Size &&
"StrOffsetSection size is less than its header");
386 while (Offset < Size) {
387 auto OldOffset =
Data.getU32(&Offset);
388 auto NewOffset = OffsetRemapping[OldOffset];
397 for (
const auto &
E : IndexEntries)
399 if (ContributionOffsets[
I])
407 if (IndexEntries.
empty())
410 unsigned Columns = 0;
411 for (
auto &
C : ContributionOffsets)
418 for (
const auto &
P : IndexEntries) {
421 auto HP = ((
S >> 32) &
Mask) | 1;
423 assert(
S != IndexEntries.begin()[Buckets[
H] - 1].first &&
438 for (
const auto &
I : Buckets)
442 for (
const auto &
I : Buckets)
446 for (
size_t I = 0;
I != ContributionOffsets.size(); ++
I)
447 if (ContributionOffsets[
I])
461 return make_error<DWPError>(
462 std::string(
"duplicate DWO ID (") + utohexstr(PrevE.first) +
") in " +
464 PrevE.second.DWOName) +
469 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
477 std::vector<StringRef> &CurTypesSection,
478 std::vector<StringRef> &CurInfoSection,
StringRef &AbbrevSection,
480 std::vector<std::pair<DWARFSectionKind, uint32_t>> &SectionLength) {
484 if (Section.isVirtual())
502 auto SectionPair = KnownSections.find(
Name);
503 if (SectionPair == KnownSections.end())
508 SectionLength.push_back(std::make_pair(
Kind, Contents.
size()));
511 if (
Kind == DW_SECT_ABBREV) {
512 AbbrevSection = Contents;
516 MCSection *OutSection = SectionPair->second.first;
517 if (OutSection == StrOffsetSection)
518 CurStrOffsetSection = Contents;
519 else if (OutSection == StrSection)
520 CurStrSection = Contents;
521 else if (OutSection == TypesSection)
522 CurTypesSection.push_back(Contents);
523 else if (OutSection == CUIndexSection)
524 CurCUIndexSection = Contents;
525 else if (OutSection == TUIndexSection)
526 CurTUIndexSection = Contents;
527 else if (OutSection == InfoSection)
528 CurInfoSection.push_back(Contents);
538 MCSection *
const StrSection = MCOFI.getDwarfStrDWOSection();
539 MCSection *
const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
540 MCSection *
const TypesSection = MCOFI.getDwarfTypesDWOSection();
541 MCSection *
const CUIndexSection = MCOFI.getDwarfCUIndexSection();
542 MCSection *
const TUIndexSection = MCOFI.getDwarfTUIndexSection();
543 MCSection *
const InfoSection = MCOFI.getDwarfInfoDWOSection();
545 {
"debug_info.dwo", {InfoSection, DW_SECT_INFO}},
547 {
"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
550 {
"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
551 {
"debug_macro.dwo", {MCOFI.getDwarfMacroDWOSection(), DW_SECT_MACRO}},
552 {
"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
553 {
"debug_loclists.dwo",
554 {MCOFI.getDwarfLoclistsDWOSection(), DW_SECT_LOCLISTS}},
555 {
"debug_rnglists.dwo",
556 {MCOFI.getDwarfRnglistsDWOSection(), DW_SECT_RNGLISTS}},
563 uint32_t ContributionOffsets[8] = {};
572 std::deque<SmallString<32>> UncompressedSections;
574 for (
const auto &Input : Inputs) {
577 return ErrOrObj.takeError();
579 auto &Obj = *ErrOrObj->getBinary();
586 std::vector<StringRef> CurTypesSection;
587 std::vector<StringRef> CurInfoSection;
595 std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLength;
597 for (
const auto &Section : Obj.sections())
599 KnownSections, StrSection, StrOffsetSection, TypesSection,
600 CUIndexSection, TUIndexSection, InfoSection, Section, Out,
601 UncompressedSections, ContributionOffsets, CurEntry,
602 CurStrSection, CurStrOffsetSection, CurTypesSection,
603 CurInfoSection, AbbrevSection, CurCUIndexSection,
604 CurTUIndexSection, SectionLength))
607 if (CurInfoSection.empty())
618 IndexVersion =
Version < 5 ? 2 : 5;
620 return make_error<DWPError>(
"incompatible DWARF compile unit versions.");
624 CurStrOffsetSection, Header.
Version);
626 for (
auto Pair : SectionLength) {
629 ContributionOffsets[Index] +=
635 if (CurCUIndexSection.
empty()) {
636 bool FoundCUUnit =
false;
640 while (
Info.size() > UnitOffset) {
650 C.Offset = InfoSectionOffset;
652 UnitOffset +=
C.Length;
654 Header.
UnitType == dwarf::DW_UT_split_compile) {
657 Info.substr(UnitOffset -
C.Length,
C.Length),
658 CurStrOffsetSection, CurStrSection);
662 const auto &
ID = *EID;
663 auto P = IndexEntries.
insert(std::make_pair(
ID.Signature, Entry));
666 P.first->second.Name =
ID.Name;
667 P.first->second.DWOName =
ID.DWOName;
670 }
else if (Header.
UnitType == dwarf::DW_UT_split_type) {
671 auto P = TypeIndexEntries.
insert(
677 InfoSectionOffset +=
C.Length;
682 return make_error<DWPError>(
"no compile unit found in file: " + Input);
684 if (IndexVersion == 2) {
687 Out, TypeIndexEntries, TypesSection, CurTypesSection, CurEntry,
693 if (CurInfoSection.size() != 1)
694 return make_error<DWPError>(
"expected exactly one occurrence of a debug "
695 "info section in a .dwp file");
699 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);
700 if (!CUIndex.
parse(CUIndexData))
701 return make_error<DWPError>(
"failed to parse cu_index");
703 return make_error<DWPError>(
"incompatible cu_index versions, found " +
705 " and expecting " + utostr(IndexVersion));
709 auto *
I =
E.getContributions();
712 auto P = IndexEntries.
insert(std::make_pair(
E.getSignature(), CurEntry));
728 const auto &
ID = *EID;
731 auto &NewEntry =
P.first->second;
732 NewEntry.Name =
ID.Name;
733 NewEntry.DWOName =
ID.DWOName;
734 NewEntry.DWPName = Input;
740 C.Offset +=
I->Offset;
741 C.Length =
I->Length;
745 auto &
C = NewEntry.Contributions[Index];
747 C.Offset = InfoSectionOffset;
748 InfoSectionOffset +=
C.Length;
751 if (!CurTUIndexSection.
empty()) {
757 TUSectionKind = DW_SECT_INFO;
758 OutSection = InfoSection;
759 TypeInputSection = DwpSingleInfoSection;
762 if (CurTypesSection.size() != 1)
763 return make_error<DWPError>(
764 "multiple type unit sections in .dwp file");
767 OutSection = TypesSection;
768 TypeInputSection = CurTypesSection.
front();
772 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
773 if (!TUIndex.
parse(TUIndexData))
774 return make_error<DWPError>(
"failed to parse tu_index");
776 return make_error<DWPError>(
"incompatible tu_index versions, found " +
778 " and expecting " + utostr(IndexVersion));
780 unsigned TypesContributionIndex =
783 TypeInputSection, CurEntry,
784 ContributionOffsets[TypesContributionIndex],
785 TypesContributionIndex);
793 ContributionOffsets[0] = 0;
795 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
796 TypeIndexEntries, IndexVersion);
803 ContributionOffsets[0] = 1;
806 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
807 IndexEntries, IndexVersion);