Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not write a reloc section for pe64 assemblies

  • Loading branch information...
commit 4ef434e294031c985cc98a81004d254e6ecb269a 1 parent b37bdfa
@jbevain authored
Showing with 47 additions and 41 deletions.
  1. +46 −40 Mono.Cecil.PE/ImageWriter.cs
  2. +1 −1  Mono.Cecil/AssemblyWriter.cs
View
86 Mono.Cecil.PE/ImageWriter.cs
@@ -75,7 +75,7 @@ sealed class ImageWriter : BinaryStreamWriter {
this.GetDebugHeader ();
this.GetWin32Resources ();
this.text_map = BuildTextMap ();
- this.sections = 2; // text + reloc
+ this.sections = (ushort) (pe64 ? 1 : 2); // text + reloc
this.time_stamp = (uint) DateTime.UtcNow.Subtract (new DateTime (1970, 1, 1)).TotalSeconds;
}
@@ -133,7 +133,8 @@ void BuildSections ()
previous = rsrc;
}
- reloc = CreateSection (".reloc", 12u, previous);
+ if (!pe64)
+ reloc = CreateSection (".reloc", 12u, previous);
}
Section CreateSection (string name, uint size, Section previous)
@@ -217,20 +218,29 @@ ushort GetMachine ()
throw new NotSupportedException ();
}
+ Section LastSection()
+ {
+ if (reloc != null)
+ return reloc;
+
+ if (rsrc != null)
+ return rsrc;
+
+ return text;
+ }
+
void WriteOptionalHeaders ()
{
WriteUInt16 ((ushort) (!pe64 ? 0x10b : 0x20b)); // Magic
WriteByte (8); // LMajor
WriteByte (0); // LMinor
WriteUInt32 (text.SizeOfRawData); // CodeSize
- WriteUInt32 (reloc.SizeOfRawData
+ WriteUInt32 (reloc != null ? reloc.SizeOfRawData : 0
+ (rsrc != null ? rsrc.SizeOfRawData : 0)); // InitializedDataSize

Aren't you missing some parentheses here? This code uses rsrc.SizeOfRawData only if reloc==null.

@jbevain Owner
jbevain added a note

Yes that looks quite wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
WriteUInt32 (0); // UninitializedDataSize
- var entry_point_rva = text_map.GetRVA (TextSegment.StartupStub);
- if (module.Architecture == TargetArchitecture.IA64)
- entry_point_rva += 0x20;
- WriteUInt32 (entry_point_rva); // EntryPointRVA
+ var startub_stub = text_map.GetRange (TextSegment.StartupStub);
+ WriteUInt32 (startub_stub.Length > 0 ? startub_stub.Start : 0); // EntryPointRVA
WriteUInt32 (text_rva); // BaseOfCode
if (!pe64) {
@@ -251,7 +261,8 @@ void WriteOptionalHeaders ()
WriteUInt16 (0); // SubSysMinor
WriteUInt32 (0); // Reserved
- WriteUInt32 (reloc.VirtualAddress + Align (reloc.VirtualSize, section_alignment)); // ImageSize
+ var last_section = LastSection();
+ WriteUInt32 (last_section.VirtualAddress + Align (last_section.VirtualSize, section_alignment)); // ImageSize
WriteUInt32 (text.PointerToRawData); // HeaderSize
WriteUInt32 (0); // Checksum
@@ -288,8 +299,8 @@ void WriteOptionalHeaders ()
WriteZeroDataDirectory (); // ExceptionTable
WriteZeroDataDirectory (); // CertificateTable
- WriteUInt32 (reloc.VirtualAddress); // BaseRelocationTable
- WriteUInt32 (reloc.VirtualSize);
+ WriteUInt32 (reloc != null ? reloc.VirtualAddress : 0); // BaseRelocationTable
+ WriteUInt32 (reloc != null ? reloc.VirtualSize : 0);
if (text_map.GetLength (TextSegment.DebugDirectory) > 0) {
WriteUInt32 (text_map.GetRVA (TextSegment.DebugDirectory));
@@ -335,7 +346,8 @@ void WriteSectionHeaders ()
if (rsrc != null)
WriteSection (rsrc, 0x40000040);
- WriteSection (reloc, 0x42000040);
+ if (reloc != null)
+ WriteSection (reloc, 0x42000040);
}
void WriteSection (Section section, uint characteristics)
@@ -386,8 +398,10 @@ void WriteText ()
// ImportAddressTable
- WriteRVA (text_map.GetRVA (TextSegment.ImportHintNameTable));
- WriteRVA (0);
+ if (!pe64) {
+ WriteRVA (text_map.GetRVA (TextSegment.ImportHintNameTable));
+ WriteRVA (0);
+ }
// CLIHeader
@@ -439,6 +453,9 @@ void WriteText ()
WriteDebugDirectory ();
}
+ if (pe64)
+ return;
+
// ImportDirectory
MoveToRVA (TextSegment.ImportDirectory);
WriteImportDirectory ();
@@ -605,19 +622,8 @@ void WriteStartupStub ()
WriteUInt16 (0x25ff);
WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.ImportAddressTable));
return;
- case TargetArchitecture.AMD64:
- WriteUInt16 (0xa148);
- WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.ImportAddressTable));
- WriteUInt16 (0xe0ff);
- return;
- case TargetArchitecture.IA64:
- WriteBytes (new byte [] {
- 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
- });
- WriteUInt32 ((uint) image_base + text_map.GetRVA (TextSegment.StartupStub));
- WriteUInt32 ((uint) image_base + text_rva);
- return;
+ default:
+ throw new NotSupportedException();
}
}
@@ -642,13 +648,8 @@ void WriteReloc ()
case TargetArchitecture.I386:
WriteUInt32 (0x3000 + reloc_rva - page_rva);
break;
- case TargetArchitecture.AMD64:
- WriteUInt32 (0xa000 + reloc_rva - page_rva);
- break;
- case TargetArchitecture.IA64:
- WriteUInt16 ((ushort) (0xa000 + reloc_rva - page_rva));
- WriteUInt16 ((ushort) (0xa000 + reloc_rva - page_rva + 8));
- break;
+ default:
+ throw new NotSupportedException();
}
WriteBytes (new byte [file_alignment - reloc.VirtualSize]);
@@ -663,7 +664,8 @@ public void WriteImage ()
WriteText ();
if (rsrc != null)
WriteRsrc ();
- WriteReloc ();
+ if (reloc != null)
+ WriteReloc ();
}
TextMap BuildTextMap ()
@@ -694,8 +696,16 @@ TextMap BuildTextMap ()
map.AddMap (TextSegment.DebugDirectory, debug_dir_len, 4);
+ if (pe64) {
+ var start = map.GetNextRVA (TextSegment.DebugDirectory);
+ map.AddMap (TextSegment.ImportDirectory, new Range (start, 0));
+ map.AddMap (TextSegment.ImportHintNameTable, new Range (start, 0));
+ map.AddMap (TextSegment.StartupStub, new Range (start, 0));
+ return map;
+ }
+
RVA import_dir_rva = map.GetNextRVA (TextSegment.DebugDirectory);
- RVA import_hnt_rva = import_dir_rva + (!pe64 ? 48u : 52u);
+ RVA import_hnt_rva = import_dir_rva + 48u;
import_hnt_rva = (import_hnt_rva + 15u) & ~15u;
uint import_dir_len = (import_hnt_rva - import_dir_rva) + 27u;
@@ -716,12 +726,8 @@ uint GetStartupStubLength ()
switch (module.Architecture) {
case TargetArchitecture.I386:
return 6;
- case TargetArchitecture.AMD64:
- return 12;
- case TargetArchitecture.IA64:
- return 48;
default:
- throw new InvalidOperationException ();
+ throw new NotSupportedException();
}
}
View
2  Mono.Cecil/AssemblyWriter.cs
@@ -786,7 +786,7 @@ public MetadataBuilder (ModuleDefinition module, string fq_name, ISymbolWriterPr
TextMap CreateTextMap ()
{
var map = new TextMap ();
- map.AddMap (TextSegment.ImportAddressTable, module.Architecture == TargetArchitecture.I386 ? 8 : 16);
+ map.AddMap (TextSegment.ImportAddressTable, module.Architecture == TargetArchitecture.I386 ? 8 : 0);
map.AddMap (TextSegment.CLIHeader, 0x48, 8);
return map;
}
Please sign in to comment.
Something went wrong with that request. Please try again.