Skip to content

Commit c604207

Browse files
committed
lldb/ObjectFile,Disassembler: read some state from the executable
Add support to inspect the ELF headers for RISCV targets to determine if RVC or RVE are enabled and the floating point support to enable. As per the RISCV specification, d implies f, q implies d implies f, which gives us the cascading effect that is used to enable the features when setting up the disassembler. With this change, it is now possible to attach the debugger to a remote process and be able to disassemble the instruction stream. ~~~ $ bin/lldb tmp/reduced (lldb) target create "reduced" Current executable set to '/tmp/reduced' (riscv64). (lldb) gdb-remote localhost:1234 (lldb) Process 5737 stopped * thread #1, name = 'reduced', stop reason = signal SIGTRAP frame #0: 0x0000003ff7fe1b20 -> 0x3ff7fe1b20: mv a0, sp 0x3ff7fe1b22: jal 1936 0x3ff7fe1b26: mv s0, a0 0x3ff7fe1b28: auipc a0, 27 ~~~
1 parent 1f45a10 commit c604207

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lldb/include/lldb/Utility/ArchSpec.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ class ArchSpec {
9292
eARM_abi_hard_float = 0x00000400
9393
};
9494

95+
enum RISCVeflags {
96+
eRISCV_rvc = 0x00000001, /// RVC, +c
97+
eRISCV_float_abi_soft = 0x00000000, /// soft float
98+
eRISCV_float_abi_single = 0x00000002, /// single precision floating point, +f
99+
eRISCV_float_abi_double = 0x00000004, /// double precision floating point, +d
100+
eRISCV_float_abi_quad = 0x00000006, /// quad precision floating point, +q
101+
eRISCV_float_abi_mask = 0x00000006,
102+
eRISCV_rve = 0x00000008, /// RVE, +e
103+
eRISCV_tso = 0x00000010, /// RVTSO (total store ordering)
104+
};
105+
95106
enum RISCVSubType {
96107
eRISCVSubType_unknown,
97108
eRISCVSubType_riscv32,

lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,24 @@ DisassemblerLLVMC::DisassemblerLLVMC(const ArchSpec &arch,
11871187
cpu = "apple-latest";
11881188
}
11891189

1190+
if (triple.isRISCV()) {
1191+
uint32_t arch_flags = arch.GetFlags();
1192+
if (arch_flags & ArchSpec::eRISCV_rvc)
1193+
features_str += "+c,";
1194+
if (arch_flags & ArchSpec::eRISCV_rve)
1195+
features_str += "+e,";
1196+
if ((arch_flags & ArchSpec::eRISCV_float_abi_single) ==
1197+
ArchSpec::eRISCV_float_abi_single)
1198+
features_str += "+f,";
1199+
if ((arch_flags & ArchSpec::eRISCV_float_abi_double) ==
1200+
ArchSpec::eRISCV_float_abi_double)
1201+
features_str += "+f,+d,";
1202+
if ((arch_flags & ArchSpec::eRISCV_float_abi_quad) ==
1203+
ArchSpec::eRISCV_float_abi_quad)
1204+
features_str += "+f,+d,+q,";
1205+
// FIXME: how do we detect features such as `+a`, `+m`?
1206+
}
1207+
11901208
// We use m_disasm_up.get() to tell whether we are valid or not, so if this
11911209
// isn't good for some reason, we won't be valid and FindPlugin will fail and
11921210
// we won't get used.

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,28 @@ size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl &section_headers,
13641364
arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);
13651365
}
13661366

1367+
if (arch_spec.GetMachine() == llvm::Triple::riscv32 ||
1368+
arch_spec.GetMachine() == llvm::Triple::riscv64) {
1369+
uint32_t flags = arch_spec.GetFlags();
1370+
1371+
if (header.e_flags & llvm::ELF::EF_RISCV_RVC)
1372+
flags |= ArchSpec::eRISCV_rvc;
1373+
if (header.e_flags & llvm::ELF::EF_RISCV_RVE)
1374+
flags |= ArchSpec::eRISCV_rve;
1375+
1376+
if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) ==
1377+
llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE)
1378+
flags |= ArchSpec::eRISCV_float_abi_single;
1379+
else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) ==
1380+
llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE)
1381+
flags |= ArchSpec::eRISCV_float_abi_double;
1382+
else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) ==
1383+
llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD)
1384+
flags |= ArchSpec::eRISCV_float_abi_quad;
1385+
1386+
arch_spec.SetFlags(flags);
1387+
}
1388+
13671389
// If there are no section headers we are done.
13681390
if (header.e_shnum == 0)
13691391
return 0;

0 commit comments

Comments
 (0)