Skip to content

[BOLT] Fix possibly incorrect CU-indicies in gdb-index #151927

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
102 changes: 95 additions & 7 deletions bolt/lib/Core/GDBIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void GDBIndex::updateGdbIndexSection(
exit(1);
}
DenseSet<uint64_t> OriginalOffsets;
for (unsigned Index = 0, Units = BC.DwCtx->getNumCompileUnits();
for (unsigned Index = 0, PresentUnitsIndex = 0,
Units = BC.DwCtx->getNumCompileUnits();
Index < Units; ++Index) {
const DWARFUnit *CU = BC.DwCtx->getUnitAtIndex(Index);
if (SkipTypeUnits && CU->isTypeUnit())
Expand All @@ -90,7 +91,7 @@ void GDBIndex::updateGdbIndexSection(
}

OriginalOffsets.insert(Offset);
OffsetToIndexMap[Offset] = Index;
OffsetToIndexMap[Offset] = PresentUnitsIndex++;
}

// Ignore old address table.
Expand Down Expand Up @@ -125,16 +126,52 @@ void GDBIndex::updateGdbIndexSection(

using MapEntry = std::pair<uint32_t, CUInfo>;
std::vector<MapEntry> CUVector(CUMap.begin(), CUMap.end());
// Remove the CUs we won't emit anyway.
CUVector.erase(std::remove_if(CUVector.begin(), CUVector.end(),
[&OriginalOffsets](const MapEntry &It) {
// Skipping TU for DWARF5 when they are not
// included in CU list.
return OriginalOffsets.count(It.first) == 0;
}),
CUVector.end());
// Need to sort since we write out all of TUs in .debug_info before CUs.
std::sort(CUVector.begin(), CUVector.end(),
[](const MapEntry &E1, const MapEntry &E2) -> bool {
return E1.second.Offset < E2.second.Offset;
});
// Create the original CU index -> updated CU index mapping,
// as the sort above could've changed the order and we have to update
// indices correspondingly in address map and constant pool.
std::unordered_map<uint32_t, uint32_t> OriginalCUIndexToUpdatedCUIndexMap;
OriginalCUIndexToUpdatedCUIndexMap.reserve(CUVector.size());
for (uint32_t I = 0; I < CUVector.size(); ++I) {
OriginalCUIndexToUpdatedCUIndexMap[OffsetToIndexMap.at(CUVector[I].first)] =
I;
}
const auto RemapCUIndex = [&OriginalCUIndexToUpdatedCUIndexMap,
CUVectorSize = CUVector.size(),
TUVectorSize = getGDBIndexTUEntryVector().size()](
uint32_t OriginalIndex) {
if (OriginalIndex >= CUVectorSize) {
if (OriginalIndex >= CUVectorSize + TUVectorSize) {
errs() << "BOLT-ERROR: .gdb_index unknown CU index\n";
exit(1);
}
// The index is into TU CU List, which we don't reorder, so return as is.
return OriginalIndex;
}

const auto It = OriginalCUIndexToUpdatedCUIndexMap.find(OriginalIndex);
if (It == OriginalCUIndexToUpdatedCUIndexMap.end()) {
errs() << "BOLT-ERROR: .gdb_index unknown CU index\n";
exit(1);
}

return It->second;
};

// Writing out CU List <Offset, Size>
for (auto &CUInfo : CUVector) {
// Skipping TU for DWARF5 when they are not included in CU list.
if (!OriginalOffsets.count(CUInfo.first))
continue;
write64le(Buffer, CUInfo.second.Offset);
// Length encoded in CU doesn't contain first 4 bytes that encode length.
write64le(Buffer + 8, CUInfo.second.Length + 4);
Expand All @@ -160,12 +197,13 @@ void GDBIndex::updateGdbIndexSection(
// Generate new address table.
for (const std::pair<const uint64_t, DebugAddressRangesVector> &CURangesPair :
ARangesSectionWriter.getCUAddressRanges()) {
const uint32_t CUIndex = OffsetToIndexMap[CURangesPair.first];
const uint32_t OriginalCUIndex = OffsetToIndexMap[CURangesPair.first];
const uint32_t UpdatedCUIndex = RemapCUIndex(OriginalCUIndex);
const DebugAddressRangesVector &Ranges = CURangesPair.second;
for (const DebugAddressRange &Range : Ranges) {
write64le(Buffer, Range.LowPC);
write64le(Buffer + 8, Range.HighPC);
write32le(Buffer + 16, CUIndex);
write32le(Buffer + 16, UpdatedCUIndex);
Buffer += 20;
}
}
Expand All @@ -178,6 +216,56 @@ void GDBIndex::updateGdbIndexSection(
// Copy over the rest of the original data.
memcpy(Buffer, Data, TrailingSize);

// Fixup CU-indices in constant pool.
const char *const OriginalConstantPoolData =
GdbIndexContents.data() + ConstantPoolOffset;
uint8_t *const UpdatedConstantPoolData =
NewGdbIndexContents + ConstantPoolOffset + Delta;

const char *OriginalSymbolTableData =
GdbIndexContents.data() + SymbolTableOffset;
std::set<uint32_t> CUVectorOffsets;
// Parse the symbol map and extract constant pool CU offsets from it.
while (OriginalSymbolTableData < OriginalConstantPoolData) {
const uint32_t NameOffset = read32le(OriginalSymbolTableData);
const uint32_t CUVectorOffset = read32le(OriginalSymbolTableData + 4);
OriginalSymbolTableData += 8;

// Iff both are zero, then the slot is considered empty in the hash-map.
if (NameOffset || CUVectorOffset) {
CUVectorOffsets.insert(CUVectorOffset);
}
}

// Update the CU-indicies in the constant pool
for (const auto CUVectorOffset : CUVectorOffsets) {
const char *CurrentOriginalConstantPoolData =
OriginalConstantPoolData + CUVectorOffset;
uint8_t *CurrentUpdatedConstantPoolData =
UpdatedConstantPoolData + CUVectorOffset;

const uint32_t Num = read32le(CurrentOriginalConstantPoolData);
CurrentOriginalConstantPoolData += 4;
CurrentUpdatedConstantPoolData += 4;

for (uint32_t J = 0; J < Num; ++J) {
const uint32_t OriginalCUIndexAndAttributes =
read32le(CurrentOriginalConstantPoolData);
CurrentOriginalConstantPoolData += 4;

// We only care for the index, which is the lowest 24 bits, other bits are
// left as is.
const uint32_t OriginalCUIndex =
OriginalCUIndexAndAttributes & ((1 << 24) - 1);
const uint32_t Attributes = OriginalCUIndexAndAttributes >> 24;
const uint32_t UpdatedCUIndexAndAttributes =
RemapCUIndex(OriginalCUIndex) | (Attributes << 24);

write32le(CurrentUpdatedConstantPoolData, UpdatedCUIndexAndAttributes);
CurrentUpdatedConstantPoolData += 4;
}
}

// Register the new section.
BC.registerOrUpdateNoteSection(".gdb_index", NewGdbIndexContents,
NewGdbIndexSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
# POSTCHECK-NEXT: 1: offset = 0x00000000, type_offset = 0x0000001e, type_signature = 0x00f6cca4e3a15118
# POSTCHECK: Address area offset = 0x68, has 2 entries
# POSTCHECK-NEXT: Low/High address = [0x[[#%.4x,ADDR:]],
# POSTCHECK-SAME: 0x[[#ADDR + 0xf]]) (Size: 0xf), CU id = 1
# POSTCHECK-SAME: 0x[[#ADDR + 0xf]]) (Size: 0xf), CU id = 0
# POSTCHECK-NEXT: Low/High address = [0x[[#%.4x,ADDR1:]],
# POSTCHECK-SAME: 0x[[#ADDR1 + 0xd]]) (Size: 0xd), CU id = 2
# POSTCHECK-SAME: 0x[[#ADDR1 + 0xd]]) (Size: 0xd), CU id = 1
# POSTCHECK: Symbol table offset = 0x90, size = 1024, filled slots
# POSTCHECK-NEXT: 2: Name offset = 0x20, CU vector offset = 0x0
# POSTCHECK-NEXT: String name: S, CU vector index: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
# POSTCHECK: Types CU list offset = 0x38, has 0 entries
# POSTCHECK: Address area offset = 0x38, has 2 entries
# POSTCHECK-NEXT: Low/High address = [0x[[#%.4x,ADDR:]],
# POSTCHECK-SAME: 0x[[#ADDR + 0xf]]) (Size: 0xf), CU id = 1
# POSTCHECK-SAME: 0x[[#ADDR + 0xf]]) (Size: 0xf), CU id = 0
# POSTCHECK-NEXT: Low/High address = [0x[[#%.4x,ADDR1:]],
# POSTCHECK-SAME: 0x[[#ADDR1 + 0xd]]) (Size: 0xd), CU id = 2
# POSTCHECK-SAME: 0x[[#ADDR1 + 0xd]]) (Size: 0xd), CU id = 1
# POSTCHECK: Symbol table offset = 0x60, size = 1024, filled slots
# POSTCHECK-NEXT: 2: Name offset = 0x38, CU vector offset = 0x0
# POSTCHECK-NEXT: String name: S, CU vector index: 0
Expand Down
Loading