Skip to content

Commit

Permalink
Support RISC-V and PowerPC arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Feb 26, 2023
1 parent f5d25bd commit 0ce0c22
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public static GolangFunction create_function(GolangBinary go_bin, GolangAnalyzer
return new GolangFunctionX86(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}else if(go_bin.is_arm()) {
return new GolangFunctionArm(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}else if(go_bin.is_ppc()) {
return new GolangFunctionPpc(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}else if(go_bin.is_riscv()) {
return new GolangFunctionRiscv(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}else {
return new GolangFunction(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}
Expand All @@ -113,6 +117,10 @@ public static GolangFunction create_function_in_function(GolangBinary go_bin, Go
return new GolangFunctionX86(go_bin, service, func, disasm_option, extended_option);
}else if(go_bin.is_arm()) {
return new GolangFunctionArm(go_bin, service, func, disasm_option, extended_option);
}else if(go_bin.is_ppc()) {
return new GolangFunctionPpc(go_bin, service, func, disasm_option, extended_option);
}else if(go_bin.is_riscv()) {
return new GolangFunctionRiscv(go_bin, service, func, disasm_option, extended_option);
}else {
return new GolangFunction(go_bin, service, func, disasm_option, extended_option);
}
Expand Down Expand Up @@ -182,6 +190,10 @@ String get_reg_arg_name(int arg_count) {
return "";
}

int get_arg_stack_base() {
return 0;
}

boolean check_inst_reg_arg(Instruction inst, Map<Register, REG_FLAG> builtin_reg_state) {
return false;
}
Expand Down Expand Up @@ -230,6 +242,7 @@ private boolean init_params() {

try {
params=new ArrayList<>();
int stack_base=get_arg_stack_base();
int stack_count=0;
for(int i=0;i<args_num && i<50;i++) {
int size=pointer_size;
Expand Down Expand Up @@ -268,7 +281,7 @@ private boolean init_params() {
}
Parameter add_param;
if(reg==null) {
add_param=new ParameterImpl(String.format("param_%d", i+1), datatype, (stack_count+1)*pointer_size, func.getProgram(), SourceType.USER_DEFINED);
add_param=new ParameterImpl(String.format("param_%d", i+1), datatype, stack_base+(stack_count+1)*pointer_size, func.getProgram(), SourceType.USER_DEFINED);
stack_count++;
}else {
add_param=new ParameterImpl(String.format("param_%d", i+1), datatype, reg, func.getProgram(), SourceType.USER_DEFINED);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package golanganalyzerextension.function;

import java.util.Map;

import ghidra.program.model.address.Address;
import ghidra.program.model.lang.Register;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Instruction;
import golanganalyzerextension.gobinary.GolangBinary;
import golanganalyzerextension.service.GolangAnalyzerExtensionService;

public class GolangFunctionPpc extends GolangFunction {

private static final String[] reg_arg_str={"r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r14", "r15", "r16", "r17"};

GolangFunctionPpc(GolangBinary go_bin, GolangAnalyzerExtensionService service, Address func_info_addr, long func_size, boolean disasm_option, boolean extended_option) {
super(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}

GolangFunctionPpc(GolangBinary go_bin, GolangAnalyzerExtensionService service, Function func, boolean disasm_option, boolean extended_option) {
super(go_bin, service, func, disasm_option, extended_option);
}

@Override
String get_reg_arg_name(int arg_count) {
if(arg_count<0 || reg_arg_str.length<=arg_count) {
return "";
}
return reg_arg_str[arg_count];
}

@Override
int get_arg_stack_base() {
return go_bin.get_pointer_size()*3;
}

@Override
boolean check_inst_reg_arg(Instruction inst, Map<Register, REG_FLAG> builtin_reg_state) {
if(go_bin.lt_go_version("go1.18beta1") || go_bin.get_pointer_size()!=8) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package golanganalyzerextension.function;

import java.util.Map;

import ghidra.program.model.address.Address;
import ghidra.program.model.lang.Register;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Instruction;
import golanganalyzerextension.gobinary.GolangBinary;
import golanganalyzerextension.service.GolangAnalyzerExtensionService;

public class GolangFunctionRiscv extends GolangFunction {

private static final String[] reg_arg_str={"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7"};

GolangFunctionRiscv(GolangBinary go_bin, GolangAnalyzerExtensionService service, Address func_info_addr, long func_size, boolean disasm_option, boolean extended_option) {
super(go_bin, service, func_info_addr, func_size, disasm_option, extended_option);
}

GolangFunctionRiscv(GolangBinary go_bin, GolangAnalyzerExtensionService service, Function func, boolean disasm_option, boolean extended_option) {
super(go_bin, service, func, disasm_option, extended_option);
}

@Override
String get_reg_arg_name(int arg_count) {
if(arg_count<0 || reg_arg_str.length<=arg_count) {
return "";
}
return reg_arg_str[arg_count];
}

@Override
boolean check_inst_reg_arg(Instruction inst, Map<Register, REG_FLAG> builtin_reg_state) {
if(go_bin.lt_go_version("go1.18beta1") || go_bin.get_pointer_size()!=8) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ public boolean is_arm() {
return program.getLanguage().getProcessor().toString().equals("ARM") || program.getLanguage().getProcessor().toString().equals("AARCH64");
}

public boolean is_ppc() {
return program.getLanguage().getProcessor().toString().equals("PowerPC");
}

public boolean is_riscv() {
return program.getLanguage().getProcessor().toString().equals("RISCV");
}

public void disassemble(Address addr, long size) throws BinaryAccessException {
Address addr_end=get_address(addr, size);

Expand Down

0 comments on commit 0ce0c22

Please sign in to comment.