Ejemplo n.º 1
0
        private void BuildTextSection()
        {
            // .text layout
            //    IAT (single entry 8 bytes for pure CIL)
            //    CLIHeader (72 bytes)
            //    CIL instructions for all methods (variable size)
            //    Strong Name Signature
            //    MetaData
            //    ManagedResources
            //    ImportTable (40 bytes)
            //    ImportLookupTable(8 bytes) (same as IAT for standard CIL files)
            //    Hint/Name Tables with entry "_CorExeMain" for .exe file and "_CorDllMain" for .dll (14 bytes)
            //    ASCII string "mscoree.dll" referenced in ImportTable (+ padding = 16 bytes)
            //    Entry Point  (0xFF25 followed by 4 bytes 0x400000 + RVA of .text)
            codeStart = FileImage.IATSize + FileImage.CLIHeaderSize;
            if (Diag.DiagOn)
            {
                Console.WriteLine("Code starts at " + Hex.Int(codeStart));
            }
            metaData.BuildMetaData();
            // strongNameSig = metaData.GetStrongNameSig();
            metaDataOffset = FileImage.IATSize + FileImage.CLIHeaderSize + metaData.CodeSize();
            if (pdbWriter != null)
            {
                debugSize       = 0x1C; // or size of debugBytes??
                debugOffset     = metaDataOffset;
                metaDataOffset += (uint)debugBytesSize + debugSize + NumToAlign((uint)debugBytesSize, 4);
            }
            resourcesOffset         = metaDataOffset + metaData.Size();
            resourcesSize           = metaData.GetResourcesSize();
            importTableOffset       = resourcesOffset + resourcesSize;
            importTablePadding      = NumToAlign(importTableOffset, 16);
            importTableOffset      += importTablePadding;
            importLookupTableOffset = importTableOffset + FileImage.ImportTableSize;
            hintNameTableOffset     = importLookupTableOffset + FileImage.IATSize;
            runtimeEngineOffset     = hintNameTableOffset + (uint)hintNameTable.Length;
            entryPointOffset        = runtimeEngineOffset + (uint)runtimeEngine.Length;
            totalImportTableSize    = entryPointOffset - importTableOffset;
            if (Diag.DiagOn)
            {
                Console.WriteLine("total import table size = " + totalImportTableSize);
                Console.WriteLine("entrypoint offset = " + Hex.Int(entryPointOffset));
            }
            entryPointPadding = NumToAlign(entryPointOffset, 4) + 2;
            entryPointOffset += entryPointPadding;
            entryPointReloc   = entryPointOffset + 2;
            text.IncTide(entryPointOffset + 6);
            // The following lines may have some benefit for speed,
            // but can increase the PE file size by up to 10k in
            // some circumstances.  Can be commented out safely.
            if (text.Tide() > 8 * FileImage.maxFileAlign)
            {
                verInfo.fileAlign = FileImage.maxFileAlign;
            }
            else if (text.Tide() > 2 * FileImage.maxFileAlign)
            {
                verInfo.fileAlign = FileImage.midFileAlign;
            }

            text.SetSize(NumToAlign(text.Tide(), verInfo.fileAlign));
            if (Diag.DiagOn)
            {
                Console.WriteLine("text size = " + text.Size() + " text tide = " + text.Tide() + " text padding = " + text.Padding());
                Console.WriteLine("metaDataOffset = " + Hex.Int(metaDataOffset));
                Console.WriteLine("importTableOffset = " + Hex.Int(importTableOffset));
                Console.WriteLine("importLookupTableOffset = " + Hex.Int(importLookupTableOffset));
                Console.WriteLine("hintNameTableOffset = " + Hex.Int(hintNameTableOffset));
                Console.WriteLine("runtimeEngineOffset = " + Hex.Int(runtimeEngineOffset));
                Console.WriteLine("entryPointOffset = " + Hex.Int(entryPointOffset));
                Console.WriteLine("entryPointPadding = " + Hex.Int(entryPointPadding));
            }
        }