Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #410 from mike-barnett/master
Browse files Browse the repository at this point in the history
For 64-bit assemblies, preserve the imagebase value when rewriting.
  • Loading branch information
SergeyTeplyakov committed May 3, 2016
2 parents 6b1f006 + b1f94fb commit 54098c5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
20 changes: 13 additions & 7 deletions System.Compiler/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ internal class NTHeader {
internal int addressOfEntryPoint;
internal int baseOfCode;
internal int baseOfData;
internal long imageBase;
internal ulong imageBase;
internal int sectionAlignment;
internal int fileAlignment;
internal ushort majorOperatingSystemVersion;
Expand Down Expand Up @@ -523,6 +523,8 @@ unsafe internal class MetadataReader : IDisposable{
readonly private MemoryCursor/*!*/ cursor;
internal int entryPointToken;
internal int fileAlignment;
internal ulong baseAddress;
internal long sizeOfStackReserve;
internal ModuleKindFlags moduleKind;
internal PEKindFlags peKind;
internal bool TrackDebugData;
Expand Down Expand Up @@ -924,6 +926,8 @@ private void ReadHeader(){ //TODO: break up this method
this.linkerMajorVersion = ntHeader.majorLinkerVersion;
this.linkerMinorVersion = ntHeader.minorLinkerVersion;
this.fileAlignment = ntHeader.fileAlignment;
this.baseAddress = ntHeader.imageBase;
this.sizeOfStackReserve = ntHeader.sizeOfStackReserve;
if ((ntHeader.characteristics & 0x2000) != 0)
this.moduleKind = ModuleKindFlags.DynamicallyLinkedLibrary;
else
Expand Down Expand Up @@ -2058,10 +2062,10 @@ internal static void ReadDOSHeader(MemoryCursor/*!*/ c) {
header.baseOfCode = c.ReadInt32();
if (header.magic == 0x10B){
header.baseOfData = c.ReadInt32();
header.imageBase = c.ReadInt32();
header.imageBase = c.ReadUInt32();
}else{
header.baseOfData = 0;
header.imageBase = c.ReadInt64();
header.imageBase = c.ReadUInt64();
}
header.sectionAlignment = c.ReadInt32();
header.fileAlignment = c.ReadInt32();
Expand Down Expand Up @@ -2208,6 +2212,8 @@ internal class MetadataWriter{
internal TypeSpecRow[] typeSpecTable;
internal int entryPointToken;
internal int fileAlignment;
internal ulong baseAddress;
internal long sizeOfStackReserve;
internal ModuleKindFlags moduleKind;
internal ushort dllCharacteristics;
internal PEKindFlags peKind;
Expand Down Expand Up @@ -3414,9 +3420,9 @@ private void WriteNTHeader(BinaryWriter/*!*/ writer)
writer.Write(this.sectionHeaders[1].virtualAddress); //baseOfData
else
writer.Write((int)0);
writer.Write((int)ntHeader.imageBase);
writer.Write((int)ntHeader.imageBase); // don't use the imageBase read in from the input assembly: it creates bad modules.
}else{
writer.Write(ntHeader.imageBase);
writer.Write(this.baseAddress); // imageBase
}
writer.Write(ntHeader.sectionAlignment);
writer.Write(this.fileAlignment);
Expand All @@ -3435,12 +3441,12 @@ private void WriteNTHeader(BinaryWriter/*!*/ writer)
writer.Write(ntHeader.subsystem);
writer.Write(ntHeader.dllCharacteristics);
if (ntHeader.magic == 0x10B){
writer.Write((int)ntHeader.sizeOfStackReserve);
writer.Write((int)this.sizeOfStackReserve);
writer.Write((int)ntHeader.sizeOfStackCommit);
writer.Write((int)ntHeader.sizeOfHeapReserve);
writer.Write((int)ntHeader.sizeOfHeapCommit);
}else{
writer.Write(ntHeader.sizeOfStackReserve);
writer.Write(this.sizeOfStackReserve);
writer.Write(ntHeader.sizeOfStackCommit);
writer.Write(ntHeader.sizeOfHeapReserve);
writer.Write(ntHeader.sizeOfHeapCommit);
Expand Down
2 changes: 2 additions & 0 deletions System.Compiler/Nodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5452,6 +5452,8 @@ public class Module : Node, IDisposable{
public bool UsePublicKeyTokensForAssemblyReferences = true;
#endif
internal int FileAlignment = 512;
internal ulong BaseAddress;
internal long SizeOfStackReserve;
internal readonly static object GlobalLock = new object();
#if !NoWriter
public bool StripOptionalModifiersFromLocals = true;
Expand Down
2 changes: 2 additions & 0 deletions System.Compiler/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ private void ReadModuleProperties(Module/*!*/ module){
module.reader = this;
module.DllCharacteristics = this.tables.dllCharacteristics;
module.FileAlignment = this.tables.fileAlignment;
module.BaseAddress = this.tables.baseAddress;
module.SizeOfStackReserve = this.tables.sizeOfStackReserve;
module.HashValue = this.tables.HashValue;
module.Kind = this.tables.moduleKind;
module.Location = this.fileName;
Expand Down
2 changes: 2 additions & 0 deletions System.Compiler/Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ private void SetupMetadataWriter(string debugSymbolsLocation){
writer.peKind = module.PEKind;
writer.TrackDebugData = module.TrackDebugData;
writer.fileAlignment = module.FileAlignment;
writer.baseAddress = module.BaseAddress;
writer.sizeOfStackReserve = module.SizeOfStackReserve;
if (writer.fileAlignment < 512) writer.fileAlignment = 512;
writer.PublicKey = this.PublicKey;
writer.SignatureKeyLength = this.SignatureKeyLength;
Expand Down

0 comments on commit 54098c5

Please sign in to comment.