diff --git a/llvm/include/llvm/DWP/DWP.h b/llvm/include/llvm/DWP/DWP.h index a759bae10d160..cc38369658eaa 100644 --- a/llvm/include/llvm/DWP/DWP.h +++ b/llvm/include/llvm/DWP/DWP.h @@ -70,6 +70,8 @@ struct CompileUnitIdentifiers { LLVM_ABI Error write(MCStreamer &Out, ArrayRef Inputs, OnCuIndexOverflow OverflowOptValue); +typedef std::vector> SectionLengths; + LLVM_ABI Error handleSection( const StringMap> &KnownSections, const MCSection *StrSection, const MCSection *StrOffsetSection, @@ -82,7 +84,7 @@ LLVM_ABI Error handleSection( std::vector &CurTypesSection, std::vector &CurInfoSection, StringRef &AbbrevSection, StringRef &CurCUIndexSection, StringRef &CurTUIndexSection, - std::vector> &SectionLength); + SectionLengths &SectionLength); LLVM_ABI Expected parseInfoSectionUnitHeader(StringRef Info); diff --git a/llvm/include/llvm/DWP/DWPStringPool.h b/llvm/include/llvm/DWP/DWPStringPool.h index 1354b46f156b6..d1486ff7872e1 100644 --- a/llvm/include/llvm/DWP/DWPStringPool.h +++ b/llvm/include/llvm/DWP/DWPStringPool.h @@ -32,13 +32,13 @@ class DWPStringPool { MCStreamer &Out; MCSection *Sec; - DenseMap Pool; - uint32_t Offset = 0; + DenseMap Pool; + uint64_t Offset = 0; public: DWPStringPool(MCStreamer &Out, MCSection *Sec) : Out(Out), Sec(Sec) {} - uint32_t getOffset(const char *Str, unsigned Length) { + uint64_t getOffset(const char *Str, unsigned Length) { assert(strlen(Str) + 1 == Length && "Ensure length hint is correct"); auto Pair = Pool.insert(std::make_pair(Str, Offset)); diff --git a/llvm/lib/DWP/DWP.cpp b/llvm/lib/DWP/DWP.cpp index b565edbfe96db..54edce81208b5 100644 --- a/llvm/lib/DWP/DWP.cpp +++ b/llvm/lib/DWP/DWP.cpp @@ -413,33 +413,43 @@ Expected parseInfoSectionUnitHeader(StringRef Info) { } static void writeNewOffsetsTo(MCStreamer &Out, DataExtractor &Data, - DenseMap &OffsetRemapping, - uint64_t &Offset, uint64_t &Size) { + DenseMap &OffsetRemapping, + uint64_t &Offset, const uint64_t Size, + uint32_t OldOffsetSize, uint32_t NewOffsetSize) { while (Offset < Size) { - auto OldOffset = Data.getU32(&Offset); - auto NewOffset = OffsetRemapping[OldOffset]; - Out.emitIntValue(NewOffset, 4); + const uint64_t OldOffset = Data.getUnsigned(&Offset, OldOffsetSize); + const uint64_t NewOffset = OffsetRemapping[OldOffset]; + assert(NewOffsetSize == 8 || NewOffset <= UINT32_MAX); + Out.emitIntValue(NewOffset, NewOffsetSize); } } void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings, MCSection *StrOffsetSection, StringRef CurStrSection, - StringRef CurStrOffsetSection, uint16_t Version) { + StringRef CurStrOffsetSection, uint16_t Version, + SectionLengths &SectionLength) { // Could possibly produce an error or warning if one of these was non-null but // the other was null. if (CurStrSection.empty() || CurStrOffsetSection.empty()) return; - DenseMap OffsetRemapping; + DenseMap OffsetRemapping; DataExtractor Data(CurStrSection, true, 0); uint64_t LocalOffset = 0; uint64_t PrevOffset = 0; + + // Keep track if any new string offsets exceed UINT32_MAX. If any do, we can + // emit a DWARF64 .debug_str_offsets table for this compile unit. + uint32_t OldOffsetSize = 4; + uint32_t NewOffsetSize = 4; while (const char *S = Data.getCStr(&LocalOffset)) { - OffsetRemapping[PrevOffset] = - Strings.getOffset(S, LocalOffset - PrevOffset); + uint64_t NewOffset = Strings.getOffset(S, LocalOffset - PrevOffset); + OffsetRemapping[PrevOffset] = NewOffset; + if (NewOffset > UINT32_MAX) + NewOffsetSize = 8; PrevOffset = LocalOffset; } @@ -451,7 +461,7 @@ void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings, uint64_t Size = CurStrOffsetSection.size(); if (Version > 4) { while (Offset < Size) { - uint64_t HeaderSize = debugStrOffsetsHeaderSize(Data, Version); + const uint64_t HeaderSize = debugStrOffsetsHeaderSize(Data, Version); assert(HeaderSize <= Size - Offset && "StrOffsetSection size is less than its header"); @@ -461,16 +471,52 @@ void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings, if (HeaderSize == 8) { ContributionSize = Data.getU32(&HeaderLengthOffset); } else if (HeaderSize == 16) { + OldOffsetSize = 8; HeaderLengthOffset += 4; // skip the dwarf64 marker ContributionSize = Data.getU64(&HeaderLengthOffset); } ContributionEnd = ContributionSize + HeaderLengthOffset; - Out.emitBytes(Data.getBytes(&Offset, HeaderSize)); - writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, ContributionEnd); + + StringRef HeaderBytes = Data.getBytes(&Offset, HeaderSize); + if (OldOffsetSize == 4 && NewOffsetSize == 8) { + // We had a DWARF32 .debug_str_offsets header, but we need to emit + // some string offsets that require 64 bit offsets on the .debug_str + // section. Emit the .debug_str_offsets header in DWARF64 format so we + // can emit string offsets that exceed UINT32_MAX without truncating + // the string offset. + + // 2 bytes for DWARF version, 2 bytes pad. + const uint64_t VersionPadSize = 4; + const uint64_t NewLength = + (ContributionSize - VersionPadSize) * 2 + VersionPadSize; + // Emit the DWARF64 length that starts with a 4 byte DW_LENGTH_DWARF64 + // value followed by the 8 byte updated length. + Out.emitIntValue(llvm::dwarf::DW_LENGTH_DWARF64, 4); + Out.emitIntValue(NewLength, 8); + // Emit DWARF version as a 2 byte integer. + Out.emitIntValue(Version, 2); + // Emit 2 bytes of padding. + Out.emitIntValue(0, 2); + // Update the .debug_str_offsets section length contribution for the + // this .dwo file. + for (auto &Pair : SectionLength) { + if (Pair.first == DW_SECT_STR_OFFSETS) { + Pair.second = NewLength + 12; + break; + } + } + } else { + // Just emit the same .debug_str_offsets header. + Out.emitBytes(HeaderBytes); + } + writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, ContributionEnd, + OldOffsetSize, NewOffsetSize); } } else { - writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, Size); + assert(OldOffsetSize == NewOffsetSize); + writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, Size, OldOffsetSize, + NewOffsetSize); } } @@ -562,7 +608,7 @@ Error handleSection( std::vector &CurTypesSection, std::vector &CurInfoSection, StringRef &AbbrevSection, StringRef &CurCUIndexSection, StringRef &CurTUIndexSection, - std::vector> &SectionLength) { + SectionLengths &SectionLength) { if (Section.isBSS()) return Error::success(); @@ -684,7 +730,7 @@ Error write(MCStreamer &Out, ArrayRef Inputs, // This maps each section contained in this file to its length. // This information is later on used to calculate the contributions, // i.e. offset and length, of each compile/type unit to a section. - std::vector> SectionLength; + SectionLengths SectionLength; for (const auto &Section : Obj.sections()) if (auto Err = handleSection( @@ -713,7 +759,7 @@ Error write(MCStreamer &Out, ArrayRef Inputs, } writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection, - CurStrOffsetSection, Header.Version); + CurStrOffsetSection, Header.Version, SectionLength); for (auto Pair : SectionLength) { auto Index = getContributionIndex(Pair.first, IndexVersion);