-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[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
itrofimow
wants to merge
3
commits into
llvm:main
Choose a base branch
from
itrofimow:llvm_bolt_gdb_index_update_cu_indicies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[BOLT] Fix possibly incorrect CU-indicies in gdb-index #151927
itrofimow
wants to merge
3
commits into
llvm:main
from
itrofimow:llvm_bolt_gdb_index_update_cu_indicies
+92
−5
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-bolt Author: None (itrofimow) ChangesAfter we sort the CUVector, we have to update CU-indices in address map and constant pool Full diff: https://github.com/llvm/llvm-project/pull/151927.diff 1 Files Affected:
diff --git a/bolt/lib/Core/GDBIndex.cpp b/bolt/lib/Core/GDBIndex.cpp
index c7fb4889646b4..0fe1c5de94138 100644
--- a/bolt/lib/Core/GDBIndex.cpp
+++ b/bolt/lib/Core/GDBIndex.cpp
@@ -130,6 +130,26 @@ void GDBIndex::updateGdbIndexSection(
[](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
+ // indexes 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](uint32_t 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.
@@ -160,12 +180,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;
}
}
@@ -178,6 +199,56 @@ void GDBIndex::updateGdbIndexSection(
// Copy over the rest of the original data.
memcpy(Buffer, Data, TrailingSize);
+ // Fixup CU-indicies 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);
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After we sort the CUVector, we have to update CU-indices in address map and constant pool