[llvm] 256a52d - Round up zero-sized symbols to 1 byte in `.debug_aranges`.

Alexander Yermolovich via llvm-commits llvm-commits at lists.llvm.org
Wed May 25 13:31:56 PDT 2022


Author: Patrick Walton
Date: 2022-05-25T13:31:36-07:00
New Revision: 256a52d9aac8a9e98fbfd6a3d91090bf127cef7d

URL: https://github.com/llvm/llvm-project/commit/256a52d9aac8a9e98fbfd6a3d91090bf127cef7d
DIFF: https://github.com/llvm/llvm-project/commit/256a52d9aac8a9e98fbfd6a3d91090bf127cef7d.diff

LOG: Round up zero-sized symbols to 1 byte in `.debug_aranges`.

This commit modifies the AsmPrinter to avoid emitting any zero-sized symbols to
the .debug_aranges table, by rounding their size up to 1. Entries with zero
length violate the DWARF 5 spec, which states:

> Each descriptor is a triple consisting of a segment selector, the beginning
> address within that segment of a range of text or data covered by some entry
> owned by the corresponding compilation unit, followed by the non-zero length
> of that range.

In practice, these zero-sized entries produce annoying warnings in lld and
cause GNU binutils to truncate the table when parsing it.

Other parts of LLVM, such as DWARFDebugARanges in the DebugInfo module
(specifically the appendRange method), already avoid emitting zero-sized
symbols to .debug_aranges, but not comprehensively in the AsmPrinter. In fact,
the AsmPrinter does try to avoid emitting such zero-sized symbols when labels
aren't involved, but doesn't when the symbol to emitted is a difference of two
labels; this patch extends that logic to handle the case in which the symbol is
defined via labels.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D126257

Added: 
    llvm/test/CodeGen/Generic/dwarf-aranges-zero-size.ll

Modified: 
    llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
    llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll
    llvm/test/DebugInfo/X86/dwarf-aranges.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 37ae84ad9bf1..50032c887d83 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -3042,13 +3042,17 @@ void DwarfDebug::emitDebugARanges() {
     for (const ArangeSpan &Span : List) {
       Asm->emitLabelReference(Span.Start, PtrSize);
 
-      // Calculate the size as being from the span start to it's end.
-      if (Span.End) {
+      // Calculate the size as being from the span start to its end.
+      //
+      // If the size is zero, then round it up to one byte. The DWARF
+      // specification requires that entries in this table have nonzero
+      // lengths.
+      uint64_t Size = SymSize[Span.Start];
+      if (Size != 0 && Span.End) {
         Asm->emitLabelDifference(Span.End, Span.Start, PtrSize);
       } else {
         // For symbols without an end marker (e.g. common), we
         // write a single arange entry containing just that one symbol.
-        uint64_t Size = SymSize[Span.Start];
         if (Size == 0)
           Size = 1;
 

diff  --git a/llvm/test/CodeGen/Generic/dwarf-aranges-zero-size.ll b/llvm/test/CodeGen/Generic/dwarf-aranges-zero-size.ll
new file mode 100644
index 000000000000..9a8be3c8f91d
--- /dev/null
+++ b/llvm/test/CodeGen/Generic/dwarf-aranges-zero-size.ll
@@ -0,0 +1,23 @@
+; Ensures that the AsmPrinter doesn't emit zero-sized symbols into `.debug_aranges`.
+;
+; RUN: llc --generate-arange-section < %s | FileCheck %s
+; CHECK: .section .debug_aranges
+; CHECK: .quad EXAMPLE
+; CHECK-NEXT: .quad 1
+; CHECK: .section
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at EXAMPLE = constant <{ [0 x i8] }> zeroinitializer, align 1, !dbg !0
+
+!llvm.module.flags = !{!3}
+!llvm.dbg.cu = !{!4}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "EXAMPLE", linkageName: "EXAMPLE", scope: null, file: null, line: 161, type: !2, isLocal: false, isDefinition: true, align: 1)
+!2 = !DIBasicType(name: "()", encoding: DW_ATE_unsigned)
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = distinct !DICompileUnit(language: DW_LANG_Rust, file: !5, producer: "rustc", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: null, globals: !6)
+!5 = !DIFile(filename: "foo", directory: "")
+!6 = !{!0}

diff  --git a/llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll b/llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll
index 98922f1b7d08..51d2c4a20589 100644
--- a/llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll
+++ b/llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll
@@ -98,7 +98,7 @@
 
 ; CHECK:      .debug_aranges contents:
 ; CHECK-NEXT: Address Range Header: length = 0x{{.*}}, format = DWARF32, version = 0x0002, cu_offset = 0x00000000, addr_size = 0x02, seg_size = 0x00
-; CHECK-NEXT: [0x0000, 0x0006)
+; CHECK-NEXT: [0x0000, 0x0001)
 
 ; CHECK:      .debug_addr contents:
 ; CHECK-NEXT: Address table header: length = 0x{{.*}}, format = DWARF32, version = 0x0005, addr_size = 0x02, seg_size = 0x00

diff  --git a/llvm/test/DebugInfo/X86/dwarf-aranges.ll b/llvm/test/DebugInfo/X86/dwarf-aranges.ll
index 5358a30c6a06..c43e9eea8b71 100644
--- a/llvm/test/DebugInfo/X86/dwarf-aranges.ll
+++ b/llvm/test/DebugInfo/X86/dwarf-aranges.ll
@@ -22,7 +22,7 @@
 
 ; <text section> - it should have made one span covering all functions in this CU.
 ; CHECK-NEXT: .quad .Lfunc_begin0
-; CHECK-NEXT: .quad .Lsec_end2-.Lfunc_begin0
+; CHECK-NEXT: .quad 1
 
 ; -- finish --
 ; CHECK-NEXT: # ARange terminator


        


More information about the llvm-commits mailing list