Skip to content

Commit 4847d50

Browse files
committed
Add API doc to PE relocation
1 parent 4896f9a commit 4847d50

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/LibObjectFile/PE/PEFile.Relocate.cs

+24-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace LibObjectFile.PE;
99

10-
/// <summary>
11-
/// A Portable Executable file that can be read, modified and written.
12-
/// </summary>
1310
partial class PEFile
1411
{
12+
/// <summary>
13+
/// Relocates the PE file to a new image base.
14+
/// </summary>
15+
/// <param name="newImageBase">The new image base.</param>
1516
public void Relocate(ulong newImageBase)
1617
{
1718
var diagnostics = new DiagnosticBag();
@@ -22,6 +23,11 @@ public void Relocate(ulong newImageBase)
2223
}
2324
}
2425

26+
/// <summary>
27+
/// Relocates the PE file to a new image base.
28+
/// </summary>
29+
/// <param name="newImageBase">The new image base.</param>
30+
/// <param name="diagnostics">The diagnostic bag to collect errors and warnings.</param>
2531
public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
2632
{
2733
if (IsPE32 && newImageBase > uint.MaxValue)
@@ -36,7 +42,7 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
3642
// Nothing to do
3743
return;
3844
}
39-
45+
4046
// If we don't have a base relocation directory, we can just update the ImageBase
4147
var baseRelocationDirectory = Directories.BaseRelocation;
4248
if (baseRelocationDirectory is null)
@@ -59,12 +65,13 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
5965
var section = baseRelocationBlock.PageLink.Container;
6066
if (section is null)
6167
{
62-
diagnostics.Error(DiagnosticId.PE_ERR_VerifyContextInvalidObject, $"The {nameof(PEBaseRelocationBlock.PageLink)} in the base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)} is null and missing a link to an actual section");
68+
diagnostics.Error(DiagnosticId.PE_ERR_VerifyContextInvalidObject,
69+
$"The {nameof(PEBaseRelocationBlock.PageLink)} in the base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)} is null and missing a link to an actual section");
6370
return;
6471
}
6572

6673
var pageBaseRva = baseRelocationBlock.PageLink.RVA();
67-
74+
6875
var relocations = CollectionsMarshal.AsSpan(baseRelocationBlock.Relocations);
6976
try
7077
{
@@ -77,11 +84,13 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
7784
{
7885
if (sectionData is null)
7986
{
80-
diagnostics.Error(DiagnosticId.PE_ERR_BaseRelocationInvalid, $"Unable to find the section data for the rva {rva} in the base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)}");
87+
diagnostics.Error(DiagnosticId.PE_ERR_BaseRelocationInvalid,
88+
$"Unable to find the section data for the rva {rva} in the base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)}");
8189
return;
8290
}
8391

84-
diagnostics.Warning(DiagnosticId.PE_WRN_BaseRelocationInVirtualMemory, $"Invalid RVA {rva} found in virtual memory from base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)}");
92+
diagnostics.Warning(DiagnosticId.PE_WRN_BaseRelocationInVirtualMemory,
93+
$"Invalid RVA {rva} found in virtual memory from base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)}");
8594
continue;
8695
}
8796

@@ -102,6 +111,7 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
102111
{
103112
goto WarningOutOfBound;
104113
}
114+
105115
break;
106116
case PEBaseRelocationType.Low:
107117
if (sectionData.CanReadWriteAt(offsetInSectionData, sizeof(ushort)))
@@ -112,6 +122,7 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
112122
{
113123
goto WarningOutOfBound;
114124
}
125+
115126
break;
116127
case PEBaseRelocationType.HighLow:
117128
if (sectionData.CanReadWriteAt(offsetInSectionData, sizeof(uint)))
@@ -122,6 +133,7 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
122133
{
123134
goto WarningOutOfBound;
124135
}
136+
125137
break;
126138
case PEBaseRelocationType.HighAdj:
127139

@@ -142,6 +154,7 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
142154
{
143155
goto WarningOutOfBound;
144156
}
157+
145158
break;
146159
case PEBaseRelocationType.Dir64:
147160
if (sectionData.CanReadWriteAt(offsetInSectionData, sizeof(ulong)))
@@ -152,6 +165,7 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
152165
{
153166
goto WarningOutOfBound;
154167
}
168+
155169
break;
156170
default:
157171
diagnostics.Error(DiagnosticId.PE_ERR_BaseRelocationInvalid, $"Unsupported relocation type {relocation.Type} #{j} in {nameof(PEBaseRelocationBlock)} {baseRelocationBlock}.");
@@ -160,7 +174,8 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
160174

161175
continue;
162176
WarningOutOfBound:
163-
diagnostics.Warning(DiagnosticId.PE_WRN_BaseRelocationInVirtualMemory, $"Cannot process base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)}. The linked address is out of bound.");
177+
diagnostics.Warning(DiagnosticId.PE_WRN_BaseRelocationInVirtualMemory,
178+
$"Cannot process base relocation block {baseRelocationBlock} at index #{i} in the {nameof(PEBaseRelocationDirectory)}. The linked address is out of bound.");
164179
continue;
165180

166181
}
@@ -174,5 +189,4 @@ public void Relocate(ulong newImageBase, DiagnosticBag diagnostics)
174189

175190
OptionalHeader.ImageBase = newImageBase;
176191
}
177-
178192
}

0 commit comments

Comments
 (0)