From 0764ca19acd615ac0231c019d9a67d264f927d2b Mon Sep 17 00:00:00 2001 From: Magnus Olsen Date: Sun, 14 Jan 2007 10:57:18 +0000 Subject: [PATCH] Fixing a bug in ppc opcode Li Fixing the memory leak bug Full implement of the Li Convert Li to my own asm langues, opcode move reg,imm Left todo is add the anyalying process and converting process, after that we can translate our frist public pe file for ppc to intel with one opcode, it must be Li svn path=/trunk/; revision=25447 --- .../devutils/cputointel/From/PPC/PPCopcode.c | 18 ++++-- rosapps/devutils/cputointel/ImageLoader.c | 55 ++++++++++++++++--- rosapps/devutils/cputointel/any_op.h | 3 + rosapps/devutils/cputointel/misc.c | 26 +++++++++ rosapps/devutils/cputointel/misc.h | 1 + 5 files changed, 91 insertions(+), 12 deletions(-) diff --git a/rosapps/devutils/cputointel/From/PPC/PPCopcode.c b/rosapps/devutils/cputointel/From/PPC/PPCopcode.c index 6862f2afb66..664ece852e2 100644 --- a/rosapps/devutils/cputointel/From/PPC/PPCopcode.c +++ b/rosapps/devutils/cputointel/From/PPC/PPCopcode.c @@ -5,6 +5,10 @@ #include "../../misc.h" #include "../../any_op.h" +/* reg r0-r31 + r3 = eax + */ + /* cpuDummyInit_Add * Input param : * out : The file pointer that we write to (the output file to intel asm) @@ -51,16 +55,22 @@ CPU_INT PPC_Ld( FILE *out, CPU_BYTE * cpu_buffer, CPU_UNINT cpu_pos, CPU_UNINT opcode; opcode = GetData32Le(cpu_buffer); + formD = (opcode & ConvertBitToByte32(PPC_D)) >> 6; formA = (opcode & ConvertBitToByte32(PPC_A)) >> 13; - formD = (opcode & ConvertBitToByte32(PPC_D)) >> 10; formDS = (opcode & ConvertBitToByte32(PPC_ds)) >> 15; - fprintf(out,"Line_0x%08x:\n",BaseAddress + cpu_pos); + if (formD != 0) + { + return 0; + } + + BaseAddress +=cpu_pos; if (mode==0) { + fprintf(out,"Line_0x%08x:\n",BaseAddress); fprintf(out,"li %%r%d,%d\n",formA, formDS); } - if (mode!=0) + else if (mode>0) { /* own translatons langues */ if (AllocAny()!=0) /* alloc memory for pMyBrainAnalys */ @@ -72,9 +82,9 @@ CPU_INT PPC_Ld( FILE *out, CPU_BYTE * cpu_buffer, CPU_UNINT cpu_pos, pMyBrainAnalys->src_size = 16; pMyBrainAnalys->src = formDS; pMyBrainAnalys->dst = formA; + pMyBrainAnalys->memAdr=BaseAddress; } - printf(";not full implement \n"); return 4; } diff --git a/rosapps/devutils/cputointel/ImageLoader.c b/rosapps/devutils/cputointel/ImageLoader.c index ccaa4ecb9b6..874993c2998 100644 --- a/rosapps/devutils/cputointel/ImageLoader.c +++ b/rosapps/devutils/cputointel/ImageLoader.c @@ -29,6 +29,7 @@ CPU_INT LoadPFileImage( char *infileName, char *outputfileName, CPU_BYTE *cpu_buffer; CPU_UNINT cpu_pos = 0; CPU_UNINT cpu_size=0; + CPU_INT ret; //fopen("testms.exe","RB"); @@ -113,6 +114,7 @@ CPU_INT LoadPFileImage( char *infileName, char *outputfileName, { type=1; } + FreeAny(); fclose(outfp); return 0; } @@ -120,25 +122,62 @@ CPU_INT LoadPFileImage( char *infileName, char *outputfileName, if (type== 1) { if (stricmp(cpuid,"m68000")) - return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68000,outfp,mode); + { + ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68000,outfp,mode); + FreeAny(); + fclose(outfp); + } else if (stricmp(cpuid,"m68010")) - return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68010,outfp,mode); + { + ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68010,outfp,mode); + FreeAny(); + fclose(outfp); + return ret; + } else if (stricmp(cpuid,"m68020")) - return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68020,outfp,mode); + { + ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68020,outfp,mode); + FreeAny(); + fclose(outfp); + return ret; + } else if (stricmp(cpuid,"m68030")) - return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68030,outfp,mode); + { + ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68030,outfp,mode); + FreeAny(); + fclose(outfp); + return ret; + } else if (stricmp(cpuid,"m68040")) - return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68040,outfp,mode); + { + ret = M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68040,outfp,mode); + FreeAny(); + fclose(outfp); + return ret; + } else if (stricmp(cpuid,"ppc")) - return PPCBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,0,outfp,mode); + { + ret = PPCBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,0,outfp,mode); + FreeAny(); + fclose(outfp); + return ret; + } else if (stricmp(cpuid,"arm4")) - return ARMBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,4,outfp,mode); + { + ret = ARMBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,4,outfp,mode); + FreeAny(); + fclose(outfp); + return ret; + } } if (type==2) { - return PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size, outfp, mode); + ret = PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size, outfp, mode); + FreeAny(); + fclose(outfp); + return ret; } return 0; diff --git a/rosapps/devutils/cputointel/any_op.h b/rosapps/devutils/cputointel/any_op.h index 035f6887800..1803362d171 100644 --- a/rosapps/devutils/cputointel/any_op.h +++ b/rosapps/devutils/cputointel/any_op.h @@ -9,12 +9,15 @@ typedef struct _BrainAnalys CPU_INT type; /* 0 = source are memmory, 1 source are register */ /* 2 = dest are memmory, 4 dest are register */ /* 8 = source are imm */ + CPU_INT src_size; /* who many bits are src not vaild for reg*/ CPU_INT dst_size; /* who many bits are dst not vaild for reg*/ CPU_UNINT64 src; CPU_UNINT64 dst; + CPU_UNINT memAdr; /* where are we in the current memory pos + baseaddress */ + /* try translate the Adress to a name */ CPU_BYTE* ptr_next; /* hook next one */ CPU_BYTE* ptr_prev; /* hook previus one */ diff --git a/rosapps/devutils/cputointel/misc.c b/rosapps/devutils/cputointel/misc.c index 0234f9c0790..f75e6ddad04 100644 --- a/rosapps/devutils/cputointel/misc.c +++ b/rosapps/devutils/cputointel/misc.c @@ -159,6 +159,32 @@ CPU_INT AllocAny() return 0; } +CPU_INT FreeAny() +{ + PMYBrainAnalys tmp; + tmp = (PMYBrainAnalys)pMyBrainAnalys->ptr_prev; + + while (pMyBrainAnalys != NULL) + { + if (pMyBrainAnalys == NULL) + { + break; + } + + free(pMyBrainAnalys); + + if (pMyBrainAnalys != NULL) + { + printf("fail to free memory"); + return -1; + } + + pMyBrainAnalys = tmp; + } + + return 0; +} + diff --git a/rosapps/devutils/cputointel/misc.h b/rosapps/devutils/cputointel/misc.h index 3e428f99df2..127ef720a7f 100644 --- a/rosapps/devutils/cputointel/misc.h +++ b/rosapps/devutils/cputointel/misc.h @@ -24,3 +24,4 @@ CPU_UNINT GetData32Le(CPU_BYTE *cpu_buffer); CPU_UNINT GetData32Be(CPU_BYTE *cpu_buffer); CPU_INT AllocAny(); +CPU_INT FreeAny();