Skip to content

Commit 3fc4e31

Browse files
committed
Instr eval based on reg width and attempt to pass tests
1 parent 33e100e commit 3fc4e31

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

llvm/include/llvm/MC/MCInstrAnalysis.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ class MCInstrAnalysis {
181181
evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
182182
uint64_t &Target) const;
183183

184+
/// Given an instruction that accesses a menory address, try to compute
185+
/// the target address. Return true success, and the address in Target.
184186
virtual bool
185187
evaluateInstruction(const MCInst &Inst, uint64_t Addr, uint64_t Size,
186-
uint64_t &Target) const;
188+
uint64_t &Target, int ArchRegWidth) const;
187189

188190
/// Given an instruction tries to get the address of a memory operand. Returns
189191
/// the address on success.

llvm/lib/MC/MCInstrAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool MCInstrAnalysis::evaluateBranch(const MCInst & /*Inst*/, uint64_t /*Addr*/,
3232

3333
bool MCInstrAnalysis::evaluateInstruction(const MCInst &Inst,
3434
uint64_t Addr, uint64_t Size,
35-
uint64_t &Target) const {
35+
uint64_t &Target, int ArchRegWidth) const {
3636
return false;
3737
}
3838

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,13 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
247247
}
248248

249249
bool evaluateInstruction(const MCInst &Inst, uint64_t Addr, uint64_t Size,
250-
uint64_t &Target) const override {
250+
uint64_t &Target, int ArchRegWidth) const override {
251251
switch(Inst.getOpcode()) {
252252
default:
253253
return false;
254254
case RISCV::ADDI: {
255255
if (auto TargetRegState = getGPRState(Inst.getOperand(1).getReg())) {
256-
// TODO: Figure out ways to find the actual value of XLEN during analysis
257-
int XLEN = 32;
258-
uint64_t Mask = ~((uint64_t)0) >> (64 - XLEN);
256+
uint64_t Mask = ~((uint64_t)0) >> (64 - ArchRegWidth);
259257
Target = *TargetRegState + SignExtend64<12>(Inst.getOperand(2).getImm());
260258
Target &= Mask;
261259
return true;

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,8 +2323,9 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
23232323
if (Disassembled && DT->InstrAnalysis) {
23242324
llvm::raw_ostream *TargetOS = &FOS;
23252325
uint64_t Target;
2326+
int TargetArchBitWidth = DT->SubtargetInfo->getTargetTriple().getArchPointerBitWidth();
23262327
bool PrintTarget = DT->InstrAnalysis->evaluateBranch(Inst, SectionAddr + Index, Size, Target) ||
2327-
DT->InstrAnalysis->evaluateInstruction(Inst, SectionAddr + Index, Size, Target);
2328+
DT->InstrAnalysis->evaluateInstruction(Inst, SectionAddr + Index, Size, Target, TargetArchBitWidth);
23282329
if (!PrintTarget) {
23292330
if (std::optional<uint64_t> MaybeTarget =
23302331
DT->InstrAnalysis->evaluateMemoryOperandAddress(
@@ -2353,27 +2354,37 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
23532354
// N.B. Except for XCOFF, we don't walk the relocations in the
23542355
// relocatable case yet.
23552356
std::vector<const SectionSymbolsTy *> TargetSectionSymbols;
2357+
bool AbsoluteFirst = false;
23562358
if (!Obj.isRelocatableObject()) {
23572359
auto It = llvm::partition_point(
23582360
SectionAddresses,
23592361
[=](const std::pair<uint64_t, SectionRef> &O) {
23602362
return O.first <= Target;
23612363
});
2362-
uint64_t TargetSecAddr = 0;
2364+
uint64_t TargetSecAddr = It == SectionAddresses.end() ? It->first : 0;
2365+
bool FoundSymbols = false;
2366+
// missing case where begin == end as in this case, we are to return 0
23632367
while (It != SectionAddresses.begin()) {
23642368
--It;
2365-
if (TargetSecAddr == 0)
2366-
TargetSecAddr = It->first;
2367-
if (It->first != TargetSecAddr)
2368-
break;
2369+
if (It->first != TargetSecAddr) {
2370+
if (FoundSymbols)
2371+
break;
2372+
else {
2373+
TargetSecAddr = It->first;
2374+
AbsoluteFirst = true;
2375+
}
2376+
}
23692377
TargetSectionSymbols.push_back(&AllSymbols[It->second]);
2370-
if (AllSymbols[It->second].empty())
2371-
TargetSecAddr = 0;
2378+
if (!AllSymbols[It->second].empty())
2379+
FoundSymbols = true;
23722380
}
23732381
} else {
23742382
TargetSectionSymbols.push_back(&Symbols);
23752383
}
2376-
TargetSectionSymbols.push_back(&AbsoluteSymbols);
2384+
if (AbsoluteFirst)
2385+
TargetSectionSymbols.insert(TargetSectionSymbols.begin(), &AbsoluteSymbols);
2386+
else
2387+
TargetSectionSymbols.push_back(&AbsoluteSymbols);
23772388

23782389
// Find the last symbol in the first candidate section whose
23792390
// offset is less than or equal to the target. If there are no

0 commit comments

Comments
 (0)