void PatchRawSection(ByteBuffer buffer, MetadataBuilder metadata) { var position = base.position; Align (4); buffer.WriteBytes (base.position - position); const byte fat_format = 0x40; const byte more_sects = 0x80; var flags = ReadByte (); if ((flags & fat_format) == 0) { buffer.WriteByte (flags); PatchRawSmallSection (buffer, metadata); } else PatchRawFatSection (buffer, metadata); if ((flags & more_sects) != 0) PatchRawSection (buffer, metadata); }
void PatchRawExceptionHandlers(ByteBuffer buffer, MetadataBuilder metadata, int count, bool fat_entry) { const int fat_entry_size = 16; const int small_entry_size = 6; for (int i = 0; i < count; i++) { ExceptionHandlerType handler_type; if (fat_entry) { var type = ReadUInt32 (); handler_type = (ExceptionHandlerType) (type & 0x7); buffer.WriteUInt32 (type); } else { var type = ReadUInt16 (); handler_type = (ExceptionHandlerType) (type & 0x7); buffer.WriteUInt16 (type); } buffer.WriteBytes (ReadBytes (fat_entry ? fat_entry_size : small_entry_size)); switch (handler_type) { case ExceptionHandlerType.Catch: var exception = reader.LookupToken (ReadToken ()); buffer.WriteUInt32 (metadata.LookupToken (exception).ToUInt32 ()); break; default: buffer.WriteUInt32 (ReadUInt32 ()); break; } } }
private void WriteWin32ResourcesDirectory(ResourceDirectory directory) { win32_resources = new ByteBuffer(); if (directory.Entries.Count != 0) { int stringTableOffset = GetDirectoryLength(directory); Dictionary<string, int> strings = new Dictionary<string, int>(); ByteBuffer stringTable = new ByteBuffer(16); int offset = 16 + directory.Entries.Count * 8; for (int pass = 0; pass < 3; pass++) Write(directory, pass, 0, ref offset, strings, ref stringTableOffset, stringTable); // the pecoff spec says that the string table is between the directory entries and the data entries, // but the windows linker puts them after the data entries, so we do too. stringTable.Align(4); offset += stringTable.length; WriteResourceDataEntries(directory, ref offset); win32_resources.WriteBytes(stringTable); WriteData(directory); } }
void PatchRawCode(ByteBuffer buffer, int code_size, CodeWriter writer) { var metadata = writer.metadata; buffer.WriteBytes (ReadBytes (code_size)); var end = buffer.position; buffer.position -= code_size; while (buffer.position < end) { OpCode opcode; var il_opcode = buffer.ReadByte (); if (il_opcode != 0xfe) { opcode = OpCodes.OneByteOpCode [il_opcode]; } else { var il_opcode2 = buffer.ReadByte (); opcode = OpCodes.TwoBytesOpCode [il_opcode2]; } switch (opcode.OperandType) { case OperandType.ShortInlineI: case OperandType.ShortInlineBrTarget: case OperandType.ShortInlineVar: case OperandType.ShortInlineArg: buffer.position += 1; break; case OperandType.InlineVar: case OperandType.InlineArg: buffer.position += 2; break; case OperandType.InlineBrTarget: case OperandType.ShortInlineR: case OperandType.InlineI: buffer.position += 4; break; case OperandType.InlineI8: case OperandType.InlineR: buffer.position += 8; break; case OperandType.InlineSwitch: var length = buffer.ReadInt32 (); buffer.position += length * 4; break; case OperandType.InlineString: var @string = GetString (new MetadataToken (buffer.ReadUInt32 ())); buffer.position -= 4; buffer.WriteUInt32 ( new MetadataToken ( TokenType.String, metadata.user_string_heap.GetStringIndex (@string)).ToUInt32 ()); break; case OperandType.InlineSig: var call_site = GetCallSite (new MetadataToken (buffer.ReadUInt32 ())); buffer.position -= 4; buffer.WriteUInt32 (writer.GetStandAloneSignature (call_site).ToUInt32 ()); break; case OperandType.InlineTok: case OperandType.InlineType: case OperandType.InlineMethod: case OperandType.InlineField: var provider = reader.LookupToken (new MetadataToken (buffer.ReadUInt32 ())); buffer.position -= 4; buffer.WriteUInt32 (metadata.LookupToken (provider).ToUInt32 ()); break; } } }
public bool GetDebugHeader(out ImageDebugDirectory directory, out byte [] header) { if (IsEmbedded) { directory = new ImageDebugDirectory (); header = Empty<byte>.Array; return false; } directory = new ImageDebugDirectory () { MajorVersion = 256, MinorVersion = 20577, Type = 2, }; var buffer = new ByteBuffer (); // RSDS buffer.WriteUInt32 (0x53445352); // Module ID buffer.WriteBytes (module.Mvid.ToByteArray ()); // PDB Age buffer.WriteUInt32 (1); // PDB Path buffer.WriteBytes (System.Text.Encoding.UTF8.GetBytes (writer.BaseStream.GetFileName ())); buffer.WriteByte (0); header = new byte [buffer.length]; Buffer.BlockCopy (buffer.buffer, 0, header, 0, buffer.length); directory.SizeOfData = header.Length; return true; }