[PATCH] D157663: [Driver] Default riscv*-linux* to -fdebug-default-version=4

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 10 15:58:11 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: asb, dblaikie, jrtc27, kito-cheng.
Herald added subscribers: VincentWu, vkmr, luismarques, sameer.abuasal, s.egerton, Jim, benna, psnobl, PkmX, rogfer01, shiva0217, simoncook, arichardson.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added subscribers: cfe-commits, wangpc, eopXD.
Herald added a project: clang.

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 <https://reviews.llvm.org/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.

This patch is planned for the main and release/17.x branches.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157663

Files:
  clang/lib/Driver/ToolChains/Linux.cpp
  clang/test/Driver/clang-g-opts.c


Index: clang/test/Driver/clang-g-opts.c
===================================================================
--- clang/test/Driver/clang-g-opts.c
+++ clang/test/Driver/clang-g-opts.c
@@ -37,3 +37,7 @@
 
 // CHECK-WITH-G-STANDALONE: "-debug-info-kind=standalone"
 // CHECK-WITH-G-STANDALONE: "-dwarf-version=2"
+
+// RUN: %clang -### -S %s -g --target=riscv64-linux-gnu 2>&1 \
+// RUN:   | FileCheck --check-prefix=VERSION4 %s
+// VERSION4: "-dwarf-version=4"
Index: clang/lib/Driver/ToolChains/Linux.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Linux.cpp
+++ clang/lib/Driver/ToolChains/Linux.cpp
@@ -343,7 +343,7 @@
 }
 
 unsigned Linux::GetDefaultDwarfVersion() const {
-  if (getTriple().isAndroid())
+  if (getTriple().isAndroid() || getTriple().isRISCV())
     return 4;
   return ToolChain::GetDefaultDwarfVersion();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157663.549191.patch
Type: text/x-patch
Size: 884 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230810/99e5ba69/attachment-0001.bin>


More information about the cfe-commits mailing list