Skip to content

Commit

Permalink
[Driver] Default riscv*- triples to -fdebug-default-version=4
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
MaskRay authored and tru committed Aug 15, 2023
1 parent 53671fc commit 3c0e8be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,10 @@ RISC-V Support
* Added intrinsics for reinterpret cast between vector boolean and vector
integer ``m1`` value
* Removed the ``vread_csr`` and ``vwrite_csr`` intrinsics
- Default ``-fdebug-dwarf-version=`` is downgraded to 4 to work around
incorrect DWARF related to ULEB128 and linker compatibility before
``R_RISCV_SET_ULEB128`` becomes more widely supported.
(`D157663 <https://reviews.llvm.org/D157663>`_).

CUDA/HIP Language Changes
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ class ToolChain {

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

// Some toolchains may have different restrictions on the DWARF version and
// may need to adjust it. E.g. NVPTX may need to enforce DWARF2 even when host
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ ToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
return UnwindTableLevel::None;
}

unsigned ToolChain::GetDefaultDwarfVersion() const {
// TODO: Remove the RISC-V special case when R_RISCV_SET_ULEB128 linker
// support becomes more widely available.
return getTriple().isRISCV() ? 4 : 5;
}

Tool *ToolChain::getClang() const {
if (!Clang)
Clang.reset(new tools::Clang(*this, useIntegratedBackend()));
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Driver/clang-g-opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@

// CHECK-WITH-G-STANDALONE: "-debug-info-kind=standalone"
// CHECK-WITH-G-STANDALONE: "-dwarf-version=2"

/// TODO: Special case before R_RISCV_SET_ULEB128 linker support becomes more widely available.
// RUN: %clang -### -S %s -g --target=riscv64-linux-gnu 2>&1 | FileCheck --check-prefix=VERSION4 %s
// RUN: %clang -### -S %s -g --target=riscv64-unknown-elf 2>&1 | FileCheck --check-prefix=VERSION4 %s
// VERSION4: "-dwarf-version=4"

0 comments on commit 3c0e8be

Please sign in to comment.