Skip to content

Commit 946090a

Browse files
committed
use pc-relative instructions for loading SubstrateMethodPointerConstants
1 parent cb22258 commit 946090a

File tree

5 files changed

+33
-38
lines changed

5 files changed

+33
-38
lines changed

substratevm/src/com.oracle.svm.core.graal.amd64/src/com/oracle/svm/core/graal/amd64/AMD64LoadMethodPointerConstantOp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class AMD64LoadMethodPointerConstantOp extends AMD64LIRInstruction
5454
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
5555
Register resultReg = asRegister(result);
5656
crb.recordInlineDataInCode(constant);
57-
masm.movq(resultReg, 0L, true);
57+
masm.leaq(resultReg, masm.getPlaceholder(masm.position()));
5858
}
5959

6060
@Override

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/aarch64/AArch64HostedPatcherFeature.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,14 @@
4141
import com.oracle.svm.core.feature.InternalFeature;
4242
import com.oracle.svm.core.graal.code.CGlobalDataReference;
4343
import com.oracle.svm.core.graal.code.PatchConsumerFactory;
44-
import com.oracle.svm.core.meta.MethodPointer;
4544
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
4645
import com.oracle.svm.core.util.VMError;
4746
import com.oracle.svm.hosted.code.HostedPatcher;
4847
import com.oracle.svm.hosted.image.RelocatableBuffer;
49-
import com.oracle.svm.hosted.meta.HostedMethod;
5048

5149
import jdk.vm.ci.code.site.ConstantReference;
5250
import jdk.vm.ci.code.site.DataSectionReference;
5351
import jdk.vm.ci.code.site.Reference;
54-
import jdk.vm.ci.meta.VMConstant;
5552

5653
@AutomaticallyRegisteredFeature
5754
@Platforms({Platform.AARCH64.class})
@@ -172,22 +169,17 @@ class AdrpAddMacroInstructionHostedPatcher extends CompilationResult.CodeAnnotat
172169

173170
@Override
174171
public void relocate(Reference ref, RelocatableBuffer relocs, int compStart) {
175-
Object relocVal = ref;
176-
if (ref instanceof ConstantReference) {
177-
VMConstant constant = ((ConstantReference) ref).getConstant();
178-
if (constant instanceof SubstrateMethodPointerConstant) {
179-
MethodPointer pointer = ((SubstrateMethodPointerConstant) constant).pointer();
180-
HostedMethod hMethod = (HostedMethod) pointer.getMethod();
181-
VMError.guarantee(hMethod.isCompiled(), "Method %s is not compiled although there is a method pointer constant created for it.", hMethod);
182-
relocVal = pointer;
183-
}
172+
if (ref instanceof ConstantReference constantRef) {
173+
VMError.guarantee(!(constantRef.getConstant() instanceof SubstrateMethodPointerConstant), "SubstrateMethodPointerConstants should not be relocated %s", constantRef);
174+
} else {
175+
VMError.guarantee(ref instanceof DataSectionReference || ref instanceof CGlobalDataReference, "Unexpected reference: %s", ref);
184176
}
185177

186178
int siteOffset = compStart + macroInstruction.instructionPosition;
187-
relocs.addRelocationWithoutAddend(siteOffset, RelocationKind.AARCH64_R_AARCH64_ADR_PREL_PG_HI21, relocVal);
179+
relocs.addRelocationWithoutAddend(siteOffset, RelocationKind.AARCH64_R_AARCH64_ADR_PREL_PG_HI21, ref);
188180

189181
siteOffset += 4;
190-
relocs.addRelocationWithoutAddend(siteOffset, RelocationKind.AARCH64_R_AARCH64_ADD_ABS_LO12_NC, relocVal);
182+
relocs.addRelocationWithoutAddend(siteOffset, RelocationKind.AARCH64_R_AARCH64_ADD_ABS_LO12_NC, ref);
191183
}
192184

193185
@Uninterruptible(reason = ".")
@@ -221,8 +213,8 @@ public void relocate(Reference ref, RelocatableBuffer relocs, int compStart) {
221213
*/
222214
int siteOffset = compStart + annotation.instructionPosition;
223215
if (ref instanceof DataSectionReference || ref instanceof CGlobalDataReference || ref instanceof ConstantReference) {
224-
if (ref instanceof ConstantReference) {
225-
assert !(((ConstantReference) ref).getConstant() instanceof SubstrateMethodPointerConstant);
216+
if (ref instanceof ConstantReference constantRef) {
217+
VMError.guarantee(!(constantRef.getConstant() instanceof SubstrateMethodPointerConstant), "SubstrateMethodPointerConstants should not be relocated %s", constantRef);
226218
}
227219
/*
228220
* calculating the last mov index. This is necessary ensure the proper overflow checks

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/amd64/AMD64HostedPatcherFeature.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@
4040
import com.oracle.svm.core.feature.InternalFeature;
4141
import com.oracle.svm.core.graal.code.CGlobalDataReference;
4242
import com.oracle.svm.core.graal.code.PatchConsumerFactory;
43-
import com.oracle.svm.core.meta.MethodPointer;
4443
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
4544
import com.oracle.svm.core.util.VMError;
4645
import com.oracle.svm.hosted.code.HostedImageHeapConstantPatch;
4746
import com.oracle.svm.hosted.code.HostedPatcher;
4847
import com.oracle.svm.hosted.image.RelocatableBuffer;
49-
import com.oracle.svm.hosted.meta.HostedMethod;
5048

5149
import jdk.vm.ci.code.site.ConstantReference;
5250
import jdk.vm.ci.code.site.DataSectionReference;
5351
import jdk.vm.ci.code.site.Reference;
5452
import jdk.vm.ci.meta.JavaConstant;
55-
import jdk.vm.ci.meta.VMConstant;
5653

5754
@AutomaticallyRegisteredFeature
5855
@Platforms({Platform.AMD64.class})
@@ -122,16 +119,9 @@ public void relocate(Reference ref, RelocatableBuffer relocs, int compStart) {
122119
*/
123120
long addend = (annotation.nextInstructionPosition - annotation.operandPosition);
124121
relocs.addRelocationWithAddend((int) siteOffset, ObjectFile.RelocationKind.getPCRelative(annotation.operandSize), addend, ref);
125-
} else if (ref instanceof ConstantReference) {
126-
VMConstant constant = ((ConstantReference) ref).getConstant();
127-
Object relocVal = ref;
128-
if (constant instanceof SubstrateMethodPointerConstant) {
129-
MethodPointer pointer = ((SubstrateMethodPointerConstant) constant).pointer();
130-
HostedMethod hMethod = (HostedMethod) pointer.getMethod();
131-
VMError.guarantee(hMethod.isCompiled(), "Method %s is not compiled although there is a method pointer constant created for it.", hMethod);
132-
relocVal = pointer;
133-
}
134-
relocs.addRelocationWithoutAddend((int) siteOffset, ObjectFile.RelocationKind.getDirect(annotation.operandSize), relocVal);
122+
} else if (ref instanceof ConstantReference constantRef) {
123+
VMError.guarantee(!(constantRef.getConstant() instanceof SubstrateMethodPointerConstant), "SubstrateMethodPointerConstants should not be relocated %s", constantRef);
124+
relocs.addRelocationWithoutAddend((int) siteOffset, ObjectFile.RelocationKind.getDirect(annotation.operandSize), ref);
135125
} else {
136126
throw VMError.shouldNotReachHere("Unknown type of reference in code");
137127
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/LIRNativeImageCodeCache.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.objectfile.ObjectFile;
4646
import com.oracle.svm.core.SubstrateOptions;
4747
import com.oracle.svm.core.config.ConfigurationValues;
48+
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
4849
import com.oracle.svm.core.util.VMError;
4950
import com.oracle.svm.hosted.code.HostedDirectCallTrampolineSupport;
5051
import com.oracle.svm.hosted.code.HostedImageHeapConstantPatch;
@@ -54,6 +55,7 @@
5455

5556
import jdk.vm.ci.code.TargetDescription;
5657
import jdk.vm.ci.code.site.Call;
58+
import jdk.vm.ci.code.site.ConstantReference;
5759
import jdk.vm.ci.code.site.DataPatch;
5860
import jdk.vm.ci.code.site.Infopoint;
5961
import jdk.vm.ci.code.site.Reference;
@@ -343,7 +345,7 @@ public void patchMethods(DebugContext debug, RelocatableBuffer relocs, ObjectFil
343345
// the codecache-relative offset of the compilation
344346
int compStart = method.getCodeAddressOffset();
345347

346-
// Build an index of PatchingAnnoations
348+
// Build an index of PatchingAnnotations
347349
Map<Integer, HostedPatcher> patches = new HashMap<>();
348350
ByteBuffer targetCode = null;
349351
for (CodeAnnotation codeAnnotation : compilation.getCodeAnnotations()) {
@@ -395,12 +397,25 @@ public void patchMethods(DebugContext debug, RelocatableBuffer relocs, ObjectFil
395397
}
396398
}
397399
for (DataPatch dataPatch : compilation.getDataPatches()) {
400+
assert dataPatch.note == null : "Unexpected note: " + dataPatch.note;
398401
Reference ref = dataPatch.reference;
399-
/*
400-
* Constants are allocated offsets in a separate space, which can be emitted as
401-
* read-only (.rodata) section.
402-
*/
403-
patches.get(dataPatch.pcOffset).relocate(ref, relocs, compStart);
402+
var patcher = patches.get(dataPatch.pcOffset);
403+
if (ref instanceof ConstantReference constant && constant.getConstant() instanceof SubstrateMethodPointerConstant methodPtrConstant) {
404+
/*
405+
* We directly patch SubstrateMethodPointerConstants.
406+
*/
407+
HostedMethod hMethod = (HostedMethod) methodPtrConstant.pointer().getMethod();
408+
VMError.guarantee(hMethod.isCompiled(), "Method %s is not compiled although there is a method pointer constant created for it.", hMethod);
409+
int targetOffset = hMethod.getCodeAddressOffset();
410+
int pcDisplacement = targetOffset - (compStart + dataPatch.pcOffset);
411+
patcher.patch(compStart, pcDisplacement, compilation.getTargetCode());
412+
} else {
413+
/*
414+
* Constants are allocated offsets in a separate space, which can be emitted as
415+
* read-only (.rodata) section.
416+
*/
417+
patcher.relocate(ref, relocs, compStart);
418+
}
404419
boolean noPriorMatch = patchedOffsets.add(dataPatch.pcOffset);
405420
VMError.guarantee(noPriorMatch, "Patching same offset twice.");
406421
patchesHandled++;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageHeapWriter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import com.oracle.svm.core.hub.DynamicHub;
5555
import com.oracle.svm.core.image.ImageHeapLayoutInfo;
5656
import com.oracle.svm.core.meta.MethodPointer;
57-
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
5857
import com.oracle.svm.core.meta.SubstrateObjectConstant;
5958
import com.oracle.svm.hosted.config.HybridLayout;
6059
import com.oracle.svm.hosted.image.NativeImageHeap.ObjectInfo;
@@ -65,7 +64,6 @@
6564
import com.oracle.svm.hosted.meta.RelocatableConstant;
6665

6766
import jdk.internal.misc.Unsafe;
68-
import jdk.vm.ci.meta.Constant;
6967
import jdk.vm.ci.meta.JavaConstant;
7068
import jdk.vm.ci.meta.JavaKind;
7169

0 commit comments

Comments
 (0)