diff --git a/.github/workflows/ci_gcc.yml b/.github/workflows/ci_gcc.yml index 448b933..c6bbd48 100644 --- a/.github/workflows/ci_gcc.yml +++ b/.github/workflows/ci_gcc.yml @@ -1,6 +1,6 @@ # CI file for GCC builds -name: Build GCC libgultra +name: Build GCC libultra # Build on every branch push, tag push, and pull request change: on: [push, pull_request_target] @@ -13,8 +13,7 @@ jobs: strategy: fail-fast: false matrix: - version: [L] # [H, I, I_patch, J, K, L] - suffix: [~, _d, _rom] + version: [libultra, libultra_d, libultra_rom] steps: - name: Checkout repository @@ -28,11 +27,8 @@ jobs: - name: Verify formatting on all files run: python3 tools/check_format.py --verbose - - name: Setup - run: make setup -j $(nproc) TARGET=libgultra${{ matrix.suffix }} VERSION=${{ matrix.version }} - - - name: Build libgultra${{ matrix.suffix }} ${{ matrix.version }} - run: make -j $(nproc) TARGET=libgultra${{ matrix.suffix }} VERSION=${{ matrix.version }} + - name: Build ${{ matrix.version }} + run: make -j $(nproc) VERSION=${{ matrix.version }} - name: 'Upload Artifact' uses: actions/upload-artifact@v4 diff --git a/Makefile b/Makefile index 223f47c..32aea90 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,41 @@ -# One of: -# libgultra_rom, libgultra_d, libgultra -TARGET ?= libgultra_rom -VERSION ?= L -VERBOSE ?= 0 - -# Use handwritten ASM implementations of select `gu` functions -MGU_ASM ?= 1 +# Makefile to build libultra include util.mk -ifeq ($(VERBOSE), 0) -V=@ -else -V= +# Preprocessor definitions + +WORKING_DIR := $(shell pwd) + +DEFINES := + +SRC_DIRS := + +# Whether to hide commands or not +VERBOSE ?= 0 +ifeq ($(VERBOSE),0) + V := @ +endif + +# Whether to colorize build messages +COLOR ?= 1 + +# TARGET - selects the version of the library to build +# libultra - standard library +# libultra_d - debug library +# libultra_rom - final ROM library +TARGET ?= libultra_rom +$(eval $(call validate-option,TARGET,libultra libultra_d libultra_rom)) + +ifeq ($(TARGET),libultra) + OPT_FLAGS := -Os -ggdb3 -ffast-math -fno-unsafe-math-optimizations + DEFINES += NDEBUG=1 +else ifeq ($(TARGET),libultra_d) + OPT_FLAGS := -Og -ggdb3 -ffast-math -fno-unsafe-math-optimizations + DEFINES += _DEBUG=1 +else ifeq ($(TARGET),libultra_rom) + OPT_FLAGS := -Os -ggdb3 -ffast-math -fno-unsafe-math-optimizations + DEFINES += NDEBUG=1 + DEFINES += _FINALROM=1 endif # detect prefix for MIPS toolchain @@ -36,98 +59,149 @@ else $(error Unable to detect a suitable MIPS toolchain installed) endif -BUILD_ROOT := build -BUILD_DIR := $(BUILD_ROOT)/ -BUILD_AR := $(BUILD_DIR)/$(TARGET).a - -WORKING_DIR := $(shell pwd) - -CPP := cpp -P -AR := $(CROSS)ar - -VERSION_DEFINE := -DBUILD_VERSION_STRING=\"2.0$(VERSION)\" - -ifeq ($(findstring _d,$(TARGET)),_d) -DEBUGFLAG := -D_DEBUG -else -DEBUGFLAG := -DNDEBUG +ifeq ($(filter clean,$(MAKECMDGOALS)),) + $(info ==== Build Options ====) + $(info Version: $(TARGET)) + $(info =======================) endif -AS := $(CROSS)gcc -x assembler-with-cpp -CC := $(CROSS)gcc +#==============================================================================# +# Target Executable and Sources # +#==============================================================================# +BUILD_DIR_BASE := build +# BUILD_DIR is the location where all build artifacts are placed +BUILD_DIR := $(BUILD_DIR_BASE)/$(TARGET) +LIB := $(BUILD_DIR)/$(TARGET).a + +# Directories containing source files +SRC_DIRS += $(shell find src -type d) + +C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) +S_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.s)) + +# Object files +O_FILES := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=.o)) \ + $(foreach file,$(S_FILES),$(BUILD_DIR)/$(file:.s=.o)) + +# Automatic dependency files +DEP_FILES := $(O_FILES:.o=.d) + +#==============================================================================# +# Compiler Options # +#==============================================================================# + +AS := $(CROSS)as +CC := $(CROSS)gcc +CPP := $(CROSS)cpp +LD := $(CROSS)ld +AR := $(CROSS)ar + +# Do NOT depend on system-installed headers! If you need to make a header change, +# test it in your source first! +INCLUDE_DIRS += $(WORKING_DIR)/include $(WORKING_DIR)/include/PR $(WORKING_DIR)/include/compiler/modern_gcc $(BUILD_DIR) $(BUILD_DIR)/include $(WORKING_DIR)/src $(WORKING_DIR) + +GBIDEFINE := -DF3DEX_GBI_2 + +C_DEFINES = $(foreach d,$(DEFINES),-D$(d)) +DEF_INC_CFLAGS = $(foreach i,$(INCLUDE_DIRS),-I$(i)) $(C_DEFINES) WARNINGS := -Wall -Wextra -Wno-format-security -Wno-unused-function -Wno-unused-parameter -Wno-unused-variable -Wno-builtin-declaration-mismatch WARNINGS += -Wno-int-conversion -Wno-incompatible-pointer-types -Wno-implicit-function-declaration # TODO: Try adjusting code to remove these -CFLAGS := -G 0 -c -nostdinc -march=vr4300 -mfix4300 -mabi=32 -mno-abicalls -mdivide-breaks -fno-PIC -fno-common -ffreestanding -fbuiltin -fno-builtin-sinf -fno-builtin-cosf -funsigned-char $(WARNINGS) +CFLAGS := -G 0 -c -nostdinc -march=vr4300 -mfix4300 -mabi=32 -mips3 -mno-abicalls -mdivide-breaks -fno-PIC -fno-common -ffreestanding -fbuiltin -fno-builtin-sinf -fno-builtin-cosf -funsigned-char $(WARNINGS) CFLAGS += -fno-strict-aliasing # TODO: Try adjusting code to remove this -ASFLAGS := -w -nostdinc -c -G 0 -march=vr4300 -mabi=32 -mgp32 -mfp32 -DMIPSEB -D_LANGUAGE_ASSEMBLY -D_MIPS_SIM=1 -D_ULTRA64 -CPPFLAGS = -DMODERN_CC -D_MIPS_SZLONG=32 -D__USE_ISOC99 $(GBIDEFINE) $(VERSION_DEFINE) $(DEBUGFLAG) -IINC = -I . -I $(WORKING_DIR)/include -I $(WORKING_DIR)/include/compiler/modern_gcc -I $(WORKING_DIR)/include/PR -MIPS_VERSION := -mips3 -ASOPTFLAGS := +CFLAGS += -D_MIPS_SZLONG=32 -D__USE_ISOC99 $(C_DEFINES) $(DEF_INC_CFLAGS) -ifeq ($(findstring _d,$(TARGET)),_d) -OPTFLAGS := -Og -ggdb3 -ffast-math -fno-unsafe-math-optimizations -else -OPTFLAGS := -Os -ggdb3 -ffast-math -fno-unsafe-math-optimizations +# C preprocessor flags +CPPFLAGS := -P -Wno-trigraphs $(DEF_INC_CFLAGS) + +# tools +PRINT = printf + +ifeq ($(COLOR),1) +NO_COL := \033[0m +RED := \033[0;31m +GREEN := \033[0;32m +BLUE := \033[0;34m +YELLOW := \033[0;33m +BLINK := \033[33;5m endif -ifeq ($(findstring _rom,$(TARGET)),_rom) -CPPFLAGS += -D_FINALROM -endif +# Common build print status function +define print + @$(PRINT) "$(GREEN)$(1) $(YELLOW)$(2)$(GREEN) -> $(BLUE)$(3)$(NO_COL)\n" +endef -SRC_DIRS := $(shell find src -type d) -C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) -S_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.s)) +#==============================================================================# +# Main Targets # +#==============================================================================# -# Versions J and below used the C matrix math implementations -MGU_MATRIX_FILES := mtxcatf normalize scale translate -ifeq ($(MGU_ASM), 1) -C_FILES := $(filter-out $(addprefix src/gu/,$(MGU_MATRIX_FILES:=.c)),$(C_FILES)) -else -S_FILES := $(filter-out $(addprefix src/mgu/,$(MGU_MATRIX_FILES:=.s)),$(S_FILES)) -endif - -C_O_FILES := $(foreach f,$(C_FILES:.c=.o),$(BUILD_DIR)/$f) -S_O_FILES := $(foreach f,$(S_FILES:.s=.o),$(BUILD_DIR)/$f) -O_FILES := $(S_O_FILES) $(C_O_FILES) - -$(shell mkdir -p src $(foreach dir,$(SRC_DIRS),$(BUILD_DIR)/$(dir))) - -.PHONY: all clean distclean setup -all: $(BUILD_AR) - -$(BUILD_AR): $(O_FILES) - @printf " [AR] $@\n" - $(V)$(AR) rcs $@ $^ +# Default target +default: $(LIB) clean: - $(RM) -rf $(BUILD_DIR) + $(RM) -r $(BUILD_DIR_BASE) -distclean: - $(RM) -rf extracted/ $(BUILD_ROOT) +ALL_DIRS := $(BUILD_DIR) $(addprefix $(BUILD_DIR)/,$(SRC_DIRS)) -GBIDEFINE := -DF3DEX_GBI +# Make sure build directory exists before compiling anything +$(shell mkdir -p $(ALL_DIRS)) +$(BUILD_DIR)/src/voice/%.o: CFLAGS += -I$(WORKING_DIR)/src/voice +$(BUILD_DIR)/src/voice/%.o: DEFINES += LANG_JAPANESE=1 $(BUILD_DIR)/src/gu/parse_gbi.o: GBIDEFINE := -DF3D_GBI $(BUILD_DIR)/src/gu/us2dex_emu.o: GBIDEFINE := $(BUILD_DIR)/src/gu/us2dex2_emu.o: GBIDEFINE := $(BUILD_DIR)/src/sp/sprite.o: GBIDEFINE := -DF3D_GBI $(BUILD_DIR)/src/sp/spriteex.o: GBIDEFINE := $(BUILD_DIR)/src/sp/spriteex2.o: GBIDEFINE := -$(BUILD_DIR)/src/voice/%.o: OPTFLAGS += -DLANG_JAPANESE -I$(WORKING_DIR)/src -I$(WORKING_DIR)/src/voice -$(BUILD_DIR)/src/voice/%.o: CC := $(WORKING_DIR)/tools/compile_sjis.py -D__CC=$(CC) -D__BUILD_DIR=$(BUILD_DIR) + +#==============================================================================# +# Compilation Recipes # +#==============================================================================# + +# Compile C code +$(BUILD_DIR)/src/voice/%.o: src/voice/%.c + $(call print,Compiling:,$<,$@) + $(V)tools/compile_sjis.py -D__CC=$(CC) -D__BUILD_DIR=$(BUILD_DIR) -c $(CFLAGS) -MMD -MP -o $@ $< $(BUILD_DIR)/%.o: %.c - @printf " [CC] $<\n" - $(V)$(CC) $(CFLAGS) $(MIPS_VERSION) $(CPPFLAGS) $(OPTFLAGS) $< $(IINC) -o $@ - $(V)tools/set_o32abi_bit.py $@ + $(call print,Compiling:,$<,$@) + $(V)$(CC) -c $(CFLAGS) $(GBIDEFINE) -MMD -MP -o $@ $< +$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c + $(call print,Compiling:,$<,$@) + $(V)$(CC) -c $(CFLAGS) $(GBIDEFINE) -MMD -MP -o $@ $< +# Assemble assembly code $(BUILD_DIR)/%.o: %.s - @printf " [AS] $<\n" - $(V)$(AS) $(ASFLAGS) $(MIPS_VERSION) $(CPPFLAGS) $(ASOPTFLAGS) $< $(IINC) -o $@ - $(V)tools/set_o32abi_bit.py $@ + $(call print,Assembling:,$<,$@) + $(V)$(CC) -c $(CFLAGS) $(foreach i,$(INCLUDE_DIRS),-Wa,-I$(i)) -x assembler-with-cpp -DMIPSEB -D_LANGUAGE_ASSEMBLY -D_ULTRA64 -MMD -MP -o $@ $< + +# Creating final library file +$(LIB): $(O_FILES) + @$(PRINT) "$(GREEN)Creating $(TARGET): $(BLUE)$@ $(NO_COL)\n" + $(V)$(AR) rcs -o $@ $(O_FILES) + +all: $(BUILD_DIR_BASE)/libultra.a $(BUILD_DIR_BASE)/libultra_d.a $(BUILD_DIR_BASE)/libultra_rom.a + +$(BUILD_DIR_BASE)/libultra.a: + $(V)$(MAKE) TARGET=libultra + $(V)cp $(BUILD_DIR_BASE)/libultra/libultra.a $(BUILD_DIR_BASE) + +$(BUILD_DIR_BASE)/libultra_d.a: + $(V)$(MAKE) TARGET=libultra_d + $(V)cp $(BUILD_DIR_BASE)/libultra_d/libultra_d.a $(BUILD_DIR_BASE) + +$(BUILD_DIR_BASE)/libultra_rom.a: + $(V)$(MAKE) TARGET=libultra_rom + $(V)cp $(BUILD_DIR_BASE)/libultra_rom/libultra_rom.a $(BUILD_DIR_BASE) + +.PHONY: clean default all install pkginstall +# with no prerequisites, .SECONDARY causes no intermediate target to be removed +.SECONDARY: + +# Remove built-in rules, to improve performance +MAKEFLAGS += --no-builtin-rules + +-include $(DEP_FILES) -# Disable built-in rules -.SUFFIXES: print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true diff --git a/include/PR/gtoff.h b/include/PR/gtoff.h new file mode 100644 index 0000000..f8838ee --- /dev/null +++ b/include/PR/gtoff.h @@ -0,0 +1,12 @@ +/* GENERATED FILE, DO NOT EDIT! */ + +/* gtState_t structure offsets for assembly language: */ +#define GT_STATE_SIZE 88 +#define GT_STATE_OFF_RENDSTATE 0x00 +#define GT_STATE_OFF_TEXSTATE 0x04 +#define GT_STATE_OFF_VTXCOUNT 0x08 +#define GT_STATE_OFF_VTXV0 0x09 +#define GT_STATE_OFF_TRICOUNT 0x0a +#define GT_STATE_OFF_RDPCMDS 0x0c +#define GT_STATE_OFF_OTHERMODE 0x10 +#define GT_STATE_OFF_TRANSFORM 0x18 diff --git a/include/PR/os_libc.h b/include/PR/os_libc.h index ec0e89b..cb53cbf 100644 --- a/include/PR/os_libc.h +++ b/include/PR/os_libc.h @@ -76,15 +76,9 @@ extern "C" { /* byte string operations */ -#ifndef MODERN_CC -extern void bcopy(const void*, void*, int); -extern int bcmp(const void*, const void*, int); -extern void bzero(void*, int); -#else extern void bcopy(const void*, void*, size_t); extern int bcmp(const void*, const void*, size_t); extern void bzero(void*, size_t); -#endif /* Printf */ diff --git a/include/PR/rmon.h b/include/PR/rmon.h index 64f1640..def309f 100644 --- a/include/PR/rmon.h +++ b/include/PR/rmon.h @@ -29,7 +29,6 @@ extern "C" { #define RMON_DBG_BUF_SIZE 2048 #define RMON_STACKSIZE 0x1000 -extern void rmonMain(void*); extern void rmonPrintf(const char*, ...); #ifdef _LANGUAGE_C_PLUS_PLUS diff --git a/include/PR/sptaskoff.h b/include/PR/sptaskoff.h new file mode 100644 index 0000000..4ceace6 --- /dev/null +++ b/include/PR/sptaskoff.h @@ -0,0 +1,17 @@ +#define OS_TASK_SIZE 64 +#define OS_TASK_OFF_TYPE 0 +#define OS_TASK_OFF_FLAGS 4 +#define OS_TASK_OFF_UBOOT 8 +#define OS_TASK_OFF_UBOOT_SZ 12 +#define OS_TASK_OFF_UCODE 16 +#define OS_TASK_OFF_UCODE_SZ 20 +#define OS_TASK_OFF_UDATA 24 +#define OS_TASK_OFF_UDATA_SZ 28 +#define OS_TASK_OFF_STACK 32 +#define OS_TASK_OFF_STACK_SZ 36 +#define OS_TASK_OFF_OUTBUFF 40 +#define OS_TASK_OFF_OUTBUFF_SZ 44 +#define OS_TASK_OFF_DATA 48 +#define OS_TASK_OFF_DATA_SZ 52 +#define OS_TASK_OFF_YIELD 56 +#define OS_TASK_OFF_YIELD_SZ 60 diff --git a/include/PRinternal/rmonint.h b/include/PRinternal/rmonint.h deleted file mode 100644 index 66d8e2d..0000000 --- a/include/PRinternal/rmonint.h +++ /dev/null @@ -1,168 +0,0 @@ -#ifndef _RMONINT_H -#define _RMONINT_H - -#include "PRinternal/dbgproto.h" -#include "PR/os_internal.h" - -/* mips */ - -#define MIPS_LWC2_OPCODE 50 -#define MIPS_SWC2_OPCODE 58 - -#define MIPS_LW_OPCODE 35 -#define MIPS_SW_OPCODE 43 - -#define MIPS_BREAK_OPCODE 0xD -#define MIPS_BREAK_MASK 0xFC00003F - -#define MIPS_BREAK(code) ((((code) & 0xFFFFF) << 6) | MIPS_BREAK_OPCODE) - -/* R4300 General Purpose Register Indices */ -#define GREG_IDX_ZERO 0 -#define GREG_IDX_AT 1 -#define GREG_IDX_T9 25 -#define GREG_IDX_K0 26 -#define GREG_IDX_GP 28 -#define GREG_IDX_RA 31 -#define GREG_IDX_LO 32 -#define GREG_IDX_HI 33 -#define GREG_IDX_CAUSE 34 -#define GREG_IDX_PC 35 -#define GREG_IDX_SR 36 - -/* RSP Scalar Register Indices */ -#define SREG_IDX_ZERO 0 -#define SREG_IDX_RA 31 -#define SREG_IDX_DRAM_ADDR (32 + 0) -#define SREG_IDX_MEM_ADDR (32 + 1) -#define SREG_IDX_RD_LEN (32 + 2) -#define SREG_IDX_PC (32 + 3) -#define SREG_IDX_WR_LEN (32 + 4) -#define SREG_IDX_STATUS (32 + 5) -#define SREG_IDX_DMA_FULL (32 + 6) -#define SREG_IDX_DMA_BUSY (32 + 7) - -/* RSP Vector Register Properties */ -#define VREG_NUM 32 -#define VREG_SIZE 0x10 - -/* rmon */ - -#define RMON_MESG_CPU_BREAK 2 -#define RMON_MESG_SP_BREAK 4 -#define RMON_MESG_FAULT 8 - -#define RMON_CPU 0 -#define RMON_RSP 1 - -/* "thread id" for rsp */ -#define RMON_TID_RSP 1000 -/* "thread priority" for rsp */ -#define RMON_PRI_RSP 42 - -/* "thread id" for no thread running */ -#define RMON_TID_NOTHREAD 1003 - -#define RMON_PID_CPU 1002 -#define RMON_PID_RSP 1001 - -/* Largest serviceable read/write memory request */ -#define RMON_MAX_XFER_SIZE 1024 - -/* rmonmain */ - -void __rmonSendHeader(KKHeader* const block, u32 blockSize, u32 type); -void __rmonSendReply(KKHeader* const block, u32 blockSize, u32 replyType); -void __rmonSendData(char* const block, unsigned int blockSize); - -extern int __rmonActive; - -/* rmonmisc */ - -void __rmonInit(void); -void __rmonPanic(void); - -extern OSMesgQueue __rmonMQ; - -/* rmonmem */ - -void __rmonWriteWordTo(u32* addr, u32 val); -u32 __rmonReadWordAt(u32* addr); -void __rmonMemcpy(u8* dest, u8* srce, u32 count); -void __rmonCopyWords(u32* dest, u32* srce, u32 count); - -extern u8 __rmonUtilityBuffer[]; - -/* rmonsio */ - -void __rmonSendFault(OSThread* thread); -void __rmonIOflush(void); -void __rmonIOputw(u32 word); -void __rmonIOhandler(void); - -extern void* __osRdb_DbgRead_Buf; -extern u8 rmonRdbReadBuf[]; - -/* rmonrcp */ - -int __rmonRCPrunning(void); -void __rmonIdleRCP(void); -void __rmonStepRCP(void); -void __rmonRunRCP(void); - -/* rmonbrk */ - -u32 __rmonGetBranchTarget(int method, int thread, char* addr); -int __rmonSetSingleStep(int thread, u32* instptr); -void __rmonGetExceptionStatus(KKStatusEvent* reply); -void __rmonHitBreak(void); -void __rmonHitSpBreak(void); -void __rmonHitCpuFault(void); - -extern u8 __rmonRcpAtBreak; - -/* rmonregs */ - -u32 __rmonGetRegisterContents(int method, int threadNumber, int regNumber); - -/* rmontask */ - -void __rmonMaskIdleThreadInts(void); -OSThread* __rmonGetTCB(int threadNumber); -int __rmonStopUserThreads(int whichThread); -int __rmonGetThreadStatus(int method, int id, KKStatusEvent* reply); - -/* rmoncmds */ - -int __rmonExecute(KKHeader* request); - -/* commands */ - -typedef int (*FUNPTR)(); - -int __rmonLoadProgram(KKHeader* req); -int __rmonListProcesses(KKHeader* req); -int __rmonGetExeName(KKHeader* req); -int __rmonListThreads(KKHeader* req); -int __rmonThreadStatus(KKHeader* req); -int __rmonStopThread(KKHeader* req); -int __rmonRunThread(KKHeader* req); -int __rmonSetFault(KKHeader* req); -int __rmonGetRegionCount(KKHeader* req); -int __rmonGetRegions(KKHeader* req); -int __rmonGetGRegisters(KKHeader* req); -int __rmonSetGRegisters(KKHeader* req); -int __rmonGetFRegisters(KKHeader* req); -int __rmonSetFRegisters(KKHeader* req); -int __rmonReadMem(KKHeader* req); -int __rmonWriteMem(KKHeader* req); -int __rmonSetBreak(KKHeader* req); -int __rmonClearBreak(KKHeader* req); -int __rmonListBreak(KKHeader* req); -int __rmonSetComm(KKHeader* req); -int __rmonGetSRegs(KKHeader* req); -int __rmonSetSRegs(KKHeader* req); -int __rmonGetVRegs(KKHeader* req); -int __rmonSetVRegs(KKHeader* req); - -#endif diff --git a/include/ultra64.h b/include/ultra64.h index 053db38..6bcb680 100644 --- a/include/ultra64.h +++ b/include/ultra64.h @@ -22,6 +22,9 @@ #ifndef _ULTRA64_H_ #define _ULTRA64_H_ +#ifdef __cplusplus +extern "C" { +#endif #include #include #include @@ -36,5 +39,8 @@ #include #include #include +#ifdef __cplusplus +} +#endif #endif diff --git a/src/error/kmcprintf.c b/src/error/kmcprintf.c deleted file mode 100644 index efe165f..0000000 --- a/src/error/kmcprintf.c +++ /dev/null @@ -1,245 +0,0 @@ -// This file was added in 2.0J and removed in 2.0K -#include "stdarg.h" -#include "PR/os.h" -#include "PR/rcp.h" -#include "PR/rdb.h" -#include "ultraerror.h" -#include "../libc/xstdio.h" - -extern u32 __kmc_pt_mode; - -static void* proutSyncPrintf(void* str, const char* buf, size_t n) { - size_t sent = 0; - - while (sent < n) { - sent += __osRdbSend(buf + sent, n - sent, RDB_TYPE_GtoH_PRINT); - } - return 1; -} - -static volatile unsigned int* stat = (unsigned*)0xbff08004; -static volatile unsigned int* wport = (unsigned*)0xbff08000; -static volatile unsigned int* piok = (unsigned*)PHYS_TO_K1(PI_STATUS_REG); - -static void rmonPutchar(char c) { - while (*piok & (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY)) { - } - - while (!(*stat & 4)) { - } - - *wport = c; -} - -static void* kmc_proutSyncPrintf(void* str, const char* buf, int n) { - int i; - char c; - char* p; - char* q; - char xbuf[128]; - static int column = 0; - - p = &xbuf; - - for (i = 0; i < n; i++) { - c = *buf++; - - switch (c) { - case '\n': - *p++ = '\n'; - column = 0; - break; - case '\t': - do { - *p++ = ' '; - } while (++column % 8); - break; - default: - column++; - *p++ = c; - break; - } - - if (c == '\n' || (p - xbuf) > 100) { - rmonPutchar((p - xbuf) - 1); - - q = xbuf; - while (q != p) { - rmonPutchar(*q++); - } - p = xbuf; - } - } - if (p != xbuf) { - rmonPutchar((p - xbuf) - 1); - - q = xbuf; - while (q != p) { - rmonPutchar(*q++); - } - } - return (void*)1; -} - -char NULSTR[] = ""; - -const char* __os_error_message[] = { - NULSTR, - "osCreateThread: stack pointer not aligned to 8 bytes (0x%x)", - "osCreateThread: priority not in range [0-255] (%d)", - "osStartThread: thread has bad state (running/runnable/other)", - "osSetThreadPri: priority not in range [0-255] (%d)", - "osCreateMesgQueue: message count not > 0 (%d)", - "osSendMesg: flag not OS_MESG_NOBLOCK or OS_MESG_BLOCK (%d)", - "osJamMesg: flag not OS_MESG_NOBLOCK or OS_MESG_BLOCK (%d)", - "osRecvMesg: flag not OS_MESG_NOBLOCK or OS_MESG_BLOCK (%d)", - "osSetEventMesg: unknown event type (%d)", - "osMapTLB: index not in range [0-30] (%d)", - "osMapTLB: asid argument not -1 or in range [0-255] (%d)", - "osUnmapTLB: index not in range [0-30] (%d)", - "osSetTLBASID: asid not in range [0-255] (%d)", - "osAiSetFrequency: freq not in range [%d-%d] (%d)", - "osAiSetNextBuffer: address not aligned to 8 bytes (0x%x)", - "osAiSetNextBuffer: size not aligned to 8 bytes (0x%x)", - "osDpSetNextBuffer: address not aligned to 8 bytes (0x%x)", - "osDpSetNextBuffer: size not aligned to 8 bytes (0x%x)", - "osPiRawReadIo: address not aligned to 4 bytes (0x%x)", - "osPiRawWriteIo: address not aligned to 4 bytes (0x%x)", - "osPiRawStartDma: direction not OS_READ or OS_WRITE (%d)", - "osPiRawStartDma: device address not aligned to 2 bytes (0x%x)", - "osPiRawStartDma: DRAM address not aligned to 8 bytes (0x%x)", - "osPiRawStartDma: size not aligned to 2 bytes (%d)", - "osPiRawStartDma: size not in range [0,16777216] (%d)", - "osPiReadIo: address not aligned to 4 bytes (0x%x)", - "osPiWriteIo: address not aligned to 4 bytes (0x%x)", - "osPiStartDma: PI Manager not yet begun by osCreatePiManager", - "osPiStartDma: priority not OS_MESG_PRI_[NORMAL|HIGH] (%d)", - "osPiStartDma: direction not OS_READ or OS_WRITE (%d)", - "osPiStartDma: device address not aligned to 2 bytes (0x%x)", - "osPiStartDma: DRAM address not aligned to 8 bytes (0x%x)", - "osPiStartDma: size not aligned to 2 bytes (%d)", - "osPiStartDma: size not in range [0,16777216] (%d)", - "osCreatePiManager: priority not in range [0-255] (%d)", - "osViGetCurrentMode: VI Manager not yet begun", - "osViGetCurrentFramebuffer: VI Manager not yet begun", - "osViGetNextFramebuffer: VI Manager not yet begun", - "osViSetXScale: value not in range [0.25,1.0] (%f)", - "osViSetXScale: VI Manager not yet begun by osCreateViManager", - "osViSetYScale: value not in range [0.05,1.0] (%f)", - "osViSetYScale: VI Manager not yet begun by osCreateViManager", - "osViSetSpecialFeatures: not a known feature value (%d)", - "osViSetSpecialFeatures: VI Manager not yet begun", - "osViSetMode: VI Manager not yet begun by osCreateViManager", - "osViSetEvent: VI Manager not yet begun by osCreateViManager", - "osViSwapBuffer: frame buffer not aligned to 64 bytes (0x%x)", - "osViSwapBuffer: VI Manager not yet begun", - "osCreateViManager: priority not in range [0-255] (%d)", - "osCreateRegion: not a known alignment (%d)", - "osCreateRegion: length (%d) too small for buffer size (%d)", - "osMalloc: invalid or corrupt region (0x%x)", - "osFree: invalid or corrupt region (0x%x)", - "osFree: invalid address (0x%x) or\n corrupt region (0x%x)", - "osGetRegionBufCount: invalid or corrupt region (0x%x)", - "osGetRegionBufSize: invalid or corrupt region (0x%x)", - "osSpTaskLoad: dram_stack not aligned to 16 bytes (0x%x)", - "osSpTaskLoad: output_buff not aligned to 16 bytes (0x%x)", - "osSpTaskLoad: output_buff_size not aligned to 16 bytes (0x%x)", - "osSpTaskLoad: yield_data_ptr not aligned to 16 bytes (0x%x)", - "osProfileInit: profile counter is running, call osProfileStop before init", - "osProfileInit: profcnt is %d", - "osProfileInit: histo_base pointer must be 32-bit aligned (%x)", - "osProfileInit: text_start (%x) >= text_end (%x)", - "osProfileInit: histo_size is an illegal size (%d)", - "osProfileStart: microseconds is < PROF_MIN_INTERVAL (%d)", - "osProfileStart: profiling has already been started", - "osProfileStop: profiling has already been stopped", - "osProfileStop: no profile timer to stop", - "osReadHost: address not aligned to 8 bytes (0x%x)", - "osReadHost: size either 0 or not aligned to 4 bytes (0x%x)", - "osWriteHost: address not aligned to 8 bytes (0x%x)", - "osWriteHost: size either 0 or not aligned to 4 bytes (0x%x)", - "osGetTime: VI manager not yet begun by osCreateViManager", - "osSetTime: VI manager not yet begun by osCreateViManager", - "osSetTimer: VI manager not yet begun by osCreateViManager", - "osStopTimer: VI manager not yet begun by osCreateViManager", - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - NULSTR, - "_handleMIDIMsg: no sound mapped", - "_handleMIDIMsg: no free voices", - "_handleMIDIMsg: couldn't map voice", - "_handleMIDIMsg: note off - couldn't find voice", - "_handleMIDIMsg: poly pressure - couldn't find voice", - "_handleEvent: no free voices", - "Synthesizer: no free updates", - "alSndPDeallocate: attempt to deallocate a sound which is playing", - "alSndpDelete: attempt to delete player with playing sounds", - "alSndpPlay: attempt to play a sound which is playing", - "alSndpSetSound: sound id (%d) out of range (0 - %d)", - "alSndpSetPriority: sound id (%d) out of range (0 - %d)", - "alSndpSet Parameter: target (%d) out of range (0 - %d)", - "alBnkfNew: bank file out of date", - "alSeqNew: 0x%x is not a midi file", - "alSeqNew: 0x%x is not a type 0 midi file", - "alSeqNew: 0x%x has more than 1 track", - "alSeqNew: SMPTE delta times not supported", - "alSeqNew: Error parsing file 0x%x (no track header)", - "alSeqNextEvent: Unsupported system exclusive", - "alSeqNextEvent: Unsupported midi meta event 0x%x", - "_handleMIDIMsg: Invalid program change to %d, max instruments %d", - "_handleMIDIMsg: Unknown midi message 0x%x", - "_unmapVoice: Couldn't unmap voice 0x%x", - "alEvtqPostEvent: Out of free events", - "alHeapAlloc: Can't allocate %d bytes", - "alHeapCheck: Heap corrupt", - "alHeapCheck: Heap corrupt - first block is bad", - "alCSeqGetTrackEvent: Running status of zero on track %d", - "alCSeqGetTrackEvent: Note on velocity of zero on track %d", - "alCSPVoiceHandler: Stopping sequence but voice not free chan %d, key %d", - "alSeqNextEvent: Read past end of sequence", - "osAiSetNextBuffer: DMA buffer location may cause audio clicks (0x%x)", - "_loadOutputBuffer: Modulated delay greater than total delay by %d samples", - "osViExtendVStart: VI Manager not yet begun by osCreateViManager", - "osViExtendVStart: value not in range [0-48] %d", - NULSTR, -}; - -static void kmcErrorHandler(s16 code, s16 numArgs, ...); -OSErrorHandler __kmcErrorHandler = kmcErrorHandler; - -static void kmcErrorHandler(s16 code, s16 numArgs, ...) { - va_list ap; - const char* fmt; - - fmt = __os_error_message[code]; - va_start(ap, numArgs); - - if (__kmc_pt_mode) { - _Printf(kmc_proutSyncPrintf, NULL, fmt, ap); - } else { - _Printf(proutSyncPrintf, NULL, fmt, ap); - } - - osSyncPrintf("\n"); - - va_end(ap); -} diff --git a/src/gu/mtxcatf.c b/src/gu/mtxcatf.c deleted file mode 100644 index 77acfba..0000000 --- a/src/gu/mtxcatf.c +++ /dev/null @@ -1,60 +0,0 @@ - -/* - * Copyright 1995, Silicon Graphics, Inc. - * ALL RIGHTS RESERVED - * - * UNPUBLISHED -- Rights reserved under the copyright laws of the United - * States. Use of a copyright notice is precautionary only and does not - * imply publication or disclosure. - * - * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND: - * Use, duplication or disclosure by the Government is subject to restrictions - * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights - * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or - * in similar or successor clauses in the FAR, or the DOD or NASA FAR - * Supplement. Contractor/manufacturer is Silicon Graphics, Inc., - * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311. - * - * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY - * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION, - * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY - * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON - * GRAPHICS, INC. - * - */ - -/* - * File: mtxcatf.c - * Creator: hsa@sgi.com - * Create Date: Thu Nov 2 13:03:02 PST 1995 - * - */ - -#include "guint.h" - -void guMtxCatF(float mf[4][4], float nf[4][4], float res[4][4]) { - int i, j, k; - float temp[4][4]; - - for (i = 0; i < 4; i++) { - for (j = 0; j < 4; j++) { - temp[i][j] = 0.0; - for (k = 0; k < 4; k++) { - temp[i][j] += mf[i][k] * nf[k][j]; - } - } - } - - /* make sure we handle case where result is an input */ - for (i = 0; i < 4; i++) { - for (j = 0; j < 4; j++) { - res[i][j] = temp[i][j]; - } - } -} - -void guMtxXFMF(float mf[4][4], float x, float y, float z, float* ox, float* oy, float* oz) { - *ox = mf[0][0] * x + mf[1][0] * y + mf[2][0] * z + mf[3][0]; - *oy = mf[0][1] * x + mf[1][1] * y + mf[2][1] * z + mf[3][1]; - *oz = mf[0][2] * x + mf[1][2] * y + mf[2][2] * z + mf[3][2]; -} diff --git a/src/gu/normalize.c b/src/gu/normalize.c deleted file mode 100644 index 82185a7..0000000 --- a/src/gu/normalize.c +++ /dev/null @@ -1,22 +0,0 @@ -/************************************************************************** - * * - * Copyright (C) 1994, Silicon Graphics, Inc. * - * * - * These coded instructions, statements, and computer programs contain * - * unpublished proprietary information of Silicon Graphics, Inc., and * - * are protected by Federal copyright law. They may not be disclosed * - * to third parties or copied or duplicated in any form, in whole or * - * in part, without the prior written consent of Silicon Graphics, Inc. * - * * - **************************************************************************/ - -#include "guint.h" - -void guNormalize(float* x, float* y, float* z) { - float m; - - m = 1 / sqrtf((*x) * (*x) + (*y) * (*y) + (*z) * (*z)); - *x *= m; - *y *= m; - *z *= m; -} diff --git a/src/gu/scale.c b/src/gu/scale.c deleted file mode 100644 index 594d043..0000000 --- a/src/gu/scale.c +++ /dev/null @@ -1,30 +0,0 @@ -/************************************************************************** - * * - * Copyright (C) 1994, Silicon Graphics, Inc. * - * * - * These coded instructions, statements, and computer programs contain * - * unpublished proprietary information of Silicon Graphics, Inc., and * - * are protected by Federal copyright law. They may not be disclosed * - * to third parties or copied or duplicated in any form, in whole or * - * in part, without the prior written consent of Silicon Graphics, Inc. * - * * - **************************************************************************/ - -#include "guint.h" - -void guScaleF(float mf[4][4], float x, float y, float z) { - guMtxIdentF(mf); - - mf[0][0] = x; - mf[1][1] = y; - mf[2][2] = z; - mf[3][3] = 1; -} - -void guScale(Mtx* m, float x, float y, float z) { - Matrix mf; - - guScaleF(mf, x, y, z); - - guMtxF2L(mf, m); -} diff --git a/src/gu/translate.c b/src/gu/translate.c deleted file mode 100644 index 8ae2538..0000000 --- a/src/gu/translate.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************** - * * - * Copyright (C) 1994, Silicon Graphics, Inc. * - * * - * These coded instructions, statements, and computer programs contain * - * unpublished proprietary information of Silicon Graphics, Inc., and * - * are protected by Federal copyright law. They may not be disclosed * - * to third parties or copied or duplicated in any form, in whole or * - * in part, without the prior written consent of Silicon Graphics, Inc. * - * * - **************************************************************************/ - -#include "guint.h" - -void guTranslateF(float mf[4][4], float x, float y, float z) { - guMtxIdentF(mf); - - mf[3][0] = x; - mf[3][1] = y; - mf[3][2] = z; -} - -void guTranslate(Mtx* m, float x, float y, float z) { - Matrix mf; - - guTranslateF(mf, x, y, z); - - guMtxF2L(mf, m); -} diff --git a/src/os/exceptasm.s b/src/os/exceptasm.s index 919482e..9a7dbea 100644 --- a/src/os/exceptasm.s +++ b/src/os/exceptasm.s @@ -1,6 +1,4 @@ -#ifdef MODERN_CC .set gp=64 -#endif #include "PR/R4300.h" #include "sys/asm.h" diff --git a/src/os/getfpccsr.s b/src/os/getfpccsr.s index f51b1d0..75cf2ef 100644 --- a/src/os/getfpccsr.s +++ b/src/os/getfpccsr.s @@ -6,8 +6,4 @@ LEAF(__osGetFpcCsr) CFC1( v0, fcr31) jr ra -#ifndef MODERN_CC -END(__osGetSR) # @bug: Should be __osGetFpcCsr -#else END(__osGetFpcCsr) -#endif diff --git a/src/os/initialize.c b/src/os/initialize.c index fedb2fd..ef23337 100644 --- a/src/os/initialize.c +++ b/src/os/initialize.c @@ -21,7 +21,6 @@ u32 __OSGlobalIntMask = OS_IM_ALL; #ifdef _FINALROM u32 __osFinalrom; #else -u32 __kmc_pt_mode; void* __printfunc = NULL; #endif diff --git a/src/os/setfpccsr.s b/src/os/setfpccsr.s index f36e5b8..de51d00 100644 --- a/src/os/setfpccsr.s +++ b/src/os/setfpccsr.s @@ -7,8 +7,4 @@ LEAF(__osSetFpcCsr) CFC1( v0, fcr31) CTC1( a0, fcr31) jr ra -#ifndef MODERN_CC -END(__osSetSR) # @bug: Should be __osSetFpcCsr -#else -END(__osSetFpcCsr) -#endif +END(__osSetFpcCsr) \ No newline at end of file diff --git a/src/rmon/rmonbrk.c b/src/rmon/rmonbrk.c deleted file mode 100644 index 59959d6..0000000 --- a/src/rmon/rmonbrk.c +++ /dev/null @@ -1,424 +0,0 @@ -#ifndef _FINALROM - -#include "PR/os_internal.h" -#include "PRinternal/dbgproto.h" -#include "PR/rcp.h" -#include "PR/sptask.h" -#include "PRinternal/rmonint.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -#define TMP_BP 0 -#define NUM_BREAKPOINTS 16 - -typedef struct { - TVushort type; - TVushort response; - TVid threadID; - void* pc; -} TVExceptionReplyMsg; - -typedef struct { - u32* breakAddress; - u32 oldInstruction; -} BREAKINFO; - -/* first breakpoint is reserved for implementing single-stepping */ -static BREAKINFO breakpoints[NUM_BREAKPOINTS] ALIGNED(0x8); -/* breakpoint for alternate branch target */ -static BREAKINFO altBreak; - -static BREAKINFO RCPbreakpoints[NUM_BREAKPOINTS] ALIGNED(0x8); - -u8 __rmonRcpAtBreak; - -static void rmonFindFaultedThreads(void); - -static void SetTempBreakpoint(u32* addr1, u32* addr2) { - STUBBED_PRINTF(("Set temp BP at %08x", addr1)); - if (addr2 != NULL) { - STUBBED_PRINTF((" and %08x", addr2)); - } - STUBBED_PRINTF(("\n")); - - /* Save the word at the target address to be restored later */ - breakpoints[TMP_BP].oldInstruction = *addr1; - /* Install a break instruction at the target address */ - *addr1 = MIPS_BREAK(16); - osWritebackDCache(addr1, sizeof(*addr1)); - osInvalICache(addr1, sizeof(*addr1)); - breakpoints[TMP_BP].breakAddress = addr1; - - /* Also do so for an alt address if required */ - if (addr2 != NULL) { - altBreak.oldInstruction = *addr2; - *addr2 = MIPS_BREAK(16); - osWritebackDCache(addr2, sizeof(*addr2)); - osInvalICache(addr2, sizeof(*addr2)); - altBreak.breakAddress = addr2; - } -} - -static void ClearTempBreakpoint(void) { - u32 inst; - - if (breakpoints[TMP_BP].breakAddress != NULL) { - inst = *breakpoints[TMP_BP].breakAddress; - - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - STUBBED_PRINTF(("ClearTempBreak @ %08x\n", breakpoints[TMP_BP].breakAddress)); - - /* After confirming that there is a break instruction with code at the target - address, restore the original contents of the word at the target address */ - *breakpoints[TMP_BP].breakAddress = breakpoints[TMP_BP].oldInstruction; - osWritebackDCache(breakpoints[TMP_BP].breakAddress, sizeof(*breakpoints[TMP_BP].breakAddress)); - osInvalICache(breakpoints[TMP_BP].breakAddress, sizeof(*breakpoints[TMP_BP].breakAddress)); - } - breakpoints[TMP_BP].breakAddress = NULL; - } - - /* Same as above for the alt breakpoint */ - if (altBreak.breakAddress != NULL) { - inst = *altBreak.breakAddress; - - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - STUBBED_PRINTF(("ClearTempBreak @ %08x\n", altBreak.breakAddress)); - - *altBreak.breakAddress = altBreak.oldInstruction; - osWritebackDCache(altBreak.breakAddress, sizeof(*altBreak.breakAddress)); - osInvalICache(altBreak.breakAddress, sizeof(*altBreak.breakAddress)); - } - altBreak.breakAddress = NULL; - } -} - -int __rmonSetBreak(KKHeader* req) { - register KKSetBkptRequest* request = (KKSetBkptRequest*)req; - register BREAKINFO* breakBase; - register BREAKINFO* whichBreak; - register BREAKINFO* lastBreak; - KKBkptEvent reply; - - STUBBED_PRINTF(("SetBreak at %08x, method %d\n", request->addr, req->method)); - - /* Select breakpoint list */ - if (req->method == RMON_RSP) { - breakBase = RCPbreakpoints; - whichBreak = &RCPbreakpoints[1]; - lastBreak = &RCPbreakpoints[NUM_BREAKPOINTS]; - } else { - breakBase = breakpoints; - whichBreak = &breakpoints[1]; - lastBreak = &breakpoints[NUM_BREAKPOINTS]; - } - - /* Find breakpoint slot */ - for (; whichBreak < lastBreak; whichBreak++) { - if (whichBreak->breakAddress != NULL) { - if (whichBreak->breakAddress == (u32*)request->addr) { - /* Breakpoint already set here */ - break; - } - continue; - } else { - /* Empty slot */ - break; - } - } - - /* No breakpoints available */ - if (whichBreak == lastBreak) { - return TV_ERROR_NO_MORE_IDS; - } - - /* Set breakpoint if not already set */ - if (whichBreak->breakAddress == NULL) { - if (req->method == RMON_RSP) { - whichBreak->oldInstruction = __rmonReadWordAt((u32*)request->addr); - __rmonWriteWordTo((u32*)request->addr, MIPS_BREAK((whichBreak - breakBase) + NUM_BREAKPOINTS)); - } else { - whichBreak->oldInstruction = *(u32*)request->addr; - *(u32*)request->addr = MIPS_BREAK((whichBreak - breakBase) + NUM_BREAKPOINTS); - osWritebackDCache((void*)request->addr, sizeof(whichBreak->oldInstruction)); - osInvalICache((void*)request->addr, sizeof(whichBreak->oldInstruction)); - } - whichBreak->breakAddress = (u32*)request->addr; - STUBBED_PRINTF(("* (%08x) = %08x (was %08x)\n", whichBreak->breakAddress, *whichBreak->breakAddress, - whichBreak->oldInstruction)); - } - - /* Send reply */ - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->object; - reply.bp = whichBreak - breakBase; - reply.instruction = whichBreak->oldInstruction; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonListBreak(KKHeader* request UNUSED) { - STUBBED_PRINTF(("ListBreak\n")); - - return TV_ERROR_ILLEGAL_CALL; -} - -int __rmonClearBreak(KKHeader* req) { - register KKClrBkptRequest* request = (KKClrBkptRequest*)req; - register BREAKINFO* whichBreak; - KKBkptEvent reply; - u32 inst; - - STUBBED_PRINTF(("ClearBreak\n")); - - /* Check valid breakpoint index */ - if (request->bp >= NUM_BREAKPOINTS) { - return TV_ERROR_INVALID_ID; - } - - /* Clear the breakpoint, restore whatever was there before */ - if (req->method == RMON_RSP) { - whichBreak = &RCPbreakpoints[request->bp]; - - if (whichBreak->breakAddress == NULL) { - return TV_ERROR_INVALID_ID; - } - - inst = __rmonReadWordAt(whichBreak->breakAddress); - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - __rmonWriteWordTo(whichBreak->breakAddress, whichBreak->oldInstruction); - } - } else { - whichBreak = &breakpoints[request->bp]; - - if (whichBreak->breakAddress == NULL) { - return TV_ERROR_INVALID_ID; - } - - inst = *whichBreak->breakAddress; - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - *whichBreak->breakAddress = whichBreak->oldInstruction; - osWritebackDCache(whichBreak->breakAddress, sizeof(*whichBreak->breakAddress)); - osInvalICache(whichBreak->breakAddress, sizeof(*whichBreak->breakAddress)); - } - } - whichBreak->breakAddress = NULL; - - /* Send reply */ - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->object; - reply.bp = request->bp; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -u32 __rmonGetBranchTarget(int method, int thread, char* addr) { - int inst; - - if (method == RMON_RSP) { - inst = __rmonReadWordAt((u32*)addr); - } else { - inst = *(u32*)addr; - } - - switch ((inst >> 26) & 0x3F) { - case 0: /* SPECIAL */ - if (((inst >> 5) & 0x7FFF) == 0 && (inst & 0x3F) == 8) { - /* JR */ - return __rmonGetRegisterContents(method, thread, (inst >> 21) & 0x1F); - } - if (((inst >> 16) & 0x1F) == 0 && (inst & 0x7FF) == 9) { - /* JALR */ - return __rmonGetRegisterContents(method, thread, (inst >> 21) & 0x1F); - } - break; - case 1: /* REGIMM */ - switch ((inst >> 16) & 0x1F) { - case 0: /* BLTZ */ - case 1: /* BGEZ */ - case 2: /* BLTZL */ - case 3: /* BGEZL */ - case 16: /* BLTZAL */ - case 17: /* BGEZAL */ - case 18: /* BLTZALL */ - case 19: /* BGEZALL */ - return (((inst << 0x10) >> 0xE) + addr + 4); - } - break; - case 2: /* J */ - case 3: /* JAL */ - return (((u32)inst << 6) >> 4) + (((s32)((u32)addr + 4) >> 0x1C) << 0x1C); - case 4: /* BEQ */ - case 5: /* BNE */ - case 20: /* BEQL */ - case 21: /* BNEL */ - return (((inst << 0x10) >> 0xE) + addr + 4); - case 6: /* BLEZ */ - case 7: /* BGTZ */ - case 22: /* BLEZL */ - case 23: /* BGTZL */ - if (((inst >> 16) & 0x1F) == 0) { - return (((inst << 0x10) >> 0xE) + addr + 4); - } - break; - case 16: /* COP0 */ - case 17: /* COP1 */ - case 18: /* COP2 */ - case 19: /* COP3 */ - if (((inst >> 21) & 0x1F) == 8) { - switch ((inst >> 16) & 0x1F) { - case 0: /* BCzF */ - case 1: /* BCzT */ - case 2: /* BCzFL */ - case 3: /* BCzTL */ - return (((inst << 0x10) >> 0xE) + addr + 4); - } - } - break; - } - return -1; -} - -static int IsJump(u32 inst) { - switch ((inst >> 26) & 0x3F) { - case 0: /* SPECIAL */ - if (((inst >> 5) & 0x7FFF) == 0 && (inst & 0x3F) == 8) { - /* JR */ - return TRUE; - } - if (((inst >> 16) & 0x1F) == 0 && (inst & 0x7FF) == 9) { - /* JALR */ - return TRUE; - } - break; - case 2: /* J */ - case 3: /* JAL */ - return TRUE; - } - return FALSE; -} - -int __rmonSetSingleStep(int thread, u32* instptr) { - u32 branchTarget = __rmonGetBranchTarget(RMON_CPU, thread, (void*)instptr); - - STUBBED_PRINTF(("SingleStep\n")); - - if ((branchTarget & 3) != 0) { - /* no branch target, set breakpoint at next pc */ - SetTempBreakpoint(instptr + 1, NULL); - } else if (branchTarget == (u32)instptr) { - /* branch target is this instruction, can't single step here */ - return FALSE; - } else if (IsJump(*instptr) || branchTarget == (u32)(instptr + 2)) { - /* unconditional branch, set at branch target */ - SetTempBreakpoint((u32*)branchTarget, NULL); - } else { - /* set two breakpoints for handling conditional branches */ - SetTempBreakpoint((u32*)branchTarget, instptr + 2); - } - return TRUE; -} - -void __rmonGetExceptionStatus(KKStatusEvent* reply) { - reply->status.flags = OS_STATE_STOPPED; - reply->status.why = 2; - reply->status.what = 0; - reply->status.rv = 0; - reply->status.info.major = 2; - reply->status.info.minor = 4; - reply->header.code = KK_CODE_THREAD_STATUS; - reply->header.error = TV_ERROR_NO_ERROR; - reply->header.length = sizeof(*reply); -} - -#define FAULT_BREAKNUM (NUM_BREAKPOINTS - 1) - -static void rmonSendBreakMessage(s32 whichThread, int breakNumber) { - KKStatusEvent reply; - - STUBBED_PRINTF(("Break %d in thread %d\n", breakNumber, whichThread)); - - /* Build thread exception status */ - __rmonGetThreadStatus(RMON_CPU, (whichThread != 0) ? whichThread : RMON_TID_NOTHREAD, &reply); - __rmonGetExceptionStatus(&reply); - - if (breakNumber == FAULT_BREAKNUM) { - /* Hit fault */ - reply.status.info.major = 1; - reply.status.info.minor = 2; - } - if (breakNumber < NUM_BREAKPOINTS) { - breakNumber = 0; - } else { - breakNumber -= NUM_BREAKPOINTS; - } - if (breakNumber != 0) { - /* Break not set by debugger, or set during single-step */ - reply.status.instr = MIPS_BREAK_OPCODE; - } - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_EXCEPTION); -} - -void __rmonHitBreak(void) { - STUBBED_PRINTF(("HitBreak\n")); - - /* Stop all user threads and report faulted threads */ - ClearTempBreakpoint(); - __rmonStopUserThreads(0); - rmonFindFaultedThreads(); -} - -void __rmonHitSpBreak(void) { - KKStatusEvent exceptionReply; - - STUBBED_PRINTF(("Hit SP Break\n")); - - /* Rewind RSP PC by one instruction to return to the location of the break instruction */ - __rmonWriteWordTo((u32*)SP_PC_REG, __rmonReadWordAt((u32*)SP_PC_REG) - 4); - - /* Report RSP break event */ - __rmonGetThreadStatus(RMON_RSP, RMON_TID_RSP, &exceptionReply); - __rmonGetExceptionStatus(&exceptionReply); - __rmonSendReply(&exceptionReply.header, sizeof(exceptionReply), KK_TYPE_EXCEPTION); - __rmonRcpAtBreak = TRUE; -} - -void __rmonHitCpuFault(void) { - STUBBED_PRINTF(("HitCpuFault\n")); - - /* Stop all user threads and report faulted threads */ - __rmonMaskIdleThreadInts(); - __rmonStopUserThreads(0); - rmonFindFaultedThreads(); -} - -static void rmonFindFaultedThreads(void) { - register OSThread* tptr = __osGetActiveQueue(); - - while (tptr->priority != -1) { - if (tptr->priority > OS_PRIORITY_IDLE && tptr->priority <= OS_PRIORITY_APPMAX) { - if (tptr->flags & OS_FLAG_CPU_BREAK) { - int inst = *(u32*)tptr->context.pc; - - STUBBED_PRINTF(("Brk in thread %d @ %08x, inst %08x\r\n", tptr->id, tptr->context.pc, inst)); - - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - rmonSendBreakMessage(tptr->id, inst >> 6); - } else { - rmonSendBreakMessage(tptr->id, 0); - } - } - if (tptr->flags & OS_FLAG_FAULT) { - __rmonSendFault(tptr); - rmonSendBreakMessage(tptr->id, FAULT_BREAKNUM); - } - } - tptr = tptr->tlnext; - } -} - -#endif diff --git a/src/rmon/rmoncmds.c b/src/rmon/rmoncmds.c deleted file mode 100644 index 3992801..0000000 --- a/src/rmon/rmoncmds.c +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef _FINALROM - -#include "PRinternal/dbgproto.h" -#include "PRinternal/rmonint.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -static int NotImplemented(KKHeader* dummy UNUSED) { - return TV_ERROR_ILLEGAL_CALL; -} - -static FUNPTR dispatchTable[] = { - __rmonLoadProgram, __rmonListProcesses, __rmonGetExeName, __rmonListThreads, __rmonThreadStatus, - NotImplemented, __rmonStopThread, __rmonRunThread, NotImplemented, NotImplemented, - __rmonSetFault, NotImplemented, __rmonGetRegionCount, __rmonGetRegions, __rmonGetGRegisters, - __rmonSetGRegisters, __rmonGetFRegisters, __rmonSetFRegisters, __rmonReadMem, __rmonWriteMem, - __rmonSetBreak, __rmonClearBreak, __rmonListBreak, NotImplemented, NotImplemented, - NotImplemented, NotImplemented, NotImplemented, NotImplemented, NotImplemented, - __rmonSetComm, NotImplemented, NotImplemented, NotImplemented, NotImplemented, - NotImplemented, NotImplemented, NotImplemented, NotImplemented, NotImplemented, - NotImplemented, NotImplemented, NotImplemented, NotImplemented, NotImplemented, - NotImplemented, NotImplemented, NotImplemented, NotImplemented, __rmonGetSRegs, - __rmonSetSRegs, __rmonGetVRegs, __rmonSetVRegs, NotImplemented, -}; - -int __rmonExecute(KKHeader* request) { - int retval; - KKHeader reply; - - if (request->code >= ARRLEN(dispatchTable) - 1) { - return TV_ERROR_ILLEGAL_CALL; - } - - retval = dispatchTable[(int)request->code](request); - if (retval < TV_ERROR_NO_ERROR) { - reply.code = request->code; - reply.error = retval; - __rmonSendReply(&reply, sizeof(reply), KK_TYPE_REPLY); - } - return retval; -} - -#endif diff --git a/src/rmon/rmonmain.c b/src/rmon/rmonmain.c deleted file mode 100644 index b286260..0000000 --- a/src/rmon/rmonmain.c +++ /dev/null @@ -1,127 +0,0 @@ -#include "PR/os_version.h" - -#ifndef _FINALROM - -#include "PRinternal/dbgproto.h" -#include "PR/os_internal.h" -#include "PRinternal/rmonint.h" -#include "PR/rcp.h" -#include "PR/sptask.h" -#include "PR/rdb.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -int __rmonActive = FALSE; - -static vu32 somethingToDo; -static u32 inbuffer[280] ALIGNED(0x10); -static u8 cmdinptr; -static u8 cmdoutptr; -static int state; -static char* inPointer; - -void __rmonSendHeader(KKHeader* const block, u32 blockSize, u32 type) { - int sent; - char* cPtr = (char*)block; - - block->rev = KK_REV; - block->type = type; - - sent = 0; - while (sent < blockSize) { - sent += __osRdbSend(cPtr + sent, blockSize - sent, RDB_TYPE_GtoH_DEBUG); - } -} - -void __rmonSendReply(KKHeader* const block, u32 blockSize, u32 replyType) { - char* cPtr; - int sent = 0; - - block->length = blockSize; - cPtr = (char*)&blockSize; - - /* send size */ - while (sent < (signed)sizeof(blockSize)) { - sent += __osRdbSend(cPtr + sent, sizeof(blockSize) - sent, RDB_TYPE_GtoH_DEBUG); - } - - /* send data */ - __rmonSendHeader(block, blockSize, replyType); - __rmonIOflush(); -} - -void __rmonSendData(char* const block, unsigned int blockSize) { - int* blockPointer = (int*)block; - unsigned int wordCount = (u32)(blockSize + 3) / 4; - u32 data; - union { - char bufBytes[4]; - u32 bufWord; - } buffer; - - if (((u32)block & 3) == 0) { - while (wordCount--) { - if ((u32)blockPointer >= SP_DMEM_START && (u32)blockPointer < 0x05000000) { - __osSpRawReadIo((u32)blockPointer++, &data); - __rmonIOputw(data); - } else { - __rmonIOputw(*(blockPointer++)); - } - } - } else - while (wordCount--) { - __rmonMemcpy((u8*)buffer.bufBytes, (u8*)blockPointer, sizeof(buffer)); - __rmonIOputw(buffer.bufWord); - blockPointer++; - } - __rmonIOflush(); -} - -void rmonMain(void) { - register int newChars UNUSED; - - STUBBED_PRINTF(("rmon: Thread %d created\n")); - STUBBED_PRINTF(("rmon: Thread %d destroyed\n")); - - somethingToDo = 0; - cmdoutptr = 0; - cmdinptr = 0; - - __rmonInit(); - __rmonActive = TRUE; - - state = 0, newChars = 0, inPointer = (void*)&inbuffer; - for (;;) { - OSMesg work; - - osRecvMesg(&__rmonMQ, &work, OS_MESG_BLOCK); - - somethingToDo |= (u32)work; - - if (somethingToDo & RMON_MESG_CPU_BREAK) { - somethingToDo &= ~RMON_MESG_CPU_BREAK; - __rmonHitBreak(); - } - if (somethingToDo & RMON_MESG_SP_BREAK) { - somethingToDo &= ~RMON_MESG_SP_BREAK; - __rmonHitSpBreak(); - } - if (somethingToDo & RMON_MESG_FAULT) { - somethingToDo &= ~RMON_MESG_FAULT; - __rmonHitCpuFault(); - } - if (somethingToDo & 0x10) { - somethingToDo; - somethingToDo &= (u8)~0x10; - } - if (somethingToDo & 0x20) { - somethingToDo; - somethingToDo &= (u8)~0x20; - } - } -} - -#endif diff --git a/src/rmon/rmonmem.c b/src/rmon/rmonmem.c deleted file mode 100644 index 6992a0c..0000000 --- a/src/rmon/rmonmem.c +++ /dev/null @@ -1,275 +0,0 @@ -#include "PR/os_version.h" - -#ifndef _FINALROM - -#include "PRinternal/dbgproto.h" -#include "PR/os_internal.h" -#include "PR/rcp.h" -#include "PR/sptask.h" -#include "PRinternal/rmonint.h" -#include "PR/rdb.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -u8 __rmonUtilityBuffer[256] ALIGNED(0x8); - -void __rmonWriteWordTo(u32* addr, u32 val) { - while (__osSpRawWriteIo((u32)addr, val) != 0) { - ; - } -} - -u32 __rmonReadWordAt(u32* addr) { - u32 data; - - if ((u32)addr >= SP_DMEM_START && (u32)addr < 0x05000000) { - __osSpRawReadIo((u32)addr, &data); - return data; - } - return 0; -} - -void __rmonMemcpy(u8* dest, u8* srce, u32 count) { - while (count--) { - *dest++ = *srce++; - } -} - -void __rmonCopyWords(u32* dest, u32* srce, u32 count) { - while (count--) { - *dest++ = *srce++; - } -} - -static void strcpy(char* dest, char* srce) { - while ((*dest++ = *srce++)) { - ; - } -} - -int __rmonReadMem(KKHeader* req) { - char* cPtr; - int sent; - int dataSize; - KKReadRequest* request = (KKReadRequest*)req; - KKBufferEvent* reply = (KKBufferEvent*)__rmonUtilityBuffer; - u8* blockStart; - - STUBBED_PRINTF(("ReadMem @ %08x for %d\n", request->addr, request->nbytes)); - - reply->header.code = request->header.code; - reply->object = request->object; - reply->header.error = TV_ERROR_NO_ERROR; - - if (request->addr == (u32)-1) { - return TV_ERROR_INVALID_ADDRESS; - } - if (request->nbytes > RMON_MAX_XFER_SIZE) { - return TV_ERROR_INVALID_CAPABILITY; - } - - if (req->method == RMON_RSP) { - if (!((request->addr < SP_IMEM_START || (request->addr + request->nbytes) > SP_IMEM_END) ? FALSE : TRUE) - && !((request->addr < SP_DMEM_START || (request->addr + request->nbytes) > SP_DMEM_END) ? FALSE : TRUE)) { - return TV_ERROR_INVALID_ADDRESS; - } - } else if (osVirtualToPhysical((void*)request->addr) == (u32)-1) { - return TV_ERROR_INVALID_ADDRESS; - } - - blockStart = (u8*)request->addr; - reply->header.length = request->nbytes + sizeof(reply->header) + sizeof(reply->object); - dataSize = request->nbytes + sizeof(reply->header) + sizeof(reply->object); - - cPtr = (char*)&dataSize; - sent = 0; - while (sent < (signed)sizeof(dataSize)) { - sent += __osRdbSend(cPtr + sent, sizeof(dataSize) - sent, RDB_TYPE_GtoH_DEBUG); - } - - __rmonSendHeader(&reply->header, sizeof(reply->header) + sizeof(reply->object), KK_TYPE_REPLY); - __rmonSendData(blockStart, request->nbytes); - return TV_ERROR_NO_ERROR; -} - -int __rmonWriteMem(KKHeader* req) { - register KKWriteRequest* request = (KKWriteRequest*)req; - KKObjectEvent reply; - - STUBBED_PRINTF(("WriteMem\n")); - - /* Bad virtual address, abort */ - if (req->method == RMON_CPU && osVirtualToPhysical((u32*)request->writeHeader.addr) == (u32)-1) { - return TV_ERROR_INVALID_ADDRESS; - } - - /* Transfer size too large, abort */ - if (request->writeHeader.nbytes > RMON_MAX_XFER_SIZE) { - return TV_ERROR_INVALID_CAPABILITY; - } - - if (((request->writeHeader.addr < SP_DMEM_START - || (request->writeHeader.addr + request->writeHeader.nbytes) > 0x04FFFFFF) - ? FALSE - : TRUE)) { - int align; - u32 word; - - if ((align = request->writeHeader.addr & 3) != 0) { - STUBBED_PRINTF(("Long unaligned write...\n")); - - if (request->writeHeader.nbytes != 1) { - return TV_ERROR_INVALID_ADDRESS; - } - - /* Unaligned write; read the word, substitute in the written byte, write it back */ - word = __rmonReadWordAt((u32*)(request->writeHeader.addr & ~3)); - if (align == 1) { - word = (word & ~0xFF0000) | (request->buffer[0] << 0x10); - } else if (align == 2) { - word = (word & ~0xFF00) | (request->buffer[0] << 8); - } else { - word = (word & ~0xFF) | (request->buffer[0] << 0); - } - __rmonWriteWordTo((u32*)(request->writeHeader.addr & ~3), word); - } else { - int wordCount = request->writeHeader.nbytes / sizeof(u32); - u32* wordPointer = (u32*)request->buffer; - - if (request->writeHeader.nbytes % sizeof(u32) != 0) { - STUBBED_PRINTF(("RCP write not an integral number of words\n")); - return TV_ERROR_INVALID_ADDRESS; - } - - while (wordCount--) { - __rmonWriteWordTo((u32*)request->writeHeader.addr, *(wordPointer++)); - request->writeHeader.addr += sizeof(*wordPointer); - } - } - } else { - __rmonMemcpy((u8*)request->writeHeader.addr, (u8*)request->buffer, request->writeHeader.nbytes); - } - - reply.header.code = request->writeHeader.header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->writeHeader.object; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - - return TV_ERROR_NO_ERROR; -} - -int __rmonListProcesses(KKHeader* req) { - KKObjectRequest* request = (KKObjectRequest*)req; - KKObjsEvent reply; - - STUBBED_PRINTF(("ListProcesses\n")); - - reply.object = 0; - reply.objs.number = 1; - reply.objs.objects[0] = (req->method == RMON_RSP) ? RMON_PID_RSP : RMON_PID_CPU; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonLoadProgram(KKHeader* request UNUSED) { - STUBBED_PRINTF(("LoadProgram\n")); - - return TV_ERROR_ILLEGAL_CALL; -} - -int __rmonGetExeName(KKHeader* req) { - KKObjectRequest* request = (KKObjectRequest*)req; - KKBufferEvent* reply = (KKBufferEvent*)__rmonUtilityBuffer; - - STUBBED_PRINTF(("GetExeName\n")); - - reply->header.code = request->header.code; - reply->header.error = TV_ERROR_NO_ERROR; - reply->object = request->object; - - if (req->method == RMON_RSP) { - strcpy(reply->buffer, "imem"); - } else { - strcpy(reply->buffer, "rmon"); - } - __rmonSendReply(&reply->header, sizeof(reply->header) + sizeof(reply->object) + 8, KK_TYPE_REPLY); - - return TV_ERROR_NO_ERROR; -} - -int __rmonGetRegionCount(KKHeader* req) { - KKObjectRequest* request = (KKObjectRequest*)req; - KKNumberEvent reply; - - STUBBED_PRINTF(("GetRegionCount\n")); - - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->object; - - reply.number = (req->method == RMON_RSP) ? 2 : 5; - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - - return TV_ERROR_NO_ERROR; -} - -int __rmonGetRegions(KKHeader* req) { - KKObjectRequest* request = (KKObjectRequest*)req; - KKRegionEvent* reply = (KKRegionEvent*)__rmonUtilityBuffer; - int numRegions; - - STUBBED_PRINTF(("GetRegions\n")); - - numRegions = (req->method == RMON_RSP) ? 2 : 6; - - reply->header.length = numRegions * sizeof(reply->regions[0]) + sizeof(*reply); - reply->header.code = request->header.code; - reply->header.error = TV_ERROR_NO_ERROR; - reply->object = request->object; - reply->number = numRegions; - - reply->regions[1].vaddr = SP_IMEM_START; - reply->regions[1].size = SP_IMEM_END + 1 - SP_IMEM_START; - reply->regions[1].flags = 1 | 2 | 4; - reply->regions[1].paddr = SP_IMEM_START; - - reply->regions[0].vaddr = SP_DMEM_START; - reply->regions[0].size = SP_DMEM_END + 1 - SP_DMEM_START; - reply->regions[0].flags = 1 | 2; - reply->regions[0].paddr = SP_DMEM_START; - - if (numRegions > 2) { - reply->regions[2].vaddr = 0x88200000; - reply->regions[2].size = 0x6130; - reply->regions[2].flags = 1 | 4; - reply->regions[2].paddr = 0; - - reply->regions[3].vaddr = 4; - reply->regions[3].size = 0x200000; - reply->regions[3].flags = 1 | 2; - reply->regions[3].paddr = 0; - - reply->regions[4].vaddr = 0x4002000; - reply->regions[4].size = 0x800000; - reply->regions[4].flags = 1 | 2; - reply->regions[4].paddr = 0; - - reply->regions[5].vaddr = 0x88206130; - reply->regions[5].size = 0x9000; - reply->regions[5].flags = 1 | 2; - reply->regions[5].paddr = 0; - } - - __rmonSendReply(&reply->header, reply->header.length, KK_TYPE_REPLY); - - return TV_ERROR_NO_ERROR; -} - -#endif diff --git a/src/rmon/rmonmisc.c b/src/rmon/rmonmisc.c deleted file mode 100644 index 17c9a03..0000000 --- a/src/rmon/rmonmisc.c +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef _FINALROM - -#include "PRinternal/dbgproto.h" -#include "PR/os_internal.h" -#include "PR/sptask.h" -#include "PRinternal/rmonint.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -int __rmonSetFault(KKHeader* req) { - KKFaultRequest* request = (KKFaultRequest*)req; - KKObjectEvent reply; - - STUBBED_PRINTF(("SetFault\n")); - - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->tid; - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -OSMesgQueue __rmonMQ ALIGNED(0x8); -static OSThread rmonIOThread ALIGNED(0x8); -static OSMesg rmonMsgs[8] ALIGNED(0x8); -static STACK(rmonIOStack, 0x4000) ALIGNED(0x10); -static OSMesg rmonPiMsgs[8] ALIGNED(0x8); -static OSMesgQueue rmonPiMQ ALIGNED(0x8); - -void __rmonInit(void) { - osCreateMesgQueue(&__rmonMQ, rmonMsgs, ARRLEN(rmonMsgs)); - osSetEventMesg(OS_EVENT_CPU_BREAK, &__rmonMQ, (OSMesg)RMON_MESG_CPU_BREAK); - osSetEventMesg(OS_EVENT_SP_BREAK, &__rmonMQ, (OSMesg)RMON_MESG_SP_BREAK); - osSetEventMesg(OS_EVENT_FAULT, &__rmonMQ, (OSMesg)RMON_MESG_FAULT); - osSetEventMesg(OS_EVENT_THREADSTATUS, &__rmonMQ, NULL); - osCreateThread(&rmonIOThread, 0, (void (*)(void*))__rmonIOhandler, NULL, STACK_START(rmonIOStack), OS_PRIORITY_MAX); - osCreatePiManager(OS_PRIORITY_PIMGR, &rmonPiMQ, rmonPiMsgs, ARRLEN(rmonPiMsgs)); - osStartThread(&rmonIOThread); -} - -void __rmonPanic(void) { - STUBBED_PRINTF(("PANIC!!\n")); - - for (;;) { - ; - } -} - -int __rmonSetComm(KKHeader* req) { - KKObjectEvent reply; - - STUBBED_PRINTF(("SetComm\n")); - - reply.header.code = req->code; - reply.object = 0; - reply.header.error = TV_ERROR_NO_ERROR; - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - - return TV_ERROR_NO_ERROR; -} - -#endif diff --git a/src/rmon/rmonprint.c b/src/rmon/rmonprint.c deleted file mode 100644 index 8ac91a5..0000000 --- a/src/rmon/rmonprint.c +++ /dev/null @@ -1 +0,0 @@ -/* Empty file */ diff --git a/src/rmon/rmonrcp.s b/src/rmon/rmonrcp.s deleted file mode 100644 index bddc172..0000000 --- a/src/rmon/rmonrcp.s +++ /dev/null @@ -1,60 +0,0 @@ -#include "PR/os_version.h" -#if !defined(_FINALROM) || BUILD_VERSION < VERSION_J - -#include "sys/asm.h" -#include "sys/regdef.h" -#include "PR/rcp.h" -#include "PR/R4300.h" - -.text - -/* check if the rsp is currently running by polling HALT or BROKE bits in SP_STATUS */ -LEAF(__rmonRCPrunning) - move v0, zero - lw t0, PHYS_TO_K1(SP_STATUS_REG) - and t0, (SP_STATUS_HALT | SP_STATUS_BROKE) - bnez t0, isHalted - ori v0, 1 -isHalted: - jr ra -END(__rmonRCPrunning) - -/* stop the rsp, first wait for any ongoing dma to complete before setting HALT in SP_STATUS */ -LEAF(__rmonIdleRCP) - li a0, PHYS_TO_K1(SP_DMA_BUSY_REG) -wait4dma: - lw v0, (a0) - bnez v0, wait4dma - li a1, SP_CLR_INTR_BREAK | SP_SET_HALT - li a0, PHYS_TO_K1(SP_STATUS_REG) - sw a1, (a0) - -/* wait for the rsp to stop */ -awaitIdle: - li a0, PHYS_TO_K1(SP_STATUS_REG) - lw v0, (a0) - and v0, (SP_STATUS_HALT | SP_STATUS_BROKE) - beqz v0, awaitIdle - jr ra -END(__rmonIdleRCP) - -/* run the rsp in single-step mode to step one instruction */ -LEAF(__rmonStepRCP) - li a0, PHYS_TO_K1(SP_STATUS_REG) - li a1, (SP_CLR_INTR_BREAK | SP_SET_SSTEP | SP_CLR_BROKE | SP_CLR_HALT) - sw a1, (a0) - b awaitIdle -END(__rmonStepRCP) - -/* run the rsp normally */ -LEAF(__rmonRunRCP) - li a0, PHYS_TO_K1(MI_INTR_MASK_REG) - li a1, MI_INTR_MASK_SET_SP - sw a1, (a0) - li a0, PHYS_TO_K1(SP_STATUS_REG) - li a1, (SP_SET_INTR_BREAK | SP_CLR_SSTEP | SP_CLR_BROKE | SP_CLR_HALT) - sw a1, (a0) - jr ra -END(__rmonRunRCP) - -#endif diff --git a/src/rmon/rmonregs.c b/src/rmon/rmonregs.c deleted file mode 100644 index fcb39b3..0000000 --- a/src/rmon/rmonregs.c +++ /dev/null @@ -1,403 +0,0 @@ -#ifndef _FINALROM - -#include "PRinternal/dbgproto.h" -#include "PR/os_internal.h" -#include "PR/rcp.h" -#include "PR/sptask.h" -#include "PRinternal/rmonint.h" -#include "PR/rdb.h" -#include "PR/os_version.h" - -#include "PRinternal/macros.h" - -// TODO: these come from headers -#ident "$Revision: 1.4 $" -// This revision was bumped down at K for some reason -#ident "$Revision: 3.70 $" -#ident "$Revision: 1.5 $" -#ident "$Revision: 1.2 $" -#ident "$Revision: 1.4 $" - -static u32 RCPpc; -static u32 oldIMEMvalue; -static u32 DMEMbuffer[4] ALIGNED(0x8); - -typedef union { - u32 everything; - struct { - int opcode : 6; - int base : 5; - int rt : 5; - int offset : 16; - } scalarop; - struct { - int opcode : 6; - int base : 5; - int rt : 5; - int size : 5; - int element : 4; - int offset : 7; - } vectorop; -} INSTRUCTION; - -static void LoadStoreSU(int opcode, int regno) { - INSTRUCTION inst; - - /* Prepare a scalar load or store instruction at DMEM address 0 */ - inst.everything = 0; - inst.scalarop.opcode = opcode; - inst.scalarop.rt = regno; - __rmonWriteWordTo((u32*)SP_IMEM_START, inst.everything); - __rmonWriteWordTo((u32*)SP_PC_REG, 0); -} - -static void LoadStoreVU(int opcode, int regno) { - INSTRUCTION inst; - - /* Prepare a vector 128-bit load or store instruction at DMEM address 0 */ - inst.everything = 0; - inst.vectorop.opcode = opcode; - inst.vectorop.rt = regno; - inst.vectorop.size = 4; /* LQV / SQV */ - __rmonWriteWordTo((u32*)SP_IMEM_START, inst.everything); - __rmonWriteWordTo((u32*)SP_PC_REG, 0); -} - -static void SetUpForRCPop(int isVector) { - /* Save RSP data that would be overwritten when reading or writing registers */ - RCPpc = __rmonReadWordAt((u32*)SP_PC_REG); - oldIMEMvalue = __rmonReadWordAt((u32*)SP_IMEM_START); - DMEMbuffer[0] = __rmonReadWordAt((u32*)SP_DMEM_START); - if (isVector) { - DMEMbuffer[1] = __rmonReadWordAt((u32*)(SP_DMEM_START + 0x4)); - DMEMbuffer[2] = __rmonReadWordAt((u32*)(SP_DMEM_START + 0x8)); - DMEMbuffer[3] = __rmonReadWordAt((u32*)(SP_DMEM_START + 0xC)); - } -} - -static void CleanupFromRCPop(int isVector) { - /* Restore RSP data that was saved to read or write registers */ - __rmonWriteWordTo((u32*)SP_DMEM_START, DMEMbuffer[0]); - if (isVector) { - __rmonWriteWordTo((u32*)(SP_DMEM_START + 0x4), DMEMbuffer[1]); - __rmonWriteWordTo((u32*)(SP_DMEM_START + 0x8), DMEMbuffer[2]); - /* BUG: the last word is not restored properly */ - __rmonWriteWordTo((u32*)(SP_DMEM_START + 0xC), DMEMbuffer[2]); - } - __rmonWriteWordTo((u32*)SP_IMEM_START, oldIMEMvalue); - __rmonWriteWordTo((u32*)SP_PC_REG, RCPpc); -} - -int __rmonGetGRegisters(KKHeader* req) { - register KKObjectRequest* request = (KKObjectRequest*)req; - KKGregEvent reply; - - STUBBED_PRINTF(("GetGRegisters\n")); - - reply.tid = request->object; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - - if (request->header.method == RMON_CPU) { - OSThread* tptr = __rmonGetTCB(request->object); - u64* tcbregptr; - register s32 i; - - if (tptr == NULL) { - return TV_ERROR_INVALID_ID; - } - - for (i = GREG_IDX_AT, tcbregptr = &tptr->context.at; i < GREG_IDX_K0; i++, tcbregptr++) { - reply.registers.gregs[i] = *tcbregptr; - } - for (i = GREG_IDX_GP, tcbregptr = &tptr->context.gp; i < GREG_IDX_CAUSE; i++, tcbregptr++) { - reply.registers.gregs[i] = *tcbregptr; - } - - reply.registers.gregs[GREG_IDX_CAUSE] = tptr->context.cause; - reply.registers.gregs[GREG_IDX_PC] = tptr->context.pc; - reply.registers.gregs[GREG_IDX_SR] = tptr->context.sr; - reply.registers.gregs[GREG_IDX_ZERO] = 0; - } else { - return TV_ERROR_INVALID_ID; - } - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonSetGRegisters(KKHeader* req) { - register KKGRegsetRequest* request = (KKGRegsetRequest*)req; - KKObjectEvent reply; - - STUBBED_PRINTF(("SetGRegisters\n")); - - if (request->header.method == RMON_CPU) { - OSThread* tptr = __rmonGetTCB(request->tid); - u64* tcbregptr; - register int i; - - if (tptr == NULL) { - return TV_ERROR_INVALID_ID; - } - - for (i = GREG_IDX_AT, tcbregptr = &tptr->context.at; i < GREG_IDX_K0; i++, tcbregptr++) { - *tcbregptr = (s32)request->registers.gregs[i]; - } - - for (i = GREG_IDX_GP, tcbregptr = &tptr->context.gp; i < GREG_IDX_CAUSE; i++, tcbregptr++) { - *tcbregptr = (s32)request->registers.gregs[i]; - } - - tptr->context.cause = request->registers.gregs[GREG_IDX_CAUSE]; - tptr->context.pc = request->registers.gregs[GREG_IDX_PC]; - tptr->context.sr = request->registers.gregs[GREG_IDX_SR]; - } else { - return TV_ERROR_INVALID_ID; - } - - reply.object = request->tid; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonGetFRegisters(KKHeader* req) { - register KKObjectRequest* request = (KKObjectRequest*)req; - KKFPregEvent reply; - OSThread* tptr; - volatile float f UNUSED; - - STUBBED_PRINTF(("GetFRegisters\n")); - - if (req->method != RMON_CPU) { - return TV_ERROR_INVALID_ID; - } - - /* touch fpu to ensure registers are saved to the context structure */ - f = 0.0f; - - tptr = __rmonGetTCB(request->object); - if (tptr == NULL) { - return TV_ERROR_INVALID_ID; - } - - __rmonCopyWords((u32*)reply.registers.fpregs.regs, (u32*)&tptr->context.fp0, ARRLEN(reply.registers.fpregs.regs)); - - reply.registers.fpcsr = tptr->context.fpcsr; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.tid = request->object; - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonSetFRegisters(KKHeader* req) { - register KKFPRegsetRequest* request = (KKFPRegsetRequest*)req; - KKObjectEvent reply; - OSThread* tptr; - volatile float f UNUSED; - - STUBBED_PRINTF(("SetFRegisters\n")); - - if (req->method != RMON_CPU) { - return TV_ERROR_INVALID_ID; - } - - /* touch fpu to ensure registers are saved to the context structure */ - f = 0.0f; - - tptr = __rmonGetTCB(request->tid); - if (tptr == NULL) { - return TV_ERROR_INVALID_ID; - } - - __rmonCopyWords((u32*)&tptr->context.fp0, (u32*)request->registers.fpregs.regs, - ARRLEN(request->registers.fpregs.regs)); - tptr->context.fpcsr = request->registers.fpcsr; - - reply.object = request->tid; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -static u32 rmonGetRcpRegister(int regNumber) { - u32 contents; - - if (__rmonRCPrunning()) { - return 0; - } - - SetUpForRCPop(FALSE); - LoadStoreSU(MIPS_SW_OPCODE, regNumber); - __rmonStepRCP(); - contents = __rmonReadWordAt((u32*)SP_DMEM_START); - CleanupFromRCPop(FALSE); - - return contents; -} - -int __rmonGetSRegs(KKHeader* req) { - register KKObjectRequest* request = (KKObjectRequest*)req; - KKCpSregEvent reply; - register int i; - - STUBBED_PRINTF(("GetSRegisters\n")); - - if (__rmonRCPrunning()) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - - reply.tid = request->object; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - - SetUpForRCPop(FALSE); - for (i = SREG_IDX_ZERO; i <= SREG_IDX_RA; i++) { - LoadStoreSU(MIPS_SW_OPCODE, i); - __rmonStepRCP(); - reply.registers.sregs[i] = __rmonReadWordAt((u32*)SP_DMEM_START); - } - CleanupFromRCPop(FALSE); - - reply.registers.sregs[SREG_IDX_DRAM_ADDR] = __rmonReadWordAt((u32*)SP_DRAM_ADDR_REG); - reply.registers.sregs[SREG_IDX_MEM_ADDR] = __rmonReadWordAt((u32*)SP_MEM_ADDR_REG); - reply.registers.sregs[SREG_IDX_RD_LEN] = __rmonReadWordAt((u32*)SP_RD_LEN_REG); - reply.registers.sregs[SREG_IDX_PC] = __rmonReadWordAt((u32*)SP_PC_REG) + SP_IMEM_START; - reply.registers.sregs[SREG_IDX_WR_LEN] = __rmonReadWordAt((u32*)SP_WR_LEN_REG); - reply.registers.sregs[SREG_IDX_STATUS] = __rmonReadWordAt((u32*)SP_STATUS_REG); - reply.registers.sregs[SREG_IDX_DMA_FULL] = __rmonReadWordAt((u32*)SP_DMA_FULL_REG); - reply.registers.sregs[SREG_IDX_DMA_BUSY] = __rmonReadWordAt((u32*)SP_DMA_BUSY_REG); - - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonSetSRegs(KKHeader* req) { - register KKCpScalarRegsetRequest* request = (KKCpScalarRegsetRequest*)req; - KKObjectEvent reply; - register int i; - - STUBBED_PRINTF(("SetSRegisters\n")); - - if (__rmonRCPrunning()) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - - SetUpForRCPop(FALSE); - for (i = SREG_IDX_ZERO; i <= SREG_IDX_RA; i++) { - __rmonWriteWordTo((u32*)SP_DMEM_START, request->registers.sregs[i]); - LoadStoreSU(MIPS_LW_OPCODE, i); - __rmonStepRCP(); - } - CleanupFromRCPop(FALSE); - - __rmonWriteWordTo((u32*)SP_DRAM_ADDR_REG, request->registers.sregs[SREG_IDX_DRAM_ADDR]); - __rmonWriteWordTo((u32*)SP_MEM_ADDR_REG, request->registers.sregs[SREG_IDX_MEM_ADDR]); - __rmonWriteWordTo((u32*)SP_PC_REG, request->registers.sregs[SREG_IDX_PC] & 0xFFF); - __rmonWriteWordTo((u32*)SP_WR_LEN_REG, request->registers.sregs[SREG_IDX_WR_LEN]); - __rmonWriteWordTo((u32*)SP_STATUS_REG, request->registers.sregs[SREG_IDX_STATUS]); - - reply.object = request->tid; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonGetVRegs(KKHeader* req) { - char* cPtr; - int sent; - int dataSize; - register KKObjectRequest* request = (KKObjectRequest*)req; - KKCpVregEvent reply; - register int i; - - STUBBED_PRINTF(("GetVRegisters\n")); - - if (__rmonRCPrunning()) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - - reply.tid = request->object; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.header.length = sizeof(reply); - - dataSize = sizeof(reply); - cPtr = (char*)&dataSize; - sent = 0; - while (sent < (signed)sizeof(dataSize)) { - sent += __osRdbSend(cPtr + sent, sizeof(dataSize) - sent, RDB_TYPE_GtoH_DEBUG); - } - - __rmonSendHeader(&reply.header, VREG_SIZE, KK_TYPE_REPLY); - - SetUpForRCPop(TRUE); - for (i = 0; i < VREG_NUM; i++) { - LoadStoreVU(MIPS_SWC2_OPCODE, i); - __rmonStepRCP(); - __rmonSendData((void*)SP_DMEM_START, VREG_SIZE); - } - CleanupFromRCPop(TRUE); - - return TV_ERROR_NO_ERROR; -} - -int __rmonSetVRegs(KKHeader* req) { - register KKCpVectorRegsetRequest* request = (KKCpVectorRegsetRequest*)req; - KKObjectEvent reply; - register int i; - - STUBBED_PRINTF(("SetVRegs\n")); - - if (__rmonRCPrunning()) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - - SetUpForRCPop(TRUE); - for (i = 0; i < VREG_NUM; i++) { - __rmonCopyWords((u32*)SP_DMEM_START, (u32*)&request->registers.vregs[i], VREG_SIZE / sizeof(u32)); - LoadStoreVU(MIPS_LWC2_OPCODE, i); - __rmonStepRCP(); - } - CleanupFromRCPop(TRUE); - - reply.object = request->tid; - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -u32 __rmonGetRegisterContents(int method, int threadNumber, int regNumber) { - if (method == RMON_CPU) { - /* CPU register */ - u32* regPointer; - OSThread* tptr; - - if (regNumber >= GREG_IDX_AT && regNumber < GREG_IDX_K0) { - regNumber -= GREG_IDX_AT - GREG_IDX_ZERO; - } else if (regNumber >= GREG_IDX_GP && regNumber < GREG_IDX_LO) { - regNumber -= GREG_IDX_GP - GREG_IDX_T9; - } else { - return 0; - } - tptr = __rmonGetTCB(threadNumber); - if (tptr == NULL) { - return 0; - } - regPointer = (u32*)&tptr->context; - regPointer += regNumber; - return *regPointer; - } else { - /* RSP register */ - return rmonGetRcpRegister(regNumber); - } -} - -#endif diff --git a/src/rmon/rmonsio.c b/src/rmon/rmonsio.c deleted file mode 100644 index b539a53..0000000 --- a/src/rmon/rmonsio.c +++ /dev/null @@ -1,79 +0,0 @@ -#include "PR/os_version.h" - -#ifndef _FINALROM - -#include "PR/os_internal.h" -#include "PR/ultraerror.h" -#include "PR/ultralog.h" -#include "PR/sptask.h" -#include "PRinternal/dbgproto.h" -#include "PRinternal/rmonint.h" -#include "PR/ramrom.h" -#include "PR/rdb.h" -#include "PR/rmon.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -static OSMesgQueue IOmq ALIGNED(0x8); -static OSMesg IOmsgs; - -void* __osRdb_DbgRead_Buf; -u8 rmonRdbReadBuf[RMON_DBG_BUF_SIZE] ALIGNED(0x10); - -void __rmonSendFault(OSThread* thread) { - volatile float f UNUSED; - u8* tPtr; - u32 sent = 0; - - /* touch fpu to ensure registers are saved to the context structure */ - f = 0.0f; - - tPtr = (u8*)thread; - while (sent < sizeof(OSThread)) { - sent += __osRdbSend(tPtr + sent, sizeof(OSThread) - sent, RDB_TYPE_GtoH_FAULT); - } -} - -void __rmonIOflush(void) { - int sent = 0; - char tstr[4]; - - while (sent <= 0) { - sent += __osRdbSend(tstr, 1, RDB_TYPE_GtoH_DEBUG_DONE); - } -} - -void __rmonIOputw(u32 word) { - int sent = 0; - char* cPtr = (char*)&word; - - while (sent < 4) { - sent += __osRdbSend(cPtr + sent, sizeof(word) - sent, RDB_TYPE_GtoH_DEBUG); - } -} - -void __rmonIOhandler(void) { - int sent; - char tstr[4]; - - osCreateMesgQueue(&IOmq, &IOmsgs, 1); - osSetEventMesg(OS_EVENT_RDB_DBG_DONE, &IOmq, NULL); - __osRdb_DbgRead_Buf = rmonRdbReadBuf; - - while (TRUE) { - osRecvMesg(&IOmq, NULL, OS_MESG_BLOCK); - - __rmonExecute((KKHeader*)&rmonRdbReadBuf); - __osRdb_DbgRead_Buf = rmonRdbReadBuf; - - sent = 0; - while (sent <= 0) { - sent += __osRdbSend(tstr, 1, RDB_TYPE_GtoH_DEBUG_READY); - } - } -} - -#endif diff --git a/src/rmon/rmontask.c b/src/rmon/rmontask.c deleted file mode 100644 index 1ff026e..0000000 --- a/src/rmon/rmontask.c +++ /dev/null @@ -1,337 +0,0 @@ -#ifndef _FINALROM - -#include "PRinternal/dbgproto.h" -#include "PR/os_internal.h" -#include "PRinternal/rmonint.h" -#include "PR/rcp.h" -#include "PR/sptask.h" - -#include "PRinternal/macros.h" - -// TODO: this comes from a header -#ident "$Revision: 1.4 $" - -void __rmonMaskIdleThreadInts(void) { - register OSThread* tptr = __osGetActiveQueue(); - - while (tptr->priority != -1) { - if (tptr->priority == OS_PRIORITY_IDLE) { - tptr->context.sr &= ~OS_IM_CPU; - tptr->context.sr |= (OS_IM_RDBREAD | OS_IM_RDBWRITE | OS_IM_CART); - break; - } - tptr = tptr->tlnext; - } -} - -OSThread* __rmonGetTCB(int threadNumber) { - register OSThread* tptr = __osGetActiveQueue(); - - if (threadNumber < 1) { - return NULL; - } - - while (tptr->priority != -1) { - if (tptr->id == threadNumber) { - return tptr; - } - tptr = tptr->tlnext; - } - - return NULL; -} - -int __rmonStopUserThreads(int whichThread) { - register int whichOne = 0; - register OSThread* tptr = __osGetActiveQueue(); - - STUBBED_PRINTF(("StopThreads %d\n", whichThread)); - - if (whichThread != 0) { - /* Stop specified thread */ - - while (tptr->priority != -1) { - if (tptr->id == whichThread) { - break; - } - tptr = tptr->tlnext; - } - - if (tptr->priority == -1) { - return 0; - } - - if (tptr->priority > OS_PRIORITY_IDLE && tptr->priority <= OS_PRIORITY_APPMAX) { - osStopThread(tptr); - if (tptr->state != OS_STATE_STOPPED) { - STUBBED_PRINTF(("Couldn't stop thread %d\n", tptr->id)); - } - whichOne = whichThread; - } - } else { - /* Stop all threads */ - - while (tptr->priority != -1) { - if (tptr->priority > OS_PRIORITY_IDLE && tptr->priority <= OS_PRIORITY_APPMAX) { - osStopThread(tptr); - if (tptr->state != OS_STATE_STOPPED) { - STUBBED_PRINTF(("Couldn\'t stop thread %d\n", tptr->id)); - } - whichOne = -1; - } - tptr = tptr->tlnext; - } - } - return whichOne; -} - -int __rmonListThreads(KKHeader* req) { - register KKObjectRequest* request = (KKObjectRequest*)req; - KKObjsEvent* reply = (KKObjsEvent*)__rmonUtilityBuffer; - - STUBBED_PRINTF(("ListThreads\n")); - - reply->object = (request->object == -1) ? RMON_PID_CPU : request->object; - - if (req->method == RMON_RSP) { - reply->objs.number = 1; - reply->objs.objects[0] = RMON_TID_RSP; - } else { - register OSThread* tptr = __osGetActiveQueue(); - - reply->objs.number = 0; - - while (tptr->priority != -1) { - if (tptr->id != 0) { - reply->objs.objects[reply->objs.number] = tptr->id; - reply->objs.number++; - } - tptr = tptr->tlnext; - } - } - reply->header.code = request->header.code; - reply->header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply->header, sizeof(*reply) + sizeof(reply->objs.objects[0]) * (reply->objs.number - 1), - KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonGetThreadStatus(int method, int id, KKStatusEvent* reply) { - u32 inst; - - STUBBED_PRINTF(("ThreadStatus %d method %d\n", id, method)); - - reply->status.tid = id; - reply->status.pid = (method == RMON_RSP) ? RMON_PID_RSP : RMON_PID_CPU; - reply->status.why = 1; - reply->status.what = 0; - reply->status.info.major = 0; - reply->status.info.minor = 0; - reply->status.rv = 0; - - if (method == RMON_RSP) { - reply->status.start = SP_IMEM_START; - reply->status.priority = RMON_PRI_RSP; - - if (__rmonRCPrunning()) { - reply->status.flags = OS_STATE_RUNNING; - /* Cannot read RSP PC or current instruction while the RSP is running */ - reply->status.info.addr = 0; - reply->status.instr = 0; - } else { - reply->status.flags = OS_STATE_STOPPED; - reply->status.info.addr = __rmonReadWordAt((u32*)SP_PC_REG) + SP_IMEM_START; - inst = __rmonReadWordAt((u32*)reply->status.info.addr); - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - inst = MIPS_BREAK_OPCODE; - } - if (__rmonRcpAtBreak) { - /* Report RSP break */ - reply->status.why = 2; - reply->status.info.major = 2; - reply->status.info.minor = 4; - } - reply->status.instr = inst; - } - } else { - OSThread* tptr = __osGetActiveQueue(); - - while (tptr->priority != -1) { - if (tptr->id == id) { - break; - } - tptr = tptr->tlnext; - } - if (tptr->priority == -1) { - return TV_ERROR_INVALID_ID; - } - - reply->status.priority = tptr->priority; - reply->status.flags = (tptr->state != 0) ? tptr->state : OS_STATE_STOPPED; - reply->status.info.addr = tptr->context.pc; - - inst = *(u32*)(tptr->context.pc); - if ((inst & MIPS_BREAK_MASK) == MIPS_BREAK_OPCODE) { - inst = MIPS_BREAK_OPCODE; - } - - reply->status.instr = inst; - reply->status.start = (int)tptr; - - if (tptr->flags & OS_FLAG_CPU_BREAK) { - /* Report break */ - reply->status.why = 2; - reply->status.info.major = 2; - reply->status.info.minor = 4; - } else if (tptr->flags & OS_FLAG_FAULT) { - /* Report fault */ - reply->status.why = 2; - reply->status.info.major = 1; - reply->status.info.minor = 2; - } - } - - return TV_ERROR_NO_ERROR; -} - -int __rmonThreadStatus(KKHeader* req) { - KKObjectRequest* request = (KKObjectRequest*)req; - KKStatusEvent reply; - - if (__rmonGetThreadStatus(req->method, request->object, &reply) != TV_ERROR_NO_ERROR) { - return TV_ERROR_INVALID_ID; - } - - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - return TV_ERROR_NO_ERROR; -} - -int __rmonStopThread(KKHeader* req) { - KKObjectRequest* request = (KKObjectRequest*)req; - KKStatusEvent reply; - u32* pc; - - STUBBED_PRINTF(("StopThread %d\n", request->object)); - - switch (req->method) { - case RMON_CPU: - __rmonStopUserThreads(request->object); - break; - case RMON_RSP: - if (__rmonRCPrunning()) { - /* Stop the rsp */ - __rmonIdleRCP(); - pc = (u32*)__rmonReadWordAt((u32*)SP_PC_REG); - if (pc == NULL) { - break; - } - pc--; - /* Check if the RSP is stopped in a branch delay slot, if it is step out of it. The RSP - would otherwise lose information about whether the branch should or should not be - taken when reading registers. */ - if (__rmonGetBranchTarget(RMON_RSP, RMON_TID_RSP, (void*)((u32)pc + SP_IMEM_START)) % 4 == 0) { - __rmonStepRCP(); - } - } - break; - default: - return TV_ERROR_OPERATIONS_PROTECTED; - } - - if (__rmonGetThreadStatus(req->method, request->object, &reply) != TV_ERROR_NO_ERROR) { - return TV_ERROR_INVALID_ID; - } - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - if (reply.status.flags == OS_STATE_STOPPED) { - reply.header.code = KK_CODE_THREAD_STATUS; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_EXCEPTION); - } - return TV_ERROR_NO_ERROR; -} - -int __rmonRunThread(KKHeader* req) { - KKRunThreadRequest* request = (KKRunThreadRequest*)req; - KKObjectEvent reply; - KKStatusEvent exceptionReply; - register OSThread* tptr; - register int runNeeded = FALSE; - - STUBBED_PRINTF(("RunThread %d\n", request->tid)); - - switch (req->method) { - case RMON_CPU: - tptr = __osGetActiveQueue(); - while (tptr->priority != -1) { - if (tptr->id == request->tid) { - break; - } - tptr = tptr->tlnext; - } - - if (tptr->priority == -1) { - return TV_ERROR_INVALID_ID; - } - if (tptr->state != OS_STATE_STOPPED) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - tptr->flags &= ~(OS_FLAG_CPU_BREAK | OS_FLAG_FAULT); - if (request->actions.flags & KK_RUN_SETPC) { - tptr->context.pc = request->actions.vaddr; - } - if ((request->actions.flags & KK_RUN_SSTEP) && !__rmonSetSingleStep(request->tid, (u32*)tptr->context.pc)) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - runNeeded = TRUE; - break; - case RMON_RSP: - if (__rmonRCPrunning()) { - return TV_ERROR_OPERATIONS_PROTECTED; - } - if (request->actions.flags & KK_RUN_SETPC) { - __rmonWriteWordTo((u32*)SP_PC_REG, request->actions.vaddr - SP_IMEM_START); - } - if (request->actions.flags & KK_RUN_SSTEP) { - /* If the RSP is stopped at a branch step twice so as to not stop in a branch delay - * slot. */ - if (__rmonGetBranchTarget(RMON_RSP, RMON_TID_RSP, - (void*)(__rmonReadWordAt((u32*)SP_PC_REG) + SP_IMEM_START)) - % 4 - == 0) { - __rmonStepRCP(); - } - __rmonStepRCP(); - __rmonRcpAtBreak = TRUE; - } else { - __rmonRcpAtBreak = FALSE; - __rmonRunRCP(); - } - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->tid; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - if (request->actions.flags & KK_RUN_SSTEP) { - __rmonGetThreadStatus(RMON_RSP, RMON_TID_RSP, &exceptionReply); - __rmonGetExceptionStatus(&exceptionReply); - __rmonSendReply(&exceptionReply.header, sizeof(exceptionReply), KK_TYPE_EXCEPTION); - } - return TV_ERROR_NO_ERROR; - default: - return TV_ERROR_OPERATIONS_PROTECTED; - } - - reply.header.code = request->header.code; - reply.header.error = TV_ERROR_NO_ERROR; - reply.object = request->tid; - __rmonSendReply(&reply.header, sizeof(reply), KK_TYPE_REPLY); - - if (runNeeded) { - osStartThread(tptr); - } - return 1; -} - -#endif diff --git a/tools/ar.py b/tools/ar.py deleted file mode 100755 index 0f3e574..0000000 --- a/tools/ar.py +++ /dev/null @@ -1,476 +0,0 @@ -#!/usr/bin/env python3 -# -# ar -# - -from genericpath import isfile -import os, struct, time -from dataclasses import dataclass -from libelf import ElfFile, SB_GLOBAL, SHN_UND - -class Archive: - @dataclass - class ArchiveFileRecord: - """ - AR file headers + data - - Offset Length Name Format - 0 16 File identifier ASCII - 16 12 File modification timestamp (in seconds) Decimal - 28 6 Owner ID Decimal - 34 6 Group ID Decimal - 40 8 File mode (type and permission) Octal - 48 10 File size in bytes Decimal - 58 2 Ending characters 0x60 0x0A - """ - name : str - time : int - uid : int - gid : int - mode : int - size : int - data : bytes - - def __init__(self): - self.files = [] # List[ArchiveFileRecord] - self.armap_entries = None - self.time = int(time.time()) - - def add_data(self, name, time, uid, gid, mode, size, data): - self.files.append(Archive.ArchiveFileRecord(name, time, uid, gid, mode, size, data)) - - def add_file(self, file_path): - if not os.path.isfile(file_path): - print(f"Error: No file named {file_path}") - return - - st = os.stat(file_path) - name = os.path.basename(file_path) - time = int(st.st_mtime) - uid = st.st_uid - gid = st.st_gid - mode = st.st_mode - size = st.st_size - - data = None - with open(file_path, "rb") as infile: - data = bytes(infile.read()) - - self.add_data(name, time, uid, gid, mode, size, data) - - def add_ar(self, file_path): - for file in Archive.from_file(file_path).files: - self.add_data(file) - - def build_image(self): - def add_bin(ba, bin): - for b in bin: - ba.append(b) - - def add_str(ba, s, pad_to_len=-1): - if pad_to_len != -1: - s = f"{s:{pad_to_len}}" - s = s.encode("ASCII") - for c in s: - ba.append(c) - - def add_pad(ba): - if len(ba) % 2 != 0: - ba.append(0) - - def add_ar_hdr(ba, name, time, uid, gid, mode, length): - add_str(ba, name, 16) - add_str(ba, str(time), 12) - add_str(ba, str(uid), 6) - add_str(ba, str(gid), 6) - add_str(ba, oct(mode)[2:], 8) - add_str(ba, str(length), 10) - add_str(ba, "`\n") - - b = bytearray() - - # MAGIC - - add_str(b, "!\n") - - # ARMAP - - armap_data= bytearray() - - armap_entries_files = [] - - num_entries = 0 - armap_size = 4 - armap_string_data = bytearray() - for i,file in enumerate(self.files): - elf = ElfFile(file.data) - if elf.symtab is not None: - for sym in elf.symtab.symbol_entries: - if sym.st_shndx != SHN_UND and sym.bind == SB_GLOBAL: - num_entries += 1 - armap_size += 4 + len(sym.name) + 1 - armap_string_data.extend(sym.name.encode("latin1") + b'\0') - armap_entries_files.append(i) - - armap_data.extend(struct.pack(">I", num_entries)) - armap_data.extend([0] * 4 * num_entries) # defer writing file positions until files are emplaced later - armap_data.extend(armap_string_data) - - if len(armap_data) % 4 != 0: - alsiz = (len(armap_data) + 3) & ~3 - armap_data.extend([0] * (alsiz - len(armap_data))) - - current_time = int(time.time()) - - add_ar_hdr(b, "/", current_time, 0, 0, 0, len(armap_data)) - armap_offsets_start = len(b) + 4 - add_bin(b, armap_data) - add_pad(b) - - # LONG STRINGS - - long_strings = "" - - file_names = [] - flag = False - for _,file in enumerate(self.files): - if len(file.name) >= 16: - fname = f"{file.name}/\n" - ind = len(long_strings) - long_strings += fname - else: - fname = f"{file.name}/" - ind = None - file_names.append((fname, ind)) - - # Weird hack - if len(long_strings) != 0 and not flag: - flag = True - long_strings += "/\n" - - long_strings += "/" - - add_ar_hdr(b, "//", current_time, 0, 0, 0, len(long_strings)) - add_str(b, long_strings) - add_pad(b) - - # FILES - - armap_pos = 0 - for i,(file,(fname,ind)) in enumerate(zip(self.files, file_names)): - file_pos = len(b) - add_ar_hdr(b, f"/{ind}" if ind is not None else fname, file.time, file.uid, file.gid, file.mode, file.size) - add_bin(b, file.data) - add_pad(b) - - # Patch the armap with file locations - while armap_pos < len(armap_entries_files) and armap_entries_files[armap_pos] == i: - b[armap_offsets_start+armap_pos*4:armap_offsets_start+armap_pos*4+4] = struct.pack(">I", file_pos) - armap_pos += 1 - - return b - - def write(self, file_path): - ar = self.build_image() - with open(file_path, "wb") as outfile: - outfile.write(ar) - - @staticmethod - def from_image(ar_data): - long_strings = None - ar = Archive() - - assert ar_data[:8].decode("ASCII") == "!\n" , "Not an archive file? Bad file magic value" - - i = 8 - while i < len(ar_data): - file_name = ar_data[i:][ 0:][:16].decode("ASCII").strip() - file_time = int(ar_data[i:][16:][:12].decode("ASCII").strip()) - file_uid = int(ar_data[i:][28:][: 6].decode("ASCII").strip()) - file_gid = int(ar_data[i:][34:][: 6].decode("ASCII").strip()) - file_mode = int(ar_data[i:][40:][: 8].decode("ASCII").strip(), 8) - file_size = int(ar_data[i:][48:][:10].decode("ASCII").strip()) - end = ar_data[i:][58:][:2].decode("ASCII") - assert end == "`\n" - - data = ar_data[i:][60:][:file_size] - assert len(data) == file_size - - if file_name == '/': - """ - "armap" - The special filename "/" denotes that this entry contains a symbol lookup table used by some libraries - to speed up file access. - The symbol table is comprised of three contiguous parts: - - A 32-bit Big Endian integer recording the number of symbol entries. - - A list of 32-bit Big Endian integers for each symbol entry, recording the position within the - archive of the header of the file containing the symbol. - - A list of null-terminated strings, the symbol names for each symbol entry. - - We rebuild this from scratch on write out. - """ - ar.time = file_time - assert file_uid == 0 - assert file_gid == 0 - assert file_mode == 0 - - # Code to interpret the armap, currently unused - """ - armap_n_syms = struct.unpack(">I", data[0:4])[0] - offsets = [i[0] for i in struct.iter_unpack(">I", data[4:4+4*armap_n_syms])] - - strings = [] - ofs = 4 + 4 * armap_n_syms - for _ in range(armap_n_syms): - to = data.find(b'\0', ofs) - assert to != -1 - string = data[ofs:to].decode('latin1') - strings.append(string) - ofs += len(string) + 1 - assert all([b == 0 for b in data[ofs:]]) - assert len(strings) == len(offsets) - - ar.armap_entries = list(zip(offsets, strings)) - """ - elif file_name == '//': - """ - Long string table. Strings larger than 16 are placed here and referenced from the header by /. - - We rebuild this from scratch on write out. - """ - assert file_time == ar.time - assert file_uid == 0 - assert file_gid == 0 - assert file_mode == 0 - long_strings = data.decode("ASCII") - else: - """ - Normal files. - """ - if file_name.startswith("/"): - assert long_strings is not None - # Fetch the name from the long string table - file_name = long_strings[int(file_name[1:]):].split("\n")[0] - # Add file - ar.add_data(file_name[:-1], file_time, file_uid, file_gid, file_mode, file_size, data) - - if file_size % 2 != 0: - file_size += 1 - i += 60 + file_size - - return ar - - @staticmethod - def from_file(file_path): - ar_file = None - with open(file_path, "rb") as infile: - ar_file = infile.read() - - return Archive.from_image(ar_file) - -def ar_usage(progname): - print(f"Usage: {progname} [-]{{dmpqrstx}}[abcDfilMNoOPsSTuvV] [member-name] [count] archive-file file...") - print(f" commands:") - print(f" d - delete file(s) from the archive") - print(f" m[ab] - move file(s) in the archive") - print(f" p - print file(s) found in the archive") - print(f" q[f] - quick append file(s) to the archive") - print(f" r[ab][f][u] - replace existing or insert new file(s) into the archive") - print(f" s - act as ranlib") - print(f" t[O][v] - display contents of the archive") - print(f" x[o] - extract file(s) from the archive") - print(f" command specific modifiers:") - print(f" [a] - put file(s) after [member-name]") - print(f" [b] - put file(s) before [member-name] (same as [i])") - print(f" [D] - use zero for timestamps and uids/gids (default)") - print(f" [U] - use actual timestamps and uids/gids") - print(f" [N] - use instance [count] of name") - print(f" [f] - truncate inserted file names") - print(f" [P] - use full path names when matching") - print(f" [o] - preserve original dates") - print(f" [O] - display offsets of files in the archive") - print(f" [u] - only replace files that are newer than current archive contents") - print(f" generic modifiers:") - print(f" [c] - do not warn if the library had to be created") - print(f" [s] - create an archive index (cf. ranlib)") - print(f" [S] - do not build a symbol table") - print(f" [v] - be verbose") - print(f" [V] - display the version number") - print(f" @ - read options from ") - print(f" --output=DIRNAME - specify the output directory for extraction operations") - return 1 - -def ar(argv): - if len(argv) < 2: - return ar_usage(argv[0]) - - progname = argv[0] - create_ok = False - make_ar_idx = False - no_symtab = False - verbose = False - - def verbose_print(msg): - if verbose: - print(msg) - - def dcmd(modifiers, args): - if modifiers != "": - print(f"{progname}: bad modifiers -- '{modifiers}'") - return ar_usage(progname) - - ar_file = args[0] - o_files = args[1:] - - ar = Archive.from_file(ar_file) - for file in ar.files: - if file.name in o_files: - ar.files.remove(file) - ar.write(ar_file) - return 0 - - def mcmd(modifiers, args): - print("") - return 1 - - def pcmd(modifiers, args): - if modifiers != "": - print(f"{progname}: bad modifiers -- '{modifiers}'") - return ar_usage(progname) - if len(args) != 1: - print(f"{progname} p: bad args") - return ar_usage(progname) - - ar_file = args[0] - - ar = Archive.from_file(ar_file) - for file in ar.files: - print(file.data) - return 0 - - def qcmd(modifiers, args): - print("") - return 1 - - def rcmd(modifiers, args): - if modifiers !="": - print("") - if len(args) < 2: - print(f"{progname} r: bad args") - return ar_usage(progname) - - ar_file = args[0] - o_files = args[1:] - - ar = Archive() - if os.path.isfile(ar_file): - ar = Archive.from_file(ar_file) - else: - if not create_ok: - print(f"Warning: Created file {ar_file}") - ar = Archive() - - for o_file in o_files: - ar.add_file(o_file) - - ar.write(ar_file) - - def scmd(modifiers, args): - print("") - return 1 - - def tcmd(modifiers, args): - # TODO modifiers - - if len(args) != 1: - print(f"{progname} t: bad args") - return ar_usage(progname) - - ar_file = args[0] - - ar = Archive.from_file(ar_file) - for file in ar.files: - print(file.name) - return 0 - - def xcmd(modifiers, args): - if modifiers not in ('', 'o'): - print(f"{progname}: bad modifiers -- '{modifiers}'") - return ar_usage(progname) - if len(args) not in (1, 3) or not (args[0].startswith("--output") or args[1].startswith("--output")): - print(f"{progname} t: bad args") - return ar_usage(progname) - - original_times = modifiers == 'o' - - if args[0].startswith("--output"): - out_dir = args[1] - ar_file = args[2] - elif args[1].startswith("--output"): - out_dir = args[2] - ar_file = args[0] - else: - ar_file = args[0] - out_dir = "" - - # Create dir if not exists - if not os.path.exists(out_dir): - os.makedirs(out_dir, exist_ok=True) - - if not os.path.isdir(out_dir): - print(f"Output directory {out_dir} is a file") - return ar_usage(progname) - - # Extract files to destination - ar = Archive.from_file(ar_file) - for file in ar.files: - opath =os.path.join(out_dir, file.name) - with open(opath, "wb") as ofile: - ofile.write(file.data) - - if not original_times: - t = time.time() - os.utime(opath, (t, t)) - # TODO patch with original dates etc. - - return 0 - - argtbl = { - 'd': dcmd, # delete file(s) from the archive - 'm': mcmd, # [ab] move file(s) in the archive - 'p': pcmd, # print file(s) found in the archive - 'q': qcmd, # [f] quick append file(s) to the archive - 'r': rcmd, # [ab][f][u] replace existing or insert new file(s) into the archive - 's': scmd, # act as ranlib - 't': tcmd, # [O][v] display contents of the archive - 'x': xcmd, # [o] extract file(s) from the archive - } - - arg1 = argv[1] - if arg1[0] == '-': - arg1 = arg1[1:] - cmd = arg1[0] - - if cmd not in argtbl: - print(f"{progname}: invalid option -- '{cmd}'") - return ar_usage(argv[0]) - - modifiers = arg1[1:] - - create_ok = "c" in modifiers - modifiers = modifiers.replace("c","") - make_ar_idx = "s" in modifiers - modifiers = modifiers.replace("s","") - no_symtab = "S" in modifiers - modifiers = modifiers.replace("S","") - verbose = "v" in modifiers - modifiers = modifiers.replace("v","") - - if "V" in modifiers: - print(f"{progname} v1.0") - - return argtbl[cmd](modifiers, argv[2:]) - -if __name__ == '__main__': - import sys - sys.exit(ar(sys.argv)) diff --git a/tools/asm_differ/.gitignore b/tools/asm_differ/.gitignore deleted file mode 100644 index eb176dc..0000000 --- a/tools/asm_differ/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.mypy_cache/ -__pycache__/ diff --git a/tools/asm_differ/.gitrepo b/tools/asm_differ/.gitrepo deleted file mode 100644 index 2e999c8..0000000 --- a/tools/asm_differ/.gitrepo +++ /dev/null @@ -1,12 +0,0 @@ -; DO NOT EDIT (unless you know what you are doing) -; -; This subdirectory is a git "subrepo", and this file is maintained by the -; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme -; -[subrepo] - remote = https://github.com/simonlindholm/asm-differ - branch = main - commit = 4b38c884c1efdc3bfa8b14f13015a69368a8d3a2 - parent = 32a1a8061de197c4f10d4904cd72a22dd7cf905c - method = merge - cmdver = 0.4.3 diff --git a/tools/asm_differ/.pre-commit-config.yaml b/tools/asm_differ/.pre-commit-config.yaml deleted file mode 100644 index 6695f71..0000000 --- a/tools/asm_differ/.pre-commit-config.yaml +++ /dev/null @@ -1,6 +0,0 @@ -repos: -- repo: https://github.com/psf/black - rev: 20.8b1 - hooks: - - id: black - language_version: python3.6 diff --git a/tools/asm_differ/LICENSE b/tools/asm_differ/LICENSE deleted file mode 100644 index cf1ab25..0000000 --- a/tools/asm_differ/LICENSE +++ /dev/null @@ -1,24 +0,0 @@ -This is free and unencumbered software released into the public domain. - -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. - -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. - -For more information, please refer to diff --git a/tools/asm_differ/README.md b/tools/asm_differ/README.md deleted file mode 100644 index 4a7f329..0000000 --- a/tools/asm_differ/README.md +++ /dev/null @@ -1,56 +0,0 @@ -# asm-differ - -Nice differ for assembly code. Currently supports MIPS, PPC, AArch64, and ARM32; should be easy to hack to support other instruction sets. - -![](screenshot.png) - -## Dependencies - -- Python >= 3.6 -- `python3 -m pip install --user colorama watchdog python-Levenshtein` (also `dataclasses` if on 3.6) - -## Usage - -Create a file `diff_settings.sh` in some directory (see the one in this repo for an example). Then from that directory, run - -```bash -/path/to/diff.sh [flags] (function|rom addr) -``` - -Recommended flags are `-mwo` (automatically run `make` on source file changes, and include symbols in diff). See `--help` for more details. - -### Tab completion - -[argcomplete](https://kislyuk.github.io/argcomplete/) can be optionally installed (with `python3 -m pip install argcomplete`) to enable tab completion in a bash shell, completing options and symbol names using the linker map. It also requires a bit more setup: - -If invoking the script **exactly** as `./diff.py`, the following should be added to the `.bashrc` according to argcomplete's instructions: - -```bash -eval "$(register-python-argcomplete ./diff.py)" -``` - -If that doesn't work, run `register-python-argcomplete ./diff.py` in your terminal and copy the output to `.bashrc`. - -If setup correctly (don't forget to restart the shell), `complete | grep ./diff.py` should output: - -```bash -complete -o bashdefault -o default -o nospace -F _python_argcomplete ./diff.py -``` - -Note for developers or for general troubleshooting: run `export _ARC_DEBUG=` to enable debug output during tab-completion, it may show otherwise silenced errors. Use `unset _ARC_DEBUG` or restart the terminal to disable. - -### Contributing - -Contributions are very welcome! Some notes on workflow: - -`black` is used for code formatting. You can either run `black diff.py` manually, or set up a pre-commit hook: -```bash -pip install pre-commit black -pre-commit install -``` - -Type annotations are used for all Python code. `mypy` should pass without any errors. - -PRs that skip the above are still welcome, however. - -The targeted Python version is 3.6. There are currently no tests. diff --git a/tools/asm_differ/diff-stylesheet.css b/tools/asm_differ/diff-stylesheet.css deleted file mode 100644 index 79da120..0000000 --- a/tools/asm_differ/diff-stylesheet.css +++ /dev/null @@ -1,67 +0,0 @@ -table.diff { - border: none; - font-family: Monospace; - white-space: pre; -} -tr.data-ref { - background-color: gray; -} -.immediate { - color: lightblue; -} -.stack { - color: yellow; -} -.register { - color: yellow; -} -.delay-slot { - font-weight: bold; - color: gray; -} -.diff-change { - color: lightblue; -} -.diff-add { - color: green; -} -.diff-remove { - color: red; -} -.source-filename { - font-weight: bold; -} -.source-function { - font-weight: bold; - text-decoration: underline; -} -.source-other { - font-style: italic; -} -.rotation-0 { - color: magenta; -} -.rotation-1 { - color: cyan; -} -.rotation-2 { - color: green; -} -.rotation-3 { - color: red; -} -.rotation-4 { - color: yellow; -} -.rotation-5 { - color: pink; -} -.rotation-6 { - color: blue; -} -.rotation-7 { - color: lime; -} -.rotation-8 { - color: gray; -} diff --git a/tools/asm_differ/diff.py b/tools/asm_differ/diff.py deleted file mode 100755 index 8ed764f..0000000 --- a/tools/asm_differ/diff.py +++ /dev/null @@ -1,2923 +0,0 @@ -#!/usr/bin/env python3 -# PYTHON_ARGCOMPLETE_OK -import argparse -import sys -from typing import ( - Any, - Callable, - Dict, - Iterator, - List, - Match, - NoReturn, - Optional, - Pattern, - Set, - Tuple, - Type, - Union, -) - - -def fail(msg: str) -> NoReturn: - print(msg, file=sys.stderr) - sys.exit(1) - - -def static_assert_unreachable(x: NoReturn) -> NoReturn: - raise Exception("Unreachable! " + repr(x)) - - -# ==== COMMAND-LINE ==== - -if __name__ == "__main__": - # Prefer to use diff_settings.py from the current working directory - sys.path.insert(0, ".") - try: - import diff_settings - except ModuleNotFoundError: - fail("Unable to find diff_settings.py in the same directory.") - sys.path.pop(0) - - try: - import argcomplete - except ModuleNotFoundError: - argcomplete = None - - parser = argparse.ArgumentParser( - description="Diff MIPS, PPC, AArch64, or ARM32 assembly." - ) - - start_argument = parser.add_argument( - "start", - help="Function name or address to start diffing from.", - ) - - if argcomplete: - - def complete_symbol( - prefix: str, parsed_args: argparse.Namespace, **kwargs: object - ) -> List[str]: - if not prefix or prefix.startswith("-"): - # skip reading the map file, which would - # result in a lot of useless completions - return [] - config: Dict[str, Any] = {} - diff_settings.apply(config, parsed_args) # type: ignore - mapfile = config.get("mapfile") - if not mapfile: - return [] - completes = [] - with open(mapfile) as f: - data = f.read() - # assume symbols are prefixed by a space character - search = f" {prefix}" - pos = data.find(search) - while pos != -1: - # skip the space character in the search string - pos += 1 - # assume symbols are suffixed by either a space - # character or a (unix-style) line return - spacePos = data.find(" ", pos) - lineReturnPos = data.find("\n", pos) - if lineReturnPos == -1: - endPos = spacePos - elif spacePos == -1: - endPos = lineReturnPos - else: - endPos = min(spacePos, lineReturnPos) - if endPos == -1: - match = data[pos:] - pos = -1 - else: - match = data[pos:endPos] - pos = data.find(search, endPos) - completes.append(match) - return completes - - setattr(start_argument, "completer", complete_symbol) - - parser.add_argument( - "end", - nargs="?", - help="Address to end diff at.", - ) - parser.add_argument( - "-o", - dest="diff_obj", - action="store_true", - help="""Diff .o files rather than a whole binary. This makes it possible to - see symbol names. (Recommended)""", - ) - parser.add_argument( - "-e", - "--elf", - dest="diff_elf_symbol", - metavar="SYMBOL", - help="""Diff a given function in two ELFs, one being stripped and the other - one non-stripped. Requires objdump from binutils 2.33+.""", - ) - parser.add_argument( - "-c", - "--source", - dest="show_source", - action="store_true", - help="Show source code (if possible). Only works with -o or -e.", - ) - parser.add_argument( - "-C", - "--source-old-binutils", - dest="source_old_binutils", - action="store_true", - help="""Tweak --source handling to make it work with binutils < 2.33. - Implies --source.""", - ) - parser.add_argument( - "-j", - "--section", - dest="diff_section", - default=".text", - metavar="SECTION", - help="Diff restricted to a given output section.", - ) - parser.add_argument( - "-L", - "--line-numbers", - dest="show_line_numbers", - action="store_const", - const=True, - help="""Show source line numbers in output, when available. May be enabled by - default depending on diff_settings.py.""", - ) - parser.add_argument( - "--no-line-numbers", - dest="show_line_numbers", - action="store_const", - const=False, - help="Hide source line numbers in output.", - ) - parser.add_argument( - "--inlines", - dest="inlines", - action="store_true", - help="Show inline function calls (if possible). Only works with -o or -e.", - ) - parser.add_argument( - "--base-asm", - dest="base_asm", - metavar="FILE", - help="Read assembly from given file instead of configured base img.", - ) - parser.add_argument( - "--write-asm", - dest="write_asm", - metavar="FILE", - help="Write the current assembly output to file, e.g. for use with --base-asm.", - ) - parser.add_argument( - "-m", - "--make", - dest="make", - action="store_true", - help="Automatically run 'make' on the .o file or binary before diffing.", - ) - parser.add_argument( - "-l", - "--skip-lines", - dest="skip_lines", - metavar="LINES", - type=int, - default=0, - help="Skip the first LINES lines of output.", - ) - parser.add_argument( - "-s", - "--stop-jr-ra", - dest="stop_jrra", - action="store_true", - help="""Stop disassembling at the first 'jr ra'. Some functions have - multiple return points, so use with care!""", - ) - parser.add_argument( - "-i", - "--ignore-large-imms", - dest="ignore_large_imms", - action="store_true", - help="Pretend all large enough immediates are the same.", - ) - parser.add_argument( - "-I", - "--ignore-addr-diffs", - dest="ignore_addr_diffs", - action="store_true", - help="Ignore address differences. Currently only affects AArch64 and ARM32.", - ) - parser.add_argument( - "-B", - "--no-show-branches", - dest="show_branches", - action="store_false", - help="Don't visualize branches/branch targets.", - ) - parser.add_argument( - "-S", - "--base-shift", - dest="base_shift", - metavar="N", - type=str, - default="0", - help="""Diff position N in our img against position N + shift in the base img. - Arithmetic is allowed, so e.g. |-S "0x1234 - 0x4321"| is a reasonable - flag to pass if it is known that position 0x1234 in the base img syncs - up with position 0x4321 in our img. Not supported together with -o.""", - ) - parser.add_argument( - "-w", - "--watch", - dest="watch", - action="store_true", - help="""Automatically update when source/object files change. - Recommended in combination with -m.""", - ) - parser.add_argument( - "-3", - "--threeway=prev", - dest="threeway", - action="store_const", - const="prev", - help="""Show a three-way diff between target asm, current asm, and asm - prior to -w rebuild. Requires -w.""", - ) - parser.add_argument( - "-b", - "--threeway=base", - dest="threeway", - action="store_const", - const="base", - help="""Show a three-way diff between target asm, current asm, and asm - when diff.py was started. Requires -w.""", - ) - parser.add_argument( - "--width", - dest="column_width", - metavar="COLS", - type=int, - default=50, - help="Sets the width of the left and right view column.", - ) - parser.add_argument( - "--algorithm", - dest="algorithm", - default="levenshtein", - choices=["levenshtein", "difflib"], - help="""Diff algorithm to use. Levenshtein gives the minimum diff, while difflib - aims for long sections of equal opcodes. Defaults to %(default)s.""", - ) - parser.add_argument( - "--max-size", - "--max-lines", - metavar="LINES", - dest="max_lines", - type=int, - default=1024, - help="The maximum length of the diff, in lines.", - ) - parser.add_argument( - "--no-pager", - dest="no_pager", - action="store_true", - help="""Disable the pager; write output directly to stdout, then exit. - Incompatible with --watch.""", - ) - parser.add_argument( - "--format", - choices=("color", "plain", "html", "json"), - default="color", - help="Output format, default is color. --format=html or json implies --no-pager.", - ) - parser.add_argument( - "-U", - "--compress-matching", - metavar="N", - dest="compress_matching", - type=int, - help="""Compress streaks of matching lines, leaving N lines of context - around non-matching parts.""", - ) - parser.add_argument( - "-V", - "--compress-sameinstr", - metavar="N", - dest="compress_sameinstr", - type=int, - help="""Compress streaks of lines with same instructions (but possibly - different regalloc), leaving N lines of context around other parts.""", - ) - - # Project-specific flags, e.g. different versions/make arguments. - add_custom_arguments_fn = getattr(diff_settings, "add_custom_arguments", None) - if add_custom_arguments_fn: - add_custom_arguments_fn(parser) - - if argcomplete: - argcomplete.autocomplete(parser) - -# ==== IMPORTS ==== - -# (We do imports late to optimize auto-complete performance.) - -import abc -import ast -from collections import Counter, defaultdict -from dataclasses import asdict, dataclass, field, replace -import difflib -import enum -import html -import itertools -import json -import os -import queue -import re -import string -import struct -import subprocess -import threading -import time -import traceback - - -MISSING_PREREQUISITES = ( - "Missing prerequisite python module {}. " - "Run `python3 -m pip install --user colorama watchdog python-Levenshtein cxxfilt` to install prerequisites (cxxfilt only needed with --source)." -) - -try: - from colorama import Back, Fore, Style - import watchdog -except ModuleNotFoundError as e: - fail(MISSING_PREREQUISITES.format(e.name)) - -# ==== CONFIG ==== - - -@dataclass -class ProjectSettings: - arch_str: str - objdump_executable: str - build_command: List[str] - map_format: str - mw_build_dir: str - baseimg: Optional[str] - myimg: Optional[str] - mapfile: Optional[str] - source_directories: Optional[List[str]] - source_extensions: List[str] - show_line_numbers_default: bool - disassemble_all: bool - - -@dataclass -class Compress: - context: int - same_instr: bool - - -@dataclass -class Config: - arch: "ArchSettings" - - # Build/objdump options - diff_obj: bool - make: bool - source_old_binutils: bool - diff_section: str - inlines: bool - max_function_size_lines: int - max_function_size_bytes: int - - # Display options - formatter: "Formatter" - threeway: Optional[str] - base_shift: int - skip_lines: int - compress: Optional[Compress] - show_branches: bool - show_line_numbers: bool - show_source: bool - stop_jrra: bool - ignore_large_imms: bool - ignore_addr_diffs: bool - algorithm: str - - # Score options - score_stack_differences = True - penalty_stackdiff = 1 - penalty_regalloc = 5 - penalty_reordering = 60 - penalty_insertion = 100 - penalty_deletion = 100 - - -def create_project_settings(settings: Dict[str, Any]) -> ProjectSettings: - return ProjectSettings( - arch_str=settings.get("arch", "mips"), - baseimg=settings.get("baseimg"), - myimg=settings.get("myimg"), - mapfile=settings.get("mapfile"), - build_command=settings.get( - "make_command", ["make", *settings.get("makeflags", [])] - ), - source_directories=settings.get("source_directories"), - source_extensions=settings.get( - "source_extensions", [".c", ".h", ".cpp", ".hpp", ".s"] - ), - objdump_executable=get_objdump_executable(settings.get("objdump_executable")), - map_format=settings.get("map_format", "gnu"), - mw_build_dir=settings.get("mw_build_dir", "build/"), - show_line_numbers_default=settings.get("show_line_numbers_default", True), - disassemble_all=settings.get("disassemble_all", False) - ) - - -def create_config(args: argparse.Namespace, project: ProjectSettings) -> Config: - arch = get_arch(project.arch_str) - - formatter: Formatter - if args.format == "plain": - formatter = PlainFormatter(column_width=args.column_width) - elif args.format == "color": - formatter = AnsiFormatter(column_width=args.column_width) - elif args.format == "html": - formatter = HtmlFormatter() - elif args.format == "json": - formatter = JsonFormatter(arch_str=arch.name) - else: - raise ValueError(f"Unsupported --format: {args.format}") - - compress = None - if args.compress_matching is not None: - compress = Compress(args.compress_matching, False) - if args.compress_sameinstr is not None: - if compress is not None: - raise ValueError( - "Cannot pass both --compress-matching and --compress-sameinstr" - ) - compress = Compress(args.compress_sameinstr, True) - - show_line_numbers = args.show_line_numbers - if show_line_numbers is None: - show_line_numbers = project.show_line_numbers_default - - return Config( - arch=arch, - # Build/objdump options - diff_obj=args.diff_obj, - make=args.make, - source_old_binutils=args.source_old_binutils, - diff_section=args.diff_section, - inlines=args.inlines, - max_function_size_lines=args.max_lines, - max_function_size_bytes=args.max_lines * 4, - # Display options - formatter=formatter, - threeway=args.threeway, - base_shift=eval_int( - args.base_shift, "Failed to parse --base-shift (-S) argument as an integer." - ), - skip_lines=args.skip_lines, - compress=compress, - show_branches=args.show_branches, - show_line_numbers=show_line_numbers, - show_source=args.show_source or args.source_old_binutils, - stop_jrra=args.stop_jrra, - ignore_large_imms=args.ignore_large_imms, - ignore_addr_diffs=args.ignore_addr_diffs, - algorithm=args.algorithm, - ) - - -def get_objdump_executable(objdump_executable: Optional[str]) -> str: - if objdump_executable is not None: - return objdump_executable - - objdump_candidates = [ - "mips-linux-gnu-objdump", - "mips64-elf-objdump", - "mips-elf-objdump", - ] - for objdump_cand in objdump_candidates: - try: - subprocess.check_call( - [objdump_cand, "--version"], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - return objdump_cand - except subprocess.CalledProcessError: - pass - except FileNotFoundError: - pass - - return fail( - f"Missing binutils; please ensure {' or '.join(objdump_candidates)} exists, or configure objdump_executable." - ) - - -def get_arch(arch_str: str) -> "ArchSettings": - for settings in ARCH_SETTINGS: - if arch_str == settings.name: - return settings - raise ValueError(f"Unknown architecture: {arch_str}") - - -BUFFER_CMD: List[str] = ["tail", "-c", str(10 ** 9)] - -# -S truncates long lines instead of wrapping them -# -R interprets color escape sequences -# -i ignores case when searching -# -c something about how the screen gets redrawn; I don't remember the purpose -# -#6 makes left/right arrow keys scroll by 6 characters -LESS_CMD: List[str] = ["less", "-SRic", "-#6"] - -DEBOUNCE_DELAY: float = 0.1 - -# ==== FORMATTING ==== - - -@enum.unique -class BasicFormat(enum.Enum): - NONE = enum.auto() - IMMEDIATE = enum.auto() - STACK = enum.auto() - REGISTER = enum.auto() - DELAY_SLOT = enum.auto() - DIFF_CHANGE = enum.auto() - DIFF_ADD = enum.auto() - DIFF_REMOVE = enum.auto() - SOURCE_FILENAME = enum.auto() - SOURCE_FUNCTION = enum.auto() - SOURCE_LINE_NUM = enum.auto() - SOURCE_OTHER = enum.auto() - - -@dataclass(frozen=True) -class RotationFormat: - group: str - index: int - key: str - - -Format = Union[BasicFormat, RotationFormat] -FormatFunction = Callable[[str], Format] - - -class Text: - segments: List[Tuple[str, Format]] - - def __init__(self, line: str = "", f: Format = BasicFormat.NONE) -> None: - self.segments = [(line, f)] if line else [] - - def reformat(self, f: Format) -> "Text": - return Text(self.plain(), f) - - def plain(self) -> str: - return "".join(s for s, f in self.segments) - - def __repr__(self) -> str: - return f"" - - def __bool__(self) -> bool: - return any(s for s, f in self.segments) - - def __str__(self) -> str: - # Use Formatter.apply(...) instead - return NotImplemented - - def __eq__(self, other: object) -> bool: - return NotImplemented - - def __add__(self, other: Union["Text", str]) -> "Text": - if isinstance(other, str): - other = Text(other) - result = Text() - # If two adjacent segments have the same format, merge their lines - if ( - self.segments - and other.segments - and self.segments[-1][1] == other.segments[0][1] - ): - result.segments = ( - self.segments[:-1] - + [(self.segments[-1][0] + other.segments[0][0], self.segments[-1][1])] - + other.segments[1:] - ) - else: - result.segments = self.segments + other.segments - return result - - def __radd__(self, other: Union["Text", str]) -> "Text": - if isinstance(other, str): - other = Text(other) - return other + self - - def finditer(self, pat: Pattern[str]) -> Iterator[Match[str]]: - """Replacement for `pat.finditer(text)` that operates on the inner text, - and returns the exact same matches as `Text.sub(pat, ...)`.""" - for chunk, f in self.segments: - for match in pat.finditer(chunk): - yield match - - def sub(self, pat: Pattern[str], sub_fn: Callable[[Match[str]], "Text"]) -> "Text": - result = Text() - for chunk, f in self.segments: - i = 0 - for match in pat.finditer(chunk): - start, end = match.start(), match.end() - assert i <= start <= end <= len(chunk) - sub = sub_fn(match) - if i != start: - result.segments.append((chunk[i:start], f)) - result.segments.extend(sub.segments) - i = end - if chunk[i:]: - result.segments.append((chunk[i:], f)) - return result - - def ljust(self, column_width: int) -> "Text": - length = sum(len(x) for x, _ in self.segments) - return self + " " * max(column_width - length, 0) - - -@dataclass -class TableMetadata: - headers: Tuple[Text, ...] - current_score: int - max_score: int - previous_score: Optional[int] - - -class Formatter(abc.ABC): - @abc.abstractmethod - def apply_format(self, chunk: str, f: Format) -> str: - """Apply the formatting `f` to `chunk` and escape the contents.""" - ... - - @abc.abstractmethod - def table(self, meta: TableMetadata, lines: List[Tuple["OutputLine", ...]]) -> str: - """Format a multi-column table with metadata""" - ... - - def apply(self, text: Text) -> str: - return "".join(self.apply_format(chunk, f) for chunk, f in text.segments) - - @staticmethod - def outputline_texts(lines: Tuple["OutputLine", ...]) -> Tuple[Text, ...]: - return tuple([lines[0].base or Text()] + [line.fmt2 for line in lines[1:]]) - - -@dataclass -class PlainFormatter(Formatter): - column_width: int - - def apply_format(self, chunk: str, f: Format) -> str: - return chunk - - def table(self, meta: TableMetadata, lines: List[Tuple["OutputLine", ...]]) -> str: - rows = [meta.headers] + [self.outputline_texts(ls) for ls in lines] - return "\n".join( - "".join(self.apply(x.ljust(self.column_width)) for x in row) for row in rows - ) - - -@dataclass -class AnsiFormatter(Formatter): - # Additional ansi escape codes not in colorama. See: - # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters - STYLE_UNDERLINE = "\x1b[4m" - STYLE_NO_UNDERLINE = "\x1b[24m" - STYLE_INVERT = "\x1b[7m" - - BASIC_ANSI_CODES = { - BasicFormat.NONE: "", - BasicFormat.IMMEDIATE: Fore.LIGHTBLUE_EX, - BasicFormat.STACK: Fore.YELLOW, - BasicFormat.REGISTER: Fore.YELLOW, - BasicFormat.DELAY_SLOT: Fore.LIGHTBLACK_EX, - BasicFormat.DIFF_CHANGE: Fore.LIGHTBLUE_EX, - BasicFormat.DIFF_ADD: Fore.GREEN, - BasicFormat.DIFF_REMOVE: Fore.RED, - BasicFormat.SOURCE_FILENAME: Style.DIM + Style.BRIGHT, - BasicFormat.SOURCE_FUNCTION: Style.DIM + Style.BRIGHT + STYLE_UNDERLINE, - BasicFormat.SOURCE_LINE_NUM: Fore.LIGHTBLACK_EX, - BasicFormat.SOURCE_OTHER: Style.DIM, - } - - BASIC_ANSI_CODES_UNDO = { - BasicFormat.NONE: "", - BasicFormat.SOURCE_FILENAME: Style.NORMAL, - BasicFormat.SOURCE_FUNCTION: Style.NORMAL + STYLE_NO_UNDERLINE, - BasicFormat.SOURCE_OTHER: Style.NORMAL, - } - - ROTATION_ANSI_COLORS = [ - Fore.MAGENTA, - Fore.CYAN, - Fore.GREEN, - Fore.RED, - Fore.LIGHTYELLOW_EX, - Fore.LIGHTMAGENTA_EX, - Fore.LIGHTCYAN_EX, - Fore.LIGHTGREEN_EX, - Fore.LIGHTBLACK_EX, - ] - - column_width: int - - def apply_format(self, chunk: str, f: Format) -> str: - if f == BasicFormat.NONE: - return chunk - undo_ansi_code = Fore.RESET - if isinstance(f, BasicFormat): - ansi_code = self.BASIC_ANSI_CODES[f] - undo_ansi_code = self.BASIC_ANSI_CODES_UNDO.get(f, undo_ansi_code) - elif isinstance(f, RotationFormat): - ansi_code = self.ROTATION_ANSI_COLORS[ - f.index % len(self.ROTATION_ANSI_COLORS) - ] - else: - static_assert_unreachable(f) - return f"{ansi_code}{chunk}{undo_ansi_code}" - - def table(self, meta: TableMetadata, lines: List[Tuple["OutputLine", ...]]) -> str: - rows = [(meta.headers, False)] + [ - (self.outputline_texts(line), line[1].is_data_ref) for line in lines - ] - return "\n".join( - "".join( - (self.STYLE_INVERT if is_data_ref else "") - + self.apply(x.ljust(self.column_width)) - for x in row - ) - for (row, is_data_ref) in rows - ) - - -@dataclass -class HtmlFormatter(Formatter): - rotation_formats: int = 9 - - def apply_format(self, chunk: str, f: Format) -> str: - chunk = html.escape(chunk) - if f == BasicFormat.NONE: - return chunk - if isinstance(f, BasicFormat): - class_name = f.name.lower().replace("_", "-") - data_attr = "" - elif isinstance(f, RotationFormat): - class_name = f"rotation-{f.index % self.rotation_formats}" - rotation_key = html.escape(f"{f.group};{f.key}", quote=True) - data_attr = f'data-rotation="{rotation_key}"' - else: - static_assert_unreachable(f) - return f"{chunk}" - - def table(self, meta: TableMetadata, lines: List[Tuple["OutputLine", ...]]) -> str: - def table_row(line: Tuple[Text, ...], is_data_ref: bool, cell_el: str) -> str: - tr_attrs = " class='data-ref'" if is_data_ref else "" - output_row = f" " - for cell in line: - cell_html = self.apply(cell) - output_row += f"<{cell_el}>{cell_html}" - output_row += "\n" - return output_row - - output = "\n" - output += " \n" - output += table_row(meta.headers, False, "th") - output += " \n" - output += " \n" - output += "".join( - table_row(self.outputline_texts(line), line[1].is_data_ref, "td") - for line in lines - ) - output += " \n" - output += "
\n" - return output - - -@dataclass -class JsonFormatter(Formatter): - arch_str: str - - def apply_format(self, chunk: str, f: Format) -> str: - # This method is unused by this formatter - return NotImplemented - - def table(self, meta: TableMetadata, rows: List[Tuple["OutputLine", ...]]) -> str: - def serialize_format(s: str, f: Format) -> Dict[str, Any]: - if f == BasicFormat.NONE: - return {"text": s} - elif isinstance(f, BasicFormat): - return {"text": s, "format": f.name.lower()} - elif isinstance(f, RotationFormat): - attrs = asdict(f) - attrs.update( - { - "text": s, - "format": "rotation", - } - ) - return attrs - else: - static_assert_unreachable(f) - - def serialize(text: Optional[Text]) -> List[Dict[str, Any]]: - if text is None: - return [] - return [serialize_format(s, f) for s, f in text.segments] - - is_threeway = len(meta.headers) == 3 - - output: Dict[str, Any] = {} - output["arch_str"] = self.arch_str - output["header"] = { - name: serialize(h) - for h, name in zip(meta.headers, ("base", "current", "previous")) - } - output["current_score"] = meta.current_score - output["max_score"] = meta.max_score - if meta.previous_score is not None: - output["previous_score"] = meta.previous_score - output_rows: List[Dict[str, Any]] = [] - for row in rows: - output_row: Dict[str, Any] = {} - output_row["key"] = row[0].key2 - output_row["is_data_ref"] = row[1].is_data_ref - iters = [ - ("base", row[0].base, row[0].line1), - ("current", row[1].fmt2, row[1].line2), - ] - if is_threeway: - iters.append(("previous", row[2].fmt2, row[2].line2)) - if all(line is None for _, _, line in iters): - # Skip rows that were only for displaying source code - continue - for column_name, text, line in iters: - column: Dict[str, Any] = {} - column["text"] = serialize(text) - if line: - if line.line_num is not None: - column["line"] = line.line_num - if line.branch_target is not None: - column["branch"] = line.branch_target - if line.source_lines: - column["src"] = line.source_lines - if line.comment is not None: - column["src_comment"] = line.comment - if line.source_line_num is not None: - column["src_line"] = line.source_line_num - if line or column["text"]: - output_row[column_name] = column - output_rows.append(output_row) - output["rows"] = output_rows - return json.dumps(output) - - -def format_fields( - pat: Pattern[str], - out1: Text, - out2: Text, - color1: FormatFunction, - color2: Optional[FormatFunction] = None, -) -> Tuple[Text, Text]: - diffs = [ - of.group() != nf.group() - for (of, nf) in zip(out1.finditer(pat), out2.finditer(pat)) - ] - - it = iter(diffs) - - def maybe_color(color: FormatFunction, s: str) -> Text: - return Text(s, color(s)) if next(it, False) else Text(s) - - out1 = out1.sub(pat, lambda m: maybe_color(color1, m.group())) - it = iter(diffs) - out2 = out2.sub(pat, lambda m: maybe_color(color2 or color1, m.group())) - - return out1, out2 - - -def symbol_formatter(group: str, base_index: int) -> FormatFunction: - symbol_formats: Dict[str, Format] = {} - - def symbol_format(s: str) -> Format: - # TODO: it would be nice to use a unique Format for each symbol, so we could - # add extra UI elements in the HTML version - f = symbol_formats.get(s) - if f is None: - index = len(symbol_formats) + base_index - f = RotationFormat(key=s, index=index, group=group) - symbol_formats[s] = f - return f - - return symbol_format - - -# ==== LOGIC ==== - -ObjdumpCommand = Tuple[List[str], str, Optional[str]] - - -def maybe_eval_int(expr: str) -> Optional[int]: - try: - ret = ast.literal_eval(expr) - if not isinstance(ret, int): - raise Exception("not an integer") - return ret - except Exception: - return None - - -def eval_int(expr: str, emsg: str) -> int: - ret = maybe_eval_int(expr) - if ret is None: - fail(emsg) - return ret - - -def eval_line_num(expr: str) -> Optional[int]: - expr = expr.strip().replace(":", "") - if expr == "": - return None - return int(expr, 16) - - -def run_make(target: str, project: ProjectSettings) -> None: - subprocess.check_call(project.build_command + [target]) - - -def run_make_capture_output( - target: str, project: ProjectSettings -) -> "subprocess.CompletedProcess[bytes]": - return subprocess.run( - project.build_command + [target], - stderr=subprocess.PIPE, - stdout=subprocess.PIPE, - ) - - -def restrict_to_function(dump: str, fn_name: str) -> str: - try: - ind = dump.index("\n", dump.index(f"<{fn_name}>:")) - return dump[ind + 1 :] - except ValueError: - return "" - - -def serialize_data_references(references: List[Tuple[int, int, str]]) -> str: - return "".join( - f"DATAREF {text_offset} {from_offset} {from_section}\n" - for (text_offset, from_offset, from_section) in references - ) - - -def maybe_get_objdump_source_flags(config: Config) -> List[str]: - flags = [] - - if config.show_line_numbers or config.show_source: - flags.append("--line-numbers") - - if config.show_source: - flags.append("--source") - - if not config.source_old_binutils: - flags.append("--source-comment=│ ") - - if config.inlines: - flags.append("--inlines") - - return flags - - -def run_objdump(cmd: ObjdumpCommand, config: Config, project: ProjectSettings) -> str: - flags, target, restrict = cmd - try: - out = subprocess.run( - [project.objdump_executable] + config.arch.arch_flags + flags + [target], - check=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, - ).stdout - except subprocess.CalledProcessError as e: - print(e.stdout) - print(e.stderr) - if "unrecognized option '--source-comment" in e.stderr: - fail("** Try using --source-old-binutils instead of --source **") - raise e - - obj_data: Optional[bytes] = None - if config.diff_obj: - with open(target, "rb") as f: - obj_data = f.read() - - return preprocess_objdump_out(restrict, obj_data, out, config) - - -def preprocess_objdump_out( - restrict: Optional[str], obj_data: Optional[bytes], objdump_out: str, config: Config -) -> str: - """ - Preprocess the output of objdump into a format that `process()` expects. - This format is suitable for saving to disk with `--write-asm`. - - - Optionally filter the output to a single function (`restrict`) - - Otherwise, strip objdump header (7 lines) - - Prepend .data references ("DATAREF" lines) when working with object files - """ - out = objdump_out - - if restrict is not None: - out = restrict_to_function(out, restrict) - else: - for i in range(7): - out = out[out.find("\n") + 1 :] - out = out.rstrip("\n") - - if obj_data: - out = serialize_data_references(parse_elf_data_references(obj_data, config)) + out - - return out - - -def search_map_file( - fn_name: str, project: ProjectSettings, config: Config -) -> Tuple[Optional[str], Optional[int]]: - if not project.mapfile: - fail(f"No map file configured; cannot find function {fn_name}.") - - try: - with open(project.mapfile) as f: - contents = f.read() - except Exception: - fail(f"Failed to open map file {project.mapfile} for reading.") - - if project.map_format == "gnu": - lines = contents.split("\n") - - try: - cur_objfile = None - ram_to_rom = None - cands = [] - last_line = "" - for line in lines: - if line.startswith(" " + config.diff_section): - cur_objfile = line.split()[3] - if "load address" in line: - tokens = last_line.split() + line.split() - ram = int(tokens[1], 0) - rom = int(tokens[5], 0) - ram_to_rom = rom - ram - if line.endswith(" " + fn_name): - ram = int(line.split()[0], 0) - if cur_objfile is not None and ram_to_rom is not None: - cands.append((cur_objfile, ram + ram_to_rom)) - last_line = line - except Exception as e: - traceback.print_exc() - fail(f"Internal error while parsing map file") - - if len(cands) > 1: - fail(f"Found multiple occurrences of function {fn_name} in map file.") - if len(cands) == 1: - return cands[0] - elif project.map_format == "mw": - section_pattern = re.escape(config.diff_section) - find = re.findall( - re.compile( - # ram elf rom - r" \S+ \S+ (\S+) (\S+) . " - + fn_name - # object name - + r"(?: \(entry of " + section_pattern + r"\))? \t(\S+)" - ), - contents, - ) - if len(find) > 1: - fail(f"Found multiple occurrences of function {fn_name} in map file.") - if len(find) == 1: - rom = int(find[0][1], 16) - objname = find[0][2] - # The metrowerks linker map format does not contain the full object path, - # so we must complete it manually. - objfiles = [ - os.path.join(dirpath, f) - for dirpath, _, filenames in os.walk(project.mw_build_dir) - for f in filenames - if f == objname - ] - if len(objfiles) > 1: - all_objects = "\n".join(objfiles) - fail( - f"Found multiple objects of the same name {objname} in {project.mw_build_dir}, " - f"cannot determine which to diff against: \n{all_objects}" - ) - if len(objfiles) == 1: - objfile = objfiles[0] - # TODO Currently the ram-rom conversion only works for diffing ELF - # executables, but it would likely be more convenient to diff DOLs. - # At this time it is recommended to always use -o when running the diff - # script as this mode does not make use of the ram-rom conversion. - return objfile, rom - else: - fail(f"Linker map format {project.map_format} unrecognised.") - return None, None - - -def parse_elf_data_references(data: bytes, config: Config) -> List[Tuple[int, int, str]]: - e_ident = data[:16] - if e_ident[:4] != b"\x7FELF": - return [] - - SHT_SYMTAB = 2 - SHT_REL = 9 - SHT_RELA = 4 - - is_32bit = e_ident[4] == 1 - is_little_endian = e_ident[5] == 1 - str_end = "<" if is_little_endian else ">" - str_off = "I" if is_32bit else "Q" - sym_size = {"B": 1, "H": 2, "I": 4, "Q": 8} - - def read(spec: str, offset: int) -> Tuple[int, ...]: - spec = spec.replace("P", str_off) - size = struct.calcsize(spec) - return struct.unpack(str_end + spec, data[offset : offset + size]) - - ( - e_type, - e_machine, - e_version, - e_entry, - e_phoff, - e_shoff, - e_flags, - e_ehsize, - e_phentsize, - e_phnum, - e_shentsize, - e_shnum, - e_shstrndx, - ) = read("HHIPPPIHHHHHH", 16) - if e_type != 1: # relocatable - return [] - assert e_shoff != 0 - assert e_shnum != 0 # don't support > 0xFF00 sections - assert e_shstrndx != 0 - - @dataclass - class Section: - sh_name: int - sh_type: int - sh_flags: int - sh_addr: int - sh_offset: int - sh_size: int - sh_link: int - sh_info: int - sh_addralign: int - sh_entsize: int - - sections = [ - Section(*read("IIPPPPIIPP", e_shoff + i * e_shentsize)) for i in range(e_shnum) - ] - shstr = sections[e_shstrndx] - sec_name_offs = [shstr.sh_offset + s.sh_name for s in sections] - sec_names = [data[offset : data.index(b"\0", offset)] for offset in sec_name_offs] - - symtab_sections = [i for i in range(e_shnum) if sections[i].sh_type == SHT_SYMTAB] - assert len(symtab_sections) == 1 - symtab = sections[symtab_sections[0]] - - section_name = config.diff_section.encode("utf-8") - text_sections = [i for i in range(e_shnum) if sec_names[i] == section_name and sections[i].sh_size != 0] - if len(text_sections) != 1: - return [] - text_section = text_sections[0] - - ret: List[Tuple[int, int, str]] = [] - for s in sections: - if s.sh_type == SHT_REL or s.sh_type == SHT_RELA: - if s.sh_info == text_section: - # Skip section_name -> section_name references - continue - sec_name = sec_names[s.sh_info].decode("latin1") - if sec_name == ".mwcats.text": - # Skip Metrowerks CATS Utility section - continue - sec_base = sections[s.sh_info].sh_offset - for i in range(0, s.sh_size, s.sh_entsize): - if s.sh_type == SHT_REL: - r_offset, r_info = read("PP", s.sh_offset + i) - else: - r_offset, r_info, r_addend = read("PPP", s.sh_offset + i) - - if is_32bit: - r_sym = r_info >> 8 - r_type = r_info & 0xFF - sym_offset = symtab.sh_offset + symtab.sh_entsize * r_sym - st_name, st_value, st_size, st_info, st_other, st_shndx = read( - "IIIBBH", sym_offset - ) - else: - r_sym = r_info >> 32 - r_type = r_info & 0xFFFFFFFF - sym_offset = symtab.sh_offset + symtab.sh_entsize * r_sym - st_name, st_info, st_other, st_shndx, st_value, st_size = read( - "IBBHQQ", sym_offset - ) - if st_shndx == text_section: - if s.sh_type == SHT_REL: - if e_machine == 8 and r_type == 2: # R_MIPS_32 - (r_addend,) = read("I", sec_base + r_offset) - else: - continue - text_offset = (st_value + r_addend) & 0xFFFFFFFF - ret.append((text_offset, r_offset, sec_name)) - return ret - - -def dump_elf( - start: str, - end: Optional[str], - diff_elf_symbol: str, - config: Config, - project: ProjectSettings, -) -> Tuple[str, ObjdumpCommand, ObjdumpCommand]: - if not project.baseimg or not project.myimg: - fail("Missing myimg/baseimg in config.") - if config.base_shift: - fail("--base-shift not compatible with -e") - - start_addr = eval_int(start, "Start address must be an integer expression.") - - if end is not None: - end_addr = eval_int(end, "End address must be an integer expression.") - else: - end_addr = start_addr + config.max_function_size_bytes - - flags1 = [ - f"--start-address={start_addr}", - f"--stop-address={end_addr}", - ] - - if project.disassemble_all: - disassemble_flag = "-D" - else: - disassemble_flag = "-d" - - flags2 = [ - f"--disassemble={diff_elf_symbol}", - ] - - objdump_flags = [disassemble_flag, "-rz", "-j", config.diff_section] - return ( - project.myimg, - (objdump_flags + flags1, project.baseimg, None), - ( - objdump_flags + flags2 + maybe_get_objdump_source_flags(config), - project.myimg, - None, - ), - ) - - -def dump_objfile( - start: str, end: Optional[str], config: Config, project: ProjectSettings -) -> Tuple[str, ObjdumpCommand, ObjdumpCommand]: - if config.base_shift: - fail("--base-shift not compatible with -o") - if end is not None: - fail("end address not supported together with -o") - if start.startswith("0"): - fail("numerical start address not supported with -o; pass a function name") - - objfile, _ = search_map_file(start, project, config) - if not objfile: - fail("Not able to find .o file for function.") - - if config.make: - run_make(objfile, project) - - if not os.path.isfile(objfile): - fail(f"Not able to find .o file for function: {objfile} is not a file.") - - refobjfile = "expected/" + objfile - if not os.path.isfile(refobjfile): - fail(f'Please ensure an OK .o file exists at "{refobjfile}".') - - if project.disassemble_all: - disassemble_flag = "-D" - else: - disassemble_flag = "-d" - - objdump_flags = [disassemble_flag, "-rz", "-j", config.diff_section] - return ( - objfile, - (objdump_flags, refobjfile, start), - (objdump_flags + maybe_get_objdump_source_flags(config), objfile, start), - ) - - -def dump_binary( - start: str, end: Optional[str], config: Config, project: ProjectSettings -) -> Tuple[str, ObjdumpCommand, ObjdumpCommand]: - if not project.baseimg or not project.myimg: - fail("Missing myimg/baseimg in config.") - if config.make: - run_make(project.myimg, project) - start_addr = maybe_eval_int(start) - if start_addr is None: - _, start_addr = search_map_file(start, project, config) - if start_addr is None: - fail("Not able to find function in map file.") - if end is not None: - end_addr = eval_int(end, "End address must be an integer expression.") - else: - end_addr = start_addr + config.max_function_size_bytes - objdump_flags = ["-Dz", "-bbinary"] + ["-EB" if config.arch.big_endian else "-EL"] - flags1 = [ - f"--start-address={start_addr + config.base_shift}", - f"--stop-address={end_addr + config.base_shift}", - ] - flags2 = [f"--start-address={start_addr}", f"--stop-address={end_addr}"] - return ( - project.myimg, - (objdump_flags + flags1, project.baseimg, None), - (objdump_flags + flags2, project.myimg, None), - ) - -# Example: "ldr r4, [pc, #56] ; (4c )" -ARM32_LOAD_POOL_PATTERN = r"(ldr\s+r([0-9]|1[0-3]),\s+\[pc,.*;\s*)(\([a-fA-F0-9]+.*\))" - - -# The base class is a no-op. -class AsmProcessor: - def __init__(self, config: Config) -> None: - self.config = config - - def process_reloc(self, row: str, prev: str) -> str: - return prev - - def normalize(self, mnemonic: str, row: str) -> str: - """This should be called exactly once for each line.""" - arch = self.config.arch - row = self._normalize_arch_specific(mnemonic, row) - if self.config.ignore_large_imms and mnemonic not in arch.branch_instructions: - row = re.sub(self.config.arch.re_large_imm, "", row) - return row - - def _normalize_arch_specific(self, mnemonic: str, row: str) -> str: - return row - - def post_process(self, lines: List["Line"]) -> None: - return - - -class AsmProcessorMIPS(AsmProcessor): - def process_reloc(self, row: str, prev: str) -> str: - arch = self.config.arch - if "R_MIPS_NONE" in row: - # GNU as emits no-op relocations immediately after real ones when - # assembling with -mabi=64. Return without trying to parse 'imm' as an - # integer. - return prev - before, imm, after = parse_relocated_line(prev) - repl = row.split()[-1] - if imm != "0": - # MIPS uses relocations with addends embedded in the code as immediates. - # If there is an immediate, show it as part of the relocation. Ideally - # we'd show this addend in both %lo/%hi, but annoyingly objdump's output - # doesn't include enough information to pair up %lo's and %hi's... - # TODO: handle unambiguous cases where all addends for a symbol are the - # same, or show "+???". - mnemonic = prev.split()[0] - if ( - mnemonic in arch.instructions_with_address_immediates - and not imm.startswith("0x") - ): - imm = "0x" + imm - repl += "+" + imm if int(imm, 0) > 0 else imm - if "R_MIPS_LO16" in row: - repl = f"%lo({repl})" - elif "R_MIPS_HI16" in row: - # Ideally we'd pair up R_MIPS_LO16 and R_MIPS_HI16 to generate a - # correct addend for each, but objdump doesn't give us the order of - # the relocations, so we can't find the right LO16. :( - repl = f"%hi({repl})" - elif "R_MIPS_26" in row: - # Function calls - pass - elif "R_MIPS_PC16" in row: - # Branch to glabel. This gives confusing output, but there's not much - # we can do here. - pass - elif "R_MIPS_GPREL16" in row: - repl = f"%gp_rel({repl})" - else: - assert False, f"unknown relocation type '{row}' for line '{prev}'" - return before + repl + after - - -class AsmProcessorPPC(AsmProcessor): - def process_reloc(self, row: str, prev: str) -> str: - arch = self.config.arch - assert any( - r in row for r in ["R_PPC_REL24", "R_PPC_ADDR16", "R_PPC_EMB_SDA21"] - ), f"unknown relocation type '{row}' for line '{prev}'" - before, imm, after = parse_relocated_line(prev) - repl = row.split()[-1] - if "R_PPC_REL24" in row: - # function calls - pass - elif "R_PPC_ADDR16_HI" in row: - # absolute hi of addr - repl = f"{repl}@h" - elif "R_PPC_ADDR16_HA" in row: - # adjusted hi of addr - repl = f"{repl}@ha" - elif "R_PPC_ADDR16_LO" in row: - # lo of addr - repl = f"{repl}@l" - elif "R_PPC_ADDR16" in row: - # 16-bit absolute addr - if "+0x7" in repl: - # remove the very large addends as they are an artifact of (label-_SDA(2)_BASE_) - # computations and are unimportant in a diff setting. - if int(repl.split("+")[1], 16) > 0x70000000: - repl = repl.split("+")[0] - elif "R_PPC_EMB_SDA21" in row: - # small data area - pass - return before + repl + after - - -class AsmProcessorARM32(AsmProcessor): - def process_reloc(self, row: str, prev: str) -> str: - arch = self.config.arch - before, imm, after = parse_relocated_line(prev) - repl = row.split()[-1] - return before + repl + after - - def _normalize_arch_specific(self, mnemonic: str, row: str) -> str: - if self.config.ignore_addr_diffs: - row = self._normalize_bl(mnemonic, row) - row = self._normalize_data_pool(row) - return row - - def _normalize_bl(self, mnemonic: str, row: str) -> str: - if mnemonic != "bl": - return row - - row, _ = split_off_address(row) - return row + "" - - def _normalize_data_pool(self, row: str) -> str: - pool_match = re.search(ARM32_LOAD_POOL_PATTERN, row) - return pool_match.group(1) if pool_match else row - - def post_process(self, lines: List["Line"]) -> None: - lines_by_line_number = {} - for line in lines: - lines_by_line_number[line.line_num] = line - for line in lines: - if line.data_pool_addr is None: - continue - - # Add data symbol and its address to the line. - line_original = lines_by_line_number[line.data_pool_addr].original - value = line_original.split()[1] - addr = "{:x}".format(line.data_pool_addr) - line.original = line.normalized_original + f"={value} ({addr})" - - -class AsmProcessorAArch64(AsmProcessor): - def __init__(self, config: Config) -> None: - super().__init__(config) - self._adrp_pair_registers: Set[str] = set() - - def _normalize_arch_specific(self, mnemonic: str, row: str) -> str: - if self.config.ignore_addr_diffs: - row = self._normalize_adrp_differences(mnemonic, row) - row = self._normalize_bl(mnemonic, row) - return row - - def _normalize_bl(self, mnemonic: str, row: str) -> str: - if mnemonic != "bl": - return row - - row, _ = split_off_address(row) - return row + "" - - def _normalize_adrp_differences(self, mnemonic: str, row: str) -> str: - """Identifies ADRP + LDR/ADD pairs that are used to access the GOT and - suppresses any immediate differences. - - Whenever an ADRP is seen, the destination register is added to the set of registers - that are part of an ADRP + LDR/ADD pair. Registers are removed from the set as soon - as they are used for an LDR or ADD instruction which completes the pair. - - This method is somewhat crude but should manage to detect most such pairs. - """ - row_parts = row.split("\t", 1) - if mnemonic == "adrp": - self._adrp_pair_registers.add(row_parts[1].strip().split(",")[0]) - row, _ = split_off_address(row) - return row + "" - elif mnemonic == "ldr": - for reg in self._adrp_pair_registers: - # ldr xxx, [reg] - # ldr xxx, [reg, ] - if f", [{reg}" in row_parts[1]: - self._adrp_pair_registers.remove(reg) - return normalize_imms(row, AARCH64_SETTINGS) - elif mnemonic == "add": - for reg in self._adrp_pair_registers: - # add reg, reg, - if row_parts[1].startswith(f"{reg}, {reg}, "): - self._adrp_pair_registers.remove(reg) - return normalize_imms(row, AARCH64_SETTINGS) - - return row - - -@dataclass -class ArchSettings: - name: str - re_int: Pattern[str] - re_comment: Pattern[str] - re_reg: Pattern[str] - re_sprel: Pattern[str] - re_large_imm: Pattern[str] - re_imm: Pattern[str] - re_reloc: Pattern[str] - branch_instructions: Set[str] - instructions_with_address_immediates: Set[str] - forbidden: Set[str] = field(default_factory=lambda: set(string.ascii_letters + "_")) - arch_flags: List[str] = field(default_factory=list) - branch_likely_instructions: Set[str] = field(default_factory=set) - proc: Type[AsmProcessor] = AsmProcessor - big_endian: Optional[bool] = True - delay_slot_instructions: Set[str] = field(default_factory=set) - -MIPS_BRANCH_LIKELY_INSTRUCTIONS = { - "beql", - "bnel", - "beqzl", - "bnezl", - "bgezl", - "bgtzl", - "blezl", - "bltzl", - "bc1tl", - "bc1fl", -} -MIPS_BRANCH_INSTRUCTIONS = MIPS_BRANCH_LIKELY_INSTRUCTIONS.union( - { - "b", - "beq", - "bne", - "beqz", - "bnez", - "bgez", - "bgtz", - "blez", - "bltz", - "bc1t", - "bc1f", - } -) - -ARM32_PREFIXES = {"b", "bl"} -ARM32_CONDS = { - "", - "eq", - "ne", - "cs", - "cc", - "mi", - "pl", - "vs", - "vc", - "hi", - "ls", - "ge", - "lt", - "gt", - "le", - "al", -} -ARM32_SUFFIXES = {"", ".n", ".w"} -ARM32_BRANCH_INSTRUCTIONS = { - f"{prefix}{cond}{suffix}" - for prefix in ARM32_PREFIXES - for cond in ARM32_CONDS - for suffix in ARM32_SUFFIXES -} - -AARCH64_BRANCH_INSTRUCTIONS = { - "b", - "b.eq", - "b.ne", - "b.cs", - "b.hs", - "b.cc", - "b.lo", - "b.mi", - "b.pl", - "b.vs", - "b.vc", - "b.hi", - "b.ls", - "b.ge", - "b.lt", - "b.gt", - "b.le", - "cbz", - "cbnz", - "tbz", - "tbnz", -} - -PPC_BRANCH_INSTRUCTIONS = { - "b", - "beq", - "beq+", - "beq-", - "bne", - "bne+", - "bne-", - "blt", - "blt+", - "blt-", - "ble", - "ble+", - "ble-", - "bdnz", - "bdnz+", - "bdnz-", - "bge", - "bge+", - "bge-", - "bgt", - "bgt+", - "bgt-", -} - -MIPS_SETTINGS = ArchSettings( - name="mips", - re_int=re.compile(r"[0-9]+"), - re_comment=re.compile(r"<.*?>"), - re_reg=re.compile( - r"\$?\b(a[0-7]|t[0-9]|s[0-8]|at|v[01]|f[12]?[0-9]|f3[01]|kt?[01]|fp|ra|zero)\b" - ), - re_sprel=re.compile(r"(?<=,)([0-9]+|0x[0-9a-f]+)\(sp\)"), - re_large_imm=re.compile(r"-?[1-9][0-9]{2,}|-?0x[0-9a-f]{3,}"), - re_imm=re.compile(r"(\b|-)([0-9]+|0x[0-9a-fA-F]+)\b(?!\(sp)|%(lo|hi)\([^)]*\)"), - re_reloc=re.compile(r"R_MIPS_"), - arch_flags=["-m", "mips:4300"], - branch_likely_instructions=MIPS_BRANCH_LIKELY_INSTRUCTIONS, - branch_instructions=MIPS_BRANCH_INSTRUCTIONS, - instructions_with_address_immediates=MIPS_BRANCH_INSTRUCTIONS.union({"jal", "j"}), - delay_slot_instructions=MIPS_BRANCH_INSTRUCTIONS.union({"j", "jal", "jr", "jalr"}), - proc=AsmProcessorMIPS, -) - -MIPSEL_SETTINGS = replace(MIPS_SETTINGS, name="mipsel", big_endian=False) - -ARM32_SETTINGS = ArchSettings( - name="arm32", - re_int=re.compile(r"[0-9]+"), - re_comment=re.compile(r"(<.*?>|//.*$)"), - # Includes: - # - General purpose registers: r0..13 - # - Frame pointer registers: lr (r14), pc (r15) - # - VFP/NEON registers: s0..31, d0..31, q0..15, fpscr, fpexc, fpsid - # SP should not be in this list. - re_reg=re.compile( - r"\$?\b([rq][0-9]|[rq]1[0-5]|pc|lr|[ds][12]?[0-9]|[ds]3[01]|fp(scr|exc|sid))\b" - ), - re_sprel=re.compile(r"sp, #-?(0x[0-9a-fA-F]+|[0-9]+)\b"), - re_large_imm=re.compile(r"-?[1-9][0-9]{2,}|-?0x[0-9a-f]{3,}"), - re_imm=re.compile(r"(?|//.*$)"), - # GPRs and FP registers: X0-X30, W0-W30, [BHSDVQ]0..31 - # (FP registers may be followed by data width and number of elements, e.g. V0.4S) - # The zero registers and SP should not be in this list. - re_reg=re.compile(r"\$?\b([bhsdvq]([12]?[0-9]|3[01])(\.\d\d?[bhsdvq])?|[xw][12]?[0-9]|[xw]30)\b"), - re_sprel=re.compile(r"sp, #-?(0x[0-9a-fA-F]+|[0-9]+)\b"), - re_large_imm=re.compile(r"-?[1-9][0-9]{2,}|-?0x[0-9a-f]{3,}"), - re_imm=re.compile(r"(?|//.*$)"), - re_reg=re.compile(r"\$?\b([rf][0-9]+)\b"), - re_sprel=re.compile(r"(?<=,)(-?[0-9]+|-?0x[0-9a-f]+)\(r1\)"), - re_large_imm=re.compile(r"-?[1-9][0-9]{2,}|-?0x[0-9a-f]{3,}"), - re_imm=re.compile(r"(\b|-)([0-9]+|0x[0-9a-fA-F]+)\b(?!\(r1)|[^@]*@(ha|h|lo)"), - re_reloc=re.compile(r"R_PPC_"), - branch_instructions=PPC_BRANCH_INSTRUCTIONS, - instructions_with_address_immediates=PPC_BRANCH_INSTRUCTIONS.union({"bl"}), - proc=AsmProcessorPPC, -) - -ARCH_SETTINGS = [ - MIPS_SETTINGS, - MIPSEL_SETTINGS, - ARM32_SETTINGS, - ARMEL_SETTINGS, - AARCH64_SETTINGS, - PPC_SETTINGS, -] - - -def hexify_int(row: str, pat: Match[str], arch: ArchSettings) -> str: - full = pat.group(0) - if len(full) <= 1: - # leave one-digit ints alone - return full - start, end = pat.span() - if start and row[start - 1] in arch.forbidden: - return full - if end < len(row) and row[end] in arch.forbidden: - return full - return hex(int(full)) - - -def parse_relocated_line(line: str) -> Tuple[str, str, str]: - for c in ",\t ": - if c in line: - ind2 = line.rindex(c) - break - else: - raise Exception(f"failed to parse relocated line: {line}") - before = line[: ind2 + 1] - after = line[ind2 + 1 :] - ind2 = after.find("(") - if ind2 == -1: - imm, after = after, "" - else: - imm, after = after[:ind2], after[ind2:] - if imm == "0x0": - imm = "0" - return before, imm, after - - -def pad_mnemonic(line: str) -> str: - if "\t" not in line: - return line - mn, args = line.split("\t", 1) - return f"{mn:<7s} {args}" - - -@dataclass -class Line: - mnemonic: str - diff_row: str - original: str - normalized_original: str - scorable_line: str - line_num: Optional[int] = None - branch_target: Optional[int] = None - data_pool_addr: Optional[int] = None - source_filename: Optional[str] = None - source_line_num: Optional[int] = None - source_lines: List[str] = field(default_factory=list) - comment: Optional[str] = None - - -def process(dump: str, config: Config) -> List[Line]: - arch = config.arch - processor = arch.proc(config) - skip_next = False - source_lines = [] - source_filename = None - source_line_num = None - - i = 0 - num_instr = 0 - data_refs: Dict[int, Dict[str, List[int]]] = defaultdict(lambda: defaultdict(list)) - output: List[Line] = [] - stop_after_delay_slot = False - lines = dump.split("\n") - while i < len(lines): - row = lines[i] - i += 1 - - if not row: - continue - - if re.match(r"^[0-9a-f]+ <.*>:$", row): - continue - - if row.startswith("DATAREF"): - parts = row.split(" ", 3) - text_offset = int(parts[1]) - from_offset = int(parts[2]) - from_section = parts[3] - data_refs[text_offset][from_section].append(from_offset) - continue - - if config.diff_obj and num_instr >= config.max_function_size_lines: - output.append( - Line( - mnemonic="...", - diff_row="...", - original="...", - normalized_original="...", - scorable_line="...", - ) - ) - break - - if not re.match(r"^\s+[0-9a-f]+:\s+", row): - # This regex is conservative, and assumes the file path does not contain "weird" - # characters like colons, tabs, or angle brackets. - if re.match( - r"^[^ \t<>:][^\t<>:]*:[0-9]+( \(discriminator [0-9]+\))?$", row - ): - source_filename, _, tail = row.rpartition(":") - source_line_num = int(tail.partition(" ")[0]) - source_lines.append(row) - continue - - # If the instructions loads a data pool symbol, extract the address of - # the symbol. - data_pool_addr = None - pool_match = re.search(ARM32_LOAD_POOL_PATTERN, row) - if pool_match: - offset = pool_match.group(3).split(" ")[0][1:] - data_pool_addr = int(offset, 16) - - m_comment = re.search(arch.re_comment, row) - comment = m_comment[0] if m_comment else None - row = re.sub(arch.re_comment, "", row) - line_num_str = row.split(":")[0] - row = row.rstrip() - tabs = row.split("\t") - row = "\t".join(tabs[2:]) - line_num = eval_line_num(line_num_str.strip()) - - if line_num in data_refs: - refs = data_refs[line_num] - ref_str = "; ".join( - section_name + "+" + ",".join(hex(off) for off in offs) - for section_name, offs in refs.items() - ) - output.append( - Line( - mnemonic="", - diff_row="", - original=ref_str, - normalized_original=ref_str, - scorable_line="", - ) - ) - - if "\t" in row: - row_parts = row.split("\t", 1) - else: - # powerpc-eabi-objdump doesn't use tabs - row_parts = [part.lstrip() for part in row.split(" ", 1)] - mnemonic = row_parts[0].strip() - - if mnemonic not in arch.instructions_with_address_immediates: - row = re.sub(arch.re_int, lambda m: hexify_int(row, m, arch), row) - - # Let 'original' be 'row' with relocations applied, while we continue - # transforming 'row' into a coarser version that ignores registers and - # immediates. - original = row - - while i < len(lines): - reloc_row = lines[i] - if re.search(arch.re_reloc, reloc_row): - original = processor.process_reloc(reloc_row, original) - else: - break - i += 1 - - normalized_original = processor.normalize(mnemonic, original) - - scorable_line = normalized_original - if not config.score_stack_differences: - scorable_line = re.sub(arch.re_sprel, "addr(sp)", scorable_line) - if mnemonic in arch.branch_instructions: - # Replace the final argument with "" - scorable_line = re.sub(r"[^, \t]+$", "", scorable_line) - - if skip_next: - skip_next = False - row = "" - mnemonic = "" - scorable_line = "" - if mnemonic in arch.branch_likely_instructions: - skip_next = True - - row = re.sub(arch.re_reg, "", row) - row = re.sub(arch.re_sprel, "addr(sp)", row) - row_with_imm = row - if mnemonic in arch.instructions_with_address_immediates: - row = row.strip() - row, _ = split_off_address(row) - row += "" - else: - row = normalize_imms(row, arch) - - branch_target = None - if mnemonic in arch.branch_instructions: - branch_target = int(row_parts[1].strip().split(",")[-1], 16) - if mnemonic in arch.branch_likely_instructions: - branch_target -= 4 - - output.append( - Line( - mnemonic=mnemonic, - diff_row=row, - original=original, - normalized_original=normalized_original, - scorable_line=scorable_line, - line_num=line_num, - branch_target=branch_target, - data_pool_addr=data_pool_addr, - source_filename=source_filename, - source_line_num=source_line_num, - source_lines=source_lines, - comment=comment, - ) - ) - num_instr += 1 - source_lines = [] - - if config.stop_jrra and mnemonic == "jr" and row_parts[1].strip() == "ra": - stop_after_delay_slot = True - elif stop_after_delay_slot: - break - - processor.post_process(output) - return output - - -def normalize_imms(row: str, arch: ArchSettings) -> str: - return re.sub(arch.re_imm, "", row) - - -def normalize_stack(row: str, arch: ArchSettings) -> str: - return re.sub(arch.re_sprel, "addr(sp)", row) - - -def imm_matches_everything(row: str, arch: ArchSettings) -> bool: - # (this should probably be arch-specific) - return "(." in row - - -def split_off_address(line: str) -> Tuple[str, str]: - """Split e.g. 'beqz $r0,1f0' into 'beqz $r0,' and '1f0'.""" - parts = line.split(",") - if len(parts) < 2: - parts = line.split(None, 1) - off = len(line) - len(parts[-1]) - return line[:off], line[off:] - - -def diff_sequences_difflib( - seq1: List[str], seq2: List[str] -) -> List[Tuple[str, int, int, int, int]]: - differ = difflib.SequenceMatcher(a=seq1, b=seq2, autojunk=False) - return differ.get_opcodes() - - -def diff_sequences( - seq1: List[str], seq2: List[str], algorithm: str -) -> List[Tuple[str, int, int, int, int]]: - if ( - algorithm != "levenshtein" - or len(seq1) * len(seq2) > 4 * 10 ** 8 - or len(seq1) + len(seq2) >= 0x110000 - ): - return diff_sequences_difflib(seq1, seq2) - - # The Levenshtein library assumes that we compare strings, not lists. Convert. - # (Per the check above we know we have fewer than 0x110000 unique elements, so chr() works.) - remapping: Dict[str, str] = {} - - def remap(seq: List[str]) -> str: - seq = seq[:] - for i in range(len(seq)): - val = remapping.get(seq[i]) - if val is None: - val = chr(len(remapping)) - remapping[seq[i]] = val - seq[i] = val - return "".join(seq) - - rem1 = remap(seq1) - rem2 = remap(seq2) - import Levenshtein - - ret: List[Tuple[str, int, int, int, int]] = Levenshtein.opcodes(rem1, rem2) - return ret - - -def diff_lines( - lines1: List[Line], - lines2: List[Line], - algorithm: str, -) -> List[Tuple[Optional[Line], Optional[Line]]]: - ret = [] - for (tag, i1, i2, j1, j2) in diff_sequences( - [line.mnemonic for line in lines1], - [line.mnemonic for line in lines2], - algorithm, - ): - for line1, line2 in itertools.zip_longest(lines1[i1:i2], lines2[j1:j2]): - if tag == "replace": - if line1 is None: - tag = "insert" - elif line2 is None: - tag = "delete" - elif tag == "insert": - assert line1 is None - elif tag == "delete": - assert line2 is None - ret.append((line1, line2)) - - return ret - - -def score_diff_lines( - lines: List[Tuple[Optional[Line], Optional[Line]]], config: Config -) -> int: - # This logic is copied from `scorer.py` from the decomp permuter project - # https://github.com/simonlindholm/decomp-permuter/blob/main/src/scorer.py - score = 0 - deletions = [] - insertions = [] - - def lo_hi_match(old: str, new: str) -> bool: - # TODO: Make this arch-independent, like `imm_matches_everything()` - old_lo = old.find("%lo") - old_hi = old.find("%hi") - new_lo = new.find("%lo") - new_hi = new.find("%hi") - - if old_lo != -1 and new_lo != -1: - old_idx = old_lo - new_idx = new_lo - elif old_hi != -1 and new_hi != -1: - old_idx = old_hi - new_idx = new_hi - else: - return False - - if old[:old_idx] != new[:new_idx]: - return False - - old_inner = old[old_idx + 4 : -1] - new_inner = new[new_idx + 4 : -1] - return old_inner.startswith(".") or new_inner.startswith(".") - - def diff_sameline(old: str, new: str) -> None: - nonlocal score - if old == new: - return - - if lo_hi_match(old, new): - return - - ignore_last_field = False - if config.score_stack_differences: - oldsp = re.search(config.arch.re_sprel, old) - newsp = re.search(config.arch.re_sprel, new) - if oldsp and newsp: - oldrel = int(oldsp.group(1) or "0", 0) - newrel = int(newsp.group(1) or "0", 0) - score += abs(oldrel - newrel) * config.penalty_stackdiff - ignore_last_field = True - - # Probably regalloc difference, or signed vs unsigned - - # Compare each field in order - newfields, oldfields = new.split(","), old.split(",") - if ignore_last_field: - newfields = newfields[:-1] - oldfields = oldfields[:-1] - for nf, of in zip(newfields, oldfields): - if nf != of: - score += config.penalty_regalloc - # Penalize any extra fields - score += abs(len(newfields) - len(oldfields)) * config.penalty_regalloc - - def diff_insert(line: str) -> None: - # Reordering or totally different codegen. - # Defer this until later when we can tell. - insertions.append(line) - - def diff_delete(line: str) -> None: - deletions.append(line) - - # Find the end of the last long streak of matching mnemonics, if it looks - # like the objdump output was truncated. This is used to skip scoring - # misaligned lines at the end of the diff. - last_mismatch = -1 - max_index = None - lines_were_truncated = False - for index, (line1, line2) in enumerate(lines): - if (line1 and line1.original == "...") or (line2 and line2.original == "..."): - lines_were_truncated = True - if line1 and line2 and line1.mnemonic == line2.mnemonic: - if index - last_mismatch >= 50: - max_index = index - else: - last_mismatch = index - if not lines_were_truncated: - max_index = None - - for index, (line1, line2) in enumerate(lines): - if max_index is not None and index > max_index: - break - if line1 and line2 and line1.mnemonic == line2.mnemonic: - diff_sameline(line1.scorable_line, line2.scorable_line) - else: - if line1: - diff_delete(line1.scorable_line) - if line2: - diff_insert(line2.scorable_line) - - insertions_co = Counter(insertions) - deletions_co = Counter(deletions) - for item in insertions_co + deletions_co: - ins = insertions_co[item] - dels = deletions_co[item] - common = min(ins, dels) - score += ( - (ins - common) * config.penalty_insertion - + (dels - common) * config.penalty_deletion - + config.penalty_reordering * common - ) - - return score - - -@dataclass(frozen=True) -class OutputLine: - base: Optional[Text] = field(compare=False) - fmt2: Text = field(compare=False) - key2: Optional[str] - boring: bool = field(compare=False) - is_data_ref: bool = field(compare=False) - line1: Optional[Line] = field(compare=False) - line2: Optional[Line] = field(compare=False) - - -@dataclass(frozen=True) -class Diff: - lines: List[OutputLine] - score: int - max_score: int - - -def trim_nops(lines: List[Line], arch: ArchSettings) -> List[Line]: - lines = lines[:] - while lines and lines[-1].mnemonic == "nop" and (len(lines) == 1 or lines[-2].mnemonic not in arch.delay_slot_instructions): - lines.pop() - return lines - -def do_diff(lines1: List[Line], lines2: List[Line], config: Config) -> Diff: - if config.show_source: - import cxxfilt - arch = config.arch - fmt = config.formatter - output: List[OutputLine] = [] - - sc1 = symbol_formatter("base-reg", 0) - sc2 = symbol_formatter("my-reg", 0) - sc3 = symbol_formatter("base-stack", 4) - sc4 = symbol_formatter("my-stack", 4) - sc5 = symbol_formatter("base-branch", 0) - sc6 = symbol_formatter("my-branch", 0) - bts1: Set[int] = set() - bts2: Set[int] = set() - - if config.show_branches: - for (lines, btset, sc) in [ - (lines1, bts1, sc5), - (lines2, bts2, sc6), - ]: - for line in lines: - bt = line.branch_target - if bt is not None: - btset.add(bt) - sc(str(bt)) - - lines1 = trim_nops(lines1, arch) - lines2 = trim_nops(lines2, arch) - - diffed_lines = diff_lines(lines1, lines2, config.algorithm) - score = score_diff_lines(diffed_lines, config) - max_score = len(lines1) * config.penalty_deletion - - line_num_base = -1 - line_num_offset = 0 - line_num_2to1 = {} - for (line1, line2) in diffed_lines: - if line1 is not None and line1.line_num is not None: - line_num_base = line1.line_num - line_num_offset = 0 - else: - line_num_offset += 1 - if line2 is not None and line2.line_num is not None: - line_num_2to1[line2.line_num] = (line_num_base, line_num_offset) - - for (line1, line2) in diffed_lines: - line_color1 = line_color2 = sym_color = BasicFormat.NONE - line_prefix = " " - is_data_ref = False - out1 = Text() if not line1 else Text(pad_mnemonic(line1.original)) - out2 = Text() if not line2 else Text(pad_mnemonic(line2.original)) - if line1 and line2 and line1.diff_row == line2.diff_row: - if line1.diff_row == "": - if line1.normalized_original != line2.normalized_original: - line_prefix = "i" - sym_color = BasicFormat.DIFF_CHANGE - out1 = out1.reformat(sym_color) - out2 = out2.reformat(sym_color) - is_data_ref = True - elif ( - line1.normalized_original == line2.normalized_original - and line2.branch_target is None - ): - # Fast path: no coloring needed. We don't include branch instructions - # in this case because we need to check that their targets line up in - # the diff, and don't just happen to have the are the same address - # by accident. - pass - elif line1.diff_row == "": - # Don't draw attention to differing branch-likely delay slots: they - # typically mirror the branch destination - 1 so the real difference - # is elsewhere. Still, do mark them as different to avoid confusion. - # No need to consider branches because delay slots can't branch. - out1 = out1.reformat(BasicFormat.DELAY_SLOT) - out2 = out2.reformat(BasicFormat.DELAY_SLOT) - else: - mnemonic = line1.original.split()[0] - branchless1, address1 = out1.plain(), "" - branchless2, address2 = out2.plain(), "" - if mnemonic in arch.instructions_with_address_immediates: - branchless1, address1 = split_off_address(branchless1) - branchless2, address2 = split_off_address(branchless2) - - out1 = Text(branchless1) - out2 = Text(branchless2) - out1, out2 = format_fields( - arch.re_imm, out1, out2, lambda _: BasicFormat.IMMEDIATE - ) - - if line2.branch_target is not None: - target = line2.branch_target - line2_target = line_num_2to1.get(line2.branch_target) - if line2_target is None: - # If the target is outside the disassembly, extrapolate. - # This only matters near the bottom. - assert line2.line_num is not None - line2_line = line_num_2to1[line2.line_num] - line2_target = (line2_line[0] + (target - line2.line_num), 0) - - # Set the key for three-way diffing to a normalized version. - norm2, norm_branch2 = split_off_address(line2.normalized_original) - if norm_branch2 != "": - line2.normalized_original = norm2 + str(line2_target) - same_target = line2_target == (line1.branch_target, 0) - else: - # Do a naive comparison for non-branches (e.g. function calls). - same_target = address1 == address2 - - if normalize_imms(branchless1, arch) == normalize_imms( - branchless2, arch - ): - if imm_matches_everything(branchless2, arch): - # ignore differences due to %lo(.rodata + ...) vs symbol - out1 = out1.reformat(BasicFormat.NONE) - out2 = out2.reformat(BasicFormat.NONE) - elif line2.branch_target is not None and same_target: - # same-target branch, don't color - pass - else: - # must have an imm difference (or else we would have hit the - # fast path) - sym_color = BasicFormat.IMMEDIATE - line_prefix = "i" - else: - out1, out2 = format_fields(arch.re_sprel, out1, out2, sc3, sc4) - if normalize_stack(branchless1, arch) == normalize_stack( - branchless2, arch - ): - # only stack differences (luckily stack and imm - # differences can't be combined in MIPS, so we - # don't have to think about that case) - sym_color = BasicFormat.STACK - line_prefix = "s" - else: - # reg differences and maybe imm as well - out1, out2 = format_fields(arch.re_reg, out1, out2, sc1, sc2) - line_color1 = line_color2 = sym_color = BasicFormat.REGISTER - line_prefix = "r" - - if same_target: - address_imm_fmt = BasicFormat.NONE - else: - address_imm_fmt = BasicFormat.IMMEDIATE - out1 += Text(address1, address_imm_fmt) - out2 += Text(address2, address_imm_fmt) - elif line1 and line2: - line_prefix = "|" - line_color1 = line_color2 = sym_color = BasicFormat.DIFF_CHANGE - out1 = out1.reformat(line_color1) - out2 = out2.reformat(line_color2) - elif line1: - line_prefix = "<" - line_color1 = sym_color = BasicFormat.DIFF_REMOVE - out1 = out1.reformat(line_color1) - out2 = Text() - elif line2: - line_prefix = ">" - line_color2 = sym_color = BasicFormat.DIFF_ADD - out1 = Text() - out2 = out2.reformat(line_color2) - - if config.show_source and line2 and line2.comment: - out2 += f" {line2.comment}" - - def format_part( - out: Text, - line: Optional[Line], - line_color: Format, - btset: Set[int], - sc: FormatFunction, - ) -> Optional[Text]: - if line is None: - return None - if line.line_num is None: - return out - in_arrow = Text(" ") - out_arrow = Text() - if config.show_branches: - if line.line_num in btset: - in_arrow = Text("~>", sc(str(line.line_num))) - if line.branch_target is not None: - out_arrow = " " + Text("~>", sc(str(line.branch_target))) - formatted_line_num = Text(hex(line.line_num)[2:] + ":", line_color) - return formatted_line_num + " " + in_arrow + " " + out + out_arrow - - part1 = format_part(out1, line1, line_color1, bts1, sc5) - part2 = format_part(out2, line2, line_color2, bts2, sc6) - - if config.show_source and line2: - for source_line in line2.source_lines: - line_format = BasicFormat.SOURCE_OTHER - if config.source_old_binutils: - if source_line and re.fullmatch(".*\.c(?:pp)?:\d+", source_line): - line_format = BasicFormat.SOURCE_FILENAME - elif source_line and source_line.endswith("():"): - line_format = BasicFormat.SOURCE_FUNCTION - try: - source_line = cxxfilt.demangle( - source_line[:-3], external_only=False - ) - except: - pass - else: - # File names and function names - if source_line and source_line[0] != "│": - line_format = BasicFormat.SOURCE_FILENAME - # Function names - if source_line.endswith("():"): - line_format = BasicFormat.SOURCE_FUNCTION - try: - source_line = cxxfilt.demangle( - source_line[:-3], external_only=False - ) - except: - pass - padding = " " * 7 if config.show_line_numbers else " " * 2 - output.append( - OutputLine( - base=None, - fmt2=padding + Text(source_line, line_format), - key2=source_line, - boring=True, - is_data_ref=False, - line1=None, - line2=None, - ) - ) - - key2 = line2.normalized_original if line2 else None - boring = False - if line_prefix == " ": - boring = True - elif config.compress and config.compress.same_instr and line_prefix in "irs": - boring = True - - if config.show_line_numbers: - if line2 and line2.source_line_num is not None: - num_color = ( - BasicFormat.SOURCE_LINE_NUM - if sym_color == BasicFormat.NONE - else sym_color - ) - num2 = Text(f"{line2.source_line_num:5}", num_color) - else: - num2 = Text(" " * 5) - else: - num2 = Text() - - fmt2 = Text(line_prefix, sym_color) + num2 + " " + (part2 or Text()) - - output.append( - OutputLine( - base=part1, - fmt2=fmt2, - key2=key2, - boring=boring, - is_data_ref=is_data_ref, - line1=line1, - line2=line2, - ) - ) - - output = output[config.skip_lines :] - return Diff(lines=output, score=score, max_score=max_score) - - -def chunk_diff_lines( - diff: List[OutputLine], -) -> List[Union[List[OutputLine], OutputLine]]: - """Chunk a diff into an alternating list like A B A B ... A, where: - * A is a List[OutputLine] of insertions, - * B is a single non-insertion OutputLine, with .base != None.""" - cur_right: List[OutputLine] = [] - chunks: List[Union[List[OutputLine], OutputLine]] = [] - for output_line in diff: - if output_line.base is not None: - chunks.append(cur_right) - chunks.append(output_line) - cur_right = [] - else: - cur_right.append(output_line) - chunks.append(cur_right) - return chunks - - -def compress_matching( - li: List[Tuple[OutputLine, ...]], context: int -) -> List[Tuple[OutputLine, ...]]: - ret: List[Tuple[OutputLine, ...]] = [] - matching_streak: List[Tuple[OutputLine, ...]] = [] - context = max(context, 0) - - def flush_matching() -> None: - if len(matching_streak) <= 2 * context + 1: - ret.extend(matching_streak) - else: - ret.extend(matching_streak[:context]) - skipped = len(matching_streak) - 2 * context - filler = OutputLine( - base=Text(f"<{skipped} lines>", BasicFormat.SOURCE_OTHER), - fmt2=Text(), - key2=None, - boring=False, - is_data_ref=False, - line1=None, - line2=None, - ) - columns = len(matching_streak[0]) - ret.append(tuple([filler] * columns)) - if context > 0: - ret.extend(matching_streak[-context:]) - matching_streak.clear() - - for line in li: - if line[0].boring: - matching_streak.append(line) - else: - flush_matching() - ret.append(line) - - flush_matching() - return ret - - -def align_diffs( - old_diff: Diff, new_diff: Diff, config: Config -) -> Tuple[TableMetadata, List[Tuple[OutputLine, ...]]]: - meta: TableMetadata - diff_lines: List[Tuple[OutputLine, ...]] - padding = " " * 7 if config.show_line_numbers else " " * 2 - - if config.threeway: - meta = TableMetadata( - headers=( - Text("TARGET"), - Text(f"{padding}CURRENT ({new_diff.score})"), - Text(f"{padding}PREVIOUS ({old_diff.score})"), - ), - current_score=new_diff.score, - max_score=new_diff.max_score, - previous_score=old_diff.score, - ) - old_chunks = chunk_diff_lines(old_diff.lines) - new_chunks = chunk_diff_lines(new_diff.lines) - diff_lines = [] - empty = OutputLine(Text(), Text(), None, True, False, None, None) - assert len(old_chunks) == len(new_chunks), "same target" - for old_chunk, new_chunk in zip(old_chunks, new_chunks): - if isinstance(old_chunk, list): - assert isinstance(new_chunk, list) - if not old_chunk and not new_chunk: - # Most of the time lines sync up without insertions/deletions, - # and there's no interdiffing to be done. - continue - differ = difflib.SequenceMatcher( - a=old_chunk, b=new_chunk, autojunk=False - ) - for (tag, i1, i2, j1, j2) in differ.get_opcodes(): - if tag in ["equal", "replace"]: - for i, j in zip(range(i1, i2), range(j1, j2)): - diff_lines.append((empty, new_chunk[j], old_chunk[i])) - if tag in ["insert", "replace"]: - for j in range(j1 + i2 - i1, j2): - diff_lines.append((empty, new_chunk[j], empty)) - if tag in ["delete", "replace"]: - for i in range(i1 + j2 - j1, i2): - diff_lines.append((empty, empty, old_chunk[i])) - else: - assert isinstance(new_chunk, OutputLine) - # old_chunk.base and new_chunk.base have the same text since - # both diffs are based on the same target, but they might - # differ in color. Use the new version. - diff_lines.append((new_chunk, new_chunk, old_chunk)) - diff_lines = [ - (base, new, old if old != new else empty) for base, new, old in diff_lines - ] - else: - meta = TableMetadata( - headers=( - Text("TARGET"), - Text(f"{padding}CURRENT ({new_diff.score})"), - ), - current_score=new_diff.score, - max_score=new_diff.max_score, - previous_score=None, - ) - diff_lines = [(line, line) for line in new_diff.lines] - if config.compress: - diff_lines = compress_matching(diff_lines, config.compress.context) - return meta, diff_lines - - -def debounced_fs_watch( - targets: List[str], - outq: "queue.Queue[Optional[float]]", - config: Config, - project: ProjectSettings, -) -> None: - import watchdog.events - import watchdog.observers - - class WatchEventHandler(watchdog.events.FileSystemEventHandler): - def __init__( - self, queue: "queue.Queue[float]", file_targets: List[str] - ) -> None: - self.queue = queue - self.file_targets = file_targets - - def on_modified(self, ev: object) -> None: - if isinstance(ev, watchdog.events.FileModifiedEvent): - self.changed(ev.src_path) - - def on_moved(self, ev: object) -> None: - if isinstance(ev, watchdog.events.FileMovedEvent): - self.changed(ev.dest_path) - - def should_notify(self, path: str) -> bool: - for target in self.file_targets: - if os.path.normpath(path) == target: - return True - if config.make and any( - path.endswith(suffix) for suffix in project.source_extensions - ): - return True - return False - - def changed(self, path: str) -> None: - if self.should_notify(path): - self.queue.put(time.time()) - - def debounce_thread() -> NoReturn: - listenq: "queue.Queue[float]" = queue.Queue() - file_targets: List[str] = [] - event_handler = WatchEventHandler(listenq, file_targets) - observer = watchdog.observers.Observer() - observed = set() - for target in targets: - if os.path.isdir(target): - observer.schedule(event_handler, target, recursive=True) - else: - file_targets.append(os.path.normpath(target)) - target = os.path.dirname(target) or "." - if target not in observed: - observed.add(target) - observer.schedule(event_handler, target) - observer.start() - while True: - t = listenq.get() - more = True - while more: - delay = t + DEBOUNCE_DELAY - time.time() - if delay > 0: - time.sleep(delay) - # consume entire queue - more = False - try: - while True: - t = listenq.get(block=False) - more = True - except queue.Empty: - pass - outq.put(t) - - th = threading.Thread(target=debounce_thread, daemon=True) - th.start() - - -class Display: - basedump: str - mydump: str - last_refresh_key: object - config: Config - emsg: Optional[str] - last_diff_output: Optional[Diff] - pending_update: Optional[str] - ready_queue: "queue.Queue[None]" - watch_queue: "queue.Queue[Optional[float]]" - less_proc: "Optional[subprocess.Popen[bytes]]" - - def __init__(self, basedump: str, mydump: str, config: Config) -> None: - self.config = config - self.base_lines = process(basedump, config) - self.mydump = mydump - self.emsg = None - self.last_refresh_key = None - self.last_diff_output = None - - def run_diff(self) -> Tuple[str, object]: - if self.emsg is not None: - return (self.emsg, self.emsg) - - my_lines = process(self.mydump, self.config) - diff_output = do_diff(self.base_lines, my_lines, self.config) - last_diff_output = self.last_diff_output or diff_output - if self.config.threeway != "base" or not self.last_diff_output: - self.last_diff_output = diff_output - - meta, diff_lines = align_diffs(last_diff_output, diff_output, self.config) - output = self.config.formatter.table(meta, diff_lines) - refresh_key = ( - [line.key2 for line in diff_output.lines], - diff_output.score, - ) - return (output, refresh_key) - - def run_less( - self, output: str - ) -> "Tuple[subprocess.Popen[bytes], subprocess.Popen[bytes]]": - # Pipe the output through 'tail' and only then to less, to ensure the - # write call doesn't block. ('tail' has to buffer all its input before - # it starts writing.) This also means we don't have to deal with pipe - # closure errors. - buffer_proc = subprocess.Popen( - BUFFER_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE - ) - less_proc = subprocess.Popen(LESS_CMD, stdin=buffer_proc.stdout) - assert buffer_proc.stdin - assert buffer_proc.stdout - buffer_proc.stdin.write(output.encode()) - buffer_proc.stdin.close() - buffer_proc.stdout.close() - return (buffer_proc, less_proc) - - def run_sync(self) -> None: - output, _ = self.run_diff() - proca, procb = self.run_less(output) - procb.wait() - proca.wait() - - def run_async(self, watch_queue: "queue.Queue[Optional[float]]") -> None: - self.watch_queue = watch_queue - self.ready_queue = queue.Queue() - self.pending_update = None - output, refresh_key = self.run_diff() - self.last_refresh_key = refresh_key - dthread = threading.Thread(target=self.display_thread, args=(output,)) - dthread.start() - self.ready_queue.get() - - def display_thread(self, initial_output: str) -> None: - proca, procb = self.run_less(initial_output) - self.less_proc = procb - self.ready_queue.put(None) - while True: - ret = procb.wait() - proca.wait() - self.less_proc = None - if ret != 0: - # fix the terminal - os.system("tput reset") - if ret != 0 and self.pending_update is not None: - # killed by program with the intent to refresh - output = self.pending_update - self.pending_update = None - proca, procb = self.run_less(output) - self.less_proc = procb - self.ready_queue.put(None) - else: - # terminated by user, or killed - self.watch_queue.put(None) - self.ready_queue.put(None) - break - - def progress(self, msg: str) -> None: - # Write message to top-left corner - sys.stdout.write("\x1b7\x1b[1;1f{}\x1b8".format(msg + " ")) - sys.stdout.flush() - - def update(self, text: str, error: bool) -> None: - if not error and not self.emsg and text == self.mydump: - self.progress("Unchanged. ") - return - if not error: - self.mydump = text - self.emsg = None - else: - self.emsg = text - output, refresh_key = self.run_diff() - if refresh_key == self.last_refresh_key: - self.progress("Unchanged. ") - return - self.last_refresh_key = refresh_key - self.pending_update = output - if not self.less_proc: - return - self.less_proc.kill() - self.ready_queue.get() - - def terminate(self) -> None: - if not self.less_proc: - return - self.less_proc.kill() - self.ready_queue.get() - - -def main() -> None: - args = parser.parse_args() - - # Apply project-specific configuration. - settings: Dict[str, Any] = {} - diff_settings.apply(settings, args) # type: ignore - project = create_project_settings(settings) - - try: - config = create_config(args, project) - except ValueError as e: - fail(str(e)) - - if config.algorithm == "levenshtein": - try: - import Levenshtein - except ModuleNotFoundError as e: - fail(MISSING_PREREQUISITES.format(e.name)) - - if config.show_source: - try: - import cxxfilt - except ModuleNotFoundError as e: - fail(MISSING_PREREQUISITES.format(e.name)) - - if config.threeway and not args.watch: - fail("Threeway diffing requires -w.") - - if args.diff_elf_symbol: - make_target, basecmd, mycmd = dump_elf( - args.start, args.end, args.diff_elf_symbol, config, project - ) - elif config.diff_obj: - make_target, basecmd, mycmd = dump_objfile( - args.start, args.end, config, project - ) - else: - make_target, basecmd, mycmd = dump_binary(args.start, args.end, config, project) - - map_build_target_fn = getattr(diff_settings, "map_build_target", None) - if map_build_target_fn: - make_target = map_build_target_fn(make_target=make_target) - - if args.write_asm is not None: - mydump = run_objdump(mycmd, config, project) - with open(args.write_asm, "w") as f: - f.write(mydump) - print(f"Wrote assembly to {args.write_asm}.") - sys.exit(0) - - if args.base_asm is not None: - with open(args.base_asm) as f: - basedump = f.read() - else: - basedump = run_objdump(basecmd, config, project) - - mydump = run_objdump(mycmd, config, project) - - display = Display(basedump, mydump, config) - - if args.no_pager or args.format in ("html", "json"): - print(display.run_diff()[0]) - elif not args.watch: - display.run_sync() - else: - if not args.make: - yn = input( - "Warning: watch-mode (-w) enabled without auto-make (-m). " - "You will have to run make manually. Ok? (Y/n) " - ) - if yn.lower() == "n": - return - if args.make: - watch_sources = None - watch_sources_for_target_fn = getattr( - diff_settings, "watch_sources_for_target", None - ) - if watch_sources_for_target_fn: - watch_sources = watch_sources_for_target_fn(make_target) - watch_sources = watch_sources or project.source_directories - if not watch_sources: - fail("Missing source_directories config, don't know what to watch.") - else: - watch_sources = [make_target] - q: "queue.Queue[Optional[float]]" = queue.Queue() - debounced_fs_watch(watch_sources, q, config, project) - display.run_async(q) - last_build = 0.0 - try: - while True: - t = q.get() - if t is None: - break - if t < last_build: - continue - last_build = time.time() - if args.make: - display.progress("Building...") - ret = run_make_capture_output(make_target, project) - if ret.returncode != 0: - display.update( - ret.stderr.decode("utf-8-sig", "replace") - or ret.stdout.decode("utf-8-sig", "replace"), - error=True, - ) - continue - mydump = run_objdump(mycmd, config, project) - display.update(mydump, error=False) - except KeyboardInterrupt: - display.terminate() - - -if __name__ == "__main__": - main() diff --git a/tools/asm_differ/diff_settings.py b/tools/asm_differ/diff_settings.py deleted file mode 100644 index 183b96b..0000000 --- a/tools/asm_differ/diff_settings.py +++ /dev/null @@ -1,11 +0,0 @@ -def apply(config, args): - config["baseimg"] = "target.bin" - config["myimg"] = "source.bin" - config["mapfile"] = "build.map" - config["source_directories"] = ["."] - # config["show_line_numbers_default"] = True - # config["arch"] = "mips" - # config["map_format"] = "gnu" # gnu or mw - # config["mw_build_dir"] = "build/" # only needed for mw map format - # config["makeflags"] = [] - # config["objdump_executable"] = "" diff --git a/tools/asm_differ/mypy.ini b/tools/asm_differ/mypy.ini deleted file mode 100644 index 138b939..0000000 --- a/tools/asm_differ/mypy.ini +++ /dev/null @@ -1,17 +0,0 @@ -[mypy] -check_untyped_defs = True -disallow_any_generics = True -disallow_incomplete_defs = True -disallow_untyped_calls = True -disallow_untyped_decorators = True -disallow_untyped_defs = True -no_implicit_optional = True -warn_redundant_casts = True -warn_return_any = True -warn_unused_ignores = True -ignore_missing_imports = True -python_version = 3.6 -files = diff.py - -[mypy-diff_settings] -ignore_errors = True diff --git a/tools/asm_differ/screenshot.png b/tools/asm_differ/screenshot.png deleted file mode 100644 index 3230555..0000000 Binary files a/tools/asm_differ/screenshot.png and /dev/null differ diff --git a/tools/disassemble_elf.py b/tools/disassemble_elf.py deleted file mode 100755 index 1fa58a8..0000000 --- a/tools/disassemble_elf.py +++ /dev/null @@ -1,312 +0,0 @@ -#!/usr/bin/env python3 -# -# ELF disassembler that attempts to be matching -# - -import argparse, struct, sys - -from libelf import * -from mdebug import * -from mips_isa import * -from util import * - -def debug_log(msg): - print(msg, file=sys.stderr) - -class MipsDisasm: - """ - """ - - def __init__(self, elf_file) -> None: - self.elf_file = elf_file - mdebug_section = elf_file.find_section_by_type(SHT_MIPS_DEBUG) - if mdebug_section is not None: - self.mdebug = mdebug_section - self.has_mdebug = True - else: - self.has_mdebug = False - self.cur_file = None - self.comment_section_pos = 1 - self.section_local_labels = {} - - def add_section_local_label(self, section, offset): - if section not in self.section_local_labels: - self.section_local_labels.update({section : set()}) - self.section_local_labels[section].add(offset) - - def advance_file(self): - seen_cur_file = False - for sym in self.elf_file.symtab.symbol_entries: - if sym.type == ST_FILE: - if seen_cur_file or self.cur_file is None: - self.cur_file = sym - break - elif self.cur_file == sym: - seen_cur_file = True - return self.cur_file is not None - - def disassemble_all_sections(self): - print(MipsDisasm.asm_prelude()) - - # debug_log("Name Type Addr Off Size ES Flg Lk Inf Al") - for section in self.elf_file.sections: - local_labels = self.section_local_labels.get(section.name, None) - # debug_log(section) - if section.name in ['', '.strtab', '.shstrtab', '.symtab', '.reginfo', '.comment', '.note', '.options', '.mdebug', '.gptab.data', '.gptab.bss'] or \ - (section.sh_type == SHT_REL or section.sh_type == SHT_RELA): - continue - if section.sh_size == 0: - continue - print("") - print(MipsDisasm.begin_section(section)) - if section.is_executable(): - self.disassemble_exec(section) - elif section.sh_type == SHT_PROGBITS: - # TODO kmc as doesn't support incbin, byte array this - # print(f".incbin \"libultra.a\", 0x{section.sh_offset:08X}, 0x{section.sh_size:X}") - first = True - for i,b in enumerate(section.data): - if local_labels is not None and i in local_labels: - if not first: - print("") - print(f".{section.name[1].upper()}_{i:08X}:") - print(" .byte ", end='') - first = True - elif first: - print(" .byte ", end='') - if not first: - print(", ", end='') - print(f"0x{int(b):02X}", end='') - first = False - print("") - elif section.sh_type == SHT_NOBITS: - print(f".skip 0x{section.sh_size:X}") - else: - assert False, f"Unhandled section: {section.name}" - # debug_log("/// UNHANDLED ///") - - def pass_section(self, section): - pass - - @staticmethod - def asm_prelude(): - return f""".include "macro.inc" -#include "regdef.h" - -// assembler directives -.set noat // allow manual use of $at -.set noreorder // don't insert nops after branches""" - - @staticmethod - def begin_section(section): - section_flags = section.flags_str().lower().replace(' ', '') - if section_flags != "": - section_flags = f", \"{section_flags}\"" - section_type = "" - if section.sh_type == SHT_PROGBITS: - section_type = ", @progbits" - elif section.sh_type == SHT_NOBITS: - section_type = ", @nobits" - if section_type != "" and section_flags == "": - section_flags = ", \"\"" - - return f""".section {section.name}{section_flags}{section_type} -.balign {section.sh_addralign} -""" - - def get_label_name(self, addr, pdr=None, optional=False): - if pdr is not None: - sym = pdr.lookup_sym(addr, EcoffSt.LABEL) - if sym is not None: - return sym.name - if not optional: - return f".L{addr:08X}" - else: - return None - - def get_comment_string(self, start): - comment_section = self.elf_file.find_section_by_name(".comment") - end = comment_section.data.find(b'\0', start) - if end == -1: - return None, None - comment = comment_section.data[start:end].decode("ASCII") - return comment, end + 1 - - def print_end(self, vaddr, eof): - ends = eof.get(vaddr, None) - if ends is not None: - for sym in ends: - print(f" .type {sym.name}, @{'function' if sym.type == ST_FUNC else 'object'}") - if sym.st_size != 0: - print(f" .size {sym.name}, . - {sym.name}") - print(f" .end {sym.name}\n") - - def disassemble_exec(self, section): - raw_insns = as_word_list(section.data) - insns = [decode_insn(raw, section.sh_addr + j * 4) for j,raw in enumerate(raw_insns)] - - # enumerate branch labels - branch_labels = set() - - for i,insn in enumerate(insns): - if insn.id in MIPS_BRANCH_INSNS or insn.id == MIPS_INS_J: - branch_labels.add(insn.target if insn.id == MIPS_INS_J else insn.offset) - - eof = {} # vaddr : name - def add_end(vaddr, sym): - if vaddr not in eof: - eof[vaddr] = set() - eof[vaddr].add(sym) - - cur_fdr = None - cur_pdr = None - for i,insn in enumerate(insns): - mnemonic = insn.mnemonic - op_str = insn.op_str - - # Update mdebug info - src_inf = "" - if self.has_mdebug: - # Get new fdr if there is one - fdr = self.mdebug.fdr_foraddr(i * 4, extensions=('.c', '.s')) - if fdr is not None: - # debug_log(fdr.name) - cur_fdr = fdr - - # Get new pdr if there is one - if cur_fdr is not None: - pdr = cur_fdr.pdr_foraddr(i * 4) - if pdr is not None: - # debug_log(pdr) - cur_pdr = pdr - - # Line numbers - if cur_pdr is not None: - asm_line = i - cur_pdr.addr//4 - if asm_line < len(cur_pdr.lines): - src_inf = f" {cur_pdr.lines[asm_line]:4}" - else: - src_inf = " PADDING" - - # Symbols for this address - syms = section.get_sym(i * 4) - # if len(syms) != 0: - # debug_log("\n".join([str(sym) for sym in syms])) - - # Print end - self.print_end(insn.vaddr, eof) - - # Print symbol - for sym in syms: - if sym.name == "gcc2_compiled.": - print(f"// compiler generated") - if self.cur_file is None: - print(f".version \"01.01\"") - if self.advance_file(): - print(f".file 1 \"{self.cur_file.name}\"") - - comment_string = None - while comment_string != "\"GCC: (GNU) 2.7.2\"": - comment_string, self.comment_section_pos = self.get_comment_string(self.comment_section_pos) - if comment_string is None: - break - print(f".ident \"{comment_string}\"") - - if sym.bind == SB_GLOBAL: - print(f"glabel {sym.name}") - else: - print(f"{sym.name}:") - - if sym.st_size != 0: - print(f" .ent {sym.name}") - add_end(insn.vaddr + sym.st_size, sym) - else: - print(f" .type {sym.name}, @{'function' if sym.type == ST_FUNC else 'object'}\n") - - # Print branch labels - lbl = self.get_label_name(insn.vaddr, pdr=cur_pdr, optional=not insn.vaddr in branch_labels) - if lbl is not None: - print(f"{lbl}:") - - # Relocations for this address - rels = section.get_rel(i * 4) - assert len(rels) < 2 # There should never be more than 1 relocation for a single address, right? - # if len(rels) != 0: - # debug_log("\n".join([str(rel) for rel in rels])) - - # Apply relocation - if len(rels) != 0: - rel = rels[0] - if rel.rel_type == R_MIPS_26: - if insn.id == MIPS_INS_JAL: - op_str = rel.relocated_symbol.name - if op_str == ".text" and cur_fdr is not None: - pdr = cur_fdr.pdr_foraddr(insn.target) - if pdr is not None: - op_str = pdr.name - elif insn.id != MIPS_INS_J: # Branch labels for j instructions are also R_MIPS_26 relocations - assert False , f"Got unexpected R_MIPS_26 relocation {insn.id}" - elif rel.rel_type == R_MIPS_HI16: - assert insn.id in [MIPS_INS_LUI] - rel_name = rel.relocated_symbol.name - if rel.relocated_symbol.type == ST_SECTION: - rel_name = f".{rel_name[1].upper()}_00000000" - if cur_fdr is not None: - pass - - op_str = f"{insn.abi.gpr_names[insn.rt]}, %hi({rel_name})" - elif rel.rel_type == R_MIPS_LO16: - # Ideally this should be in the elf code so the relocations don't look identical - addend = insn.imm - rel_name = rel.relocated_symbol.name - if rel.relocated_symbol.type == ST_SECTION: - rel_name = f".{rel_name[1].upper()}_{addend:08X}" - self.add_section_local_label(rel.relocated_symbol.name, addend) - addend = 0 - addend_str = f" + 0x{addend:X}" if addend != 0 else "" - - if insn.id == MIPS_INS_ADDIU: - op_str = f"{insn.abi.gpr_names[insn.rt]}, {insn.abi.gpr_names[insn.rs]}, %lo({rel_name}{addend_str})" - elif insn.id in MIPS_LOAD_STORE_INSNS: - if insn.id in MIPS_FP_LOAD_STORE_INSNS: - op_str = f"{insn.abi.cop1_names[insn.ft]}, " - else: - op_str = f"{insn.abi.gpr_names[insn.rt]}, " - op_str += f"%lo({rel_name}{addend_str})({insn.abi.gpr_names[insn.base]})" - else: - assert False - else: - assert False - - # Apply branch labels - if insn.id in MIPS_BRANCH_INSNS: - op_str_parts = [] - for field in insn.fields: - if field == 'offset': - op_str_parts.append(self.get_label_name(insn.offset, cur_pdr)) - else: - op_str_parts.append(insn.format_field(field)) - op_str = ", ".join(op_str_parts) - elif insn.id == MIPS_INS_J: - op_str = self.get_label_name(insn.target, cur_pdr) - - print(f"/* {section.sh_offset + i * 4:06X} {insn.vaddr:08X} {insn.raw:08X}{src_inf} */ {mnemonic:12}{op_str:35}".rstrip()) - self.print_end(section.sh_addr + section.sh_size, eof) - -def main(): - parser = argparse.ArgumentParser(description="Disassemble relocatable ELF object.") - parser.add_argument("filepath", help="path to the ELF file") - # TODO unimplemented optionals - parser.add_argument("--compiler", help="original compiler that produced the ELF (IDO or GCC, IDO default)", default="IDO") - parser.add_argument("--strenc", help="string encoding, default is EUC-JP for IDO and SJIS for GCC") - args = parser.parse_args() - - elf_file = None - with open(args.filepath, "rb") as elf: - elf_file = ElfFile(bytearray(elf.read())) - - disassembler = MipsDisasm(elf_file) - disassembler.disassemble_all_sections() - -if __name__ == '__main__': - main() diff --git a/tools/fix_objfile.py b/tools/fix_objfile.py deleted file mode 100755 index 52a79bc..0000000 --- a/tools/fix_objfile.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python3 -# -# Fixes garbage data between sections and optionally rodata section flags -# - -import argparse -from libelf import * - -def fix_section_flags(elf): - for section in elf.sections: - # Unset Alloc flag in .rodata section - if section.name == ".rodata": - section.sh_flags &= ~SHF_ALLOC - -def fix_garbage(elf, original): - # Begin with the original data, and paste header and section data over it - result = original - if len(original) < len(elf.data): - result.extend([0]*(len(elf.data) - len(original))) - else: - result = result[:len(elf.data)] - - # NOTE: This only supports the elf header, program headers, section headers and section data at this time - - # emplace elf header - hdr = elf.elf_header.to_bin() - result[0:len(hdr)] = hdr - - # emplace program headers - for i,proghdr in enumerate(elf.progheaders): - offset = elf.elf_header.e_phoff + i * elf.elf_header.e_phentsize - result[offset:offset+elf.elf_header.e_phentsize] = proghdr.to_bin() - - # emplace section headers and section data - for i,section in enumerate(elf.sections): - offset = elf.elf_header.e_shoff + i * elf.elf_header.e_shentsize - section_header, section_data = section.to_bin() - result[offset:offset+elf.elf_header.e_shentsize] = section_header - if section_data is not None: - result[section.sh_offset:section.sh_offset+section.sh_size] = section_data - - return result - -def main(): - parser = argparse.ArgumentParser(description="Disassemble relocatable ELF object.") - parser.add_argument("compiled", help="path to the compiled ELF file") - parser.add_argument("original", help="path to the original ELF file") - parser.add_argument("--fix-section-flags", help="", action="store_true") - args = parser.parse_args() - - elf = None - with open(args.compiled, "rb") as elf_file: - elf = ElfFile(bytearray(elf_file.read())) - - original = None - with open(args.original, "rb") as original_file: - original = bytearray(original_file.read()) - - if args.fix_section_flags: - fix_section_flags(elf) - - result_data = fix_garbage(elf, original) - - with open(args.compiled, "wb") as elf_file: - elf_file.write(result_data) - -if __name__ == '__main__': - main() diff --git a/tools/libdiff.py b/tools/libdiff.py deleted file mode 100755 index ac6b139..0000000 --- a/tools/libdiff.py +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/env python3 -from typing import Any, Dict, Optional -import json -import logging -import subprocess -import tempfile -import pathlib -import sys -import queue - -import asm_differ.diff as asm_differ - -MAX_FUNC_SIZE_LINES = 5000 - -class AsmDifferWrapper: - @staticmethod - def create_config(arch: asm_differ.ArchSettings) -> asm_differ.Config: - return asm_differ.Config( - arch=arch, - # Build/objdump options - diff_obj=True, - make=False, - source_old_binutils=True, - diff_section=".text", - inlines=False, - max_function_size_lines=MAX_FUNC_SIZE_LINES, - max_function_size_bytes=MAX_FUNC_SIZE_LINES * 4, - # Display options - formatter=asm_differ.AnsiFormatter(column_width=50), - threeway=None, - base_shift=0, - skip_lines=0, - compress=None, - show_branches=True, - show_line_numbers=False, - show_source=False, - stop_jrra=False, - ignore_large_imms=False, - ignore_addr_diffs=False, - algorithm="levenshtein", - ) - - @staticmethod - def run_objdump(target_data: bytes, config: asm_differ.Config, label: Optional[str]) -> Optional[str]: - flags = [ - "--disassemble", - "--disassemble-zeroes", - "--line-numbers", - "--reloc", - ] - - with tempfile.TemporaryDirectory() as tempdir: - target_path = pathlib.Path(tempdir) / "out.s" - target_path.write_bytes(target_data) - - start_addr = 0 - - if label: - nm_command = "mips-linux-gnu-nm" - - if nm_command: - try: - nm_proc = subprocess.run( - [nm_command] + [target_path], - capture_output=True, - universal_newlines=True - ) - except subprocess.CalledProcessError as e: - logger.error(f"Error running nm: {e}") - logger.error(e.stderr) - - if nm_proc.stdout: - for line in nm_proc.stdout.splitlines(): - if label in line: - start_addr = int(line.split()[0], 16) - break - else: - # logger.error(f"No nm command for {platform}") - return None - - flags.append(f"--start-address={start_addr}") - - objdump_command = "mips-linux-gnu-objdump" - cmds = [objdump_command] + config.arch.arch_flags + flags + [target_path] - - try: - objdump_proc = subprocess.run( - [objdump_command] + config.arch.arch_flags + flags + [target_path], - capture_output=True, - universal_newlines=True - ) - except subprocess.CalledProcessError as e: - # logger.error(e) - # logger.error(e.stderr) - return None - - out = objdump_proc.stdout - return out - - @staticmethod - def diff(target_elf: bytes, compiled_elf: bytes, diff_label:Optional[str]) -> Dict[str, Any]: - arch = asm_differ.get_arch("mips") - - config = AsmDifferWrapper.create_config(arch) - - # Base - if len(target_elf) == 0: - print("Base asm empty") - return - - basedump = AsmDifferWrapper.run_objdump(target_elf, config, diff_label) - if not basedump: - print("Error running asm-differ on basedump") - return - - if len(compiled_elf) == 0: - print("Creation of compilation elf_object failed") - return - - mydump = AsmDifferWrapper.run_objdump(compiled_elf, config, diff_label) - if not mydump: - print("Error running asm-differ") - return - - # Preprocess the dumps - basedump = asm_differ.preprocess_objdump_out(None, target_elf, basedump, config) - mydump = asm_differ.preprocess_objdump_out(None, compiled_elf, mydump, config) - - try: - display = asm_differ.Display(basedump, mydump, config) - except Exception: - print("Error running asm-differ") - return - - # Print the output - display.run_sync() - -if __name__ == "__main__": - if len(sys.argv) != 3 and len(sys.argv) != 4: - print(f"Usage: {sys.argv[0]} [path/to/target.o] [path/to/compiled.o] [function name (optional)]") - sys.exit(0) - target = pathlib.Path(sys.argv[1]) - compiled = pathlib.Path(sys.argv[2]) - if len(sys.argv) == 4: - label = sys.argv[3] - else: - label = None - target_bytes = target.read_bytes() - compiled_bytes = compiled.read_bytes() - AsmDifferWrapper.diff(target_bytes, compiled_bytes, label) diff --git a/tools/libelf.py b/tools/libelf.py deleted file mode 100755 index 11c4c03..0000000 --- a/tools/libelf.py +++ /dev/null @@ -1,1181 +0,0 @@ -#!/usr/bin/env python3 -# -# MIPS ELF library -# - -import struct - -from mdebug import EcoffHDRR, EcoffFdr, EcoffPdr, EcoffLiner, EcoffSymr - -# ===================================================================================================== -# Utility -# ===================================================================================================== - -def align_as(value, align): - if align == 0: - return value - while (value % align != 0): - value += 1 - return value - -# ===================================================================================================== -# ELF Identity -# ===================================================================================================== - -# Offsets into e_ident -EI_MAG0 = 0x00 # Magic char 0 , 0x7F -EI_MAG1 = 0x01 # Magic char 1 , 0x45 -EI_MAG2 = 0x02 # Magic char 2 , 0x4C -EI_MAG3 = 0x03 # Magic char 3 , 0x46 -EI_CLASS = 0x04 # -EI_DATA = 0x05 # -EI_VERSION = 0x06 # -EI_OSABI = 0x07 # -EI_ABIVERSION = 0x08 # -EI_PAD = 0x09 # -EI_NIDENT = 0x10 # - -# Values for e_ident[EI_DATA] -EI_DATA_LE = 1 # little endian -EI_DATA_BE = 2 # big endian - -EI_DATA_V = { - EI_DATA_LE : 'Little Endian', - EI_DATA_BE : 'Big Endian' -} - -# Values for e_ident[EI_CLASS] -EI_CLASS_32 = 1 # 32-bit -EI_CLASS_64 = 2 # 64-bit - -EI_CLASS_V = { - EI_CLASS_32 : 'ELF32', - EI_CLASS_64 : 'ELF64' -} - -# Values for e_ident[EI_OSABI] -EI_OSABI_V = { - 0x00 : 'UNIX / System V', - 0x01 : 'HP-UX', - 0x02 : 'NetBSD', - 0x03 : 'Linux', - 0x04 : 'GNU Hurd', - 0x06 : 'Solaris', - 0x07 : 'AIX', - 0x08 : 'IRIX', - 0x09 : 'FreeBSD', - 0x0A : 'Tru64', - 0x0B : 'Novell Modesto', - 0x0C : 'OpenBSD', - 0x0D : 'OpenVMS', - 0x0E : 'NonStop Kernel', - 0x0F : 'AROS', - 0x10 : 'Fenix OS', - 0x11 : 'CloudABI', - 0x12 : 'Stratus Technologies OpenVOS' -} - -# ===================================================================================================== -# ELF Types -# ===================================================================================================== - -ET_NONE = 0x0000 # -ET_REL = 0x0001 # relocatable -ET_EXEC = 0x0002 # -ET_DYN = 0x0003 # -ET_CORE = 0x0004 # -ET_LOOS = 0xFE00 # -ET_HIOS = 0xFEFF # -ET_LOPROC = 0xFF00 # -ET_HIPROC = 0xFFFF # - -E_TYPE = { - ET_NONE : 'ET_NONE', - ET_REL : 'ET_REL', - ET_EXEC : 'ET_EXEC', - ET_DYN : 'ET_DYN', - ET_CORE : 'ET_CORE', - ET_LOOS : 'ET_LOOS', - ET_HIOS : 'ET_HIOS', - ET_LOPROC : 'ET_LOPROC', - ET_HIPROC : 'ET_HIPROC' -} - -# ===================================================================================================== -# ELF Machines -# ===================================================================================================== - -EM_UNSPECIFIED = 0x00 -EM_ATNT_WE_32100 = 0x01 -EM_SPARC = 0x02 -EM_X86 = 0x03 -EM_MOTOROLA_68000 = 0x04 -EM_MOTOROLA_88000 = 0x05 -EM_INTEL_MCU = 0x06 -EM_INTEL_80860 = 0x07 -EM_MIPS = 0x08 -EM_IBM_SYSTEM_370 = 0x09 -EM_MIPS_RS3000_LE = 0x0A -EM_RESERVED_xB = 0x0B -EM_RESERVED_xC = 0x0C -EM_RESERVED_xD = 0x0D -EM_HEWLETT_PACKARD = 0x0E -EM_RESERVED_xF = 0x0F -EM_INTEL_80960 = 0x13 -EM_POWERPC = 0x14 -EM_POWERPC_64 = 0x15 -EM_5390 = 0x16 -EM_ARM = 0x28 -EM_SUPERH = 0x2A -EM_IA64 = 0x32 -EM_AMD64 = 0x3E -EM_TMS320C6000 = 0x8C -EM_ARM64 = 0xB7 -EM_RISC_V = 0xF3 - -E_MACHINE = { - EM_UNSPECIFIED : 'Unspecified', - EM_ATNT_WE_32100 : 'AT&T WE 32100', - EM_SPARC : 'SPARC', - EM_X86 : 'x86', - EM_MOTOROLA_68000 : 'Motorola 68000 (M68K)', - EM_MOTOROLA_88000 : 'Motorola 88000 (M88K)', - EM_INTEL_MCU : 'Intel MCU', - EM_INTEL_80860 : 'Intel 80860', - EM_MIPS : 'MIPS', - EM_IBM_SYSTEM_370 : 'IBM_System/370', - EM_MIPS_RS3000_LE : 'MIPS RS3000 Little-endian', - EM_RESERVED_xB : 'Reserved', - EM_RESERVED_xC : 'Reserved', - EM_RESERVED_xD : 'Reserved', - EM_HEWLETT_PACKARD : 'Hewlett-Packard PA-RISC', - EM_RESERVED_xF : 'Reserved', - EM_INTEL_80960 : 'Intel 80960', - EM_POWERPC : 'PowerPC', - EM_POWERPC_64 : 'PowerPC (64-bit)', - EM_5390 : 'S390, including S390x', - EM_ARM : 'ARM (up to ARMv7/Aarch32)', - EM_IA64 : 'SuperH', - EM_IA64 : 'IA-64', - EM_AMD64 : 'amd64', - EM_TMS320C6000 : 'TMS320C6000 Family', - EM_ARM64 : 'ARM 64-bits (ARMv8/Aarch64)', - EM_RISC_V : 'RISC-V' -} - -# ===================================================================================================== -# Program Types -# ===================================================================================================== - -PT_NULL = 0x00000000 # Program header table entry unused -PT_LOAD = 0x00000001 # Loadable segment -PT_DYNAMIC = 0x00000002 # Dynamic linking information -PT_INTERP = 0x00000003 # Interpreter information -PT_NOTE = 0x00000004 # Auxiliary information -PT_SHLIB = 0x00000005 # Reserved -PT_PHDR = 0x00000006 # Segment containing the program header table itself -PT_TLS = 0x00000007 # Thread-Local storage template -PT_LOOS = 0x60000000 # Inclusive reserved range for processor-specific semantics -PT_HIOS = 0x6FFFFFFF # ^ -PT_LOPROC = 0x70000000 # Inclusive reserved range for processor-specific semantics -PT_HIPROC = 0x7FFFFFFF # ^ - -P_TYPE = { - PT_NULL : 'PT_NULL' , - PT_LOAD : 'PT_LOAD' , - PT_DYNAMIC : 'PT_DYNAMIC', - PT_INTERP : 'PT_INTERP' , - PT_NOTE : 'PT_NOTE' , - PT_SHLIB : 'PT_SHLIB' , - PT_PHDR : 'PT_PHDR' , - PT_TLS : 'PT_TLS' , - PT_LOOS : 'PT_LOOS' , - PT_HIOS : 'PT_HIOS' , - PT_LOPROC : 'PT_LOPROC' , - PT_HIPROC : 'PT_HIPROC' -} - -# ===================================================================================================== -# Program Flags ( May be platform specific ! ) -# ===================================================================================================== - -PF_E = 1 << 0 # Execute -PF_W = 1 << 1 # Write -PF_R = 1 << 2 # Read - -# ===================================================================================================== -# Symbol Info -# ===================================================================================================== - -# Symbol Types - -ST_NOTYPE = 0 -ST_OBJECT = 1 -ST_FUNC = 2 -ST_SECTION = 3 -ST_FILE = 4 - -SYM_TYPE = { - ST_NOTYPE : 'NOTYPE', - ST_OBJECT : 'OBJECT', - ST_FUNC : 'FUNC', - ST_SECTION : 'SECTION', - ST_FILE : 'FILE' -} - -# Symbol Bind - -SB_LOCAL = 0 -SB_GLOBAL = 1 -SB_WEAK = 2 - -SYM_BIND = { - SB_LOCAL : 'LOCAL', - SB_GLOBAL : 'GLOBAL', - SB_WEAK : 'WEAK' -} - -# Symbol Visibility - -SV_DEFAULT = 0 - -SYM_VIS = { - SV_DEFAULT : 'DEFAULT' -} - -# NDX - -SHN_UND = 0x0000 -SHN_ABS = 0xFFF1 -SHN_COMMON = 0xFFF2 -SHN_XINDEX = 0xFFFF -SHN_LORESERVE = 0xFF00 - -SYM_NDX = { - SHN_UND : 'UND', - SHN_ABS : 'ABS', - SHN_COMMON : 'COMMON', - SHN_XINDEX : 'XINDEX', - SHN_LORESERVE : 'LORESERVE' -} - -# ===================================================================================================== -# Relocation Type -# ===================================================================================================== - -# EM_MIPS -R_MIPS_32 = 2 # Write the 32 bit address of the symbol -R_MIPS_26 = 4 # Write the 26 bit address of the symbol divided by four (for relocating branch instructions). Fail if address won't fit -R_MIPS_HI16 = 5 # Write the high 16 bits of the address of the symbol -R_MIPS_LO16 = 6 # Write the low 16 bits of the address of the symbol - -# EM_POWERPC -R_PPC_NONE = 0 # Do nothing. Skip this entry -R_PPC_ADDR32 = 1 # Write the 32 bit address of the symbol -R_PPC_ADDR24 = 2 # Write the 24 bit address of the symbol divided by four shifted up 2 bits to the 32 bit value (for relocating branch instructions). Fail if address won't fit -R_PPC_ADDR16 = 3 # Write the 16 bit address of the symbol. Fail if address more than 16 bits -R_PPC_ADDR16_LO = 4 # Write the low 16 bits of the address of the symbol -R_PPC_ADDR16_HI = 5 # Write the high 16 bits of the address of the symbol -R_PPC_ADDR16_HA = 6 # Write the high 16 bits of the address of the symbol plus 0x8000 -R_PPC_ADDR14 = 7 # Write the 14 bits of the address of the symbol divided by four shifted up 2 bits to the 32 bit value (for relocating conditional branch instructions). Fail if address won't fit -R_PPC_REL24 = 10 # Write the 24 bit address of the symbol minus the address of the relocation divided by four shifted up 2 bits to the 32 bit value (for relocating relative branch instructions). Fail if address won't fit -R_PPC_REL14 = 11 # Write the 14 bit address of the symbol minus the address of the relocation divided by four shifted up 2 bits to the 32 bit value (for relocating conditional relative branch instructions). Fail if address won't fit -R_PPC_EMB_SDA21 = 109 # Small Data Area - -# Gamecube/Wii custom relocations -R_DOLPHIN_NOP = 201 # Do nothing. Skip this entry. Carry the address of the symbol to the next entry -R_DOLPHIN_SECTION = 202 # Change which section relocations are being applied to. Set the offset into the section to 0 -R_DOLPHIN_END = 203 # Stop parsing the relocation table -R_DOLPHIN_MRKREF = 204 # Unknown - -RELOC_TYPE = { - EM_MIPS : { - R_MIPS_32 : 'R_MIPS_32', - R_MIPS_26 : 'R_MIPS_26', - R_MIPS_HI16 : 'R_MIPS_HI16', - R_MIPS_LO16 : 'R_MIPS_LO16' - }, - EM_POWERPC : { - R_PPC_NONE : 'R_PPC_NONE', - R_PPC_ADDR32 : 'R_PPC_ADDR32', - R_PPC_ADDR24 : 'R_PPC_ADDR24', - R_PPC_ADDR16 : 'R_PPC_ADDR16', - R_PPC_ADDR16_LO : 'R_PPC_ADDR16_LO', - R_PPC_ADDR16_HI : 'R_PPC_ADDR16_HI', - R_PPC_ADDR16_HA : 'R_PPC_ADDR16_HA', - R_PPC_ADDR14 : 'R_PPC_ADDR14', - 8 : 'R_PPC_ADDR14', - 9 : 'R_PPC_ADDR14', - R_PPC_REL24 : 'R_PPC_REL24', - R_PPC_REL14 : 'R_PPC_REL14', - 12 : 'R_PPC_REL14', - 13 : 'R_PPC_REL14', - R_PPC_EMB_SDA21 : 'R_PPC_EMB_SDA21', - R_DOLPHIN_NOP : 'R_DOLPHIN_NOP', - R_DOLPHIN_SECTION : 'R_DOLPHIN_SECTION', - R_DOLPHIN_END : 'R_DOLPHIN_END', - R_DOLPHIN_MRKREF : 'R_DOLPHIN_MRKREF' - } -} - -# ===================================================================================================== -# Section Types -# ===================================================================================================== - -SHT_NULL = 0x00000000 # Section header table entry unused -SHT_PROGBITS = 0x00000001 # Program data -SHT_SYMTAB = 0x00000002 # Symbol table -SHT_STRTAB = 0x00000003 # String table -SHT_RELA = 0x00000004 # Relocation entries with addends -SHT_HASH = 0x00000005 # Symbol hash table -SHT_DYNAMIC = 0x00000006 # Dynamic linking information -SHT_NOTE = 0x00000007 # Notes -SHT_NOBITS = 0x00000008 # Program space with no data (bss) -SHT_REL = 0x00000009 # Relocation entries, no addends -SHT_SHLIB = 0x0000000A # Reserved -SHT_DYNSYM = 0x0000000B # Dynamic linker symbol table -SHT_INIT_ARRAY = 0x0000000E # Array of constructors -SHT_FINI_ARRAY = 0x0000000F # Array of destructors -SHT_PREINIT_ARRAY = 0x00000010 # Array of pre-constructors -SHT_GROUP = 0x00000011 # Section group -SHT_SYMTAB_SHNDX = 0x00000012 # Extended section indices -SHT_NUM = 0x00000013 # Number of defined types. -SHT_LOOS = 0x60000000 # Start OS-specific. - -# MIPS specific -SHT_MIPS_DEBUG = 0x70000005 # .mdebug -SHT_MIPS_REGINFO = 0x70000006 # .reginfo -SHT_MIPS_OPTIONS = 0x7000000D # .options - -SH_TYPE = { - SHT_NULL : 'SHT_NULL', - SHT_PROGBITS : 'SHT_PROGBITS', - SHT_SYMTAB : 'SHT_SYMTAB', - SHT_STRTAB : 'SHT_STRTAB', - SHT_RELA : 'SHT_RELA', - SHT_HASH : 'SHT_HASH', - SHT_DYNAMIC : 'SHT_DYNAMIC', - SHT_NOTE : 'SHT_NOTE', - SHT_NOBITS : 'SHT_NOBITS', - SHT_REL : 'SHT_REL', - SHT_SHLIB : 'SHT_SHLIB', - SHT_DYNSYM : 'SHT_DYNSYM', - SHT_INIT_ARRAY : 'SHT_INIT_ARRAY', - SHT_FINI_ARRAY : 'SHT_FINI_ARRAY', - SHT_PREINIT_ARRAY : 'SHT_PREINIT_ARRAY', - SHT_GROUP : 'SHT_GROUP', - SHT_SYMTAB_SHNDX : 'SHT_SYMTAB_SHNDX', - SHT_NUM : 'SHT_NUM', - SHT_LOOS : 'SHT_LOOS', - - SHT_MIPS_DEBUG : 'SHT_MIPS_DEBUG', - SHT_MIPS_REGINFO : 'SHT_MIPS_REGINFO', - SHT_MIPS_OPTIONS : 'SHT_MIPS_OPTIONS', -} - -# ===================================================================================================== -# Section Flags -# ===================================================================================================== - -SHF_WRITE = 1 << 0 # Writable -SHF_ALLOC = 1 << 1 # Occupies memory during execution -SHF_EXECINSTR = 1 << 2 # Executable -SHF_MERGE = 1 << 4 # Might be merged -SHF_STRINGS = 1 << 5 # Contains null-terminated strings -SHF_INFO_LINK = 1 << 6 # 'sh_info' contains SHT index -SHF_LINK_ORDER = 1 << 7 # Preserve order after combining -SHF_OS_NONCONFORMING = 1 << 8 # Non-standard OS specific handling required -SHF_GROUP = 1 << 9 # Section is member of a group -SHF_TLS = 1 << 10 # Section hold thread-local data - -SHF_MASKOS = 0x0ff00000 # OS-specific -SHF_MASKPROC = 0xf0000000 # Processor-specific -SHF_ORDERED = 0x04000000 # Special ordering requirement (Solaris) -SHF_EXCLUDE = 0x08000000 # Section is excluded unless referenced or allocated (Solaris) - -SH_FLAG = { - SHF_WRITE : 'SHF_WRITE', - SHF_ALLOC : 'SHF_ALLOC', - SHF_EXECINSTR : 'SHF_EXECINSTR', - SHF_MERGE : 'SHF_MERGE', - SHF_STRINGS : 'SHF_STRINGS', - SHF_INFO_LINK : 'SHF_INFO_LINK', - SHF_LINK_ORDER : 'SHF_LINK_ORDER', - SHF_OS_NONCONFORMING : 'SHF_OS_NONCONFORMING', - SHF_GROUP : 'SHF_GROUP', - SHF_TLS : 'SHF_TLS', - SHF_MASKOS : 'SHF_MASKOS', - SHF_MASKPROC : 'SHF_MASKPROC', - SHF_ORDERED : 'SHF_ORDERED', - SHF_EXCLUDE : 'SHF_EXCLUDE' -} - -# ===================================================================================================== -# ELF Header -# ===================================================================================================== - -ELF32_BIG_STRUCT = '>HHIIIIIHHHHHH' -ELF32_LITTLE_STRUCT = 'IIIBBH', data) - assert self.st_shndx != SHN_XINDEX, "too many sections (SHN_XINDEX not supported)" - self.bind = st_info >> 4 - self.type = st_info & 0xF - self.visibility = self.st_other & 3 - self.parent_section = elf_file.sections[self.st_shndx] if self.st_shndx < SHN_LORESERVE else None - - self.name = name - if self.type == ST_SECTION: - self.name = elf_file.shstrtab.lookup_str(self.parent_section.sh_name) - if self.name == None: - self.name = elf_file.symtab.strtab.lookup_str(self.st_name) - - @staticmethod - def from_parts(st_name, st_value, st_size, st_info, st_other, st_shndx, strtab, name): - header = struct.pack('>IIIBBH', st_name, st_value, st_size, st_info, st_other, st_shndx) - return Symbol(header, strtab, name) - - def to_bin(self): - st_info = (self.bind << 4) | self.type - return struct.pack('>IIIBBH', self.st_name, self.st_value, self.st_size, st_info, self.st_other, self.st_shndx) - - def section_offset(self): - return self.st_value - self.parent_section.sh_addr - - def padded_size_n(self, n): - return align_as(self.st_size, n) - - def padded_size(self): - return self.padded_size_n(self.parent_section.sh_addralign) - - def __str__(self): - # Num: Value Size Type Bind Vis Ndx Name - ndx = ' ' + (SYM_NDX[self.st_shndx] if self.st_shndx in SYM_NDX.keys() else self.parent_section.name) - out = f"{self.st_value:08X} {self.st_size:06X} {SYM_TYPE[self.type]:7} {SYM_BIND[self.bind]:6} {SYM_VIS[self.visibility]:7} {ndx:12} {self.name}" - return out - -# ===================================================================================================== -# Relocation -# ===================================================================================================== - -class Relocation: - """ - typedef struct { - Elf32_Word r_offset; - struct { - Elf32_Word sym_index : 24; - Elf32_Word rel_type : 8; - } r_info; - Elf32_Word r_addend; - } Elf32_Reloc; - """ - - def __init__(self, data, elf_file, target_section, sh_type): - self.sh_type = sh_type - if sh_type == SHT_REL: - self.r_offset, self.r_info = struct.unpack('>II', data) - self.r_addend = 0 - else: - self.r_offset, self.r_info, self.r_addend = struct.unpack('>III', data) - self.sym_index = self.r_info >> 8 - self.rel_type = self.r_info & 0xff - self.relocated_symbol = elf_file.symtab.symbol_entries[self.sym_index] - self.elf_machine = elf_file.elf_header.e_machine - self.target_section = target_section - # TODO obtain the addend if the target section is executable - - def to_bin(self): - self.r_info = (self.sym_index << 8) | self.rel_type - if self.sh_type == SHT_REL: - return struct.pack('>II', self.r_offset, self.r_info) - else: - return struct.pack('>III', self.r_offset, self.r_info, self.r_addend) - - def relocated_symbol_in_section(self, symtab, section): - return symtab.lookup_symbol_in_section(symtab.symbol_entries[self.sym_index].st_value, section) - - def __str__(self): - relocated_symbol = self.relocated_symbol - # Offset Info Type Sym.Value Sym. Name + Addend - # 80135560 00091201 R_MY_RELOC_TYPE 800f39e0 ...data.0 + 0 - out = f"{self.r_offset:08X} {self.r_info:08X} {RELOC_TYPE[self.elf_machine][self.rel_type]:17} {relocated_symbol.st_value:08X} {relocated_symbol.name} + 0x{self.r_addend:X}" - return out - -# ===================================================================================================== -# Section -# ===================================================================================================== - -class Section: - """ - Describes a section - - typedef struct { - Elf32_Word sh_name; - Elf32_Word sh_type; - Elf32_Word sh_flags; - Elf32_Addr sh_addr; - Elf32_Off sh_offset; - Elf32_Word sh_size; - Elf32_Word sh_link; - Elf32_Word sh_info; - Elf32_Word sh_addralign; - Elf32_Word sh_entsize; - } Elf32_Shdr; - -x00 x00 4 4 sh_name Offset to string in the .shstrtab section representing this section's name - -x04 x04 4 4 sh_type See SH_TYPE - -x08 x08 4 8 sh_flags See SH_FLAG - -x0C x10 4 8 sh_addr Virtual address of the section in memory (for loaded sections) - -x10 x18 4 8 sh_offset Offset of the section in the file image - -x14 x20 4 8 sh_size Size in bytes of the section in the file image - -x18 x28 4 4 sh_link Contains the section index of an associated section. This field is used for several purposes depending on section type - -x1C x2C 4 4 sh_info Contains extra information about the section. This field is used for several purposes, depending on section type - -x20 x30 4 8 sh_addralign Contains the required alignment of the section. This field must be a power of two - -x24 x38 4 8 sh_entsize Contains the size, in bytes, of each entry, for sections that contain fixed-size entries. Zero otherwise - -x28 x40 END - """ - - def __init__(self, header, elf_file, index): - self.late_init_done = False - self.sh_name, self.sh_type, \ - self.sh_flags, self.sh_addr, \ - self.sh_offset, self.sh_size, \ - self.sh_link, self.sh_info, \ - self.sh_addralign, self.sh_entsize = struct.unpack('>IIIIIIIIII', header) - assert not self.sh_flags & SHF_LINK_ORDER - if self.sh_entsize != 0: - assert self.sh_size % self.sh_entsize == 0 - if self.sh_type in [SHT_NULL, SHT_NOBITS]: - self.data = None - else: - self.data = elf_file.data[self.sh_offset:][:self.sh_size] - self.index = index - self.relocated_by = [] - self.elf_file = elf_file - - def late_init(self): - self.late_init_done = True - - @staticmethod - def from_parts(sh_name, sh_type, sh_flags, sh_link, sh_info, sh_addralign, sh_entsize, sh_addr, sh_offset, data, index): - header = struct.pack('>IIIIIIIIII', sh_name, sh_type, sh_flags, sh_addr, sh_offset, \ - len(data), sh_link, sh_info, sh_addralign, sh_entsize) - return Section(header, data, index) - - def to_bin(self): - if self.sh_type not in [SHT_NULL, SHT_NOBITS]: - self.sh_size = len(self.data) - header = struct.pack('>IIIIIIIIII', self.sh_name, self.sh_type, self.sh_flags, self.sh_addr, self.sh_offset, self.sh_size, self.sh_link, self.sh_info, self.sh_addralign, self.sh_entsize) - return header, self.data - - def get_rel(self, addr): - relocs = [] - for reloc_section in self.relocated_by: - reloc = reloc_section.find_reloc(addr) - if reloc is not None: - relocs.append(reloc) - return relocs - - def get_sym(self, addr): - symtab = self.elf_file.symtab - symbols = [] - for symbol in symtab.symbol_entries: - if symbol.st_value == addr and symbol.st_shndx == self.index and symbol.type != ST_SECTION: - symbols.append(symbol) - return symbols - - def is_rel(self): - return self.sh_type == SHT_REL or self.sh_type == SHT_RELA - - def is_allocable(self): - return (self.sh_flags & SHF_ALLOC) == SHF_ALLOC - - def is_executable(self): - return (self.sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR - - def is_writable(self): - return (self.sh_flags & SHF_WRITE) == SHF_WRITE - - def padded_size(self): - return align_as(self.sh_size, self.sh_addralign) - - def padded_size_4(self): - return align_as(self.sh_size, 4) - - def flags_str(self): - flags = "" - flags += "W" if self.is_writable() else " " - flags += "A" if self.is_allocable() else " " - flags += "X" if self.is_executable() else " " - return flags - - def __str__(self): - s_type = SH_TYPE[self.sh_type].replace('SHT_','') if self.sh_type in SH_TYPE.keys() else str(self.sh_type) - out = f"{self.name:12}{s_type:12} " - out += f"{self.sh_addr:08X} " - out += f"{self.sh_offset:06X} " - out += f"{self.sh_size:06X} " - out += f"{self.sh_entsize:2X} " - out += f"{self.flags_str():3} " - out += f"{self.sh_link:2} " - out += f"{self.sh_info:3} " - out += f"{self.sh_addralign:5X} " - return out - -class SymtabSection(Section): - """ - Symbol Table Section - """ - - def __init__(self, header, elf_file, index): - super().__init__(header, elf_file, index) - assert self.sh_entsize == 16 - - def late_init(self): - if self.late_init_done: - return - super().late_init() - self.strtab = self.elf_file.sections[self.sh_link] - self.symbol_entries = [] - for i in range(0, self.sh_size, self.sh_entsize): - self.symbol_entries.append(Symbol(self.data[i:i+self.sh_entsize], self.elf_file)) - - def to_bin(self): - header, _ = super().to_bin() - data = bytearray() - for sym in self.symbol_entries: - data.extend(sym.to_bin()) - return header, data - - def find_symbol(self, name): - for s in self.symbol_entries: - if s.name == name: - return (s.st_shndx, s.st_value) - return None - - def lookup_symbol(self, vaddr): - found = [] - for s in self.symbol_entries: - if s.st_value == vaddr and s.type != ST_SECTION: - found.append(s) - if len(found) != 0: - found.sort(reverse=True, key=(lambda s : s.type)) - return found[0] - return None - - def lookup_symbol_for_section(self, vaddr, shndx): - found = [] - for s in self.symbol_entries: - if s.st_value == vaddr and s.type != ST_SECTION and s.st_shndx == shndx: - found.append(s) - if len(found) != 0: - found.sort(reverse=True, key=(lambda s : s.type)) - return found[0] - return None - - def lookup_symbol_in_section(self, vaddr, section): - found = [] - for s in self.symbol_entries: - if s.st_value == vaddr and s.type != ST_SECTION and s.st_shndx == section.index: - found.append(s) - if len(found) != 0: - found.sort(reverse=True, key=(lambda s : s.type)) - return found[0] - return None - - def find_symbol_in_section(self, name, section): - pos = self.find_symbol(name) - assert pos is not None - assert pos[0] == section.index - return pos[1] - - def local_symbols(self): - return self.symbol_entries[:self.sh_info] - - def global_symbols(self): - return self.symbol_entries[self.sh_info:] - -class StrtabSection(Section): - """ - String Table Section - """ - def __init__(self, header, data, index): - super().__init__(header, data, index) - - def lookup_str(self, index): - to = self.data.find(b'\0', index) - assert to != -1 - return self.data[index:to].decode('latin1') - -class RelocationSection(Section): - """ - Relocation Section - """ - def __init__(self, header, data, index): - super().__init__(header, data, index) - - def late_init(self): - if self.late_init_done: - return - super().late_init() - self.rel_target = self.elf_file.sections[self.sh_info] - self.rel_target.relocated_by.append(self) - self.relocations = [] - for i in range(0, self.sh_size, self.sh_entsize): - self.relocations.append(Relocation(self.data[i:][:self.sh_entsize], self.elf_file, self.rel_target, self.sh_type)) - - def to_bin(self): - header, _ = super().to_bin() - data = bytearray() - for rel in self.relocations: - data.extend(rel.to_bin()) - return header, data - - def lookup_reloc(self, vaddr): - for r in self.relocations: - if r.r_offset - r.r_offset % 4 == vaddr: - return r - return None - - def find_reloc(self, vaddr): - for r in self.relocations: - if r.r_offset == vaddr: - return r - return None - - def lookup_jtbl_reloc(self, vaddr, symtab): - for r in self.relocations: - r_sym = r.relocated_symbol(symtab) - if r_sym.st_value + r.r_addend == vaddr: - return r - return None - -class MdebugSection(Section): - """ - MIPS Debugging Section - """ - def __init__(self, header, elf_file, index): - super().__init__(header, elf_file, index) - self.parent = self.elf_file - self.hdrr = EcoffHDRR(self.data) - - self.fdrs = [] - for i in range(self.hdrr.ifdMax): - fdr = EcoffFdr.from_binary(self, i) - self.fdrs.append(fdr) - - def late_init(self): - if self.late_init_done: - return - super().late_init() - for fdr in self.fdrs: - fdr.late_init() - - def fdr_forname(self, filename): - for fdr in self.fdrs: - # remove path and file ext - normalized_name = ".".join(fdr.name.split("/")[-1].split(".")[:-1]) - - if normalized_name == filename: - return fdr - return None - - def fdr_foraddr(self, addr, extensions=('.c')): - for fdr in self.fdrs: - if fdr.adr == addr and any((fdr.name.endswith(ext) for ext in extensions)): - return fdr - return None - - def read_string(self, index): - to = self.elf_file.data.find(b'\0', self.hdrr.cbSsOffset + index) - assert to != -1 - return self.elf_file.data[self.hdrr.cbSsOffset + index:to].decode("ASCII") - - def read_ext_string(self, index): - to = self.elf_file.data.find(b'\0', self.hdrr.cbSsExtOffset + index) - assert to != -1 - return self.elf_file.data[self.hdrr.cbSsExtOffset + index:to].decode("ASCII") - -class ReginfoSection(Section): - """ - MIPS Register Information Section - """ - def __init__(self, header, elf_file, index): - super().__init__(header, elf_file, index) - -# ===================================================================================================== -# Elf File -# ===================================================================================================== - -class ElfFile: - def __init__(self, data): - def init_section(i): - offset = self.elf_header.e_shoff + i * self.elf_header.e_shentsize - section_type = struct.unpack(">I", data[offset + 4:][:4])[0] - header_data = data[offset:][:self.elf_header.e_shentsize] - - if section_type == SHT_REL or section_type == SHT_RELA: - return RelocationSection(header_data, self, i) - elif section_type == SHT_SYMTAB: - return SymtabSection(header_data, self, i) - elif section_type == SHT_STRTAB: - return StrtabSection(header_data, self, i) - elif section_type == SHT_MIPS_DEBUG: - return MdebugSection(header_data, self, i) - elif section_type == SHT_MIPS_REGINFO: - return ReginfoSection(header_data, self, i) - else: - return Section(header_data, self, i) - - self.data = data - self.elf_header = ElfHeader(data[0:52]) - - num_progheaders = self.elf_header.e_phnum - num_sections = self.elf_header.e_shnum - - # Init program headers - self.progheaders = [] - for i in range(num_progheaders): - offset = self.elf_header.e_phoff + i * self.elf_header.e_phentsize - self.progheaders.append(ProgramHeader(data[offset:][:self.elf_header.e_phentsize], self.elf_header.e_ident[EI_CLASS])) - - # Init sections - self.sections = [] - for i in range(num_sections): - self.sections.append(init_section(i)) - - # Init shstrtab and name sections - self.shstrtab = self.sections[self.elf_header.e_shstrndx] - assert isinstance(self.shstrtab, StrtabSection) - - for s in self.sections: - s.name = self.shstrtab.lookup_str(s.sh_name) - - # Init symtab - symtab = None - for s in self.sections: - if s.sh_type == SHT_SYMTAB: - assert not symtab , "Found more than one symtab section?" - symtab = s - self.symtab = symtab - if self.symtab is not None: - self.symtab.late_init() - - # Late Init sections - for s in self.sections: - s.late_init() - - def find_section_by_name(self, name): - for s in self.sections: - if s.name == name: - return s - return None - - def find_section_by_type(self, type): - for s in self.sections: - if s.sh_type == type: - return s - return None - -### Tests - -if __name__ == "__main__": - import sys - - elf_file = None - - with open(sys.argv[1], "rb") as elf: - elf_file = ElfFile(bytearray(elf.read())) - - # Header Info Test - print(elf_file.elf_header) - # Program Headers Info Test - print("") - print("Program Headers:") - print(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align") - for phdr in elf_file.progheaders: - print(phdr) - # Section Headers Info Test - print("") - print("Section Headers:") - print(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al") - for i,s in enumerate(elf_file.sections,0): - print(f" [{i:2}] {s}") - # Symbols Info Test - if elf_file.symtab is not None: - print("") - print(f"Symbol table '{elf_file.symtab.name}' contains {len(elf_file.symtab.symbol_entries)} entries") - print(" Num: Value Size Type Bind Vis Ndx Name") - for i,sym in enumerate(elf_file.symtab.symbol_entries,0): - print(f"{i:6}: {sym}") - # Relocations Info Test - print("") - for s in elf_file.sections: - if s.is_rel(): - print(f"\nRelocation section '{s.name}' at offset 0x{s.sh_offset:06X} contains {len(s.relocations)} entries:") - print(" Offset Info Type Sym.Value Sym.Name + Addend") - for reloc in s.relocations: - print(f"{reloc}") - # mdebug Info Test - print("") - mdebug_section = elf_file.find_section_by_type(SHT_MIPS_DEBUG) - if mdebug_section is not None: - """ - for fdr in mdebug_section.fdrs: - print(fdr) - for symr in fdr.symrs: - print(symr) - for pdr in fdr.pdrs: - print(pdr) - for symr in pdr.symrs: - print(symr) - for liner in pdr.lines: - print(liner) - """ - for fdr in mdebug_section.fdrs: - print(fdr.c_str()) diff --git a/tools/m2ctx.py b/tools/m2ctx.py deleted file mode 100755 index 39c5dc5..0000000 --- a/tools/m2ctx.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import os -import sys -import subprocess -import tempfile - -script_dir = os.path.dirname(os.path.realpath(__file__)) -root_dir = os.path.abspath(os.path.join(script_dir, "..")) -src_dir = root_dir + "src/" - -# Project-specific -CPP_FLAGS = [ - "-Iinclude", - "-Iinclude/PR", - "-Iinclude/gcc", - "-Isrc", - "-Isrc/libc", - "-Iver/current/build/include", - "-D_LANGUAGE_C", - "-DF3DEX_GBI_2", - "-D_MIPS_SZLONG=32", - "-DSCRIPT(...)={}" - "-D__attribute__(...)=", - "-D__asm__(...)=", - "-ffreestanding", - "-DM2CTX", -] - -def import_c_file(in_file) -> str: - in_file = os.path.relpath(in_file, root_dir) - cpp_command = ["gcc", "-E", "-P", "-dM", *CPP_FLAGS, in_file] - cpp_command2 = ["gcc", "-E", "-P", *CPP_FLAGS, in_file] - - with tempfile.NamedTemporaryFile(suffix=".c") as tmp: - stock_macros = subprocess.check_output(["gcc", "-E", "-P", "-dM", tmp.name], cwd=root_dir, encoding="utf-8") - - out_text = "" - try: - out_text += subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8") - out_text += subprocess.check_output(cpp_command2, cwd=root_dir, encoding="utf-8") - except subprocess.CalledProcessError: - print( - "Failed to preprocess input file, when running command:\n" - + cpp_command, - file=sys.stderr, - ) - sys.exit(1) - - if not out_text: - print("Output is empty - aborting") - sys.exit(1) - - for line in stock_macros.strip().splitlines(): - out_text = out_text.replace(line + "\n", "") - return out_text - -def main(): - parser = argparse.ArgumentParser( - description="""Create a context file which can be used for mips_to_c""" - ) - parser.add_argument( - "c_file", - help="""File from which to create context""", - ) - args = parser.parse_args() - - output = import_c_file(args.c_file) - - with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f: - f.write(output) - - -if __name__ == "__main__": - main() diff --git a/tools/mdebug.py b/tools/mdebug.py deleted file mode 100644 index 6f061ec..0000000 --- a/tools/mdebug.py +++ /dev/null @@ -1,938 +0,0 @@ -#!/usr/bin/env python3 -# -# .mdebug section -# - -""" -References: -https://www.cs.unibo.it/~solmi/teaching/arch_2002-2003/AssemblyLanguageProgDoc.pdf -https://web.archive.org/web/20010628021622/http://reality.sgi.com/davea/objectinfo.html -https://github.com/astrelsky/ghidra_mdebug -https://github.com/Rozelette/print-mdebug/blob/master/print_mdebug.py -https://opensource.apple.com/source/gcc_legacy/gcc_legacy-938/gcc/mips-tdump.c.auto.html -https://github.com/uhhpctools/openuh/blob/master/osprey-gcc-4.2.0/gcc/mips-tdump.c -https://github.com/bminor/binutils-gdb/blob/master/gdb/mdebugread.c - -(stabs docs): -https://sourceware.org/gdb/current/onlinedocs/stabs.html - -(ecoff docs): -https://web.archive.org/web/20160305114748/http://h41361.www4.hp.com/docs/base_doc/DOCUMENTATION/V50A_ACRO_SUP/OBJSPEC.PDF -https://chromium.googlesource.com/native_client/nacl-toolchain/+/refs/tags/gcc-4.4.3/binutils/gas/ecoff.c -https://kernel.googlesource.com/pub/scm/linux/kernel/git/hjl/binutils/+/hjl/secondary/include/coff/sym.h -https://kernel.googlesource.com/pub/scm/linux/kernel/git/hjl/binutils/+/hjl/secondary/include/coff/symconst.h -""" - -from enum import IntEnum -import struct - -class EcoffBt(IntEnum): # Basic Type - NIL = 0 # - ADR = 1 # pointer-sized integer type - CHAR = 2 # char - UCHAR = 3 # unsigned char - SHORT = 4 # short - USHORT = 5 # unsigned short - INT = 6 # int - UINT = 7 # unsigned int - LONG = 8 # long - ULONG = 9 # unsigned long - FLOAT = 10 # float - DOUBLE = 11 # double - STRUCT = 12 # struct - UNION = 13 # union - ENUM = 14 # enum - TYPEDEF = 15 # type definition - RANGE = 16 # subrange of int - SET = 17 # pascal set - COMPLEX = 18 # FORTRAN complex - DCOMPLEX = 19 # FORTRAN double com[plex - INDIRECT = 20 # forward or unnamed typedef - FIXEDDEC = 21 # Fixed point decimal - FLOATDEC = 22 # Floating point decimal - STRING = 23 # Varying length character string - BIT = 24 # Aligned bit tring - PICTURE = 25 # picture - VOID = 26 # void - LONGLONG = 27 # long long int - ULONGLONG = 28 # unsigned long long int - LONG64 = 30 # - ULONG64 = 31 # - LONGLONG64 = 32 # - ULONGLONG64 = 33 # - ADR64 = 34 # - INT64 = 35 # - UINT64 = 36 # - - AGGREGATE = 63 # not a basic type - MAX = 64 # - -class EcoffSc(IntEnum): - NIL = 0 - TEXT = 1 # .text symbol - DATA = 2 # .data symbol - BSS = 3 # .bss symbol - REGISTER = 4 # value of symbol is register number - ABS = 5 # value of symbol is absolute - UNDEFINED = 6 # value of symbol is undefined - CDBLOCAL = 7 # variable value is in se->va.?? - BITS = 8 # variable is a bit field - CDBSYSTEM = 9 # variable value is in cdb address space - REGIMAGE = 10 # register value is saved on stack - INFO = 11 # symbol contains debugger information - USERSTRUCT = 12 # address in struct user for current process - SDATA = 13 # load time only small data - SBSS = 14 # load time only small common - RDATA = 15 # load time only read-only data - VAR = 16 # var parameter (FORTRAN, Pascal) - COMMON = 17 # common variable - SCOMMON = 18 # small common - VARREGISTER = 19 # var parameter in a register - VARIANT = 20 # variant record - SUNDEFINED = 21 # small undefined (external) data - INIT = 22 # .init section symbol - BASEDVAR = 23 # FORTRAN or PL/1 ptr based var - XDATA = 24 # exception handling data - PDATA = 25 # procedure section - FINI = 26 # .fini section - RCONST = 27 # .rconst section - MAX = 32 # - -class EcoffSt(IntEnum): - NIL = 0 # - GLOBAL = 1 # external symbol - STATIC = 2 # static symbol - PARAM = 3 # procedure argument - LOCAL = 4 # local variable - LABEL = 5 # label - PROC = 6 # procedure - BLOCK = 7 # beginning of block - END = 8 # end of something - MEMBER = 9 # member of struct/union/enum/.. - TYPEDEF = 10 # type definition - FILE = 11 # filename - REGRELOC = 12 # register relocation - FORWARD = 13 # forwarding address - STATICPROC = 14 # load time only static procedures - # (CONSTANT and STAPARAM are in different orders between different sources...) - CONSTANT = 15 # constant - STAPARAM = 16 # FORTRAN static parameters - STRUCT = 26 # structure - UNION = 27 # union - ENUM = 28 # enum - INDIRECT = 34 # - -class EcoffTq(IntEnum): # Type qualifier - NIL = 0 # - PTR = 1 # pointer - PROC = 2 # procedure - ARRAY = 3 # array - FAR = 4 # longer addressing - VOL = 5 # volatile - CONST = 6 # constant - MAX = 8 # - - UNK7 = 7 # invalid - UNK9 = 9 # invalid - UNK10 = 10 # invalid - UNK11 = 11 # invalid - UNK12 = 12 # invalid - UNK13 = 13 # invalid - UNK14 = 14 # invalid - UNK = 15 # invalid - -class EcoffLanguageCode(IntEnum): - C = 0 - PASCAL = 1 - FORTRAN = 2 - ASM = 3 - MACHINE = 4 - NIL = 5 - ADA = 6 - PL1 = 7 - COBOL = 8 - STDC = 9 - CPLUSPLUSV2 = 10 - MAX = 11 - -def get_bitrange(value, start, length): - return (value >> start) & ((1 << length) - 1) - -def sign_extend_16(value): - return (value & 0x7FFF) - (value & 0x8000) - -def sign_extend_4(value): - return (value & 0x7) - (value & 0x8) - -class EcoffLiner: - """ - ECOFF Line Numbers Mapping - - typedef struct sLINER { - s32 count : 4; - s32 delta : 4; - } tLINER, *pLINER; - """ - - def __init__(self, data) -> None: - self.count = get_bitrange(data[0], 0, 4) + 1 - self.delta = get_bitrange(data[0], 4, 4) - - if self.delta == 8: - self.is_extended = True - self.delta = sign_extend_16((data[1] << 8) | data[2]) - self.data = data[:3] - else: - self.is_extended = False - self.delta = sign_extend_4(self.delta) - self.data = data[:1] - - def __str__(self) -> str: - return f"""= EcoffLiner ============= -delta = {self.delta} -count = {self.count} -extended = {self.is_extended}""" - -class EcoffTir: - """ - ECOFF Type Information Record - - typedef struct { -#ifdef LITTLE_ENDIAN - u32 tq3 : 4; - u32 tq2 : 4; - u32 tq1 : 4; /* 6 type qualifiers - tqPtr, etc. */ - u32 tq0 : 4; - /* ---- 16 bit boundary ---- */ - u32 tq5 : 4; - u32 tq4 : 4; - u32 bt : 6; /* basic type */ - u32 continued : 1; /* indicates additional TQ info in next AUX */ - u32 fBitfield : 1; /* set if bit width is specified */ -#else - u32 fBitfield : 1; /* set if bit width is specified */ - u32 continued : 1; /* indicates additional TQ info in next AUX */ - u32 bt : 6; /* basic type */ - u32 tq4 : 4; - u32 tq5 : 4; - /* ---- 16 bit boundary ---- */ - u32 tq0 : 4; - u32 tq1 : 4; /* 6 type qualifiers - tqPtr, etc. */ - u32 tq2 : 4; - u32 tq3 : 4; -#endif - } TIR, *pTIR; // size = 4 - """ - SIZE = 4 - - def __init__(self, data, endian) -> None: - if endian == 1: - self.tq3 = EcoffTq(get_bitrange(data, 0, 4)) - self.tq2 = EcoffTq(get_bitrange(data, 4, 4)) - self.tq1 = EcoffTq(get_bitrange(data, 8, 4)) - self.tq0 = EcoffTq(get_bitrange(data, 12, 4)) - self.tq5 = EcoffTq(get_bitrange(data, 16, 4)) - self.tq4 = EcoffTq(get_bitrange(data, 20, 4)) - self.bt = EcoffBt(get_bitrange(data, 24, 6)) - self.continued = get_bitrange(data, 30, 1) - self.fBitfield = get_bitrange(data, 31, 1) - else: - self.fBitfield = get_bitrange(data, 0, 1) - self.continued = get_bitrange(data, 1, 1) - self.bt = EcoffBt(get_bitrange(data, 2, 6)) - self.tq4 = EcoffTq(get_bitrange(data, 8, 4)) - self.tq5 = EcoffTq(get_bitrange(data, 12, 4)) - self.tq0 = EcoffTq(get_bitrange(data, 16, 4)) - self.tq1 = EcoffTq(get_bitrange(data, 20, 4)) - self.tq2 = EcoffTq(get_bitrange(data, 24, 4)) - self.tq3 = EcoffTq(get_bitrange(data, 28, 4)) - self.tqs = (self.tq0, self.tq1, self.tq2, self.tq3, self.tq4, self.tq5) - - def __str__(self) -> str: - return f"""= EcoffTIR ============== -fBitfield = {self.fBitfield} -continued = {self.continued} -bt = {self.bt.name} -tq4 = {self.tq4} -tq5 = {self.tq5} -tq0 = {self.tq0} -tq1 = {self.tq1} -tq2 = {self.tq2} -tq3 = {self.tq3}""" - -class EcoffRNDXR: - """ - typedef struct { -#ifdef LITTLE_ENDIAN - u32 index : 20; /* index int sym/aux/iss tables */ - u32 rfd : 12; /* index into the file indirect table */ -#else - u32 rfd : 12; /* index into the file indirect table */ - u32 index : 20; /* index int sym/aux/iss tables */ -#endif - } RNDXR, *pRNDXR; // size = 4 - """ - SIZE = 4 - - def __init__(self, data, endian) -> None: - if endian == 1: - self.index = get_bitrange(data, 0, 20) - self.rfd = get_bitrange(data, 20, 12) - else: - self.rfd = get_bitrange(data, 0, 12) - self.index = get_bitrange(data, 12, 20) - - def __str__(self) -> str: - return f"""= EcoffRNDXR ============== -index = {self.index} -rfd = {self.rfd}""" - -class EcoffAux: - """ - typedef union __sgi_auxu_u { - TIR ti; /* type information record */ - RNDXR rndx; /* relative index into symbol table */ - long_i dnLow; /* low dimension of array */ - long_i dnHigh; /* high dimension of array */ - long_i isym; /* symbol table index (end of proc) */ - long_i iss; /* index into string space (not used) */ - long_i width; /* width for non-default sized struct fields */ - long_i count; /* count of ranges for variant arm */ - } AUXU, *pAUXU; // size = 4 - """ - SIZE = 4 - - def __init__(self, fdr, data, endian) -> None: - data = struct.unpack((">" if endian == 1 else "<") + "I", data)[0] - self.ti = EcoffTir(data, endian) - self.rndx = EcoffRNDXR(data, endian) - self.dnLow = self.dnHigh = self.isym = self.iss = self.width = self.count = data - self.fdr = fdr - - def __str__(self) -> str: - return f"""= EcoffAux ============== -ti = {{ -{self.ti} -}} -rndx = {{ -{self.rndx} -}} -dnLow = {self.dnLow:04X} -dnHigh = {self.dnHigh:04X} -isym = {self.isym:04X} -iss = {self.iss:04X} -width = {self.width:04X} -count = {self.count:04X}""" - -class EcoffSymr: - """ - ECOFF Local Symbol - - typedef struct sSymr { - s32 iss; /* index into String Space of name */ - s32 value; /* symbol value */ - /* value can be an address, size or frame offset depending on symbol type */ - EcoffSt st : 6; /* symbol type */ - EcoffSc sc : 5; /* storage class - text, data, etc */ - s32 _reserved : 1; /* reserved bit */ - s32 index : 20; /* index into sym/aux table */ - } tSymr, *pSymr; // size = 0xC - """ - SIZE = 0xC - - def __init__(self, parent, idx, data): - self.idx = idx - self.parent = parent # can be either Fdr or Pdr - self.pdr = parent if type(parent) == EcoffPdr else None - self.fdr = self.pdr.parent if self.pdr is not None else parent - - self.data = data[:EcoffSymr.SIZE] - - self.iss, self.value, bits = struct.unpack(">III", self.data) - self.st = EcoffSt(get_bitrange(bits, 26, 6)) - self.sc = EcoffSc(get_bitrange(bits, 21, 5)) - self._reserved = get_bitrange(bits, 20, 1) - self.index = get_bitrange(bits, 0, 20) - - self.name = self.fdr.read_string(self.iss) - self.type_name = None - - self.c_repr = None - - assert self._reserved == 0 # Sanity check - - def link_syms(self): - if self.st == EcoffSt.END: - self.start_sym = self.fdr.symrs[self.index] - elif self.st in [EcoffSt.BLOCK, EcoffSt.FILE, EcoffSt.STRUCT, EcoffSt.UNION, EcoffSt.ENUM]: - self.end_sym = self.fdr.symrs[self.index - 1] - elif self.st in [EcoffSt.PROC, EcoffSt.STATICPROC]: - aux = self.fdr.auxs[self.index] - self.end_sym = self.fdr.symrs[aux.isym - 1] - elif self.st in [EcoffSt.GLOBAL, EcoffSt.STATIC, EcoffSt.PARAM, EcoffSt.LOCAL, EcoffSt.MEMBER, EcoffSt.TYPEDEF, EcoffSt.FORWARD]: - pass - - def late_init(self): - if self.st == EcoffSt.END: - """ - END symbols index the associated begin symbol - """ - self.start_sym = self.fdr.symrs[self.index] - if self.start_sym.st == EcoffSt.BLOCK: - self.c_repr = "}" - elif self.start_sym.st == EcoffSt.FILE: - self.c_repr = f"// end of file: \"{self.start_sym.name}\"" - elif self.start_sym.st in [EcoffSt.STRUCT, EcoffSt.UNION, EcoffSt.ENUM]: - self.c_repr = "}" - if len(self.start_sym.type_name) != 0: - self.c_repr += " " + self.start_sym.type_name - self.c_repr += f";" - elif self.st in [EcoffSt.BLOCK, EcoffSt.FILE, EcoffSt.STRUCT, EcoffSt.UNION, EcoffSt.ENUM]: - """ - These symbols index the first symbol after their associated END symbol - """ - self.end_sym = self.fdr.symrs[self.index - 1] - if self.st == EcoffSt.BLOCK: - self.c_repr = "{" - elif self.st == EcoffSt.FILE: - self.c_repr = f"#line 1 \"{self.name}\"" - elif self.st in [EcoffSt.STRUCT, EcoffSt.UNION, EcoffSt.ENUM]: - keyword = "" - self.type_name = "" - next_sym = self.fdr.symrs[self.index] - if next_sym.st == EcoffSt.TYPEDEF: - # possible typedef struct/union/enum - # TODO check this by ensuring the typedef symbol references this symbol - keyword += "typedef " - self.type_name = next_sym.name - - if self.st == EcoffSt.UNION: - keyword += "union" - elif self.st == EcoffSt.ENUM: - keyword += "enum" - else: - keyword += "struct" - - name = self.name - if len(name) != 0: - name = ' ' + name - - self.c_repr = f"{keyword}{name} {{" - elif self.st in [EcoffSt.PROC, EcoffSt.STATICPROC]: - aux1 = self.fdr.auxs[self.index] - self.end_sym = self.fdr.symrs[aux1.isym - 1] - - self.c_repr = "" - if self.st == EcoffSt.STATICPROC: - self.c_repr += "static " - - self.return_type, _ = self.process_type_information(1) - self.c_repr += self.return_type - if len(self.return_type) != 0: - self.c_repr += " " - self.c_repr += self.name - self.c_repr += "()" - elif self.st in [EcoffSt.GLOBAL, EcoffSt.STATIC, EcoffSt.PARAM, EcoffSt.LOCAL, EcoffSt.MEMBER, EcoffSt.FORWARD]: - self.c_repr = "" - if self.st == EcoffSt.MEMBER: - # value of a stMember is the offset in bits - self.c_repr += f"/* 0x{self.value//8:X} */ " - - type_str, bitwidth = self.process_type_information(0) - self.c_repr += type_str - if len(self.c_repr) != 0: - self.c_repr += " " - self.c_repr += f"{self.name}{f' : {bitwidth}' if bitwidth is not None else ''};" - elif self.st == EcoffSt.TYPEDEF: - # TODO the typedef may already be absorbed into a struct or similar, check before emitting - type_str, _ = self.process_type_information(0) - self.c_repr = f"typedef {type_str} {self.name};" - elif self.st == EcoffSt.LABEL: - self.c_repr = f"{self.name}:" - - def process_type_information(self, ind): - c_bt_names = { - EcoffBt.NIL : None, - EcoffBt.ADR : None, - EcoffBt.CHAR : "signed char", - EcoffBt.UCHAR : "char", - EcoffBt.SHORT : "short", - EcoffBt.USHORT : "unsigned short", - EcoffBt.INT : "int", - EcoffBt.UINT : "unsigned int", - EcoffBt.LONG : "long", - EcoffBt.ULONG : "unsigned long", - EcoffBt.FLOAT : "float", - EcoffBt.DOUBLE : "double", - EcoffBt.STRUCT : "struct", - EcoffBt.UNION : "union", - EcoffBt.ENUM : "enum", - EcoffBt.TYPEDEF : "typedef", - EcoffBt.RANGE : None, - EcoffBt.SET : None, - EcoffBt.COMPLEX : "complex", - EcoffBt.DCOMPLEX : "double complex", - EcoffBt.INDIRECT : None, - EcoffBt.FIXEDDEC : None, - EcoffBt.FLOATDEC : None, - EcoffBt.STRING : "const char*", - EcoffBt.BIT : None, - EcoffBt.PICTURE : None, - EcoffBt.VOID : "void", - EcoffBt.LONGLONG : "long long", - EcoffBt.ULONGLONG : "unsigned long long", - EcoffBt.LONG64 : "long", - EcoffBt.ULONG64 : "unsigned long", - EcoffBt.LONGLONG64 : "long long", - EcoffBt.ULONGLONG64 : "unsigned long long", - EcoffBt.ADR64 : None, - EcoffBt.INT64 : None, - EcoffBt.UINT64 : None, - } - c_tq_str = { - EcoffTq.NIL : "", - EcoffTq.PTR : "*", - EcoffTq.PROC : "()", - EcoffTq.ARRAY : "[]", - EcoffTq.FAR : "/* FAR */", - EcoffTq.VOL : "volatile", - EcoffTq.CONST : "const", - } - - if self.index == 0xFFFFF: - # no type info - return "", None - - aux = self.fdr.auxs[self.index + ind] - ind += 1 - type_str = "" - - bit_width = None - if aux.ti.fBitfield == 1: - bit_width = self.fdr.auxs[self.index + ind].isym - ind += 1 - - if aux.ti.bt in [EcoffBt.STRUCT, EcoffBt.UNION, EcoffBt.ENUM, EcoffBt.TYPEDEF]: - type_ref_aux = self.fdr.auxs[self.index + ind] - ind += 1 - - fd_ref_idx = type_ref_aux.rndx.rfd - if fd_ref_idx == 4095: - fd_ref_idx = self.fdr.auxs[self.index + ind].isym - ind += 1 - - fdr_ref = self.fdr.parent.fdrs[fd_ref_idx] - sym_ref = fdr_ref.symrs[type_ref_aux.rndx.index] - # now we have the reference to the stStruct, stUnion, stEnum, or stTypeDef - type_str += f"{sym_ref.type_name if sym_ref.type_name is not None else sym_ref.name}" - else: - type_str += f"{c_bt_names[aux.ti.bt]}" - - # TODO improve emitting qualified types - tqs = "" - for tq in aux.ti.tqs: - if tq == EcoffTq.NIL: - continue - if tq == EcoffTq.ARRAY: - ind += 2 # skips over some info such as the type of index (always int for C) - array_low_aux = self.fdr.auxs[self.index + ind] - array_high_aux = self.fdr.auxs[self.index + ind + 1] - stride_aux = self.fdr.auxs[self.index + ind + 2] - ind += 3 - tqs += "[" - if array_high_aux.dnHigh != 0xFFFFFFFF: - tqs += '%d' % (array_high_aux.dnHigh + 1) - tqs += "]" - else: - tqs += c_tq_str[tq] - tqs += " " - tqs = tqs.strip() - if len(tqs) != 0: - type_str += " " + tqs - - return type_str, bit_width - - def __str__(self) -> str: - return f"""= EcoffSymr ============== {self.idx} -iss = 0x{self.iss:08X} -value = 0x{self.value:08X} -st = st{self.st.name} -sc = sc{self.sc.name} -_reserved = {self._reserved} -index = 0x{self.index:05X} -name = {self.name}""" - -class EcoffPdr: - """ - ECOFF Procedure Descriptor - - typedef struct sPDR { - s32 addr; /* memory address of start of procedure */ - s32 isym; /* start of local symbol entries */ - s32 iline; /* start of line number entries */ - s32 regmask; /* save register mask */ - s32 regoffset; /* save register offset */ - s32 iopt; /* start of optimization symbol entries */ - s32 fregmask; /* save floating point register mask */ - s32 fregoffset; /* save floating point register offset */ - s32 frameoffset; /* frame size */ - u16 framereg; /* frame pointer register */ - u16 pcreg; /* offset or reg of return pc */ - s32 lnLow; /* lowest line in the procedure */ - s32 lnHigh; /* highest line in the procedure */ - s32 cbLineOffset; /* byte offset for this procedure from the fd base */ -#ifdef 64_BIT - // TODO there's a bitfield in here - s32 gpPrologue; /* byte size of GP prologue */ - s32 gpUsed; /* true if the procedure uses GP */ - s32 regFrame; /* true if register frame procedure */ - s32 prof; /* true if compiled with -pg */ - s32 localOffset; /* offset of local variables from vfp */ -#endif - } tPDR, *pPDR; // size = 0x34 - """ - SIZE = 0x34 - - def __init__(self, fdr, data) -> None: - self.parent = fdr - self.data = data[:EcoffPdr.SIZE] - - self.addr, self.isym, self.iline, self.regmask, \ - self.regoffset, self.iopt, self.fregmask, self.fregoffset, \ - self.frameoffset, self.framereg, self.pcreg, self.lnLow, \ - self.lnHigh, self.cbLineOffset = struct.unpack(">IIIIIIIIIHHIII", self.data) - - self.symrs = [] - - i = self.isym - symr = self.parent.symrs[i] - assert symr.st == EcoffSt.PROC or symr.st == EcoffSt.STATICPROC - # Inherit procedure name from procedure symbol - self.name = symr.name - - self.symrs.append(symr) - while not (symr.st == EcoffSt.END and symr.name == self.symrs[0].name): - i += 1 - symr = self.parent.symrs[i] - self.symrs.append(symr) - - assert symr.st == EcoffSt.END and symr.sc == EcoffSc.TEXT - self.size = symr.value # value field of an stEND and scTEXT symbol is the procedure size - assert self.size % 4 == 0 - - # indexed by asm word offset from proc start - self.lines = [] - - # ilineMax = self.parent.parent.hdrr.ilineMax - # cbLine = self.parent.parent.hdrr.cbLine - # cbLineOffset = self.parent.parent.hdrr.cbLineOffset - # ilineBase = self.parent.ilineBase - # cline = self.parent.cline - # cbLineOffset = self.parent.cbLineOffset - # cbLine = self.parent.cbLine - # lnLow = self.lnLow - # lnHigh = self.lnHigh - # iline = self.iline - - elf_data = self.parent.parent.parent.data - - line_no = self.lnLow # first line in the procedure - line_data = self.parent.parent.hdrr.cbLineOffset + self.parent.cbLineOffset + self.cbLineOffset - # line_end = self.parent.parent.hdrr.cbLineOffset + self.parent.cbLineOffset + self.parent.cbLine - - # print(self) - # print(f"{self.name} [{self.lnLow}:{self.lnHigh}]") - # print(self.size//4) - while len(self.lines) < self.size//4: - # assert line_data < line_end , "Overflow in line numbers table" - - liner = EcoffLiner(elf_data[line_data:]) - line_no += liner.delta - # if line_no < self.lnLow or line_no > self.lnHigh: - # break - - # print(liner) - for i in range(liner.count): - # print(f"[{len(self.lines)}] {line_no}") - self.lines.append(line_no) - - line_data += len(liner.data) - - def lookup_sym(self, value, type=-1): - for sym in self.symrs: - if sym.value == value and (type == -1 or type == sym.st): - return sym - return None - - def __str__(self) -> str: - return f"""= EcoffPdr =============== -addr = 0x{self.addr:08X} -isym = 0x{self.isym:08X} -iline = 0x{self.iline:08X} -regmask = 0b{self.regmask:032b} -regoffset = 0x{self.regoffset:08X} -iopt = 0x{self.iopt:08X} -fregmask = 0b{self.fregmask:032b} -fregoffset = 0x{self.fregoffset:08X} -frameoffset = 0x{self.frameoffset:08X} -framereg = ${self.framereg} -pcreg = ${self.pcreg} -lnLow = {self.lnLow} -lnHigh = {self.lnHigh} -cbLineOffset = 0x{self.cbLineOffset:08X} -name = {self.name}""" - -class EcoffFdr: - """ - ECOFF File Descriptor - - typedef struct sFDR { - u32 adr; /* memory address of beginning of file */ - s32 rss; /* file name (of source, if known) */ - s32 issBase; /* file's string space */ - s32 cbSs; /* number of bytes in the ss */ - s32 isymBase; /* beginning of symbols */ - s32 csym; /* count file's of symbols */ - s32 ilineBase; /* file's line symbols */ - s32 cline; /* count of file's line symbols */ - s32 ioptBase; /* file's optimization entries */ - s32 copt; /* count of file's optimization entries */ - u16 ipdFirst; /* start of procedures for this file */ - u16 cpd; /* count of procedures for this file */ - s32 iauxBase; /* file's auxiliary entries */ - s32 caux; /* count of file's auxiliary entries */ - s32 rfdBase; /* index into the file indirect table */ - s32 crfd; /* count file indirect entries */ - EcoffLanguageCode lang : 5; /* language for this file */ - u32 fMerge : 1; /* whether this file can be merged */ - u32 fReadin : 1; /* true if it was read in (not just created) */ - u32 fBigEndian : 1; /* true if AUXU's are big endian */ - u32 glevel : 2; /* level this file was compiled with */ - u32 _reserved : 20; /* reserved bits */ - s32 cbLineOffset; /* byte offset from header for this file ln's */ - s32 cbLine; /* size of lines for this file */ - } tFDR, *pFDR; // size = 0x48 - """ - SIZE = 0x48 - - @staticmethod - def from_binary(mdebug, i): - # Init - if 'init' not in EcoffFdr.from_binary.__dict__: - EcoffFdr.from_binary.cache = {} - EcoffFdr.from_binary.init = True - # Parent Init - if mdebug not in EcoffFdr.from_binary.cache: - EcoffFdr.from_binary.cache[mdebug] = {} - # Cache hit - if i in EcoffFdr.from_binary.cache[mdebug]: - return EcoffFdr.from_binary.cache[mdebug][i] - # Cache miss - cbFdOffset = mdebug.hdrr.cbFdOffset - elf_data = mdebug.parent.data - EcoffFdr.from_binary.cache[mdebug][i] = EcoffFdr(mdebug, elf_data[cbFdOffset+i*EcoffFdr.SIZE:cbFdOffset+(i+1)*EcoffFdr.SIZE]) - return EcoffFdr.from_binary.cache[mdebug][i] - - def __init__(self, mdebug, data) -> None: - self.parent = mdebug - self.data = data[:EcoffFdr.SIZE] - - self.adr, self.rss, self.issBase, self.cbSs, \ - self.isymBase, self.csym, self.ilineBase, self.cline, \ - self.ioptBase, self.copt, self.ipdFirst, self.cpd, \ - self.iauxBase, self.caux, self.rfdBase, self.crfd, \ - bits, self.cbLineOffset, self.cbLine = struct.unpack(">IIIIIIIIIIHHIIIIIII", self.data) - - self.lang = EcoffLanguageCode(get_bitrange(bits, 27, 5)) - self.fMerge = get_bitrange(bits, 26, 1) - self.fReadin = get_bitrange(bits, 25, 1) - self.fBigEndian = get_bitrange(bits, 24, 1) - self.glevel = get_bitrange(bits, 22, 2) - self._reserved = get_bitrange(bits, 2, 20) - - self.name = self.parent.read_string(self.issBase + self.rss) - - # print(self) - - hdrr = self.parent.hdrr - elf_data = self.parent.parent.data - - # Aux Symbols - self.auxs = [] - for i in range(self.caux): - i += self.iauxBase - assert i < hdrr.iauxMax , "Out of bounds in Auxiliary Symbol Table" - aux = EcoffAux(self, elf_data[hdrr.cbAuxOffset+i*EcoffAux.SIZE:][:EcoffAux.SIZE], self.fBigEndian) - self.auxs.append(aux) - - # Symbols - self.symrs = [] - for i in range(self.csym): - j = i + self.isymBase - assert j < hdrr.isymMax , "Out of bounds in Local Symbol Table" - symr = EcoffSymr(self, i, elf_data[hdrr.cbSymOffset+j*EcoffSymr.SIZE:hdrr.cbSymOffset+(j+1)*EcoffSymr.SIZE]) - self.symrs.append(symr) - for symr in self.symrs: - symr.link_syms() - - # PDRs - self.pdrs = [] - for i in range(self.cpd): - i += self.ipdFirst - assert i < hdrr.ipdMax , "Out of bounds in Procedure Descriptor Table" - pdr = EcoffPdr(self, elf_data[hdrr.cbPdOffset+i*EcoffPdr.SIZE:hdrr.cbPdOffset+(i+1)*EcoffPdr.SIZE]) - self.pdrs.append(pdr) - - self.size = sum([pdr.size for pdr in self.pdrs]) - - def late_init(self): - for symr in self.symrs: - symr.late_init() - - def pdr_forname(self, procedure_name): - for pdr in self.pdrs: - if pdr.name == procedure_name: - return pdr - return None - - def pdr_foranyaddr(self, addr): - for pdr in self.pdrs: - if pdr.addr <= addr and pdr.addr + pdr.size > addr: - return pdr - return None - - def pdr_foraddr(self, addr): - for pdr in self.pdrs: - if pdr.addr == addr: - return pdr - return None - - def read_string(self, index): - return self.parent.read_string(self.issBase + index) - - def c_str(self) -> str: - """ - C prettyprint file - """ - def print_symbol(symr): - return f"{symr.st.name} :: {symr.c_repr}" - - indent = 0 - out = f"File: {self.name}\n" - - for symr in self.symrs: - if symr.st in [EcoffSt.END]: - indent -= 2 - - out += " " * indent - out += print_symbol(symr) - out += "\n" - - if symr.st in [EcoffSt.FILE, EcoffSt.STRUCT, EcoffSt.UNION, EcoffSt.PROC, EcoffSt.STATICPROC, EcoffSt.BLOCK]: - indent += 2 - - return out - - def __str__(self) -> str: - return f"""= EcoffFdr =============== -adr = 0x{self.adr:08X} -rss = 0x{self.rss:08X} -issBase = 0x{self.issBase:08X} -cbSs = 0x{self.cbSs:08X} -isymBase = 0x{self.isymBase:08X} -csym = 0x{self.csym:08X} -ilineBase = 0x{self.ilineBase:08X} -cline = 0x{self.cline:08X} -ioptBase = 0x{self.ioptBase:08X} -copt = 0x{self.copt:08X} -ipdFirst = 0x{self.ipdFirst:08X} -cpd = 0x{self.cpd:08X} -iauxBase = 0x{self.iauxBase:08X} -caux = 0x{self.caux:08X} -rfdBase = 0x{self.rfdBase:08X} -crfd = 0x{self.crfd:08X} -lang = {self.lang.name} -fMerge = {bool(self.fMerge)} -fReadin = {bool(self.fReadin)} -fBigEndian = {bool(self.fBigEndian)} -glevel = {self.glevel} -_reserved = {self._reserved} -cbLineOffset = 0x{self.cbLineOffset:08X} -cbLine = 0x{self.cbLine:08X} -name = {self.name}""" - -class EcoffHDRR: - """ - Symbolic Header - - typedef struct sHDRR { - u16 magic; /* 0x7009 */ - u16 vstamp; /* version stamp */ - s32 ilineMax; /* number of line number entries */ - s32 cbLine; /* number of bytes for line number entries */ - s32 cbLineOffset; /* offset to start of line number entries */ - s32 idnMax; /* max index into dense number table */ - s32 cbDnOffset; /* offset to start dense number table */ - s32 ipdMax; /* number of procedures */ - s32 cbPdOffset; /* offset to procedure descriptor table */ - s32 isymMax; /* number of local symbols */ - s32 cbSymOffset; /* offset to start of local symbols */ - s32 ioptMax; /* max index into optimization symbol entries */ - s32 cbOptOffset; /* offset to optimization symbol entries */ - s32 iauxMax; /* number of auxillary symbol entries */ - s32 cbAuxOffset; /* offset to start of auxillary symbol entries */ - s32 issMax; /* max index into local strings */ - s32 cbSsOffset; /* offset to start of local strings */ - s32 issExtMax; /* max index into external strings */ - s32 cbSsExtOffset; /* offset to start of external strings */ - s32 ifdMax; /* number of file descriptor entries */ - s32 cbFdOffset; /* offset to file descriptor table */ - s32 crfd; /* number of relative file descriptor entries */ - s32 cbRfdOffset; /* offset to relative file descriptor table */ - s32 iextMax; /* max index into external symbols */ - s32 cbExtOffset; /* offset to start of external symbol entries */ - } tHDRR, *pHDRR; // size = 0x60 - """ - HDRR_MAGIC = 0x7009 - SIZE = 0x60 - - def __init__(self, data) -> None: - self.data = data[:EcoffHDRR.SIZE] - - self.magic, self.vstamp, self.ilineMax, self.cbLine, \ - self.cbLineOffset, self.idnMax, self.cbDnOffset, self.ipdMax, \ - self.cbPdOffset, self.isymMax, self.cbSymOffset, self.ioptMax, \ - self.cbOptOffset, self.iauxMax, self.cbAuxOffset, self.issMax, \ - self.cbSsOffset, self.issExtMax, self.cbSsExtOffset, self.ifdMax, \ - self.cbFdOffset, self.crfd, self.cbRfdOffset, self.iextMax, \ - self.cbExtOffset = struct.unpack(">HHIIIIIIIIIIIIIIIIIIIIIII", self.data) - - assert self.magic == EcoffHDRR.HDRR_MAGIC , f"Symbolic Header magic value is incorrect. Got 0x{self.magic:04X}, expected 0x{EcoffHDRR.HDRR_MAGIC:04X}" - - def __str__(self) -> str: - return f"""= EcoffHDRR ============== -magic = 0x{self.magic:04X} -vstamp = 0x{self.vstamp:04X} -ilineMax = 0x{self.ilineMax:08X} -cbLine = 0x{self.cbLine:08X} -cbLineOffset = 0x{self.cbLineOffset:08X} -idnMax = 0x{self.idnMax:08X} -cbDnOffset = 0x{self.cbDnOffset:08X} -ipdMax = 0x{self.ipdMax:08X} -cbPdOffset = 0x{self.cbPdOffset:08X} -isymMax = 0x{self.isymMax:08X} -cbSymOffset = 0x{self.cbSymOffset:08X} -ioptMax = 0x{self.ioptMax:08X} -cbOptOffset = 0x{self.cbOptOffset:08X} -iauxMax = 0x{self.iauxMax:08X} -cbAuxOffset = 0x{self.cbAuxOffset:08X} -issMax = 0x{self.issMax:08X} -cbSsOffset = 0x{self.cbSsOffset:08X} -issExtMax = 0x{self.issExtMax:08X} -cbSsExtOffset = 0x{self.cbSsExtOffset:08X} -ifdMax = 0x{self.ifdMax:08X} -cbFdOffset = 0x{self.cbFdOffset:08X} -crfd = 0x{self.crfd:08X} -cbRfdOffset = 0x{self.cbRfdOffset:08X} -iextMax = 0x{self.iextMax:08X} -cbExtOffset = 0x{self.cbExtOffset:08X}""" diff --git a/tools/mips_isa.py b/tools/mips_isa.py deleted file mode 100644 index d99dcda..0000000 --- a/tools/mips_isa.py +++ /dev/null @@ -1,1309 +0,0 @@ -# TODO enum these constants -from enum import IntEnum, auto - -# Register IDs -class MipsGPReg(IntEnum): - R0 = 0 # 0 - AT = auto() # 1 - V0 = auto() # 2 - V1 = auto() # 3 - A0 = auto() # 4 - A1 = auto() # 5 - A2 = auto() # 6 - A3 = auto() # 7 - T0 = auto() # 8 - T1 = auto() # 9 - T2 = auto() # 10 - T3 = auto() # 11 - T4 = auto() # 12 - T5 = auto() # 13 - T6 = auto() # 14 - T7 = auto() # 15 - S0 = auto() # 16 - S1 = auto() # 17 - S2 = auto() # 18 - S3 = auto() # 19 - S4 = auto() # 20 - S5 = auto() # 21 - S6 = auto() # 22 - S7 = auto() # 23 - T8 = auto() # 24 - T9 = auto() # 25 - K0 = auto() # 26 - K1 = auto() # 27 - GP = auto() # 28 - SP = auto() # 29 - FP = auto() # 30 - RA = auto() # 31 - -class MipsFPReg(IntEnum): - F0 = 0 # 0 - F1 = auto() # 1 - F2 = auto() # 2 - F3 = auto() # 3 - F4 = auto() # 4 - F5 = auto() # 5 - F6 = auto() # 6 - F7 = auto() # 7 - F8 = auto() # 8 - F9 = auto() # 9 - F10 = auto() # 10 - F11 = auto() # 11 - F12 = auto() # 12 - F13 = auto() # 13 - F14 = auto() # 14 - F15 = auto() # 15 - F16 = auto() # 16 - F17 = auto() # 17 - F18 = auto() # 18 - F19 = auto() # 19 - F20 = auto() # 20 - F21 = auto() # 21 - F22 = auto() # 22 - F23 = auto() # 23 - F24 = auto() # 24 - F25 = auto() # 25 - F26 = auto() # 26 - F27 = auto() # 27 - F28 = auto() # 28 - F29 = auto() # 29 - F30 = auto() # 30 - F31 = auto() # 31 - -# Instruction Unique IDs -MIPS_INS_SLL = 0 -MIPS_INS_SRL = 1 -MIPS_INS_SRA = 2 -MIPS_INS_SLLV = 3 -MIPS_INS_SRLV = 4 -MIPS_INS_SRAV = 5 -MIPS_INS_JR = 6 -MIPS_INS_JALR = 7 -MIPS_INS_SYSCALL = 8 -MIPS_INS_BREAK = 9 -MIPS_INS_SYNC = 10 -MIPS_INS_MFHI = 11 -MIPS_INS_MTHI = 12 -MIPS_INS_MFLO = 13 -MIPS_INS_MTLO = 14 -MIPS_INS_DSLLV = 15 -MIPS_INS_DSRLV = 16 -MIPS_INS_DSRAV = 17 -MIPS_INS_MULT = 18 -MIPS_INS_MULTU = 19 -MIPS_INS_DIV = 20 -MIPS_INS_DIVU = 21 -MIPS_INS_DMULT = 22 -MIPS_INS_DMULTU = 23 -MIPS_INS_DDIV = 24 -MIPS_INS_DDIVU = 25 -MIPS_INS_ADD = 26 -MIPS_INS_ADDU = 27 -MIPS_INS_SUB = 28 -MIPS_INS_SUBU = 29 -MIPS_INS_AND = 30 -MIPS_INS_OR = 31 -MIPS_INS_XOR = 32 -MIPS_INS_NOR = 33 -MIPS_INS_SLT = 34 -MIPS_INS_SLTU = 35 -MIPS_INS_DADD = 36 -MIPS_INS_DADDU = 37 -MIPS_INS_DSUB = 38 -MIPS_INS_DSUBU = 39 -MIPS_INS_TGE = 40 -MIPS_INS_TGEU = 41 -MIPS_INS_TLT = 42 -MIPS_INS_TLTU = 43 -MIPS_INS_TEQ = 44 -MIPS_INS_TNE = 45 -MIPS_INS_DSLL = 46 -MIPS_INS_DSRL = 47 -MIPS_INS_DSRA = 48 -MIPS_INS_DSLL32 = 49 -MIPS_INS_DSRL32 = 50 -MIPS_INS_DSRA32 = 51 -MIPS_INS_BLTZ = 52 -MIPS_INS_BGEZ = 53 -MIPS_INS_BLTZL = 54 -MIPS_INS_BGEZL = 55 -MIPS_INS_TGEI = 56 -MIPS_INS_TGEIU = 57 -MIPS_INS_TLTI = 58 -MIPS_INS_TLTIU = 59 -MIPS_INS_TEQI = 60 -MIPS_INS_TNEI = 61 -MIPS_INS_BLTZAL = 62 -MIPS_INS_BGEZAL = 63 -MIPS_INS_BLTZALL = 64 -MIPS_INS_BGEZALL = 65 -MIPS_INS_J = 66 -MIPS_INS_JAL = 67 -MIPS_INS_BEQ = 68 -MIPS_INS_BNE = 69 -MIPS_INS_BLEZ = 70 -MIPS_INS_BGTZ = 71 -MIPS_INS_ADDI = 72 -MIPS_INS_ADDIU = 73 -MIPS_INS_SLTI = 74 -MIPS_INS_SLTIU = 75 -MIPS_INS_ANDI = 76 -MIPS_INS_ORI = 77 -MIPS_INS_XORI = 78 -MIPS_INS_LUI = 79 -MIPS_INS_MFC0 = 80 -MIPS_INS_MTC0 = 81 -MIPS_INS_TLBR = 82 -MIPS_INS_TLBWI = 83 -MIPS_INS_TLBWR = 84 -MIPS_INS_TLBP = 85 -MIPS_INS_ERET = 86 -MIPS_INS_MFC1 = 87 -MIPS_INS_DMFC1 = 88 -MIPS_INS_CFC1 = 89 -MIPS_INS_MTC1 = 90 -MIPS_INS_DMTC1 = 91 -MIPS_INS_CTC1 = 92 -MIPS_INS_BC1F = 93 -MIPS_INS_BC1T = 94 -MIPS_INS_BC1FL = 95 -MIPS_INS_BC1TL = 96 -MIPS_INS_ADD_S = 97 -MIPS_INS_SUB_S = 98 -MIPS_INS_MUL_S = 99 -MIPS_INS_DIV_S = 100 -MIPS_INS_SQRT_S = 101 -MIPS_INS_ABS_S = 102 -MIPS_INS_MOV_S = 103 -MIPS_INS_NEG_S = 104 -MIPS_INS_ROUND_L_S = 105 -MIPS_INS_TRUNC_L_S = 106 -MIPS_INS_CEIL_L_S = 107 -MIPS_INS_FLOOR_L_S = 108 -MIPS_INS_ROUND_W_S = 109 -MIPS_INS_TRUNC_W_S = 110 -MIPS_INS_CEIL_W_S = 111 -MIPS_INS_FLOOR_W_S = 112 -MIPS_INS_CVT_D_S = 113 -MIPS_INS_CVT_W_S = 114 -MIPS_INS_CVT_L_S = 115 -MIPS_INS_C_F_S = 116 -MIPS_INS_C_UN_S = 117 -MIPS_INS_C_EQ_S = 118 -MIPS_INS_C_UEQ_S = 119 -MIPS_INS_C_OLT_S = 120 -MIPS_INS_C_ULT_S = 121 -MIPS_INS_C_OLE_S = 122 -MIPS_INS_C_ULE_S = 123 -MIPS_INS_C_SF_S = 124 -MIPS_INS_C_NGLE_S = 125 -MIPS_INS_C_SEQ_S = 126 -MIPS_INS_C_NGL_S = 127 -MIPS_INS_C_LT_S = 128 -MIPS_INS_C_NGE_S = 129 -MIPS_INS_C_LE_S = 130 -MIPS_INS_C_NGT_S = 131 -MIPS_INS_ADD_D = 132 -MIPS_INS_SUB_D = 133 -MIPS_INS_MUL_D = 134 -MIPS_INS_DIV_D = 135 -MIPS_INS_SQRT_D = 136 -MIPS_INS_ABS_D = 137 -MIPS_INS_MOV_D = 138 -MIPS_INS_NEG_D = 139 -MIPS_INS_ROUND_L_D = 140 -MIPS_INS_TRUNC_L_D = 141 -MIPS_INS_CEIL_L_D = 142 -MIPS_INS_FLOOR_L_D = 143 -MIPS_INS_ROUND_W_D = 144 -MIPS_INS_TRUNC_W_D = 145 -MIPS_INS_CEIL_W_D = 146 -MIPS_INS_FLOOR_W_D = 147 -MIPS_INS_CVT_S_D = 148 -MIPS_INS_CVT_W_D = 149 -MIPS_INS_CVT_L_D = 150 -MIPS_INS_C_F_D = 151 -MIPS_INS_C_UN_D = 152 -MIPS_INS_C_EQ_D = 153 -MIPS_INS_C_UEQ_D = 154 -MIPS_INS_C_OLT_D = 155 -MIPS_INS_C_ULT_D = 156 -MIPS_INS_C_OLE_D = 157 -MIPS_INS_C_ULE_D = 158 -MIPS_INS_C_SF_D = 159 -MIPS_INS_C_NGLE_D = 160 -MIPS_INS_C_SEQ_D = 161 -MIPS_INS_C_NGL_D = 162 -MIPS_INS_C_LT_D = 163 -MIPS_INS_C_NGE_D = 164 -MIPS_INS_C_LE_D = 165 -MIPS_INS_C_NGT_D = 166 -MIPS_INS_CVT_S_W = 167 -MIPS_INS_CVT_D_W = 168 -MIPS_INS_CVT_S_L = 169 -MIPS_INS_CVT_D_L = 170 -MIPS_INS_BEQL = 171 -MIPS_INS_BNEL = 172 -MIPS_INS_BLEZL = 173 -MIPS_INS_BGTZL = 174 -MIPS_INS_DADDI = 175 -MIPS_INS_DADDIU = 176 -MIPS_INS_LDL = 177 -MIPS_INS_LDR = 178 -MIPS_INS_LB = 179 -MIPS_INS_LH = 180 -MIPS_INS_LWL = 181 -MIPS_INS_LW = 182 -MIPS_INS_LBU = 183 -MIPS_INS_LHU = 184 -MIPS_INS_LWR = 185 -MIPS_INS_LWU = 186 -MIPS_INS_SB = 187 -MIPS_INS_SH = 188 -MIPS_INS_SWL = 189 -MIPS_INS_SW = 190 -MIPS_INS_SDL = 191 -MIPS_INS_SDR = 192 -MIPS_INS_SWR = 193 -MIPS_INS_CACHE = 194 -MIPS_INS_LL = 195 -MIPS_INS_LWC1 = 196 -MIPS_INS_LWC2 = 197 -MIPS_INS_LLD = 198 -MIPS_INS_LDC1 = 199 -MIPS_INS_LDC2 = 200 -MIPS_INS_LD = 201 -MIPS_INS_SC = 202 -MIPS_INS_SWC1 = 203 -MIPS_INS_SWC2 = 204 -MIPS_INS_SCD = 205 -MIPS_INS_SDC1 = 206 -MIPS_INS_SDC2 = 207 -MIPS_INS_SD = 208 - -# RSP COP2 -MIPS_INS_VMULF = 209 -MIPS_INS_VMULU = 210 -MIPS_INS_VRNDP = 211 -MIPS_INS_VMULQ = 212 -MIPS_INS_VMUDL = 213 -MIPS_INS_VMUDM = 214 -MIPS_INS_VMUDN = 215 -MIPS_INS_VMUDH = 216 -MIPS_INS_VMACF = 217 -MIPS_INS_VMACU = 218 -MIPS_INS_VRNDN = 219 -MIPS_INS_VMACQ = 220 -MIPS_INS_VMADL = 221 -MIPS_INS_VMADM = 222 -MIPS_INS_VMADN = 223 -MIPS_INS_VMADH = 224 -MIPS_INS_VADD = 225 -MIPS_INS_VSUB = 226 -MIPS_INS_VABS = 227 -MIPS_INS_VADDC = 228 -MIPS_INS_VSUBC = 229 -MIPS_INS_VSAR = 230 -MIPS_INS_VLT = 231 -MIPS_INS_VEQ = 232 -MIPS_INS_VNE = 233 -MIPS_INS_VGE = 234 -MIPS_INS_VCL = 235 -MIPS_INS_VCH = 236 -MIPS_INS_VCR = 237 -MIPS_INS_VMRG = 238 -MIPS_INS_VAND = 239 -MIPS_INS_VNAND = 240 -MIPS_INS_VOR = 241 -MIPS_INS_VNOR = 242 -MIPS_INS_VXOR = 243 -MIPS_INS_VNXOR = 244 -MIPS_INS_VRCP = 245 -MIPS_INS_VRCPL = 246 -MIPS_INS_VRCPH = 247 -MIPS_INS_VMOV = 248 -MIPS_INS_VRSQ = 249 -MIPS_INS_VRSQL = 250 -MIPS_INS_VRSQH = 251 -MIPS_INS_VNOP = 252 -MIPS_INS_LBV = 253 -MIPS_INS_LSV = 254 -MIPS_INS_LLV = 255 -MIPS_INS_LDV = 256 -MIPS_INS_LQV = 257 -MIPS_INS_LRV = 258 -MIPS_INS_LPV = 259 -MIPS_INS_LUV = 260 -MIPS_INS_LHV = 261 -MIPS_INS_LFV = 262 -MIPS_INS_LTV = 263 -MIPS_INS_SBV = 264 -MIPS_INS_SSV = 265 -MIPS_INS_SLV = 266 -MIPS_INS_SDV = 267 -MIPS_INS_SQV = 268 -MIPS_INS_SRV = 269 -MIPS_INS_SPV = 270 -MIPS_INS_SUV = 271 -MIPS_INS_SHV = 272 -MIPS_INS_SFV = 273 -MIPS_INS_SWV = 274 -MIPS_INS_STV = 275 -MIPS_INS_MFC2 = 276 -MIPS_INS_MTC2 = 277 -MIPS_INS_CFC2 = 278 -MIPS_INS_CTC2 = 279 - -# Pseudo-Instruction Unique IDs -MIPS_INS_BEQZ = 280 -MIPS_INS_BNEZ = 281 -MIPS_INS_B = 282 -MIPS_INS_NOP = 283 -MIPS_INS_MOVE = 284 -MIPS_INS_NEGU = 285 -MIPS_INS_NOT = 286 - -# Invalid Instruction Unique ID -MIPS_INS_INVALID = -1 - -# Op IDs -# MIPS_OP_RS = 0 -# MIPS_OP_RT = 0 -# MIPS_OP_RD = 0 -# MIPS_OP_IMM = 0 - -# Instruction Groups - -MIPS_BRANCH_LIKELY_INSNS = [ - MIPS_INS_BEQL, MIPS_INS_BGEZALL, - MIPS_INS_BGEZL, MIPS_INS_BGTZL, - MIPS_INS_BLEZL, MIPS_INS_BLTZALL, - MIPS_INS_BLTZL, MIPS_INS_BNEL, - MIPS_INS_BC1TL, MIPS_INS_BC1FL, -] - -MIPS_BRANCH_INSNS = [ - *MIPS_BRANCH_LIKELY_INSNS, - - MIPS_INS_BEQ, - MIPS_INS_BGEZ, MIPS_INS_BGEZAL, - MIPS_INS_BGTZ, - MIPS_INS_BNE, - MIPS_INS_BLTZ, MIPS_INS_BLTZAL, - MIPS_INS_BLEZ, - MIPS_INS_BC1T, MIPS_INS_BC1F, - - MIPS_INS_BEQZ, - MIPS_INS_BNEZ, - MIPS_INS_B, -] - -MIPS_JUMP_INSNS = [ - MIPS_INS_JAL, MIPS_INS_JALR, MIPS_INS_J, MIPS_INS_JR -] - -MIPS_DELAY_SLOT_INSNS = [ - *MIPS_BRANCH_INSNS, *MIPS_JUMP_INSNS -] - -MIPS_FP_LOAD_INSNS = [ - MIPS_INS_LWC1, MIPS_INS_LDC1 -] - -MIPS_LOAD_INSNS = [ - MIPS_INS_LB, MIPS_INS_LBU, - MIPS_INS_LH, MIPS_INS_LHU, - MIPS_INS_LW, MIPS_INS_LWL, MIPS_INS_LWR, MIPS_INS_LWU, - MIPS_INS_LD, MIPS_INS_LDL, MIPS_INS_LDR, - MIPS_INS_LL, MIPS_INS_LLD, - *MIPS_FP_LOAD_INSNS -] - -MIPS_FP_STORE_INSNS = [ - MIPS_INS_SWC1, MIPS_INS_SDC1 -] - -MIPS_STORE_INSNS = [ - MIPS_INS_SB, - MIPS_INS_SH, - MIPS_INS_SW, MIPS_INS_SWL, MIPS_INS_SWR, - MIPS_INS_SD, MIPS_INS_SDL, MIPS_INS_SDR, - MIPS_INS_SC, MIPS_INS_SCD, - *MIPS_FP_STORE_INSNS -] - -MIPS_LOAD_STORE_INSNS = [ - *MIPS_LOAD_INSNS, - *MIPS_STORE_INSNS, -] - -MIPS_FP_LOAD_STORE_INSNS = [ - *MIPS_FP_LOAD_INSNS, *MIPS_FP_STORE_INSNS -] - -RSP_VECTOR_LOAD_STORES = [ - MIPS_INS_LBV, MIPS_INS_LSV, MIPS_INS_LLV, MIPS_INS_LDV, - MIPS_INS_LQV, MIPS_INS_LRV, MIPS_INS_LPV, MIPS_INS_LUV, - MIPS_INS_LHV, MIPS_INS_LFV, MIPS_INS_LTV, - - MIPS_INS_SBV, MIPS_INS_SSV, MIPS_INS_SLV, MIPS_INS_SDV, - MIPS_INS_SQV, MIPS_INS_SRV, MIPS_INS_SPV, MIPS_INS_SUV, - MIPS_INS_SHV, MIPS_INS_SFV, MIPS_INS_SWV, MIPS_INS_STV, -] - -# Labels - -# These labels can be referenced from pointers/loads/stores/etc. -LABEL_TYPE_FUNC = 0 -LABEL_TYPE_JTBL = 1 -LABEL_TYPE_DATA = 2 -LABEL_TYPE_BRANCH = 3 - -# Unknown -LABEL_TYPE_UNK = 20 - -class MipsLabel: - """ - Label - """ - - def __init__(self, vaddr, name=None, lbl_type=LABEL_TYPE_UNK, is_global=None) -> None: - self.name = name - self.vaddr = vaddr - self.lbl_type = lbl_type - self.is_global = is_global or (False if (lbl_type == LABEL_TYPE_BRANCH) else True) - - def __str__(self): - if self.name is not None: - return self.name - - if self.lbl_type == LABEL_TYPE_FUNC: - return f"func_{self.vaddr:08X}" - elif self.lbl_type == LABEL_TYPE_JTBL: - return f"jtbl_{self.vaddr:08X}" - elif self.lbl_type == LABEL_TYPE_BRANCH: - return f".L{self.vaddr:08X}" - elif (self.lbl_type == LABEL_TYPE_DATA or self.lbl_type == LABEL_TYPE_UNK): - return f"D_{self.vaddr:08X}" - assert False , f"Unimplemented default name for label type {self.lbl_type}" - -# Register Names - -mips_gpr_names = ( - "$zero", - "$at", - "$v0", "$v1", - "$a0", "$a1", "$a2", "$a3", - "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", - "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7", - "$t8", "$t9", - "$k0", "$k1", - "$gp", - "$sp", - "$fp", - "$ra", -) - -rsp_gpr_names = ( - "$zero", - "$1", "$2", "$3", "$4", "$5", "$6", - "$7", "$8", "$9", "$10", "$11", "$12", - "$13", "$14", "$15", "$16", "$17", "$18", - "$19", "$20", "$21", "$22", "$23", "$24", - "$25", "$26", "$27", "$28", "$29", "$30", - "$ra", -) - -mips_cop0_names = ( - "Index" , "Random" , "EntryLo0" , "EntryLo1" , - "Context" , "PageMask" , "Wired" , "Reserved07", - "BadVaddr" , "Count" , "EntryHi" , "Compare" , - "Status" , "Cause" , "EPC" , "PRevID" , - "Config" , "LLAddr" , "WatchLo" , "WatchHi" , - "XContext" , "Reserved21", "Reserved22", "Reserved23", - "Reserved24", "Reserved25", "PErr" , "CacheErr" , - "TagLo" , "TagHi" , "ErrorEPC" , "Reserved31", -) - -rsp_cop0_names = ( - "SP_MEM_ADDR", "SP_DRAM_ADDR", "SP_RD_LEN" , "SP_WR_LEN" , - "SP_STATUS" , "SP_DMA_FULL" , "SP_DMA_BUSY" , "SP_SEMAPHORE", - "DPC_START" , "DPC_END" , "DPC_CURRENT" , "DPC_STATUS" , - "DPC_CLOCK" , "DPC_BUFBUSY" , "DPC_PIPEBUSY", "DPC_TMEM" , -) - -mips_cop1_names = ( - "$f0", "$f1", "$f2", "$f3", - "$f4", "$f5", "$f6", "$f7", "$f8", "$f9", "$f10", "$f11", - "$f12", "$f13", "$f14", "$f15", - "$f16", "$f17", "$f18", "$f19", - "$f20", "$f21", "$f22", "$f23", "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", - # Status register - "FpCsr", -) - -rsp_cop2_names = ( - "$v0", "$v1", "$v2", "$v3", "$v4", "$v5", "$v6", "$v7", - "$v8", "$v9", "$v10", "$v11", "$v12", "$v13", "$v14", "$v15", - "$v16", "$v17", "$v18", "$v19", "$v20", "$v21", "$v22", "$v23", - "$v24", "$v25", "$v26", "$v27", "$v28", "$v29", "$v30", "$v31", -) - -rsp_cop2_ctrl_names = ( - '$vco', '$vcc', '$vce' -) - -# Instruction sets - -class MipsAbi: - def __init__(self, name, gpr_names, cop0_names, cop1_names, cop2_names): - self.name = name - self.gpr_names, self.cop0_names, self.cop1_names, self.cop2_names = \ - gpr_names, cop0_names, cop1_names, cop2_names - -ABI_VR4300 = MipsAbi("VR4300", mips_gpr_names, mips_cop0_names, mips_cop1_names, None ) -ABI_RSP = MipsAbi("RSP", rsp_gpr_names, rsp_cop0_names, None, rsp_cop2_names) - -# Instruction field fetching - -def sign_extend_6(value): - if value & (1 << (6 - 1)): - return value - (1 << 6) - return value - -def sign_extend_16(value): - return (value & 0x7FFF) - (value & 0x8000) - -def mask_shift(v, s, w): - return (v >> s) & ((1 << w) - 1) - -mips_get_field = lambda raw,vaddr : mask_shift(raw, 26, 6) -mips_get_special = lambda raw,vaddr : mask_shift(raw, 0, 6) -mips_get_cop0 = lambda raw,vaddr : mask_shift(raw, 21, 5) -mips_get_cop1 = lambda raw,vaddr : mask_shift(raw, 21, 5) -mips_get_cop2 = lambda raw,vaddr : mask_shift(raw, 21, 4) -mips_get_regimm = lambda raw,vaddr : mask_shift(raw, 16, 5) -mips_get_tlb = lambda raw,vaddr : mask_shift(raw, 0, 5) -mips_get_function = lambda raw,vaddr : mask_shift(raw, 0, 6) - -mips_get_cond = lambda raw,vaddr : mask_shift(raw, 0, 4) -mips_get_fd = lambda raw,vaddr : MipsFPReg(mask_shift(raw, 6, 5)) -mips_get_fs = lambda raw,vaddr : MipsFPReg(mask_shift(raw, 11, 5)) -mips_get_ft = lambda raw,vaddr : MipsFPReg(mask_shift(raw, 16, 5)) -mips_get_fmt = lambda raw,vaddr : mask_shift(raw, 21, 5) -mips_get_ndtf = lambda raw,vaddr : mask_shift(raw, 16, 2) - -mips_get_target = lambda raw,vaddr : ((vaddr & 0xFC000000) | (mask_shift(raw, 0, 26) << 2)) -mips_get_offset = lambda raw,vaddr : vaddr + 4 + sign_extend_16(mask_shift(raw, 0, 16)) * 4 -mips_get_imm = lambda raw,vaddr : mask_shift(raw, 0, 16) - -mips_get_base = lambda raw,vaddr : MipsGPReg(mask_shift(raw, 21, 5)) - -mips_get_cd = lambda raw,vaddr : mask_shift(raw, 11, 5) - -mips_get_code = lambda raw,vaddr : (mask_shift(raw, 6, 20) << 6) >> 16 -mips_get_op = lambda raw,vaddr : mask_shift(raw, 16, 5) - -mips_get_sa = lambda raw,vaddr : mask_shift(raw, 6, 5) - -mips_get_rd = lambda raw,vaddr : MipsGPReg(mask_shift(raw, 11, 5)) -mips_get_rs = lambda raw,vaddr : MipsGPReg(mask_shift(raw, 21, 5)) -mips_get_rt = lambda raw,vaddr : MipsGPReg(mask_shift(raw, 16, 5)) - -rsp_load_store_multiplier = { - 0b00000 : 0x01, # lbv, sbv - 0b00001 : 0x02, # lsv, ssv - 0b00010 : 0x04, # llv, slv - 0b00011 : 0x08, # ldv, sdv - 0b00100 : 0x10, # lqv, sqv - 0b00101 : 0x10, # lrv, srv - 0b00110 : 0x08, # lpv, spv - 0b00111 : 0x08, # luv, suv - - 0b01000 : 0x02, # lhv, shv - 0b01001 : 0x04, # lfv, sfv - 0b01010 : 0x10, # swv - 0b01011 : 0x10, # ltv, stv -} - -mips_get_vc = lambda raw,vaddr : mask_shift(raw, 11, 5) -mips_get_vd = lambda raw,vaddr : mask_shift(raw, 6, 5) -mips_get_vs = lambda raw,vaddr : mask_shift(raw, 11, 5) -mips_get_vt = lambda raw,vaddr : mask_shift(raw, 16, 5) -mips_get_elem = lambda raw,vaddr : mask_shift(raw, 21, 4) -mips_get_elemd = lambda raw,vaddr : mask_shift(raw, 7, 4) -mips_get_cop2_func = lambda raw,vaddr : mask_shift(raw, 25, 1) -mips_get_lwc2 = lambda raw,vaddr : mask_shift(raw, 11, 5) -mips_get_swc2 = lambda raw,vaddr : mask_shift(raw, 11, 5) -mips_get_voffset = lambda raw,vaddr : sign_extend_6(mask_shift(raw, 0, 6)) * rsp_load_store_multiplier[mips_get_lwc2(raw,vaddr)] - -# Formatting - -def resolve_pseudo_insn(insn): - # move varies between assemblers - # IDO - move_insn = MIPS_INS_OR - # GCC - # move_insn = MIPS_INS_ADDU - - if insn.id == MIPS_INS_SLL and insn.rd == MipsGPReg.R0 and insn.rt == MipsGPReg.R0 and insn.sa == 0: - return MIPS_INS_NOP, "nop", (), () - elif insn.id == MIPS_INS_BEQ and insn.rs == MipsGPReg.R0 and insn.rt == MipsGPReg.R0: - return MIPS_INS_B, "b", ("offset",), (False,) - elif insn.id == move_insn and insn.rt == MipsGPReg.R0: - return MIPS_INS_MOVE, "move", ("rd","rs"), (True,False) - elif insn.id == MIPS_INS_BEQ and insn.rt == MipsGPReg.R0: - return MIPS_INS_BEQZ, "beqz", ("rs","offset"), (False,False) - elif insn.id == MIPS_INS_BNE and insn.rt == MipsGPReg.R0: - return MIPS_INS_BNEZ, "bnez", ("rs","offset"), (False,False) - elif insn.id == MIPS_INS_SUBU and insn.rs == MipsGPReg.R0: - return MIPS_INS_NEGU, "negu", ("rd","rt"), (True,False) - elif insn.id == MIPS_INS_NOR and insn.rt == MipsGPReg.R0: - return MIPS_INS_NOT, "not", ("rd","rs"), (True,False) - else: - return insn.id, insn.mnemonic, insn.fields, insn.writes - -def format_hex(v, signed, zeros, no_zero): - if abs(v) < 10: - if v == 0 and no_zero: - return "" - return f"{v}" - elif not signed: - return f"0x{v:{f'0{zeros}' if zeros > 0 else ''}x}" - else: - return f"{v:#x}" - -def format_vector_elem(insn, elem): - if insn.id in RSP_VECTOR_LOAD_STORES: - return f"[{elem}]" - elif (elem & 8) == 8: - return f"[{elem & 7}]" - elif (elem & 0xC) == 4: - return f"[{elem & 3}h]" - elif (elem & 0xE) == 2: - return f"[{elem & 1}q]" - else: - return "" - -mips_field_formatters = { - 'code' : (lambda insn : f'{insn.code}' if insn.code != 0 else ''), - 'cd' : (lambda insn : insn.abi.cop0_names[insn.cd]), - 'rd' : (lambda insn : insn.abi.gpr_names[insn.rd]), - 'rs' : (lambda insn : insn.abi.gpr_names[insn.rs]), - 'rt' : (lambda insn : insn.abi.gpr_names[insn.rt]), - 'fd' : (lambda insn : insn.abi.cop1_names[insn.fd]), - 'fs' : (lambda insn : insn.abi.cop1_names[insn.fs]), - 'ft' : (lambda insn : insn.abi.cop1_names[insn.ft]), - 'sa' : (lambda insn : format_hex(insn.sa, True, 0, False)), - 'op' : (lambda insn : format_hex(insn.op, False, 0, False)), - 'imm' : (lambda insn : format_hex(insn.imm, True, 0, False)), - 'offset(base)' : (lambda insn : f'{format_hex(insn.imm, True, 0, True)}({insn.abi.gpr_names[insn.base]})'), - 'offset' : (lambda insn : f'{format_hex(insn.offset, True, 0, False)}'), - 'target' : (lambda insn : f'{format_hex(insn.target, False, 0, False)}'), - 'vd' : (lambda insn : insn.abi.cop2_names[insn.vd]), - 'vs' : (lambda insn : insn.abi.cop2_names[insn.vs]), - 'vt' : (lambda insn : insn.abi.cop2_names[insn.vt]), - 'vt[e]' : (lambda insn : f"{insn.abi.cop2_names[insn.vt]}{format_vector_elem(insn, insn.elem)}"), - 'vt[ed]' : (lambda insn : f"{insn.abi.cop2_names[insn.vt]}{format_vector_elem(insn, insn.elemd)}"), - 'vd[e]' : (lambda insn : f"{insn.abi.cop2_names[insn.vd]}{format_vector_elem(insn, insn.elem)}"), - 'vd[ed]' : (lambda insn : f"{insn.abi.cop2_names[insn.vd]}{format_vector_elem(insn, insn.elemd)}"), - 'voffset' : (lambda insn : f'{format_hex(insn.voffset, True, 0, False)}'), - 'voffset(base)' : (lambda insn : f'{format_hex(insn.voffset, True, 0, True)}({insn.abi.gpr_names[insn.base]})'), - 'vc' : (lambda insn : rsp_cop2_ctrl_names[insn.vc]) -} - -class MipsInsn: - - def __init__(self, abi, raw, vaddr, values): - self.abi = abi - self.raw = raw - self.vaddr = vaddr - - if values is None: - values = MIPS_INS_INVALID, f"/* Invalid Instruction */ .4byte 0x{raw:08X}", (), () - - self.id, self.mnemonic, self.fields, self.writes = values - - # self.code = self.sa = self.op = self.cd = self.rd = self.rs = self.rt = self.fd = self.fs = self.ft = self.imm = self.offset = self.base = self.target = None - - for field in self.fields: - self.set_value(field) - - self.id, self.mnemonic, self.fields, self.writes = resolve_pseudo_insn(self) - - self.op_str = self.format_insn() - - def read_fields(self): - return [field for i,field in enumerate(self.fields) if self.writes[i] == False] - - def write_fields(self): - return [field for i,field in enumerate(self.fields) if self.writes[i] == True] - - def has_field(self, field): - return field in self.fields - - def format_field(self, field): - return mips_field_formatters[field](self) - - def format_insn(self): - return ", ".join([self.format_field(field) for field in self.fields]) - - def __str__(self): - return f"{self.mnemonic:12}{self.op_str}" - - # Field setters - - def set_code(self): - self.code = mips_get_code(self.raw, self.vaddr) - - def set_sa(self): - self.sa = mips_get_sa(self.raw, self.vaddr) - - def set_op(self): - self.op = mips_get_op(self.raw, self.vaddr) - - def set_cd(self): - self.cd = mips_get_cd(self.raw, self.vaddr) - - def set_rd(self): - self.rd = mips_get_rd(self.raw, self.vaddr) - - def set_rs(self): - self.rs = mips_get_rs(self.raw, self.vaddr) - - def set_rt(self): - self.rt = mips_get_rt(self.raw, self.vaddr) - - def set_fd(self): - self.fd = mips_get_fd(self.raw, self.vaddr) - - def set_fs(self): - self.fs = mips_get_fs(self.raw, self.vaddr) - - def set_ft(self): - self.ft = mips_get_ft(self.raw, self.vaddr) - - do_sign_extend = [ - MIPS_INS_ADDIU, MIPS_INS_SLTI, MIPS_INS_ADDI, MIPS_INS_DADDIU, - MIPS_INS_LB, MIPS_INS_LBU, - MIPS_INS_LH, MIPS_INS_LHU, - MIPS_INS_LW, MIPS_INS_LWL, MIPS_INS_LWR, MIPS_INS_LWU, - MIPS_INS_LWC1, - MIPS_INS_LD, MIPS_INS_LDL, MIPS_INS_LDR, - MIPS_INS_LDC1, - MIPS_INS_LL, MIPS_INS_LLD, - MIPS_INS_SB, - MIPS_INS_SH, - MIPS_INS_SW, MIPS_INS_SWL, MIPS_INS_SWR, - MIPS_INS_SWC1, - MIPS_INS_SD, MIPS_INS_SDL, MIPS_INS_SDR, - MIPS_INS_SDC1, - MIPS_INS_SC, MIPS_INS_SCD, - ] - - def set_imm(self): - self.imm = mips_get_imm(self.raw, self.vaddr) - if self.id in MipsInsn.do_sign_extend: # sign extended immediates - self.imm = sign_extend_16(self.imm) - - def set_offset(self): - self.offset = mips_get_offset(self.raw, self.vaddr) - - def set_base(self): - self.base = mips_get_base(self.raw, self.vaddr) - - def set_offset_base(self): - self.set_imm() - self.set_base() - - def set_target(self): - self.target = mips_get_target(self.raw, self.vaddr) - - def set_vd(self): - self.vd = mips_get_vd(self.raw, self.vaddr) - - def set_vs(self): - self.vs = mips_get_vs(self.raw, self.vaddr) - - def set_vt(self): - self.vt = mips_get_vt(self.raw, self.vaddr) - - def set_e(self): - self.elem = mips_get_elem(self.raw, self.vaddr) - - def set_ed(self): - self.elemd = mips_get_elemd(self.raw, self.vaddr) - - def set_vd_e(self): - self.set_vd() - self.set_e() - - def set_vt_ed(self): - self.set_vt() - self.set_ed() - - def set_vt_e(self): - self.set_vt() - self.set_e() - - def set_vd_ed(self): - self.set_vd() - self.set_ed() - - def set_voffset(self): - self.voffset = mips_get_voffset(self.raw, self.vaddr) - - def set_voffset_base(self): - self.set_voffset() - self.set_base() - - def set_vc(self): - self.vc = mips_get_vc(self.raw, self.vaddr) - - field_setters = { - 'code' : set_code, - 'sa' : set_sa, - 'op' : set_op, - 'cd' : set_cd, - 'rd' : set_rd, - 'rs' : set_rs, - 'rt' : set_rt, - 'fd' : set_fd, - 'fs' : set_fs, - 'ft' : set_ft, - 'imm' : set_imm, - 'offset' : set_offset, - 'base' : set_base, - 'offset(base)' : set_offset_base, - 'target' : set_target, - 'vd' : set_vd, - 'vs' : set_vs, - 'vt' : set_vt, - 'vt[e]' : set_vt_e, - 'vt[ed]' : set_vt_ed, - 'vd[e]' : set_vd_e, - 'vd[ed]' : set_vd_ed, - 'voffset' : set_voffset, - 'voffset(base)' : set_voffset_base, - 'vc' : set_vc - } - - def set_value(self, name): - MipsInsn.field_setters[name](self) - - # Field getters - - field_getters = { - 'code' : (lambda insn: insn.code), - 'sa' : (lambda insn: insn.sa), - 'op' : (lambda insn: insn.op), - 'cd' : (lambda insn: insn.cd), - 'rd' : (lambda insn: insn.rd), - 'rs' : (lambda insn: insn.rs), - 'rt' : (lambda insn: insn.rt), - 'fd' : (lambda insn: insn.fd), - 'fs' : (lambda insn: insn.fs), - 'ft' : (lambda insn: insn.ft), - 'imm' : (lambda insn: insn.imm), - 'offset' : (lambda insn: insn.offset), - 'base' : (lambda insn: insn.base), - 'offset(base)' : (lambda insn: (insn.offset, insn.base)), - 'target' : (lambda insn: insn.target), - 'vd' : (lambda insn: insn.vd), - 'vs' : (lambda insn: insn.vs), - 'vt' : (lambda insn: insn.vt), - 'vt[e]' : (lambda insn: (insn.vt, insn.elem)), - 'vt[ed]' : (lambda insn: (insn.vt, insn.elemd)), - 'vd[e]' : (lambda insn: (insn.vd, insn.elem)), - 'vd[ed]' : (lambda insn: (insn.vd, insn.elemd)), - 'voffset' : (lambda insn: insn.voffset), - 'voffset(base)' : (lambda insn: (insn.voffset, insn.base)), - 'vc' : (lambda insn: insn.vc), - } - - def value_forname(self, name): - return MipsInsn.field_getters[name](self) - -def fetch_insn(raw, vaddr, insn_set, func): - insn = insn_set.get(func(raw, vaddr), None) # default none for invalid instruction encoding - - if insn is not None and type(insn[1]) == dict: # extra decoding required - insn = fetch_insn(raw, vaddr, insn[1], insn[0]) - return insn - -def decode_insn(raw, vaddr): - return MipsInsn(ABI_VR4300, raw, vaddr, fetch_insn(raw, vaddr, mips_insns, mips_get_field)) - -def decode_rsp_insn(raw, vaddr): - return MipsInsn(ABI_RSP, raw, vaddr, fetch_insn(raw, vaddr, rsp_insns, mips_get_field)) - -mips_insns = { - 0b000000: (mips_get_special, { - # opcode id mnemonic fields field is written to - 0b000000: (MIPS_INS_SLL, "sll", ("rd","rt","sa"), (True , False, False)), - 0b000010: (MIPS_INS_SRL, "srl", ("rd","rt","sa"), (True , False, False)), - 0b000011: (MIPS_INS_SRA, "sra", ("rd","rt","sa"), (True , False, False)), - 0b000100: (MIPS_INS_SLLV, "sllv", ("rd","rt","rs"), (True , False, False)), - 0b000110: (MIPS_INS_SRLV, "srlv", ("rd","rt","rs"), (True , False, False)), - 0b000111: (MIPS_INS_SRAV, "srav", ("rd","rt","rs"), (True , False, False)), - 0b001000: (MIPS_INS_JR, "jr", ("rs", ), (False, )), - 0b001001: (MIPS_INS_JALR, "jalr", ("rs", ), (False, )), # technically also rd but it's always $ra - 0b001100: (MIPS_INS_SYSCALL, "syscall", ( ), ( )), - 0b001101: (MIPS_INS_BREAK, "break", ("code", ), (False, )), - 0b001111: (MIPS_INS_SYNC, "sync", ( ), ( )), - 0b010000: (MIPS_INS_MFHI, "mfhi", ("rd", ), (True , )), - 0b010001: (MIPS_INS_MTHI, "mthi", ("rs", ), (False, )), - 0b010010: (MIPS_INS_MFLO, "mflo", ("rd", ), (True , )), - 0b010011: (MIPS_INS_MTLO, "mtlo", ("rs", ), (False, )), - 0b010100: (MIPS_INS_DSLLV, "dsllv", ("rd","rt","rs"), (True , False, False)), - 0b010110: (MIPS_INS_DSRLV, "dsrlv", ("rd","rt","rs"), (True , False, False)), - 0b010111: (MIPS_INS_DSRAV, "dsrav", ("rd","rt","rs"), (True , False, False)), - 0b011000: (MIPS_INS_MULT, "mult", ("rs","rt" ), (False, False )), - 0b011001: (MIPS_INS_MULTU, "multu", ("rs","rt" ), (False, False )), - 0b011010: (MIPS_INS_DIV, "div", ("rd","rs","rt"), (False, False, False)), # for some reason gas hates div instructions - 0b011011: (MIPS_INS_DIVU, "divu", ("rd","rs","rt"), (False, False, False)), # with the correct number of operands - 0b011100: (MIPS_INS_DMULT, "dmult", ("rs","rt" ), (False, False )), - 0b011101: (MIPS_INS_DMULTU, "dmultu", ("rs","rt" ), (False, False )), - 0b011110: (MIPS_INS_DDIV, "ddiv", ("rd","rs","rt"), (False, False, False)), - 0b011111: (MIPS_INS_DDIVU, "ddivu", ("rd","rs","rt"), (False, False, False)), - 0b100000: (MIPS_INS_ADD, "add", ("rd","rs","rt"), (True , False, False)), - 0b100001: (MIPS_INS_ADDU, "addu", ("rd","rs","rt"), (True , False, False)), - 0b100010: (MIPS_INS_SUB, "sub", ("rd","rs","rt"), (True , False, False)), - 0b100011: (MIPS_INS_SUBU, "subu", ("rd","rs","rt"), (True , False, False)), - 0b100100: (MIPS_INS_AND, "and", ("rd","rs","rt"), (True , False, False)), - 0b100101: (MIPS_INS_OR, "or", ("rd","rs","rt"), (True , False, False)), - 0b100110: (MIPS_INS_XOR, "xor", ("rd","rs","rt"), (True , False, False)), - 0b100111: (MIPS_INS_NOR, "nor", ("rd","rs","rt"), (True , False, False)), - 0b101010: (MIPS_INS_SLT, "slt", ("rd","rs","rt"), (True , False, False)), - 0b101011: (MIPS_INS_SLTU, "sltu", ("rd","rs","rt"), (True , False, False)), - 0b101100: (MIPS_INS_DADD, "dadd", ("rd","rs","rt"), (True , False, False)), - 0b101101: (MIPS_INS_DADDU, "daddu", ("rd","rs","rt"), (True , False, False)), - 0b101110: (MIPS_INS_DSUB, "dsub", ("rd","rs","rt"), (True , False, False)), - 0b101111: (MIPS_INS_DSUBU, "dsubu", ("rd","rs","rt"), (True , False, False)), - 0b110000: (MIPS_INS_TGE, "tge", ("rs","rt" ), (False, False )), - 0b110001: (MIPS_INS_TGEU, "tgeu", ("rs","rt" ), (False, False )), - 0b110010: (MIPS_INS_TLT, "tlt", ("rs","rt" ), (False, False )), - 0b110011: (MIPS_INS_TLTU, "tltu", ("rs","rt" ), (False, False )), - 0b110100: (MIPS_INS_TEQ, "teq", ("rs","rt" ), (False, False )), - 0b110110: (MIPS_INS_TNE, "tne", ("rs","rt" ), (False, False )), - 0b111000: (MIPS_INS_DSLL, "dsll", ("rd","rt","sa"), (True , False, False)), - 0b111010: (MIPS_INS_DSRL, "dsrl", ("rd","rt","sa"), (True , False, False)), - 0b111011: (MIPS_INS_DSRA, "dsra", ("rd","rt","sa"), (True , False, False)), - 0b111100: (MIPS_INS_DSLL32, "dsll32", ("rd","rt","sa"), (True , False, False)), - 0b111110: (MIPS_INS_DSRL32, "dsrl32", ("rd","rt","sa"), (True , False, False)), - 0b111111: (MIPS_INS_DSRA32, "dsra32", ("rd","rt","sa"), (True , False, False)), - }), - 0b000001: (mips_get_regimm, { - 0b00000: (MIPS_INS_BLTZ, "bltz", ("rs","offset"), (False, False)), - 0b00001: (MIPS_INS_BGEZ, "bgez", ("rs","offset"), (False, False)), - 0b00010: (MIPS_INS_BLTZL, "bltzl", ("rs","offset"), (False, False)), - 0b00011: (MIPS_INS_BGEZL, "bgezl", ("rs","offset"), (False, False)), - 0b01000: (MIPS_INS_TGEI, "tgei", ("rs","imm" ), (False, False)), - 0b01001: (MIPS_INS_TGEIU, "tgeiu", ("rs","imm" ), (False, False)), - 0b01010: (MIPS_INS_TLTI, "tlti", ("rs","imm" ), (False, False)), - 0b01011: (MIPS_INS_TLTIU, "tltiu", ("rs","imm" ), (False, False)), - 0b01100: (MIPS_INS_TEQI, "teqi", ("rs","imm" ), (False, False)), - 0b01110: (MIPS_INS_TNEI, "tnei", ("rs","imm" ), (False, False)), - 0b10000: (MIPS_INS_BLTZAL, "bltzal", ("rs","offset"), (False, False)), - 0b10001: (MIPS_INS_BGEZAL, "bgezal", ("rs","offset"), (False, False)), - 0b10010: (MIPS_INS_BLTZALL, "bltzall", ("rs","offset"), (False, False)), - 0b10011: (MIPS_INS_BGEZALL, "bgezall", ("rs","offset"), (False, False)), - }), - 0b000010: (MIPS_INS_J, "j", ("target", ), (False, )), - 0b000011: (MIPS_INS_JAL, "jal", ("target", ), (False, )), - 0b000100: (MIPS_INS_BEQ, "beq", ("rs","rt","offset"), (False, False, False)), - 0b000101: (MIPS_INS_BNE, "bne", ("rs","rt","offset"), (False, False, False)), - 0b000110: (MIPS_INS_BLEZ, "blez", ("rs","offset" ), (False, False, )), - 0b000111: (MIPS_INS_BGTZ, "bgtz", ("rs","offset" ), (False, False, )), - 0b001000: (MIPS_INS_ADDI, "addi", ("rt","rs","imm" ), (True , False, False)), - 0b001001: (MIPS_INS_ADDIU, "addiu", ("rt","rs","imm" ), (True , False, False)), - 0b001010: (MIPS_INS_SLTI, "slti", ("rt","rs","imm" ), (True , False, False)), - 0b001011: (MIPS_INS_SLTIU, "sltiu", ("rt","rs","imm" ), (True , False, False)), - 0b001100: (MIPS_INS_ANDI, "andi", ("rt","rs","imm" ), (True , False, False)), - 0b001101: (MIPS_INS_ORI, "ori", ("rt","rs","imm" ), (True , False, False)), - 0b001110: (MIPS_INS_XORI, "xori", ("rt","rs","imm" ), (True , False, False)), - 0b001111: (MIPS_INS_LUI, "lui", ("rt","imm" ), (True , False )), - 0b010000: (mips_get_cop0, { - 0b00000: (MIPS_INS_MFC0, "mfc0", ("rt","cd"), (True , False)), - 0b00100: (MIPS_INS_MTC0, "mtc0", ("rt","cd"), (False, True )), - 0b10000: (mips_get_tlb, { - 0b000001: (MIPS_INS_TLBR, "tlbr", (), ()), - 0b000010: (MIPS_INS_TLBWI, "tlbwi", (), ()), - 0b000110: (MIPS_INS_TLBWR, "tlbwr", (), ()), - 0b001000: (MIPS_INS_TLBP, "tlbp", (), ()), - 0b011000: (MIPS_INS_ERET, "eret", (), ()), - }), - }), - 0b010001: (mips_get_cop1, { - 0b00000: (MIPS_INS_MFC1, "mfc1", ("rt","fs"), (True , False)), - 0b00001: (MIPS_INS_DMFC1, "dmfc1", ("rt","fs"), (True , False)), - 0b00010: (MIPS_INS_CFC1, "cfc1", ("rt","fs"), (True , False)), - 0b00100: (MIPS_INS_MTC1, "mtc1", ("rt","fs"), (False, True )), - 0b00101: (MIPS_INS_DMTC1, "dmtc1", ("rt","fs"), (False, True )), - 0b00110: (MIPS_INS_CTC1, "ctc1", ("rt","fs"), (False, True )), - 0b01000: (mips_get_ndtf, { - 0b00: (MIPS_INS_BC1F, "bc1f", ("offset",), (False,)), - 0b01: (MIPS_INS_BC1T, "bc1t", ("offset",), (False,)), - 0b10: (MIPS_INS_BC1FL, "bc1fl", ("offset",), (False,)), - 0b11: (MIPS_INS_BC1TL, "bc1tl", ("offset",), (False,)), - }), - 0b010000: (mips_get_function, { - 0b000000: (MIPS_INS_ADD_S, "add.s", ("fd","fs","ft"), (True , False, False)), - 0b000001: (MIPS_INS_SUB_S, "sub.s", ("fd","fs","ft"), (True , False, False)), - 0b000010: (MIPS_INS_MUL_S, "mul.s", ("fd","fs","ft"), (True , False, False)), - 0b000011: (MIPS_INS_DIV_S, "div.s", ("fd","fs","ft"), (True , False, False)), - 0b000100: (MIPS_INS_SQRT_S, "sqrt.s", ("fd","fs" ), (True , False )), - 0b000101: (MIPS_INS_ABS_S, "abs.s", ("fd","fs" ), (True , False )), - 0b000110: (MIPS_INS_MOV_S, "mov.s", ("fd","fs" ), (True , False )), - 0b000111: (MIPS_INS_NEG_S, "neg.s", ("fd","fs" ), (True , False )), - 0b001000: (MIPS_INS_ROUND_L_S, "round.l.s", ("fd","fs" ), (True , False )), - 0b001001: (MIPS_INS_TRUNC_L_S, "trunc.l.s", ("fd","fs" ), (True , False )), - 0b001010: (MIPS_INS_CEIL_L_S, "ceil.l.s", ("fd","fs" ), (True , False )), - 0b001011: (MIPS_INS_FLOOR_L_S, "floor.l.s", ("fd","fs" ), (True , False )), - 0b001100: (MIPS_INS_ROUND_W_S, "round.w.s", ("fd","fs" ), (True , False )), - 0b001101: (MIPS_INS_TRUNC_W_S, "trunc.w.s", ("fd","fs" ), (True , False )), - 0b001110: (MIPS_INS_CEIL_W_S, "ceil.w.s", ("fd","fs" ), (True , False )), - 0b001111: (MIPS_INS_FLOOR_W_S, "floor.w.s", ("fd","fs" ), (True , False )), - 0b100001: (MIPS_INS_CVT_D_S, "cvt.d.s", ("fd","fs" ), (True , False )), - 0b100100: (MIPS_INS_CVT_W_S, "cvt.w.s", ("fd","fs" ), (True , False )), - 0b100101: (MIPS_INS_CVT_L_S, "cvt.l.s", ("fd","fs" ), (True , False )), - 0b110000: (MIPS_INS_C_F_S, "c.f.s", ("fs","ft" ), (False, False )), - 0b110001: (MIPS_INS_C_UN_S, "c.un.s", ("fs","ft" ), (False, False )), - 0b110010: (MIPS_INS_C_EQ_S, "c.eq.s", ("fs","ft" ), (False, False )), - 0b110011: (MIPS_INS_C_UEQ_S, "c.ueq.s", ("fs","ft" ), (False, False )), - 0b110100: (MIPS_INS_C_OLT_S, "c.olt.s", ("fs","ft" ), (False, False )), - 0b110101: (MIPS_INS_C_ULT_S, "c.ult.s", ("fs","ft" ), (False, False )), - 0b110110: (MIPS_INS_C_OLE_S, "c.ole.s", ("fs","ft" ), (False, False )), - 0b110111: (MIPS_INS_C_ULE_S, "c.ule.s", ("fs","ft" ), (False, False )), - 0b111000: (MIPS_INS_C_SF_S, "c.sf.s", ("fs","ft" ), (False, False )), - 0b111001: (MIPS_INS_C_NGLE_S, "c.ngle.s", ("fs","ft" ), (False, False )), - 0b111010: (MIPS_INS_C_SEQ_S, "c.seq.s", ("fs","ft" ), (False, False )), - 0b111011: (MIPS_INS_C_NGL_S, "c.ngl.s", ("fs","ft" ), (False, False )), - 0b111100: (MIPS_INS_C_LT_S, "c.lt.s", ("fs","ft" ), (False, False )), - 0b111101: (MIPS_INS_C_NGE_S, "c.nge.s", ("fs","ft" ), (False, False )), - 0b111110: (MIPS_INS_C_LE_S, "c.le.s", ("fs","ft" ), (False, False )), - 0b111111: (MIPS_INS_C_NGT_S, "c.ngt.s", ("fs","ft" ), (False, False )), - }), - 0b010001: (mips_get_function, { - 0b000000: (MIPS_INS_ADD_D, "add.d", ("fd","fs","ft"), (True , False, False)), - 0b000001: (MIPS_INS_SUB_D, "sub.d", ("fd","fs","ft"), (True , False, False)), - 0b000010: (MIPS_INS_MUL_D, "mul.d", ("fd","fs","ft"), (True , False, False)), - 0b000011: (MIPS_INS_DIV_D, "div.d", ("fd","fs","ft"), (True , False, False)), - 0b000100: (MIPS_INS_SQRT_D, "sqrt.d", ("fd","fs" ), (True , False )), - 0b000101: (MIPS_INS_ABS_D, "abs.d", ("fd","fs" ), (True , False )), - 0b000110: (MIPS_INS_MOV_D, "mov.d", ("fd","fs" ), (True , False )), - 0b000111: (MIPS_INS_NEG_D, "neg.d", ("fd","fs" ), (True , False )), - 0b001000: (MIPS_INS_ROUND_L_D, "round.l.d", ("fd","fs" ), (True , False )), - 0b001001: (MIPS_INS_TRUNC_L_D, "trunc.l.d", ("fd","fs" ), (True , False )), - 0b001010: (MIPS_INS_CEIL_L_D, "ceil.l.d", ("fd","fs" ), (True , False )), - 0b001011: (MIPS_INS_FLOOR_L_D, "floor.l.d", ("fd","fs" ), (True , False )), - 0b001100: (MIPS_INS_ROUND_W_D, "round.w.d", ("fd","fs" ), (True , False )), - 0b001101: (MIPS_INS_TRUNC_W_D, "trunc.w.d", ("fd","fs" ), (True , False )), - 0b001110: (MIPS_INS_CEIL_W_D, "ceil.w.d", ("fd","fs" ), (True , False )), - 0b001111: (MIPS_INS_FLOOR_W_D, "floor.w.d", ("fd","fs" ), (True , False )), - 0b100000: (MIPS_INS_CVT_S_D, "cvt.s.d", ("fd","fs" ), (True , False )), - 0b100100: (MIPS_INS_CVT_W_D, "cvt.w.d", ("fd","fs" ), (True , False )), - 0b100101: (MIPS_INS_CVT_L_D, "cvt.l.d", ("fd","fs" ), (True , False )), - 0b110000: (MIPS_INS_C_F_D, "c.f.d", ("fs","ft" ), (False, False )), - 0b110001: (MIPS_INS_C_UN_D, "c.un.d", ("fs","ft" ), (False, False )), - 0b110010: (MIPS_INS_C_EQ_D, "c.eq.d", ("fs","ft" ), (False, False )), - 0b110011: (MIPS_INS_C_UEQ_D, "c.ueq.d", ("fs","ft" ), (False, False )), - 0b110100: (MIPS_INS_C_OLT_D, "c.olt.d", ("fs","ft" ), (False, False )), - 0b110101: (MIPS_INS_C_ULT_D, "c.ult.d", ("fs","ft" ), (False, False )), - 0b110110: (MIPS_INS_C_OLE_D, "c.ole.d", ("fs","ft" ), (False, False )), - 0b110111: (MIPS_INS_C_ULE_D, "c.ule.d", ("fs","ft" ), (False, False )), - 0b111000: (MIPS_INS_C_SF_D, "c.sf.d", ("fs","ft" ), (False, False )), - 0b111001: (MIPS_INS_C_NGLE_D, "c.ngle.d", ("fs","ft" ), (False, False )), - 0b111010: (MIPS_INS_C_SEQ_D, "c.seq.d", ("fs","ft" ), (False, False )), - 0b111011: (MIPS_INS_C_NGL_D, "c.ngl.d", ("fs","ft" ), (False, False )), - 0b111100: (MIPS_INS_C_LT_D, "c.lt.d", ("fs","ft" ), (False, False )), - 0b111101: (MIPS_INS_C_NGE_D, "c.nge.d", ("fs","ft" ), (False, False )), - 0b111110: (MIPS_INS_C_LE_D, "c.le.d", ("fs","ft" ), (False, False )), - 0b111111: (MIPS_INS_C_NGT_D, "c.ngt.d", ("fs","ft" ), (False, False )), - }), - 0b010100: (mips_get_function, { - 0b100000: (MIPS_INS_CVT_S_W, "cvt.s.w", ("fd","fs"), (True , False)), - 0b100001: (MIPS_INS_CVT_D_W, "cvt.d.w", ("fd","fs"), (True , False)), - }), - 0b010101: (mips_get_function, { - 0b100000: (MIPS_INS_CVT_S_L, "cvt.s.l", ("fd","fs"), (True , False)), - 0b100001: (MIPS_INS_CVT_D_L, "cvt.d.l", ("fd","fs"), (True , False)), - }), - }), - 0b010100: (MIPS_INS_BEQL, "beql", ("rs","rt","offset" ), (False, False, False)), - 0b010101: (MIPS_INS_BNEL, "bnel", ("rs","rt","offset" ), (False, False, False)), - 0b010110: (MIPS_INS_BLEZL, "blezl", ("rs","offset" ), (False, False )), - 0b010111: (MIPS_INS_BGTZL, "bgtzl", ("rs","offset" ), (False, False )), - 0b011000: (MIPS_INS_DADDI, "daddi", ("rt","rs","imm" ), (True , False, False)), - 0b011001: (MIPS_INS_DADDIU, "daddiu", ("rt","rs","imm" ), (True , False, False)), - 0b011010: (MIPS_INS_LDL, "ldl", ("rt","offset(base)"), (True , False )), - 0b011011: (MIPS_INS_LDR, "ldr", ("rt","offset(base)"), (True , False )), - 0b100000: (MIPS_INS_LB, "lb", ("rt","offset(base)"), (True , False )), - 0b100001: (MIPS_INS_LH, "lh", ("rt","offset(base)"), (True , False )), - 0b100010: (MIPS_INS_LWL, "lwl", ("rt","offset(base)"), (True , False )), - 0b100011: (MIPS_INS_LW, "lw", ("rt","offset(base)"), (True , False )), - 0b100100: (MIPS_INS_LBU, "lbu", ("rt","offset(base)"), (True , False )), - 0b100101: (MIPS_INS_LHU, "lhu", ("rt","offset(base)"), (True , False )), - 0b100110: (MIPS_INS_LWR, "lwr", ("rt","offset(base)"), (True , False )), - 0b100111: (MIPS_INS_LWU, "lwu", ("rt","offset(base)"), (True , False )), - 0b101000: (MIPS_INS_SB, "sb", ("rt","offset(base)"), (False, False )), - 0b101001: (MIPS_INS_SH, "sh", ("rt","offset(base)"), (False, False )), - 0b101010: (MIPS_INS_SWL, "swl", ("rt","offset(base)"), (False, False )), - 0b101011: (MIPS_INS_SW, "sw", ("rt","offset(base)"), (False, False )), - 0b101100: (MIPS_INS_SDL, "sdl", ("rt","offset(base)"), (False, False )), - 0b101101: (MIPS_INS_SDR, "sdr", ("rt","offset(base)"), (False, False )), - 0b101110: (MIPS_INS_SWR, "swr", ("rt","offset(base)"), (False, False )), - 0b101111: (MIPS_INS_CACHE, "cache", ("op","offset(base)"), (False, False, False)), - 0b110000: (MIPS_INS_LL, "ll", ("rt","offset(base)"), (True , False )), - 0b110001: (MIPS_INS_LWC1, "lwc1", ("ft","offset(base)"), (True , False )), - # lwc2 - 0b110100: (MIPS_INS_LLD, "lld", ("rt","offset(base)"), (True , False )), - 0b110101: (MIPS_INS_LDC1, "ldc1", ("ft","offset(base)"), (True , False )), - # ldc2 - 0b110111: (MIPS_INS_LD, "ld", ("rt","offset(base)"), (True , False )), - 0b111000: (MIPS_INS_SC, "sc", ("rt","offset(base)"), (False, False )), - 0b111001: (MIPS_INS_SWC1, "swc1", ("ft","offset(base)"), (False, False )), - # lwc2 - 0b111100: (MIPS_INS_SCD, "scd", ("rt","offset(base)"), (False, False )), - 0b111101: (MIPS_INS_SDC1, "sdc1", ("ft","offset(base)"), (False, False )), - # sdc2 - 0b111111: (MIPS_INS_SD, "sd", ("rt","offset(base)"), (False, False )), -} - -rsp_insns = { - 0b000000: (mips_get_special, { - # opcode id mnemonic fields field is written to - 0b000000: (MIPS_INS_SLL, "sll", ("rd","rt","sa"), (True , False, False)), - 0b000010: (MIPS_INS_SRL, "srl", ("rd","rt","sa"), (True , False, False)), - 0b000011: (MIPS_INS_SRA, "sra", ("rd","rt","sa"), (True , False, False)), - 0b000100: (MIPS_INS_SLLV, "sllv", ("rd","rt","rs"), (True , False, False)), - 0b000110: (MIPS_INS_SRLV, "srlv", ("rd","rt","rs"), (True , False, False)), - 0b000111: (MIPS_INS_SRAV, "srav", ("rd","rt","rs"), (True , False, False)), - 0b001000: (MIPS_INS_JR, "jr", ("rs", ), (False, )), - 0b001001: (MIPS_INS_JALR, "jalr", ("rs", ), (False, )), # technically also rd but it's always $ra - 0b001101: (MIPS_INS_BREAK, "break", ("code", ), (False, )), - 0b100000: (MIPS_INS_ADD, "add", ("rd","rs","rt"), (True , False, False)), - 0b100001: (MIPS_INS_ADDU, "addu", ("rd","rs","rt"), (True , False, False)), - 0b100010: (MIPS_INS_SUB, "sub", ("rd","rs","rt"), (True , False, False)), - 0b100011: (MIPS_INS_SUBU, "subu", ("rd","rs","rt"), (True , False, False)), - 0b100100: (MIPS_INS_AND, "and", ("rd","rs","rt"), (True , False, False)), - 0b100101: (MIPS_INS_OR, "or", ("rd","rs","rt"), (True , False, False)), - 0b100110: (MIPS_INS_XOR, "xor", ("rd","rs","rt"), (True , False, False)), - 0b100111: (MIPS_INS_NOR, "nor", ("rd","rs","rt"), (True , False, False)), - 0b101010: (MIPS_INS_SLT, "slt", ("rd","rs","rt"), (True , False, False)), - 0b101011: (MIPS_INS_SLTU, "sltu", ("rd","rs","rt"), (True , False, False)), - }), - 0b000001: (mips_get_regimm, { - 0b00000: (MIPS_INS_BLTZ, "bltz", ("rs","offset"), (False, False)), - 0b00001: (MIPS_INS_BGEZ, "bgez", ("rs","offset"), (False, False)), - 0b10000: (MIPS_INS_BLTZAL, "bltzal", ("rs","offset"), (False, False)), - 0b10001: (MIPS_INS_BGEZAL, "bgezal", ("rs","offset"), (False, False)), - }), - 0b000010: (MIPS_INS_J, "j", ("target", ), (False, )), - 0b000011: (MIPS_INS_JAL, "jal", ("target", ), (False, )), - 0b000100: (MIPS_INS_BEQ, "beq", ("rs","rt","offset"), (False, False, False)), - 0b000101: (MIPS_INS_BNE, "bne", ("rs","rt","offset"), (False, False, False)), - 0b000110: (MIPS_INS_BLEZ, "blez", ("rs","offset" ), (False, False )), - 0b000111: (MIPS_INS_BGTZ, "bgtz", ("rs","offset" ), (False, False )), - 0b001000: (MIPS_INS_ADDI, "addi", ("rt","rs","imm" ), (True , False, False)), - 0b001001: (MIPS_INS_ADDIU, "addiu", ("rt","rs","imm" ), (True , False, False)), - 0b001010: (MIPS_INS_SLTI, "slti", ("rt","rs","imm" ), (True , False, False)), - 0b001011: (MIPS_INS_SLTIU, "sltiu", ("rt","rs","imm" ), (True , False, False)), - 0b001100: (MIPS_INS_ANDI, "andi", ("rt","rs","imm" ), (True , False, False)), - 0b001101: (MIPS_INS_ORI, "ori", ("rt","rs","imm" ), (True , False, False)), - 0b001110: (MIPS_INS_XORI, "xori", ("rt","rs","imm" ), (True , False, False)), - 0b001111: (MIPS_INS_LUI, "lui", ("rt","imm" ), (True , False )), - 0b010000: (mips_get_cop0, { - 0b00000: (MIPS_INS_MFC0, "mfc0", ("rt","cd"), (True , False)), - 0b00100: (MIPS_INS_MTC0, "mtc0", ("rt","cd"), (False, True )), - }), - 0b010010: (mips_get_cop2_func, { # TODO this encoding got ugly, move to a mask matcher like gnu objdump - 0b0: (mips_get_cop2, { - 0b0000: (MIPS_INS_MFC2, "mfc2", ("rt", "vd[e]"), (True , False)), - 0b0100: (MIPS_INS_MTC2, "mtc2", ("rt", "vd[e]"), (False, True )), - 0b0010: (MIPS_INS_CFC2, "cfc2", ("rt", "vc" ), (True , False)), - 0b0110: (MIPS_INS_CTC2, "ctc2", ("rt", "vc" ), (False, True )), - }), - 0b1: (mips_get_function, { - 0b000000: (MIPS_INS_VMULF, "vmulf", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000001: (MIPS_INS_VMULU, "vmulu", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000010: (MIPS_INS_VRNDP, "vrndp", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000011: (MIPS_INS_VMULQ, "vmulq", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000100: (MIPS_INS_VMUDL, "vmudl", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000101: (MIPS_INS_VMUDM, "vmudm", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000110: (MIPS_INS_VMUDN, "vmudn", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b000111: (MIPS_INS_VMUDH, "vmudh", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001000: (MIPS_INS_VMACF, "vmacf", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001001: (MIPS_INS_VMACU, "vmacu", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001010: (MIPS_INS_VRNDN, "vrndn", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001011: (MIPS_INS_VMACQ, "vmacq", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001100: (MIPS_INS_VMADL, "vmadl", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001101: (MIPS_INS_VMADM, "vmadm", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001110: (MIPS_INS_VMADN, "vmadn", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b001111: (MIPS_INS_VMADH, "vmadh", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b010000: (MIPS_INS_VADD, "vadd", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b010001: (MIPS_INS_VSUB, "vsub", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b010011: (MIPS_INS_VABS, "vabs", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b010100: (MIPS_INS_VADDC, "vaddc", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b010101: (MIPS_INS_VSUBC, "vsubc", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b011101: (MIPS_INS_VSAR, "vsar", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100000: (MIPS_INS_VLT, "vlt", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100001: (MIPS_INS_VEQ, "veq", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100010: (MIPS_INS_VNE, "vne", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100011: (MIPS_INS_VGE, "vge", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100100: (MIPS_INS_VCL, "vcl", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100101: (MIPS_INS_VCH, "vch", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100110: (MIPS_INS_VCR, "vcr", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b100111: (MIPS_INS_VMRG, "vmrg", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b101000: (MIPS_INS_VAND, "vand", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b101001: (MIPS_INS_VNAND, "vnand", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b101010: (MIPS_INS_VOR, "vor", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b101011: (MIPS_INS_VNOR, "vnor", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b101100: (MIPS_INS_VXOR, "vxor", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b101101: (MIPS_INS_VNXOR, "vnxor", ("vd", "vs", "vt[e]"), (True , False, False)), - 0b110000: (MIPS_INS_VRCP, "vrcp", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110001: (MIPS_INS_VRCPL, "vrcpl", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110010: (MIPS_INS_VRCPH, "vrcph", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110011: (MIPS_INS_VMOV, "vmov", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110100: (MIPS_INS_VRSQ, "vrsq", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110101: (MIPS_INS_VRSQL, "vrsql", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110110: (MIPS_INS_VRSQH, "vrsqh", ("vd[ed]", "vt[e]" ), (True , False)), - 0b110111: (MIPS_INS_VNOP, "vnop", ( ), ()), - }), - }), - 0b100000: (MIPS_INS_LB, "lb", ("rt","offset(base)"), (True , False)), - 0b100001: (MIPS_INS_LH, "lh", ("rt","offset(base)"), (True , False)), - 0b100011: (MIPS_INS_LW, "lw", ("rt","offset(base)"), (True , False)), - 0b100100: (MIPS_INS_LBU, "lbu", ("rt","offset(base)"), (True , False)), - 0b100101: (MIPS_INS_LHU, "lhu", ("rt","offset(base)"), (True , False)), - 0b101000: (MIPS_INS_SB, "sb", ("rt","offset(base)"), (False, False)), - 0b101001: (MIPS_INS_SH, "sh", ("rt","offset(base)"), (False, False)), - 0b101011: (MIPS_INS_SW, "sw", ("rt","offset(base)"), (False, False)), - 0b110010: (mips_get_lwc2, { - 0b00000: (MIPS_INS_LBV, "lbv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00001: (MIPS_INS_LSV, "lsv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00010: (MIPS_INS_LLV, "llv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00011: (MIPS_INS_LDV, "ldv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00100: (MIPS_INS_LQV, "lqv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00101: (MIPS_INS_LRV, "lrv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00110: (MIPS_INS_LPV, "lpv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b00111: (MIPS_INS_LUV, "luv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b01000: (MIPS_INS_LHV, "lhv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b01001: (MIPS_INS_LFV, "lfv", ("vt[ed]", "voffset(base)"), (True , False)), - 0b01011: (MIPS_INS_LTV, "ltv", ("vt[ed]", "voffset(base)"), (True , False)), - }), - 0b111010: (mips_get_swc2, { - 0b00000: (MIPS_INS_SBV, "sbv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00001: (MIPS_INS_SSV, "ssv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00010: (MIPS_INS_SLV, "slv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00011: (MIPS_INS_SDV, "sdv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00100: (MIPS_INS_SQV, "sqv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00101: (MIPS_INS_SRV, "srv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00110: (MIPS_INS_SPV, "spv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b00111: (MIPS_INS_SUV, "suv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b01000: (MIPS_INS_SHV, "shv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b01001: (MIPS_INS_SFV, "sfv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b01010: (MIPS_INS_SWV, "swv", ("vt[ed]", "voffset(base)"), (False, False)), - 0b01011: (MIPS_INS_STV, "stv", ("vt[ed]", "voffset(base)"), (False, False)), - }), -} diff --git a/tools/patch_ar_meta.py b/tools/patch_ar_meta.py deleted file mode 100755 index d831785..0000000 --- a/tools/patch_ar_meta.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env python3 -# -# Patch metadata in .ar files -# - -import argparse - -def ar_patch(ar, original, new_uid, new_gid, new_filemode): - """ - AR file headers - - Offset Length Name Format - 0 16 File identifier ASCII - 16 12 File modification timestamp (in seconds) Decimal - 28 6 Owner ID Decimal - 34 6 Group ID Decimal - 40 8 File mode (type and permission) Octal - 48 10 File size in bytes Decimal - 58 2 Ending characters 0x60 0x0A - """ - assert len(ar) > 0x24 , "Got empty archive?" - - armap_time1 = None - armap_time2 = None - - i = 8 - while i < len(original): - file_name = original[i:][0:][:16].decode("ASCII") - file_size = int(original[i:][48:][:10].decode("ASCII").strip()) - end = original[i:][58:][:2].decode("ASCII") - - assert end == "`\n" - - if file_name.strip() == "/": - armap_time1 = original[i+16:i+16+12] - elif file_name.strip() == "//": - armap_time2 = original[i+16:i+16+12] - - if file_size % 2 != 0: - file_size += 1 - i += file_size + 60 - - assert armap_time1 is not None - assert armap_time2 is not None - - i = 8 - while i < len(ar): - file_name = ar[i:][0:][:16].decode("ASCII") - file_size = int(ar[i:][48:][:10].decode("ASCII").strip()) - end = ar[i:][58:][:2].decode("ASCII") - - assert end == "`\n" - - if file_name.strip() == "/": - ar[i+16:i+16+12] = armap_time1 - elif file_name.strip() == "//": - ar[i+16:i+16+12] = armap_time2 - else: - ar[i+28:i+28+6] = f"{new_uid:<6}".encode("ASCII")[:6] - ar[i+34:i+34+6] = f"{new_gid:<6}".encode("ASCII")[:6] - ar[i+40:i+40+8] = f"{new_filemode:<8}".encode("ASCII")[:8] - - if file_size % 2 != 0: - file_size += 1 - i += file_size + 60 - -def main(): - parser = argparse.ArgumentParser(description="Patch metadata in .a files.") - parser.add_argument("target", help="path to the ar file to patch") - parser.add_argument("original", help="path to the original ar file") - parser.add_argument("uid", help="new uid") - parser.add_argument("gid", help="new gid") - parser.add_argument("filemode", help="new filemode") - args = parser.parse_args() - - ar = None - with open(args.target, "rb") as ar_file: - ar = bytearray(ar_file.read()) - - original = None - with open(args.original, "rb") as original_ar_file: - original = bytearray(original_ar_file.read()) - - ar_patch(ar, original, args.uid, args.gid, args.filemode) - - with open(args.target, "wb") as ar_file: - ar_file.write(ar) - -if __name__ == '__main__': - main() diff --git a/tools/print_mdebug.py b/tools/print_mdebug.py deleted file mode 100755 index e0b295a..0000000 --- a/tools/print_mdebug.py +++ /dev/null @@ -1,589 +0,0 @@ -#!/usr/bin/env python3 -''' -Resources: -http://www.cs.unibo.it/~solmi/teaching/arch_2002-2003/AssemblyLanguageProgDoc.pdf -https://github.com/pathscale/binutils/blob/5c2c133020e41fc4aadd80a99156d2cea4754b96/include/coff/sym.h -https://github.com/pathscale/binutils/blob/5c2c133020e41fc4aadd80a99156d2cea4754b96/include/coff/symconst.h -https://github.com/pathscale/binutils/blob/5c2c133020e41fc4aadd80a99156d2cea4754b96/gas/ecoff.c -https://github.com/pathscale/binutils/blob/5c2c133020e41fc4aadd80a99156d2cea4754b96/bfd/ecoff.c -https://github.com/pathscale/absoft/blob/master/svn/trunk/ekopath-gcc/ekopath-gcc-4.2.0/gcc/mips-tdump.c -https://chromium.googlesource.com/native_client/nacl-toolchain/+/refs/tags/gcc-4.4.3/binutils/gas/ecoff.c -https://android.googlesource.com/toolchain/binutils/+/refs/heads/donut/binutils-2.17/bfd/ecofflink.c -https://kernel.googlesource.com/pub/scm/linux/kernel/git/hjl/binutils/+/hjl/secondary/include/coff/symconst.h -https://c0de.pw/bg/binutils-gdb/blob/cdbf20f73486c66e24f322400eba877eb534ae51/gdb/mdebugread.c -''' - -import os -import struct -import collections -import sys - -OFFSET = 0 # TODO why are the offsets in the symbolic header off by some amount? - -indent_level = 0 -is_comment = False - -symbol_type_list = [ - 'stNil', 'stGlobal', 'stStatic', 'stParam', 'stLocal', 'stLabel', 'stProc', 'stBlock', - 'stEnd', 'stMember', 'stTypedef', 'stFile', 'INVALID', 'INVALID', 'stStaticProc', 'stConstant', - 'stStaParam', 'INVALID', 'INVALID', 'INVALID', 'INVALID', 'INVALID', 'INVALID', 'INVALID', - 'INVALID', 'INVALID', 'stStruct', 'stUnion', 'stEnum', 'INVALID', 'INVALID', 'INVALID', - 'INVALID', 'INVALID', 'stIndirect'] -storage_class_list = ['scNil', 'scText', 'scData', 'scBss', 'scRegister', 'scAbs', 'scUndefined', 'reserved', - 'scBits', 'scDbx', 'scRegImage', 'scInfo', 'scUserStruct', 'scSData', 'scSBss', 'scRData', - 'scVar', 'scCommon', 'scSCommon', 'scVarRegister', 'scVariant', 'scUndefined', 'scInit'] -basic_type_c_list = ['nil', 'addr', 'signed char', 'unsigned char', 'short', 'unsigned short', 'int', 'unsigned int', - 'long', 'unsigned long', 'float', 'double', 'struct', 'union', 'enum', 'typedef', - 'range', 'set', 'complex', 'double complex', 'indirect', 'fixed decimal', 'float decimal', 'string', - 'bit', 'picture', 'void', 'long long', 'unsigned long long', 'INVALID', 'long', 'unsigned long', - 'long long', 'unsigned long long', 'addr', 'int64', 'unsigned int64'] - -def increase_indent(): - global indent_level - indent_level += 1 - -def decrease_indent(): - global indent_level - indent_level -= 1 - -def set_is_comment(set_to): - global is_comment - old = is_comment - is_comment = set_to - return old - -def get_indent(): - global indent_level - global is_comment - ret = '//' if is_comment else '' - for i in range(indent_level): - ret += ' ' - return ret - -def check_indent(): - global indent_level - assert indent_level == 0 - -def read_uint32_be(file_data, offset): - return struct.unpack('>I', file_data[offset:offset+4])[0] - -def read_uint16_be(file_data, offset): - return struct.unpack('>H', file_data[offset:offset+2])[0] - -def read_uint8_be(file_data, offset): - return struct.unpack('>B', file_data[offset:offset+1])[0] - -def read_elf_header(file_data, offset): - Header = collections.namedtuple('ElfHeader', - '''e_magic e_class e_data e_version e_osabi e_abiversion e_pad - e_type e_machine e_version2 e_entry e_phoff e_shoff e_flags - e_ehsize e_phentsize e_phnum e_shentsize e_shnum e_shstrndx''') - return Header._make(struct.unpack('>I5B7s2H5I6H', file_data[offset:offset+52])) - -def read_elf_section_header(file_data, offset): - Header = collections.namedtuple('SectionHeader', - '''sh_name sh_type sh_flags sh_addr sh_offset sh_size sh_link - sh_info sh_addralign sh_entsize''') - return Header._make(struct.unpack('>10I', file_data[offset:offset+40])) - -def read_symbolic_header(file_data, offset): - Header = collections.namedtuple('SymbolicHeader', - '''magic vstamp ilineMax cbLine cbLineOffset idnMax cbDnOffset - ipdMax cbPdOffset isymMax cbSymOffset ioptMax cbOptOffset - iauxMax cbAuxOffset issMax cbSsOffset issExtMax cbSsExtOffset - ifdMax cbFdOffset crfd cbRfdOffset iextMax cbExtOffset''') - return Header._make(struct.unpack('>2H23I', file_data[offset:offset+96])) - -# TODO find a better solution for the bitfield -def read_file_descriptor(file_data, offset): - if 'init' not in read_file_descriptor.__dict__: - read_file_descriptor.cache = {} - read_file_descriptor.header = collections.namedtuple('FileDescriptor', - '''adr rss issBase cbSs isymBase csym ilineBase cline ioptBase - copt ipdFirst cpd iauxBase caux rfdBase crfd XXX_bitfield - cbLineOffset cbLine''') - read_file_descriptor.init = True - if offset in read_file_descriptor.cache: - return read_file_descriptor.cache[offset] - read_file_descriptor.cache[offset] = read_file_descriptor.header._make( - struct.unpack('>I2iI6iHh4iI2I', file_data[offset:offset+72])) - return read_file_descriptor.cache[offset] - -def read_procedure_descriptor(file_data, offset): - Header = collections.namedtuple('ProcedureDescriptor', - '''adr isym iline regmask regoffset iopt fregmask fregoffset - frameoffset framereg pcreg lnLow lnHigh cbLineOffset''') - return Header._make(struct.unpack('>I8i2h2iI', file_data[offset:offset+52])) - -def read_symbol(file_data, offset): - if 'init' not in read_symbol.__dict__: - read_symbol.cache = {} - read_symbol.header = collections.namedtuple('Symbol', '''iss value st sc index''') - read_symbol.init = True - if offset in read_symbol.cache: - return read_symbol.cache[offset] - (word0, word1, word2) = struct.unpack('>iiI', file_data[offset:offset+12]) - read_symbol.cache[offset] = read_symbol.header._make(( - word0, word1, (word2 >> 26) & 0x3F, (word2 >> 21) & 0x1F, word2 & 0xFFFFF)) - return read_symbol.cache[offset] - -def read_auxiliary_symbol(file_data, offset): - if 'init' not in read_auxiliary_symbol.__dict__: - read_auxiliary_symbol.cache = {} - read_auxiliary_symbol.header = collections.namedtuple('AuxSymbol', - '''ti rndx dnLow dnHigh isym iss width count''') - read_auxiliary_symbol.type_info = collections.namedtuple('TypeInfo', - '''fBitfield continued bt tq4 tq5 tq0 tq1 tq2 tq3''') - read_auxiliary_symbol.rel_sym = collections.namedtuple('RelativeSymbol', '''rfd index''') - read_auxiliary_symbol.init = True - if offset in read_auxiliary_symbol.cache: - return read_auxiliary_symbol.cache[offset] - word0 = struct.unpack('>I', file_data[offset:offset+4])[0] - read_auxiliary_symbol.cache[offset] = read_auxiliary_symbol.header._make(( - read_auxiliary_symbol.type_info._make(((word0 >> 31) & 1, (word0 >> 30) & 1, (word0 >> 24) & 0x3F, (word0 >> 20) & 0xF, (word0 >> 16) & 0xF, (word0 >> 12) & 0xF, (word0 >> 8) & 0xF, (word0 >> 4) & 0xF, word0 & 0xF)), - read_auxiliary_symbol.rel_sym._make(((word0 >> 20) & 0xFFF, word0 & 0xFFFFF)), - word0, word0, word0, word0, word0, word0)) - return read_auxiliary_symbol.cache[offset] - -def read_string(file_data, offset): - current_offset = 0 - current_string = b'' - while True: - char = struct.unpack('c', file_data[offset+current_offset:offset+current_offset+1])[0] - if char == b'\0': - return current_string.decode('ascii') - else: - current_string += char - current_offset += 1 - -def map_relative_file_descriptor(file_data, fd, symbolic_header, rfd_num): - if fd.crfd == 0: - return rfd_num - - offset = symbolic_header.cbRfdOffset - OFFSET + (fd.rfdBase + rfd_num)*4 - return struct.unpack('>I', file_data[offset:offset+4])[0] - -def get_symbol_name_from_aux(file_data, fd, symbolic_header, aux_num, search_for_typedef): - aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + aux_num)*4) - fd_num = aux.rndx.rfd - next_aux = aux_num+1 - if fd_num == 4095: - aux2 = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + next_aux)*4) - fd_num = aux2.isym - next_aux = next_aux+1; - fd_num = map_relative_file_descriptor(file_data, fd, symbolic_header, fd_num) - fd2 = read_file_descriptor(file_data, symbolic_header.cbFdOffset - OFFSET + fd_num*72) - sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + (fd2.isymBase + aux.rndx.index)*12) - ret = '' - #print('%r' % (aux,)); - #print('%r' % (aux2,)); - #print('%r' % (sym,)); - if sym.st == 26 or sym.st == 27: #stStruct, stunion - ret = get_struct_or_union_string(file_data, fd2, symbolic_header, fd2.isymBase + aux.rndx.index, search_for_typedef) - elif sym.st == 28: #stEnum: - ret = get_enum_string(file_data, fd2, symbolic_header, fd2.isymBase + aux.rndx.index) - else: - ret = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd2.issBase + sym.iss) - return (ret, next_aux) - -def get_type_string(file_data, fd, symbolic_header, aux_num, name, search_for_typedef): - ret = '' - aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + aux_num)*4) - #print(''); - #print('%r' % (aux,)); - next_aux = aux_num+1 - has_bitfield = aux.ti.fBitfield == 1 - bitwidth = 0 - if has_bitfield: - bit_aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + next_aux)*4) - bitwidth = bit_aux.isym - next_aux = next_aux+1 - if aux.ti.bt == 12: # btStruct - (ret, next_aux) = get_symbol_name_from_aux(file_data, fd, symbolic_header, next_aux, search_for_typedef) - elif aux.ti.bt == 13: # btUnion - (ret, next_aux) = get_symbol_name_from_aux(file_data, fd, symbolic_header, next_aux, search_for_typedef) - elif aux.ti.bt == 15: # btTypedef - (ret, next_aux) = get_symbol_name_from_aux(file_data, fd, symbolic_header, next_aux, search_for_typedef) - elif aux.ti.bt == 14: # btEnum - (ret, next_aux) = get_symbol_name_from_aux(file_data, fd, symbolic_header, next_aux, search_for_typedef) - else: - if aux.ti.bt >= 36: - print('Error unknow bt: %d' % (aux.ti.bt)) - ret = basic_type_c_list[aux.ti.bt] - - tq_list = (aux.ti.tq0, aux.ti.tq1, aux.ti.tq2, aux.ti.tq3, aux.ti.tq4, aux.ti.tq5) - - # TODO this is very naive and probably does not work in a large amount of cases - last_was_proc = False # if we see a tqProc, assume the next will be a tqPtr - for tq in tq_list: - if tq == 0: # tqNil - break; - elif tq == 1: # tqPtr - if last_was_proc: - last_was_proc = False - continue - ret += '*' - elif tq == 2: # tqProc - last_was_proc = True - name = '(*%s)(/* ECOFF does not store param types */)' % name - elif tq == 3: # tqArray - next_aux += 2 # todo what does this skip over? (Apparantly the type of the index, so always int for C) - array_low_aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + next_aux)*4) - array_high_aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + next_aux+1)*4) - stride_aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + next_aux+2)*4) - next_aux += 3 - if array_high_aux.dnHigh == 0xFFFFFFFF: - name += '[]' - else: - name += '[%d]' % (array_high_aux.dnHigh + 1) - elif tq == 4: # tqFar - print('ERROR tqFar in get_type_name') - elif tq == 5: # tqVol - ret = 'volatile ' + ret - elif tq == 6: # tqConst - ret = 'const ' + ret - if has_bitfield: - name += ' : %d' % bitwidth - return ret + ' ' + name - -def get_enum_string(file_data, fd, symbolic_header, enum_sym_num): - ret = '' - start_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + enum_sym_num*12) - if start_sym.st != 28: - print('ERROR unknown type in get_enum_string start:%d' % start_sym.st) - return ret - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + start_sym.iss) - if name != '': - name += ' ' - ret += 'enum %s{\n' % name - increase_indent() - sym_num = enum_sym_num + 1 - while sym_num < fd.isymBase + start_sym.index: - sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - if sym.st == 8: # stEnd - decrease_indent() - ret += get_indent() - ret += '}' - elif sym.st == 9: # stMember - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - ret += get_indent() - ret += '%s = %d,\n' % (name, sym.value) - else: - print('ERROR unknown type in get_enum_string:%d' % sym.st) - break - sym_num += 1 - return ret - -def get_struct_or_union_string(file_data, fd, symbolic_header, union_sym_num, search_for_typedef): - ret = '' - start_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + union_sym_num*12) - if search_for_typedef: - typedef_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + (fd.isymBase + start_sym.index)*12) - if typedef_sym.st == 10: # stTypedef - return read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + typedef_sym.iss) - else: - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + start_sym.iss) - if name != '': - return name - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + start_sym.iss) - if name != '': - name += ' ' - if start_sym.st == 26: # stStruct - ret += 'struct %s{\n' % name - increase_indent() - elif start_sym.st == 27: # stUnion - ret += 'union %s{\n' % name - increase_indent() - else: - print('ERROR unknown type in get_struct_or_union_string start:%d' % start_sym.st) - return ret - sym_num = union_sym_num + 1 - while sym_num < fd.isymBase + start_sym.index: - sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - if sym.st == 8: # stEnd - decrease_indent() - ret += get_indent() - ret += '}' - elif sym.st == 9: # stMember - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - ret += get_indent() - ret += '/* 0x%X */ %s;\n' % (sym.value // 8, get_type_string(file_data, fd, symbolic_header, sym.index, name, True)) - elif sym.st == 26 or sym.st == 27: #stStruct, stUnion - sym_num = fd.isymBase + sym.index - continue - elif sym.st == 34: # stIndirect - # TODO what even is a stIndirect? - sym_num += 1 - else: - print('ERROR unknown type in get_struct_or_union_string:%d' % sym.st) - break - sym_num += 1 - return ret - -def print_typedef_symbols(file_data, fd, symbolic_header, typedef_sym_num): - typedef_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + typedef_sym_num*12) - if typedef_sym.st != 10: # stTypedef - print('ERROR expected stTypedef symbol in print_typedef_symbols, found:%d' % typedef_sym.st) - return - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + typedef_sym.iss) - print('typedef %s;' % get_type_string(file_data, fd, symbolic_header, typedef_sym.index, name, False)) - -def print_procedure(file_data, fd, symbolic_header, proc_sym_num): - proc_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + proc_sym_num*12) - proc_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + proc_sym.iss) - print('%s(' % get_type_string(file_data, fd, symbolic_header, proc_sym.index+1, proc_name, True), end='') - sym_num = proc_sym_num+1 - param_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - first = True - while param_sym.st == 3: # stParam - param_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + param_sym.iss) - print('%s%s' % ('' if first else ', ', - get_type_string(file_data, fd, symbolic_header, param_sym.index, param_name, True)), - end='') - sym_num += 1 - param_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - first = False - - print(');') - - check_indent(); - - comment_old = set_is_comment(True) - while sym_num < fd.isymBase + fd.csym: - sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - sym_num += 1 - if sym.st == 7: # stBlock - print('%s{' % get_indent()) - increase_indent() - elif sym.st == 8: # stEnd - end_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - if end_name == proc_name: - break - if end_name != '': - # this is a stEnd for something other than the function. Let's back out and return - sym_num -= 1 - break - decrease_indent() - print('%s}' % get_indent()) - elif sym.st == 4: # stLocal - local_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - is_reg = sym.sc == 4 # scRegister - print('%s%s%s;' % (get_indent(), - 'register ' if is_reg else '', - get_type_string(file_data, fd, symbolic_header, sym.index, local_name, True))) - elif sym.st == 2: # stStatic - static_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - if sym.index == 0xFFFFF: - print('%sstatic %s; // no type symbol' % (get_indent(),static_name)) - else: - print('%sstatic %s;' % (get_indent(),get_type_string(file_data, fd, symbolic_header, sym.index, static_name, True))) - elif sym.st == 5: # stLabel - label_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - print('%sLabel: %s @ %d;' % (get_indent(), label_name, sym.value)) - elif sym.st == 6: # stProc - # multiple name for function? - sym_num = print_procedure(file_data, fd, symbolic_header, sym_num-1) - elif sym.st == 26 or sym.st == 27: # stStruct, stUnion - print('%s%s;' % (get_indent(), get_struct_or_union_string(file_data, fd, symbolic_header, sym_num-1, False))) - sym_num = fd.isymBase + sym.index - elif sym.st == 28: # stEnum - print('%s%s;' % (get_indent(), get_enum_string(file_data, fd, symbolic_header, sym_num-1))) - sym_num = fd.isymBase + sym.index - elif sym.st == 34: # stIndirect - # TODO what even is a stIndirect? - indirect_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss) - print('%sTODO Indirect: %s;' % (get_indent(), indirect_name)) - else: - print('ERROR unknown st in print_procedure: %d' % sym.st) - set_is_comment(comment_old) - - check_indent(); - - return sym_num - -def print_symbols(file_data, fd, symbolic_header): - sym_num = fd.isymBase - indirects = [] - typedefs = [] - while sym_num < fd.isymBase + fd.csym: - root_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - if root_sym.st == 10: # stTypedef - aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + root_sym.index)*4) - offset = 0 - if aux.ti.fBitfield == 1: - offset = 1 - if aux.ti.bt == 12 or aux.ti.bt == 13 or aux.ti.bt == 14 or aux.ti.bt == 15: # btStruct, btUnion, btEnum, btTypedef - aux2 = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + root_sym.index + 1 + offset)*4) - fd_num = aux2.rndx.rfd - if fd_num == 4095: - fd_num = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + root_sym.index + 2 + offset)*4).isym - fd_num = map_relative_file_descriptor(file_data, fd, symbolic_header, fd_num) - fd2 = read_file_descriptor(file_data, symbolic_header.cbFdOffset - OFFSET + fd_num*72) - sym2 = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + (fd2.isymBase + aux2.rndx.index)*12) - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd2.issBase + sym2.iss) - if name != '': - typedefs.append(name) - elif root_sym.st == 34: # stIndirect - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + root_sym.iss) - indirects.append(name); - sym_num += 1 - sym_num = fd.isymBase - while sym_num < fd.isymBase + fd.csym: - root_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - if root_sym.st == 11: # stFile - file_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + root_sym.iss) - print('// begin file %s\n' % file_name) - sym_num += 1 - leaf_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - while leaf_sym.st != 8: # stEnd - if leaf_sym.st == 26 or leaf_sym.st == 27: # stStruct, stUnion - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + leaf_sym.iss) - if (name != '') and ((name in indirects) or (name not in typedefs)): # TODO - print('%s;\n' % get_struct_or_union_string(file_data, fd, symbolic_header, sym_num, False)) - sym_num = fd.isymBase + leaf_sym.index - elif leaf_sym.st == 28: # stEnum - name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + leaf_sym.iss) - if (name != '') and (name not in typedefs): # TODO - print('%s;\n' % get_enum_string(file_data, fd, symbolic_header, sym_num)) - sym_num = fd.isymBase + leaf_sym.index - elif leaf_sym.st == 10: # stTypedef - # TODO typedef for stIndirect shoulf print the keyword i.e. typdef >struct< THING thing - print_typedef_symbols(file_data, fd, symbolic_header, sym_num) - sym_num += 1 - print('') - elif leaf_sym.st == 6 or leaf_sym.st == 14: # stProc, stStaticProc - # TODO how do stProc and stStaticProc differ? stStaticProc isn't exported? - sym_num = print_procedure(file_data, fd, symbolic_header, sym_num) - print('') - elif leaf_sym.st == 2: # stStatic - static_name = read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + leaf_sym.iss) - if leaf_sym.sc == 2 or leaf_sym.sc == 3 or leaf_sym.sc == 5 or leaf_sym.sc == 15: # scData, scBss, scAbsolute, scRData - if leaf_sym.index != 0xFFFFF: # looks like it's an invalid value for .s files - print('static %s;\n' % get_type_string(file_data, fd, symbolic_header, leaf_sym.index, static_name, True)) - else: - print('static %s;\n' % static_name) - else: - print('ERROR unknown sc for stStatic in print_symbols: %d' % leaf_sym.sc) - sym_num += 1 - elif leaf_sym.st == 34: # stIndirect - # stIndirect is put out when the compiler sees a struct when it is not yet defined - # TODO more info - sym_num += 1 - elif leaf_sym.st == 5: # stLabel - print('// label: %s' % read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + leaf_sym.iss)) - sym_num += 1 - elif leaf_sym.st == 0: # stNil - print('// nil: %s' % read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + leaf_sym.iss)) - sym_num += 1 - else: - print('ERROR unknown st in leaf_sym in print_symbols: %d' % leaf_sym.st) - sym_num += 1 - leaf_sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - sym_num = fd.isymBase + root_sym.index - print('// end file %s' % file_name) - else: - print('ERROR expected st of stFile as only root type in print_symbols:%d' % root_sym.st) - return - -def main(): - global OFFSET - if len(sys.argv) < 2: - return # TODO print usage - - filename = sys.argv[1] - - try: - with open(filename, 'rb') as f: - file_data = f.read() - except IOError: - print('failed to read file ' + filename) - return - - elf_header = read_elf_header(file_data, 0) - section_headers = [] - debug_index = 0xFFFFFFFF - #print('%r' % (elf_header,)) - for i in range(elf_header.e_shnum): - section_headers.append(read_elf_section_header(file_data, elf_header.e_shoff + i*40)) - #print('%r' % (section_headers[i],)) - if section_headers[i].sh_type == 0x70000005: - debug_index = i - - if debug_index != 0xFFFFFFFF: - symbolic_header = read_symbolic_header(file_data, section_headers[debug_index].sh_offset) - file_descriptors = [] - print('%r' % (symbolic_header,)) - # Set offset by assuming that there are no optimization symbols so cbOptOffset points to the start of the symbolic header - #OFFSET = symbolic_header.cbOptOffset - section_headers[debug_index].sh_offset - #print('Using OFFSET of %d' % OFFSET) - #for sym_num in range(symbolic_header.isymMax): - #sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - #print('%d:%r' % (sym_num, (sym,))); - #for aux_num in range(symbolic_header.iauxMax): - #aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + aux_num*4) - #print('%d:%r' % (aux_num, (aux,))); - for file_num in range(symbolic_header.ifdMax): - fd = read_file_descriptor(file_data, symbolic_header.cbFdOffset - OFFSET + file_num*72) - file_descriptors.append(fd) - for file_num in range(symbolic_header.ifdMax): - fd = read_file_descriptor(file_data, symbolic_header.cbFdOffset - OFFSET + file_num*72) - print('%r' % (fd,)) - print(' name:%s' % read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + fd.rss)) - - ''' - print(' Relative File Descriptors:') - for rfd_num in range(fd.rfdBase, fd.rfdBase + fd.crfd): - offset = symbolic_header.cbRfdOffset - OFFSET + rfd_num*4 - rfd_index = struct.unpack('>I', file_data[offset:offset+4])[0] - rfd = read_file_descriptor(file_data, symbolic_header.cbFdOffset - OFFSET + rfd_index*72) - print(' %d:%r' % (rfd_index, (rfd,))) - ''' - - ''' - print(' procedures:') - for proc_num in range(fd.ipdFirst, fd.ipdFirst + fd.cpd): - pd = read_procedure_descriptor(file_data, symbolic_header.cbPdOffset - OFFSET + proc_num*52) - print(' %r' % ((pd,))) - ''' - - ''' - print(' symbols:') - for sym_num in range(fd.isymBase, fd.isymBase + fd.csym): - sym = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + sym_num*12) - print(' %r' % ((sym,))) - print(' name:%s' % read_string(file_data, symbolic_header.cbSsOffset - OFFSET + fd.issBase + sym.iss)) - print(' type:%s(%d)' % (symbol_type_list[sym.st], sym.st)) - print(' storage class:%s(%d)' % (storage_class_list[sym.sc], sym.sc)) - if sym.st == 3 or sym.st == 4 or sym.st == 9 or sym.st == 10 or sym.st == 28: # stParam, stLocal, stMember, stTypedef, stEnum - aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + sym.index)*4) - print(' %r' % ((aux,))) - offset = 0 - if aux.ti.fBitfield == 1: - bitfield_aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + sym.index + 1)*4) - print(' %r' % ((bitfield_aux,))) - offset = 1 - if aux.ti.bt == 12 or aux.ti.bt == 13 or aux.ti.bt == 14 or aux.ti.bt == 15: # btStruct, btUnion, btEnum, btTypedef - aux2 = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + sym.index + 1 + offset)*4) - print(' %r' % ((aux2,))) - fd_num = aux2.rndx.rfd - if fd_num == 4095: - aux3 = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + sym.index + 2 + offset)*4) - print(' %r' % ((aux3,))) - fd_num = aux3.isym - fd_num = map_relative_file_descriptor(file_data, fd, symbolic_header, fd_num) - sym2 = read_symbol(file_data, symbolic_header.cbSymOffset - OFFSET + (file_descriptors[fd_num].isymBase + aux2.rndx.index)*12) - print(' %r' % (sym2,)) - print(' name:%s' % read_string(file_data, symbolic_header.cbSsOffset - OFFSET + file_descriptors[aux3.isym].issBase + sym2.iss)) - if sym.st == 6 or sym.st == 14: # stProc, stStaticProc - # TODO what is the first aux symbol for? - aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + sym.index)*4) - type_aux = read_auxiliary_symbol(file_data, symbolic_header.cbAuxOffset - OFFSET + (fd.iauxBase + sym.index+1)*4) - print(' %r' % ((aux,))) - print(' %r' % ((type_aux,))) - ''' - - print(' pretty print:') - print_symbols(file_data, fd, symbolic_header) - - -main() \ No newline at end of file diff --git a/tools/set_o32abi_bit.py b/tools/set_o32abi_bit.py deleted file mode 100755 index fc22d2c..0000000 --- a/tools/set_o32abi_bit.py +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env python3 -import argparse, struct, sys - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - - parser.add_argument('file', help='input file') - args = parser.parse_args() - - with open(args.file, 'r+b') as f: - magic = struct.unpack('>I', f.read(4))[0] - if magic != 0x7F454C46: - print('Error: Not an ELF file') - sys.exit(1) - - f.seek(36) - flags = struct.unpack('>I', f.read(4))[0] - # if flags & 0xF0000000 != 0x20000000: # test for mips3 - # print('Error: Architecture not mips3') - # sys.exit(1) - - flags |= 0x00001000 # set EF_MIPS_ABI_O32 - f.seek(36) - f.write(struct.pack('>I', flags)) - diff --git a/tools/strip_debug.sh b/tools/strip_debug.sh deleted file mode 100755 index 4a8be78..0000000 --- a/tools/strip_debug.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/bash - -cd $1 - -mkdir -p .cmp - -for f in *.o ; -do - ${CROSS}objcopy -p --strip-debug $f .cmp/${f/.o/.cmp.o} -done diff --git a/tools/util.py b/tools/util.py deleted file mode 100644 index b6c6155..0000000 --- a/tools/util.py +++ /dev/null @@ -1,44 +0,0 @@ - -import struct - -def enumerate_stepped(l, start=0, step=1): - p = start - for e in l: - yield p, e - p += step - -def back_align(x, al): - return x - (x % al) - -def forward_align(x, al): - return (x + (al - 1)) & ~(al - 1) - -def as_double(b): - return struct.unpack(">d", b)[0] - -def as_dword(b): - return struct.unpack(">Q", b)[0] - -def as_float(b): - return struct.unpack(">f", b)[0] - -def as_word(b): - return struct.unpack(">I", b)[0] - -def as_hword(b): - return struct.unpack(">H", b)[0] - -def as_double_list(b): - return [i[0] for i in struct.iter_unpack(">d", b)] - -def as_dword_list(b): - return [i[0] for i in struct.iter_unpack(">Q", b)] - -def as_float_list(b): - return [i[0] for i in struct.iter_unpack(">f", b)] - -def as_word_list(b): - return [i[0] for i in struct.iter_unpack(">I", b)] - -def as_hword_list(b): - return [h[0] for h in struct.iter_unpack(">H", b)]