[BOLT][DWARF] Do not emit zero low_pc address arange (#81955)

According to DWARF spec zero entires indicate end of arange. Changed so
that BOLT does not emit zero low_pc arange.
This commit is contained in:
Alexander Yermolovich
2024-02-16 11:23:28 -08:00
committed by GitHub
parent dc85719d5b
commit 5ff8b30327
4 changed files with 568 additions and 1 deletions

View File

@@ -873,7 +873,9 @@ void DWARFRewriter::updateUnitDebugInfo(
OutputRanges.push_back({0, 0});
const uint64_t RangesSectionOffset =
RangesSectionWriter.addRanges(OutputRanges);
if (!Unit.isDWOUnit())
// Don't emit the zero low_pc arange.
if (!Unit.isDWOUnit() && !OutputRanges.empty() &&
OutputRanges.back().LowPC)
ARangesSectionWriter->addCURanges(Unit.getOffset(),
std::move(OutputRanges));
updateDWARFObjectAddressRanges(Unit, DIEBldr, *Die, RangesSectionOffset,

View File

@@ -0,0 +1,325 @@
# clang++ -O0 -g2 -S -ffunction-sections -o
# int Foo;
# int helper() {
# return 0;
# }
#
# int helperhelper() {
# return 0;
# }
# int helperhelper2() {
# return 1;
# }
.text
.file "helper.cpp"
.file 0 "/repro2" "helper.cpp" md5 0xef451fc8d099ed067c9f2ff97d8aacf6
.section .text._Z6helperv,"ax",@progbits
.globl _Z6helperv # -- Begin function _Z6helperv
.p2align 4, 0x90
.type _Z6helperv,@function
_Z6helperv: # @_Z6helperv
.Lfunc_begin0:
.loc 0 2 0 # helper.cpp:2:0
.cfi_startproc
# %bb.0: # %entry
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
.Ltmp0:
.loc 0 3 3 prologue_end # helper.cpp:3:3
xorl %eax, %eax
.loc 0 3 3 epilogue_begin is_stmt 0 # helper.cpp:3:3
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Ltmp1:
.Lfunc_end0:
.size _Z6helperv, .Lfunc_end0-_Z6helperv
.cfi_endproc
# -- End function
.section .text._Z12helperhelperv,"ax",@progbits
.globl _Z12helperhelperv # -- Begin function _Z12helperhelperv
.p2align 4, 0x90
.type _Z12helperhelperv,@function
_Z12helperhelperv: # @_Z12helperhelperv
.Lfunc_begin1:
.loc 0 5 0 is_stmt 1 # helper.cpp:5:0
.cfi_startproc
# %bb.0: # %entry
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
.Ltmp2:
.loc 0 6 3 prologue_end # helper.cpp:6:3
xorl %eax, %eax
.loc 0 6 3 epilogue_begin is_stmt 0 # helper.cpp:6:3
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Ltmp3:
.Lfunc_end1:
.size _Z12helperhelperv, .Lfunc_end1-_Z12helperhelperv
.cfi_endproc
# -- End function
.section .text._Z13helperhelper2v,"ax",@progbits
.globl _Z13helperhelper2v # -- Begin function _Z13helperhelper2v
.p2align 4, 0x90
.type _Z13helperhelper2v,@function
_Z13helperhelper2v: # @_Z13helperhelper2v
.Lfunc_begin2:
.loc 0 8 0 is_stmt 1 # helper.cpp:8:0
.cfi_startproc
# %bb.0: # %entry
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
.Ltmp4:
.loc 0 9 3 prologue_end # helper.cpp:9:3
movl $1, %eax
.loc 0 9 3 epilogue_begin is_stmt 0 # helper.cpp:9:3
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Ltmp5:
.Lfunc_end2:
.size _Z13helperhelper2v, .Lfunc_end2-_Z13helperhelper2v
.cfi_endproc
# -- End function
.type Foo,@object # @Foo
.bss
.globl Foo
.p2align 2, 0x0
Foo:
.long 0 # 0x0
.size Foo, 4
.section .debug_abbrev,"",@progbits
.byte 1 # Abbreviation Code
.byte 17 # DW_TAG_compile_unit
.byte 1 # DW_CHILDREN_yes
.byte 37 # DW_AT_producer
.byte 37 # DW_FORM_strx1
.byte 19 # DW_AT_language
.byte 5 # DW_FORM_data2
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 114 # DW_AT_str_offsets_base
.byte 23 # DW_FORM_sec_offset
.byte 16 # DW_AT_stmt_list
.byte 23 # DW_FORM_sec_offset
.byte 27 # DW_AT_comp_dir
.byte 37 # DW_FORM_strx1
.byte 17 # DW_AT_low_pc
.byte 1 # DW_FORM_addr
.byte 85 # DW_AT_ranges
.byte 35 # DW_FORM_rnglistx
.byte 115 # DW_AT_addr_base
.byte 23 # DW_FORM_sec_offset
.byte 116 # DW_AT_rnglists_base
.byte 23 # DW_FORM_sec_offset
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 2 # Abbreviation Code
.byte 52 # DW_TAG_variable
.byte 0 # DW_CHILDREN_no
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 73 # DW_AT_type
.byte 19 # DW_FORM_ref4
.byte 63 # DW_AT_external
.byte 25 # DW_FORM_flag_present
.byte 58 # DW_AT_decl_file
.byte 11 # DW_FORM_data1
.byte 59 # DW_AT_decl_line
.byte 11 # DW_FORM_data1
.byte 2 # DW_AT_location
.byte 24 # DW_FORM_exprloc
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 3 # Abbreviation Code
.byte 36 # DW_TAG_base_type
.byte 0 # DW_CHILDREN_no
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 62 # DW_AT_encoding
.byte 11 # DW_FORM_data1
.byte 11 # DW_AT_byte_size
.byte 11 # DW_FORM_data1
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 4 # Abbreviation Code
.byte 46 # DW_TAG_subprogram
.byte 0 # DW_CHILDREN_no
.byte 17 # DW_AT_low_pc
.byte 27 # DW_FORM_addrx
.byte 18 # DW_AT_high_pc
.byte 6 # DW_FORM_data4
.byte 64 # DW_AT_frame_base
.byte 24 # DW_FORM_exprloc
.byte 110 # DW_AT_linkage_name
.byte 37 # DW_FORM_strx1
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 58 # DW_AT_decl_file
.byte 11 # DW_FORM_data1
.byte 59 # DW_AT_decl_line
.byte 11 # DW_FORM_data1
.byte 73 # DW_AT_type
.byte 19 # DW_FORM_ref4
.byte 63 # DW_AT_external
.byte 25 # DW_FORM_flag_present
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 0 # EOM(3)
.section .debug_info,"",@progbits
.Lcu_begin0:
.long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
.Ldebug_info_start0:
.short 5 # DWARF version number
.byte 1 # DWARF Unit Type
.byte 8 # Address Size (in bytes)
.long .debug_abbrev # Offset Into Abbrev. Section
.byte 1 # Abbrev [1] 0xc:0x5f DW_TAG_compile_unit
.byte 0 # DW_AT_producer
.short 33 # DW_AT_language
.byte 1 # DW_AT_name
.long .Lstr_offsets_base0 # DW_AT_str_offsets_base
.long .Lline_table_start0 # DW_AT_stmt_list
.byte 2 # DW_AT_comp_dir
.quad 0 # DW_AT_low_pc
.byte 0 # DW_AT_ranges
.long .Laddr_table_base0 # DW_AT_addr_base
.long .Lrnglists_table_base0 # DW_AT_rnglists_base
.byte 2 # Abbrev [2] 0x2b:0xb DW_TAG_variable
.byte 3 # DW_AT_name
.long 54 # DW_AT_type
# DW_AT_external
.byte 0 # DW_AT_decl_file
.byte 1 # DW_AT_decl_line
.byte 2 # DW_AT_location
.byte 161
.byte 0
.byte 3 # Abbrev [3] 0x36:0x4 DW_TAG_base_type
.byte 4 # DW_AT_name
.byte 5 # DW_AT_encoding
.byte 4 # DW_AT_byte_size
.byte 4 # Abbrev [4] 0x3a:0x10 DW_TAG_subprogram
.byte 1 # DW_AT_low_pc
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
.byte 1 # DW_AT_frame_base
.byte 86
.byte 5 # DW_AT_linkage_name
.byte 6 # DW_AT_name
.byte 0 # DW_AT_decl_file
.byte 2 # DW_AT_decl_line
.long 54 # DW_AT_type
# DW_AT_external
.byte 4 # Abbrev [4] 0x4a:0x10 DW_TAG_subprogram
.byte 2 # DW_AT_low_pc
.long .Lfunc_end1-.Lfunc_begin1 # DW_AT_high_pc
.byte 1 # DW_AT_frame_base
.byte 86
.byte 7 # DW_AT_linkage_name
.byte 8 # DW_AT_name
.byte 0 # DW_AT_decl_file
.byte 5 # DW_AT_decl_line
.long 54 # DW_AT_type
# DW_AT_external
.byte 4 # Abbrev [4] 0x5a:0x10 DW_TAG_subprogram
.byte 3 # DW_AT_low_pc
.long .Lfunc_end2-.Lfunc_begin2 # DW_AT_high_pc
.byte 1 # DW_AT_frame_base
.byte 86
.byte 9 # DW_AT_linkage_name
.byte 10 # DW_AT_name
.byte 0 # DW_AT_decl_file
.byte 8 # DW_AT_decl_line
.long 54 # DW_AT_type
# DW_AT_external
.byte 0 # End Of Children Mark
.Ldebug_info_end0:
.section .debug_rnglists,"",@progbits
.long .Ldebug_list_header_end0-.Ldebug_list_header_start0 # Length
.Ldebug_list_header_start0:
.short 5 # Version
.byte 8 # Address size
.byte 0 # Segment selector size
.long 1 # Offset entry count
.Lrnglists_table_base0:
.long .Ldebug_ranges0-.Lrnglists_table_base0
.Ldebug_ranges0:
.byte 3 # DW_RLE_startx_length
.byte 1 # start index
.uleb128 .Lfunc_end0-.Lfunc_begin0 # length
.byte 3 # DW_RLE_startx_length
.byte 2 # start index
.uleb128 .Lfunc_end1-.Lfunc_begin1 # length
.byte 3 # DW_RLE_startx_length
.byte 3 # start index
.uleb128 .Lfunc_end2-.Lfunc_begin2 # length
.byte 0 # DW_RLE_end_of_list
.Ldebug_list_header_end0:
.section .debug_str_offsets,"",@progbits
.long 48 # Length of String Offsets Set
.short 5
.short 0
.Lstr_offsets_base0:
.section .debug_str,"MS",@progbits,1
.Linfo_string0:
.asciz "clang version 19.0.0git (git@github.com:ayermolo/llvm-project.git 6c884ef47e88e5ff18353819e806fe1b84e3c5b5)" # string offset=0
.Linfo_string1:
.asciz "helper.cpp" # string offset=108
.Linfo_string2:
.asciz "/repro2" # string offset=119
.Linfo_string3:
.asciz "Foo" # string offset=164
.Linfo_string4:
.asciz "int" # string offset=168
.Linfo_string5:
.asciz "_Z6helperv" # string offset=172
.Linfo_string6:
.asciz "helper" # string offset=183
.Linfo_string7:
.asciz "_Z12helperhelperv" # string offset=190
.Linfo_string8:
.asciz "helperhelper" # string offset=208
.Linfo_string9:
.asciz "_Z13helperhelper2v" # string offset=221
.Linfo_string10:
.asciz "helperhelper2" # string offset=240
.section .debug_str_offsets,"",@progbits
.long .Linfo_string0
.long .Linfo_string1
.long .Linfo_string2
.long .Linfo_string3
.long .Linfo_string4
.long .Linfo_string5
.long .Linfo_string6
.long .Linfo_string7
.long .Linfo_string8
.long .Linfo_string9
.long .Linfo_string10
.section .debug_addr,"",@progbits
.long .Ldebug_addr_end0-.Ldebug_addr_start0 # Length of contribution
.Ldebug_addr_start0:
.short 5 # DWARF version number
.byte 8 # Address size
.byte 0 # Segment selector size
.Laddr_table_base0:
.quad Foo
.quad .Lfunc_begin0
.quad .Lfunc_begin1
.quad .Lfunc_begin2
.Ldebug_addr_end0:
.ident "clang version 19.0.0git (git@github.com:ayermolo/llvm-project.git 6c884ef47e88e5ff18353819e806fe1b84e3c5b5)"
.section ".note.GNU-stack","",@progbits
.addrsig
.section .debug_line,"",@progbits
.Lline_table_start0:

View File

@@ -0,0 +1,221 @@
# clang++ -O0 -g2 -S -ffunction-sections -o
# extern int Foo;
# int main(int argc, char *argv[]) {
# return 0;
# }
.text
.file "main.cpp"
.section .text.main,"ax",@progbits
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.Lfunc_begin0:
.file 0 "/repro2" "main.cpp" md5 0x44051a7da6269c029bfea564d92d1db4
.loc 0 2 0 # main.cpp:2:0
.cfi_startproc
# %bb.0: # %entry
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl $0, -4(%rbp)
movl %edi, -8(%rbp)
movq %rsi, -16(%rbp)
.Ltmp0:
.loc 0 3 3 prologue_end # main.cpp:3:3
xorl %eax, %eax
.loc 0 3 3 epilogue_begin is_stmt 0 # main.cpp:3:3
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Ltmp1:
.Lfunc_end0:
.size main, .Lfunc_end0-main
.cfi_endproc
# -- End function
.section .debug_abbrev,"",@progbits
.byte 1 # Abbreviation Code
.byte 17 # DW_TAG_compile_unit
.byte 1 # DW_CHILDREN_yes
.byte 37 # DW_AT_producer
.byte 37 # DW_FORM_strx1
.byte 19 # DW_AT_language
.byte 5 # DW_FORM_data2
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 114 # DW_AT_str_offsets_base
.byte 23 # DW_FORM_sec_offset
.byte 16 # DW_AT_stmt_list
.byte 23 # DW_FORM_sec_offset
.byte 27 # DW_AT_comp_dir
.byte 37 # DW_FORM_strx1
.byte 17 # DW_AT_low_pc
.byte 27 # DW_FORM_addrx
.byte 18 # DW_AT_high_pc
.byte 6 # DW_FORM_data4
.byte 115 # DW_AT_addr_base
.byte 23 # DW_FORM_sec_offset
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 2 # Abbreviation Code
.byte 46 # DW_TAG_subprogram
.byte 1 # DW_CHILDREN_yes
.byte 17 # DW_AT_low_pc
.byte 27 # DW_FORM_addrx
.byte 18 # DW_AT_high_pc
.byte 6 # DW_FORM_data4
.byte 64 # DW_AT_frame_base
.byte 24 # DW_FORM_exprloc
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 58 # DW_AT_decl_file
.byte 11 # DW_FORM_data1
.byte 59 # DW_AT_decl_line
.byte 11 # DW_FORM_data1
.byte 73 # DW_AT_type
.byte 19 # DW_FORM_ref4
.byte 63 # DW_AT_external
.byte 25 # DW_FORM_flag_present
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 3 # Abbreviation Code
.byte 5 # DW_TAG_formal_parameter
.byte 0 # DW_CHILDREN_no
.byte 2 # DW_AT_location
.byte 24 # DW_FORM_exprloc
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 58 # DW_AT_decl_file
.byte 11 # DW_FORM_data1
.byte 59 # DW_AT_decl_line
.byte 11 # DW_FORM_data1
.byte 73 # DW_AT_type
.byte 19 # DW_FORM_ref4
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 4 # Abbreviation Code
.byte 36 # DW_TAG_base_type
.byte 0 # DW_CHILDREN_no
.byte 3 # DW_AT_name
.byte 37 # DW_FORM_strx1
.byte 62 # DW_AT_encoding
.byte 11 # DW_FORM_data1
.byte 11 # DW_AT_byte_size
.byte 11 # DW_FORM_data1
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 5 # Abbreviation Code
.byte 15 # DW_TAG_pointer_type
.byte 0 # DW_CHILDREN_no
.byte 73 # DW_AT_type
.byte 19 # DW_FORM_ref4
.byte 0 # EOM(1)
.byte 0 # EOM(2)
.byte 0 # EOM(3)
.section .debug_info,"",@progbits
.Lcu_begin0:
.long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
.Ldebug_info_start0:
.short 5 # DWARF version number
.byte 1 # DWARF Unit Type
.byte 8 # Address Size (in bytes)
.long .debug_abbrev # Offset Into Abbrev. Section
.byte 1 # Abbrev [1] 0xc:0x50 DW_TAG_compile_unit
.byte 0 # DW_AT_producer
.short 33 # DW_AT_language
.byte 1 # DW_AT_name
.long .Lstr_offsets_base0 # DW_AT_str_offsets_base
.long .Lline_table_start0 # DW_AT_stmt_list
.byte 2 # DW_AT_comp_dir
.byte 0 # DW_AT_low_pc
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
.long .Laddr_table_base0 # DW_AT_addr_base
.byte 2 # Abbrev [2] 0x23:0x26 DW_TAG_subprogram
.byte 0 # DW_AT_low_pc
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
.byte 1 # DW_AT_frame_base
.byte 86
.byte 3 # DW_AT_name
.byte 0 # DW_AT_decl_file
.byte 2 # DW_AT_decl_line
.long 73 # DW_AT_type
# DW_AT_external
.byte 3 # Abbrev [3] 0x32:0xb DW_TAG_formal_parameter
.byte 2 # DW_AT_location
.byte 145
.byte 120
.byte 5 # DW_AT_name
.byte 0 # DW_AT_decl_file
.byte 2 # DW_AT_decl_line
.long 73 # DW_AT_type
.byte 3 # Abbrev [3] 0x3d:0xb DW_TAG_formal_parameter
.byte 2 # DW_AT_location
.byte 145
.byte 112
.byte 6 # DW_AT_name
.byte 0 # DW_AT_decl_file
.byte 2 # DW_AT_decl_line
.long 77 # DW_AT_type
.byte 0 # End Of Children Mark
.byte 4 # Abbrev [4] 0x49:0x4 DW_TAG_base_type
.byte 4 # DW_AT_name
.byte 5 # DW_AT_encoding
.byte 4 # DW_AT_byte_size
.byte 5 # Abbrev [5] 0x4d:0x5 DW_TAG_pointer_type
.long 82 # DW_AT_type
.byte 5 # Abbrev [5] 0x52:0x5 DW_TAG_pointer_type
.long 87 # DW_AT_type
.byte 4 # Abbrev [4] 0x57:0x4 DW_TAG_base_type
.byte 7 # DW_AT_name
.byte 6 # DW_AT_encoding
.byte 1 # DW_AT_byte_size
.byte 0 # End Of Children Mark
.Ldebug_info_end0:
.section .debug_str_offsets,"",@progbits
.long 36 # Length of String Offsets Set
.short 5
.short 0
.Lstr_offsets_base0:
.section .debug_str,"MS",@progbits,1
.Linfo_string0:
.asciz "clang version 19.0.0git (git@github.com:ayermolo/llvm-project.git 6c884ef47e88e5ff18353819e806fe1b84e3c5b5)" # string offset=0
.Linfo_string1:
.asciz "main.cpp" # string offset=108
.Linfo_string2:
.asciz "/repro2" # string offset=117
.Linfo_string3:
.asciz "main" # string offset=162
.Linfo_string4:
.asciz "int" # string offset=167
.Linfo_string5:
.asciz "argc" # string offset=171
.Linfo_string6:
.asciz "argv" # string offset=176
.Linfo_string7:
.asciz "char" # string offset=181
.section .debug_str_offsets,"",@progbits
.long .Linfo_string0
.long .Linfo_string1
.long .Linfo_string2
.long .Linfo_string3
.long .Linfo_string4
.long .Linfo_string5
.long .Linfo_string6
.long .Linfo_string7
.section .debug_addr,"",@progbits
.long .Ldebug_addr_end0-.Ldebug_addr_start0 # Length of contribution
.Ldebug_addr_start0:
.short 5 # DWARF version number
.byte 8 # Address size
.byte 0 # Segment selector size
.Laddr_table_base0:
.quad .Lfunc_begin0
.Ldebug_addr_end0:
.ident "clang version 19.0.0git (git@github.com:ayermolo/llvm-project.git 6c884ef47e88e5ff18353819e806fe1b84e3c5b5)"
.section ".note.GNU-stack","",@progbits
.addrsig
.section .debug_line,"",@progbits
.Lline_table_start0:

View File

@@ -0,0 +1,19 @@
# REQUIRES: system-linux
# RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-empty-arange-main.s -o %tmain.o
# RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-empty-arange-helper.s -o %thelper.o
# RUN: %clang %cflags -dwarf-5 %tmain.o %thelper.o -o %t.exe -Wl,--entry=main -Wl,-q -Wl,-gc-sections
# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections
# RUN: llvm-dwarfdump --debug-aranges %t.bolt > %t.txt
# RUN: llvm-dwarfdump --debug-info -r 0 %t.bolt >> %t.txt
# RUN: cat %t.txt | FileCheck --check-prefix=POSTCHECK %s
## This tests checks that BOLT handles correctly empty arange. Before we would output 0x0,0x0.
## Which tools interpreted as an end to aragnes list.
# POSTCHECK: Address Range Header
# POSTCHECK-NOT: Address Range Header
# POSTCHECK: DW_TAG_compile_unit
# POSTCHECK: DW_TAG_compile_unit
# POSTCHECK: DW_AT_ranges
# POSTCHECK-NEXT: [0x0000000000000000, 0x0000000000000000))