Skip to content
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

adding mips3 support #6013

Open
wants to merge 27 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3c6cdd1
added all the old stuff, need to decide on getopt for windows stuff
v1X3Q0 Mar 12, 2024
767860f
forgot the unsubmodule changes
v1X3Q0 Mar 12, 2024
8541410
removed windows compat stuff
v1X3Q0 Mar 12, 2024
7c035f2
from master merge
v1X3Q0 Mar 16, 2024
8207bad
early lifting, missing fcmpo
v1X3Q0 Mar 18, 2024
6ce4096
merge resolved, now with G's 64 bit
v1X3Q0 Mar 29, 2024
e6754f7
mac support, needed threadlocal for the disassembler.cpp
v1X3Q0 Mar 29, 2024
f2950ea
moved around the external routines, and added them to the il
v1X3Q0 Mar 29, 2024
ae80211
removed duplicate enums
v1X3Q0 Mar 29, 2024
6c78209
merge in master
v1X3Q0 Apr 3, 2024
6397c23
merging main
v1X3Q0 Apr 7, 2024
83653f2
merge master
v1X3Q0 May 2, 2024
65d0a3e
Merge https://github.com/Vector35/binaryninja-api into dev
v1X3Q0 May 6, 2024
249bb51
fixed disassembly for fp stuff
v1X3Q0 May 7, 2024
4d6c5a7
adding mips tester
v1X3Q0 May 8, 2024
f6c8b41
test rework, gonna add the capstone stuff
v1X3Q0 May 8, 2024
19fe070
adding test updates and new architecture mips3 to disassembly
v1X3Q0 May 9, 2024
26d561f
Merge https://github.com/Vector35/binaryninja-api into dev
v1X3Q0 May 9, 2024
9dcd245
cmakelist needed patch so it could build on mac and windows for mips
v1X3Q0 May 9, 2024
54b36f5
Merge https://github.com/Vector35/binaryninja-api into dev
v1X3Q0 May 18, 2024
cd28c25
pull and test_disasm
v1X3Q0 May 18, 2024
cb81564
Merge https://github.com/Vector35/binaryninja-api into dev
v1X3Q0 May 22, 2024
f449ef5
Merge https://github.com/Vector35/binaryninja-api into dev
v1X3Q0 Jul 2, 2024
1a0517e
merging mips code
v1X3Q0 Aug 16, 2024
dbcca7c
Merge https://github.com/Vector35/binaryninja-api into dev
v1X3Q0 Aug 16, 2024
6b89dc9
merge with main
v1X3Q0 Oct 19, 2024
82e05b3
removing the ppc
v1X3Q0 Oct 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions arch/mips/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,22 @@ if(BN_INTERNAL_BUILD)
LIBRARY_OUTPUT_DIRECTORY ${BN_CORE_PLUGIN_DIR}
RUNTIME_OUTPUT_DIRECTORY ${BN_CORE_PLUGIN_DIR})
endif()

if (DEFINED FORCE_TEST)

if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
add_executable(mipstest mips/mips.c mips/test.c)

target_compile_definitions(mipstest PRIVATE FORCE_TEST=1)

set_target_properties(mipstest PROPERTIES
CXX_STANDARD 17
CXX_VISIBILITY_PRESET hidden
CXX_STANDARD_REQUIRED ON
VISIBILITY_INLINES_HIDDEN ON
POSITION_INDEPENDENT_CODE ON)

target_link_libraries(mipstest PRIVATE arch_mips)
endif()
endif()

42 changes: 28 additions & 14 deletions arch/mips/arch_mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,25 @@ class MipsArchitecture: public Architecture
protected:
size_t m_bits;
BNEndianness m_endian;
MipsVersion version_overwrite;
uint32_t m_decomposeFlags;

virtual bool Disassemble(const uint8_t* data, uint64_t addr, size_t maxLen, Instruction& result)
{
MipsVersion version = version_overwrite;

memset(&result, 0, sizeof(result));
if (mips_decompose((uint32_t*)data, maxLen, &result, m_bits == 64 ? MIPS_64 : MIPS_32, addr, m_endian, m_decomposeFlags) != 0)
if (m_bits == 64)
{
version = MIPS_64;
}

if (mips_decompose((uint32_t*)data, maxLen, &result, version, addr, m_endian, m_decomposeFlags) != 0)
return false;
return true;
}

virtual size_t GetAddressSize() const override
{
return m_bits / 8;
}
virtual size_t GetAddressSize() const override { return m_bits / 8; }

size_t InstructionHasBranchDelay(const Instruction& instr)
{
Expand Down Expand Up @@ -402,8 +407,8 @@ class MipsArchitecture: public Architecture
}

public:
MipsArchitecture(const std::string& name, BNEndianness endian, size_t bits, uint32_t decomposeFlags = 0)
: Architecture(name), m_bits(bits), m_endian(endian), m_decomposeFlags(decomposeFlags)
MipsArchitecture(const std::string& name, BNEndianness endian, size_t bits, MipsVersion version_in, uint32_t decomposeFlags = 0)
: Architecture(name), m_bits(bits), m_endian(endian), version_overwrite(version_in), m_decomposeFlags(decomposeFlags)
{
Ref<Settings> settings = Settings::Instance();
uint32_t flag_pseudo_ops = settings->Get<bool>("arch.mips.disassembly.pseudoOps") ? DECOMPOSE_FLAGS_PSEUDO_OP : 0;
Expand Down Expand Up @@ -3238,18 +3243,20 @@ extern "C"
{
InitMipsSettings();

Architecture* mipsel = new MipsArchitecture("mipsel32", LittleEndian, 32);
Architecture* mipseb = new MipsArchitecture("mips32", BigEndian, 32);
Architecture* mips64el = new MipsArchitecture("mipsel64", LittleEndian, 64);
Architecture* mips64eb = new MipsArchitecture("mips64", BigEndian, 64);
Architecture* cnmips64eb = new MipsArchitecture("cavium-mips64", BigEndian, 64, DECOMPOSE_FLAGS_CAVIUM);
Architecture* mipsel = new MipsArchitecture("mipsel32", LittleEndian, 32, MIPS_32);
Architecture* mipseb = new MipsArchitecture("mips32", BigEndian, 32, MIPS_32);
Architecture* mips3 = new MipsArchitecture("mips3", BigEndian, 32, MIPS_3);
Architecture* mips64el = new MipsArchitecture("mipsel64", LittleEndian, 64, MIPS_64);
Architecture* mips64eb = new MipsArchitecture("mips64", BigEndian, 64, MIPS_64);
Architecture* cnmips64eb = new MipsArchitecture("cavium-mips64", BigEndian, 64, MIPS_64, DECOMPOSE_FLAGS_CAVIUM);

Architecture::Register(mipsel);
Architecture::Register(mipseb);
Architecture::Register(mips3);
Architecture::Register(mips64el);
Architecture::Register(mips64eb);
Architecture::Register(cnmips64eb);

/* calling conventions */
MipsO32CallingConvention* o32LE = new MipsO32CallingConvention(mipsel);
MipsO32CallingConvention* o32BE = new MipsO32CallingConvention(mipseb);
Expand All @@ -3258,9 +3265,11 @@ extern "C"
MipsN64CallingConvention* n64BEc = new MipsN64CallingConvention(cnmips64eb);

mipsel->RegisterCallingConvention(o32LE);
mipseb->RegisterCallingConvention(o32BE);
mipsel->SetDefaultCallingConvention(o32LE);
mipseb->RegisterCallingConvention(o32BE);
mipseb->SetDefaultCallingConvention(o32BE);
mips3->RegisterCallingConvention(o32BE);
mips3->SetDefaultCallingConvention(o32BE);
mips64el->RegisterCallingConvention(n64LE);
mips64el->SetDefaultCallingConvention(n64LE);
mips64eb->RegisterCallingConvention(n64BE);
Expand All @@ -3272,19 +3281,23 @@ extern "C"
MipsLinuxSyscallCallingConvention* linuxSyscallBE = new MipsLinuxSyscallCallingConvention(mipseb);
mipsel->RegisterCallingConvention(linuxSyscallLE);
mipseb->RegisterCallingConvention(linuxSyscallBE);
mips3->RegisterCallingConvention(linuxSyscallBE);

mipsel->RegisterCallingConvention(new MipsLinuxRtlResolveCallingConvention(mipsel));
mipseb->RegisterCallingConvention(new MipsLinuxRtlResolveCallingConvention(mipseb));
mips3->RegisterCallingConvention(new MipsLinuxRtlResolveCallingConvention(mips3));
mips64el->RegisterCallingConvention(new MipsLinuxRtlResolveCallingConvention(mips64el));
mips64eb->RegisterCallingConvention(new MipsLinuxRtlResolveCallingConvention(mips64eb));
cnmips64eb->RegisterCallingConvention(new MipsLinuxRtlResolveCallingConvention(cnmips64eb));

/* function recognizers */
mipsel->RegisterFunctionRecognizer(new MipsImportedFunctionRecognizer());
mipseb->RegisterFunctionRecognizer(new MipsImportedFunctionRecognizer());
mips3->RegisterFunctionRecognizer(new MipsImportedFunctionRecognizer());

mipsel->RegisterRelocationHandler("ELF", new MipsElfRelocationHandler());
mipseb->RegisterRelocationHandler("ELF", new MipsElfRelocationHandler());
mips3->RegisterRelocationHandler("ELF", new MipsElfRelocationHandler());
mips64el->RegisterRelocationHandler("ELF", new MipsElfRelocationHandler());
mips64eb->RegisterRelocationHandler("ELF", new MipsElfRelocationHandler());
cnmips64eb->RegisterRelocationHandler("ELF", new MipsElfRelocationHandler());
Expand All @@ -3303,6 +3316,7 @@ extern "C"
BinaryViewType::RegisterArchitecture("ELF", ARCH_ID_MIPS64, BigEndian, mips64eb);
BinaryViewType::RegisterArchitecture("ELF", ARCH_ID_MIPS32, LittleEndian, mipsel);
BinaryViewType::RegisterArchitecture("ELF", ARCH_ID_MIPS32, BigEndian, mipseb);
BinaryViewType::RegisterArchitecture("ELF", ARCH_ID_MIPS32, BigEndian, mips3);

Ref<BinaryViewType> elf = BinaryViewType::GetByName("ELF");
if (elf)
Expand Down
99 changes: 73 additions & 26 deletions arch/mips/mips/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ b mips_disassemble

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h>

#include "mips.h"

int disassemble(uint32_t insword, uint64_t address, enum MipsVersion version, char *result)
int disassemble(uint32_t insword, uint64_t address, MipsVersion version, char *result)
{
int rc;
Instruction instr;
Expand Down Expand Up @@ -40,42 +41,88 @@ int disassemble(uint32_t insword, uint64_t address, enum MipsVersion version, ch
exit(-1); \
}

void usage(char** av)
{
printf("usage:\n");
printf("\t%s [instruction_words]\n", av[0]);
printf("\t%s test\n", av[0]);
printf("example:\n");
printf("\t%s 3c028081 68435a50 24445a50 6c830007\n", av[0]);
printf("\t%s test\n", av[0]);
exit(-1);
}

int main(int ac, char **av)
{
char instxt[4096];
uint32_t insword = 0;
uint64_t baseaddr = 0;
int instindex = 0;
int c = 0;
int version = MIPS_32;

if(ac == 1) {
printf("usage:\n");
printf("\t%s [<address>] <instruction_word>\n", av[0]);
printf("\t%s <instruction_word>\n", av[0]);
printf("\t%s test\n", av[0]);
printf("examples:\n");
printf("\t%s 0 14E00003\n", av[0]);
printf("\t%s 00405A58 14E00003\n", av[0]);
printf("\t%s test\n", av[0]);
exit(-1);
while ((c = getopt(ac, av, "klmnoa:")) != -1)
{
switch (c)
{
case 'k':
version = MIPS_64;
break;
case 'l':
version = MIPS_1;
break;
case 'm':
version = MIPS_2;
break;
case 'n':
version = MIPS_3;
break;
case 'o':
version = MIPS_4;
break;
case 'a':
baseaddr = strtoull(optarg, NULL, 0x10);
break;
default:
usage(av);
goto cleanup;
}
}

if(ac == 2 && !strcmp(av[1], "test")) {
disassemble(0x14E00003, 0, MIPS_32, instxt);
if (optind >= ac)
{
usage(av);
goto cleanup;
}

instindex = optind;

if (ac == 2 && !strcmp(av[1], "test"))
{
disassemble(0x14E00003, 0, version, instxt);
ASSERT(!strcmp(instxt, "bne\t$a3, $zero, 0x10"));
disassemble(0x14E00003, 0x405a58, MIPS_32, instxt);
disassemble(0x14E00003, 4, version, instxt);
ASSERT(!strcmp(instxt, "bne\t$a3, $zero, 0x405a68"));
exit(0);
}

uint64_t address = 0;
uint32_t insword = 0;
if(ac == 2) {
address = 0;
insword = strtoul(av[1], NULL, 16);
}
else if(ac == 3) {
address = strtoul(av[1], NULL, 16);
insword = strtoul(av[2], NULL, 16);
}
while (instindex < ac)
{
insword = strtoul(av[instindex], NULL, 16);

if(0 == disassemble(insword, address, MIPS_32, instxt)) {
printf("%08" PRIX64 ": %08X %s\n", address, insword, instxt);
if (0 == disassemble(insword, baseaddr, version, instxt))
{
printf("%08llX: %08X %s\n", baseaddr, insword, instxt);
}
else
{
printf("%08llX: %08X ??\n", baseaddr, insword);
}

baseaddr += 4;
instindex++;
}

cleanup:
return 0;
}
2 changes: 1 addition & 1 deletion arch/x86/xed
Submodule xed updated 1372 files