private void ParseBinaryFile(UInt64 address)
        {
            // Output console message
            Console.WriteLine("Parsing the input file, {0}.", fileName);

            // Set endian to little
            endian = Endian.LittleEndian;

            // Set section count
            sectionCount         = 1;
            loadableSectionCount = 1;
            symbolCount          = 0;

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

            sections    = new ObjectSection[sectionCount];
            sections[0] = new ObjectSection();

            sections[0].name        = fileName;
            sections[0].loadAddr    = address;
            sections[0].runAddr     = address;
            sections[0].size        = (UInt64)binFile.Length;
            sections[0].size        = ((sections[0].size + 3) >> 2) << 2;
            sections[0].binFileAddr = 0x00000000;
            sections[0].isLoadable  = true;

            loadableSections    = new ObjectSection[loadableSectionCount];
            loadableSections[0] = sections[0];
        } // end ParseBinaryFile()
        public Byte[] secRead(ObjectSection section)
        {
            Byte[]             dataArr = new Byte[section.size];
            EndianBinaryReader ebr     = new EndianBinaryReader(this.binFile, this.endian);

            // Go to start of section in the binary file
            binFile.Seek((Int64)section.binFileAddr, SeekOrigin.Begin);

            // Read section bytes
            dataArr = ebr.ReadBytes((Int32)section.size);

            // Check to see if we need to pad the output array
            // Should only be needed for binary files
            if (dataArr.Length < (Int32)section.size)
            {
                Array.Resize(ref dataArr, (Int32)section.size);
            }

            return(dataArr);
        }
Пример #3
0
        } // end ParseCOFFFile()

        /// <summary>
        /// Parse the section headers.
        /// </summary>
        private void ParseSectionHdrs()
        {
            UInt32             numBytesInSectionHdr;
            EndianBinaryReader ebr = new EndianBinaryReader(binFile, endian);

            if (hdr.c_version == COFF_Version.COFF2)
            {
                numBytesInSectionHdr = 48;
            }
            else if (hdr.c_version == COFF_Version.COFF1)
            {
                numBytesInSectionHdr = 40;
            }
            else
            {
                numBytesInSectionHdr = 0;
            }

            sectionRef = new Hashtable[sectionCount];
            sections   = new ObjectSection[sectionCount];

            for (UInt16 secNum = 0; secNum < sectionCount; secNum++)
            {
                sectionRef[secNum] = new Hashtable();
                sections[secNum]   = new ObjectSection();

                ebr.BaseStream.Seek(numBytesInSectionHdr * secNum + COFFHeaderSize + hdr.c_ehsize, SeekOrigin.Begin);
                sections[secNum].name = COFF_getName();

                ebr.BaseStream.Seek(numBytesInSectionHdr * secNum + COFFHeaderSize + hdr.c_ehsize + 8, SeekOrigin.Begin);
                sections[secNum].runAddr     = (UInt64)ebr.ReadUInt32();
                sections[secNum].loadAddr    = (UInt64)ebr.ReadUInt32();
                sections[secNum].size        = (UInt64)ebr.ReadUInt32();
                sections[secNum].size        = ((sections[secNum].size + 3) >> 2) << 2;
                sections[secNum].binFileAddr = (UInt64)ebr.ReadUInt32();

                sectionRef[secNum]["reloPtr"] = ebr.ReadUInt32();
                sectionRef[secNum]["linePtr"] = ebr.ReadUInt32();

                if (hdr.c_version == COFF_Version.COFF2)
                {
                    sectionRef[secNum].Add("numRelos", ebr.ReadUInt32());
                    sectionRef[secNum].Add("numLines", ebr.ReadUInt32());
                    sectionRef[secNum].Add("flags", ebr.ReadUInt32());
                    sectionRef[secNum].Add("reserved", (UInt32)ebr.ReadUInt16());
                    sectionRef[secNum].Add("memPage", (UInt32)ebr.ReadUInt16());
                }
                else
                {
                    sectionRef[secNum].Add("numRelos", (UInt32)ebr.ReadUInt16());
                    sectionRef[secNum].Add("numLines", (UInt32)ebr.ReadUInt16());
                    sectionRef[secNum].Add("flags", (UInt32)ebr.ReadUInt16());
                    sectionRef[secNum].Add("reserved", (UInt32)ebr.ReadByte());
                    sectionRef[secNum].Add("memPage", (UInt32)ebr.ReadByte());
                }

                //Check to see if section is bootable
                UInt32 flags = (UInt32)sectionRef[secNum]["flags"];
                sectionRef[secNum]["bootable"] = false;
                if ((flags & ((UInt32)(COFF_SectionType.TEXT | COFF_SectionType.DATA))) != 0)
                {
                    if ((flags & ((UInt32)COFF_SectionType.COPY)) == 0)
                    {
                        if (sections[secNum].size != 0)
                        {
                            headerRef["numBootSections"]   = ((UInt32)headerRef["numBootSections"]) + 1;
                            sectionRef[secNum]["bootable"] = true;
                        }
                    }
                }

                // Check to see if section is loadable
                sections[secNum].isLoadable = false;
                if ((sections[secNum].binFileAddr != 0) && (sections[secNum].size != 0))
                {
                    if ((flags & ((UInt32)(COFF_SectionType.BSS |    // No BSS sections
                                           COFF_SectionType.COPY |   // No COPY sections
                                           COFF_SectionType.NOLOAD | // No NOLOAD sections
                                           COFF_SectionType.DUMMY))  // No DUMMY sections
                         ) == 0)
                    {
                        sections[secNum].isLoadable = true;
                        loadableSectionCount++;
                    }
                }

                Debug.DebugMSG("ObjectSection sections[" + secNum + "] = \n{");
                Debug.DebugMSG("\tname = " + sections[secNum].name + ",");
                Debug.DebugMSG("\tsize = " + sections[secNum].size.ToString("X8") + ",");
                Debug.DebugMSG("\trunAddr = " + sections[secNum].runAddr.ToString("X8") + ",");
                Debug.DebugMSG("\tloadAddr = " + sections[secNum].loadAddr.ToString("X8") + ",");
                Debug.DebugMSG("\tisLoadable = " + sections[secNum].isLoadable + ",");
                Debug.DebugMSG("\tbinFileAddr = " + sections[secNum].binFileAddr.ToString("X8"));
                Debug.DebugMSG("}");
            }

            // Fill in the loadableSections array
            loadableSections = new ObjectSection[loadableSectionCount];
            for (UInt32 secNum = 0, loadableSecNum = 0; secNum < sectionCount; secNum++)
            {
                if (sections[secNum].isLoadable)
                {
                    loadableSections[loadableSecNum] = sections[secNum];
                    loadableSecNum++;
                }
            }

            // Finally, sort the loadable sections array by load address
            Array.Sort <ObjectSection>(loadableSections);

            Debug.DebugMSG("Parse Section Headers Done");
        } // end of ParseSectionHdrs()
        public Byte[] secRead()
        {
            ObjectSection section = Sections[currSectionIndex];

            return(secRead(section));
        }