From d60447d838d5ce3dbfe2f29c05bafff1aefc59c0 Mon Sep 17 00:00:00 2001 From: Tom Shull Date: Fri, 6 Oct 2023 11:49:36 +0200 Subject: [PATCH] Add option to ensure no direct relocations are emitted in the text section. --- .../src/com/oracle/objectfile/ObjectFile.java | 27 +++++++++++++++++++ .../com/oracle/svm/core/SubstrateOptions.java | 3 +++ .../oracle/svm/hosted/image/NativeImage.java | 17 ++++++++++++ 3 files changed, 47 insertions(+) diff --git a/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java b/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java index 519fa8734266..45befb0a0fd0 100644 --- a/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java +++ b/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java @@ -329,6 +329,33 @@ public static boolean isPCRelative(RelocationKind kind) { case PC_RELATIVE_2: case PC_RELATIVE_4: case PC_RELATIVE_8: + case AARCH64_R_AARCH64_ADR_PREL_PG_HI21: + case AARCH64_R_AARCH64_ADD_ABS_LO12_NC: + case AARCH64_R_LD_PREL_LO19: + case AARCH64_R_GOT_LD_PREL19: + case AARCH64_R_AARCH64_LDST128_ABS_LO12_NC: + case AARCH64_R_AARCH64_LDST64_ABS_LO12_NC: + case AARCH64_R_AARCH64_LDST32_ABS_LO12_NC: + case AARCH64_R_AARCH64_LDST16_ABS_LO12_NC: + case AARCH64_R_AARCH64_LDST8_ABS_LO12_NC: + return true; + } + return false; + } + + public static boolean isDirect(RelocationKind kind) { + switch (kind) { + case DIRECT_1: + case DIRECT_2: + case DIRECT_4: + case DIRECT_8: + case AARCH64_R_MOVW_UABS_G0: + case AARCH64_R_MOVW_UABS_G0_NC: + case AARCH64_R_MOVW_UABS_G1: + case AARCH64_R_MOVW_UABS_G1_NC: + case AARCH64_R_MOVW_UABS_G2: + case AARCH64_R_MOVW_UABS_G2_NC: + case AARCH64_R_MOVW_UABS_G3: return true; } return false; diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java index 2948aacaa928..f06cf70dc159 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java @@ -409,6 +409,9 @@ public static void updateMaxJavaStackTraceDepth(EconomicMap, Object @Option(help = "Use only a writable native image heap (requires ld.gold linker)")// public static final HostedOptionKey ForceNoROSectionRelocations = new HostedOptionKey<>(false); + @Option(help = "Force no direct relocations to be present in the text section of the generated image", type = OptionType.Debug) // + public static final HostedOptionKey NoDirectRelocationsInText = new HostedOptionKey<>(true); + @Option(help = "Support multiple isolates.") // public static final HostedOptionKey SpawnIsolates = new HostedOptionKey<>(true); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImage.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImage.java index 68bb406bb11a..d2cc85366598 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImage.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImage.java @@ -97,7 +97,9 @@ import com.oracle.svm.core.image.ImageHeapLayoutInfo; import com.oracle.svm.core.image.ImageHeapPartition; import com.oracle.svm.core.meta.MethodPointer; +import com.oracle.svm.core.option.SubstrateOptionsParser; import com.oracle.svm.core.util.UserError; +import com.oracle.svm.core.util.VMError; import com.oracle.svm.hosted.FeatureImpl; import com.oracle.svm.hosted.NativeImageOptions; import com.oracle.svm.hosted.c.CGlobalDataFeature; @@ -566,9 +568,20 @@ private static boolean checkEmbeddedOffset(ProgbitsSectionImpl sectionImpl, fina return true; } + private void validateNoDirectRelocationsInTextSection(RelocatableBuffer.Info info) { + if (SubstrateOptions.NoDirectRelocationsInText.getValue() && RelocationKind.isDirect(info.getRelocationKind())) { + String message = "%nFound direct relocation in text section. This means that the resulting generated image will have relocations present within the text section. If this is okay, you can skip this check by setting the flag %s"; + throw VMError.shouldNotReachHere(message, SubstrateOptionsParser.commandArgument(SubstrateOptions.NoDirectRelocationsInText, "-")); + } + } + private void markFunctionRelocationSite(final ProgbitsSectionImpl sectionImpl, final int offset, final RelocatableBuffer.Info info) { assert info.getTargetObject() instanceof CFunctionPointer : "Wrong type for FunctionPointer relocation: " + info.getTargetObject().toString(); + if (sectionImpl.getElement() == textSection) { + validateNoDirectRelocationsInTextSection(info); + } + // References to functions are via relocations to the symbol for the function. MethodPointer methodPointer = (MethodPointer) info.getTargetObject(); ResolvedJavaMethod method = methodPointer.getMethod(); @@ -622,10 +635,14 @@ private void markDataRelocationSiteFromText(RelocatableBuffer buffer, final Prog info.getRelocationSize(); Object target = info.getTargetObject(); if (target instanceof DataSectionReference) { + validateNoDirectRelocationsInTextSection(info); + long addend = ((DataSectionReference) target).getOffset() - info.getAddend(); assert isAddendAligned(arch, addend, info.getRelocationKind()) : "improper addend alignment"; sectionImpl.markRelocationSite(offset, info.getRelocationKind(), roDataSection.getName(), addend); } else if (target instanceof CGlobalDataReference) { + validateNoDirectRelocationsInTextSection(info); + CGlobalDataReference ref = (CGlobalDataReference) target; CGlobalDataInfo dataInfo = ref.getDataInfo(); CGlobalDataImpl data = dataInfo.getData();