| 1 |
// |
| 2 |
// ImageInitializer.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// Jb Evain ([email protected]) |
| 6 |
// |
| 7 |
// (C) 2005 Jb Evain |
| 8 |
// |
| 9 |
// Permission is hereby granted, free of charge, to any person obtaining |
| 10 |
// a copy of this software and associated documentation files (the |
| 11 |
// "Software"), to deal in the Software without restriction, including |
| 12 |
// without limitation the rights to use, copy, modify, merge, publish, |
| 13 |
// distribute, sublicense, and/or sell copies of the Software, and to |
| 14 |
// permit persons to whom the Software is furnished to do so, subject to |
| 15 |
// the following conditions: |
| 16 |
// |
| 17 |
// The above copyright notice and this permission notice shall be |
| 18 |
// included in all copies or substantial portions of the Software. |
| 19 |
// |
| 20 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 24 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 25 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 26 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 27 |
// |
| 28 |
|
| 29 |
namespace Mono.Cecil.Binary { |
| 30 |
|
| 31 |
using System; |
| 32 |
|
| 33 |
using Mono.Cecil.Metadata; |
| 34 |
|
| 35 |
sealed class ImageInitializer : BaseImageVisitor { |
| 36 |
|
| 37 |
Image m_image; |
| 38 |
MetadataInitializer m_mdinit; |
| 39 |
|
| 40 |
public Image Image { |
| 41 |
get { return m_image; } |
| 42 |
} |
| 43 |
|
| 44 |
public MetadataInitializer Metadata { |
| 45 |
get { return m_mdinit; } |
| 46 |
} |
| 47 |
|
| 48 |
public ImageInitializer (Image image) |
| 49 |
{ |
| 50 |
m_image = image; |
| 51 |
m_image.CLIHeader = new CLIHeader (); |
| 52 |
m_mdinit = new MetadataInitializer (this); |
| 53 |
} |
| 54 |
|
| 55 |
public override void VisitDOSHeader (DOSHeader header) |
| 56 |
{ |
| 57 |
header.SetDefaultValues (); |
| 58 |
} |
| 59 |
|
| 60 |
public override void VisitPEOptionalHeader (PEOptionalHeader header) |
| 61 |
{ |
| 62 |
header.SetDefaultValues (); |
| 63 |
} |
| 64 |
|
| 65 |
public override void VisitPEFileHeader (PEFileHeader header) |
| 66 |
{ |
| 67 |
header.SetDefaultValues (); |
| 68 |
header.TimeDateStamp = TimeDateStampFromEpoch (); |
| 69 |
} |
| 70 |
|
| 71 |
public override void VisitNTSpecificFieldsHeader (PEOptionalHeader.NTSpecificFieldsHeader header) |
| 72 |
{ |
| 73 |
header.SetDefaultValues (); |
| 74 |
} |
| 75 |
|
| 76 |
public override void VisitStandardFieldsHeader (PEOptionalHeader.StandardFieldsHeader header) |
| 77 |
{ |
| 78 |
header.SetDefaultValues (); |
| 79 |
} |
| 80 |
|
| 81 |
public override void VisitDataDirectoriesHeader (PEOptionalHeader.DataDirectoriesHeader header) |
| 82 |
{ |
| 83 |
header.SetDefaultValues (); |
| 84 |
} |
| 85 |
|
| 86 |
public override void VisitSectionCollection (SectionCollection coll) |
| 87 |
{ |
| 88 |
Section text = new Section (); |
| 89 |
text.Name = Section.Text; |
| 90 |
text.Characteristics = SectionCharacteristics.ContainsCode | |
| 91 |
SectionCharacteristics.MemoryRead | SectionCharacteristics.MemExecute; |
| 92 |
m_image.TextSection = text; |
| 93 |
|
| 94 |
Section reloc = new Section (); |
| 95 |
reloc.Name = Section.Relocs; |
| 96 |
reloc.Characteristics = SectionCharacteristics.ContainsInitializedData | |
| 97 |
SectionCharacteristics.MemDiscardable | SectionCharacteristics.MemoryRead; |
| 98 |
|
| 99 |
coll.Add (text); |
| 100 |
coll.Add (reloc); |
| 101 |
} |
| 102 |
|
| 103 |
public override void VisitSection (Section sect) |
| 104 |
{ |
| 105 |
sect.SetDefaultValues (); |
| 106 |
} |
| 107 |
|
| 108 |
public override void VisitDebugHeader (DebugHeader dh) |
| 109 |
{ |
| 110 |
if (dh != null) |
| 111 |
dh.SetDefaultValues (); |
| 112 |
} |
| 113 |
|
| 114 |
public override void VisitCLIHeader (CLIHeader header) |
| 115 |
{ |
| 116 |
header.SetDefaultValues (); |
| 117 |
m_image.MetadataRoot.Accept (m_mdinit); |
| 118 |
} |
| 119 |
|
| 120 |
public override void VisitImportTable (ImportTable it) |
| 121 |
{ |
| 122 |
it.ImportAddressTable = new RVA (0x2000); |
| 123 |
} |
| 124 |
|
| 125 |
public override void VisitHintNameTable (HintNameTable hnt) |
| 126 |
{ |
| 127 |
hnt.Hint = 0; |
| 128 |
hnt.RuntimeLibrary = HintNameTable.RuntimeCorEE; |
| 129 |
hnt.EntryPoint = 0x25ff; |
| 130 |
hnt.RVA = new RVA (0x402000); |
| 131 |
} |
| 132 |
|
| 133 |
public static uint TimeDateStampFromEpoch () |
| 134 |
{ |
| 135 |
return (uint) DateTime.UtcNow.Subtract ( |
| 136 |
new DateTime (1970, 1, 1)).TotalSeconds; |
| 137 |
} |
| 138 |
} |
| 139 |
} |