Ejemplo n.º 1
0
        /*-------------------- Constructors ---------------------------------*/

        /// <summary>
        /// Create a new PE File with the name "fileName".  If "fileName" ends in ".dll" then
        /// the file is a dll, otherwise it is an exe file.  This PE File has no assembly.
        /// </summary>
        /// <param name="fileName">Name for the output file.</param>
        public PEFile(string fileName)
            : base(fileName)
        {
            Contract.Requires(fileName != null);
            //PrimitiveType.ClearAddedFlags();   // KJG 18-April-2005 - Now done in MetaDataOut
            this.fileName = fileName;
            metaData      = new MetaDataOut();
            versionInfo   = new PEFileVersionInfo();
            versionInfo.SetDefaults(fileName);
        }
Ejemplo n.º 2
0
        private System.IO.FileStream unmanagedResources; // Unmanaged resources read from a file.

        #endregion Fields

        #region Constructors

        /*-------------------- Constructors ---------------------------------*/
        /// <summary>
        /// Create a new PE File with the name "fileName".  If "fileName" ends in ".dll" then
        /// the file is a dll, otherwise it is an exe file.  This PE File has no assembly.
        /// </summary>
        /// <param name="fileName">Name for the output file.</param>
        public PEFile(string fileName)
            : base(fileName)
        {
            Contract.Requires(fileName != null);
            //PrimitiveType.ClearAddedFlags();   // KJG 18-April-2005 - Now done in MetaDataOut
            this.fileName = fileName;
            metaData = new MetaDataOut();
            versionInfo = new PEFileVersionInfo();
            versionInfo.SetDefaults(fileName);
        }
Ejemplo n.º 3
0
        /*-------------------- Constructors ---------------------------------*/

        internal PEWriter(PEFileVersionInfo verInfo, string fileName, MetaDataOut md, bool writePDB)
            : base(new FileStream(fileName, FileMode.Create))
        {
            Contract.Requires(verInfo != null);
            Contract.Requires(fileName != null);
            Contract.Requires(md != null);
            InitPEWriter(verInfo, md, writePDB, fileName);
            TimeSpan tmp = System.IO.File.GetCreationTime(fileName).Subtract(FileImage.origin);

            dateStamp = Convert.ToUInt32(tmp.TotalSeconds);
        }
Ejemplo n.º 4
0
        internal PEWriter(PEFileVersionInfo verInfo, Stream str, MetaDataOut md)
            : base(str)
        {
            Contract.Requires(verInfo != null);
            Contract.Requires(str != null);
            Contract.Requires(md != null);
            // NOTE: Can not write a PDB file if using a stream.
            InitPEWriter(verInfo, md, false, null);
            TimeSpan tmp = DateTime.Now.Subtract(FileImage.origin);

            dateStamp   = Convert.ToUInt32(tmp.TotalSeconds);
            closeStream = false;
        }
Ejemplo n.º 5
0
        internal void MakeFile(PEFileVersionInfo verInfo)
        {
            Contract.Requires(verInfo != null);
            this.verInfo = verInfo;
            if (this.verInfo.isDLL)
            {
                hintNameTable = FileImage.dllHintNameTable.ToCharArray();
            }
            else
            {
                hintNameTable = FileImage.exeHintNameTable.ToCharArray();
            }

            BuildTextSection();
            CalcOffsets();
            BuildRelocSection();
            // now write it out
            WriteHeader();
            WriteSections();
            Flush();
            if (closeStream)
            {
                Close();
            }
            if (pdbWriter != null)
            {
                // Write the PDB file
                pdbWriter.WritePDBFile();

                // Check to make sure the DebugInfo is the length we expected.
                if (pdbWriter.DebugInfo.Length != debugBytesSize)
                {
                    throw new Exception("DebugInfo for the new PDB file is incompatible with the PE file.  This is most likely an internal error.  Please consult your vendor if you continue to have this problem.");
                }

                // Write the debug info to the PE file
                using (FileStream fs = new FileStream(pdbWriter.PEFilename, FileMode.Open, FileAccess.ReadWrite))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        // Get to the DebugInfo section
                        bw.Seek((int)debugBytesStartOffset, SeekOrigin.Begin);
                        bw.Write(pdbWriter.DebugInfo, 0, pdbWriter.DebugInfo.Length);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /*----------------------------- Writing -----------------------------------------*/

        private void InitPEWriter(PEFileVersionInfo verInfo, MetaDataOut md, bool writePDB, string fileName)
        {
            Contract.Requires(verInfo != null);
            Contract.Requires(md != null);
            Contract.Requires(fileName != null);
            this.verInfo = verInfo;
            if (!verInfo.fromExisting)
            {
                verInfo.lMajor = MetaData.LMajors[(int)verInfo.netVersion];
            }
            if (verInfo.isDLL)
            {
                hintNameTable = FileImage.dllHintNameTable.ToCharArray();
                if (!verInfo.fromExisting)
                {
                    verInfo.characteristics = FileImage.dllCharacteristics;
                }
            }
            else
            {
                hintNameTable = FileImage.exeHintNameTable.ToCharArray();
                if (!verInfo.fromExisting)
                {
                    verInfo.characteristics = FileImage.exeCharacteristics;
                }
            }
            text     = new Section(FileImage.textName, 0x60000020); // IMAGE_SCN_CNT  CODE, EXECUTE, READ
            metaData = md;
            metaData.InitMetaDataOut(this);

            // Check if we should include a PDB file
            if (writePDB)
            {
                // Work out the PDB filename from the PE files filename
                if ((fileName == null) || (fileName == ""))
                {
                    fileName = "default";
                }

                // Setup the PDB Writer object
                pdbWriter = new PDBWriter(fileName);

                // Set the amount of space required for the debug information
                debugBytesSize += pdbWriter.PDBFilename.Length;
            }
        }
Ejemplo n.º 7
0
 internal PEWriter(PEFileVersionInfo verInfo, Stream str, MetaDataOut md)
     : base(str)
 {
     Contract.Requires(verInfo != null);
     Contract.Requires(str != null);
     Contract.Requires(md != null);
     // NOTE: Can not write a PDB file if using a stream.
     InitPEWriter(verInfo, md, false, null);
     TimeSpan tmp = DateTime.Now.Subtract(FileImage.origin);
     dateStamp = Convert.ToUInt32(tmp.TotalSeconds);
     closeStream = false;
 }
Ejemplo n.º 8
0
 /*-------------------- Constructors ---------------------------------*/
 internal PEWriter(PEFileVersionInfo verInfo, string fileName, MetaDataOut md, bool writePDB)
     : base(new FileStream(fileName, FileMode.Create))
 {
     Contract.Requires(verInfo != null);
     Contract.Requires(fileName != null);
     Contract.Requires(md != null);
     InitPEWriter(verInfo, md, writePDB, fileName);
     TimeSpan tmp = System.IO.File.GetCreationTime(fileName).Subtract(FileImage.origin);
     dateStamp = Convert.ToUInt32(tmp.TotalSeconds);
 }
Ejemplo n.º 9
0
        /*----------------------------- Writing -----------------------------------------*/
        private void InitPEWriter(PEFileVersionInfo verInfo, MetaDataOut md, bool writePDB, string fileName)
        {
            Contract.Requires(verInfo != null);
            Contract.Requires(md != null);
            Contract.Requires(fileName != null);
            this.verInfo = verInfo;
            if (!verInfo.fromExisting)
                verInfo.lMajor = MetaData.LMajors[(int)verInfo.netVersion];
            if (verInfo.isDLL)
            {
                hintNameTable = FileImage.dllHintNameTable.ToCharArray();
                if (!verInfo.fromExisting) verInfo.characteristics = FileImage.dllCharacteristics;
            }
            else
            {
                hintNameTable = FileImage.exeHintNameTable.ToCharArray();
                if (!verInfo.fromExisting) verInfo.characteristics = FileImage.exeCharacteristics;
            }
            text = new Section(FileImage.textName, 0x60000020);     // IMAGE_SCN_CNT  CODE, EXECUTE, READ
            metaData = md;
            metaData.InitMetaDataOut(this);

            // Check if we should include a PDB file
            if (writePDB)
            {

                // Work out the PDB filename from the PE files filename
                if ((fileName == null) || (fileName == "")) fileName = "default";

                // Setup the PDB Writer object
                pdbWriter = new PDBWriter(fileName);

                // Set the amount of space required for the debug information
                debugBytesSize += pdbWriter.PDBFilename.Length;

            }
        }
Ejemplo n.º 10
0
        internal void MakeFile(PEFileVersionInfo verInfo)
        {
            Contract.Requires(verInfo != null);
            this.verInfo = verInfo;
            if (this.verInfo.isDLL) hintNameTable = FileImage.dllHintNameTable.ToCharArray();
            else hintNameTable = FileImage.exeHintNameTable.ToCharArray();

            BuildTextSection();
            CalcOffsets();
            BuildRelocSection();
            // now write it out
            WriteHeader();
            WriteSections();
            Flush();
            if (closeStream) Close();
            if (pdbWriter != null)
            {

                // Write the PDB file
                pdbWriter.WritePDBFile();

                // Check to make sure the DebugInfo is the length we expected.
                if (pdbWriter.DebugInfo.Length != debugBytesSize)
                    throw new Exception("DebugInfo for the new PDB file is incompatible with the PE file.  This is most likely an internal error.  Please consult your vendor if you continue to have this problem.");

                // Write the debug info to the PE file
                using (FileStream fs = new FileStream(pdbWriter.PEFilename, FileMode.Open, FileAccess.ReadWrite))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        // Get to the DebugInfo section
                        bw.Seek((int)debugBytesStartOffset, SeekOrigin.Begin);
                        bw.Write(pdbWriter.DebugInfo, 0, pdbWriter.DebugInfo.Length);
                    }
                }

            }
        }