Skip to content

Commit f0b43e9

Browse files
Addressing reviewers
1 parent 1388020 commit f0b43e9

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

bolt/include/bolt/Core/BinarySection.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ class BinarySection {
6666
// from the original section address.
6767
RelocationSetType DynamicRelocations;
6868

69-
/// Pending relocations for this section and whether they are optional, i.e.,
70-
/// added as part of an optimization. In that case they can be safely omitted
71-
/// if flushPendingRelocations discovers they cannot be encoded.
72-
std::vector<std::pair<Relocation, bool>> PendingRelocations;
69+
// Pending relocations for this section.
70+
std::vector<Relocation> PendingRelocations;
7371

7472
struct BinaryPatch {
7573
uint64_t Offset;
@@ -377,10 +375,9 @@ class BinarySection {
377375
DynamicRelocations.emplace(Reloc);
378376
}
379377

380-
/// Add relocation against the original contents of this section. When added
381-
/// as part of an optimization it is marked as \p Optional.
382-
void addPendingRelocation(const Relocation &Rel, bool Optional = false) {
383-
PendingRelocations.push_back({Rel, Optional});
378+
/// Add relocation against the original contents of this section.
379+
void addPendingRelocation(const Relocation &Rel) {
380+
PendingRelocations.push_back(Rel);
384381
}
385382

386383
/// Add patch to the input contents of this section.

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,8 @@ bool BinaryFunction::scanExternalRefs() {
17951795
// Create relocation for every fixup.
17961796
for (const MCFixup &Fixup : Fixups) {
17971797
std::optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
1798+
// Can be skipped under the right circumstances.
1799+
Rel->setOptional();
17981800
if (!Rel) {
17991801
Success = false;
18001802
continue;
@@ -1824,7 +1826,7 @@ bool BinaryFunction::scanExternalRefs() {
18241826
// Add relocations unless disassembly failed for this function.
18251827
if (!DisassemblyFailed)
18261828
for (Relocation &Rel : FunctionRelocations)
1827-
getOriginSection()->addPendingRelocation(Rel, /*Optional*/ true);
1829+
getOriginSection()->addPendingRelocation(Rel);
18281830

18291831
// Add patches grouping them together.
18301832
if (!InstructionPatches.empty()) {

bolt/lib/Core/BinarySection.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ void BinarySection::flushPendingRelocations(raw_pwrite_stream &OS,
176176
SectionFileOffset + Patch.Offset);
177177

178178
uint64_t SkippedPendingRelocations = 0;
179-
for (auto &[Reloc, Optional] : PendingRelocations) {
179+
for (Relocation &Reloc : PendingRelocations) {
180180
uint64_t Value = Reloc.Addend;
181181
if (Reloc.Symbol)
182182
Value += Resolver(Reloc.Symbol);
183183

184-
// Safely skip any pending relocation that cannot be encoded and was added
185-
// as part of an optimization.
186-
if (Optional && !Relocation::canEncodeValue(
187-
Reloc.Type, Value, SectionAddress + Reloc.Offset)) {
184+
// Safely skip any optional pending relocation that cannot be encoded.
185+
if (Reloc.isOptional() &&
186+
!Relocation::canEncodeValue(Reloc.Type, Value,
187+
SectionAddress + Reloc.Offset)) {
188188

189189
// A successful run of 'scanExternalRefs' means that all pending
190190
// relocations are flushed. Otherwise, PatchEntries should run.
@@ -219,9 +219,10 @@ void BinarySection::flushPendingRelocations(raw_pwrite_stream &OS,
219219

220220
clearList(PendingRelocations);
221221

222-
if (SkippedPendingRelocations > 0)
223-
LLVM_DEBUG(dbgs() << "BOLT-INFO: Skipped " << SkippedPendingRelocations
224-
<< " pending relocations as they were out of range\n");
222+
if (SkippedPendingRelocations > 0 && opts::Verbosity >= 1) {
223+
BC.outs() << "BOLT-INFO: skipped " << SkippedPendingRelocations
224+
<< " out-of-range optional relocations\n";
225+
}
225226
}
226227

227228
BinarySection::~BinarySection() { updateContents(nullptr, 0); }

bolt/unittests/Core/BinaryContext.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ TEST_P(BinaryContextTester,
177177
// Create symbol 'Func0x4'
178178
MCSymbol *RelSymbol = BC->getOrCreateGlobalSymbol(4, "Func");
179179
ASSERT_TRUE(RelSymbol);
180-
BS.addPendingRelocation(Relocation{8, RelSymbol, ELF::R_AARCH64_CALL26, 0, 0},
181-
/*Optional*/ true);
180+
Relocation Reloc{8, RelSymbol, ELF::R_AARCH64_CALL26, 0, 0};
181+
Reloc.setOptional();
182+
BS.addPendingRelocation(Reloc);
182183

183184
SmallVector<char> Vect;
184185
raw_svector_ostream OS(Vect);
@@ -201,29 +202,26 @@ TEST_P(BinaryContextTester,
201202
// relocations that cannot be encoded, given that PatchEntries runs.
202203
opts::ForcePatch = true;
203204

204-
bool DebugFlagPrev = ::llvm::DebugFlag;
205-
::llvm::DebugFlag = true;
206-
testing::internal::CaptureStderr();
205+
opts::Verbosity = 1;
206+
testing::internal::CaptureStdout();
207207

208208
BinarySection &BS = BC->registerOrUpdateSection(
209209
".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
210210
MCSymbol *RelSymbol = BC->getOrCreateGlobalSymbol(4, "Func");
211211
ASSERT_TRUE(RelSymbol);
212-
BS.addPendingRelocation(Relocation{8, RelSymbol, ELF::R_AARCH64_CALL26, 0, 0},
213-
/*Optional*/ true);
212+
Relocation Reloc{8, RelSymbol, ELF::R_AARCH64_CALL26, 0, 0};
213+
Reloc.setOptional();
214+
BS.addPendingRelocation(Reloc);
214215

215216
SmallVector<char> Vect;
216217
raw_svector_ostream OS(Vect);
217218

218219
// Resolve relocation symbol to a high value so encoding will be out of range.
219220
BS.flushPendingRelocations(OS, [&](const MCSymbol *S) { return 0x800000F; });
220-
221-
std::string CapturedStdErr = testing::internal::GetCapturedStderr();
222-
EXPECT_TRUE(CapturedStdErr.find("BOLT-INFO: Skipped 1 pending relocations as "
223-
"they were out of range") !=
224-
std::string::npos);
225-
226-
::llvm::DebugFlag = DebugFlagPrev;
221+
outs().flush();
222+
std::string CapturedStdOut = testing::internal::GetCapturedStdout();
223+
EXPECT_EQ(CapturedStdOut,
224+
"BOLT-INFO: skipped 1 out-of-range optional relocations\n");
227225
}
228226

229227
#endif

0 commit comments

Comments
 (0)