Skip to content

[BOLT][DWARF] Fix out of order rangelists/loclists #81645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions bolt/lib/Core/DebugData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ template <typename DebugVector, typename ListEntry, typename DebugAddressEntry>
static bool emitWithBase(raw_ostream &OS, const DebugVector &Entries,
DebugAddrWriter &AddrWriter, DWARFUnit &CU,
uint32_t &Index, const ListEntry BaseAddressx,
const ListEntry OffsetPair, const ListEntry EndOfList,
const ListEntry OffsetPair,
const std::function<void(uint32_t)> &Func) {
if (Entries.size() < 2)
return false;
Expand All @@ -241,7 +241,9 @@ static bool emitWithBase(raw_ostream &OS, const DebugVector &Entries,
const DebugAddressEntry &Entry = Entries[Index];
if (Entry.LowPC == 0)
break;
assert(Base <= Entry.LowPC && "Entry base is higher than low PC");
// In case rnglists or loclists are not sorted.
if (Base > Entry.LowPC)
break;
uint32_t StartOffset = Entry.LowPC - Base;
uint32_t EndOffset = Entry.HighPC - Base;
if (encodeULEB128(EndOffset, TempBuffer) > 2)
Expand All @@ -266,8 +268,6 @@ static bool emitWithBase(raw_ostream &OS, const DebugVector &Entries,
encodeULEB128(OffsetEntry.EndOffset, OS);
Func(OffsetEntry.Index);
}
support::endian::write(OS, static_cast<uint8_t>(EndOfList),
llvm::endianness::little);
return true;
}

Expand All @@ -276,19 +276,17 @@ DebugRangeListsSectionWriter::addRanges(DebugAddressRangesVector &Ranges) {
std::lock_guard<std::mutex> Lock(WriterMutex);

RangeEntries.push_back(CurrentOffset);
bool WrittenStartxLength = false;
std::sort(
Ranges.begin(), Ranges.end(),
[](const DebugAddressRange &R1, const DebugAddressRange &R2) -> bool {
return R1.LowPC < R2.LowPC;
});
for (unsigned I = 0; I < Ranges.size();) {
WrittenStartxLength = false;
if (emitWithBase<DebugAddressRangesVector, dwarf::RnglistEntries,
DebugAddressRange>(
*CUBodyStream, Ranges, *AddrWriter, *CU, I,
dwarf::DW_RLE_base_addressx, dwarf::DW_RLE_offset_pair,
dwarf::DW_RLE_end_of_list, [](uint32_t Index) -> void {}))
DebugAddressRange>(*CUBodyStream, Ranges, *AddrWriter, *CU,
I, dwarf::DW_RLE_base_addressx,
dwarf::DW_RLE_offset_pair,
[](uint32_t Index) -> void {}))
continue;

const DebugAddressRange &Range = Ranges[I];
Expand All @@ -299,12 +297,11 @@ DebugRangeListsSectionWriter::addRanges(DebugAddressRangesVector &Ranges) {
encodeULEB128(Index, *CUBodyStream);
encodeULEB128(Range.HighPC - Range.LowPC, *CUBodyStream);
++I;
WrittenStartxLength = true;
}
if (WrittenStartxLength)
support::endian::write(*CUBodyStream,
static_cast<uint8_t>(dwarf::DW_RLE_end_of_list),
llvm::endianness::little);

support::endian::write(*CUBodyStream,
static_cast<uint8_t>(dwarf::DW_RLE_end_of_list),
llvm::endianness::little);
CurrentOffset = CUBodyBuffer->size();
return RangeEntries.size() - 1;
}
Expand Down Expand Up @@ -688,20 +685,18 @@ static void writeDWARF5LocList(uint32_t &NumberOfEntries, DIEValue &AttrInfo,
}

std::vector<uint64_t> OffsetsArray;
bool WrittenStartxLength = false;
auto writeExpression = [&](uint32_t Index) -> void {
const DebugLocationEntry &Entry = LocList[Index];
encodeULEB128(Entry.Expr.size(), LocBodyStream);
LocBodyStream << StringRef(
reinterpret_cast<const char *>(Entry.Expr.data()), Entry.Expr.size());
};
for (unsigned I = 0; I < LocList.size();) {
WrittenStartxLength = false;
if (emitWithBase<DebugLocationsVector, dwarf::LoclistEntries,
DebugLocationEntry>(
LocBodyStream, LocList, AddrWriter, CU, I,
dwarf::DW_LLE_base_addressx, dwarf::DW_LLE_offset_pair,
dwarf::DW_LLE_end_of_list, writeExpression))
DebugLocationEntry>(LocBodyStream, LocList, AddrWriter, CU,
I, dwarf::DW_LLE_base_addressx,
dwarf::DW_LLE_offset_pair,
writeExpression))
continue;

const DebugLocationEntry &Entry = LocList[I];
Expand All @@ -713,13 +708,11 @@ static void writeDWARF5LocList(uint32_t &NumberOfEntries, DIEValue &AttrInfo,
encodeULEB128(Entry.HighPC - Entry.LowPC, LocBodyStream);
writeExpression(I);
++I;
WrittenStartxLength = true;
}

if (WrittenStartxLength)
support::endian::write(LocBodyStream,
static_cast<uint8_t>(dwarf::DW_LLE_end_of_list),
llvm::endianness::little);
support::endian::write(LocBodyStream,
static_cast<uint8_t>(dwarf::DW_LLE_end_of_list),
llvm::endianness::little);
}

void DebugLoclistWriter::addList(DIEBuilder &DIEBldr, DIE &Die,
Expand Down
Loading