[PATCH 2/2] riscv: Restrict DWARF5 when building with LLVM to known working versions

Fangrui Song maskray at google.com
Fri Dec 1 12:12:26 PST 2023


On Fri, Dec 1, 2023 at 11:49 AM Nathan Chancellor <nathan at kernel.org> wrote:
>
> On Thu, Nov 30, 2023 at 08:16:41AM -0800, Fangrui Song wrote:
> > On Wed, Nov 29, 2023 at 3:30 PM Nathan Chancellor <nathan at kernel.org> wrote:
> > >
> > > LLVM prior to 18.0.0 would generate incorrect debug info for DWARF5 due
> > > to linker relaxation, which was worked around in clang by defaulting
> > > RISC-V to DWARF4 [1]. Unfortunately, this workaround does not work for
> > > the kernel because the DWARF version can be independently changed from
> > > the default in Kconfig.
> > >
> > > Do not allow DWARF5 to be selected for RISC-V when using linker
> > > relaxation (ld.lld >= 15.0.0) and a version of LLVM that does not have
> > > the fixes (the integrated assembler [2] and ld.lld [3] < 18.0.0)
> > > necessary to generate the correct debug info.
> > >
> > > Link: https://github.com/llvm/llvm-project/commit/bbc0f99f3bc96f1db16f649fc21dd18e5b0918f6 [1]
> > > Link: https://github.com/llvm/llvm-project/commit/1df5ea29b43690b6622db2cad7b745607ca4de6a [2]
> > > Link: https://github.com/llvm/llvm-project/commit/7ffabb61a5569444b5ac9322e22e5471cc5e4a77 [3]
> > > Signed-off-by: Nathan Chancellor <nathan at kernel.org>
> > > ---
> > >  arch/riscv/Kconfig | 9 +++++++++
> > >  lib/Kconfig.debug  | 1 +
> > >  2 files changed, 10 insertions(+)
> > >
> > > diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> > > index 72be1d8122a3..81b473cb47b0 100644
> > > --- a/arch/riscv/Kconfig
> > > +++ b/arch/riscv/Kconfig
> > > @@ -186,6 +186,15 @@ config RISCV_USE_LINKER_RELAXATION
> > >         # https://github.com/llvm/llvm-project/commit/6611d58f5bbcbec77262d392e2923e1d680f6985
> > >         depends on !LD_IS_LLD || LLD_VERSION >= 150000
> > >
> > > +# https://github.com/llvm/llvm-project/commit/bbc0f99f3bc96f1db16f649fc21dd18e5b0918f6
> > > +config ARCH_HAS_BROKEN_DWARF5
> > > +       def_bool y
> > > +       depends on RISCV_USE_LINKER_RELAXATION
> > > +       # https://github.com/llvm/llvm-project/commit/1df5ea29b43690b6622db2cad7b745607ca4de6a
> > > +       depends on AS_IS_LLVM && AS_VERSION < 180000
> > > +       # https://github.com/llvm/llvm-project/commit/7ffabb61a5569444b5ac9322e22e5471cc5e4a77
> > > +       depends on LD_IS_LLD && LLD_VERSION < 180000
> > > +
> > >  config ARCH_MMAP_RND_BITS_MIN
> > >         default 18 if 64BIT
> > >         default 8
> > > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> > > index cc7d53d9dc01..a0ebce05a368 100644
> > > --- a/lib/Kconfig.debug
> > > +++ b/lib/Kconfig.debug
> > > @@ -282,6 +282,7 @@ config DEBUG_INFO_DWARF4
> > >  config DEBUG_INFO_DWARF5
> > >         bool "Generate DWARF Version 5 debuginfo"
> > >         select DEBUG_INFO
> > > +       depends on !ARCH_HAS_BROKEN_DWARF5
> > >         depends on !CC_IS_CLANG || AS_IS_LLVM || (AS_IS_GNU && AS_VERSION >= 23502 && AS_HAS_NON_CONST_LEB128)
> > >         help
> > >           Generate DWARF v5 debug info. Requires binutils 2.35.2, gcc 5.0+ (gcc
> > >
> > > --
> > > 2.43.0
> > >
> >
> > A comment and "depends on" condition introduced by commit
> > 0a6de78cff600cb991f2a1b7ed376935871796a0 "lib/Kconfig.debug: Add check
> > for non-constant .{s,u}leb128 support to DWARF5"
> > can now be refined.
> >
> > > ib/Kconfig.debug
> > > (AS_IS_GNU && AS_VERSION >= 23502 && AS_HAS_NON_CONST_LEB128)
> >
> > AS_VERSION can now be 24100.
>
> That will impact all architectures that use GNU as, even though the
> issue was really only seen on RISC-V. It probably doesn't matter since
> most architectures work with the integrated assembler and that is what
> we tell people to use but I think the current check will continue to
> work.

Agreed.

    depends on !CC_IS_CLANG || AS_IS_LLVM || (AS_IS_GNU && AS_VERSION
>= 23502 && AS_HAS_NON_CONST_LEB128)

is enabled for GCC. The long condition chain is for Clang. Given
Clang's behavior, we can simplify this to

    depends on !CC_IS_CLANG || AS_IS_LLVM || (AS_IS_GNU &&
AS_HAS_NON_CONST_LEB128)

This will still work even after R_LARCH_{ADD,SUB}_ULEB128 support is
added to llvm-project.
This condition matches HAVE_AS_LEB128 in GCC in spirit.

> Alternatively, I could do something like this but it is quite ugly and
> verbose :/
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index a0ebce05a368..aea0ef5a5d1f 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -231,11 +231,6 @@ config DEBUG_INFO
>           in the "Debug information" choice below, indicating that debug
>           information will be generated for build targets.
>
> -# Clang is known to generate .{s,u}leb128 with symbol deltas with DWARF5, which
> -# some targets may not support: https://sourceware.org/bugzilla/show_bug.cgi?id=27215
> -config AS_HAS_NON_CONST_LEB128
> -       def_bool $(as-instr,.uleb128 .Lexpr_end4 - .Lexpr_start3\n.Lexpr_start3:\n.Lexpr_end4:)
> -

Consider keeping this config, but just reword the comment.

>  choice
>         prompt "Debug information"
>         depends on DEBUG_KERNEL
> @@ -258,7 +253,7 @@ config DEBUG_INFO_NONE
>  config DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT
>         bool "Rely on the toolchain's implicit default DWARF version"
>         select DEBUG_INFO
> -       depends on !CC_IS_CLANG || AS_IS_LLVM || CLANG_VERSION < 140000 || (AS_IS_GNU && AS_VERSION >= 23502 && AS_HAS_NON_CONST_LEB128)
> +       depends on !CC_IS_CLANG || AS_IS_LLVM || CLANG_VERSION < 140000 || (AS_IS_GNU && ((!RISCV && AS_VERSION >= 23502) || (RISCV && AS_VERSION >= 24100)))

The RISCV condition would cause additional churn when LoongArch
ULEB128 support is added.

>         help
>           The implicit default version of DWARF debug info produced by a
>           toolchain changes over time.
> @@ -283,7 +278,7 @@ config DEBUG_INFO_DWARF5
>         bool "Generate DWARF Version 5 debuginfo"
>         select DEBUG_INFO
>         depends on !ARCH_HAS_BROKEN_DWARF5
> -       depends on !CC_IS_CLANG || AS_IS_LLVM || (AS_IS_GNU && AS_VERSION >= 23502 && AS_HAS_NON_CONST_LEB128)
> +       depends on !CC_IS_CLANG || AS_IS_LLVM || (AS_IS_GNU && ((!RISCV && AS_VERSION >= 23502) || (RISCV && AS_VERSION >= 24100)))
>         help
>           Generate DWARF v5 debug info. Requires binutils 2.35.2, gcc 5.0+ (gcc
>           5.0+ accepts the -gdwarf-5 flag but only had partial support for some



-- 
宋方睿



More information about the linux-riscv mailing list