public static ImageCor20HeaderTreeNode Create(HexDocument doc, IPEImage peImage) { var dnDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14]; if (dnDir.VirtualAddress != 0 && dnDir.Size >= 0x48) return new ImageCor20HeaderTreeNode(doc, (ulong)peImage.ToFileOffset(dnDir.VirtualAddress)); return null; }
/// <summary> /// Initializes the metadata, tables, streams /// </summary> public void Initialize(DataReaderFactory mdReaderFactory) { mdReaderFactoryToDisposeLater = mdReaderFactory; uint metadataBaseOffset; if (peImage != null) { Debug.Assert(mdReaderFactory == null); Debug.Assert(cor20Header != null); metadataBaseOffset = (uint)peImage.ToFileOffset(cor20Header.Metadata.VirtualAddress); mdReaderFactory = peImage.DataReaderFactory; } else { Debug.Assert(mdReaderFactory != null); metadataBaseOffset = 0; } InitializeInternal(mdReaderFactory, metadataBaseOffset); if (tablesStream == null) { throw new BadImageFormatException("Missing MD stream"); } if (isStandalonePortablePdb && pdbStream == null) { throw new BadImageFormatException("Missing #Pdb stream"); } InitializeNonExistentHeaps(); }
internal void InitializeReusedMethodBodies(IPEImage peImage, uint fileOffsetDelta) { foreach (var info in reusedMethods) { var offset = peImage.ToFileOffset(info.RVA) + fileOffsetDelta; info.MethodBody.SetOffset(offset, info.RVA); } }
uint GetLastFileSectionOffset() { uint rva = 0; foreach (var sect in origSections) { rva = Math.Max(rva, (uint)sect.peSection.VirtualAddress + sect.peSection.SizeOfRawData); } return((uint)peImage.ToFileOffset((RVA)(rva - 1)) + 1); }
public static ImageCor20HeaderTreeNode Create(HexDocument doc, IPEImage peImage) { var dnDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14]; if (dnDir.VirtualAddress != 0 && dnDir.Size >= 0x48) { return(new ImageCor20HeaderTreeNode(doc, (ulong)peImage.ToFileOffset(dnDir.VirtualAddress))); } return(null); }
/// <summary> /// Initializes the metadata, tables, streams /// </summary> public void Initialize(DataReaderFactory mdReaderFactory) { mdReaderFactoryToDisposeLater = mdReaderFactory; uint metadataBaseOffset; if (!(peImage is null)) { Debug.Assert(mdReaderFactory is null); Debug.Assert(!(cor20Header is null)); metadataBaseOffset = (uint)peImage.ToFileOffset(cor20Header.Metadata.VirtualAddress); mdReaderFactory = peImage.DataReaderFactory; }
protected override bool TryGetExportedRvaFromStubCore(ref DataReader reader, IPEImage peImage, out uint funcRva) { funcRva = 0; // From ExportStubIA64Template in coreclr/src/ilasm/writer.cpp // // ld8 r9 = [gp] ;; // ld8 r10 = [r9],8 // nop.i ;; // ld8 gp = [r9] // mov b6 = r10 // br.cond.sptk.few b6 // // 0x0B, 0x48, 0x00, 0x02, 0x18, 0x10, 0xA0, 0x40, // 0x24, 0x30, 0x28, 0x00, 0x00, 0x00, 0x04, 0x00, // 0x10, 0x08, 0x00, 0x12, 0x18, 0x10, 0x60, 0x50, // 0x04, 0x80, 0x03, 0x00, 0x60, 0x00, 0x80, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//address of the template // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //address of VTFixup slot ulong addrTemplate = reader.ReadUInt64(); ulong absAddr = reader.ReadUInt64(); reader.Position = (uint)peImage.ToFileOffset((RVA)(addrTemplate - peImage.ImageNTHeaders.OptionalHeader.ImageBase)); if (reader.ReadUInt64() != 0x40A010180200480BUL) { return(false); } if (reader.ReadUInt64() != 0x0004000000283024UL) { return(false); } if (reader.ReadUInt64() != 0x5060101812000810UL) { return(false); } if (reader.ReadUInt64() != 0x0080006000038004UL) { return(false); } ulong rva = absAddr - peImage.ImageNTHeaders.OptionalHeader.ImageBase; if (rva > uint.MaxValue) { return(false); } funcRva = (uint)rva; return(true); }
static Dictionary <long, MethodExportInfo> GetOffsetToExportInfoDictionary(IImageStream reader, IPEImage peImage, ImageDataDirectory exportHdr, CpuArch cpuArch) { reader.Position = (long)peImage.ToFileOffset(exportHdr.VirtualAddress); // Skip Characteristics(4), TimeDateStamp(4), MajorVersion(2), MinorVersion(2), Name(4) reader.Position += 16; uint ordinalBase = reader.ReadUInt32(); int numFuncs = reader.ReadInt32(); int numNames = reader.ReadInt32(); long offsetOfFuncs = (long)peImage.ToFileOffset((RVA)reader.ReadUInt32()); long offsetOfNames = (long)peImage.ToFileOffset((RVA)reader.ReadUInt32()); long offsetOfNameIndexes = (long)peImage.ToFileOffset((RVA)reader.ReadUInt32()); var names = ReadNames(reader, peImage, numNames, offsetOfNames, offsetOfNameIndexes); reader.Position = offsetOfFuncs; var allInfos = new MethodExportInfo[numFuncs]; var dict = new Dictionary <long, MethodExportInfo>(numFuncs); for (int i = 0; i < allInfos.Length; i++) { var currOffset = reader.Position; var nextOffset = reader.Position + 4; uint funcRva = 0; var rva = (RVA)reader.ReadUInt32(); reader.Position = (long)peImage.ToFileOffset(rva); bool rvaValid = rva != 0 && cpuArch.TryGetExportedRvaFromStub(reader, peImage, out funcRva); long funcOffset = rvaValid ? (long)peImage.ToFileOffset((RVA)funcRva) : 0; var exportInfo = new MethodExportInfo((ushort)(ordinalBase + (uint)i)); if (funcOffset != 0) { dict[funcOffset] = exportInfo; } allInfos[i] = exportInfo; reader.Position = nextOffset; } foreach (var info in names) { int index = info.Index; if ((uint)index >= (uint)numFuncs) { continue; } allInfos[index].Ordinal = null; allInfos[index].Name = info.Name; } return(dict); }
static NameAndIndex[] ReadNames(ref DataReader reader, IPEImage peImage, int numNames, uint offsetOfNames, uint offsetOfNameIndexes) { var names = new NameAndIndex[numNames]; reader.Position = offsetOfNameIndexes; for (int i = 0; i < names.Length; i++) { names[i].Index = reader.ReadUInt16(); } var currentOffset = offsetOfNames; for (int i = 0; i < names.Length; i++, currentOffset += 4) { reader.Position = currentOffset; uint offsetOfName = (uint)peImage.ToFileOffset((RVA)reader.ReadUInt32()); names[i].Name = ReadMethodNameASCIIZ(ref reader, offsetOfName); } return(names); }
/// <summary> /// Creates a stream to access part of the PE image from <paramref name="rva"/> /// with length <paramref name="length"/> /// </summary> /// <param name="self">this</param> /// <param name="rva">RVA</param> /// <param name="length">Length of data</param> /// <returns>A new stream</returns> /// <exception cref="ArgumentOutOfRangeException">If any arg is invalid</exception> public static IImageStream CreateStream(this IPEImage self, RVA rva, long length) { return(self.CreateStream(self.ToFileOffset(rva), length)); }
public uint RvaToOffset(uint rva) => (uint)peImage.ToFileOffset((RVA)rva);
public uint RvaToOffset(uint rva) { return((uint)peImage.ToFileOffset((RVA)rva)); }