Skip to content

[feature][riscv] handle target address calculation in llvm-objdump disassembly for riscv #144620

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 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion cross-project-tests/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = [".c", ".cl", ".cpp", ".m"]
config.suffixes = [".c", ".cl", ".cpp", ".m", ".s"]

# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
# subdirectories contain auxiliary inputs for various tests in their parent
Expand Down
2 changes: 2 additions & 0 deletions cross-project-tests/tools/llvm-objdump/RISCV/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if "clang" not in config.available_features or "RISCV" not in config.targets_to_build:
config.unsupported = True
31 changes: 31 additions & 0 deletions cross-project-tests/tools/llvm-objdump/RISCV/riscv32-ar-coverage.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# RUN: %clang --target=fuchsia-elf-riscv32 -march=rv32g_zclsd_zilsd %s -nostdlib -o %t
# RUN: llvm-objdump -d %t | FileCheck %s

# CHECK: 00001000 <_start>:
# CHECK-NEXT: 1000: 00000517 auipc a0, 0x0
# CHECK-NEXT: 1004: 0559 addi a0, a0, 0x16 <target>
# CHECK-NEXT: 1006: 00000517 auipc a0, 0x0
# CHECK-NEXT: 100a: 6910 ld a2, 0x10(a0) <target>
# CHECK-NEXT: 100c: 00000517 auipc a0, 0x0
# CHECK-NEXT: 1010: 00c53523 sd a2, 0xa(a0) <target>
# CHECK-NEXT: 1014: 0000 unimp

# the structure of this test file is similar to that of riscv64-ar-coverage
# with the major difference being that these tests are focused on instructions
# for 32 bit architecture

.global _start
.text
_start:
auipc a0, 0x0
addi a0, a0, 0x16 # addi -- behavior changes with different architectures

auipc a0, 0x0
c.ld a2, 0x10(a0) # zclsd instruction

auipc a0, 0x0
sd a2, 0xa(a0) # zilsd instruction

.skip 0x2
target:
ret:
110 changes: 110 additions & 0 deletions cross-project-tests/tools/llvm-objdump/RISCV/riscv64-ar-coverage.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# RUN: %clang --target=fuchsia-elf-riscv64 -march=rv64gc_zcb %s -nostdlib -o %t
# RUN: llvm-objcopy --add-symbol abs=0,global %t
# RUN: llvm-objdump -d %t | FileCheck %s

# CHECK: 0000000000001000 <_start>:
# CHECK-NEXT: 1000: 00001517 auipc a0, 0x1
# CHECK-NEXT: 1004: 00450513 addi a0, a0, 0x4 <target>
# CHECK-NEXT: 1008: 00001517 auipc a0, 0x1
# CHECK-NEXT: 100c: 1571 addi a0, a0, -0x4 <target>
# CHECK-NEXT: 100e: 6509 lui a0, 0x2
# CHECK-NEXT: 1010: 0045059b addiw a1, a0, 0x4 <target>
# CHECK-NEXT: 1014: 6509 lui a0, 0x2
# CHECK-NEXT: 1016: 2511 addiw a0, a0, 0x4 <target>
# CHECK-NEXT: 1018: 00102537 lui a0, 0x102
# CHECK-NEXT: 101c: c50c sw a1, 0x8(a0) <far_target>
# CHECK-NEXT: 101e: 00102537 lui a0, 0x102
# CHECK-NEXT: 1022: 4508 lw a0, 0x8(a0) <far_target>
# CHECK-NEXT: 1024: 6509 lui a0, 0x2
# CHECK-NEXT: 1026: 6585 lui a1, 0x1
# CHECK-NEXT: 1028: 0306 slli t1, t1, 0x1
# CHECK-NEXT: 102a: 0511 addi a0, a0, 0x4 <target>
# CHECK-NEXT: 102c: 0505 addi a0, a0, 0x1
# CHECK-NEXT: 102e: 00200037 lui zero, 0x200
# CHECK-NEXT: 1032: 00a02423 sw a0, 0x8(zero) <abs+0x8>
# CHECK-NEXT: 1036: 00101097 auipc ra, 0x101
# CHECK-NEXT: 103a: fd6080e7 jalr -0x2a(ra) <func>
# CHECK-NEXT: 103e: 640d lui s0, 0x3
# CHECK-NEXT: 1040: 8800 sb s0, 0x0(s0) <zcb>
# CHECK-NEXT: 1042: 4522 lw a0, 0x8(sp)


.global _start
.text

# The core of the feature being added was address resolution for instruction
# sequences where an register is populated by immediate values via two
# separate instructions. First by an instruction that provides the upper bits
# (auipc, lui ...) followed by another instruction for the lower bits (addi,
# jalr, ld ...).

_start:
# Test block 1-3 each focus on a certain starting instruction in a sequences,
# the ones that provide the upper bits. The other sequence is another
# instruction the provides the lower bits. The second instruction is
# arbitrarily chosen to increase code coverage

# test block #1
lla a0, target # addi
auipc a0, 0x1
c.addi a0, -0x4 # c.addi

# test block #2
c.lui a0, 0x2
addiw a1, a0, 0x4 # addiw
c.lui a0, 0x2
c.addiw a0, 0x4 # c.addiw

# test block #3
lui a0, 0x102
sw a1, 0x8(a0) # sw
lui a0, 0x102
c.lw a0, 0x8(a0) # lw

# Test block 4 tests instruction interleaving, essentially the code's
# ability to keep track of a valid sequence even if multiple other unrelated
# instructions separate the two

# test #4
lui a0, 0x2
lui a1, 0x1 # unrelated instruction
slli t1, t1, 0x1 # unrelated instruction
addi a0, a0, 0x4
addi a0, a0, 0x1 # verify register tracking terminates

# Test 5 check instructions providing upper bits does not change the tracked
# value of zero register + ensure load/store instructions accessing data
# relative to the zero register trigger address resolution. The latter kind
# of instructions are essentially memory accesses relative to the zero
# register

# test #5
lui x0, 0x200
sw a0, 0x8(x0)

# Test 6 ensures that the newly added functionality is compatible with
# code that already worked for branch instructions

# test #6
call func

# test #7 zcb extension
lui x8, 0x3
c.sb x8, 0(x8)

# test #8 stack based load/stores
c.lwsp a0, 0x8(sp)

# these are the labels that the instructions above are expecteed to resolve to
.section .data
.skip 0x4
target:
.word 1
.skip 0xff8
zcb:
.word 1
.skip 0xff004
far_target:
.word 2
func:
ret
7 changes: 7 additions & 0 deletions llvm/include/llvm/MC/MCInstrAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Compiler.h"
#include <cstdint>
#include <vector>
Expand Down Expand Up @@ -182,6 +183,12 @@ class LLVM_ABI MCInstrAnalysis {
evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const;

/// Given an instruction that accesses a memory address, try to compute
/// the target address. Return true on success, and the address in \p Target.
virtual bool evaluateInstruction(const MCInst &Inst, uint64_t Addr,
uint64_t Size, uint64_t &Target,
const MCSubtargetInfo &STI) const;

/// Given an instruction tries to get the address of a memory operand. Returns
/// the address on success.
virtual std::optional<uint64_t>
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/MC/MCInstrAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ bool MCInstrAnalysis::evaluateBranch(const MCInst & /*Inst*/, uint64_t /*Addr*/,
return false;
}

bool MCInstrAnalysis::evaluateInstruction(const MCInst &Inst, uint64_t Addr,
uint64_t Size, uint64_t &Target,
const MCSubtargetInfo &STI) const {
return false;
}

std::optional<uint64_t> MCInstrAnalysis::evaluateMemoryOperandAddress(
const MCInst &Inst, const MCSubtargetInfo *STI, uint64_t Addr,
uint64_t Size) const {
Expand Down
105 changes: 99 additions & 6 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include <bitset>
#include <cstdint>

#define GET_INSTRINFO_MC_DESC
#define ENABLE_INSTR_PREDICATE_VERIFIER
Expand Down Expand Up @@ -183,6 +185,17 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
}

switch (Inst.getOpcode()) {
case RISCV::C_LUI:
case RISCV::LUI: {
setGPRState(Inst.getOperand(0).getReg(),
SignExtend64<32>(Inst.getOperand(1).getImm() << 12));
break;
}
case RISCV::AUIPC: {
setGPRState(Inst.getOperand(0).getReg(),
Addr + SignExtend64<32>(Inst.getOperand(1).getImm() << 12));
break;
}
default: {
// Clear the state of all defined registers for instructions that we don't
// explicitly support.
Expand All @@ -194,10 +207,6 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
}
break;
}
case RISCV::AUIPC:
setGPRState(Inst.getOperand(0).getReg(),
Addr + SignExtend64<32>(Inst.getOperand(1).getImm() << 12));
break;
}
}

Expand Down Expand Up @@ -235,6 +244,91 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
return false;
}

bool evaluateInstruction(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo &STI) const override {
unsigned int ArchRegWidth = STI.getTargetTriple().getArchPointerBitWidth();
switch(Inst.getOpcode()) {
default:
return false;
case RISCV::C_ADDI:
case RISCV::ADDI: {
MCRegister Reg = Inst.getOperand(1).getReg();
auto TargetRegState = getGPRState(Reg);
if (TargetRegState && Reg != RISCV::X0) {
Target = *TargetRegState + Inst.getOperand(2).getImm();
Target &= maskTrailingOnes<uint64_t>(ArchRegWidth);
return true;
}
break;
}
case RISCV::C_ADDIW:
case RISCV::ADDIW: {
MCRegister Reg = Inst.getOperand(1).getReg();
auto TargetRegState = getGPRState(Reg);
if (TargetRegState && Reg != RISCV::X0) {
Target = *TargetRegState + Inst.getOperand(2).getImm();
Target = SignExtend64<32>(Target);
return true;
}
break;
}
case RISCV::LB:
case RISCV::LH:
case RISCV::LD:
case RISCV::LW:
case RISCV::LBU:
case RISCV::LHU:
case RISCV::LWU:
case RISCV::SB:
case RISCV::SH:
case RISCV::SW:
case RISCV::SD:
case RISCV::FLH:
case RISCV::FLW:
case RISCV::FLD:
case RISCV::FSH:
case RISCV::FSW:
case RISCV::FSD:
case RISCV::C_LD:
case RISCV::C_SD:
case RISCV::C_FLD:
case RISCV::C_FSD:
case RISCV::C_SW:
case RISCV::C_LW:
case RISCV::C_FSW:
case RISCV::C_FLW:
case RISCV::C_LBU:
case RISCV::C_LH:
case RISCV::C_LHU:
case RISCV::C_SB:
case RISCV::C_SH:
case RISCV::C_LWSP:
case RISCV::C_SWSP:
case RISCV::C_LDSP:
case RISCV::C_SDSP:
case RISCV::C_FLWSP:
case RISCV::C_FSWSP:
case RISCV::C_FLDSP:
case RISCV::C_FSDSP:
case RISCV::C_LD_RV32:
case RISCV::C_SD_RV32:
case RISCV::C_SDSP_RV32:
case RISCV::LD_RV32:
case RISCV::C_LDSP_RV32:
case RISCV::SD_RV32: {
MCRegister Reg = Inst.getOperand(1).getReg();
auto TargetRegState = getGPRState(Reg);
if (TargetRegState) {
Target = *TargetRegState + Inst.getOperand(2).getImm();
return true;
}
break;
}
}
return false;
}

bool isTerminator(const MCInst &Inst) const override {
if (MCInstrAnalysis::isTerminator(Inst))
return true;
Expand Down Expand Up @@ -346,12 +440,11 @@ LLVMInitializeRISCVTargetMC() {
TargetRegistry::RegisterELFStreamer(*T, createRISCVELFStreamer);
TargetRegistry::RegisterObjectTargetStreamer(
*T, createRISCVObjectTargetStreamer);
TargetRegistry::RegisterMCInstrAnalysis(*T, createRISCVInstrAnalysis);

// Register the asm target streamer.
TargetRegistry::RegisterAsmTargetStreamer(*T, createRISCVAsmTargetStreamer);
// Register the null target streamer.
TargetRegistry::RegisterNullTargetStreamer(*T,
createRISCVNullTargetStreamer);
TargetRegistry::RegisterMCInstrAnalysis(*T, createRISCVInstrAnalysis);
}
}
12 changes: 7 additions & 5 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,8 @@ collectLocalBranchTargets(ArrayRef<uint8_t> Bytes, MCInstrAnalysis *MIA,
if (MIA) {
if (Disassembled) {
uint64_t Target;
bool TargetKnown = MIA->evaluateBranch(Inst, Index, Size, Target);
if (TargetKnown && (Target >= Start && Target < End) &&
bool BranchTargetKnown = MIA->evaluateBranch(Inst, Index, Size, Target);
if (BranchTargetKnown && (Target >= Start && Target < End) &&
!Targets.count(Target)) {
// On PowerPC and AIX, a function call is encoded as a branch to 0.
// On other PowerPC platforms (ELF), a function call is encoded as
Expand Down Expand Up @@ -2356,8 +2356,10 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
llvm::raw_ostream *TargetOS = &FOS;
uint64_t Target;
bool PrintTarget = DT->InstrAnalysis->evaluateBranch(
Inst, SectionAddr + Index, Size, Target);

Inst, SectionAddr + Index, Size, Target) ||
DT->InstrAnalysis->evaluateInstruction(
Inst, SectionAddr + Index, Size, Target,
*DT->SubtargetInfo);
if (!PrintTarget) {
if (std::optional<uint64_t> MaybeTarget =
DT->InstrAnalysis->evaluateMemoryOperandAddress(
Expand Down Expand Up @@ -2430,7 +2432,7 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
break;
}

// Branch targets are printed just after the instructions.
// Branch and instruction targets are printed just after the instructions.
// Print the labels corresponding to the target if there's any.
bool BBAddrMapLabelAvailable = BBAddrMapLabels.count(Target);
bool LabelAvailable = AllLabels.count(Target);
Expand Down