diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h --- a/llvm/include/llvm/MC/MCStreamer.h +++ b/llvm/include/llvm/MC/MCStreamer.h @@ -993,6 +993,10 @@ /// This implements the CodeView '.cv_fpo_data' assembler directive. virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {} + /// Emits an expression in such a way that relocations can be performed on its + /// components. + void emitRelocatableExpr(const MCExpr *Expr, unsigned Size); + /// Emit the absolute difference between two symbols. /// /// \pre Offset of \c Hi is greater than the offset \c Lo. 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 @@ -3043,12 +3043,24 @@ Asm->emitLabelReference(Span.Start, PtrSize); // Calculate the size as being from the span start to it's end. + uint64_t Size = SymSize[Span.Start]; if (Span.End) { - Asm->emitLabelDifference(Span.End, Span.Start, PtrSize); + MCContext &Context = Asm->OutStreamer->getContext(); + const MCExpr *Expr = + MCBinaryExpr::createSub(MCSymbolRefExpr::create(Span.End, Context), + MCSymbolRefExpr::create(Span.Start, Context), + Context); + // Pretend that zero-sized symbols have length 1. The DWARF specification + // requires that entries in this table have nonzero lengths. + if (Size == 0) { + Expr = MCBinaryExpr::createAdd(Expr, + MCConstantExpr::create(1, Context), + Context); + } + Asm->OutStreamer->emitRelocatableExpr(Expr, 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/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp --- a/llvm/lib/MC/MCStreamer.cpp +++ b/llvm/lib/MC/MCStreamer.cpp @@ -1114,25 +1114,29 @@ getCurrentSectionOnly(), Probe, InlineStack); } -void MCStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, - unsigned Size) { - // Get the Hi-Lo expression. - const MCExpr *Diff = - MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context), - MCSymbolRefExpr::create(Lo, Context), Context); - +void MCStreamer::emitRelocatableExpr(const MCExpr *Expr, unsigned Size) { const MCAsmInfo *MAI = Context.getAsmInfo(); if (!MAI->doesSetDirectiveSuppressReloc()) { - emitValue(Diff, Size); + emitValue(Expr, Size); return; } // Otherwise, emit with .set (aka assignment). MCSymbol *SetLabel = Context.createTempSymbol("set"); - emitAssignment(SetLabel, Diff); + emitAssignment(SetLabel, Expr); emitSymbolValue(SetLabel, Size); } +void MCStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, + unsigned Size) { + // Get the Hi-Lo expression. + const MCExpr *Diff = + MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context), + MCSymbolRefExpr::create(Lo, Context), Context); + + emitRelocatableExpr(Diff, Size); +} + void MCStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) { // Get the Hi-Lo expression. 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 --- /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 ({{\.?[a-zA-Z0-9_]+}}-EXAMPLE)+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} diff --git a/llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll b/llvm/test/DebugInfo/MSP430/dwarf-basics-v5.ll --- 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, 0x0007) ; 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 --- a/llvm/test/DebugInfo/X86/dwarf-aranges.ll +++ b/llvm/test/DebugInfo/X86/dwarf-aranges.ll @@ -22,7 +22,7 @@ ; - 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 (.Lsec_end2-.Lfunc_begin0)+1 ; -- finish -- ; CHECK-NEXT: # ARange terminator