Skip to content

Commit cc4bda0

Browse files
committed
Do not add redundant -pie when building static executables on Linux platforms
It otherwise results in a warning from the Clang linker driver. Resolves rdar://143793051
1 parent a5b8982 commit cc4bda0

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ extension GenericUnixToolchain {
8686
}
8787
}
8888

89+
let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
90+
negative: .noStaticStdlib,
91+
default: false)
92+
let staticExecutable = parsedOptions.hasFlag(positive: .staticExecutable,
93+
negative: .noStaticExecutable,
94+
default: false)
8995
let clangTool: Tool = cxxCompatEnabled ? .clangxx : .clang
9096
var clangPath = try getToolPath(clangTool)
9197
if let toolsDirPath = parsedOptions.getLastArgument(.toolsDirectory) {
@@ -105,7 +111,7 @@ extension GenericUnixToolchain {
105111
}
106112

107113
// Executables on Linux get -pie
108-
if targetTriple.os == .linux && linkerOutputType == .executable {
114+
if targetTriple.os == .linux && linkerOutputType == .executable && !staticExecutable {
109115
commandLine.appendFlag("-pie")
110116
}
111117

@@ -132,12 +138,6 @@ extension GenericUnixToolchain {
132138
}
133139
}
134140

135-
let staticStdlib = parsedOptions.hasFlag(positive: .staticStdlib,
136-
negative: .noStaticStdlib,
137-
default: false)
138-
let staticExecutable = parsedOptions.hasFlag(positive: .staticExecutable,
139-
negative: .noStaticExecutable,
140-
default: false)
141141
let isEmbeddedEnabled = parsedOptions.isEmbeddedEnabled
142142

143143
let toolchainStdlibRpath = parsedOptions

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,45 @@ final class SwiftDriverTests: XCTestCase {
20562056
]))
20572057
}
20582058

2059+
do {
2060+
try withTemporaryDirectory { path in
2061+
try localFileSystem.writeFileContents(path.appending(components: "linux", "static-executable-args.lnk")) {
2062+
$0.send("empty")
2063+
}
2064+
// Ensure that when building a static executable on Linux we do not pass in
2065+
// a redundant '-pie'
2066+
var driver = try Driver(args: commonArgs + ["-emit-executable", "-L", "/tmp", "-Xlinker", "--export-all",
2067+
"-Xlinker", "-E", "-Xclang-linker", "foo",
2068+
"-resource-dir", path.pathString,
2069+
"-static-executable",
2070+
"-target", "x86_64-unknown-linux"], env: env)
2071+
let plannedJobs = try driver.planBuild()
2072+
XCTAssertEqual(plannedJobs.count, 4)
2073+
let linkJob = plannedJobs[3]
2074+
let cmd = linkJob.commandLine
2075+
XCTAssertFalse(cmd.contains(.flag("-pie")))
2076+
}
2077+
2078+
}
2079+
2080+
do {
2081+
try withTemporaryDirectory { path in
2082+
try localFileSystem.writeFileContents(path.appending(components: "linux", "static-executable-args.lnk")) {
2083+
$0.send("empty")
2084+
}
2085+
// Ensure that when building a non-static executable on Linux, we specify '-pie'
2086+
var driver = try Driver(args: commonArgs + ["-emit-executable", "-L", "/tmp", "-Xlinker", "--export-all",
2087+
"-Xlinker", "-E", "-Xclang-linker", "foo",
2088+
"-resource-dir", path.pathString,
2089+
"-target", "x86_64-unknown-linux"], env: env)
2090+
let plannedJobs = try driver.planBuild()
2091+
XCTAssertEqual(plannedJobs.count, 4)
2092+
let linkJob = plannedJobs[3]
2093+
let cmd = linkJob.commandLine
2094+
XCTAssertTrue(cmd.contains(.flag("-pie")))
2095+
}
2096+
}
2097+
20592098
do {
20602099
// Xlinker flags
20612100
// Ensure that Xlinker flags are passed as such to the clang linker invocation.

0 commit comments

Comments
 (0)