Skip to content

Commit

Permalink
Automatic merge of master into galahad
Browse files Browse the repository at this point in the history
  • Loading branch information
OracleLabsAutomation committed Mar 5, 2025
2 parents 4765c77 + 8d4cfcb commit 084aacd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ public void addStaticJniLibrary(String library, String... dependencies) {
List<String> allDeps = new ArrayList<>(Arrays.asList(dependencies));
/* "jvm" is a basic dependence for static JNI libs */
allDeps.add("jvm");
if (library.equals("nio")) {
/* "nio" implicitly depends on "net" */
allDeps.add("net");
}
dependencyGraph.add(library, allDeps);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,18 @@ protected void setOutputKind(List<String> cmd) {
@Override
protected List<String> getLibrariesCommand() {
List<String> cmd = new ArrayList<>();
String pushState = "-Wl,--push-state";
String popState = "-Wl,--pop-state";
if (customStaticLibs) {
cmd.add("-Wl,--push-state");
cmd.add(pushState);
}
String previousLayerLib = null;
for (String lib : libs) {
String linkingMode = null;
if (ImageLayerBuildingSupport.buildingImageLayer() && HostedDynamicLayerInfo.singleton().isImageLayerLib(lib)) {
linkingMode = "dynamic";
if (ImageLayerBuildingSupport.buildingExtensionLayer() && HostedDynamicLayerInfo.singleton().isImageLayerLib(lib)) {
VMError.guarantee(!lib.isEmpty());
VMError.guarantee(previousLayerLib == null, "We currently only support one previous layer."); // GR-58631
previousLayerLib = lib;
} else if (dynamicLibC) {
linkingMode = LIB_C_NAMES.contains(lib) ? "dynamic" : "static";
} else if (staticLibCpp) {
Expand All @@ -358,7 +363,14 @@ protected List<String> getLibrariesCommand() {
cmd.add("-l" + lib);
}
if (customStaticLibs) {
cmd.add("-Wl,--pop-state");
cmd.add(popState);
}

if (previousLayerLib != null) {
cmd.add(pushState);
cmd.add("-Wl,-Bdynamic");
cmd.add("-l" + previousLayerLib);
cmd.add(popState);
}

// Make sure libgcc gets statically linked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

import org.graalvm.nativeimage.ImageSingletons;
Expand Down Expand Up @@ -127,7 +125,7 @@ public void afterAnalysis(AfterAnalysisAccess access) {
isSunMSCAPIProviderReachable = optSunMSCAPIClass.isPresent() && access.isReachable(optSunMSCAPIClass.get());
}
if (ImageLayerBuildingSupport.buildingExtensionLayer()) {
for (String library : jniRegistrationSupportSingleton.baseLayerRegisteredLibraries) {
for (String library : jniRegistrationSupportSingleton.prevLayerRegisteredLibraries) {
addLibrary(library);
}
}
Expand Down Expand Up @@ -159,7 +157,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
}

void registerLibrary(String libname) {
if (libname != null && jniRegistrationSupportSingleton.registeredLibraries.putIfAbsent(libname, Boolean.TRUE) == null) {
if (libname != null && !jniRegistrationSupportSingleton.currentLayerRegisteredLibraries.contains(libname)) {
jniRegistrationSupportSingleton.currentLayerRegisteredLibraries.add(libname);
addLibrary(libname);
}
}
Expand All @@ -175,7 +174,7 @@ private void addLibrary(String libname) {
}

public boolean isRegisteredLibrary(String libname) {
return jniRegistrationSupportSingleton.registeredLibraries.containsKey(libname);
return jniRegistrationSupportSingleton.currentLayerRegisteredLibraries.contains(libname);
}

/** Adds exports that `jvm` shim should re-export. */
Expand Down Expand Up @@ -262,8 +261,8 @@ private void copyJDKLibraries(Path jdkLibDir) {
DebugContext debug = accessImpl.getDebugContext();
try (Scope s = debug.scope("copy");
Indent i = debug.logAndIndent("from: %s", jdkLibDir)) {
for (String libname : new TreeSet<>(jniRegistrationSupportSingleton.registeredLibraries.keySet())) {
if (jniRegistrationSupportSingleton.baseLayerRegisteredLibraries.contains(libname)) {
for (String libname : new TreeSet<>(jniRegistrationSupportSingleton.currentLayerRegisteredLibraries)) {
if (jniRegistrationSupportSingleton.prevLayerRegisteredLibraries.contains(libname)) {
/* Skip libraries copied in the base layer. */
debug.log(DebugContext.INFO_LEVEL, "%s: SKIPPED", libname);
continue;
Expand Down Expand Up @@ -372,8 +371,8 @@ private Path getImageImportLib() {
}

private static final class JNIRegistrationSupportSingleton implements LayeredImageSingleton {
private final ConcurrentMap<String, Boolean> registeredLibraries = new ConcurrentHashMap<>();
private final Set<String> baseLayerRegisteredLibraries = new HashSet<>();
private final List<String> currentLayerRegisteredLibraries = new CopyOnWriteArrayList<>();
private final List<String> prevLayerRegisteredLibraries = new ArrayList<>();

public static JNIRegistrationSupportSingleton singleton() {
return ImageSingletons.lookup(JNIRegistrationSupportSingleton.class);
Expand All @@ -387,15 +386,15 @@ public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
@Override
public PersistFlags preparePersist(ImageSingletonWriter writer) {
var snapshotWriter = ((SVMImageLayerWriter.ImageSingletonWriterImpl) writer).getSnapshotBuilder();
SVMImageLayerWriter.initStringList(snapshotWriter::initRegisteredJNILibraries, registeredLibraries.keySet().stream());
SVMImageLayerWriter.initStringList(snapshotWriter::initRegisteredJNILibraries, currentLayerRegisteredLibraries.stream());
return PersistFlags.CREATE;
}

@SuppressWarnings("unused")
public static Object createFromLoader(ImageSingletonLoader loader) {
JNIRegistrationSupportSingleton singleton = new JNIRegistrationSupportSingleton();
var snapshotReader = ((SVMImageLayerSingletonLoader.ImageSingletonLoaderImpl) loader).getSnapshotReader();
SVMImageLayerLoader.streamStrings(snapshotReader.getRegisteredJNILibraries()).forEach(singleton.baseLayerRegisteredLibraries::add);
SVMImageLayerLoader.streamStrings(snapshotReader.getRegisteredJNILibraries()).forEach(singleton.prevLayerRegisteredLibraries::add);
return singleton;
}
}
Expand Down

0 comments on commit 084aacd

Please sign in to comment.