@@ -28,52 +28,11 @@ public sealed class R2RPEBuilder : PEBuilder
28
28
/// </summary>
29
29
const int RVABitsToMatchFilePos = 16 ;
30
30
31
- /// <summary>
32
- /// This structure describes how a particular section moved between the original MSIL
33
- /// and the output PE file. It holds beginning and end RVA of the input (MSIL) section
34
- /// and a delta between the input and output starting RVA of the section.
35
- /// </summary>
36
- struct SectionRVADelta
37
- {
38
- /// <summary>
39
- /// Starting RVA of the section in the input MSIL PE.
40
- /// </summary>
41
- public readonly int StartRVA ;
42
-
43
- /// <summary>
44
- /// End RVA (one plus the last RVA in the section) of the section in the input MSIL PE.
45
- /// </summary>
46
- public readonly int EndRVA ;
47
-
48
- /// <summary>
49
- /// Starting RVA of the section in the output PE minus its starting RVA in the input MSIL.
50
- /// </summary>
51
- public readonly int DeltaRVA ;
52
-
53
- /// <summary>
54
- /// Initialize the section RVA delta information.
55
- /// </summary>
56
- /// <param name="startRVA">Starting RVA of the section in the input MSIL</param>
57
- /// <param name="endRVA">End RVA of the section in the input MSIL</param>
58
- /// <param name="deltaRVA">Output RVA of the section minus input RVA of the section</param>
59
- public SectionRVADelta ( int startRVA , int endRVA , int deltaRVA )
60
- {
61
- StartRVA = startRVA ;
62
- EndRVA = endRVA ;
63
- DeltaRVA = deltaRVA ;
64
- }
65
- }
66
-
67
31
/// <summary>
68
32
/// Name of the text section.
69
33
/// </summary>
70
34
public const string TextSectionName = ".text" ;
71
35
72
- /// <summary>
73
- /// Name of the initialized data section.
74
- /// </summary>
75
- public const string SDataSectionName = ".sdata" ;
76
-
77
36
/// <summary>
78
37
/// Name of the relocation section.
79
38
/// </summary>
@@ -100,13 +59,6 @@ public SectionRVADelta(int startRVA, int endRVA, int deltaRVA)
100
59
/// </summary>
101
60
private Func < RuntimeFunctionsTableNode > _getRuntimeFunctionsTable ;
102
61
103
- /// <summary>
104
- /// For each copied section, we store its initial and end RVA in the source PE file
105
- /// and the RVA difference between the old and new file. We use this table to relocate
106
- /// directory entries in the PE file header.
107
- /// </summary>
108
- private List < SectionRVADelta > _sectionRvaDeltas ;
109
-
110
62
private class SerializedSectionData
111
63
{
112
64
/// <summary>
@@ -194,7 +146,6 @@ public R2RPEBuilder(
194
146
{
195
147
_target = target ;
196
148
_getRuntimeFunctionsTable = getRuntimeFunctionsTable ;
197
- _sectionRvaDeltas = new List < SectionRVADelta > ( ) ;
198
149
199
150
_sectionBuilder = new SectionBuilder ( target ) ;
200
151
@@ -210,16 +161,13 @@ public R2RPEBuilder(
210
161
_sectionBuilder . SetDllNameForExportDirectoryTable ( outputFileSimpleName ) ;
211
162
}
212
163
213
- if ( _sectionBuilder . FindSection ( R2RPEBuilder . RelocSectionName ) == null )
214
- {
215
- // Always inject the relocation section to the end of section list
216
- _sectionBuilder . AddSection (
217
- R2RPEBuilder . RelocSectionName ,
218
- SectionCharacteristics . ContainsInitializedData |
219
- SectionCharacteristics . MemRead |
220
- SectionCharacteristics . MemDiscardable ,
221
- PEHeaderConstants . SectionAlignment ) ;
222
- }
164
+ // Always inject the relocation section to the end of section list
165
+ _sectionBuilder . AddSection (
166
+ R2RPEBuilder . RelocSectionName ,
167
+ SectionCharacteristics . ContainsInitializedData |
168
+ SectionCharacteristics . MemRead |
169
+ SectionCharacteristics . MemDiscardable ,
170
+ PEHeaderConstants . SectionAlignment ) ;
223
171
224
172
List < SerializedSectionData > sectionData = new List < SerializedSectionData > ( ) ;
225
173
foreach ( SectionInfo sectionInfo in _sectionBuilder . GetSections ( ) )
@@ -353,7 +301,7 @@ public void AddSections(OutputInfoBuilder outputInfoBuilder)
353
301
sizeof ( int ) + // SizeOfUninitializedData
354
302
sizeof ( int ) + // AddressOfEntryPoint
355
303
sizeof ( int ) + // BaseOfCode
356
- sizeof ( long ) ; // PE32: BaseOfData (int), ImageBase (int)
304
+ sizeof ( long ) ; // PE32: BaseOfData (int), ImageBase (int)
357
305
// PE32+: ImageBase (long)
358
306
const int OffsetOfChecksum = OffsetOfSectionAlign +
359
307
sizeof ( int ) + // SectionAlignment
@@ -371,7 +319,7 @@ public void AddSections(OutputInfoBuilder outputInfoBuilder)
371
319
const int OffsetOfSizeOfImage = OffsetOfChecksum - 2 * sizeof ( int ) ; // SizeOfHeaders, SizeOfImage
372
320
373
321
const int SectionHeaderNameSize = 8 ;
374
- const int SectionHeaderVirtualSize = SectionHeaderNameSize ; // VirtualSize follows
322
+ const int SectionHeaderVirtualSize = SectionHeaderNameSize ; // VirtualSize follows
375
323
const int SectionHeaderRVAOffset = SectionHeaderVirtualSize + sizeof ( int ) ; // RVA Offset follows VirtualSize + 4 bytes VirtualSize
376
324
const int SectionHeaderSizeOfRawData = SectionHeaderRVAOffset + sizeof ( int ) ; // SizeOfRawData follows RVA
377
325
const int SectionHeaderPointerToRawDataOffset = SectionHeaderSizeOfRawData + sizeof ( int ) ; // PointerToRawData immediately follows the SizeOfRawData
@@ -385,7 +333,7 @@ public void AddSections(OutputInfoBuilder outputInfoBuilder)
385
333
sizeof ( int ) + // PointerToRelocations
386
334
sizeof ( int ) + // PointerToLineNumbers
387
335
sizeof ( short ) + // NumberOfRelocations
388
- sizeof ( short ) + // NumberOfLineNumbers
336
+ sizeof ( short ) + // NumberOfLineNumbers
389
337
sizeof ( int ) ; // SectionCharacteristics
390
338
391
339
/// <summary>
@@ -538,42 +486,8 @@ protected override PEDirectoriesBuilder GetDirectories()
538
486
size : runtimeFunctionsTable . TableSizeExcludingSentinel ) ;
539
487
}
540
488
}
541
-
542
- return builder ;
543
- }
544
489
545
- /// <summary>
546
- /// Relocate a single directory entry.
547
- /// </summary>
548
- /// <param name="entry">Directory entry to allocate</param>
549
- /// <returns>Relocated directory entry</returns>
550
- public DirectoryEntry RelocateDirectoryEntry ( DirectoryEntry entry )
551
- {
552
- return new DirectoryEntry ( RelocateRVA ( entry . RelativeVirtualAddress ) , entry . Size ) ;
553
- }
554
-
555
- /// <summary>
556
- /// Relocate a given RVA using the section offset table produced during section serialization.
557
- /// </summary>
558
- /// <param name="rva">RVA to relocate</param>
559
- /// <returns>Relocated RVA</returns>
560
- private int RelocateRVA ( int rva )
561
- {
562
- if ( rva == 0 )
563
- {
564
- // Zero RVA is normally used as NULL
565
- return rva ;
566
- }
567
- foreach ( SectionRVADelta sectionRvaDelta in _sectionRvaDeltas )
568
- {
569
- if ( rva >= sectionRvaDelta . StartRVA && rva < sectionRvaDelta . EndRVA )
570
- {
571
- // We found the input section holding the RVA, apply its specific delt (output RVA - input RVA).
572
- return rva + sectionRvaDelta . DeltaRVA ;
573
- }
574
- }
575
- Debug . Fail ( "RVA is not within any of the input sections - output PE may be inconsistent" ) ;
576
- return rva ;
490
+ return builder ;
577
491
}
578
492
579
493
/// <summary>
0 commit comments