public R2RImportSection(int index, byte[] image, int rva, int size, CorCompileImportFlags flags, byte type, byte entrySize, int signatureRVA, List <ImportSectionEntry> entries, int auxDataRVA, int auxDataOffset, Machine machine, ushort majorVersion) { Index = index; SectionRVA = rva; SectionSize = size; Flags = flags; Type = (CorCompileImportType)type; EntrySize = entrySize; SignatureRVA = signatureRVA; Entries = entries; AuxiliaryDataRVA = auxDataRVA; AuxiliaryData = null; if (AuxiliaryDataRVA != 0) { if (machine == Machine.Amd64) { AuxiliaryData = new Amd64.GcInfo(image, auxDataOffset, machine, majorVersion); } else if (machine == Machine.I386) { AuxiliaryData = new x86.GcInfo(image, auxDataOffset, machine, majorVersion); } } }
public R2RImportSection( int index, R2RReader reader, int rva, int size, CorCompileImportFlags flags, byte type, byte entrySize, int signatureRVA, List <ImportSectionEntry> entries, int auxDataRVA, int auxDataOffset, Machine machine, ushort majorVersion) { Index = index; SectionRVA = rva; SectionSize = size; Flags = flags; Type = (CorCompileImportType)type; EntrySize = entrySize; SignatureRVA = signatureRVA; Entries = entries; AuxiliaryDataRVA = auxDataRVA; AuxiliaryDataSize = 0; if (AuxiliaryDataRVA != 0) { int endOffset = auxDataOffset + BitConverter.ToInt32(reader.Image, auxDataOffset); for (int i = 0; i < Entries.Count; i++) { int entryStartOffset = auxDataOffset + BitConverter.ToInt32(reader.Image, auxDataOffset + sizeof(int) * (Entries[i].Index / GCRefMap.GCREFMAP_LOOKUP_STRIDE)); int remaining = Entries[i].Index % GCRefMap.GCREFMAP_LOOKUP_STRIDE; while (remaining != 0) { while ((reader.Image[entryStartOffset] & 0x80) != 0) { entryStartOffset++; } entryStartOffset++; remaining--; } GCRefMapDecoder decoder = new GCRefMapDecoder(reader, entryStartOffset); Entries[i].GCRefMap = decoder.ReadMap(); endOffset = decoder.GetOffset(); } AuxiliaryDataSize = endOffset - auxDataOffset; } }
public ImportSectionNode(string name, CorCompileImportType importType, CorCompileImportFlags flags, byte entrySize, bool emitPrecode) { _name = name; _type = importType; _flags = flags; _entrySize = entrySize; _emitPrecode = emitPrecode; _imports = new ArrayOfEmbeddedDataNode <Import>(_name + "_ImportBegin", _name + "_ImportEnd", null); _signatures = new ArrayOfEmbeddedPointersNode <Signature>(_name + "_SigBegin", _name + "_SigEnd", null); }
public ImportSectionNode(string name, CorCompileImportType importType, CorCompileImportFlags flags, byte entrySize, bool emitPrecode, bool emitGCRefMap) { _name = name; _type = importType; _flags = flags; _entrySize = entrySize; _emitPrecode = emitPrecode; _emitGCRefMap = emitGCRefMap; _imports = new ImportTable(_name + "_ImportBegin", _name + "_ImportEnd"); _signatures = new ArrayOfEmbeddedPointersNode <Signature>(_name + "_SigBegin", _name + "_SigEnd", new EmbeddedObjectNodeComparer(new CompilerComparer())); _signatureList = new List <Signature>(); _gcRefMap = _emitGCRefMap ? new GCRefMapNode(this) : null; }
public R2RImportSection( int index, R2RReader reader, int rva, int size, CorCompileImportFlags flags, byte type, byte entrySize, int signatureRVA, List <ImportSectionEntry> entries, int auxDataRVA, int auxDataOffset, Machine machine, ushort majorVersion) { Index = index; SectionRVA = rva; SectionSize = size; Flags = flags; Type = (CorCompileImportType)type; EntrySize = entrySize; SignatureRVA = signatureRVA; Entries = entries; AuxiliaryDataRVA = auxDataRVA; AuxiliaryDataSize = 0; if (AuxiliaryDataRVA != 0) { int startOffset = auxDataOffset + BitConverter.ToInt32(reader.Image, auxDataOffset); for (int i = 0; i < Entries.Count; i++) { GCRefMapDecoder decoder = new GCRefMapDecoder(reader, startOffset); Entries[i].GCRefMap = decoder.ReadMap(); startOffset = decoder.GetOffset(); } AuxiliaryDataSize = startOffset - auxDataOffset; } }
/// <summary> /// based on <a href="https://github.com/dotnet/coreclr/blob/master/src/zap/zapimport.cpp">ZapImportSectionsTable::Save</a> /// </summary> private void EnsureImportSections() { if (_importSections != null) { return; } _importSections = new List <ReadyToRunImportSection>(); _importCellNames = new Dictionary <int, string>(); _importSignatures = new Dictionary <int, ReadyToRunSignature>(); if (!ReadyToRunHeader.Sections.TryGetValue(ReadyToRunSectionType.ImportSections, out ReadyToRunSection importSectionsSection)) { return; } int offset = GetOffset(importSectionsSection.RelativeVirtualAddress); int endOffset = offset + importSectionsSection.Size; while (offset < endOffset) { int rva = NativeReader.ReadInt32(Image, ref offset); int sectionOffset = GetOffset(rva); int startOffset = sectionOffset; int size = NativeReader.ReadInt32(Image, ref offset); CorCompileImportFlags flags = (CorCompileImportFlags)NativeReader.ReadUInt16(Image, ref offset); byte type = NativeReader.ReadByte(Image, ref offset); byte entrySize = NativeReader.ReadByte(Image, ref offset); if (entrySize == 0) { switch (Machine) { case Machine.I386: case Machine.ArmThumb2: entrySize = 4; break; case Machine.Amd64: case Machine.Arm64: entrySize = 8; break; default: throw new NotImplementedException(Machine.ToString()); } } int entryCount = 0; if (entrySize != 0) { entryCount = size / entrySize; } int signatureRVA = NativeReader.ReadInt32(Image, ref offset); int signatureOffset = 0; if (signatureRVA != 0) { signatureOffset = GetOffset(signatureRVA); } List <ReadyToRunImportSection.ImportSectionEntry> entries = new List <ReadyToRunImportSection.ImportSectionEntry>(); for (int i = 0; i < entryCount; i++) { int entryOffset = sectionOffset - startOffset; long section = NativeReader.ReadInt64(Image, ref sectionOffset); uint sigRva = NativeReader.ReadUInt32(Image, ref signatureOffset); int sigOffset = GetOffset((int)sigRva); ReadyToRunSignature signature; string cellName = MetadataNameFormatter.FormatSignature(_assemblyResolver, this, sigOffset, out signature); entries.Add(new ReadyToRunImportSection.ImportSectionEntry(entries.Count, entryOffset, entryOffset + rva, section, sigRva, cellName)); _importCellNames.Add(rva + entrySize * i, cellName); _importSignatures.Add(rva + entrySize * i, signature); } int auxDataRVA = NativeReader.ReadInt32(Image, ref offset); int auxDataOffset = 0; if (auxDataRVA != 0) { auxDataOffset = GetOffset(auxDataRVA); } _importSections.Add(new ReadyToRunImportSection(_importSections.Count, this, rva, size, flags, type, entrySize, signatureRVA, entries, auxDataRVA, auxDataOffset, Machine, ReadyToRunHeader.MajorVersion)); } }
/// <summary> /// based on <a href="https://github.com/dotnet/coreclr/blob/master/src/zap/zapimport.cpp">ZapImportSectionsTable::Save</a> /// </summary> private void ParseImportSections() { if (!R2RHeader.Sections.ContainsKey(R2RSection.SectionType.READYTORUN_SECTION_IMPORT_SECTIONS)) { return; } R2RSection importSectionsSection = R2RHeader.Sections[R2RSection.SectionType.READYTORUN_SECTION_IMPORT_SECTIONS]; int offset = GetOffset(importSectionsSection.RelativeVirtualAddress); int endOffset = offset + importSectionsSection.Size; while (offset < endOffset) { int rva = NativeReader.ReadInt32(Image, ref offset); int sectionOffset = GetOffset(rva); int startOffset = sectionOffset; int size = NativeReader.ReadInt32(Image, ref offset); CorCompileImportFlags flags = (CorCompileImportFlags)NativeReader.ReadUInt16(Image, ref offset); byte type = NativeReader.ReadByte(Image, ref offset); byte entrySize = NativeReader.ReadByte(Image, ref offset); if (entrySize == 0) { switch (Machine) { case Machine.I386: case Machine.ArmThumb2: entrySize = 4; break; case Machine.Amd64: case Machine.Arm64: entrySize = 8; break; default: throw new NotImplementedException(Machine.ToString()); } } int entryCount = 0; if (entrySize != 0) { entryCount = size / entrySize; } int signatureRVA = NativeReader.ReadInt32(Image, ref offset); int signatureOffset = 0; if (signatureRVA != 0) { signatureOffset = GetOffset(signatureRVA); } List <R2RImportSection.ImportSectionEntry> entries = new List <R2RImportSection.ImportSectionEntry>(); for (int i = 0; i < entryCount; i++) { int entryOffset = sectionOffset - startOffset; long section = NativeReader.ReadInt64(Image, ref sectionOffset); uint sigRva = NativeReader.ReadUInt32(Image, ref signatureOffset); int sigOffset = GetOffset((int)sigRva); string cellName = MetadataNameFormatter.FormatSignature(_assemblyResolver, this, sigOffset); entries.Add(new R2RImportSection.ImportSectionEntry(entries.Count, entryOffset, entryOffset + rva, section, sigRva, cellName)); ImportCellNames.Add(rva + entrySize * i, cellName); } int auxDataRVA = NativeReader.ReadInt32(Image, ref offset); int auxDataOffset = 0; if (auxDataRVA != 0) { auxDataOffset = GetOffset(auxDataRVA); } ImportSections.Add(new R2RImportSection(ImportSections.Count, this, rva, size, flags, type, entrySize, signatureRVA, entries, auxDataRVA, auxDataOffset, Machine, R2RHeader.MajorVersion)); } }