Skip to content

Commit 9d117e7

Browse files
committed
Define __GCC_HAVE_DWARF2_CFI_ASM if applicable
In -fno-exceptions -fno-asynchronous-unwind-tables -g0 mode, GCC does not emit `.cfi_*` directives. ``` % diff <(gcc -fno-asynchronous-unwind-tables -dM -E a.c) <(gcc -dM -E a.c) 130a131 > #define __GCC_HAVE_DWARF2_CFI_ASM 1 ``` This macro is useful because code can decide whether inline asm should include `.cfi_*` directives. `.cfi_*` directives without `.cfi_startproc` can cause assembler errors (integrated assembler: `this directive must appear between .cfi_startproc and .cfi_endproc directives`). Differential Revision: https://reviews.llvm.org/D97743
1 parent bcb5399 commit 9d117e7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
404404
/// master flag, -fexceptions and also language specific flags to enable/disable
405405
/// C++ and Objective-C exceptions. This makes it possible to for example
406406
/// disable C++ exceptions but enable Objective-C exceptions.
407-
static void addExceptionArgs(const ArgList &Args, types::ID InputType,
407+
static bool addExceptionArgs(const ArgList &Args, types::ID InputType,
408408
const ToolChain &TC, bool KernelOrKext,
409409
const ObjCRuntime &objcRuntime,
410410
ArgStringList &CmdArgs) {
@@ -419,7 +419,7 @@ static void addExceptionArgs(const ArgList &Args, types::ID InputType,
419419
Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
420420
Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
421421
Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
422-
return;
422+
return false;
423423
}
424424

425425
// See if the user explicitly enabled exceptions.
@@ -462,6 +462,7 @@ static void addExceptionArgs(const ArgList &Args, types::ID InputType,
462462

463463
if (EH)
464464
CmdArgs.push_back("-fexceptions");
465+
return EH;
465466
}
466467

467468
static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
@@ -4971,14 +4972,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
49714972
// This is a coarse approximation of what llvm-gcc actually does, both
49724973
// -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
49734974
// complicated ways.
4974-
bool AsynchronousUnwindTables =
4975+
bool UnwindTables =
49754976
Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
49764977
options::OPT_fno_asynchronous_unwind_tables,
49774978
(TC.IsUnwindTablesDefault(Args) ||
49784979
TC.getSanitizerArgs().needsUnwindTables()) &&
49794980
!Freestanding);
4980-
if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
4981-
AsynchronousUnwindTables))
4981+
UnwindTables = Args.hasFlag(options::OPT_funwind_tables,
4982+
options::OPT_fno_unwind_tables, UnwindTables);
4983+
if (UnwindTables)
49824984
CmdArgs.push_back("-munwind-tables");
49834985

49844986
// Prepare `-aux-target-cpu` and `-aux-target-feature` unless
@@ -6039,8 +6041,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
60396041
CmdArgs.push_back("-fapplication-extension");
60406042

60416043
// Handle GCC-style exception args.
6044+
bool EH = false;
60426045
if (!C.getDriver().IsCLMode())
6043-
addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
6046+
EH = addExceptionArgs(Args, InputType, TC, KernelOrKext, Runtime, CmdArgs);
60446047

60456048
// Handle exception personalities
60466049
Arg *A = Args.getLastArg(
@@ -6600,6 +6603,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
66006603
!TC.getTriple().isAndroid() && TC.useIntegratedAs()))
66016604
CmdArgs.push_back("-faddrsig");
66026605

6606+
if ((Triple.isOSBinFormatELF() || Triple.isOSBinFormatMachO()) &&
6607+
(EH || UnwindTables || DebugInfoKind != codegenoptions::NoDebugInfo))
6608+
CmdArgs.push_back("-D__GCC_HAVE_DWARF2_CFI_ASM=1");
6609+
66036610
if (Arg *A = Args.getLastArg(options::OPT_fsymbol_partition_EQ)) {
66046611
std::string Str = A->getAsString(Args);
66056612
if (!TC.getTriple().isOSBinFormatELF())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang %s -dM -E -target x86_64-windows | FileCheck %s --check-prefix=NO
2+
// RUN: %clang %s -dM -E -target x86_64 -fno-asynchronous-unwind-tables | FileCheck %s --check-prefix=NO
3+
4+
// RUN: %clang %s -dM -E -target x86_64 | FileCheck %s
5+
// RUN: %clang %s -dM -E -target aarch64-apple-darwin | FileCheck %s
6+
// RUN: %clang %s -dM -E -target x86_64 -fno-asynchronous-unwind-tables -g | FileCheck %s
7+
// RUN: %clang %s -dM -E -target x86_64 -fno-asynchronous-unwind-tables -fexceptions | FileCheck %s
8+
9+
// NO-NOT: #define __GCC_HAVE_DWARF2_CFI_ASM
10+
// CHECK: #define __GCC_HAVE_DWARF2_CFI_ASM 1

0 commit comments

Comments
 (0)