-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathNativeModuleWriter.cs
844 lines (737 loc) · 30.8 KB
/
NativeModuleWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
// dnlib: See LICENSE.txt for more info
using System;
using System.Collections.Generic;
using dnlib.IO;
using dnlib.PE;
using dnlib.W32Resources;
using dnlib.DotNet.MD;
using System.Diagnostics;
namespace dnlib.DotNet.Writer {
/// <summary>
/// <see cref="NativeModuleWriter"/> options
/// </summary>
public sealed class NativeModuleWriterOptions : ModuleWriterOptionsBase {
/// <summary>
/// If <c>true</c>, any extra data after the PE data in the original file is also saved
/// at the end of the new file. Enable this option if some protector has written data to
/// the end of the file and uses it at runtime.
/// </summary>
public bool KeepExtraPEData { get; set; }
/// <summary>
/// If <c>true</c>, keep the original Win32 resources
/// </summary>
public bool KeepWin32Resources { get; set; }
internal bool OptimizeImageSize { get; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="module">Module</param>
/// <param name="optimizeImageSize">true to optimize the image size so it's as small as possible.
/// Since the file can contain native methods and other native data, we re-use the
/// original file when writing the new file. If <paramref name="optimizeImageSize"/> is true,
/// we'll try to re-use the old method body locations in the original file and
/// also try to fit the new metadata in the old metadata location.</param>
public NativeModuleWriterOptions(ModuleDefMD module, bool optimizeImageSize) : base(module) {
// C++ .NET mixed mode assemblies sometimes/often call Module.ResolveMethod(),
// so method metadata tokens must be preserved.
MetadataOptions.Flags |= MetadataFlags.PreserveAllMethodRids;
if (optimizeImageSize) {
OptimizeImageSize = true;
// Prevent the #Blob heap from getting bigger. Encoded TypeDefOrRef tokens are stored there (in
// typesigs and callingconvsigs) so we must preserve TypeDefOrRef tokens (or the #Blob heap could
// grow in size and new MD won't fit in old location)
MetadataOptions.Flags |= MetadataFlags.PreserveTypeRefRids | MetadataFlags.PreserveTypeDefRids | MetadataFlags.PreserveTypeSpecRids;
}
}
}
/// <summary>
/// A module writer that supports saving mixed-mode modules (modules with native code).
/// The original image will be re-used. See also <see cref="ModuleWriter"/>
/// </summary>
public sealed class NativeModuleWriter : ModuleWriterBase {
/// <summary>The original .NET module</summary>
readonly ModuleDefMD module;
/// <summary>All options</summary>
NativeModuleWriterOptions options;
/// <summary>
/// Any extra data found at the end of the original file. This is <c>null</c> if there's
/// no extra data or if <see cref="NativeModuleWriterOptions.KeepExtraPEData"/> is
/// <c>false</c>.
/// </summary>
DataReaderChunk extraData;
/// <summary>The original PE sections and their data</summary>
List<OrigSection> origSections;
readonly struct ReusedChunkInfo {
public IReuseChunk Chunk { get; }
public RVA RVA { get; }
public ReusedChunkInfo(IReuseChunk chunk, RVA rva) {
Chunk = chunk;
RVA = rva;
}
}
List<ReusedChunkInfo> reusedChunks;
/// <summary>Original PE image</summary>
readonly IPEImage peImage;
/// <summary>New sections we've added and their data</summary>
List<PESection> sections;
/// <summary>New .text section where we put some stuff, eg. .NET metadata</summary>
PESection textSection;
/// <summary>The new COR20 header</summary>
ByteArrayChunk imageCor20Header;
/// <summary>
/// New .rsrc section where we put the new Win32 resources. This is <c>null</c> if there
/// are no Win32 resources or if <see cref="NativeModuleWriterOptions.KeepWin32Resources"/>
/// is <c>true</c>
/// </summary>
PESection rsrcSection;
/// <summary>
/// Offset in <see cref="ModuleWriterBase.destStream"/> of the PE checksum field.
/// </summary>
long checkSumOffset;
/// <summary>
/// Original PE section
/// </summary>
public sealed class OrigSection : IDisposable {
/// <summary>PE section</summary>
public ImageSectionHeader PESection;
/// <summary>PE section data</summary>
public DataReaderChunk Chunk;
/// <summary>
/// Constructor
/// </summary>
/// <param name="peSection">PE section</param>
public OrigSection(ImageSectionHeader peSection) =>
PESection = peSection;
/// <inheritdoc/>
public void Dispose() {
Chunk = null;
PESection = null;
}
/// <inheritdoc/>
public override string ToString() {
uint offs = Chunk.CreateReader().StartOffset;
return $"{PESection.DisplayName} FO:{offs:X8} L:{Chunk.CreateReader().Length:X8}";
}
}
/// <summary>
/// Gets the module
/// </summary>
public ModuleDefMD ModuleDefMD => module;
/// <inheritdoc/>
public override ModuleDef Module => module;
/// <inheritdoc/>
public override ModuleWriterOptionsBase TheOptions => Options;
/// <summary>
/// Gets/sets the writer options. This is never <c>null</c>
/// </summary>
public NativeModuleWriterOptions Options {
get => options ??= new NativeModuleWriterOptions(module, optimizeImageSize: true);
set => options = value;
}
/// <summary>
/// Gets all <see cref="PESection"/>s
/// </summary>
public override List<PESection> Sections => sections;
/// <summary>
/// Gets the original PE sections and their data
/// </summary>
public List<OrigSection> OrigSections => origSections;
/// <summary>
/// Gets the <c>.text</c> section
/// </summary>
public override PESection TextSection => textSection;
/// <summary>
/// Gets the <c>.rsrc</c> section or <c>null</c> if there's none
/// </summary>
public override PESection RsrcSection => rsrcSection;
/// <summary>
/// Constructor
/// </summary>
/// <param name="module">The module</param>
/// <param name="options">Options or <c>null</c></param>
public NativeModuleWriter(ModuleDefMD module, NativeModuleWriterOptions options) {
this.module = module;
this.options = options;
peImage = module.Metadata.PEImage;
reusedChunks = new List<ReusedChunkInfo>();
}
/// <inheritdoc/>
protected override long WriteImpl() {
try {
return Write();
}
finally {
if (origSections is not null) {
foreach (var section in origSections)
section.Dispose();
}
}
}
long Write() {
Initialize();
// It's not safe to create new Field RVAs so re-use them all. The user can override
// this by setting field.RVA = 0 when creating a new field.InitialValue.
metadata.KeepFieldRVA = true;
metadata.CreateTables();
return WriteFile();
}
void Initialize() {
CreateSections();
OnWriterEvent(ModuleWriterEvent.PESectionsCreated);
CreateChunks();
OnWriterEvent(ModuleWriterEvent.ChunksCreated);
AddChunksToSections();
OnWriterEvent(ModuleWriterEvent.ChunksAddedToSections);
}
void CreateSections() {
CreatePESections();
CreateRawSections();
CreateExtraData();
}
void CreateChunks() {
CreateMetadataChunks(module);
methodBodies.CanReuseOldBodyLocation = Options.OptimizeImageSize;
CreateDebugDirectory();
imageCor20Header = new ByteArrayChunk(new byte[0x48]);
CreateStrongNameSignature();
}
void AddChunksToSections() {
textSection.Add(imageCor20Header, DEFAULT_COR20HEADER_ALIGNMENT);
textSection.Add(strongNameSignature, DEFAULT_STRONGNAMESIG_ALIGNMENT);
textSection.Add(constants, DEFAULT_CONSTANTS_ALIGNMENT);
textSection.Add(methodBodies, DEFAULT_METHODBODIES_ALIGNMENT);
textSection.Add(netResources, DEFAULT_NETRESOURCES_ALIGNMENT);
textSection.Add(metadata, DEFAULT_METADATA_ALIGNMENT);
textSection.Add(debugDirectory, DebugDirectory.DEFAULT_DEBUGDIRECTORY_ALIGNMENT);
if (rsrcSection is not null)
rsrcSection.Add(win32Resources, DEFAULT_WIN32_RESOURCES_ALIGNMENT);
}
/// <inheritdoc/>
protected override Win32Resources GetWin32Resources() {
if (Options.KeepWin32Resources)
return null;
if (Options.NoWin32Resources)
return null;
return Options.Win32Resources ?? module.Win32Resources;
}
void CreatePESections() {
sections = new List<PESection>();
sections.Add(textSection = new PESection(".text", 0x60000020));
if (GetWin32Resources() is not null)
sections.Add(rsrcSection = new PESection(".rsrc", 0x40000040));
}
/// <summary>
/// Gets the raw section data of the image. The sections are saved in
/// <see cref="origSections"/>.
/// </summary>
void CreateRawSections() {
var fileAlignment = peImage.ImageNTHeaders.OptionalHeader.FileAlignment;
origSections = new List<OrigSection>(peImage.ImageSectionHeaders.Count);
foreach (var peSection in peImage.ImageSectionHeaders) {
var newSection = new OrigSection(peSection);
origSections.Add(newSection);
uint sectionSize = Utils.AlignUp(peSection.SizeOfRawData, fileAlignment);
newSection.Chunk = new DataReaderChunk(peImage.CreateReader(peSection.VirtualAddress, sectionSize), peSection.VirtualSize);
}
}
/// <summary>
/// Creates the PE header "section"
/// </summary>
DataReaderChunk CreateHeaderSection(out IChunk extraHeaderData) {
uint afterLastSectHeader = GetOffsetAfterLastSectionHeader() + (uint)sections.Count * 0x28;
uint firstRawOffset = Math.Min(GetFirstRawDataFileOffset(), peImage.ImageNTHeaders.OptionalHeader.SectionAlignment);
uint headerLen = afterLastSectHeader;
if (firstRawOffset > headerLen)
headerLen = firstRawOffset;
headerLen = Utils.AlignUp(headerLen, peImage.ImageNTHeaders.OptionalHeader.FileAlignment);
if (headerLen <= peImage.ImageNTHeaders.OptionalHeader.SectionAlignment) {
uint origSizeOfHeaders = peImage.ImageNTHeaders.OptionalHeader.SizeOfHeaders;
uint extraLen;
if (headerLen <= origSizeOfHeaders)
extraLen = 0;
else {
extraLen = headerLen - origSizeOfHeaders;
headerLen = origSizeOfHeaders;
}
if (extraLen > 0)
extraHeaderData = new ByteArrayChunk(new byte[(int)extraLen]);
else
extraHeaderData = null;
return new DataReaderChunk(peImage.CreateReader((FileOffset)0, headerLen));
}
//TODO: Support this too
throw new ModuleWriterException("Could not create header");
}
uint GetOffsetAfterLastSectionHeader() {
var lastSect = peImage.ImageSectionHeaders[peImage.ImageSectionHeaders.Count - 1];
return (uint)lastSect.EndOffset;
}
uint GetFirstRawDataFileOffset() {
uint len = uint.MaxValue;
foreach (var section in peImage.ImageSectionHeaders)
len = Math.Min(len, section.PointerToRawData);
return len;
}
/// <summary>
/// Saves any data that is appended to the original PE file
/// </summary>
void CreateExtraData() {
if (!Options.KeepExtraPEData)
return;
var lastOffs = GetLastFileSectionOffset();
extraData = new DataReaderChunk(peImage.CreateReader((FileOffset)lastOffs));
if (extraData.CreateReader().Length == 0)
extraData = null;
}
uint GetLastFileSectionOffset() {
uint rva = 0;
foreach (var sect in origSections)
rva = Math.Max(rva, (uint)sect.PESection.VirtualAddress + sect.PESection.SizeOfRawData);
return (uint)peImage.ToFileOffset((RVA)(rva - 1)) + 1;
}
void ReuseIfPossible(PESection section, IReuseChunk chunk, RVA origRva, uint origSize, uint requiredAlignment) {
if (origRva == 0 || origSize == 0)
return;
if (chunk is null)
return;
if (!chunk.CanReuse(origRva, origSize))
return;
if (((uint)origRva & (requiredAlignment - 1)) != 0)
return;
if (section.Remove(chunk) is null)
throw new InvalidOperationException();
reusedChunks.Add(new ReusedChunkInfo(chunk, origRva));
}
FileOffset GetNewFileOffset(RVA rva) {
foreach (var sect in origSections) {
var section = sect.PESection;
if (section.VirtualAddress <= rva && rva < section.VirtualAddress + Math.Max(section.VirtualSize, section.SizeOfRawData))
return sect.Chunk.FileOffset + (rva - section.VirtualAddress);
}
return (FileOffset)rva;
}
long WriteFile() {
bool entryPointIsManagedOrNoEntryPoint = GetEntryPoint(out uint entryPointToken);
OnWriterEvent(ModuleWriterEvent.BeginWritePdb);
WritePdbFile();
OnWriterEvent(ModuleWriterEvent.EndWritePdb);
metadata.OnBeforeSetOffset();
OnWriterEvent(ModuleWriterEvent.BeginCalculateRvasAndFileOffsets);
if (Options.OptimizeImageSize) {
// Check if we can reuse the old MD location for the new MD.
// If we can't reuse it, it could be due to several reasons:
// - TypeDefOrRef tokens weren't preserved resulting in a new #Blob heap that's bigger than the old #Blob heap
// - New MD was added or existing MD was modified (eg. types were renamed) by the user so it's
// now bigger and doesn't fit in the old location
// - The original location wasn't aligned properly
// - The new MD is bigger because the other MD writer was slightly better at optimizing the MD.
// This should be considered a bug.
var mdDataDir = module.Metadata.ImageCor20Header.Metadata;
metadata.SetOffset(peImage.ToFileOffset(mdDataDir.VirtualAddress), mdDataDir.VirtualAddress);
ReuseIfPossible(textSection, metadata, mdDataDir.VirtualAddress, mdDataDir.Size, DEFAULT_METADATA_ALIGNMENT);
var resourceDataDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[2];
if (win32Resources is not null && resourceDataDir.VirtualAddress != 0 && resourceDataDir.Size != 0) {
var win32ResourcesOffset = peImage.ToFileOffset(resourceDataDir.VirtualAddress);
if (win32Resources.CheckValidOffset(win32ResourcesOffset)) {
win32Resources.SetOffset(win32ResourcesOffset, resourceDataDir.VirtualAddress);
ReuseIfPossible(rsrcSection, win32Resources, resourceDataDir.VirtualAddress, resourceDataDir.Size, DEFAULT_WIN32_RESOURCES_ALIGNMENT);
}
}
ReuseIfPossible(textSection, imageCor20Header, module.Metadata.PEImage.ImageNTHeaders.OptionalHeader.DataDirectories[14].VirtualAddress, module.Metadata.PEImage.ImageNTHeaders.OptionalHeader.DataDirectories[14].Size, DEFAULT_COR20HEADER_ALIGNMENT);
if ((module.Metadata.ImageCor20Header.Flags & ComImageFlags.StrongNameSigned) != 0)
ReuseIfPossible(textSection, strongNameSignature, module.Metadata.ImageCor20Header.StrongNameSignature.VirtualAddress, module.Metadata.ImageCor20Header.StrongNameSignature.Size, DEFAULT_STRONGNAMESIG_ALIGNMENT);
ReuseIfPossible(textSection, netResources, module.Metadata.ImageCor20Header.Resources.VirtualAddress, module.Metadata.ImageCor20Header.Resources.Size, DEFAULT_NETRESOURCES_ALIGNMENT);
if (methodBodies.ReusedAllMethodBodyLocations)
textSection.Remove(methodBodies);
var debugDataDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[6];
if (debugDataDir.VirtualAddress != 0 && debugDataDir.Size != 0 && TryGetRealDebugDirectorySize(peImage, out uint realDebugDirSize))
ReuseIfPossible(textSection, debugDirectory, debugDataDir.VirtualAddress, realDebugDirSize, DebugDirectory.DEFAULT_DEBUGDIRECTORY_ALIGNMENT);
}
if (constants.IsEmpty)
textSection.Remove(constants);
if (netResources.IsEmpty)
textSection.Remove(netResources);
if (textSection.IsEmpty)
sections.Remove(textSection);
if (rsrcSection is not null && rsrcSection.IsEmpty) {
sections.Remove(rsrcSection);
rsrcSection = null;
}
var headerSection = CreateHeaderSection(out var extraHeaderData);
var chunks = new List<IChunk>();
uint headerLen;
if (extraHeaderData is not null) {
var list = new ChunkList<IChunk>();
list.Add(headerSection, 1);
list.Add(extraHeaderData, 1);
chunks.Add(list);
headerLen = headerSection.GetVirtualSize() + extraHeaderData.GetVirtualSize();
}
else {
chunks.Add(headerSection);
headerLen = headerSection.GetVirtualSize();
}
foreach (var origSection in origSections)
chunks.Add(origSection.Chunk);
foreach (var section in sections)
chunks.Add(section);
if (extraData is not null)
chunks.Add(extraData);
CalculateRvasAndFileOffsets(chunks, 0, 0, peImage.ImageNTHeaders.OptionalHeader.FileAlignment, peImage.ImageNTHeaders.OptionalHeader.SectionAlignment);
if (reusedChunks.Count > 0 || methodBodies.HasReusedMethods) {
methodBodies.InitializeReusedMethodBodies(GetNewFileOffset);
foreach (var info in reusedChunks) {
var offset = GetNewFileOffset(info.RVA);
info.Chunk.SetOffset(offset, info.RVA);
}
}
metadata.UpdateMethodAndFieldRvas();
foreach (var section in origSections) {
if (section.Chunk.RVA != section.PESection.VirtualAddress)
throw new ModuleWriterException("Invalid section RVA");
}
OnWriterEvent(ModuleWriterEvent.EndCalculateRvasAndFileOffsets);
OnWriterEvent(ModuleWriterEvent.BeginWriteChunks);
var writer = new DataWriter(destStream);
WriteChunks(writer, chunks, 0, peImage.ImageNTHeaders.OptionalHeader.FileAlignment);
long imageLength = writer.Position - destStreamBaseOffset;
if (reusedChunks.Count > 0 || methodBodies.HasReusedMethods) {
var pos = writer.Position;
foreach (var info in reusedChunks) {
Debug.Assert(info.Chunk.RVA == info.RVA);
if (info.Chunk.RVA != info.RVA)
throw new InvalidOperationException();
writer.Position = destStreamBaseOffset + (uint)info.Chunk.FileOffset;
info.Chunk.VerifyWriteTo(writer);
}
methodBodies.WriteReusedMethodBodies(writer, destStreamBaseOffset);
writer.Position = pos;
}
var sectionSizes = new SectionSizes(peImage.ImageNTHeaders.OptionalHeader.FileAlignment, peImage.ImageNTHeaders.OptionalHeader.SectionAlignment, headerLen, GetSectionSizeInfos);
UpdateHeaderFields(writer, entryPointIsManagedOrNoEntryPoint, entryPointToken, ref sectionSizes);
OnWriterEvent(ModuleWriterEvent.EndWriteChunks);
OnWriterEvent(ModuleWriterEvent.BeginStrongNameSign);
if (Options.StrongNameKey is not null)
StrongNameSign((long)strongNameSignature.FileOffset);
OnWriterEvent(ModuleWriterEvent.EndStrongNameSign);
OnWriterEvent(ModuleWriterEvent.BeginWritePEChecksum);
if (Options.AddCheckSum) {
destStream.Position = destStreamBaseOffset;
uint newCheckSum = destStream.CalculatePECheckSum(imageLength, checkSumOffset);
writer.Position = checkSumOffset;
writer.WriteUInt32(newCheckSum);
}
OnWriterEvent(ModuleWriterEvent.EndWritePEChecksum);
return imageLength;
}
static bool TryGetRealDebugDirectorySize(IPEImage peImage, out uint realSize) {
realSize = 0;
if (peImage.ImageDebugDirectories.Count == 0)
return false;
var dirs = new List<ImageDebugDirectory>(peImage.ImageDebugDirectories);
dirs.Sort((a, b) => a.AddressOfRawData.CompareTo(b.AddressOfRawData));
var debugDataDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[6];
var prevEnd = (uint)debugDataDir.VirtualAddress + debugDataDir.Size;
for (int i = 0; i < dirs.Count; i++) {
uint prevEndAligned = (prevEnd + 3) & ~3U;
var dir = dirs[i];
if (dir.AddressOfRawData == 0 || dir.SizeOfData == 0)
continue;
if (!(prevEnd <= (uint)dir.AddressOfRawData && (uint)dir.AddressOfRawData <= prevEndAligned))
return false;
prevEnd = (uint)dir.AddressOfRawData + dir.SizeOfData;
}
realSize = prevEnd - (uint)debugDataDir.VirtualAddress;
return true;
}
/// <summary>
/// <c>true</c> if image is 64-bit
/// </summary>
bool Is64Bit() => peImage.ImageNTHeaders.OptionalHeader is ImageOptionalHeader64;
Characteristics GetCharacteristics() {
var ch = module.Characteristics;
if (Is64Bit())
ch &= ~Characteristics.Bit32Machine;
else
ch |= Characteristics.Bit32Machine;
if (Options.IsExeFile)
ch &= ~Characteristics.Dll;
else
ch |= Characteristics.Dll;
return ch;
}
/// <summary>
/// Updates the PE header and COR20 header fields that need updating. All sections are
/// also updated, and the new ones are added.
/// </summary>
void UpdateHeaderFields(DataWriter writer, bool entryPointIsManagedOrNoEntryPoint, uint entryPointToken, ref SectionSizes sectionSizes) {
long fileHeaderOffset = destStreamBaseOffset + (long)peImage.ImageNTHeaders.FileHeader.StartOffset;
long optionalHeaderOffset = destStreamBaseOffset + (long)peImage.ImageNTHeaders.OptionalHeader.StartOffset;
long sectionsOffset = destStreamBaseOffset + (long)peImage.ImageSectionHeaders[0].StartOffset;
long dataDirOffset = destStreamBaseOffset + (long)peImage.ImageNTHeaders.OptionalHeader.EndOffset - 16 * 8;
long cor20Offset = destStreamBaseOffset + (long)imageCor20Header.FileOffset;
// Update PE file header
var peOptions = Options.PEHeadersOptions;
writer.Position = fileHeaderOffset;
writer.WriteUInt16((ushort)(peOptions.Machine ?? module.Machine));
writer.WriteUInt16((ushort)(origSections.Count + sections.Count));
WriteUInt32(writer, peOptions.TimeDateStamp);
WriteUInt32(writer, peOptions.PointerToSymbolTable);
WriteUInt32(writer, peOptions.NumberOfSymbols);
writer.Position += 2; // sizeof(SizeOfOptionalHeader)
writer.WriteUInt16((ushort)(peOptions.Characteristics ?? GetCharacteristics()));
// Update optional header
writer.Position = optionalHeaderOffset;
bool is32BitOptionalHeader = peImage.ImageNTHeaders.OptionalHeader is ImageOptionalHeader32;
if (is32BitOptionalHeader) {
writer.Position += 2;
WriteByte(writer, peOptions.MajorLinkerVersion);
WriteByte(writer, peOptions.MinorLinkerVersion);
writer.WriteUInt32(sectionSizes.SizeOfCode);
writer.WriteUInt32(sectionSizes.SizeOfInitdData);
writer.WriteUInt32(sectionSizes.SizeOfUninitdData);
writer.Position += 4; // EntryPoint
writer.WriteUInt32(sectionSizes.BaseOfCode);
writer.WriteUInt32(sectionSizes.BaseOfData);
WriteUInt32(writer, peOptions.ImageBase);
writer.Position += 8; // SectionAlignment, FileAlignment
WriteUInt16(writer, peOptions.MajorOperatingSystemVersion);
WriteUInt16(writer, peOptions.MinorOperatingSystemVersion);
WriteUInt16(writer, peOptions.MajorImageVersion);
WriteUInt16(writer, peOptions.MinorImageVersion);
WriteUInt16(writer, peOptions.MajorSubsystemVersion);
WriteUInt16(writer, peOptions.MinorSubsystemVersion);
WriteUInt32(writer, peOptions.Win32VersionValue);
writer.WriteUInt32(sectionSizes.SizeOfImage);
writer.WriteUInt32(sectionSizes.SizeOfHeaders);
checkSumOffset = writer.Position;
writer.WriteInt32(0); // CheckSum
WriteUInt16(writer, peOptions.Subsystem);
WriteUInt16(writer, peOptions.DllCharacteristics);
WriteUInt32(writer, peOptions.SizeOfStackReserve);
WriteUInt32(writer, peOptions.SizeOfStackCommit);
WriteUInt32(writer, peOptions.SizeOfHeapReserve);
WriteUInt32(writer, peOptions.SizeOfHeapCommit);
WriteUInt32(writer, peOptions.LoaderFlags);
WriteUInt32(writer, peOptions.NumberOfRvaAndSizes);
}
else {
writer.Position += 2;
WriteByte(writer, peOptions.MajorLinkerVersion);
WriteByte(writer, peOptions.MinorLinkerVersion);
writer.WriteUInt32(sectionSizes.SizeOfCode);
writer.WriteUInt32(sectionSizes.SizeOfInitdData);
writer.WriteUInt32(sectionSizes.SizeOfUninitdData);
writer.Position += 4; // EntryPoint
writer.WriteUInt32(sectionSizes.BaseOfCode);
WriteUInt64(writer, peOptions.ImageBase);
writer.Position += 8; // SectionAlignment, FileAlignment
WriteUInt16(writer, peOptions.MajorOperatingSystemVersion);
WriteUInt16(writer, peOptions.MinorOperatingSystemVersion);
WriteUInt16(writer, peOptions.MajorImageVersion);
WriteUInt16(writer, peOptions.MinorImageVersion);
WriteUInt16(writer, peOptions.MajorSubsystemVersion);
WriteUInt16(writer, peOptions.MinorSubsystemVersion);
WriteUInt32(writer, peOptions.Win32VersionValue);
writer.WriteUInt32(sectionSizes.SizeOfImage);
writer.WriteUInt32(sectionSizes.SizeOfHeaders);
checkSumOffset = writer.Position;
writer.WriteInt32(0); // CheckSum
WriteUInt16(writer, peOptions.Subsystem ?? GetSubsystem());
WriteUInt16(writer, peOptions.DllCharacteristics ?? module.DllCharacteristics);
WriteUInt64(writer, peOptions.SizeOfStackReserve);
WriteUInt64(writer, peOptions.SizeOfStackCommit);
WriteUInt64(writer, peOptions.SizeOfHeapReserve);
WriteUInt64(writer, peOptions.SizeOfHeapCommit);
WriteUInt32(writer, peOptions.LoaderFlags);
WriteUInt32(writer, peOptions.NumberOfRvaAndSizes);
}
// Update Win32 resources data directory, if we wrote a new one
if (win32Resources is not null) {
writer.Position = dataDirOffset + 2 * 8;
writer.WriteDataDirectory(win32Resources);
}
// Clear the security descriptor directory
writer.Position = dataDirOffset + 4 * 8;
writer.WriteDataDirectory(null);
// Write a new debug directory
writer.Position = dataDirOffset + 6 * 8;
writer.WriteDebugDirectory(debugDirectory);
// Write a new Metadata data directory
writer.Position = dataDirOffset + 14 * 8;
writer.WriteDataDirectory(imageCor20Header);
// Update old sections, and add new sections
writer.Position = sectionsOffset;
foreach (var section in origSections) {
writer.Position += 0x14;
writer.WriteUInt32((uint)section.Chunk.FileOffset); // PointerToRawData
writer.Position += 0x10;
}
foreach (var section in sections)
section.WriteHeaderTo(writer, peImage.ImageNTHeaders.OptionalHeader.FileAlignment, peImage.ImageNTHeaders.OptionalHeader.SectionAlignment, (uint)section.RVA);
// Write the .NET header
writer.Position = cor20Offset;
writer.WriteInt32(0x48); // cb
WriteUInt16(writer, Options.Cor20HeaderOptions.MajorRuntimeVersion);
WriteUInt16(writer, Options.Cor20HeaderOptions.MinorRuntimeVersion);
writer.WriteDataDirectory(metadata);
writer.WriteUInt32((uint)GetComImageFlags(entryPointIsManagedOrNoEntryPoint));
writer.WriteUInt32(entryPointToken);
writer.WriteDataDirectory(netResources);
writer.WriteDataDirectory(strongNameSignature);
WriteDataDirectory(writer, module.Metadata.ImageCor20Header.CodeManagerTable);
WriteDataDirectory(writer, module.Metadata.ImageCor20Header.VTableFixups);
WriteDataDirectory(writer, module.Metadata.ImageCor20Header.ExportAddressTableJumps);
WriteDataDirectory(writer, module.Metadata.ImageCor20Header.ManagedNativeHeader);
UpdateVTableFixups(writer);
}
static void WriteDataDirectory(DataWriter writer, ImageDataDirectory dataDir) {
writer.WriteUInt32((uint)dataDir.VirtualAddress);
writer.WriteUInt32(dataDir.Size);
}
static void WriteByte(DataWriter writer, byte? value) {
if (value is null)
writer.Position++;
else
writer.WriteByte(value.Value);
}
static void WriteUInt16(DataWriter writer, ushort? value) {
if (value is null)
writer.Position += 2;
else
writer.WriteUInt16(value.Value);
}
static void WriteUInt16(DataWriter writer, Subsystem? value) {
if (value is null)
writer.Position += 2;
else
writer.WriteUInt16((ushort)value.Value);
}
static void WriteUInt16(DataWriter writer, DllCharacteristics? value) {
if (value is null)
writer.Position += 2;
else
writer.WriteUInt16((ushort)value.Value);
}
static void WriteUInt32(DataWriter writer, uint? value) {
if (value is null)
writer.Position += 4;
else
writer.WriteUInt32(value.Value);
}
static void WriteUInt32(DataWriter writer, ulong? value) {
if (value is null)
writer.Position += 4;
else
writer.WriteUInt32((uint)value.Value);
}
static void WriteUInt64(DataWriter writer, ulong? value) {
if (value is null)
writer.Position += 8;
else
writer.WriteUInt64(value.Value);
}
ComImageFlags GetComImageFlags(bool isManagedEntryPoint) {
var flags = Options.Cor20HeaderOptions.Flags ?? module.Cor20HeaderFlags;
if (Options.Cor20HeaderOptions.EntryPoint is not null)
return flags;
if (isManagedEntryPoint)
return flags & ~ComImageFlags.NativeEntryPoint;
return flags | ComImageFlags.NativeEntryPoint;
}
Subsystem GetSubsystem() {
if (module.Kind == ModuleKind.Windows)
return Subsystem.WindowsGui;
return Subsystem.WindowsCui;
}
/// <summary>
/// Converts <paramref name="rva"/> to a file offset in the destination stream
/// </summary>
/// <param name="rva">RVA</param>
long ToWriterOffset(RVA rva) {
if (rva == 0)
return 0;
foreach (var sect in origSections) {
var section = sect.PESection;
if (section.VirtualAddress <= rva && rva < section.VirtualAddress + Math.Max(section.VirtualSize, section.SizeOfRawData))
return destStreamBaseOffset + (long)sect.Chunk.FileOffset + (rva - section.VirtualAddress);
}
return 0;
}
IEnumerable<SectionSizeInfo> GetSectionSizeInfos() {
foreach (var section in origSections)
yield return new SectionSizeInfo(section.Chunk.GetVirtualSize(), section.PESection.Characteristics);
foreach (var section in sections)
yield return new SectionSizeInfo(section.GetVirtualSize(), section.Characteristics);
}
void UpdateVTableFixups(DataWriter writer) {
var vtableFixups = module.VTableFixups;
if (vtableFixups is null || vtableFixups.VTables.Count == 0)
return;
writer.Position = ToWriterOffset(vtableFixups.RVA);
if (writer.Position == 0) {
Error("Could not convert RVA to file offset");
return;
}
foreach (var vtable in vtableFixups) {
if (vtable.Methods.Count > ushort.MaxValue)
throw new ModuleWriterException("Too many methods in vtable");
writer.WriteUInt32((uint)vtable.RVA);
writer.WriteUInt16((ushort)vtable.Methods.Count);
writer.WriteUInt16((ushort)vtable.Flags);
long pos = writer.Position;
writer.Position = ToWriterOffset(vtable.RVA);
if (writer.Position == 0) {
if (vtable.RVA != 0 || vtable.Methods.Count > 0)
Error("Could not convert RVA to file offset");
}
else {
var methods = vtable.Methods;
int count = methods.Count;
for (int i = 0; i < count; i++) {
var method = methods[i];
writer.WriteUInt32(GetMethodToken(method));
if (vtable.Is64Bit)
writer.WriteInt32(0);
}
}
writer.Position = pos;
}
}
uint GetMethodToken(IMethod method) {
if (method is MethodDef md)
return new MDToken(Table.Method, metadata.GetRid(md)).Raw;
if (method is MemberRef mr)
return new MDToken(Table.MemberRef, metadata.GetRid(mr)).Raw;
if (method is MethodSpec ms)
return new MDToken(Table.MethodSpec, metadata.GetRid(ms)).Raw;
if (method is null)
return 0;
Error("Invalid VTable method type: {0}", method.GetType());
return 0;
}
/// <summary>
/// Gets the entry point
/// </summary>
/// <param name="ep">Updated with entry point (either a token or RVA of native method)</param>
/// <returns><c>true</c> if it's a managed entry point or there's no entry point,
/// <c>false</c> if it's a native entry point</returns>
bool GetEntryPoint(out uint ep) {
var tok = Options.Cor20HeaderOptions.EntryPoint;
if (tok is not null) {
ep = tok.Value;
return ep == 0 || ((Options.Cor20HeaderOptions.Flags ?? 0) & ComImageFlags.NativeEntryPoint) == 0;
}
if (module.ManagedEntryPoint is MethodDef epMethod) {
ep = new MDToken(Table.Method, metadata.GetRid(epMethod)).Raw;
return true;
}
if (module.ManagedEntryPoint is FileDef file) {
ep = new MDToken(Table.File, metadata.GetRid(file)).Raw;
return true;
}
ep = (uint)module.NativeEntryPoint;
return ep == 0;
}
}
}