Skip to content

Commit a7b524e

Browse files
Strip debug sections unconditionally.
1 parent e9e411e commit a7b524e

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
3030

31-
import jdk.graal.compiler.debug.DebugContext;
32-
import jdk.graal.compiler.debug.DebugContext.Builder;
33-
import jdk.graal.compiler.debug.Indent;
34-
import jdk.graal.compiler.printer.GraalDebugHandlersFactory;
31+
import org.graalvm.nativeimage.Platform;
32+
import org.graalvm.nativeimage.impl.InternalPlatform;
3533

3634
import com.oracle.graal.pointsto.util.GraalAccess;
3735
import com.oracle.objectfile.ObjectFile;
@@ -47,12 +45,18 @@
4745
import com.oracle.svm.hosted.c.util.FileUtils;
4846
import com.oracle.svm.util.LogUtils;
4947

48+
import jdk.graal.compiler.core.common.SuppressFBWarnings;
49+
import jdk.graal.compiler.debug.DebugContext;
50+
import jdk.graal.compiler.debug.DebugContext.Builder;
51+
import jdk.graal.compiler.debug.Indent;
52+
import jdk.graal.compiler.printer.GraalDebugHandlersFactory;
53+
5054
@AutomaticallyRegisteredFeature
5155
public class NativeImageDebugInfoStripFeature implements InternalFeature {
5256

5357
@Override
5458
public boolean isInConfiguration(IsInConfigurationAccess access) {
55-
return SubstrateOptions.useDebugInfoGeneration() && SubstrateOptions.StripDebugInfo.getValue();
59+
return SubstrateOptions.StripDebugInfo.getValue();
5660
}
5761

5862
@SuppressWarnings("try")
@@ -77,13 +81,17 @@ public void afterImageWrite(AfterImageWriteAccess access) {
7781
}
7882
}
7983

84+
@SuppressFBWarnings(value = "", justification = "FB reports null pointer dereferencing although it is not possible in this case.")
8085
private static void stripLinux(AfterImageWriteAccessImpl accessImpl) {
8186
String objcopyExe = "objcopy";
8287
String debugExtension = ".debug";
8388
Path imagePath = accessImpl.getImagePath();
89+
if (imagePath == null) {
90+
assert !Platform.includedIn(InternalPlatform.NATIVE_ONLY.class);
91+
return;
92+
}
93+
8494
Path imageName = imagePath.getFileName();
85-
Path outputDirectory = imagePath.getParent();
86-
String debugInfoName = imageName + debugExtension;
8795
boolean objcopyAvailable = false;
8896
try {
8997
objcopyAvailable = FileUtils.executeCommand(objcopyExe, "--version") == 0;
@@ -94,16 +102,21 @@ private static void stripLinux(AfterImageWriteAccessImpl accessImpl) {
94102
}
95103

96104
if (!objcopyAvailable) {
97-
LogUtils.warning("%s not available. Skipping generation of separate debuginfo file %s, debuginfo will remain embedded in the executable.", objcopyExe, debugInfoName);
105+
LogUtils.warning("%s not available. The debuginfo will remain embedded in the executable.", objcopyExe);
98106
} else {
99107
try {
108+
Path outputDirectory = imagePath.getParent();
100109
String imageFilePath = outputDirectory.resolve(imageName).toString();
101-
Path debugInfoFilePath = outputDirectory.resolve(debugInfoName);
102-
FileUtils.executeCommand(objcopyExe, "--only-keep-debug", imageFilePath, debugInfoFilePath.toString());
103-
BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, debugInfoFilePath);
110+
if (SubstrateOptions.useDebugInfoGeneration()) {
111+
/* Generate a separate debug file before stripping the executable. */
112+
String debugInfoName = imageName + debugExtension;
113+
Path debugInfoFilePath = outputDirectory.resolve(debugInfoName);
114+
FileUtils.executeCommand(objcopyExe, "--only-keep-debug", imageFilePath, debugInfoFilePath.toString());
115+
BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, debugInfoFilePath);
116+
FileUtils.executeCommand(objcopyExe, "--add-gnu-debuglink=" + debugInfoFilePath, imageFilePath);
117+
}
104118
Path exportedSymbolsPath = createKeepSymbolsListFile(accessImpl);
105119
FileUtils.executeCommand(objcopyExe, "--strip-all", "--keep-symbols=" + exportedSymbolsPath, imageFilePath);
106-
FileUtils.executeCommand(objcopyExe, "--add-gnu-debuglink=" + debugInfoFilePath, imageFilePath);
107120
} catch (IOException e) {
108121
throw UserError.abort("Generation of separate debuginfo file failed", e);
109122
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)