Skip to content

Commit 6551ac7

Browse files
committed
[Driver] Make -static-libgcc imply static libunwind
In the GNU toolchain, `-static-libgcc` implies that the unwindlib will be linked statically. However, when `--unwindlib=libunwind`, this flag is ignored, and a bare `-lunwind` is added to the linker args. Unfortunately, this means that if both `libunwind.so`, and `libunwind.a` are present in the library path, `libunwind.so` will be chosen in all cases where `-static` is not set. This change makes `-static-libgcc` affect the `-l` flag produced by `--unwindlib=libunwind`. After this patch, providing `-static-libgcc --unwindlib=libunwind` will cause the driver to explicitly emit `-l:libunwind.a` to statically link libunwind. For all other cases it will emit `-l:libunwind.so` matching current behavior with a more explicit link line. https://reviews.llvm.org/D70416
1 parent 1668685 commit 6551ac7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,10 @@ static void AddUnwindLibrary(const ToolChain &TC, const Driver &D,
12091209
break;
12101210
}
12111211
case ToolChain::UNW_CompilerRT:
1212-
CmdArgs.push_back("-lunwind");
1212+
if (LGT == LibGccType::StaticLibGcc)
1213+
CmdArgs.push_back("-l:libunwind.a");
1214+
else
1215+
CmdArgs.push_back("-l:libunwind.so");
12131216
break;
12141217
}
12151218

clang/test/Driver/compiler-rt-unwind.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
// RUN: --gcc-toolchain="" \
1414
// RUN: | FileCheck --check-prefix=RTLIB-GCC-UNWINDLIB-COMPILER-RT %s
1515
// RTLIB-GCC-UNWINDLIB-COMPILER-RT: "{{.*}}lgcc"
16-
// RTLIB-GCC-UNWINDLIB-COMPILER-RT: "{{.*}}lunwind"
16+
// RTLIB-GCC-UNWINDLIB-COMPILER-RT: "{{.*}}l:libunwind.so"
17+
//
18+
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
19+
// RUN: --target=x86_64-unknown-linux -rtlib=libgcc --unwindlib=libunwind \
20+
// RUN: -static-libgcc \
21+
// RUN: --gcc-toolchain="" \
22+
// RUN: | FileCheck --check-prefix=RTLIB-GCC-STATIC-UNWINDLIB-COMPILER-RT %s
23+
// RTLIB-GCC-STATIC-UNWINDLIB-COMPILER-RT: "{{.*}}lgcc"
24+
// RTLIB-GCC-STATIC-UNWINDLIB-COMPILER-RT: "{{.*}}l:libunwind.a"
1725
//
1826
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
1927
// RUN: --target=x86_64-unknown-linux -rtlib=compiler-rt \

0 commit comments

Comments
 (0)