Skip to content

Commit bed3608

Browse files
authored
[BOLT][NFC] Factor out RI::disassemblePLTInstruction (#80302)
1 parent a52eea6 commit bed3608

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ class RewriteInstance {
264264
void createPLTBinaryFunction(uint64_t TargetAddress, uint64_t EntryAddress,
265265
uint64_t EntrySize);
266266

267+
/// Disassemble PLT instruction.
268+
void disassemblePLTInstruction(const BinarySection &Section,
269+
uint64_t InstrOffset, MCInst &Instruction,
270+
uint64_t &InstrSize);
271+
267272
/// Disassemble aarch64-specific .plt \p Section auxiliary function
268273
void disassemblePLTSectionAArch64(BinarySection &Section);
269274

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,25 +1470,29 @@ void RewriteInstance::createPLTBinaryFunction(uint64_t TargetAddress,
14701470
setPLTSymbol(BF, Symbol->getName());
14711471
}
14721472

1473-
void RewriteInstance::disassemblePLTSectionAArch64(BinarySection &Section) {
1473+
void RewriteInstance::disassemblePLTInstruction(const BinarySection &Section,
1474+
uint64_t InstrOffset,
1475+
MCInst &Instruction,
1476+
uint64_t &InstrSize) {
14741477
const uint64_t SectionAddress = Section.getAddress();
14751478
const uint64_t SectionSize = Section.getSize();
14761479
StringRef PLTContents = Section.getContents();
14771480
ArrayRef<uint8_t> PLTData(
14781481
reinterpret_cast<const uint8_t *>(PLTContents.data()), SectionSize);
14791482

1480-
auto disassembleInstruction = [&](uint64_t InstrOffset, MCInst &Instruction,
1481-
uint64_t &InstrSize) {
1482-
const uint64_t InstrAddr = SectionAddress + InstrOffset;
1483-
if (!BC->DisAsm->getInstruction(Instruction, InstrSize,
1484-
PLTData.slice(InstrOffset), InstrAddr,
1485-
nulls())) {
1486-
errs() << "BOLT-ERROR: unable to disassemble instruction in PLT section "
1487-
<< Section.getName() << " at offset 0x"
1488-
<< Twine::utohexstr(InstrOffset) << '\n';
1489-
exit(1);
1490-
}
1491-
};
1483+
const uint64_t InstrAddr = SectionAddress + InstrOffset;
1484+
if (!BC->DisAsm->getInstruction(Instruction, InstrSize,
1485+
PLTData.slice(InstrOffset), InstrAddr,
1486+
nulls())) {
1487+
errs() << "BOLT-ERROR: unable to disassemble instruction in PLT section "
1488+
<< Section.getName() << formatv(" at offset {0:x}\n", InstrOffset);
1489+
exit(1);
1490+
}
1491+
}
1492+
1493+
void RewriteInstance::disassemblePLTSectionAArch64(BinarySection &Section) {
1494+
const uint64_t SectionAddress = Section.getAddress();
1495+
const uint64_t SectionSize = Section.getSize();
14921496

14931497
uint64_t InstrOffset = 0;
14941498
// Locate new plt entry
@@ -1500,7 +1504,7 @@ void RewriteInstance::disassemblePLTSectionAArch64(BinarySection &Section) {
15001504
uint64_t InstrSize;
15011505
// Loop through entry instructions
15021506
while (InstrOffset < SectionSize) {
1503-
disassembleInstruction(InstrOffset, Instruction, InstrSize);
1507+
disassemblePLTInstruction(Section, InstrOffset, Instruction, InstrSize);
15041508
EntrySize += InstrSize;
15051509
if (!BC->MIB->isIndirectBranch(Instruction)) {
15061510
Instructions.emplace_back(Instruction);
@@ -1521,7 +1525,7 @@ void RewriteInstance::disassemblePLTSectionAArch64(BinarySection &Section) {
15211525

15221526
// Skip nops if any
15231527
while (InstrOffset < SectionSize) {
1524-
disassembleInstruction(InstrOffset, Instruction, InstrSize);
1528+
disassemblePLTInstruction(Section, InstrOffset, Instruction, InstrSize);
15251529
if (!BC->MIB->isNoop(Instruction))
15261530
break;
15271531

@@ -1578,29 +1582,13 @@ void RewriteInstance::disassemblePLTSectionX86(BinarySection &Section,
15781582
uint64_t EntrySize) {
15791583
const uint64_t SectionAddress = Section.getAddress();
15801584
const uint64_t SectionSize = Section.getSize();
1581-
StringRef PLTContents = Section.getContents();
1582-
ArrayRef<uint8_t> PLTData(
1583-
reinterpret_cast<const uint8_t *>(PLTContents.data()), SectionSize);
1584-
1585-
auto disassembleInstruction = [&](uint64_t InstrOffset, MCInst &Instruction,
1586-
uint64_t &InstrSize) {
1587-
const uint64_t InstrAddr = SectionAddress + InstrOffset;
1588-
if (!BC->DisAsm->getInstruction(Instruction, InstrSize,
1589-
PLTData.slice(InstrOffset), InstrAddr,
1590-
nulls())) {
1591-
errs() << "BOLT-ERROR: unable to disassemble instruction in PLT section "
1592-
<< Section.getName() << " at offset 0x"
1593-
<< Twine::utohexstr(InstrOffset) << '\n';
1594-
exit(1);
1595-
}
1596-
};
15971585

15981586
for (uint64_t EntryOffset = 0; EntryOffset + EntrySize <= SectionSize;
15991587
EntryOffset += EntrySize) {
16001588
MCInst Instruction;
16011589
uint64_t InstrSize, InstrOffset = EntryOffset;
16021590
while (InstrOffset < EntryOffset + EntrySize) {
1603-
disassembleInstruction(InstrOffset, Instruction, InstrSize);
1591+
disassemblePLTInstruction(Section, InstrOffset, Instruction, InstrSize);
16041592
// Check if the entry size needs adjustment.
16051593
if (EntryOffset == 0 && BC->MIB->isTerminateBranch(Instruction) &&
16061594
EntrySize == 8)

0 commit comments

Comments
 (0)