Skip to content

Commit e2c7bdb

Browse files
Merge pull request swiftlang#8644 from rastogishubham/DbgStrOffs6
Add zlib compression to the debug_str_offset section
2 parents 0f54a18 + 05797c8 commit e2c7bdb

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

llvm/include/llvm/MCCAS/MCCASObjectV1.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ class MCCASBuilder {
588588

589589
Expected<SmallVector<DebugStrRef, 0>> createDebugStringRefs();
590590

591+
std::optional<Expected<DebugStrOffsetsRef>> createDebugStrOffsetsRef();
592+
591593
template <typename SectionTy>
592594
std::optional<Expected<SectionTy>> createGenericDebugRef(MCSection *Section);
593595

llvm/lib/MCCAS/MCCASObjectV1.cpp

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,9 +1370,46 @@ static Expected<uint64_t> materializeGenericDebugSection(MCCASReader &Reader,
13701370
Expected<uint64_t>
13711371
DebugStringOffsetsSectionRef::materialize(MCCASReader &Reader,
13721372
raw_ostream *Stream) const {
1373+
// Start a new section for relocations.
1374+
Reader.Relocations.emplace_back();
1375+
SmallVector<char, 0> SectionContents;
1376+
raw_svector_ostream SectionStream(SectionContents);
1377+
1378+
unsigned Size = 0;
13731379
StringRef Remaining = getData();
1374-
return materializeGenericDebugSection<DebugStringOffsetsSectionRef>(
1375-
Reader, Remaining, *this);
1380+
auto Refs = DebugStringOffsetsSectionRef::decodeReferences(*this, Remaining);
1381+
if (!Refs)
1382+
return Refs.takeError();
1383+
1384+
for (auto ID : *Refs) {
1385+
auto FragmentSize = Reader.materializeSection(ID, &SectionStream);
1386+
if (!FragmentSize)
1387+
return FragmentSize.takeError();
1388+
Size += *FragmentSize;
1389+
}
1390+
1391+
if (auto E = decodeRelocations(Reader, Remaining))
1392+
return std::move(E);
1393+
1394+
#if LLVM_ENABLE_ZLIB
1395+
StringRef SectionStringRef = toStringRef(SectionContents);
1396+
ArrayRef<uint8_t> BufRef = arrayRefFromStringRef(SectionStringRef);
1397+
assert(BufRef.size() >= 8 &&
1398+
"Debug String Offset buffer less than 8 bytes in size!");
1399+
// The zlib decompress function needs to know the uncompressed size of the
1400+
// buffer. That size is stored as a ULEB at the end of the buffer
1401+
auto UncompressedSize = decodeULEB128(BufRef.data() + BufRef.size() - 8);
1402+
BufRef = BufRef.drop_back(8);
1403+
SmallVector<uint8_t> OutBuff;
1404+
if (auto E = compression::zlib::decompress(BufRef, OutBuff, UncompressedSize))
1405+
return E;
1406+
SectionStringRef = toStringRef(OutBuff);
1407+
Reader.OS << SectionStringRef;
1408+
return UncompressedSize;
1409+
#endif
1410+
1411+
Reader.OS << SectionContents;
1412+
return Size;
13761413
}
13771414

13781415
Expected<uint64_t> DebugLocSectionRef::materialize(MCCASReader &Reader,
@@ -2615,10 +2652,44 @@ MCCASBuilder::createGenericDebugRef(MCSection *Section) {
26152652
return *DebugCASRef;
26162653
}
26172654

2655+
std::optional<Expected<DebugStrOffsetsRef>>
2656+
MCCASBuilder::createDebugStrOffsetsRef() {
2657+
2658+
if (!DwarfSections.StrOffsets ||
2659+
!DwarfSections.StrOffsets->getFragmentList().size())
2660+
return std::nullopt;
2661+
2662+
auto DebugStrOffsetsData = mergeMCFragmentContents(
2663+
DwarfSections.StrOffsets->getFragmentList(), false);
2664+
2665+
if (!DebugStrOffsetsData)
2666+
return DebugStrOffsetsData.takeError();
2667+
2668+
#if LLVM_ENABLE_ZLIB
2669+
SmallVector<uint8_t> CompressedBuff;
2670+
compression::zlib::compress(
2671+
arrayRefFromStringRef(toStringRef(*DebugStrOffsetsData)), CompressedBuff);
2672+
// Reserve 8 bytes for ULEB to store the size of the uncompressed data.
2673+
CompressedBuff.append(8, 0);
2674+
encodeULEB128(DebugStrOffsetsData->size(), CompressedBuff.end() - 8,
2675+
8 /*Pad to*/);
2676+
auto DbgStrOffsetsRef =
2677+
DebugStrOffsetsRef::create(*this, toStringRef(CompressedBuff));
2678+
if (!DbgStrOffsetsRef)
2679+
return DbgStrOffsetsRef.takeError();
2680+
return *DbgStrOffsetsRef;
2681+
#else
2682+
auto DbgStrOffsetsRef =
2683+
DebugStrOffsetsRef::create(*this, toStringRef(*DebugStrOffsetsData));
2684+
if (!DbgStrOffsetsRef)
2685+
return DbgStrOffsetsRef.takeError();
2686+
return *DbgStrOffsetsRef;
2687+
#endif
2688+
}
2689+
26182690
Error MCCASBuilder::createDebugStrOffsetsSection() {
26192691

2620-
auto MaybeDebugStringOffsetsRef =
2621-
createGenericDebugRef<DebugStrOffsetsRef>(DwarfSections.StrOffsets);
2692+
auto MaybeDebugStringOffsetsRef = createDebugStrOffsetsRef();
26222693
if (!MaybeDebugStringOffsetsRef)
26232694
return Error::success();
26242695

0 commit comments

Comments
 (0)