5
// Jb Evain (jbevain@gmail.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
29
namespace Mono.Cecil.Binary {
33
using Mono.Cecil.Metadata;
35
sealed class ImageInitializer : BaseImageVisitor {
38
MetadataInitializer m_mdinit;
41
get { return m_image; }
44
public MetadataInitializer Metadata {
45
get { return m_mdinit; }
48
public ImageInitializer (Image image)
51
m_image.CLIHeader = new CLIHeader ();
52
m_mdinit = new MetadataInitializer (this);
55
public override void VisitDOSHeader (DOSHeader header)
57
header.SetDefaultValues ();
60
public override void VisitPEOptionalHeader (PEOptionalHeader header)
62
header.SetDefaultValues ();
65
public override void VisitPEFileHeader (PEFileHeader header)
67
header.SetDefaultValues ();
68
header.TimeDateStamp = TimeDateStampFromEpoch ();
71
public override void VisitNTSpecificFieldsHeader (PEOptionalHeader.NTSpecificFieldsHeader header)
73
header.SetDefaultValues ();
76
public override void VisitStandardFieldsHeader (PEOptionalHeader.StandardFieldsHeader header)
78
header.SetDefaultValues ();
81
public override void VisitDataDirectoriesHeader (PEOptionalHeader.DataDirectoriesHeader header)
83
header.SetDefaultValues ();
86
public override void VisitSectionCollection (SectionCollection coll)
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;
94
Section reloc = new Section ();
95
reloc.Name = Section.Relocs;
96
reloc.Characteristics = SectionCharacteristics.ContainsInitializedData |
97
SectionCharacteristics.MemDiscardable | SectionCharacteristics.MemoryRead;
103
public override void VisitSection (Section sect)
105
sect.SetDefaultValues ();
108
public override void VisitDebugHeader (DebugHeader dh)
111
dh.SetDefaultValues ();
114
public override void VisitCLIHeader (CLIHeader header)
116
header.SetDefaultValues ();
117
m_image.MetadataRoot.Accept (m_mdinit);
120
public override void VisitImportTable (ImportTable it)
122
it.ImportAddressTable = new RVA (0x2000);
125
public override void VisitHintNameTable (HintNameTable hnt)
128
hnt.RuntimeLibrary = HintNameTable.RuntimeCorEE;
129
hnt.EntryPoint = 0x25ff;
130
hnt.RVA = new RVA (0x402000);
133
public static uint TimeDateStampFromEpoch ()
135
return (uint) DateTime.UtcNow.Subtract (
136
new DateTime (1970, 1, 1)).TotalSeconds;