-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...m.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageDebugInfoStripFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
package com.oracle.svm.hosted.image; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.graalvm.compiler.debug.DebugContext; | ||
import org.graalvm.compiler.debug.DebugContext.Builder; | ||
import org.graalvm.compiler.debug.Indent; | ||
import org.graalvm.compiler.printer.GraalDebugHandlersFactory; | ||
|
||
import com.oracle.graal.pointsto.util.GraalAccess; | ||
import com.oracle.objectfile.ObjectFile; | ||
import com.oracle.svm.core.BuildArtifacts; | ||
import com.oracle.svm.core.BuildArtifacts.ArtifactType; | ||
import com.oracle.svm.core.SubstrateOptions; | ||
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; | ||
import com.oracle.svm.core.feature.InternalFeature; | ||
import com.oracle.svm.core.option.HostedOptionValues; | ||
import com.oracle.svm.core.option.SubstrateOptionsParser; | ||
import com.oracle.svm.core.util.InterruptImageBuilding; | ||
import com.oracle.svm.core.util.UserError; | ||
import com.oracle.svm.hosted.FeatureImpl.AfterImageWriteAccessImpl; | ||
import com.oracle.svm.hosted.c.util.FileUtils; | ||
|
||
@AutomaticallyRegisteredFeature | ||
public class NativeImageDebugInfoStripFeature implements InternalFeature { | ||
|
||
@Override | ||
public boolean isInConfiguration(IsInConfigurationAccess access) { | ||
return SubstrateOptions.GenerateDebugInfo.getValue() > 0 && SubstrateOptions.StripDebugInfo.getValue(); | ||
} | ||
|
||
@SuppressWarnings("try") | ||
@Override | ||
public void afterImageWrite(AfterImageWriteAccess access) { | ||
AfterImageWriteAccessImpl accessImpl = (AfterImageWriteAccessImpl) access; | ||
DebugContext debugContext = new Builder(HostedOptionValues.singleton(), new GraalDebugHandlersFactory(GraalAccess.getOriginalSnippetReflection())).build(); | ||
try (Indent indent = debugContext.logAndIndent("Stripping debuginfo")) { | ||
switch (ObjectFile.getNativeFormat()) { | ||
case ELF: | ||
stripLinux(accessImpl); | ||
break; | ||
case MACH_O: | ||
case PECOFF: | ||
if (SubstrateOptions.StripDebugInfo.hasBeenSet()) { | ||
System.out.println("Warning: Using " + SubstrateOptionsParser.commandArgument(SubstrateOptions.StripDebugInfo, "+") + " not supported on macOS and Windows"); | ||
} | ||
break; | ||
default: | ||
throw UserError.abort("Unsupported object file format"); | ||
} | ||
} | ||
} | ||
|
||
private static void stripLinux(AfterImageWriteAccessImpl accessImpl) { | ||
String objcopyExe = "objcopy"; | ||
String debugExtension = ".debug"; | ||
Path imagePath = accessImpl.getImagePath(); | ||
Path imageName = imagePath.getFileName(); | ||
Path outputDirectory = imagePath.getParent(); | ||
String debugInfoName = imageName + debugExtension; | ||
boolean objcopyAvailable = false; | ||
try { | ||
objcopyAvailable = FileUtils.executeCommand(objcopyExe, "--version") == 0; | ||
} catch (IOException e) { | ||
/* Fall through to `if (!objcopyAvailable)` */ | ||
} catch (InterruptedException e) { | ||
throw new InterruptImageBuilding("Interrupted during checking for " + objcopyExe + " availability"); | ||
} | ||
|
||
if (!objcopyAvailable) { | ||
System.out.printf("Warning: %s not available. Skip generation of debuginfo file %s%n", objcopyExe, debugInfoName); | ||
} else { | ||
try { | ||
String imageFilePath = outputDirectory.resolve(imageName).toString(); | ||
Path debugInfoFilePath = outputDirectory.resolve(debugInfoName); | ||
FileUtils.executeCommand(objcopyExe, "--only-keep-debug", imageFilePath, debugInfoFilePath.toString()); | ||
BuildArtifacts.singleton().add(ArtifactType.DEBUG_INFO, debugInfoFilePath); | ||
Path exportedSymbolsPath = createKeepSymbolsListFile(accessImpl); | ||
FileUtils.executeCommand(objcopyExe, "--strip-all", "--keep-symbols=" + exportedSymbolsPath, imageFilePath); | ||
FileUtils.executeCommand(objcopyExe, "--add-gnu-debuglink=" + debugInfoFilePath, imageFilePath); | ||
} catch (IOException e) { | ||
throw UserError.abort("Generation of separate debuginfo file failed", e); | ||
} catch (InterruptedException e) { | ||
throw new InterruptImageBuilding("Interrupted during debuginfo file splitting of image " + imageName); | ||
} | ||
} | ||
} | ||
|
||
private static Path createKeepSymbolsListFile(AfterImageWriteAccessImpl accessImpl) throws IOException { | ||
Path exportedSymbolsPath = accessImpl.getTempDirectory().resolve("keep-symbols.list").toAbsolutePath(); | ||
Files.write(exportedSymbolsPath, accessImpl.getImageSymbols(false)); | ||
return exportedSymbolsPath; | ||
} | ||
} |