Exemple #1
0
 /// <summary>
 /// Public EndianBinaryWriter constructor
 /// </summary>
 /// <param name="str">Base Stream object</param>
 /// <param name="enc">Encoding object</param>
 /// <param name="endian">Endian object</param>
 public EndianBinaryWriter(Stream str, Encoding enc, Endian endian)
     : base(str, enc)
 {
     myEndian = endian;
 }
Exemple #2
0
 /// <summary>
 /// Public EndianBinaryWriter constructor
 /// </summary>
 /// <param name="str">Stream</param>
 /// <param name="endian">Endian object</param>
 public EndianBinaryWriter(Stream str, Endian endian)
     : base(str)
 {
     myEndian = endian;
 }
Exemple #3
0
        /// <summary>
        /// Parse the COFF file and put info into public Hashtables
        /// </summary>
        private void ParseObjectFile()
        {
            ELF_SectionHeader secHdr;

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

              // Parse the ELF header
              try
              {
            hdr = ELFFile.ParseELFHeader(binFile);
              }
              catch (Exception e)
              {
            Console.Write(e.Message);
            return;
              }

              // Determine the Endianness and use an EndianBinaryReader Object
              endian = (ELF_DataEncoding.ELFDATA_2LSB == hdr.e_ident.dataEncoding) ? Endian.LittleEndian : Endian.BigEndian;
              EndianBinaryReader ebr = new EndianBinaryReader(binFile,endian);

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

              // Read the section headers
              headerRef["numSectionHdrs"] = (UInt32) hdr.e_shnum;
              Debug.DebugMSG("numSectionHdrs: " + ((UInt32)headerRef["numSectionHdrs"]).ToString("X4"));
              headerRef["numBootSections"] = (UInt32)0;
              headerRef["numTargetSections"] = (UInt32)0;
              ParseSectionHdrs();

              // Find the symbol table section, symbol table entry size, and numbr of symbols
              secFind(".symtab");
              secHdr = ReadSectionHeader(currSecNum);
              if (secHdr.sh_type != ELF_SectionType.SHT_SYMTAB)
              {
            throw new Exception("Symbol table not found in file!");
              }
              headerRef["symbolTableAddr"] = (UInt64) secHdr.sh_offset;
              headerRef["symbolTableEntrySize"] = (UInt32) secHdr.sh_entsize;
              headerRef["numEntriesInSymTable"] =  (UInt32) (secHdr.sh_size / secHdr.sh_entsize);

              Debug.DebugMSG("symbolTableAddr: " + ((UInt64)headerRef["symbolTableAddr"]).ToString("X8"));
              Debug.DebugMSG("symbolTableEntrySize: " + ((UInt32)headerRef["symbolTableEntrySize"]).ToString("X8"));
              Debug.DebugMSG("numEntriesInSymTable: " + ((UInt32)headerRef["numEntriesInSymTable"]).ToString());

              // Find the string table section
              secFind(".strtab");
              secHdr = ReadSectionHeader(currSecNum);
              if (secHdr.sh_type != ELF_SectionType.SHT_STRTAB)
              {
            throw new Exception("String table not found in file!");
              }
              // Verify that the section header string table section is flagged as a string section
              if ( ( ((UInt64) secHdr.sh_flags) & ((UInt64) ELF_SectionFlag.SHF_STRINGS) ) == 0 )
              {
            throw new Exception("Section Header String Section is not flagged with SHF_STRINGS.");
              }

              headerRef["stringTableAddr"] = (UInt64) secHdr.sh_offset;
              Debug.DebugMSG("stringTableAddr: " + ((UInt64)headerRef["stringTableAddr"]).ToString("X8"));

              // Now parse the symbol table
              ParseSymbolTable();

              // Read the remaining pertinent info from header
              headerRef["versionID"] = (UInt32) hdr.e_version;
              Debug.DebugMSG("versionID: " + ((UInt32)headerRef["versionID"]).ToString("X4"));

              headerRef["flags"] = hdr.e_flags;
              Debug.DebugMSG("flags: " + ((UInt32)headerRef["flags"]).ToString("X8"));
        }
Exemple #4
0
 // Equals operator for this class
 public bool Equals(Endian endian)
 {
     return (bool)( this.endianness == endian.endianness);
 }
Exemple #5
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();
        }