TextMap BuildTextMap()
        {
            var map = new TextMap();

            map.AddMap(TextSegment.ImportAddressTable, mr.inh.ifh.Machine == 0x014c ? 8 : 0);               // I386
            map.AddMap(TextSegment.CLIHeader, 0x48, 8);

            //map.AddMap (TextSegment.Code, metadata.code.length, !pe64 ? 4 : 16);
            map.AddMap(TextSegment.Code, methodcode.length, !pe64 ? 4 : 16);
            map.AddMap(TextSegment.Resources, this.resources.length, 8);
            map.AddMap(TextSegment.Data, this.Data.length, 4);
            //if (metadata.data.length > 0)
            //	metadata.table_heap.FixupData (map.GetRVA (TextSegment.Data));

            map.AddMap(TextSegment.StrongNameSignature, this.StrongName.length, 4);

            map.AddMap(TextSegment.MetadataHeader, GetMetadataHeaderLength());
            map.AddMap(TextSegment.TableHeap, this.Tables.length, 4);
            map.AddMap(TextSegment.StringHeap, this.Strings.length, 4);
            map.AddMap(TextSegment.UserStringHeap, this.US.length, 4);
            map.AddMap(TextSegment.GuidHeap, 16);
            map.AddMap(TextSegment.BlobHeap, this.Blob.length, 4);

            // debug_dir_len = 0
            map.AddMap(TextSegment.DebugDirectory, 0, 4);


            if (pe64)
            {
                var start = map.GetNextRVA(TextSegment.DebugDirectory);
                map.AddMap(TextSegment.ImportDirectory, new Range(start, 0));
                map.AddMap(TextSegment.ImportHintNameTable, new Range(start, 0));
                map.AddMap(TextSegment.StartupStub, new Range(start, 0));
                return(map);
            }

            RVA import_dir_rva = map.GetNextRVA(TextSegment.DebugDirectory);
            RVA import_hnt_rva = import_dir_rva + 48u;

            import_hnt_rva = (import_hnt_rva + 15u) & ~15u;
            uint import_dir_len = (import_hnt_rva - import_dir_rva) + 27u;

            RVA startup_stub_rva = import_dir_rva + import_dir_len;

            startup_stub_rva = mr.inh.ifh.Machine == 0x0200              // IA64
                                ? (startup_stub_rva + 15u) & ~15u
                                : 2 + ((startup_stub_rva + 3u) & ~3u);

            map.AddMap(TextSegment.ImportDirectory, new Range(import_dir_rva, import_dir_len));
            map.AddMap(TextSegment.ImportHintNameTable, new Range(import_hnt_rva, 0));
            map.AddMap(TextSegment.StartupStub, new Range(startup_stub_rva, GetStartupStubLength()));

            return(map);
        }
        ImageWriter(Metadata_ReaderWriter.MetadataReader mr, Stream stream)
            : base(stream)
        {
            this.mr = mr;

            this.methodcode = new DataBuffer();
            this.methodcode.WriteBytes(mr.methodcode.ToArray());

            this.resources = mr.resources;

            this.Data = new DataBuffer();
            this.Data.WriteBytes(mr.FieldsInitialData.ToArray());

            this.Tables = new DataBuffer();
            this.Tables.WriteBytes(mr.TablesBytes);

            this.Strings = new DataBuffer();
            this.Strings.WriteBytes(mr.Strings);

            this.Blob = new DataBuffer();
            this.Blob.WriteBytes(mr.Blob);

            this.US = new DataBuffer();
            if (mr.US != null)
            {
                this.US.WriteBytes(mr.US);
            }

            this.StrongName = new DataBuffer();
            if (mr.StrongName != null)
            {
                this.StrongName.WriteBytes(mr.StrongName);
            }

            //this.pe64 = module.Architecture != TargetArchitecture.I386;
            this.pe64 = mr.inh.ifh.Machine != 0x14C;
            //this.GetDebugHeader (); removed
            this.GetWin32Resources();
            this.text_map   = BuildTextMap();
            this.sections   = (ushort)(pe64 ? 1 : 2);            // text + reloc
            this.time_stamp = (uint)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
        }