void CopyPropagation::TransformRelationInst(TACInstPtr& inst) { auto condInst = static_cast<TACRelationInst*>(inst.get()); /* Propagate copy */ ReadVar(condInst->srcLhs); ReadVar(condInst->srcRhs); }
FilePos* ReadFilePos (FILE* F, FilePos* Pos) /* Read a file position from the file */ { /* Read the data fields */ Pos->Line = ReadVar (F); Pos->Col = ReadVar (F); Pos->Name = ReadVar (F); return Pos; }
void CopyPropagation::TransformModifyInst(TACInstPtr& inst) { auto modifyInst = static_cast<TACModifyInst*>(inst.get()); /* Propagate copy */ ReadVar(modifyInst->srcLhs); ReadVar(modifyInst->srcRhs); KillCopy(modifyInst->dest); }
void ObjReadData (FILE* F, ObjData* O) /* Read object file data from the given file. The function expects the Name * and Start fields to be valid. Header and basic data are read. */ { unsigned long Count; /* Seek to the start of the object file data */ fseek (F, O->Start, SEEK_SET); /* Read the object file header */ ObjReadHeader (F, &O->Header, O->Name); /* Read the string pool */ fseek (F, O->Start + O->Header.StrPoolOffs, SEEK_SET); Count = ReadVar (F); CollGrow (&O->Strings, Count); while (Count--) { CollAppend (&O->Strings, ReadStr (F)); } /* Read the exports */ fseek (F, O->Start + O->Header.ExportOffs, SEEK_SET); Count = ReadVar (F); CollGrow (&O->Exports, Count); while (Count--) { unsigned char ConDes[CD_TYPE_COUNT]; /* Skip data until we get to the name */ unsigned Type = ReadVar (F); (void) Read8 (F); /* AddrSize */ ReadData (F, ConDes, SYM_GET_CONDES_COUNT (Type)); /* Now this is what we actually need: The name of the export */ CollAppend (&O->Exports, CollAt (&O->Strings, ReadVar (F))); /* Skip the export value */ if (SYM_IS_EXPR (Type)) { /* Expression tree */ SkipExpr (F); } else { /* Literal value */ (void) Read32 (F); } /* Skip the size if necessary */ if (SYM_HAS_SIZE (Type)) { (void) ReadVar (F); } /* Line info indices */ SkipLineInfoList (F); SkipLineInfoList (F); } }
void RefreshAllBoolsVars( ) { int NumVar; for (NumVar=0; NumVar<NBR_BOOLS_VAR_SPY; NumVar++) { gtk_toggle_button_set_active( (GtkToggleButton *)chkvar[0][NumVar], ReadVar(VAR_MEM_BIT,NumVar+ValOffsetBoolVar[ 0 ])?TRUE:FALSE ); gtk_toggle_button_set_active( (GtkToggleButton *)chkvar[1][NumVar], ReadVar(VAR_PHYS_INPUT,NumVar+ValOffsetBoolVar[ 1 ])?TRUE:FALSE ); gtk_toggle_button_set_active( (GtkToggleButton *)chkvar[2][NumVar], ReadVar(VAR_PHYS_OUTPUT,NumVar+ValOffsetBoolVar[ 2 ])?TRUE:FALSE ); } }
static void SkipLineInfoList (FILE* F) /* Skip a list of line infos in F */ { /* Number of indices preceeds the list */ unsigned long Count = ReadVar (F); /* Skip indices */ while (Count--) { (void) ReadVar (F); } }
void DoRead() { Match('('); GetName(); ReadVar(); while(Look == ',') { Match(','); GetName(); ReadVar(); } Match(')'); }
static void SkipSpanList (FILE* F) /* Skip a span list from the given file */ { /* Count preceeds the list */ unsigned long Count = ReadVar (F); /* Skip indices */ while (Count--) { (void) ReadVar (F); } }
void DumpObjImports (FILE* F, unsigned long Offset) /* Dump the imports in the object file */ { ObjHeader H; Collection StrPool = AUTO_COLLECTION_INITIALIZER; unsigned Count; unsigned I; /* Seek to the header position and read the header */ FileSetPos (F, Offset); ReadObjHeader (F, &H); /* Seek to the start of the string pool and read it */ FileSetPos (F, Offset + H.StrPoolOffs); ReadStrPool (F, &StrPool); /* Seek to the start of the imports */ FileSetPos (F, Offset + H.ImportOffs); /* Output a header */ printf (" Imports:\n"); /* Read the number of imports and print it */ Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all imports */ for (I = 0; I < Count; ++I) { /* Read the data for one import */ unsigned char AddrSize = Read8 (F); const char* Name = GetString (&StrPool, ReadVar (F)); unsigned Len = strlen (Name); /* Skip both line info lists */ SkipLineInfoList (F); SkipLineInfoList (F); /* Print the header */ printf (" Index:%27u\n", I); /* Print the data */ printf (" Address size:%14s0x%02X (%s)\n", "", AddrSize, AddrSizeToStr (AddrSize)); printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name); } /* Destroy the string pool */ DestroyStrPool (&StrPool); }
ExprNode* ReadExpr (FILE* F, ObjData* O) /* Read an expression from the given file */ { ExprNode* Expr; /* Read the node tag and handle NULL nodes */ unsigned char Op = Read8 (F); if (Op == EXPR_NULL) { return 0; } /* Create a new node */ Expr = NewExprNode (O, Op); /* Check the tag and handle the different expression types */ if (EXPR_IS_LEAF (Op)) { switch (Op) { case EXPR_LITERAL: Expr->V.IVal = Read32Signed (F); break; case EXPR_SYMBOL: /* Read the import number */ Expr->V.ImpNum = ReadVar (F); break; case EXPR_SECTION: /* Read the section number */ Expr->V.SecNum = ReadVar (F); break; default: Error ("Invalid expression op: %02X", Op); } } else { /* Not a leaf node */ Expr->Left = ReadExpr (F, O); Expr->Right = ReadExpr (F, O); } /* Return the tree */ return Expr; }
static void ReadIndex (void) /* Read the index of a library file */ { unsigned Count, I; /* Seek to the start of the index */ fseek (Lib, Header.IndexOffs, SEEK_SET); /* Read the object file count and calculate the cross ref size */ Count = ReadVar (Lib); /* Read all entries in the index */ while (Count--) { ReadIndexEntry (); } /* Read basic object file data from the actual entries */ for (I = 0; I < CollCount (&ObjPool); ++I) { /* Get the object file entry */ ObjData* O = CollAtUnchecked (&ObjPool, I); /* Read data */ ObjReadData (Lib, O); } }
void CopyPropagation::TransformSwitchInst(TACInstPtr& inst) { auto switchInst = static_cast<TACSwitchInst*>(inst.get()); /* Propagate copy */ ReadVar(switchInst->src); }
static void SkipExpr (FILE* F) /* Skip an expression in F */ { /* Get the operation and skip it */ unsigned char Op = Read8 (F); /* Handle then different expression nodes */ switch (Op) { case EXPR_NULL: break; case EXPR_LITERAL: /* 32 bit literal value */ (void) Read32 (F); break; case EXPR_SYMBOL: /* Variable seized symbol index */ (void) ReadVar (F); break; case EXPR_SECTION: /* 8 bit segment number */ (void) Read8 (F); break; default: /* What's left are unary and binary nodes */ SkipExpr (F); /* Left */ SkipExpr (F); /* right */ break; } }
void DumpObjFiles (FILE* F, unsigned long Offset) /* Dump the source files */ { ObjHeader H; Collection StrPool = AUTO_COLLECTION_INITIALIZER; unsigned Count; unsigned I; /* Seek to the header position and read the header */ FileSetPos (F, Offset); ReadObjHeader (F, &H); /* Seek to the start of the string pool and read it */ FileSetPos (F, Offset + H.StrPoolOffs); ReadStrPool (F, &StrPool); /* Seek to the start of the source files */ FileSetPos (F, Offset + H.FileOffs); /* Output a header */ printf (" Files:\n"); /* Read the number of files and print it */ Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all files */ for (I = 0; I < Count; ++I) { /* Read the data for one file */ const char* Name = GetString (&StrPool, ReadVar (F)); unsigned long MTime = Read32 (F); unsigned long Size = ReadVar (F); unsigned Len = strlen (Name); /* Print the header */ printf (" Index:%27u\n", I); /* Print the data */ printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name); printf (" Size:%26lu\n", Size); printf (" Modification time:%13lu (%s)\n", MTime, TimeToStr (MTime)); } /* Destroy the string pool */ DestroyStrPool (&StrPool); }
void CopyPropagation::TransformReturnInst(TACInstPtr& inst) { auto returnInst = static_cast<TACReturnInst*>(inst.get()); /* Propagate copy */ if (returnInst->hasVar) ReadVar(returnInst->src); }
void CopyPropagation::TransformHeapInst(TACInstPtr& inst) { auto heapInst = static_cast<TACHeapInst*>(inst.get()); /* Propagate copy */ if (heapInst->IsStoreOp()) ReadVar(heapInst->var); else KillCopy(heapInst->var); }
void ReadStrPool (FILE* F, Collection* C) /* Read a string pool from the current position into C. */ { /* The number of strings is the first item */ unsigned long Count = ReadVar (F); /* Read all the strings into C */ while (Count--) { CollAppend (C, ReadStr (F)); } }
void CopyPropagation::TransformCopyInst(TACInstPtr& inst) { auto copyInst = static_cast<TACCopyInst*>(inst.get()); if (!copyInst->src.IsLabel()) { /* Propagate copy */ ReadVar(copyInst->src); WriteVar(copyInst->dest, copyInst->src); } }
Export* ReadExport (FILE* F, ObjData* O) /* Read an export from a file */ { unsigned ConDesCount; Export* E; /* Read the type */ unsigned char Type = Read8 (F); /* Read the address size */ unsigned char AddrSize = Read8 (F); /* Create a new export without a name */ E = NewExport (Type, AddrSize, INVALID_STRING_ID, O); /* Read the constructor/destructor decls if we have any */ ConDesCount = GET_EXP_CONDES_COUNT (Type); if (ConDesCount > 0) { unsigned char ConDes[CD_TYPE_COUNT]; unsigned I; /* Read the data into temp storage */ ReadData (F, ConDes, ConDesCount); /* Re-order the data. In the file, each decl is encoded into a byte * which contains the type and the priority. In memory, we will use * an array of types which contain the priority. This array was * cleared by the constructor (NewExport), so we must only set the * fields that contain values. */ for (I = 0; I < ConDesCount; ++I) { unsigned ConDesType = CD_GET_TYPE (ConDes[I]); unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]); E->ConDes[ConDesType] = ConDesPrio; } } /* Read the name */ E->Name = MakeGlobalStringId (O, ReadVar (F)); /* Read the value */ if (IS_EXP_EXPR (Type)) { E->Expr = ReadExpr (F, O); } else { E->Expr = LiteralExpr (Read32 (F), O); } /* Last is the file position where the definition was done */ ReadFilePos (F, &E->Pos); /* Return the new export */ return E; }
static void SkipExpr (FILE* F) /* Skip an expression from the given file */ { /* Read the node tag and handle NULL nodes */ unsigned char Op = Read8 (F); if (Op == EXPR_NULL) { return; } /* Check the tag and handle the different expression types */ if (EXPR_IS_LEAF (Op)) { switch (Op) { case EXPR_LITERAL: (void) Read32Signed (F); break; case EXPR_SYMBOL: /* Read the import number */ (void) ReadVar (F); break; case EXPR_SECTION: case EXPR_BANK: /* Read the segment number */ (void) ReadVar (F); break; default: Error ("Invalid expression op: %02X", Op); } } else { /* Not a leaf node */ SkipExpr (F); SkipExpr (F); } }
Assertion* ReadAssertion (FILE* F, struct ObjData* O) /* Read an assertion from the given file */ { /* Allocate memory */ Assertion* A = xmalloc (sizeof (Assertion)); /* Read the fields from the file */ A->LineInfos = EmptyCollection; A->Expr = ReadExpr (F, O); A->Action = (AssertAction) ReadVar (F); A->Msg = MakeGlobalStringId (O, ReadVar (F)); ReadLineInfoList (F, O, &A->LineInfos); /* Set remaining fields */ A->Obj = O; /* Add the assertion to the global list */ CollAppend (&Assertions, A); /* Return the new struct */ return A; }
void DumpObjSegments (FILE* F, unsigned long Offset) /* Dump the segments in the object file */ { ObjHeader H; Collection StrPool = AUTO_COLLECTION_INITIALIZER; unsigned Count; unsigned I; /* Seek to the header position and read the header */ FileSetPos (F, Offset); ReadObjHeader (F, &H); /* Seek to the start of the string pool and read it */ FileSetPos (F, Offset + H.StrPoolOffs); ReadStrPool (F, &StrPool); /* Seek to the start of the segments */ FileSetPos (F, Offset + H.SegOffs); /* Output a header */ printf (" Segments:\n"); /* Read the number of segments and print it */ Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all segments */ for (I = 0; I < Count; ++I) { /* Read the data for one segments */ unsigned long DataSize = Read32 (F); unsigned long NextSeg = ftell (F) + DataSize; const char* Name = GetString (&StrPool, ReadVar (F)); unsigned Len = strlen (Name); unsigned Flags = ReadVar (F); unsigned long Size = ReadVar (F); unsigned long Align = ReadVar (F); unsigned char AddrSize = Read8 (F); unsigned long FragCount = ReadVar (F); /* Print the header */ printf (" Index:%27u\n", I); /* Print the data */ printf (" Name:%*s\"%s\"\n", (int)(24-Len), "", Name); printf (" Flags:%25u\n", Flags); printf (" Size:%26lu\n", Size); printf (" Alignment:%21lu\n", Align); printf (" Address size:%14s0x%02X (%s)\n", "", AddrSize, AddrSizeToStr (AddrSize)); printf (" Fragment count:%16lu\n", FragCount); /* Seek to the end of the segment data (start of next) */ FileSetPos (F, NextSeg); } /* Destroy the string pool */ DestroyStrPool (&StrPool); }
FileInfo* ReadFileInfo (FILE* F, ObjData* O) /* Read a file info from a file and return it */ { /* Allocate a new FileInfo structure */ FileInfo* FI = NewFileInfo (); /* Read the fields from the file */ FI->Name = MakeGlobalStringId (O, ReadVar (F)); FI->MTime = Read32 (F); FI->Size = Read32 (F); /* Return the new struct */ return FI; }
void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O) /* Read the line infos from a file at the given position */ { unsigned I; /* Seek to the correct position */ FileSetPos (F, Pos); /* Read the data */ O->LineInfoCount = ReadVar (F); O->LineInfos = xmalloc (O->LineInfoCount * sizeof (O->LineInfos[0])); for (I = 0; I < O->LineInfoCount; ++I) { O->LineInfos[I] = ReadLineInfo (F, O); } }
void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O) /* Read the string pool from a file at the given position */ { unsigned I; /* Seek to the correct position */ FileSetPos (F, Pos); /* Read the data */ O->StringCount = ReadVar (F); O->Strings = xmalloc (O->StringCount * sizeof (O->Strings[0])); for (I = 0; I < O->StringCount; ++I) { O->Strings[I] = ReadStr (F); } }
void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O) /* Read the assertions from a file at the given offset */ { unsigned I; /* Seek to the correct position */ FileSetPos (F, Pos); /* Read the data */ O->AssertionCount = ReadVar (F); O->Assertions = xmalloc (O->AssertionCount * sizeof (O->Assertions[0])); for (I = 0; I < O->AssertionCount; ++I) { O->Assertions[I] = ReadAssertion (F, O); } }
void ObjReadScopes (FILE* F, unsigned long Pos, ObjData* O) /* Read the scope table from a file at the given offset */ { unsigned I; /* Seek to the correct position */ FileSetPos (F, Pos); /* Read the data */ O->ScopeCount = ReadVar (F); O->Scopes = xmalloc (O->ScopeCount * sizeof (O->Scopes[0])); for (I = 0; I < O->ScopeCount; ++I) { O->Scopes[I] = 0; /* ReadScope (F, O); ### not implemented */ } }
void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O) /* Read the debug symbols from a file at the given position */ { unsigned I; /* Seek to the correct position */ FileSetPos (F, Pos); /* Read the data */ O->DbgSymCount = ReadVar (F); O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (O->DbgSyms[0])); for (I = 0; I < O->DbgSymCount; ++I) { O->DbgSyms [I] = ReadDbgSym (F, O); } }
/* Give final variable (taking into acount the value of an index if present) */ int IdentifyFinalVar( char *StartExpr, int * ResType,int * ResOffset ) { int IndexVarType,IndexVarOffset; int SyntaxOk = IdentifyVarIndexedOrNot( StartExpr, ResType, ResOffset, &IndexVarType, &IndexVarOffset ); if ( SyntaxOk ) { if ( IndexVarType!=-1 && IndexVarOffset!=-1 ) { // add index value from content of the index variable int IndexValue = ReadVar( IndexVarType, IndexVarOffset ); *ResOffset = *ResOffset + IndexValue; } //printf("Final var for %s => %d/%d\n", StartExpr, *ResType, *ResOffset ); } return SyntaxOk; }
char* ReadStr (FILE* F) /* Read a string from the file into a malloced area */ { /* Read the length */ unsigned Len = ReadVar (F); /* Allocate memory */ char* Str = xmalloc (Len + 1); /* Read the string itself */ ReadData (F, Str, Len); /* Terminate the string and return it */ Str [Len] = '\0'; return Str; }