From 85c5c654576dcb5da29b0606ea5edbcbb2625942 Mon Sep 17 00:00:00 2001 From: Sacha Coppey Date: Tue, 24 Sep 2024 21:41:00 +0200 Subject: [PATCH] Link base layer libraries as a dynamic library --- .../svm/hosted/NativeImageGenerator.java | 2 +- .../svm/hosted/image/CCLinkerInvocation.java | 6 +++++- .../imagelayer/HostedDynamicLayerInfo.java | 21 ++++++++++++++++--- .../HostedImageLayerBuildingSupport.java | 10 +++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java index dccbb0149a19..ecd4088a5119 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java @@ -1323,7 +1323,7 @@ protected NativeLibraries setupNativeLibraries(HostedProviders providers, CEnumC throw new InterruptImageBuilding("Exiting image generation because of " + SubstrateOptionsParser.commandArgument(CAnnotationProcessorCache.Options.ExitAfterCAPCache, "+")); } if (ImageLayerBuildingSupport.buildingExtensionLayer()) { - HostedImageLayerBuildingSupport.setupSharedLayerLibrary(nativeLibs); + HostedImageLayerBuildingSupport.singleton().setupSharedLayerLibrary(nativeLibs); } return nativeLibs; } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/CCLinkerInvocation.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/CCLinkerInvocation.java index ec406fa64ceb..f476c2d3dee4 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/CCLinkerInvocation.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/CCLinkerInvocation.java @@ -48,6 +48,7 @@ import com.oracle.svm.core.SubstrateOptions; import com.oracle.svm.core.c.libc.BionicLibC; import com.oracle.svm.core.c.libc.LibCBase; +import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport; import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue; import com.oracle.svm.core.option.HostedOptionKey; import com.oracle.svm.core.util.UserError; @@ -56,6 +57,7 @@ import com.oracle.svm.hosted.c.NativeLibraries; import com.oracle.svm.hosted.c.codegen.CCompilerInvoker; import com.oracle.svm.hosted.c.libc.HostedLibCBase; +import com.oracle.svm.hosted.imagelayer.HostedDynamicLayerInfo; import com.oracle.svm.hosted.jdk.JNIRegistrationSupport; import jdk.graal.compiler.options.Option; @@ -343,7 +345,9 @@ protected List getLibrariesCommand() { } for (String lib : libs) { String linkingMode = null; - if (dynamicLibC) { + if (ImageLayerBuildingSupport.buildingImageLayer() && HostedDynamicLayerInfo.singleton().isImageLayerLib(lib)) { + linkingMode = "dynamic"; + } else if (dynamicLibC) { linkingMode = LIB_C_NAMES.contains(lib) ? "dynamic" : "static"; } else if (staticLibCpp) { linkingMode = lib.equals("stdc++") ? "static" : "dynamic"; diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedDynamicLayerInfo.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedDynamicLayerInfo.java index 5fccabf435e0..442e951f8087 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedDynamicLayerInfo.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedDynamicLayerInfo.java @@ -68,20 +68,23 @@ public class HostedDynamicLayerInfo extends DynamicImageLayerInfo implements Lay private final ConcurrentHashMap methodIdToNameInfoMap; private final CGlobalData cGlobalData; private final Set priorLayerMethodSymbols = new HashSet<>(); + private final List libNames; private boolean persisted = false; HostedDynamicLayerInfo() { - this(0, null, new HashMap<>(), new ConcurrentHashMap<>()); + this(0, null, new HashMap<>(), new ConcurrentHashMap<>(), new ArrayList<>()); } public static HostedDynamicLayerInfo singleton() { return (HostedDynamicLayerInfo) ImageSingletons.lookup(DynamicImageLayerInfo.class); } - private HostedDynamicLayerInfo(int layerNumber, String codeSectionStartSymbol, Map methodIdToOffsetMap, ConcurrentHashMap methodIdToNameInfoMap) { + private HostedDynamicLayerInfo(int layerNumber, String codeSectionStartSymbol, Map methodIdToOffsetMap, ConcurrentHashMap methodIdToNameInfoMap, + List libNames) { super(layerNumber); this.methodIdToOffsetMap = methodIdToOffsetMap; this.methodIdToNameInfoMap = methodIdToNameInfoMap; + this.libNames = libNames; cGlobalData = codeSectionStartSymbol == null ? null : CGlobalDataFactory.forSymbol(codeSectionStartSymbol); } @@ -141,6 +144,14 @@ public void defineSymbolsForPriorLayerMethods(ObjectFile objectFile) { priorLayerMethodSymbols.forEach(symbol -> objectFile.createUndefinedSymbol(symbol, 0, true)); } + public void registerLibName(String lib) { + libNames.add(lib); + } + + public boolean isImageLayerLib(String lib) { + return libNames.contains(lib); + } + @Override public EnumSet getImageBuilderFlags() { return LayeredImageSingletonBuilderFlags.BUILDTIME_ACCESS_ONLY; @@ -207,6 +218,8 @@ public PersistFlags preparePersist(ImageSingletonWriter writer) { writer.writeIntList("methodNameIDs", methodNameIds); writer.writeStringList("names", names); + writer.writeStringList("libNames", libNames); + return PersistFlags.CREATE; } @@ -248,7 +261,9 @@ public static Object createFromLoader(ImageSingletonLoader loader) { assert prev == null; } - return new HostedDynamicLayerInfo(layerNumber, codeSectionStartSymbol, initialMethodIdToOffsetMap, initialMethodIdToMethodNameMap); + var libNames = loader.readStringList("libNames"); + + return new HostedDynamicLayerInfo(layerNumber, codeSectionStartSymbol, initialMethodIdToOffsetMap, initialMethodIdToMethodNameMap, libNames); } } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedImageLayerBuildingSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedImageLayerBuildingSupport.java index 013f3d1f6146..9a1dae084dd7 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedImageLayerBuildingSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/HostedImageLayerBuildingSupport.java @@ -189,16 +189,18 @@ public static HostedImageLayerBuildingSupport initialize(HostedOptionValues valu } @SuppressFBWarnings(value = "NP", justification = "FB reports null pointer dereferencing because it doesn't see through UserError.guarantee.") - public static void setupSharedLayerLibrary(NativeLibraries nativeLibs) { + public void setupSharedLayerLibrary(NativeLibraries nativeLibs) { Path sharedLibPath = HostedImageLayerBuildingSupport.singleton().getLoadLayerArchiveSupport().getSharedLibraryPath(); Path parent = sharedLibPath.getParent(); VMError.guarantee(parent != null, "Shared layer library path doesn't have a parent."); nativeLibs.getLibraryPaths().add(parent.toString()); Path fileName = sharedLibPath.getFileName(); VMError.guarantee(fileName != null, "Cannot determine shared layer library file name."); - String libName = fileName.toString(); - VMError.guarantee(libName.startsWith("lib") && libName.endsWith(".so"), "Expecting that shared layer library file starts with lib and ends with .so. Found: %s", libName); - nativeLibs.addDynamicNonJniLibrary(libName.substring("lib".length(), libName.indexOf(".so"))); + String fullLibName = fileName.toString(); + VMError.guarantee(fullLibName.startsWith("lib") && fullLibName.endsWith(".so"), "Expecting that shared layer library file starts with lib and ends with .so. Found: %s", fullLibName); + String libName = fullLibName.substring("lib".length(), fullLibName.length() - ".so".length()); + HostedDynamicLayerInfo.singleton().registerLibName(libName); + nativeLibs.addDynamicNonJniLibrary(libName); } public static void setupImageLayerArtifacts(String imageName) {