Browse Source

Fix #661: Emit ".data" IL directives for fields with associated data

pull/1556/head
Daniel Grunwald 6 years ago
parent
commit
f5317b853a
  1. 16
      ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs
  2. 19
      ICSharpCode.Decompiler/SRMExtensions.cs

16
ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs

@ -1112,7 +1112,7 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -1112,7 +1112,7 @@ namespace ICSharpCode.Decompiler.Disassembler
var fieldName = metadata.GetString(fieldDefinition.Name);
output.Write(DisassemblerHelpers.Escape(fieldName));
if (fieldDefinition.HasFlag(FieldAttributes.HasFieldRVA)) {
output.Write(" at I_{0:x8}", fieldDefinition.GetRelativeVirtualAddress());
output.Write(" at D_{0:X8}", fieldDefinition.GetRelativeVirtualAddress());
}
var defaultValue = fieldDefinition.GetDefaultValue();
@ -1127,6 +1127,20 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -1127,6 +1127,20 @@ namespace ICSharpCode.Decompiler.Disassembler
WriteAttributes(module, fieldDefinition.GetCustomAttributes());
output.MarkFoldEnd();
}
if (fieldDefinition.HasFlag(FieldAttributes.HasFieldRVA)) {
BlobReader initVal;
try {
initVal = fieldDefinition.GetInitialValue(module.Reader, null);
} catch (BadImageFormatException ex) {
initVal = default;
output.WriteLine("// .data D_{0:X8} = {1}", fieldDefinition.GetRelativeVirtualAddress(), ex.Message);
}
if (initVal.Length > 0) {
output.Write(".data D_{0:X8} = bytearray ", fieldDefinition.GetRelativeVirtualAddress());
WriteBlob(initVal);
output.WriteLine();
}
}
}
#endregion

19
ICSharpCode.Decompiler/SRMExtensions.cs

@ -386,23 +386,25 @@ namespace ICSharpCode.Decompiler @@ -386,23 +386,25 @@ namespace ICSharpCode.Decompiler
sealed class FieldValueSizeDecoder : ISignatureTypeProvider<int, GenericContext>
{
MetadataModule module;
readonly MetadataModule module;
// This is the same as Cecil does, but probably not a good idea.
static readonly int pointerSize = IntPtr.Size;
public FieldValueSizeDecoder(ICompilation typeSystem)
public FieldValueSizeDecoder(ICompilation typeSystem = null)
{
this.module = (MetadataModule)typeSystem.MainModule;
this.module = (MetadataModule)typeSystem?.MainModule;
}
public int GetArrayType(int elementType, ArrayShape shape) => GetPrimitiveType(PrimitiveTypeCode.Object);
public int GetSZArrayType(int elementType) => GetPrimitiveType(PrimitiveTypeCode.Object);
public int GetByReferenceType(int elementType) => GetPointerType(elementType);
public int GetFunctionPointerType(MethodSignature<int> signature) => GetPrimitiveType(PrimitiveTypeCode.IntPtr);
public int GetByReferenceType(int elementType) => pointerSize;
public int GetFunctionPointerType(MethodSignature<int> signature) => pointerSize;
public int GetGenericInstantiation(int genericType, ImmutableArray<int> typeArguments) => genericType;
public int GetGenericMethodParameter(GenericContext genericContext, int index) => 0;
public int GetGenericTypeParameter(GenericContext genericContext, int index) => 0;
public int GetModifiedType(int modifier, int unmodifiedType, bool isRequired) => unmodifiedType;
public int GetPinnedType(int elementType) => elementType;
public int GetPointerType(int elementType) => GetPrimitiveType(PrimitiveTypeCode.IntPtr);
public int GetPointerType(int elementType) => pointerSize;
public int GetPrimitiveType(PrimitiveTypeCode typeCode)
{
@ -425,8 +427,7 @@ namespace ICSharpCode.Decompiler @@ -425,8 +427,7 @@ namespace ICSharpCode.Decompiler
return 8;
case PrimitiveTypeCode.IntPtr:
case PrimitiveTypeCode.UIntPtr:
// This is the same as Cecil does, but probably not a good idea.
return IntPtr.Size;
return pointerSize;
default:
return 0;
}
@ -440,7 +441,7 @@ namespace ICSharpCode.Decompiler @@ -440,7 +441,7 @@ namespace ICSharpCode.Decompiler
public int GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind)
{
var typeDef = module.ResolveType(handle, new GenericContext()).GetDefinition();
var typeDef = module?.ResolveType(handle, new GenericContext()).GetDefinition();
if (typeDef == null || typeDef.MetadataToken.IsNil)
return 0;
reader = typeDef.ParentModule.PEFile.Metadata;

Loading…
Cancel
Save