Skip to content

Commit 4019e83

Browse files
authored
Don't apply IMAGE_REL_BASED_REL32 when address doesn't belong to current section (#66855)
* Don't attempt to apply IMAGE_REL_BASED_REL32 when address doesn't belong to current block in src/coreclr/ToolBox/superpmi/superpmi-shared/compileresult.cpp * Update "Exit Codes" section in the superpmi help command output
1 parent ef4773c commit 4019e83

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/coreclr/tools/superpmi/superpmi-shared/compileresult.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -884,13 +884,14 @@ void CompileResult::applyRelocs(unsigned char* block1, ULONG blocksize1, void* o
884884
continue;
885885

886886
// Now do all-platform relocations.
887-
888-
switch (tmp.fRelocType)
887+
if (tmp.fRelocType == IMAGE_REL_BASED_REL32)
889888
{
890-
case IMAGE_REL_BASED_REL32:
889+
DWORDLONG fixupLocation = tmp.location + tmp.slotNum;
890+
891+
size_t address = section_begin + (size_t)fixupLocation - (size_t)originalAddr;
892+
if ((section_begin <= address) && (address < section_end)) // A reloc for our section?
891893
{
892894
DWORDLONG target = tmp.target + tmp.addlDelta;
893-
DWORDLONG fixupLocation = tmp.location + tmp.slotNum;
894895
DWORDLONG baseAddr = fixupLocation + sizeof(INT32);
895896
INT64 delta = (INT64)(target - baseAddr);
896897

@@ -899,13 +900,12 @@ void CompileResult::applyRelocs(unsigned char* block1, ULONG blocksize1, void* o
899900
if (delta != (INT64)(int)delta)
900901
{
901902
// This isn't going to fit in a signed 32-bit address. Use something that will fit,
902-
// since we assume that original compilation fit fine. This is only an issue for
903-
// 32-bit offsets on 64-bit targets.
903+
// since we assume that original compilation fit fine.
904+
// This is only an issue for 32-bit offsets on 64-bit targets.
904905
target = (DWORDLONG)originalAddr + (DWORDLONG)blocksize1;
905906
INT64 newdelta = (INT64)(target - baseAddr);
906907

907-
LogDebug(" REL32 overflow. Mapping target to %016llX. Mapping delta: %016llX => %016llX", target,
908-
delta, newdelta);
908+
LogDebug(" REL32 overflow. Mapping target to %016llX. Mapping delta: %016llX => %016llX", target, delta, newdelta);
909909

910910
delta = newdelta;
911911
}
@@ -916,32 +916,29 @@ void CompileResult::applyRelocs(unsigned char* block1, ULONG blocksize1, void* o
916916
LogError("REL32 relocation overflows field! delta=0x%016llX", delta);
917917
}
918918

919-
// Write 32-bits into location
920-
size_t address = section_begin + (size_t)fixupLocation - (size_t)originalAddr;
921-
if ((section_begin <= address) && (address < section_end)) // A reloc for our section?
919+
if (targetArch == SPMI_TARGET_ARCHITECTURE_AMD64)
922920
{
923-
if (targetArch == SPMI_TARGET_ARCHITECTURE_AMD64)
924-
{
925-
// During an actual compile, recordRelocation() will be called before the compile
926-
// is actually finished, and it will write the relative offset into the fixupLocation.
927-
// Then, emitEndCodeGen() will patch forward jumps by subtracting any adjustment due
928-
// to overestimation of instruction sizes. Because we're applying the relocs after the
929-
// compile has finished, we need to reverse that: i.e. add in the (negative) adjustment
930-
// that's now in the fixupLocation.
931-
INT32 adjustment = *(INT32*)address;
932-
delta += adjustment;
933-
}
934-
935-
LogDebug(" fixupLoc-%016llX (@%p) : %08X => %08X", fixupLocation, address, *(DWORD*)address,
936-
delta);
937-
*(DWORD*)address = (DWORD)delta;
921+
// During an actual compile, recordRelocation() will be called before the compile
922+
// is actually finished, and it will write the relative offset into the fixupLocation.
923+
// Then, emitEndCodeGen() will patch forward jumps by subtracting any adjustment due
924+
// to overestimation of instruction sizes. Because we're applying the relocs after the
925+
// compile has finished, we need to reverse that: i.e. add in the (negative) adjustment
926+
// that's now in the fixupLocation.
927+
INT32 adjustment = *(INT32*)address;
928+
delta += adjustment;
938929
}
930+
931+
// Write 32-bits into location
932+
LogDebug(" fixupLoc-%016llX (@%p) : %08X => %08X", fixupLocation, address, *(DWORD*)address, delta);
933+
*(DWORD*)address = (DWORD)delta;
939934
}
940-
break;
941935

942-
default:
943-
LogError("Unknown reloc type %u", tmp.fRelocType);
944-
break;
936+
wasRelocHandled = true;
937+
}
938+
939+
if (!wasRelocHandled)
940+
{
941+
LogError("Unknown reloc type %u", tmp.fRelocType);
945942
}
946943
}
947944
}

src/coreclr/tools/superpmi/superpmi/commandline.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ void CommandLine::DumpHelp(const char* program)
156156
printf("-2 : JIT failed to initialize\n");
157157
printf("1 : there were compilation failures\n");
158158
printf("2 : there were assembly diffs\n");
159+
printf("3 : there were missing values in method context\n");
159160
printf("\n");
160161
printf("Examples:\n");
161162
printf(" %s " MAKEDLLNAME_A("clrjit") " test.mch\n", program);

0 commit comments

Comments
 (0)