Ejemplo n.º 1
0
        /*----------------------------- Writing -----------------------------------------*/
        private void InitPEWriter(PEFileVersionInfo verInfo, MetaDataOut md, bool writePDB, string fileName)
        {
            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
            //      rsrc = new Section(rsrcName,0x40000040);     // IMAGE_SCN_CNT  INITIALIZED_DATA, 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.º 2
0
        internal void MakeFile(PEFileVersionInfo verInfo)
        {
            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.º 3
0
 /*-------------------- Constructors ---------------------------------*/
 internal PEWriter(PEFileVersionInfo verInfo, string fileName, MetaDataOut md, bool writePDB)
     : base(new FileStream(fileName,FileMode.Create))
 {
     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)
 {
     // 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
 /// <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 file has an Assembly called
 /// "assemblyName".
 /// </summary>
 /// <param name="fileName">Name for the output file</param>
 /// <param name="assemblyName">Name of the assembly</param>
 public PEFile(string fileName, string assemblyName)
     : base(fileName)
 {
     //PrimitiveType.ClearAddedFlags();   // KJG 18-April-2005 - Now done in MetaDataOut
     this.fileName = fileName;
     thisAssembly = new Assembly(assemblyName, this);
     metaData = new MetaDataOut();
     versionInfo = new PEFileVersionInfo();
     versionInfo.SetDefaults(fileName);
 }