diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -3042,13 +3042,18 @@ 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. + auto SizeRef = SymSize.find(Span.Start); + if ((SizeRef == SymSize.end() || SizeRef->second != 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]; + auto Size = SizeRef->second; if (Size == 0) Size = 1; diff --git a/llvm/test/CodeGen/X86/dwarf-aranges-zero-size.ll b/llvm/test/CodeGen/X86/dwarf-aranges-zero-size.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/dwarf-aranges-zero-size.ll @@ -0,0 +1,23 @@ +; Ensures that the AsmPrinter rounds up zero-sized symbols in `.debug_aranges` to one byte. +; +; 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" + +@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}