void ReadSectionData(Section section) { var position = BaseStream.Position; MoveTo(section.PointerToRawData); var length = (int)section.SizeOfRawData; var data = new byte[length]; int offset = 0, read; while ((read = Read(data, offset, length - offset)) > 0) offset += read; section.Data = data; BaseStream.Position = position; }
void ReadSections(ushort count) { var sections = new Section[count]; for (int i = 0; i < count; i++) { var section = new Section(); // Name section.Name = ReadZeroTerminatedString(8); // VirtualSize 4 Advance(4); // VirtualAddress 4 section.VirtualAddress = ReadUInt32(); // SizeOfRawData 4 section.SizeOfRawData = ReadUInt32(); // PointerToRawData 4 section.PointerToRawData = ReadUInt32(); // PointerToRelocations 4 // PointerToLineNumbers 4 // NumberOfRelocations 2 // NumberOfLineNumbers 2 // Characteristics 4 Advance(16); sections[i] = section; ReadSectionData(section); } image.Sections = sections; }
public uint ResolveVirtualAddressInSection(RVA rva, Section section) { return rva + section.PointerToRawData - section.VirtualAddress; }
void ReadMetadataStream(Section section) { // Offset 4 uint start = metadata.VirtualAddress - section.VirtualAddress + ReadUInt32(); // relative to the section start // Size 4 uint size = ReadUInt32(); var name = ReadAlignedString(16); switch (name) { case "#~": case "#-": image.TableHeap = new TableHeap(section, start, size); break; case "#Strings": image.StringHeap = new StringHeap(section, start, size); break; case "#Blob": image.BlobHeap = new BlobHeap(section, start, size); break; case "#GUID": image.GuidHeap = new GuidHeap(section, start, size); break; case "#US": image.UserStringHeap = new UserStringHeap(section, start, size); break; } }
protected Heap(Section section, uint offset, uint size) { this.Section = section; this.Offset = offset; this.Size = size; }
public StringHeap(Section section, uint start, uint size) : base(section, start, size) { }
public BlobHeap(Section section, uint start, uint size) : base(section, start, size) { }
public TableHeap(Section section, uint start, uint size) : base(section, start, size) { }
void PrepareSection(Section section) { MoveTo(section.PointerToRawData); const int buffer_size = 4096; if (section.SizeOfRawData <= buffer_size) { Write(new byte[section.SizeOfRawData]); MoveTo(section.PointerToRawData); return; } var written = 0; var buffer = new byte[buffer_size]; while (written != section.SizeOfRawData) { var write_size = System.Math.Min((int)section.SizeOfRawData - written, buffer_size); Write(buffer, 0, write_size); written += write_size; } MoveTo(section.PointerToRawData); }
void WriteSection(Section section, uint characteristics) { var name = new byte[8]; var sect_name = section.Name; for (int i = 0; i < sect_name.Length; i++) name[i] = (byte)sect_name[i]; WriteBytes(name); WriteUInt32(section.VirtualSize); WriteUInt32(section.VirtualAddress); WriteUInt32(section.SizeOfRawData); WriteUInt32(section.PointerToRawData); WriteUInt32(0); // PointerToRelocations WriteUInt32(0); // PointerToLineNumbers WriteUInt16(0); // NumberOfRelocations WriteUInt16(0); // NumberOfLineNumbers WriteUInt32(characteristics); }
void MoveToRVA(Section section, RVA rva) { BaseStream.Seek(section.PointerToRawData + rva - section.VirtualAddress, SeekOrigin.Begin); }
Section CreateSection(string name, uint size, Section previous) { return new Section { Name = name, VirtualAddress = previous != null ? previous.VirtualAddress + Align(previous.VirtualSize, section_alignment) : text_rva, VirtualSize = size, PointerToRawData = previous != null ? previous.PointerToRawData + previous.SizeOfRawData : Align(GetHeaderSize(), file_alignment), SizeOfRawData = Align(size, file_alignment) }; }
void BuildSections() { var has_win32_resources = win32_resources != null; if (has_win32_resources) sections++; text = CreateSection(".text", text_map.GetLength(), null); var previous = text; if (has_win32_resources) { rsrc = CreateSection(".rsrc", (uint)win32_resources.length, previous); PatchWin32Resources(win32_resources); previous = rsrc; } if (has_reloc) reloc = CreateSection(".reloc", 12u, previous); }
public void MoveTo(int rva) { if (!IsInSection(rva)) { code_section = reader.image.GetSectionAtVirtualAddress((uint)rva); Reset(code_section.Data); } base.position = rva - (int)code_section.VirtualAddress; }
public CodeReader(Section section, MetadataReader reader) : base(section.Data) { this.code_section = section; this.reader = reader; }