Skip to content

Commit 3c0e8be

Browse files
MaskRaytru
authored andcommitted
[Driver] Default riscv*- triples to -fdebug-default-version=4
This adds a RISC-V special case to ToolChain::GetDefaultDwarfVersion, affecting Linux/Haiku/RISCVToolChain. DWARF v5 .debug_loclists/.debug_rnglists's DW_LLE_offset_pair/DW_RLE_offset_pair entry kinds utilitize `.uleb128 A-B` directives where A and B reference local labels in code sections. When A and B are separated by a RISC-V linker-relaxable instruction, A-B is incorrectly folded without a relocation, causing incorrect debug information. ``` void ext(void); int foo(int x) {ext(); return 0;} // DW_AT_location [DW_FORM_loclistx] of a DW_TAG_formal_parameter references a DW_LLE_offset_pair that can be incorrect after linker relaxation. int ext(void); void foo() { { int ret = ext(); if (__builtin_expect(ret, 0)) ext(); } } // DW_AT_ranges [DW_FORM_rnglistx] of a DW_TAG_lexical_block references a DW_RLE_offset_pair that can be incorrect after linker relaxation. ``` D157657 will implement R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 relocations, fixing the issue, but the relocation is only supported by bleeding-edge binutils 2.41 and not by lld/ELF yet. The goal is to make the emitted DWARF correct after linking. Many users don't care about the default DWARF version, but a linker error will be unacceptable. Let's just downgrade the default DWARF version, before binutils>=2.41 is more widely available. An alternative compatibility option is to add a toggle to DwarfDebug.cpp, but that doesn't seem like a good idea. Reviewed By: asb, kito-cheng Differential Revision: https://reviews.llvm.org/D157663 (cherry picked from commit bbc0f99) (with a release note)
1 parent 53671fc commit 3c0e8be

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,10 @@ RISC-V Support
962962
* Added intrinsics for reinterpret cast between vector boolean and vector
963963
integer ``m1`` value
964964
* Removed the ``vread_csr`` and ``vwrite_csr`` intrinsics
965+
- Default ``-fdebug-dwarf-version=`` is downgraded to 4 to work around
966+
incorrect DWARF related to ULEB128 and linker compatibility before
967+
``R_RISCV_SET_ULEB128`` becomes more widely supported.
968+
(`D157663 <https://reviews.llvm.org/D157663>`_).
965969

966970
CUDA/HIP Language Changes
967971
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Driver/ToolChain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class ToolChain {
561561

562562
// Return the DWARF version to emit, in the absence of arguments
563563
// to the contrary.
564-
virtual unsigned GetDefaultDwarfVersion() const { return 5; }
564+
virtual unsigned GetDefaultDwarfVersion() const;
565565

566566
// Some toolchains may have different restrictions on the DWARF version and
567567
// may need to adjust it. E.g. NVPTX may need to enforce DWARF2 even when host

clang/lib/Driver/ToolChain.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ ToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
427427
return UnwindTableLevel::None;
428428
}
429429

430+
unsigned ToolChain::GetDefaultDwarfVersion() const {
431+
// TODO: Remove the RISC-V special case when R_RISCV_SET_ULEB128 linker
432+
// support becomes more widely available.
433+
return getTriple().isRISCV() ? 4 : 5;
434+
}
435+
430436
Tool *ToolChain::getClang() const {
431437
if (!Clang)
432438
Clang.reset(new tools::Clang(*this, useIntegratedBackend()));

clang/test/Driver/clang-g-opts.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@
3737

3838
// CHECK-WITH-G-STANDALONE: "-debug-info-kind=standalone"
3939
// CHECK-WITH-G-STANDALONE: "-dwarf-version=2"
40+
41+
/// TODO: Special case before R_RISCV_SET_ULEB128 linker support becomes more widely available.
42+
// RUN: %clang -### -S %s -g --target=riscv64-linux-gnu 2>&1 | FileCheck --check-prefix=VERSION4 %s
43+
// RUN: %clang -### -S %s -g --target=riscv64-unknown-elf 2>&1 | FileCheck --check-prefix=VERSION4 %s
44+
// VERSION4: "-dwarf-version=4"

0 commit comments

Comments
 (0)