Beispiel #1
0
        //- reading in ----------------------------------------------------------------

        public static CoffSection readSection(BinaryIn source)
        {
            string      name = source.getAsciiString(8);
            CoffSection sec  = new CoffSection(name);

            sec.memSize  = source.getFour();
            sec.memPos   = source.getFour();
            sec.fileSize = source.getFour();
            sec.filePos  = source.getFour();

            sec.relocTblPos = source.getFour();
            uint skip1 = source.getFour();              //line numbers are deprecated

            sec.relocTblCount = source.getTwo();
            uint skip2 = source.getTwo();

            uint flagval = source.getFour();

            sec.settings = SectionSettings.decodeFlags(flagval);

            uint mark = source.getPos();

            source.seek(sec.filePos);
            byte[] secdata = source.getRange(sec.fileSize);
            sec.data = new List <byte>(secdata);
            source.seek(mark);

            return(sec);
        }
Beispiel #2
0
        //new section cons
        public CoffSection(String _name)
        {
            secNum = 0;
            name   = _name;

            memSize  = 0;
            memPos   = 0;
            fileSize = 0;
            filePos  = 0;

            settings = new SectionSettings();

            relocations = new List <CoffRelocation>();

            data = new List <byte>();

            relocTblPos   = 0;
            relocTblCount = 0;
        }
Beispiel #3
0
        internal static SectionSettings decodeFlags(uint flags)
        {
            SectionSettings settings = new SectionSettings();

            settings.hasCode       = (flags & 0x20) != 0;
            settings.hasInitData   = (flags & 0x40) != 0;
            settings.hasUninitData = (flags & 0x80) != 0;
            settings.hasInfo       = (flags & 0x200) != 0;
            settings.willRemove    = (flags & 0x800) != 0;
            settings.hasComdat     = (flags & 0x1000) != 0;
            settings.hasGPRel      = (flags & 0x8000) != 0;
            settings.hasExtRelocs  = (flags & 0x01000000) != 0;
            settings.canDiscard    = (flags & 0x02000000) != 0;
            settings.dontCache     = (flags & 0x04000000) != 0;
            settings.notPageable   = (flags & 0x08000000) != 0;
            settings.canShare      = (flags & 0x10000000) != 0;
            settings.canExecute    = (flags & 0x20000000) != 0;
            settings.canRead       = (flags & 0x40000000) != 0;
            settings.canWrite      = (flags & 0x80000000) != 0;

            settings.dataAlignment = DATAALIGNMENTS[(flags & 0x00F00000) >> 20];

            return(settings);
        }
Beispiel #4
0
 public CoffSection(String _name, SectionSettings _settings)
     : this(_name)
 {
     settings = _settings;
 }
Beispiel #5
0
        public static Win32Obj readFromFile(String filename)
        {
            Win32Obj objfile = new Win32Obj(filename);
            BinaryIn source  = new BinaryIn(filename);

            //coff header
            objfile.machine = (MachineType)source.getTwo();
            uint sectionCount = source.getTwo();

            objfile.timeStamp = source.getFour();
            uint symbolTblAddr   = source.getFour();
            uint symbolCount     = source.getFour();
            uint optionalHdrSize = source.getTwo();

            objfile.characteristics = (int)source.getTwo();

            //string tbl - follows symbol tbl
            uint strtblpos = symbolTblAddr + symbolCount * CoffSymbol.SYMTBLENTRYSIZE;

            source.seek(strtblpos);
            byte[] strtbl = null;
            uint   len    = source.getFour();

            if (len > 4)
            {
                source.seek(strtblpos);
                strtbl = source.getRange(len);
            }

            //section tbl
            source.seek(COFFHDRSIZE);
            for (int i = 0; i < sectionCount; i++)
            {
                //if section name is stored in string tbl, we read in index & let caller deref the actual name
                String secname = source.getAsciiString(8);
                if (secname[0] == '/')
                {
                    int stridx = Int32.Parse(secname.Substring(1));
                    secname = readString(strtbl, stridx);
                }

                //read section hdr field
                uint memSize  = source.getFour();       //don't use - 0 in object files
                uint memPos   = source.getFour();
                uint fileSize = source.getFour();
                uint filePos  = source.getFour();

                uint relocPos     = source.getFour();
                uint lineNumPos   = source.getFour();   //don't use - deprecated
                uint relocCount   = source.getTwo();
                uint lineNumCount = source.getTwo();    //don't use
                uint flagval      = source.getFour();

                SectionSettings settings = SectionSettings.decodeFlags(flagval);
                CoffSection     section  = new CoffSection(secname, settings);
                section.owner         = objfile;
                section.secNum        = i + 1;
                section.memPos        = memPos;
                section.fileSize      = fileSize;
                section.filePos       = filePos;
                section.relocTblPos   = relocPos;
                section.relocTblCount = relocCount;

                objfile.sections.Add(section);
                objfile.secNames[section.name] = section;
            }

            //load symbols
            source.seek(symbolTblAddr);
            loadSymbols(source, symbolCount, strtbl, objfile);

            foreach (CoffSection section in objfile.sections)
            {
                //load section data
                section.data = new List <Byte>(source.getRange(section.filePos, section.fileSize));

                //load sectionrelocs
                loadRelocations(source, section, objfile);
            }


            return(objfile);
        }