Skip to content

Commit

Permalink
fixed code till it compiled, cstool runs successfully (although with …
Browse files Browse the repository at this point in the history
…bad output)
  • Loading branch information
moste00 committed Oct 24, 2024
1 parent d4bf832 commit 08d68d1
Show file tree
Hide file tree
Showing 17 changed files with 23,666 additions and 115,596 deletions.
24 changes: 11 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -570,24 +570,22 @@ if(CAPSTONE_RISCV_SUPPORT)
add_definitions(-DCAPSTONE_HAS_RISCV)
set(SOURCES_RISCV
arch/RISCV/RISCVDisassembler.c
arch/RISCV/RISCVInstPrinter.c
arch/RISCV/RISCVMapping.c
arch/RISCV/RISCVDetails.c
arch/RISCV/RISCVModule.c
)
set(HEADERS_RISCV
arch/RISCV/RISCVBaseInfo.h
arch/RISCV/riscv_ast.gen.inc
arch/RISCV/riscv_decode.gen.inc
arch/RISCV/riscv_ast2str.gen.inc
arch/RISCV/riscv_ast2str_tbls.gen.inc
"include/capstone/riscv_insn.gen.inc"
arch/RISCV/riscv_insn_mapping.gen.inc

arch/RISCV/RISCVDetails.h
arch/RISCV/RISCVDisassembler.h
arch/RISCV/RISCVInstPrinter.h
arch/RISCV/RISCVMapping.h
arch/RISCV/riscv_helpers_ast2str.h
arch/RISCV/riscv_helpers_rvconf.h
arch/RISCV/RISCVModule.h
arch/RISCV/RISCVGenAsmWriter.inc
arch/RISCV/RISCVGenDisassemblerTables.inc
arch/RISCV/RISCVGenInsnNameMaps.inc
arch/RISCV/RISCVGenInstrInfo.inc
arch/RISCV/RISCVGenRegisterInfo.inc
arch/RISCV/RISCVGenSubtargetInfo.inc
arch/RISCV/RISCVMappingInsn.inc
arch/RISCV/RISCVMappingInsnOp.inc
)
endif()

Expand Down
8 changes: 4 additions & 4 deletions arch/RISCV/RISCVDetails.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ The size calculation algorithm according to the RISCV spec:
and it's not worth complicating the code with a bitvector type to represent bigger instructions.)
*/
bool riscv_fill_size(cs_insn *insn, uint8_t first_byte) {
if (first_byte & 0x3 != 0x3) {
if ((first_byte & 0x3) != 0x3) {
insn->size = 2;
} else if ((first_byte >> 2) & 0x7 != 0x7) {
} else if (((first_byte >> 2) & 0x7) != 0x7) {
insn->size = 4;
} else if ((first_byte >> 5) & 0x1 == 0x0) {
} else if (((first_byte >> 5) & 0x1) == 0x0) {
insn->size = 6;
} else if ((first_byte >> 6) & 0x1 == 0x0) {
} else if (((first_byte >> 6) & 0x1) == 0x0) {
insn->size = 8;
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion arch/RISCV/RISCVDetails.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "capstone.h"
#include "../include/capstone/capstone.h"

bool riscv_fill_size(cs_insn *insn, uint8_t binary);
53 changes: 53 additions & 0 deletions arch/RISCV/RISCVDisassembler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "RISCVDisassembler.h"
#include "RISCVDetails.h"

#include "riscv_decode.gen.inc"
#include "riscv_insn_mapping.gen.inc"
#include "riscv_ast2str.gen.inc"

bool riscv_get_instruction(csh handle,
const uint8_t *code, size_t code_len, MCInst *instr,
uint16_t *size, uint64_t address, void *info) {
cs_insn *insn = instr->flat_insn;

if (!riscv_fill_size(insn, code[0])) {
return false;
}
// TODO: add compressed 2-bytes instructions
if (insn->size == 2) {

} else if (insn->size == 4) {
struct ast instruction;
decode(&instruction, code[3] << 24 | code[2] << 16 | code[1] << 8 | code[0]);

insn->id = get_insn_type(&instruction);
insn->address = address;
*size = insn->size;
memcpy(insn->bytes, code, insn->size);

char instruction_as_str[RISCV_MAX_INSTRUCTION_STR_LEN];
riscv_conf conf;
conf.sys_enable_fdext = NULL;
conf.sys_enable_zfinx = NULL;

ast2str(&instruction, instruction_as_str, &conf);

char *curr = instruction_as_str;
uint16_t mnemonic_len = 0;
while (*curr != ' ') {
mnemonic_len++;
curr++;
}
uint16_t operand_len = 0;
while (*curr) {
operand_len++;
curr++;
}
memcpy(insn->mnemonic, instruction_as_str, mnemonic_len);
memcpy(insn->op_str, instruction_as_str + mnemonic_len + 1, operand_len);
return true;
} else {

}
return false;
}
File renamed without changes.
15 changes: 0 additions & 15 deletions arch/RISCV/RISCVDisassembly.c

This file was deleted.

42 changes: 27 additions & 15 deletions arch/RISCV/RISCVModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@
/* RISC-V Backend By Rodrigo Cortes Porto <porto703@gmail.com> &
Shawn Chang <citypw@gmail.com>, HardenedLinux@2018 */

//#ifdef CAPSTONE_HAS_RISCV
#ifdef CAPSTONE_HAS_RISCV

#include "RISCVModule.h"

void noop_printer(MCInst *MI, SStream *OS, void *info) {

}

void noop_postprinter(csh handle, cs_insn *, SStream *mnem, MCInst *mci) {

}

const char *noop_getname(csh handle, unsigned int id) {
return "";
}

void noop_getid(cs_struct *h, cs_insn *insn, unsigned int id) {

}

cs_err RISCV_global_init(cs_struct * ud)
{
MCRegisterInfo *mri;
mri = cs_mem_malloc(sizeof(*mri));

RISCV_init(mri);
ud->printer = RISCV_printInst;
ud->printer_info = mri;
ud->getinsn_info = mri;
ud->disasm = RISCV_getInstruction;
ud->post_printer = NULL;
ud->printer = noop_printer;
ud->printer_info = NULL;
ud->getinsn_info = NULL;
ud->disasm = riscv_get_instruction;
ud->post_printer = noop_postprinter;

ud->reg_name = RISCV_reg_name;
ud->insn_id = RISCV_get_insn_id;
ud->insn_name = RISCV_insn_name;
ud->group_name = RISCV_group_name;
ud->reg_name = noop_getname;
ud->insn_id = noop_getid;
ud->insn_name = noop_getname;
ud->group_name = noop_getname;

return CS_ERR_OK;
}
Expand All @@ -37,4 +49,4 @@ cs_err RISCV_option(cs_struct * handle, cs_opt_type type, size_t value)
return CS_ERR_OK;
}

//#endif
#endif
1 change: 1 addition & 0 deletions arch/RISCV/RISCVModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define CS_RISCV_MODULE_H

#include "../../utils.h"
#include "RISCVDisassembler.h"

cs_err RISCV_global_init(cs_struct * ud);
cs_err RISCV_option(cs_struct * handle, cs_opt_type type, size_t value);
Expand Down
Loading

0 comments on commit 08d68d1

Please sign in to comment.