private static BaseRelocationBlock[] GetBaseRelocationsFor(string file, PEFile pe) { var stream = new MemoryStream(File.ReadAllBytes(file)); var reader = new BinaryStreamReader(stream, new byte[1024]); pe.ReadFrom(reader); var baseRelocationDirectory = pe.OptionalHeader.DataDirectories[(int)DataDirectoryKind.BaseRelocation]; var rvaStream = new RvaStream( stream, pe.SectionHeaders.Select( s => new RvaStream.Range { PhysicalAddress = s.PointerToRawData, Size = s.VirtualSize, VirtualAddress = s.VirtualAddress }) .ToArray()); rvaStream.Position = baseRelocationDirectory.VirtualAddress; var sectionReader = new BinaryStreamReader(rvaStream, new byte[32]); var result = BaseRelocationBlock.ReadBlocks(sectionReader, baseRelocationDirectory.Size); return(result); }
private static void PrintBaseRelocations(BaseRelocationBlock[] relocBlocks) { foreach (var b in relocBlocks) { Console.WriteLine(b.PageRVA.ToString("X")+"h ("+b.Size+")"); foreach (var e in b.Entries) { Console.WriteLine(" " + e.Offset.ToString("X").PadLeft(4, '0') + "h " + e.Type); } } }
private void AppendRelocationDirectory() { if (Assembly.RelocationDirectory != null) { Assembly.RelocationDirectory.Blocks.Clear(); var block = new BaseRelocationBlock(0); block.Entries.Add(new BaseRelocationEntry(BaseRelocationType.HighLow, 0)); block.Entries.Add(new BaseRelocationEntry(BaseRelocationType.Absolute, 0)); Assembly.RelocationDirectory.Blocks.Add(block); _relocSectionBuilder.Segments.Add(Assembly.RelocationDirectory); } }
private void CreateRelocationSection() { Assembly.RelocationDirectory.Blocks.Clear(); var block = new BaseRelocationBlock(0); block.Entries.Add(new BaseRelocationEntry(BaseRelocationType.HighLow, 0)); block.Entries.Add(new BaseRelocationEntry(BaseRelocationType.Absolute, 0)); Assembly.RelocationDirectory.Blocks.Add(block); _relocSectionHeader = new ImageSectionHeader { Name = ".reloc", Attributes = ImageSectionAttributes.MemoryRead | ImageSectionAttributes.ContentInitializedData, Section = { Segments = { Assembly.RelocationDirectory } } }; }