Index: llvm/trunk/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- llvm/trunk/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ llvm/trunk/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -410,9 +410,10 @@ void DwarfCompileUnit::attachRangesOrLowHighPC( DIE &Die, SmallVector Ranges) { - if (Ranges.size() == 1) { - const auto &single = Ranges.front(); - attachLowHighPC(Die, single.getStart(), single.getEnd()); + if (Ranges.size() == 1 || !DD->useRangesSection()) { + const RangeSpan &Front = Ranges.front(); + const RangeSpan &Back = Ranges.back(); + attachLowHighPC(Die, Front.getStart(), Back.getEnd()); } else addScopeRangeList(Die, std::move(Ranges)); } Index: llvm/trunk/lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- llvm/trunk/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ llvm/trunk/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -261,6 +261,9 @@ /// Whether to emit DWARF pub sections or not. bool UsePubSections = true; + /// Allow emission of .debug_ranges section. + bool UseRangesSection = true; + /// DWARF5 Experimental Options /// @{ bool HasDwarfAccelTables; @@ -507,6 +510,9 @@ /// Returns whether GNU oub sections should be emitted. bool usePubSections() const { return UsePubSections; } + /// Returns whether ranges section should be emitted. + bool useRangesSection() const { return UseRangesSection; } + // Experimental DWARF5 features. /// Returns whether or not to emit tables that dwarf consumers can Index: llvm/trunk/lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- llvm/trunk/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ llvm/trunk/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -128,6 +128,11 @@ cl::desc("Disable emission of DWARF pub sections."), cl::init(false)); +static cl::opt + NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, + cl::desc("Disable emission .debug_ranges section."), + cl::init(false)); + enum LinkageNameOption { DefaultLinkageNames, AllLinkageNames, @@ -316,6 +321,7 @@ DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION; UsePubSections = !NoDwarfPubSections; + UseRangesSection = !NoDwarfRangesSection; // Work around a GDB bug. GDB doesn't support the standard opcode; // SCE doesn't support GNU's; LLDB prefers the standard opcode, which @@ -744,7 +750,7 @@ // ranges for all subprogram DIEs for mach-o. DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; if (unsigned NumRanges = TheCU.getRanges().size()) { - if (NumRanges > 1) + if (NumRanges > 1 && useRangesSection()) // A DW_AT_low_pc attribute may also be specified in combination with // DW_AT_ranges to specify the default base address for use in // location lists (see Section 2.6.2) and range lists (see Section @@ -1906,6 +1912,16 @@ if (CUMap.empty()) return; + if (!useRangesSection()) { + assert(llvm::all_of( + CUMap, + [](const decltype(CUMap)::const_iterator::value_type &Pair) { + return Pair.second->getRangeLists().empty(); + }) && + "No debug ranges expected."); + return; + } + // Start the dwarf ranges section. Asm->OutStreamer->SwitchSection( Asm->getObjFileLowering().getDwarfRangesSection()); Index: llvm/trunk/test/DebugInfo/X86/no_debug_ranges.ll =================================================================== --- llvm/trunk/test/DebugInfo/X86/no_debug_ranges.ll +++ llvm/trunk/test/DebugInfo/X86/no_debug_ranges.ll @@ -0,0 +1,54 @@ +; RUN: llc -filetype=asm -mtriple=x86_64-pc-linux-gnu < %s -o - -dwarf-version=2 -no-dwarf-ranges-section | FileCheck %s --check-prefix=DISABLED +; RUN: llc -filetype=asm -mtriple=x86_64-pc-linux-gnu < %s -o - -dwarf-version=2 | FileCheck %s + +; DISABLED-NOT: {{DW_AT_ranges|.debug_ranges}} +; DISABLED: .section .debug_info +; DISABLED-NOT: {{DW_AT_ranges|.section}} +; DISABLED: .quad .Lfunc_begin0 # DW_AT_low_pc +; DISABLED-NEXT: .quad .Lfunc_end1 # DW_AT_high_pc +; DISABLED-NOT: {{DW_AT_ranges|.debug_ranges}} + +; .debug_ranges section must be emitted by default +; CHECK: .section .debug_info +; CHECK: quad 0 # DW_AT_low_pc +; CHECK-NEXT: long [[RANGE0:[.]Ldebug_ranges[0-9]+]] # DW_AT_ranges +; CHECK: .debug_ranges +; CHECK-NEXT: [[RANGE0]]: +; CHECK-NEXT: .quad .Lfunc_begin0 +; CHECK-NEXT: .quad .Lfunc_end0 +; CHECK-NEXT: .quad .Lfunc_begin1 +; CHECK-NEXT: .quad .Lfunc_end1 +; CHECK-NEXT: .quad 0 +; CHECK-NEXT: .quad 0 + +; Function Attrs: noinline nounwind optnone uwtable +define void @_Z2f1v() #0 section "a" !dbg !7 { +entry: + ret void, !dbg !10 +} + +; Function Attrs: noinline nounwind optnone uwtable +define void @_Z2f2v() #0 section "b" !dbg !11 { +entry: + ret void, !dbg !12 +} + +attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 6.0.0 (trunk 309523) (llvm/trunk 309526)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "funcs.cpp", directory: "/usr/local/google/home/blaikie/dev/scratch") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 6.0.0 (trunk 309523) (llvm/trunk 309526)"} +!7 = distinct !DISubprogram(name: "f1", linkageName: "_Z2f1v", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{null} +!10 = !DILocation(line: 1, column: 42, scope: !7) +!11 = distinct !DISubprogram(name: "f2", linkageName: "_Z2f2v", scope: !1, file: !1, line: 2, type: !8, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!12 = !DILocation(line: 2, column: 42, scope: !11)