Skip to content

Commit

Permalink
Preserve LargeAwareAddress image flag (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbevain authored Jul 1, 2021
1 parent b0b93f5 commit d820a5b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Mono.Cecil.PE/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ sealed class Image : IDisposable {
public string FileName;

public ModuleKind Kind;
public uint Characteristics;
public string RuntimeVersion;
public TargetArchitecture Architecture;
public ModuleCharacteristics Characteristics;
public ModuleCharacteristics DllCharacteristics;
public ushort LinkerVersion;
public ushort SubSystemMajor;
public ushort SubSystemMinor;
Expand Down
3 changes: 2 additions & 1 deletion Mono.Cecil.PE/ImageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ void ReadImage ()
ReadMetadata ();
ReadDebugHeader ();

image.Characteristics = characteristics;
image.Kind = GetModuleKind (characteristics, subsystem);
image.Characteristics = (ModuleCharacteristics) dll_characteristics;
image.DllCharacteristics = (ModuleCharacteristics) dll_characteristics;
}

TargetArchitecture ReadArchitecture ()
Expand Down
12 changes: 9 additions & 3 deletions Mono.Cecil.PE/ImageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,18 @@ void WritePEFileHeader ()
WriteUInt32 (metadata.timestamp);
WriteUInt32 (0); // PointerToSymbolTable
WriteUInt32 (0); // NumberOfSymbols
WriteUInt16 (SizeOfOptionalHeader ()); // SizeOfOptionalHeader
WriteUInt16 (SizeOfOptionalHeader ()); // SizeOfOptionalHeader

// ExecutableImage | (pe64 ? 32BitsMachine : LargeAddressAware)
var characteristics = (ushort) (0x0002 | (!pe64 ? 0x0100 : 0x0020));
const ushort LargeAddressAware = 0x0020;

// ExecutableImage | (!pe64 ? 32BitsMachine : LargeAddressAware)
var characteristics = (ushort) (0x0002 | (!pe64 ? 0x0100 : LargeAddressAware));
if (module.Kind == ModuleKind.Dll || module.Kind == ModuleKind.NetModule)
characteristics |= 0x2000;

if (module.Image != null && (module.Image.Characteristics & LargeAddressAware) != 0)
characteristics |= LargeAddressAware;

WriteUInt16 (characteristics); // Characteristics
}

Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ internal ModuleDefinition (Image image)
this.RuntimeVersion = image.RuntimeVersion;
this.architecture = image.Architecture;
this.attributes = image.Attributes;
this.characteristics = image.Characteristics;
this.characteristics = image.DllCharacteristics;
this.linker_version = image.LinkerVersion;
this.subsystem_major = image.SubSystemMajor;
this.subsystem_minor = image.SubSystemMinor;
Expand Down
8 changes: 8 additions & 0 deletions Test/Mono.Cecil.Tests/ImageReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public void X64Module ()
}, verify: !Platform.OnMono);
}

[Test]
public void AnyCPU32BitPreferred ()
{
TestModule ("anycpu32bitpreferred.exe", module => {
Assert.AreNotEqual (0, module.Image.Characteristics & 0x0020);
});
}

[Test]
public void X64ModuleTextOnlySection ()
{
Expand Down
Binary file not shown.

0 comments on commit d820a5b

Please sign in to comment.