Пример #1
0
 public void Write(WriterContext ctxt, BlobWriter writer, bool isFat, Func<OpCode, object, Row> findRow)
 {
     if (isFat)
     {
         writer.WriteUInt32((uint)Flags);
         writer.WriteUInt32((uint)TryOffset);
         writer.WriteUInt32((uint)TryLength);
         writer.WriteUInt32((uint)HandlerOffset);
         writer.WriteUInt32((uint)HandlerLength);
     }
     else
     {
         writer.WriteUInt16((ushort)Flags);
         writer.WriteUInt16((ushort)TryOffset);
         writer.WriteByte((byte)TryLength);
         writer.WriteUInt16((ushort)HandlerOffset);
         writer.WriteByte((byte)HandlerLength);
     }
     var rowRef = default(TokenRef);
     rowRef.Value = Class == null ? null : findRow(OpCode.Ldobj, Class);
     rowRef.PersistIndexes(ctxt);
     rowRef.Write(ctxt, writer);
     if (Flags == CorILExceptionClause.Filter)
         writer.WriteUInt32((uint)FilterOffset);
 }
Пример #2
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteBytes(prefix);
     LfaNew.Write(writer);
     writer.WriteBytes(suffix);
     writer.WriteUInt32(Final);
 }
Пример #3
0
        public void PersistIndexes(WriterContext ctxt)
        {
            AssemblyTable.PersistIndexes(ctxt);
            AssemblyOSTable.PersistIndexes(ctxt);
            AssemblyProcessorTable.PersistIndexes(ctxt);
            AssemblyRefTable.PersistIndexes(ctxt);
            AssemblyRefOSTable.PersistIndexes(ctxt);
            AssemblyRefProcessorTable.PersistIndexes(ctxt);
            ClassLayoutTable.PersistIndexes(ctxt);
            ConstantTable.PersistIndexes(ctxt);
            CustomAttributeTable.PersistIndexes(ctxt);
            DeclSecurityTable.PersistIndexes(ctxt);
            EventMapTable.PersistIndexes(ctxt);
            EventTable.PersistIndexes(ctxt);
            ExportedTypeTable.PersistIndexes(ctxt);
            FieldTable.PersistIndexes(ctxt);
            FieldLayoutTable.PersistIndexes(ctxt);
            FieldMarshalTable.PersistIndexes(ctxt);
            FieldRVATable.PersistIndexes(ctxt);
            FileTable.PersistIndexes(ctxt);
            GenericParamTable.PersistIndexes(ctxt);
            GenericParamConstraintTable.PersistIndexes(ctxt);
            ImplMapTable.PersistIndexes(ctxt);
            InterfaceImplTable.PersistIndexes(ctxt);
            ManifestResourceTable.PersistIndexes(ctxt);
            MemberRefTable.PersistIndexes(ctxt);
            MethodDefTable.PersistIndexes(ctxt);
            MethodImplTable.PersistIndexes(ctxt);
            MethodSemanticsTable.PersistIndexes(ctxt);
            MethodSpecTable.PersistIndexes(ctxt);
            ModuleTable.PersistIndexes(ctxt);
            ModuleRefTable.PersistIndexes(ctxt);
            NestedClassTable.PersistIndexes(ctxt);
            ParamTable.PersistIndexes(ctxt);
            PropertyTable.PersistIndexes(ctxt);
            PropertyMapTable.PersistIndexes(ctxt);
            StandAloneSigTable.PersistIndexes(ctxt);
            TypeDefTable.PersistIndexes(ctxt);
            TypeRefTable.PersistIndexes(ctxt);
            TypeSpecTable.PersistIndexes(ctxt);
      
            throw new InvalidOperationException();
#if false
            IsStringStreamBig = ctxt.StringHeap.Size >= 0x10000;
            IsGuidStreamBig = ctxt.GuidHeap.Size >= 0x10000;
            IsBlobStreamBig = ctxt.BlobHeap.Size >= 0x10000;
#endif
            DetermineIndexCodingSizes();
        }
Пример #4
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     for (var i = 0; i < Fixups.Length; i++)
         Fixups[i].Write(ctxt, writer);
 }
Пример #5
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt32(cb);
     writer.WriteUInt16(MajorRuntimeVersion);
     writer.WriteUInt16(MinorRuntimeVersion);
     MetaData.Write(writer);
     writer.WriteUInt32((uint)Flags);
     writer.WriteUInt32(EntryPointToken);
     Resources.Write(writer);
     StrongNameSignature.Write(writer);
     writer.WriteUInt64(codeManagerTable);
     VtableFixups.Write(writer);
     writer.WriteUInt64(exportAddressTableJumps);
     writer.WriteUInt64(managedNativeHeader);
 }
Пример #6
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt32(Page);
     var blockSize = (uint)((Entries.Length * FixupEntry.Size) + 8);
     writer.WriteUInt32(blockSize);
     for (var i = 0; i < Entries.Length; i++)
         Entries[i].Write(ctxt, writer);
     writer.Align(4);
 }
Пример #7
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     var word = (ushort)(((byte)Type) << 12 | Offset);
     writer.WriteUInt16(word);
 }
Пример #8
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt16(hint);
     writer.WriteAsciiZeroTerminatedString(Name, 1);
 }
Пример #9
0
 public uint Write(WriterContext ctxt, BlobWriter writer)
 {
     var offset = writer.Offset;
     ImportLookupTable.Write(writer);
     writer.WriteUInt32(dateTimeStamp);
     writer.WriteUInt32(forwarderChain);
     Name.Write(writer);
     ImportAddressTable.Write(writer);
     writer.Pad(paddingBytes);
     return writer.Offset - offset;
 }
Пример #10
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt32(prefix);
     writer.WriteUInt16(machine);
     writer.WriteUInt16(NumberOfSections);
     var seconds = (uint)(DateTimeStamp.ToUniversalTime() - nineteenSeventy).TotalSeconds;
     writer.WriteUInt32(seconds);
     writer.WriteUInt32(pointerToSymbolTable);
     writer.WriteUInt32(numberOfSymbols);
     writer.WriteUInt16(PEOptionalHeader.Size);
     writer.WriteUInt16((ushort)Flags);
 }
Пример #11
0
 public void Alloc(WriterContext ctxt)
 {
     DateTimeStamp = DateTime.Now; // UTC???
 }
Пример #12
0
        private void Write(WriterContext ctxt)
        {
            // TextSection.PointerToRawData = Helpers.Align((uint)(376 + 40 * PEFileHeader.NumberOfSections), PEOptionalHeader.NTSpecificFields.SectionAlignment);
            TextSection.PointerToRawData = 0x200;
            PEOptionalHeader.StandardFields.EntryPointRVA = WriteEntryPoint();
            RelocationTable.Blocks.Add
                (new FixupBlock
                 {
                     PageRVA = TextSection.PointerToRawData,
                     Entries =
                         {
                             new FixupEntry
                             {
                                 Offset =
                                     (ushort)
                                     (PEOptionalHeader.StandardFields.EntryPointRVA - TextSection.PointerToRawData),
                                 Type = ImageRelocation.IMAGE_REL_BASED_HIGHLOW
                             }
                         }
                 });
            MetadataTables.PersistIndexes(this);

            MetadataHeader.PersistIndexes(this);
            CLIHeader.PersistIndexes(this);
            ImportAddressTable.PersistIndexes(this);
            ImportLookupTable.PersistIndexes(this);
            ImportTable.PersistIndexes(this);
            DosHeader.PersistIndexes(this);
            PEFileHeader.PersistIndexes(this);
            PEOptionalHeader.PersistIndexes(this);
            RelocationTable.Write(RelocationSection.Writer);
            foreach (var sectionHeader in SectionHeaders)
            {
                sectionHeader.PersistIndexes(this);
            }
            PEOptionalHeader.DataDirectories.BaseRelocationTable = RelocationSection.PointerToRawData;

            InitializePEFile();
            PersistIndexes();
            var writer = new BlobWriter();
            DosHeader.Write(writer);
            PEFileHeader.Write(writer);
            PEOptionalHeader.Write(writer);

            // TODO: precalc offsets in written section headers...
            // TODO: Write stream data
            foreach (var sectionHeader in SectionHeaders)
            {
                sectionHeader.Write(writer);
            }
            foreach (var sectionHeader in SectionHeaders)
            {
                writer.EnsureAtOffset(sectionHeader.PointerToRawData);
                sectionHeader.WriteData(this, writer);
            }
            return writer.GetBlob();
        }
Пример #13
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt32(signature);
     writer.WriteUInt16(MajorVersion);
     writer.WriteUInt16(MinorVersion);
     writer.WriteUInt32(reserved);
     writer.WriteUTF8SizedZeroPaddedString(Version, 4);
     writer.WriteUInt16(flags);
     writer.WriteUInt16((ushort)StreamHeaders.Length);
     for (var i = 0; i < StreamHeaders.Length; i++)
         StreamHeaders[i].Write(ctxt, writer);
 }
Пример #14
0
 public void Write(WriterContext ctxt,  BlobWriter writer)
 {
     Offset.Write(writer);
     writer.WriteUInt32(Size);
     writer.WriteAsciiZeroTerminatedString(Name, 4);
 }
Пример #15
0
 public void Write(WriterContext ctxt, BlobWriter writer, uint beginOffset, Func<OpCode, object, Row> findRow)
 {
     var offset = (int)(writer.Offset - beginOffset);
     if (offset != Offset)
         throw new PEException("invalid instruction offset");
     var highByte = (ushort)OpCode >> 8;
     if (highByte > 0)
         writer.WriteByte((byte)highByte);
     writer.WriteByte((byte)OpCode);
     switch (OpCode)
     {
     case OpCode.Nop:
     case OpCode.Break:
     case OpCode.Ldarg_0:
     case OpCode.Ldarg_1:
     case OpCode.Ldarg_2:
     case OpCode.Ldarg_3:
     case OpCode.Ldloc_0:
     case OpCode.Ldloc_1:
     case OpCode.Ldloc_2:
     case OpCode.Ldloc_3:
     case OpCode.Stloc_0:
     case OpCode.Stloc_1:
     case OpCode.Stloc_2:
     case OpCode.Stloc_3:
     case OpCode.Ldnull:
     case OpCode.Ldc_i4_m1:
     case OpCode.Ldc_i4_0:
     case OpCode.Ldc_i4_1:
     case OpCode.Ldc_i4_2:
     case OpCode.Ldc_i4_3:
     case OpCode.Ldc_i4_4:
     case OpCode.Ldc_i4_5:
     case OpCode.Ldc_i4_6:
     case OpCode.Ldc_i4_7:
     case OpCode.Ldc_i4_8:
     case OpCode.Dup:
     case OpCode.Pop:
     case OpCode.Ret:
     case OpCode.Ldind_i1:
     case OpCode.Ldind_u1:
     case OpCode.Ldind_i2:
     case OpCode.Ldind_u2:
     case OpCode.Ldind_i4:
     case OpCode.Ldind_u4:
     case OpCode.Ldind_i8:
     case OpCode.Ldind_i:
     case OpCode.Ldind_r4:
     case OpCode.Ldind_r8:
     case OpCode.Ldind_ref:
     case OpCode.Stind_ref:
     case OpCode.Stind_i1:
     case OpCode.Stind_i2:
     case OpCode.Stind_i4:
     case OpCode.Stind_i8:
     case OpCode.Stind_r4:
     case OpCode.Stind_r8:
     case OpCode.Add:
     case OpCode.Sub:
     case OpCode.Mul:
     case OpCode.Div:
     case OpCode.Div_un:
     case OpCode.Rem:
     case OpCode.Rem_un:
     case OpCode.And:
     case OpCode.Or:
     case OpCode.Xor:
     case OpCode.Shl:
     case OpCode.Shr:
     case OpCode.Shr_un:
     case OpCode.Neg:
     case OpCode.Not:
     case OpCode.Conv_i1:
     case OpCode.Conv_i2:
     case OpCode.Conv_i4:
     case OpCode.Conv_i8:
     case OpCode.Conv_r4:
     case OpCode.Conv_r8:
     case OpCode.Conv_u4:
     case OpCode.Conv_u8:
     case OpCode.Conv_r_un:
     case OpCode.Throw:
     case OpCode.Conv_ovf_i1_un:
     case OpCode.Conv_ovf_i2_un:
     case OpCode.Conv_ovf_i4_un:
     case OpCode.Conv_ovf_i8_un:
     case OpCode.Conv_ovf_u1_un:
     case OpCode.Conv_ovf_u2_un:
     case OpCode.Conv_ovf_u4_un:
     case OpCode.Conv_ovf_u8_un:
     case OpCode.Conv_ovf_i_un:
     case OpCode.Conv_ovf_u_un:
     case OpCode.Ldlen:
     case OpCode.Ldelem_i1:
     case OpCode.Ldelem_u1:
     case OpCode.Ldelem_i2:
     case OpCode.Ldelem_u2:
     case OpCode.Ldelem_i4:
     case OpCode.Ldelem_u4:
     case OpCode.Ldelem_i8:
     case OpCode.Ldelem_i:
     case OpCode.Ldelem_r4:
     case OpCode.Ldelem_r8:
     case OpCode.Ldelem_ref:
     case OpCode.Stelem_i:
     case OpCode.Stelem_i1:
     case OpCode.Stelem_i2:
     case OpCode.Stelem_i4:
     case OpCode.Stelem_i8:
     case OpCode.Stelem_r4:
     case OpCode.Stelem_r8:
     case OpCode.Stelem_ref:
     case OpCode.Conv_ovf_i1:
     case OpCode.Conv_ovf_u1:
     case OpCode.Conv_ovf_i2:
     case OpCode.Conv_ovf_u2:
     case OpCode.Conv_ovf_i4:
     case OpCode.Conv_ovf_u4:
     case OpCode.Conv_ovf_i8:
     case OpCode.Conv_ovf_u8:
     case OpCode.Ckfinite:
     case OpCode.Conv_u2:
     case OpCode.Conv_u1:
     case OpCode.Conv_i:
     case OpCode.Conv_ovf_i:
     case OpCode.Conv_ovf_u:
     case OpCode.Add_ovf:
     case OpCode.Add_ovf_un:
     case OpCode.Mul_ovf:
     case OpCode.Mul_ovf_un:
     case OpCode.Sub_ovf:
     case OpCode.Sub_ovf_un:
     case OpCode.Endfinally:
     case OpCode.Stind_i:
     case OpCode.Conv_u:
     case OpCode.Prefix7:
     case OpCode.Prefix6:
     case OpCode.Prefix5:
     case OpCode.Prefix4:
     case OpCode.Prefix3:
     case OpCode.Prefix2:
     case OpCode.Prefix1:
     case OpCode.Prefixref:
     case OpCode.Arglist:
     case OpCode.Ceq:
     case OpCode.Cgt:
     case OpCode.Cgt_un:
     case OpCode.Clt:
     case OpCode.Clt_un:
     case OpCode.Localloc:
     case OpCode.Endfilter:
     case OpCode.Volatile:
     case OpCode.Tailcall:
     case OpCode.Cpblk:
     case OpCode.Initblk:
     case OpCode.Rethrow:
     case OpCode.Refanytype:
     case OpCode.Readonly:
         break;
     case OpCode.Br:
     case OpCode.Brfalse:
     case OpCode.Brtrue:
     case OpCode.Beq:
     case OpCode.Bge:
     case OpCode.Bgt:
     case OpCode.Ble:
     case OpCode.Blt:
     case OpCode.Bne_un:
     case OpCode.Bge_un:
     case OpCode.Bgt_un:
     case OpCode.Ble_un:
     case OpCode.Blt_un:
     case OpCode.Leave:
         {
             var target = (int)Value;
             // NOTE: Delta is relatative to start of next instruction
             var delta = (int)beginOffset + target - ((int)writer.Offset + 4);
             writer.WriteInt32(delta);
         }
         break;
     case OpCode.Br_s:
     case OpCode.Brfalse_s:
     case OpCode.Brtrue_s:
     case OpCode.Beq_s:
     case OpCode.Bge_s:
     case OpCode.Bgt_s:
     case OpCode.Ble_s:
     case OpCode.Blt_s:
     case OpCode.Bne_un_s:
     case OpCode.Bge_un_s:
     case OpCode.Bgt_un_s:
     case OpCode.Ble_un_s:
     case OpCode.Blt_un_s:
     case OpCode.Leave_s:
         {
             var target = (int)Value;
             // NOTE: Delta is w.r.t. begining of next instruction
             var delta = (int)beginOffset + target - ((int)writer.Offset + 1);
             if (delta > 0xff)
                 throw new PEException("cannot use small form for this instruction");
             writer.WriteSByte((sbyte)delta);
         }
         break;
     case OpCode.Ldc_i4_s:
         writer.WriteSByte((sbyte)(int)Value);
         break;
     case OpCode.Ldarg_s:
     case OpCode.Ldarga_s:
     case OpCode.Starg_s:
     case OpCode.Ldloc_s:
     case OpCode.Ldloca_s:
     case OpCode.Stloc_s:
     case OpCode.Unaligned:
         writer.WriteByte((byte)(int)Value);
         break;
     case OpCode.Ldc_i4:
         writer.WriteInt32((int)Value);
         break;
     case OpCode.Ldarg:
     case OpCode.Ldarga:
     case OpCode.Starg:
     case OpCode.Ldloc:
     case OpCode.Ldloca:
     case OpCode.Stloc:
         writer.WriteUInt32((uint)(int)Value);
         break;
     case OpCode.Ldc_i8:
         writer.WriteInt64((long)Value);
         break;
     case OpCode.Ldc_r4:
         writer.WriteSingle((float)Value);
         break;
     case OpCode.Ldc_r8:
         writer.WriteDouble((double)Value);
         break;
     case OpCode.Ldstr:
         WriteUserString(ctxt, writer, (string)Value);
         break;
     case OpCode.Switch:
         {
             var targets = (Seq<int>)Value;
             writer.WriteUInt32((uint)targets.Count);
             // NOTE: Deltas are w.r.t. start of next instruction
             for (var i = 0; i < targets.Count; i++)
             {
                 var delta = (int)beginOffset + targets[i] - ((int)writer.Offset + (targets.Count * 4));
                 writer.WriteInt32(delta);
             }
         }
         break;
     case OpCode.Calli:
     case OpCode.Jmp:
     case OpCode.Call:
     case OpCode.Callvirt:
     case OpCode.Newobj:
     case OpCode.Ldftn:
     case OpCode.Ldvirtftn:
     case OpCode.Ldfld:
     case OpCode.Ldflda:
     case OpCode.Stfld:
     case OpCode.Ldsfld:
     case OpCode.Ldsflda:
     case OpCode.Stsfld:
     case OpCode.Ldtoken:
     case OpCode.Cpobj:
     case OpCode.Ldobj:
     case OpCode.Castclass:
     case OpCode.Isinst:
     case OpCode.Unbox:
     case OpCode.Stobj:
     case OpCode.Box:
     case OpCode.Newarr:
     case OpCode.Ldelema:
     case OpCode.Ldelem:
     case OpCode.Stelem:
     case OpCode.Unbox_any:
     case OpCode.Refanyval:
     case OpCode.Mkrefany:
     case OpCode.Initobj:
     case OpCode.Constrained:
     case OpCode.Sizeof:
         WriteToken(ctxt, writer, findRow(OpCode, Value));
         break;
     default:
         throw new PEException("unrecognised opcode");
     }
 }
Пример #16
0
 private static void WriteUserString(WriterContext ctxt, BlobWriter writer, string str)
 {
     var strref = default(UserStringRef);
     strref.Value = str;
     strref.PersistIndexes(ctxt);
     strref.Write(ctxt, writer);
 }
Пример #17
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     var name = default(string);
     switch (Section)
     {
     case Section.Text:
         name = ".text"; break;
     case Section.Rsrc:
         name = ".rsrc"; break;
     case Section.Reloc:
         name = ".reloc"; break;
     default:
         throw new ArgumentOutOfRangeException();
     }
     writer.WriteAsciiZeroPaddedString(name, 8);
     writer.WriteUInt32(VirtualSize);
     writer.WriteUInt32(VirtualAddress);
     writer.WriteUInt32(SizeOfRawData);
     PointerToRawData.Write(writer);
     writer.WriteUInt32(PointerToRelocations);
     writer.WriteUInt32(pointerToLinenumbers);
     writer.WriteUInt16(NumberOfRelocations);
     writer.WriteUInt16(numberOfLinenumbers);
     writer.WriteUInt32((uint)Characteristics);
 }
Пример #18
0
 public void Alloc(WriterContext ctxt)
 {
     EntryPoint.Alloc(ctxt, Section.Text).WriteBytes(EntryPoint.Value);
 }
Пример #19
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     HintNameTable.Write(writer);
     writer.WriteUInt32(padding);
 }
Пример #20
0
 public void Fixup(WriterContext ctxt)
 {
     CodeSize = ctxt.CodeSize();
     InitializedDataSize = ctxt.InitializedDataSize();
     EntryPoint.Fixup(ctxt, Section.Text);
     BaseOfCode.Address = ctxt.BaseOfCode();
     BaseOfData.Address = ctxt.BaseOfData();
 }
Пример #21
0
 public void Alloc(WriterContext ctxt)
 {
     LfaNew.Offset = Size;
 }
Пример #22
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt16(magic);
     writer.WriteByte(LMajor);
     writer.WriteByte(lMinor);
     writer.WriteUInt32(CodeSize);
     writer.WriteUInt32(InitializedDataSize);
     writer.WriteUInt32(uninitializedDataSize);
     EntryPoint.Write(writer);
     BaseOfCode.Write(writer);
     BaseOfData.Write(writer);
 }
Пример #23
0
 public void Fixup(WriterContext ctxt)
 {
     ImageSize = Constants.RoundUp(ctxt.VirtualLimit(), sectionAlignment);
     HeaderSize = Constants.RoundUp(DOSHeader.Size + PEFileHeader.Size + PEOptionalHeader.Size, FileAlignment);
     FileChecksum = 0; // ????
 }
Пример #24
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt32(ImageBase);
     writer.WriteUInt32(sectionAlignment);
     writer.WriteUInt32(FileAlignment);
     writer.WriteUInt16(osMajor);
     writer.WriteUInt16(osMinor);
     writer.WriteUInt16(userMajor);
     writer.WriteUInt16(userMinor);
     writer.WriteUInt16(subSysMajor);
     writer.WriteUInt16(subSysMinor);
     writer.WriteUInt32(reserved);
     writer.WriteUInt32(ImageSize);
     writer.WriteUInt32(HeaderSize);
     writer.WriteUInt32(FileChecksum);
     writer.WriteUInt16((ushort)SubSystem);
     writer.WriteUInt16(DllFlags);
     writer.WriteUInt32(stackReserveSize);
     writer.WriteUInt32(StackCommitSize);
     writer.WriteUInt32(HeapReserveSize);
     writer.WriteUInt32(HeapCommitSize);
     writer.WriteUInt32(loaderFlags);
     writer.WriteUInt32(numberOfDataDirectories);
 }
Пример #25
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     for (var i = 0; i < Blocks.Length; i++)
         Blocks[i].Write(ctxt, writer);
 }
Пример #26
0
 public void Alloc(WriterContext ctxt)
 {
     ImportTable.Size = ImportTable.Value.Write(ctxt, ImportTable.Alloc(ctxt, Section.Text));
     if (ResourceTable.Value != null)
     {
         ResourceTable.Alloc(ctxt, Section.Rsrc).WriteBytes(ResourceTable.Value);
         ResourceTable.Size = (uint)ResourceTable.Value.Length;
     }
     // ##########
 }
Пример #27
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt32(VirtualAddress);
     writer.WriteUInt16(Size);
     writer.WriteUInt16((ushort)Type);
 }
Пример #28
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     writer.WriteUInt64(exportTable);
     ImportTable.Write(writer);
     ResourceTable.Write(writer);
     writer.WriteUInt64(exceptionTable);
     CertificateTable.Write(writer);
     BaseRelocationTable.Write(writer);
     Debug.Write(writer);
     writer.WriteUInt64(copyright);
     writer.WriteUInt64(globalPtr);
     writer.WriteUInt64(tlsTable);
     writer.WriteUInt64(loadConfigTable);
     writer.WriteUInt64(boundImport);
     IAT.Write(writer);
     writer.WriteUInt64(delayImportDescriptor);
     CLIHeader.Write(writer);
     writer.WriteUInt64(reserved);
 }
Пример #29
0
        public void Write(WriterContext ctxt)
        {
            var writer = ctxt.GetTablesWriter();

            writer.WriteUInt32(reserved0);
            writer.WriteByte(majorVersion);
            writer.WriteByte(minorVersion);
            var heapSizes = default(byte);

            if (IsStringStreamBig)
            {
                heapSizes |= 0x01;
            }
            if (IsGuidStreamBig)
            {
                heapSizes |= 0x02;
            }
            if (IsBlobStreamBig)
            {
                heapSizes |= 0x04;
            }
            writer.WriteByte(heapSizes);
            writer.WriteByte(Reserved1);

            var valid = new IntSet64();

            valid[(int)TableTag.Assembly]             = AssemblyTable.Count > 0;
            valid[(int)TableTag.AssemblyOS]           = AssemblyOSTable.Count > 0;
            valid[(int)TableTag.AssemblyProcessor]    = AssemblyProcessorTable.Count > 0;
            valid[(int)TableTag.AssemblyRef]          = AssemblyRefTable.Count > 0;
            valid[(int)TableTag.AssemblyRefOS]        = AssemblyRefOSTable.Count > 0;
            valid[(int)TableTag.AssemblyRefProcessor] = AssemblyRefProcessorTable.Count > 0;
            valid[(int)TableTag.ClassLayout]          = ClassLayoutTable.Count > 0;
            valid[(int)TableTag.Constant]             = ConstantTable.Count > 0;
            valid[(int)TableTag.CustomAttribute]      = CustomAttributeTable.Count > 0;
            valid[(int)TableTag.DeclSecurity]         = DeclSecurityTable.Count > 0;
            valid[(int)TableTag.EventMap]             = EventMapTable.Count > 0;
            valid[(int)TableTag.Event]                  = EventTable.Count > 0;
            valid[(int)TableTag.ExportedType]           = ExportedTypeTable.Count > 0;
            valid[(int)TableTag.Field]                  = FieldTable.Count > 0;
            valid[(int)TableTag.FieldLayout]            = FieldLayoutTable.Count > 0;
            valid[(int)TableTag.FieldMarshal]           = FieldMarshalTable.Count > 0;
            valid[(int)TableTag.FieldRVA]               = FieldRVATable.Count > 0;
            valid[(int)TableTag.File]                   = FileTable.Count > 0;
            valid[(int)TableTag.GenericParam]           = GenericParamTable.Count > 0;
            valid[(int)TableTag.GenericParamConstraint] = GenericParamConstraintTable.Count > 0;
            valid[(int)TableTag.ImplMap]                = ImplMapTable.Count > 0;
            valid[(int)TableTag.InterfaceImpl]          = InterfaceImplTable.Count > 0;
            valid[(int)TableTag.ManifestResource]       = ManifestResourceTable.Count > 0;
            valid[(int)TableTag.MemberRef]              = MemberRefTable.Count > 0;
            valid[(int)TableTag.MethodDef]              = MethodDefTable.Count > 0;
            valid[(int)TableTag.MethodImpl]             = MethodImplTable.Count > 0;
            valid[(int)TableTag.MethodSemantics]        = MethodSemanticsTable.Count > 0;
            valid[(int)TableTag.MethodSpec]             = MethodSpecTable.Count > 0;
            valid[(int)TableTag.Module]                 = ModuleTable.Count > 0;
            valid[(int)TableTag.ModuleRef]              = ModuleRefTable.Count > 0;
            valid[(int)TableTag.NestedClass]            = NestedClassTable.Count > 0;
            valid[(int)TableTag.Param]                  = ParamTable.Count > 0;
            valid[(int)TableTag.Property]               = PropertyTable.Count > 0;
            valid[(int)TableTag.PropertyMap]            = PropertyMapTable.Count > 0;
            valid[(int)TableTag.StandAloneSig]          = StandAloneSigTable.Count > 0;
            valid[(int)TableTag.TypeDef]                = TypeDefTable.Count > 0;
            valid[(int)TableTag.TypeRef]                = TypeRefTable.Count > 0;
            valid[(int)TableTag.TypeSpec]               = TypeSpecTable.Count > 0;
            writer.WriteUInt64(valid.ToUInt64());
            writer.WriteUInt64(valid.ToUInt64());

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    var numRows = default(int);
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        numRows = ModuleTable.NumRows;
                        break;

                    case TableTag.Assembly:
                        numRows = AssemblyTable.NumRows;
                        break;

                    case TableTag.AssemblyOS:
                        numRows = AssemblyOSTable.NumRows;
                        break;

                    case TableTag.AssemblyProcessor:
                        numRows = AssemblyProcessorTable.NumRows;
                        break;

                    case TableTag.AssemblyRef:
                        numRows = AssemblyRefTable.NumRows;
                        break;

                    case TableTag.AssemblyRefOS:
                        numRows = AssemblyRefOSTable.NumRows;
                        break;

                    case TableTag.AssemblyRefProcessor:
                        numRows = AssemblyRefProcessorTable.NumRows;
                        break;

                    case TableTag.ClassLayout:
                        numRows = ClassLayoutTable.NumRows;
                        break;

                    case TableTag.Constant:
                        numRows = ConstantTable.NumRows;
                        break;

                    case TableTag.CustomAttribute:
                        numRows = CustomAttributeTable.NumRows;
                        break;

                    case TableTag.DeclSecurity:
                        numRows = DeclSecurityTable.NumRows;
                        break;

                    case TableTag.EventMap:
                        numRows = EventMapTable.NumRows;
                        break;

                    case TableTag.Event:
                        numRows = EventTable.NumRows;
                        break;

                    case TableTag.ExportedType:
                        numRows = ExportedTypeTable.NumRows;
                        break;

                    case TableTag.Field:
                        numRows = FieldTable.NumRows;
                        break;

                    case TableTag.FieldLayout:
                        numRows = FieldLayoutTable.NumRows;
                        break;

                    case TableTag.FieldMarshal:
                        numRows = FieldMarshalTable.NumRows;
                        break;

                    case TableTag.FieldRVA:
                        numRows = FieldRVATable.NumRows;
                        break;

                    case TableTag.File:
                        numRows = FileTable.NumRows;
                        break;

                    case TableTag.GenericParam:
                        numRows = GenericParamTable.NumRows;
                        break;

                    case TableTag.GenericParamConstraint:
                        numRows = GenericParamConstraintTable.NumRows;
                        break;

                    case TableTag.ImplMap:
                        numRows = ImplMapTable.NumRows;
                        break;

                    case TableTag.InterfaceImpl:
                        numRows = InterfaceImplTable.NumRows;
                        break;

                    case TableTag.ManifestResource:
                        numRows = ManifestResourceTable.NumRows;
                        break;

                    case TableTag.MemberRef:
                        numRows = MemberRefTable.NumRows;
                        break;

                    case TableTag.MethodDef:
                        numRows = MethodDefTable.NumRows;
                        break;

                    case TableTag.MethodImpl:
                        numRows = MethodImplTable.NumRows;
                        break;

                    case TableTag.MethodSemantics:
                        numRows = MethodSemanticsTable.NumRows;
                        break;

                    case TableTag.MethodSpec:
                        numRows = MethodSpecTable.NumRows;
                        break;

                    case TableTag.ModuleRef:
                        numRows = ModuleRefTable.NumRows;
                        break;

                    case TableTag.NestedClass:
                        numRows = NestedClassTable.NumRows;
                        break;

                    case TableTag.Param:
                        numRows = ParamTable.NumRows;
                        break;

                    case TableTag.Property:
                        numRows = PropertyTable.NumRows;
                        break;

                    case TableTag.PropertyMap:
                        numRows = PropertyMapTable.NumRows;
                        break;

                    case TableTag.StandAloneSig:
                        numRows = StandAloneSigTable.NumRows;
                        break;

                    case TableTag.TypeDef:
                        numRows = TypeDefTable.NumRows;
                        break;

                    case TableTag.TypeRef:
                        numRows = TypeRefTable.NumRows;
                        break;

                    case TableTag.TypeSpec:
                        numRows = TypeSpecTable.NumRows;
                        break;

                    default:
                        throw new PEException("invalid TableTag");
                    }
                    writer.WriteUInt32((uint)numRows);
                }
            }

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        ModuleTable.Write(ctxt, writer);
                        break;

                    case TableTag.Assembly:
                        AssemblyTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyOS:
                        AssemblyOSTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyProcessor:
                        AssemblyProcessorTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyRef:
                        AssemblyRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyRefOS:
                        AssemblyRefOSTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyRefProcessor:
                        AssemblyRefProcessorTable.Write(ctxt, writer);
                        break;

                    case TableTag.ClassLayout:
                        ClassLayoutTable.Write(ctxt, writer);
                        break;

                    case TableTag.Constant:
                        ConstantTable.Write(ctxt, writer);
                        break;

                    case TableTag.CustomAttribute:
                        CustomAttributeTable.Write(ctxt, writer);
                        break;

                    case TableTag.DeclSecurity:
                        DeclSecurityTable.Write(ctxt, writer);
                        break;

                    case TableTag.EventMap:
                        EventMapTable.Write(ctxt, writer);
                        break;

                    case TableTag.Event:
                        EventTable.Write(ctxt, writer);
                        break;

                    case TableTag.ExportedType:
                        ExportedTypeTable.Write(ctxt, writer);
                        break;

                    case TableTag.Field:
                        FieldTable.Write(ctxt, writer);
                        break;

                    case TableTag.FieldLayout:
                        FieldLayoutTable.Write(ctxt, writer);
                        break;

                    case TableTag.FieldMarshal:
                        FieldMarshalTable.Write(ctxt, writer);
                        break;

                    case TableTag.FieldRVA:
                        FieldRVATable.Write(ctxt, writer);
                        break;

                    case TableTag.File:
                        FileTable.Write(ctxt, writer);
                        break;

                    case TableTag.GenericParam:
                        GenericParamTable.Write(ctxt, writer);
                        break;

                    case TableTag.GenericParamConstraint:
                        GenericParamConstraintTable.Write(ctxt, writer);
                        break;

                    case TableTag.ImplMap:
                        ImplMapTable.Write(ctxt, writer);
                        break;

                    case TableTag.InterfaceImpl:
                        InterfaceImplTable.Write(ctxt, writer);
                        break;

                    case TableTag.ManifestResource:
                        ManifestResourceTable.Write(ctxt, writer);
                        break;

                    case TableTag.MemberRef:
                        MemberRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodDef:
                        MethodDefTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodImpl:
                        MethodImplTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodSemantics:
                        MethodSemanticsTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodSpec:
                        MethodSpecTable.Write(ctxt, writer);
                        break;

                    case TableTag.ModuleRef:
                        ModuleRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.NestedClass:
                        NestedClassTable.Write(ctxt, writer);
                        break;

                    case TableTag.Param:
                        ParamTable.Write(ctxt, writer);
                        break;

                    case TableTag.Property:
                        PropertyTable.Write(ctxt, writer);
                        break;

                    case TableTag.PropertyMap:
                        PropertyMapTable.Write(ctxt, writer);
                        break;

                    case TableTag.StandAloneSig:
                        StandAloneSigTable.Write(ctxt, writer);
                        break;

                    case TableTag.TypeDef:
                        TypeDefTable.Write(ctxt, writer);
                        break;

                    case TableTag.TypeRef:
                        TypeRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.TypeSpec:
                        TypeSpecTable.Write(ctxt, writer);
                        break;

                    default:
                        throw new PEException("invalid TableTag");
                    }
                }
            }
        }
Пример #30
0
 public void Write(WriterContext ctxt, BlobWriter writer)
 {
     StandardFields.Write(ctxt, writer);
     NTSpecificFields.Write(ctxt, writer);
     DataDirectories.Write(ctxt, writer);
 }
Пример #31
0
        public void Write(WriterContext ctxt)
        {
            var writer = ctxt.GetTablesWriter();

            writer.WriteUInt32(reserved0);
            writer.WriteByte(majorVersion);
            writer.WriteByte(minorVersion);
            var heapSizes = default(byte);
            if (IsStringStreamBig)
                heapSizes |= 0x01;
            if (IsGuidStreamBig)
                heapSizes |= 0x02;
            if (IsBlobStreamBig)
                heapSizes |= 0x04;
            writer.WriteByte(heapSizes);
            writer.WriteByte(Reserved1);

            var valid = new IntSet64();
            valid[(int)TableTag.Assembly] = AssemblyTable.Count > 0;
            valid[(int)TableTag.AssemblyOS] = AssemblyOSTable.Count > 0;
            valid[(int)TableTag.AssemblyProcessor] = AssemblyProcessorTable.Count > 0;
            valid[(int)TableTag.AssemblyRef] = AssemblyRefTable.Count > 0;
            valid[(int)TableTag.AssemblyRefOS] = AssemblyRefOSTable.Count > 0;
            valid[(int)TableTag.AssemblyRefProcessor] = AssemblyRefProcessorTable.Count > 0;
            valid[(int)TableTag.ClassLayout] = ClassLayoutTable.Count > 0;
            valid[(int)TableTag.Constant] = ConstantTable.Count > 0;
            valid[(int)TableTag.CustomAttribute] = CustomAttributeTable.Count > 0;
            valid[(int)TableTag.DeclSecurity] = DeclSecurityTable.Count > 0;
            valid[(int)TableTag.EventMap] = EventMapTable.Count > 0;
            valid[(int)TableTag.Event] = EventTable.Count > 0;
            valid[(int)TableTag.ExportedType] = ExportedTypeTable.Count > 0;
            valid[(int)TableTag.Field] = FieldTable.Count > 0;
            valid[(int)TableTag.FieldLayout] = FieldLayoutTable.Count > 0;
            valid[(int)TableTag.FieldMarshal] = FieldMarshalTable.Count > 0;
            valid[(int)TableTag.FieldRVA] = FieldRVATable.Count > 0;
            valid[(int)TableTag.File] = FileTable.Count > 0;
            valid[(int)TableTag.GenericParam] = GenericParamTable.Count > 0;
            valid[(int)TableTag.GenericParamConstraint] = GenericParamConstraintTable.Count > 0;
            valid[(int)TableTag.ImplMap] = ImplMapTable.Count > 0;
            valid[(int)TableTag.InterfaceImpl] = InterfaceImplTable.Count > 0;
            valid[(int)TableTag.ManifestResource] = ManifestResourceTable.Count > 0;
            valid[(int)TableTag.MemberRef] = MemberRefTable.Count > 0;
            valid[(int)TableTag.MethodDef] = MethodDefTable.Count > 0;
            valid[(int)TableTag.MethodImpl] = MethodImplTable.Count > 0;
            valid[(int)TableTag.MethodSemantics] = MethodSemanticsTable.Count > 0;
            valid[(int)TableTag.MethodSpec] = MethodSpecTable.Count > 0;
            valid[(int)TableTag.Module] = ModuleTable.Count > 0;
            valid[(int)TableTag.ModuleRef] = ModuleRefTable.Count > 0;
            valid[(int)TableTag.NestedClass] = NestedClassTable.Count > 0;
            valid[(int)TableTag.Param] = ParamTable.Count > 0;
            valid[(int)TableTag.Property] = PropertyTable.Count > 0;
            valid[(int)TableTag.PropertyMap] = PropertyMapTable.Count > 0;
            valid[(int)TableTag.StandAloneSig] = StandAloneSigTable.Count > 0;
            valid[(int)TableTag.TypeDef] = TypeDefTable.Count > 0;
            valid[(int)TableTag.TypeRef] = TypeRefTable.Count > 0;
            valid[(int)TableTag.TypeSpec] = TypeSpecTable.Count > 0;
            writer.WriteUInt64(valid.ToUInt64());
            writer.WriteUInt64(valid.ToUInt64());

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    var numRows = default(int);
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        numRows = ModuleTable.NumRows;
                        break;
                    case TableTag.Assembly:
                        numRows = AssemblyTable.NumRows;
                        break;
                    case TableTag.AssemblyOS:
                        numRows = AssemblyOSTable.NumRows;
                        break;
                    case TableTag.AssemblyProcessor:
                        numRows = AssemblyProcessorTable.NumRows;
                        break;
                    case TableTag.AssemblyRef:
                        numRows = AssemblyRefTable.NumRows;
                        break;
                    case TableTag.AssemblyRefOS:
                        numRows = AssemblyRefOSTable.NumRows;
                        break;
                    case TableTag.AssemblyRefProcessor:
                        numRows = AssemblyRefProcessorTable.NumRows;
                        break;
                    case TableTag.ClassLayout:
                        numRows = ClassLayoutTable.NumRows;
                        break;
                    case TableTag.Constant:
                        numRows = ConstantTable.NumRows;
                        break;
                    case TableTag.CustomAttribute:
                        numRows = CustomAttributeTable.NumRows;
                        break;
                    case TableTag.DeclSecurity:
                        numRows = DeclSecurityTable.NumRows;
                        break;
                    case TableTag.EventMap:
                        numRows = EventMapTable.NumRows;
                        break;
                    case TableTag.Event:
                        numRows = EventTable.NumRows;
                        break;
                    case TableTag.ExportedType:
                        numRows = ExportedTypeTable.NumRows;
                        break;
                    case TableTag.Field:
                        numRows = FieldTable.NumRows;
                        break;
                    case TableTag.FieldLayout:
                        numRows = FieldLayoutTable.NumRows;
                        break;
                    case TableTag.FieldMarshal:
                        numRows = FieldMarshalTable.NumRows;
                        break;
                    case TableTag.FieldRVA:
                        numRows = FieldRVATable.NumRows;
                        break;
                    case TableTag.File:
                        numRows = FileTable.NumRows;
                        break;
                    case TableTag.GenericParam:
                        numRows = GenericParamTable.NumRows;
                        break;
                    case TableTag.GenericParamConstraint:
                        numRows = GenericParamConstraintTable.NumRows;
                        break;
                    case TableTag.ImplMap:
                        numRows = ImplMapTable.NumRows;
                        break;
                    case TableTag.InterfaceImpl:
                        numRows = InterfaceImplTable.NumRows;
                        break;
                    case TableTag.ManifestResource:
                        numRows = ManifestResourceTable.NumRows;
                        break;
                    case TableTag.MemberRef:
                        numRows = MemberRefTable.NumRows;
                        break;
                    case TableTag.MethodDef:
                        numRows = MethodDefTable.NumRows;
                        break;
                    case TableTag.MethodImpl:
                        numRows = MethodImplTable.NumRows;
                        break;
                    case TableTag.MethodSemantics:
                        numRows = MethodSemanticsTable.NumRows;
                        break;
                    case TableTag.MethodSpec:
                        numRows = MethodSpecTable.NumRows;
                        break;
                    case TableTag.ModuleRef:
                        numRows = ModuleRefTable.NumRows;
                        break;
                    case TableTag.NestedClass:
                        numRows = NestedClassTable.NumRows;
                        break;
                    case TableTag.Param:
                        numRows = ParamTable.NumRows;
                        break;
                    case TableTag.Property:
                        numRows = PropertyTable.NumRows;
                        break;
                    case TableTag.PropertyMap:
                        numRows = PropertyMapTable.NumRows;
                        break;
                    case TableTag.StandAloneSig:
                        numRows = StandAloneSigTable.NumRows;
                        break;
                    case TableTag.TypeDef:
                        numRows = TypeDefTable.NumRows;
                        break;
                    case TableTag.TypeRef:
                        numRows = TypeRefTable.NumRows;
                        break;
                    case TableTag.TypeSpec:
                        numRows = TypeSpecTable.NumRows;
                        break;
                    default:
                        throw new PEException("invalid TableTag");
                    }
                    writer.WriteUInt32((uint)numRows);
                }
            }

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        ModuleTable.Write(ctxt, writer);
                        break;
                    case TableTag.Assembly:
                        AssemblyTable.Write(ctxt, writer);
                        break;
                    case TableTag.AssemblyOS:
                        AssemblyOSTable.Write(ctxt, writer);
                        break;
                    case TableTag.AssemblyProcessor:
                        AssemblyProcessorTable.Write(ctxt, writer);
                        break;
                    case TableTag.AssemblyRef:
                        AssemblyRefTable.Write(ctxt, writer);
                        break;
                    case TableTag.AssemblyRefOS:
                        AssemblyRefOSTable.Write(ctxt, writer);
                        break;
                    case TableTag.AssemblyRefProcessor:
                        AssemblyRefProcessorTable.Write(ctxt, writer);
                        break;
                    case TableTag.ClassLayout:
                        ClassLayoutTable.Write(ctxt, writer);
                        break;
                    case TableTag.Constant:
                        ConstantTable.Write(ctxt, writer);
                        break;
                    case TableTag.CustomAttribute:
                        CustomAttributeTable.Write(ctxt, writer);
                        break;
                    case TableTag.DeclSecurity:
                        DeclSecurityTable.Write(ctxt, writer);
                        break;
                    case TableTag.EventMap:
                        EventMapTable.Write(ctxt, writer);
                        break;
                    case TableTag.Event:
                        EventTable.Write(ctxt, writer);
                        break;
                    case TableTag.ExportedType:
                        ExportedTypeTable.Write(ctxt, writer);
                        break;
                    case TableTag.Field:
                        FieldTable.Write(ctxt, writer);
                        break;
                    case TableTag.FieldLayout:
                        FieldLayoutTable.Write(ctxt, writer);
                        break;
                    case TableTag.FieldMarshal:
                        FieldMarshalTable.Write(ctxt, writer);
                        break;
                    case TableTag.FieldRVA:
                        FieldRVATable.Write(ctxt, writer);
                        break;
                    case TableTag.File:
                        FileTable.Write(ctxt, writer);
                        break;
                    case TableTag.GenericParam:
                        GenericParamTable.Write(ctxt, writer);
                        break;
                    case TableTag.GenericParamConstraint:
                        GenericParamConstraintTable.Write(ctxt, writer);
                        break;
                    case TableTag.ImplMap:
                        ImplMapTable.Write(ctxt, writer);
                        break;
                    case TableTag.InterfaceImpl:
                        InterfaceImplTable.Write(ctxt, writer);
                        break;
                    case TableTag.ManifestResource:
                        ManifestResourceTable.Write(ctxt, writer);
                        break;
                    case TableTag.MemberRef:
                        MemberRefTable.Write(ctxt, writer);
                        break;
                    case TableTag.MethodDef:
                        MethodDefTable.Write(ctxt, writer);
                        break;
                    case TableTag.MethodImpl:
                        MethodImplTable.Write(ctxt, writer);
                        break;
                    case TableTag.MethodSemantics:
                        MethodSemanticsTable.Write(ctxt, writer);
                        break;
                    case TableTag.MethodSpec:
                        MethodSpecTable.Write(ctxt, writer);
                        break;
                    case TableTag.ModuleRef:
                        ModuleRefTable.Write(ctxt, writer);
                        break;
                    case TableTag.NestedClass:
                        NestedClassTable.Write(ctxt, writer);
                        break;
                    case TableTag.Param:
                        ParamTable.Write(ctxt, writer);
                        break;
                    case TableTag.Property:
                        PropertyTable.Write(ctxt, writer);
                        break;
                    case TableTag.PropertyMap:
                        PropertyMapTable.Write(ctxt, writer);
                        break;
                    case TableTag.StandAloneSig:
                        StandAloneSigTable.Write(ctxt, writer);
                        break;
                    case TableTag.TypeDef:
                        TypeDefTable.Write(ctxt, writer);
                        break;
                    case TableTag.TypeRef:
                        TypeRefTable.Write(ctxt, writer);
                        break;
                    case TableTag.TypeSpec:
                        TypeSpecTable.Write(ctxt, writer);
                        break;
                    default:
                        throw new PEException("invalid TableTag");
                    }
                }
            }
        }
Пример #32
0
        public void Write(WriterContext ctxt, BlobWriter writer, uint beginOffset, Func <OpCode, object, Row> findRow)
        {
            var offset = (int)(writer.Offset - beginOffset);

            if (offset != Offset)
            {
                throw new PEException("invalid instruction offset");
            }
            var highByte = (ushort)OpCode >> 8;

            if (highByte > 0)
            {
                writer.WriteByte((byte)highByte);
            }
            writer.WriteByte((byte)OpCode);
            switch (OpCode)
            {
            case OpCode.Nop:
            case OpCode.Break:
            case OpCode.Ldarg_0:
            case OpCode.Ldarg_1:
            case OpCode.Ldarg_2:
            case OpCode.Ldarg_3:
            case OpCode.Ldloc_0:
            case OpCode.Ldloc_1:
            case OpCode.Ldloc_2:
            case OpCode.Ldloc_3:
            case OpCode.Stloc_0:
            case OpCode.Stloc_1:
            case OpCode.Stloc_2:
            case OpCode.Stloc_3:
            case OpCode.Ldnull:
            case OpCode.Ldc_i4_m1:
            case OpCode.Ldc_i4_0:
            case OpCode.Ldc_i4_1:
            case OpCode.Ldc_i4_2:
            case OpCode.Ldc_i4_3:
            case OpCode.Ldc_i4_4:
            case OpCode.Ldc_i4_5:
            case OpCode.Ldc_i4_6:
            case OpCode.Ldc_i4_7:
            case OpCode.Ldc_i4_8:
            case OpCode.Dup:
            case OpCode.Pop:
            case OpCode.Ret:
            case OpCode.Ldind_i1:
            case OpCode.Ldind_u1:
            case OpCode.Ldind_i2:
            case OpCode.Ldind_u2:
            case OpCode.Ldind_i4:
            case OpCode.Ldind_u4:
            case OpCode.Ldind_i8:
            case OpCode.Ldind_i:
            case OpCode.Ldind_r4:
            case OpCode.Ldind_r8:
            case OpCode.Ldind_ref:
            case OpCode.Stind_ref:
            case OpCode.Stind_i1:
            case OpCode.Stind_i2:
            case OpCode.Stind_i4:
            case OpCode.Stind_i8:
            case OpCode.Stind_r4:
            case OpCode.Stind_r8:
            case OpCode.Add:
            case OpCode.Sub:
            case OpCode.Mul:
            case OpCode.Div:
            case OpCode.Div_un:
            case OpCode.Rem:
            case OpCode.Rem_un:
            case OpCode.And:
            case OpCode.Or:
            case OpCode.Xor:
            case OpCode.Shl:
            case OpCode.Shr:
            case OpCode.Shr_un:
            case OpCode.Neg:
            case OpCode.Not:
            case OpCode.Conv_i1:
            case OpCode.Conv_i2:
            case OpCode.Conv_i4:
            case OpCode.Conv_i8:
            case OpCode.Conv_r4:
            case OpCode.Conv_r8:
            case OpCode.Conv_u4:
            case OpCode.Conv_u8:
            case OpCode.Conv_r_un:
            case OpCode.Throw:
            case OpCode.Conv_ovf_i1_un:
            case OpCode.Conv_ovf_i2_un:
            case OpCode.Conv_ovf_i4_un:
            case OpCode.Conv_ovf_i8_un:
            case OpCode.Conv_ovf_u1_un:
            case OpCode.Conv_ovf_u2_un:
            case OpCode.Conv_ovf_u4_un:
            case OpCode.Conv_ovf_u8_un:
            case OpCode.Conv_ovf_i_un:
            case OpCode.Conv_ovf_u_un:
            case OpCode.Ldlen:
            case OpCode.Ldelem_i1:
            case OpCode.Ldelem_u1:
            case OpCode.Ldelem_i2:
            case OpCode.Ldelem_u2:
            case OpCode.Ldelem_i4:
            case OpCode.Ldelem_u4:
            case OpCode.Ldelem_i8:
            case OpCode.Ldelem_i:
            case OpCode.Ldelem_r4:
            case OpCode.Ldelem_r8:
            case OpCode.Ldelem_ref:
            case OpCode.Stelem_i:
            case OpCode.Stelem_i1:
            case OpCode.Stelem_i2:
            case OpCode.Stelem_i4:
            case OpCode.Stelem_i8:
            case OpCode.Stelem_r4:
            case OpCode.Stelem_r8:
            case OpCode.Stelem_ref:
            case OpCode.Conv_ovf_i1:
            case OpCode.Conv_ovf_u1:
            case OpCode.Conv_ovf_i2:
            case OpCode.Conv_ovf_u2:
            case OpCode.Conv_ovf_i4:
            case OpCode.Conv_ovf_u4:
            case OpCode.Conv_ovf_i8:
            case OpCode.Conv_ovf_u8:
            case OpCode.Ckfinite:
            case OpCode.Conv_u2:
            case OpCode.Conv_u1:
            case OpCode.Conv_i:
            case OpCode.Conv_ovf_i:
            case OpCode.Conv_ovf_u:
            case OpCode.Add_ovf:
            case OpCode.Add_ovf_un:
            case OpCode.Mul_ovf:
            case OpCode.Mul_ovf_un:
            case OpCode.Sub_ovf:
            case OpCode.Sub_ovf_un:
            case OpCode.Endfinally:
            case OpCode.Stind_i:
            case OpCode.Conv_u:
            case OpCode.Prefix7:
            case OpCode.Prefix6:
            case OpCode.Prefix5:
            case OpCode.Prefix4:
            case OpCode.Prefix3:
            case OpCode.Prefix2:
            case OpCode.Prefix1:
            case OpCode.Prefixref:
            case OpCode.Arglist:
            case OpCode.Ceq:
            case OpCode.Cgt:
            case OpCode.Cgt_un:
            case OpCode.Clt:
            case OpCode.Clt_un:
            case OpCode.Localloc:
            case OpCode.Endfilter:
            case OpCode.Volatile:
            case OpCode.Tailcall:
            case OpCode.Cpblk:
            case OpCode.Initblk:
            case OpCode.Rethrow:
            case OpCode.Refanytype:
            case OpCode.Readonly:
                break;

            case OpCode.Br:
            case OpCode.Brfalse:
            case OpCode.Brtrue:
            case OpCode.Beq:
            case OpCode.Bge:
            case OpCode.Bgt:
            case OpCode.Ble:
            case OpCode.Blt:
            case OpCode.Bne_un:
            case OpCode.Bge_un:
            case OpCode.Bgt_un:
            case OpCode.Ble_un:
            case OpCode.Blt_un:
            case OpCode.Leave:
            {
                var target = (int)Value;
                // NOTE: Delta is relatative to start of next instruction
                var delta = (int)beginOffset + target - ((int)writer.Offset + 4);
                writer.WriteInt32(delta);
            }
            break;

            case OpCode.Br_s:
            case OpCode.Brfalse_s:
            case OpCode.Brtrue_s:
            case OpCode.Beq_s:
            case OpCode.Bge_s:
            case OpCode.Bgt_s:
            case OpCode.Ble_s:
            case OpCode.Blt_s:
            case OpCode.Bne_un_s:
            case OpCode.Bge_un_s:
            case OpCode.Bgt_un_s:
            case OpCode.Ble_un_s:
            case OpCode.Blt_un_s:
            case OpCode.Leave_s:
            {
                var target = (int)Value;
                // NOTE: Delta is w.r.t. begining of next instruction
                var delta = (int)beginOffset + target - ((int)writer.Offset + 1);
                if (delta > 0xff)
                {
                    throw new PEException("cannot use small form for this instruction");
                }
                writer.WriteSByte((sbyte)delta);
            }
            break;

            case OpCode.Ldc_i4_s:
                writer.WriteSByte((sbyte)(int)Value);
                break;

            case OpCode.Ldarg_s:
            case OpCode.Ldarga_s:
            case OpCode.Starg_s:
            case OpCode.Ldloc_s:
            case OpCode.Ldloca_s:
            case OpCode.Stloc_s:
            case OpCode.Unaligned:
                writer.WriteByte((byte)(int)Value);
                break;

            case OpCode.Ldc_i4:
                writer.WriteInt32((int)Value);
                break;

            case OpCode.Ldarg:
            case OpCode.Ldarga:
            case OpCode.Starg:
            case OpCode.Ldloc:
            case OpCode.Ldloca:
            case OpCode.Stloc:
                writer.WriteUInt32((uint)(int)Value);
                break;

            case OpCode.Ldc_i8:
                writer.WriteInt64((long)Value);
                break;

            case OpCode.Ldc_r4:
                writer.WriteSingle((float)Value);
                break;

            case OpCode.Ldc_r8:
                writer.WriteDouble((double)Value);
                break;

            case OpCode.Ldstr:
                WriteUserString(ctxt, writer, (string)Value);
                break;

            case OpCode.Switch:
            {
                var targets = (Seq <int>)Value;
                writer.WriteUInt32((uint)targets.Count);
                // NOTE: Deltas are w.r.t. start of next instruction
                for (var i = 0; i < targets.Count; i++)
                {
                    var delta = (int)beginOffset + targets[i] - ((int)writer.Offset + (targets.Count * 4));
                    writer.WriteInt32(delta);
                }
            }
            break;

            case OpCode.Calli:
            case OpCode.Jmp:
            case OpCode.Call:
            case OpCode.Callvirt:
            case OpCode.Newobj:
            case OpCode.Ldftn:
            case OpCode.Ldvirtftn:
            case OpCode.Ldfld:
            case OpCode.Ldflda:
            case OpCode.Stfld:
            case OpCode.Ldsfld:
            case OpCode.Ldsflda:
            case OpCode.Stsfld:
            case OpCode.Ldtoken:
            case OpCode.Cpobj:
            case OpCode.Ldobj:
            case OpCode.Castclass:
            case OpCode.Isinst:
            case OpCode.Unbox:
            case OpCode.Stobj:
            case OpCode.Box:
            case OpCode.Newarr:
            case OpCode.Ldelema:
            case OpCode.Ldelem:
            case OpCode.Stelem:
            case OpCode.Unbox_any:
            case OpCode.Refanyval:
            case OpCode.Mkrefany:
            case OpCode.Initobj:
            case OpCode.Constrained:
            case OpCode.Sizeof:
                WriteToken(ctxt, writer, findRow(OpCode, Value));
                break;

            default:
                throw new PEException("unrecognised opcode");
            }
        }