Skip to content

Commit 5d39162

Browse files
authored
Merge pull request #31244 from drexin/wip-backport-static-linking
[5.2] Fix linker flags for -static-executable
2 parents 679273a + dfa94e2 commit 5d39162

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

lib/Driver/UnixToolChains.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
189189
Arguments.push_back(context.Args.MakeArgString(A->getValue()));
190190
}
191191

192-
if (getTriple().getOS() == llvm::Triple::Linux &&
193-
job.getKind() == LinkKind::Executable) {
192+
if (getTriple().getObjectFormat() == llvm::Triple::ELF &&
193+
job.getKind() == LinkKind::Executable &&
194+
!context.Args.hasFlag(options::OPT_static_executable,
195+
options::OPT_no_static_executable, false)) {
194196
Arguments.push_back("-pie");
195197
}
196198

test/Driver/static-executable.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Create a self contained binary
2+
// REQUIRES: OS=linux-gnu
3+
// REQUIRES: static_stdlib
4+
print("hello world!")
5+
// RUN: %empty-directory(%t)
6+
// RUN: %target-swiftc_driver -static-executable -o %t/static-executable %s
7+
// RUN: %t/static-executable | %FileCheck %s
8+
// RUN: %llvm-readelf -program-headers %t/static-executable | %FileCheck %s --check-prefix=ELF
9+
// CHECK: hello world!
10+
// ELF-NOT: INTERP

test/lit.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ config.llvm_cov = inferSwiftBinary('llvm-cov')
283283
config.llvm_strings = inferSwiftBinary('llvm-strings')
284284
config.filecheck = inferSwiftBinary('FileCheck')
285285
config.llvm_dwarfdump = inferSwiftBinary('llvm-dwarfdump')
286+
config.llvm_readelf = inferSwiftBinary('llvm-readelf')
286287
config.llvm_dis = inferSwiftBinary('llvm-dis')
287288
config.sourcekitd_test = inferSwiftBinary('sourcekitd-test')
288289
config.complete_test = inferSwiftBinary('complete-test')
@@ -419,6 +420,7 @@ config.substitutions.append( ('%swift-indent', config.swift_indent) )
419420
config.substitutions.append( ('%llvm-link', config.llvm_link) )
420421
config.substitutions.append( ('%swift-llvm-opt', config.swift_llvm_opt) )
421422
config.substitutions.append( ('%llvm-dwarfdump', config.llvm_dwarfdump) )
423+
config.substitutions.append( ('%llvm-readelf', config.llvm_readelf) )
422424
config.substitutions.append( ('%llvm-dis', config.llvm_dis) )
423425
config.substitutions.append( ('%swift-demangle-yamldump', config.swift_demangle_yamldump) )
424426
config.substitutions.append( ('%Benchmark_O', config.benchmark_o) )

utils/static-executable-args.lnk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
-Xlinker
99
--defsym=__import_pthread_key_create=pthread_key_create
1010
-lpthread
11-
-licui18n
12-
-licuuc
13-
-licudata
11+
-licui18nswift
12+
-licuucswift
13+
-licudataswift
1414
-ldl
1515
-lstdc++
1616
-lm

0 commit comments

Comments
 (0)