Пример #1
0
        /// <summary>
        /// Parse the COFF file and put info into public Hashtables
        /// </summary>
        private void ParseCOFFFile()
        {
            UInt32 magicNum;
            UInt32 thisCoff;

            // Output console message
            Console.WriteLine("Parsing the input COFF file, {0}.", (String)headerRef["fullName"]);

            // Check for COFF magic number and endianness
            binFile.Seek(20, SeekOrigin.Begin);
            magicNum = ((UInt32)binFile.ReadByte() << 8) | ((UInt32)binFile.ReadByte());
            Debug.DebugMSG("MagicNum: " + magicNum.ToString("X4"));
            if (magicNum == 0x9900)
            {
                endian = Endian.LittleEndian;
            }
            else if (magicNum == 0x0099)
            {
                endian = Endian.BigEndian;
            }
            else
            {
                throw new Exception("Invalid COFF magic number " + magicNum.ToString("X4"));
            }

            // Set endianness
            headerRef["endian"] = endian;
            Debug.DebugMSG("Endianness: " + endian.ToString());

            binFile.Seek(0, SeekOrigin.Begin);
            EndianBinaryReader COFFbr = new EndianBinaryReader(binFile, endian);

            thisCoff = (UInt32)COFFbr.ReadUInt16();
            if (thisCoff == 0x0099)
            {
                COFFVersion = COFFType.COFF0;
            }
            else if (thisCoff == (UInt16)COFFType.COFF1)
            {
                COFFVersion = COFFType.COFF1;
            }
            else if (thisCoff == (UInt16)COFFType.COFF2)
            {
                COFFVersion = COFFType.COFF2;
            }

            // Read the main COFF header
            headerRef["versionID"] = thisCoff;
            Debug.DebugMSG("versionID: " + thisCoff.ToString("X4"));

            headerRef["numSectionHdrs"] = (UInt32)COFFbr.ReadUInt16();
            Debug.DebugMSG("numSectionHdrs: " + ((UInt32)headerRef["numSectionHdrs"]).ToString("X4"));

            headerRef["dateStamp"] = COFFbr.ReadUInt32();
            Debug.DebugMSG("datestamp: " + ((UInt32)headerRef["dateStamp"]).ToString("X8"));

            headerRef["symbolTableAddr"] = COFFbr.ReadUInt32();
            Debug.DebugMSG("symbolTableAddr: " + ((UInt32)headerRef["symbolTableAddr"]).ToString("X8"));

            headerRef["numEntriesInSymTable"] = COFFbr.ReadUInt32();
            Debug.DebugMSG("numEntriesInSymTable: " + ((UInt32)headerRef["numEntriesInSymTable"]).ToString("X8"));

            numBytesInOptHdr = (UInt32)COFFbr.ReadUInt16();
            headerRef["numBytesInOptHdr"] = numBytesInOptHdr;
            Debug.DebugMSG("numBytesInOptHdr: " + ((UInt32)headerRef["numBytesInOptHdr"]).ToString("X4"));

            headerRef["flags"] = (UInt32)COFFbr.ReadUInt16();
            Debug.DebugMSG("flags: " + ((UInt32)headerRef["flags"]).ToString("X4"));

            Debug.DebugMSG("COFFType: " + ((UInt32)COFFType.COFF0).ToString());

            if (COFFVersion != COFFType.COFF0)
            {
                headerRef["magicNum"] = (UInt32)COFFbr.ReadUInt16();
                Debug.DebugMSG("magicNum: " + ((UInt32)headerRef["magicNum"]).ToString("X4"));
                numBytesInHdr = 22;
            }
            else
            {
                numBytesInHdr = 20;
            }

            headerRef["numBootSections"]   = (UInt32)0;
            headerRef["numTargetSections"] = (UInt32)0;

            // Read the optional COFF header
            if (numBytesInOptHdr != 0)
            {
                COFFbr.BaseStream.Seek(numBytesInHdr, SeekOrigin.Begin);
                headerRef["optMagicNumber"] = (UInt32)COFFbr.ReadUInt16();
                Debug.DebugMSG("optMagicNumber: " + ((UInt32)headerRef["optMagicNumber"]).ToString("X4"));

                headerRef["optVersionStamp"] = (UInt32)COFFbr.ReadUInt16();
                Debug.DebugMSG("optVersionStamp: " + ((UInt32)headerRef["optVersionStamp"]).ToString("X4"));

                headerRef["optExeSize"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optExeSize: " + ((UInt32)headerRef["optExeSize"]).ToString("X8"));

                headerRef["optInitSize"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optInitSize: " + ((UInt32)headerRef["optInitSize"]).ToString("X8"));

                headerRef["optUninitSize"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optUninitSize: " + ((UInt32)headerRef["optUninitSize"]).ToString("X8"));

                headerRef["optEntryPoint"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optEntryPoint: " + ((UInt32)headerRef["optEntryPoint"]).ToString("X8"));

                headerRef["optExeAddr"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optExeAddr: " + ((UInt32)headerRef["optExeAddr"]).ToString("X8"));

                headerRef["optInitAddr"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optInitAddr: " + ((UInt32)headerRef["optInitAddr"]).ToString("X8"));
            }

            // Read the section headers
            ParseSectionHdrs();

            // Parse the symbol table
            ParseSymbolTable();
        } // end ParseCOFFFile()
Пример #2
0
        /// <summary>
        /// Parse the COFF file and put info into public Hashtables
        /// </summary>
        private void ParseCOFFFile()
        {
            UInt32 magicNum;
            UInt32 thisCoff;
            // Output console message
            Console.WriteLine("Parsing the input COFF file, {0}.", (String) headerRef["fullName"]);

            // Check for COFF magic number and endianness
            binFile.Seek(20, SeekOrigin.Begin);
            magicNum = ((UInt32)binFile.ReadByte() << 8) | ((UInt32)binFile.ReadByte());
            Debug.DebugMSG("MagicNum: " + magicNum.ToString("X4"));
            if (magicNum == 0x9900)
                endian = Endian.LittleEndian;
            else if (magicNum == 0x0099)
                endian = Endian.BigEndian;
            else
                throw new Exception("Invalid COFF magic number " + magicNum.ToString("X4"));

            // Set endianness
            headerRef["endian"] = endian;
            Debug.DebugMSG("Endianness: " + endian.ToString());

            binFile.Seek(0, SeekOrigin.Begin);
            EndianBinaryReader COFFbr = new EndianBinaryReader(binFile,endian);

            thisCoff = (UInt32) COFFbr.ReadUInt16();
            if (thisCoff == 0x0099)
            {
                COFFVersion = COFFType.COFF0;
            }
            else if (thisCoff == (UInt16)COFFType.COFF1)
            {
                COFFVersion = COFFType.COFF1;
            }
            else if (thisCoff == (UInt16)COFFType.COFF2)
            {
                COFFVersion = COFFType.COFF2;
            }

            // Read the main COFF header
            headerRef["versionID"] = thisCoff;
            Debug.DebugMSG("versionID: " + thisCoff.ToString("X4"));

            headerRef["numSectionHdrs"] = (UInt32) COFFbr.ReadUInt16();
            Debug.DebugMSG("numSectionHdrs: " + ((UInt32)headerRef["numSectionHdrs"]).ToString("X4"));

            headerRef["dateStamp"] = COFFbr.ReadUInt32();
            Debug.DebugMSG("datestamp: " + ((UInt32)headerRef["dateStamp"]).ToString("X8"));

            headerRef["symbolTableAddr"] = COFFbr.ReadUInt32();
            Debug.DebugMSG("symbolTableAddr: " + ((UInt32)headerRef["symbolTableAddr"]).ToString("X8"));

            headerRef["numEntriesInSymTable"] = COFFbr.ReadUInt32();
            Debug.DebugMSG("numEntriesInSymTable: " + ((UInt32)headerRef["numEntriesInSymTable"]).ToString("X8"));

            numBytesInOptHdr = (UInt32) COFFbr.ReadUInt16();
            headerRef["numBytesInOptHdr"] = numBytesInOptHdr;
            Debug.DebugMSG("numBytesInOptHdr: " + ((UInt32)headerRef["numBytesInOptHdr"]).ToString("X4"));

            headerRef["flags"] = (UInt32) COFFbr.ReadUInt16();
            Debug.DebugMSG("flags: " + ((UInt32)headerRef["flags"]).ToString("X4"));

            Debug.DebugMSG("COFFType: " + ((UInt32)COFFType.COFF0).ToString());

            if (COFFVersion != COFFType.COFF0)
            {
                headerRef["magicNum"] = (UInt32) COFFbr.ReadUInt16();
                Debug.DebugMSG("magicNum: " + ((UInt32)headerRef["magicNum"]).ToString("X4"));
                numBytesInHdr = 22;
            }
            else
            {
                numBytesInHdr = 20;
            }

            headerRef["numBootSections"] = (UInt32)0;
            headerRef["numTargetSections"] = (UInt32)0;

            // Read the optional COFF header
            if (numBytesInOptHdr != 0)
            {
                COFFbr.BaseStream.Seek(numBytesInHdr,SeekOrigin.Begin);
                headerRef["optMagicNumber"] = (UInt32) COFFbr.ReadUInt16();
                Debug.DebugMSG("optMagicNumber: " + ((UInt32)headerRef["optMagicNumber"]).ToString("X4"));

                headerRef["optVersionStamp"] = (UInt32)COFFbr.ReadUInt16();
                Debug.DebugMSG("optVersionStamp: " + ((UInt32)headerRef["optVersionStamp"]).ToString("X4"));

                headerRef["optExeSize"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optExeSize: " + ((UInt32)headerRef["optExeSize"]).ToString("X8"));

                headerRef["optInitSize"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optInitSize: " + ((UInt32)headerRef["optInitSize"]).ToString("X8"));

                headerRef["optUninitSize"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optUninitSize: " + ((UInt32)headerRef["optUninitSize"]).ToString("X8"));

                headerRef["optEntryPoint"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optEntryPoint: " + ((UInt32)headerRef["optEntryPoint"]).ToString("X8"));

                headerRef["optExeAddr"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optExeAddr: " + ((UInt32)headerRef["optExeAddr"]).ToString("X8"));

                headerRef["optInitAddr"] = COFFbr.ReadUInt32();
                Debug.DebugMSG("optInitAddr: " + ((UInt32)headerRef["optInitAddr"]).ToString("X8"));
            }

            // Read the section headers
            ParseSectionHdrs();

            // Parse the symbol table
            ParseSymbolTable();
        }