From 51fc742ce716a09359f4f87ffe3876cb7ff9479d Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Mon, 12 Jul 2021 15:46:20 -0700 Subject: [PATCH] [Driver] Let -fno-integrated-as -gdwarf-5 use -fdwarf-directory-asm While GNU as only allows the directory form of the .file directive for DWARF v5, the integrated assembler prefers the directory form on all DWARF versions (-fdwarf-directory-asm). We currently set CC1 -fno-dwarf-directory-asm for -fno-integrated-as -gdwarf-5 which may cause the directory entry 0 and the filename entry 0 to be incorrect (see D105662 and the example below). This patch makes -fno-integrated-as -gdwarf-5 use -fdwarf-directory-asm as well. ``` cd /tmp/c before % clang -g -gdwarf-5 -fno-integrated-as e/a.c -S -o - | grep '\.file.*0' .file 0 "/tmp/c/e/a.c" md5 0x97e31cee64b4e58a4af8787512d735b6 % clang -g -gdwarf-5 -fno-integrated-as e/a.c -c % llvm-dwarfdump a.o | grep include_directories include_directories[ 0] = "/tmp/c/e" after % clang -g -gdwarf-5 -fno-integrated-as e/a.c -S -o - | grep '\.file.*0' .file 0 "/tmp/c" "e/a.c" md5 0x97e31cee64b4e58a4af8787512d735b6 % clang -g -gdwarf-5 -fno-integrated-as e/a.c -c % llvm-dwarfdump a.o | grep include_directories include_directories[ 0] = "/tmp/c" ``` Reviewed By: #debug-info, dblaikie, osandov Differential Revision: https://reviews.llvm.org/D105835 --- clang/lib/Driver/ToolChains/Clang.cpp | 19 ++++++++----------- clang/test/Driver/debug-options.c | 11 +++++++++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 85204ceaa49a..4336a25f091c 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -490,14 +490,6 @@ static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC, Default); } -static bool ShouldDisableDwarfDirectory(const ArgList &Args, - const ToolChain &TC) { - bool UseDwarfDirectory = - Args.hasFlag(options::OPT_fdwarf_directory_asm, - options::OPT_fno_dwarf_directory_asm, TC.useIntegratedAs()); - return !UseDwarfDirectory; -} - // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases // to the corresponding DebugInfoKind. static codegenoptions::DebugInfoKind DebugLevelToInfoKind(const Arg &A) { @@ -4175,6 +4167,14 @@ static void renderDebugOptions(const ToolChain &TC, const Driver &D, } } + // To avoid join/split of directory+filename, the integrated assembler prefers + // the directory form of .file on all DWARF versions. GNU as doesn't allow the + // form before DWARF v5. + if (!Args.hasFlag(options::OPT_fdwarf_directory_asm, + options::OPT_fno_dwarf_directory_asm, + TC.useIntegratedAs() || EffectiveDWARFVersion >= 5)) + CmdArgs.push_back("-fno-dwarf-directory-asm"); + // Decide how to render forward declarations of template instantiations. // SCE wants full descriptions, others just get them in the name. if (DebuggerTuning == llvm::DebuggerKind::SCE) @@ -5506,9 +5506,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fno-gnu-keywords"); } - if (ShouldDisableDwarfDirectory(Args, TC)) - CmdArgs.push_back("-fno-dwarf-directory-asm"); - if (!ShouldEnableAutolink(Args, TC, JA)) CmdArgs.push_back("-fno-autolink"); diff --git a/clang/test/Driver/debug-options.c b/clang/test/Driver/debug-options.c index 54a8a6a3f74a..3b1f1d555bbc 100644 --- a/clang/test/Driver/debug-options.c +++ b/clang/test/Driver/debug-options.c @@ -424,3 +424,14 @@ // GDWARF64_VER: error: invalid argument '-gdwarf64' only allowed with 'DWARFv3 or greater' // GDWARF64_32ARCH: error: invalid argument '-gdwarf64' only allowed with '64 bit architecture' // GDWARF64_ELF: error: invalid argument '-gdwarf64' only allowed with 'ELF platforms' + +/// Default to -fno-dwarf-directory-asm for -fno-integrated-as before DWARF v5. +// RUN: %clang -### -target x86_64 -c -gdwarf-2 %s 2>&1 | FileCheck --check-prefix=DIRECTORY %s +// RUN: %clang -### -target x86_64 -c -gdwarf-5 %s 2>&1 | FileCheck --check-prefix=DIRECTORY %s +// RUN: %clang -### -target x86_64 -c -gdwarf-4 -fno-integrated-as %s 2>&1 | FileCheck --check-prefix=NODIRECTORY %s +// RUN: %clang -### -target x86_64 -c -gdwarf-5 -fno-integrated-as %s 2>&1 | FileCheck --check-prefix=DIRECTORY %s + +// RUN: %clang -### -target x86_64 -c -gdwarf-4 -fno-dwarf-directory-asm %s 2>&1 | FileCheck --check-prefix=NODIRECTORY %s + +// DIRECTORY-NOT: "-fno-dwarf-directory-asm" +// NODIRECTORY: "-fno-dwarf-directory-asm"