Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional native flag to build PIE binaries #33931

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ public interface NativeConfig {
*/
Optional<Boolean> containerBuild();

/**
* Explicit configuration option to generate a native Position Independent Executable (PIE) for Linux.
* If the system supports PIE generation, the default behaviour is to disable it for
* <a href="https://www.redhat.com/en/blog/position-independent-executable-pie-performance">performance reasons</a>.
* However, some systems can only run position-independent executables,
* so this option enables the generation of such native executables.
*/
Optional<Boolean> pie();

/**
* If this build is done using a remote docker daemon.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,15 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
Path outputDir = nativeImageSourceJarBuildItem.getPath().getParent();
final String runnerJarName = runnerJar.getFileName().toString();

String noPIE = "";
String pie = "";

boolean isContainerBuild = nativeImageRunner.isContainerBuild();
if (!isContainerBuild && SystemUtils.IS_OS_LINUX) {
noPIE = detectNoPIE();
if (nativeConfig.pie().isPresent() && nativeConfig.pie().get()) {
pie = detectPIE();
} else {
pie = detectNoPIE();
}
}

String nativeImageName = getNativeImageName(outputTargetBuildItem, packageConfig);
Expand Down Expand Up @@ -242,7 +246,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
.setOutputDir(outputDir)
.setRunnerJarName(runnerJarName)
.setNativeImageName(nativeImageName)
.setNoPIE(noPIE)
.setPIE(pie)
.setGraalVMVersion(graalVMVersion)
.setNativeImageFeatures(nativeImageFeatures)
.setContainerBuild(isContainerBuild)
Expand Down Expand Up @@ -522,6 +526,10 @@ private static String detectNoPIE() {
return argument.length() == 0 ? testGCCArgument("-nopie") : argument;
}

private static String detectPIE() {
return testGCCArgument("-pie");
}

private static String testGCCArgument(String argument) {
try {
Process gcc = new ProcessBuilder("cc", "-v", "-E", argument, "-").start();
Expand Down Expand Up @@ -562,7 +570,7 @@ static class Builder {
private List<NativeImageFeatureBuildItem> nativeImageFeatures;
private Path outputDir;
private String runnerJarName;
private String noPIE = "";
private String pie = "";
private GraalVM.Version graalVMVersion = null;
private String nativeImageName;
private boolean classpathIsBroken;
Expand Down Expand Up @@ -646,8 +654,8 @@ public Builder setRunnerJarName(String runnerJarName) {
return this;
}

public Builder setNoPIE(String noPIE) {
this.noPIE = noPIE;
public Builder setPIE(String pie) {
this.pie = pie;
return this;
}

Expand Down Expand Up @@ -857,8 +865,8 @@ public NativeImageInvokerInfo build() {
if (!inlineBeforeAnalysis) {
nativeImageArgs.add("-H:-InlineBeforeAnalysis");
}
if (!noPIE.isEmpty()) {
nativeImageArgs.add("-H:NativeLinkerOption=" + noPIE);
if (!pie.isEmpty()) {
nativeImageArgs.add("-H:NativeLinkerOption=" + pie);
}

if (!nativeConfig.enableIsolates()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public Optional<Boolean> containerBuild() {
return Optional.empty();
}

@Override
public Optional<Boolean> pie() {
return Optional.empty();
}

@Override
public boolean remoteContainerBuild() {
return false;
Expand Down