Skip to content

Commit 084aacd

Browse files
Automatic merge of master into galahad
2 parents 4765c77 + 8d4cfcb commit 084aacd

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/NativeLibraries.java

+4
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ public void addStaticJniLibrary(String library, String... dependencies) {
446446
List<String> allDeps = new ArrayList<>(Arrays.asList(dependencies));
447447
/* "jvm" is a basic dependence for static JNI libs */
448448
allDeps.add("jvm");
449+
if (library.equals("nio")) {
450+
/* "nio" implicitly depends on "net" */
451+
allDeps.add("net");
452+
}
449453
dependencyGraph.add(library, allDeps);
450454
}
451455

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,18 @@ protected void setOutputKind(List<String> cmd) {
340340
@Override
341341
protected List<String> getLibrariesCommand() {
342342
List<String> cmd = new ArrayList<>();
343+
String pushState = "-Wl,--push-state";
344+
String popState = "-Wl,--pop-state";
343345
if (customStaticLibs) {
344-
cmd.add("-Wl,--push-state");
346+
cmd.add(pushState);
345347
}
348+
String previousLayerLib = null;
346349
for (String lib : libs) {
347350
String linkingMode = null;
348-
if (ImageLayerBuildingSupport.buildingImageLayer() && HostedDynamicLayerInfo.singleton().isImageLayerLib(lib)) {
349-
linkingMode = "dynamic";
351+
if (ImageLayerBuildingSupport.buildingExtensionLayer() && HostedDynamicLayerInfo.singleton().isImageLayerLib(lib)) {
352+
VMError.guarantee(!lib.isEmpty());
353+
VMError.guarantee(previousLayerLib == null, "We currently only support one previous layer."); // GR-58631
354+
previousLayerLib = lib;
350355
} else if (dynamicLibC) {
351356
linkingMode = LIB_C_NAMES.contains(lib) ? "dynamic" : "static";
352357
} else if (staticLibCpp) {
@@ -358,7 +363,14 @@ protected List<String> getLibrariesCommand() {
358363
cmd.add("-l" + lib);
359364
}
360365
if (customStaticLibs) {
361-
cmd.add("-Wl,--pop-state");
366+
cmd.add(popState);
367+
}
368+
369+
if (previousLayerLib != null) {
370+
cmd.add(pushState);
371+
cmd.add("-Wl,-Bdynamic");
372+
cmd.add("-l" + previousLayerLib);
373+
cmd.add(popState);
362374
}
363375

364376
// Make sure libgcc gets statically linked

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JNIRegistrationSupport.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@
3131
import java.nio.file.Files;
3232
import java.nio.file.NoSuchFileException;
3333
import java.nio.file.Path;
34+
import java.util.ArrayList;
3435
import java.util.Collection;
3536
import java.util.EnumSet;
36-
import java.util.HashSet;
3737
import java.util.List;
38-
import java.util.Set;
3938
import java.util.SortedMap;
4039
import java.util.SortedSet;
4140
import java.util.TreeMap;
4241
import java.util.TreeSet;
43-
import java.util.concurrent.ConcurrentHashMap;
44-
import java.util.concurrent.ConcurrentMap;
42+
import java.util.concurrent.CopyOnWriteArrayList;
4543
import java.util.stream.Stream;
4644

4745
import org.graalvm.nativeimage.ImageSingletons;
@@ -127,7 +125,7 @@ public void afterAnalysis(AfterAnalysisAccess access) {
127125
isSunMSCAPIProviderReachable = optSunMSCAPIClass.isPresent() && access.isReachable(optSunMSCAPIClass.get());
128126
}
129127
if (ImageLayerBuildingSupport.buildingExtensionLayer()) {
130-
for (String library : jniRegistrationSupportSingleton.baseLayerRegisteredLibraries) {
128+
for (String library : jniRegistrationSupportSingleton.prevLayerRegisteredLibraries) {
131129
addLibrary(library);
132130
}
133131
}
@@ -159,7 +157,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
159157
}
160158

161159
void registerLibrary(String libname) {
162-
if (libname != null && jniRegistrationSupportSingleton.registeredLibraries.putIfAbsent(libname, Boolean.TRUE) == null) {
160+
if (libname != null && !jniRegistrationSupportSingleton.currentLayerRegisteredLibraries.contains(libname)) {
161+
jniRegistrationSupportSingleton.currentLayerRegisteredLibraries.add(libname);
163162
addLibrary(libname);
164163
}
165164
}
@@ -175,7 +174,7 @@ private void addLibrary(String libname) {
175174
}
176175

177176
public boolean isRegisteredLibrary(String libname) {
178-
return jniRegistrationSupportSingleton.registeredLibraries.containsKey(libname);
177+
return jniRegistrationSupportSingleton.currentLayerRegisteredLibraries.contains(libname);
179178
}
180179

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

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

378377
public static JNIRegistrationSupportSingleton singleton() {
379378
return ImageSingletons.lookup(JNIRegistrationSupportSingleton.class);
@@ -387,15 +386,15 @@ public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
387386
@Override
388387
public PersistFlags preparePersist(ImageSingletonWriter writer) {
389388
var snapshotWriter = ((SVMImageLayerWriter.ImageSingletonWriterImpl) writer).getSnapshotBuilder();
390-
SVMImageLayerWriter.initStringList(snapshotWriter::initRegisteredJNILibraries, registeredLibraries.keySet().stream());
389+
SVMImageLayerWriter.initStringList(snapshotWriter::initRegisteredJNILibraries, currentLayerRegisteredLibraries.stream());
391390
return PersistFlags.CREATE;
392391
}
393392

394393
@SuppressWarnings("unused")
395394
public static Object createFromLoader(ImageSingletonLoader loader) {
396395
JNIRegistrationSupportSingleton singleton = new JNIRegistrationSupportSingleton();
397396
var snapshotReader = ((SVMImageLayerSingletonLoader.ImageSingletonLoaderImpl) loader).getSnapshotReader();
398-
SVMImageLayerLoader.streamStrings(snapshotReader.getRegisteredJNILibraries()).forEach(singleton.baseLayerRegisteredLibraries::add);
397+
SVMImageLayerLoader.streamStrings(snapshotReader.getRegisteredJNILibraries()).forEach(singleton.prevLayerRegisteredLibraries::add);
399398
return singleton;
400399
}
401400
}

0 commit comments

Comments
 (0)