Index: lld/ELF/InputSection.cpp =================================================================== --- lld/ELF/InputSection.cpp +++ lld/ELF/InputSection.cpp @@ -782,6 +782,8 @@ // function as a performance optimization. template void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef Rels) { + assert(!Config->Relocatable); + const unsigned Bits = sizeof(typename ELFT::uint) * 8; for (const RelTy &Rel : Rels) { @@ -829,8 +831,16 @@ if (Sym.isTls() && !Out::TlsPhdr) Target->relocateOne(BufLoc, Type, 0); - else + else if (Sym.getOutputSection()) Target->relocateOne(BufLoc, Type, SignExtend64(Sym.getVA(Addend))); + else + // if relocation points to the deleted section(-Wl,--gc-sections) + // then - it is neccessary to make start offset to be out of + // module address range to not to overlap with module`s ranges. + // UINT64_MAX-1 used as maximal value of an address. UINT64_MAX has + // special usage in .debug_ranges so we use UINT64_MAX minus one. + // https://bugs.llvm.org/show_bug.cgi?id=41124 + Target->relocateOne(BufLoc, Type, SignExtend64(UINT64_MAX - 1)); } } Index: lld/test/ELF/Inputs/main.s =================================================================== --- /dev/null +++ lld/test/ELF/Inputs/main.s @@ -0,0 +1,150 @@ + .text + .file "main.cpp" + .globl main # -- Begin function main + .p2align 4, 0x90 + .type main,@function +main: # @main +.Lfunc_begin0: + .file 0 "" "main.cpp" + .file 1 "main.cpp" + .loc 1 1 0 # main.cpp:1:0 +# %bb.0: # %entry + .loc 1 2 4 prologue_end # main.cpp:2:4 + xorl %eax, %eax + retq +.Ltmp0: +.Lfunc_end0: + .size main, .Lfunc_end0-main + # -- End function +.global _start +_start: + jmp main + + .section .debug_str_offsets,"",@progbits + .long 24 + .short 5 + .short 0 +.Lstr_offsets_base0: + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 9.0.0 " # string offset=0 +.Linfo_string1: + .asciz "main.cpp" # string offset=104 +.Linfo_string2: + .asciz "" # string offset=113 +.Linfo_string3: + .asciz "main" # string offset=141 +.Linfo_string4: + .asciz "int" # string offset=146 + .section .debug_str_offsets,"",@progbits + .long .Linfo_string0 + .long .Linfo_string1 + .long .Linfo_string2 + .long .Linfo_string3 + .long .Linfo_string4 + .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 115 # DW_AT_addr_base + .byte 23 # DW_FORM_sec_offset + .byte 17 # DW_AT_low_pc + .byte 27 # DW_FORM_addrx + .byte 18 # DW_AT_high_pc + .byte 6 # DW_FORM_data4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 2 # 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 122 # DW_AT_call_all_calls + .byte 25 # DW_FORM_flag_present + .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 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 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:0x2b DW_TAG_compile_unit + .byte 0 # DW_AT_producer + .short 4 # 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 + .long .Laddr_table_base0 # DW_AT_addr_base + .byte 0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc + .byte 2 # Abbrev [2] 0x23:0xf 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 87 + # DW_AT_call_all_calls + .byte 3 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 1 # DW_AT_decl_line + .long 50 # DW_AT_type + # DW_AT_external + .byte 3 # Abbrev [3] 0x32:0x4 DW_TAG_base_type + .byte 4 # DW_AT_name + .byte 5 # DW_AT_encoding + .byte 4 # DW_AT_byte_size + .byte 0 # End Of Children Mark +.Ldebug_info_end0: + .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: + + .section .debug_line,"",@progbits +.Lline_table_start0: Index: lld/test/ELF/Inputs/not_used.s =================================================================== --- /dev/null +++ lld/test/ELF/Inputs/not_used.s @@ -0,0 +1,135 @@ + .text + .file "not_used.cpp" + .globl _Z3foov # -- Begin function _Z3foov + .p2align 4, 0x90 + .type _Z3foov,@function +_Z3foov: # @_Z3foov +.Lfunc_begin0: + .file 0 "" "not_used.cpp" + .file 1 "not_used.cpp" + .loc 1 1 0 # not_used.cpp:1:0 +# %bb.0: # %entry + .loc 1 2 5 prologue_end # not_used.cpp:2:5 + + .rept 3300000; nop; .endr + + .loc 1 3 1 # not_used.cpp:3:1 + retq +.Ltmp0: +.Lfunc_end0: + .size _Z3foov, .Lfunc_end0-_Z3foov + # -- End function + .section .debug_str_offsets,"",@progbits + .long 24 + .short 5 + .short 0 +.Lstr_offsets_base0: + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 9.0.0 " # string offset=0 +.Linfo_string1: + .asciz "not_used.cpp" # string offset=104 +.Linfo_string2: + .asciz "" # string offset=117 +.Linfo_string3: + .asciz "_Z3foov" # string offset=145 +.Linfo_string4: + .asciz "foo" # string offset=153 + .section .debug_str_offsets,"",@progbits + .long .Linfo_string0 + .long .Linfo_string1 + .long .Linfo_string2 + .long .Linfo_string3 + .long .Linfo_string4 + .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 115 # DW_AT_addr_base + .byte 23 # DW_FORM_sec_offset + .byte 17 # DW_AT_low_pc + .byte 27 # DW_FORM_addrx + .byte 18 # DW_AT_high_pc + .byte 6 # DW_FORM_data4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 2 # 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 122 # DW_AT_call_all_calls + .byte 25 # DW_FORM_flag_present + .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 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:0x24 DW_TAG_compile_unit + .byte 0 # DW_AT_producer + .short 4 # 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 + .long .Laddr_table_base0 # DW_AT_addr_base + .byte 0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc + .byte 2 # Abbrev [2] 0x23:0xc 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 87 + # DW_AT_call_all_calls + .byte 3 # DW_AT_linkage_name + .byte 4 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 1 # DW_AT_decl_line + # DW_AT_external + .byte 0 # End Of Children Mark +.Ldebug_info_end0: + .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: + + .section .debug_line,"",@progbits +.Lline_table_start0: + Index: lld/test/ELF/gc-sections-alloc.s =================================================================== --- lld/test/ELF/gc-sections-alloc.s +++ lld/test/ELF/gc-sections-alloc.s @@ -20,7 +20,7 @@ # CHECK-NEXT: AddressAlignment: # CHECK-NEXT: EntrySize: # CHECK-NEXT: SectionData ( -# CHECK-NEXT: 0000: 00000000 00000000 | +# CHECK-NEXT: 0000: FEFFFFFF FFFFFFFF | # CHECK-NEXT: ) Index: lld/test/ELF/gc-sections-debuginfo.s =================================================================== --- /dev/null +++ lld/test/ELF/gc-sections-debuginfo.s @@ -0,0 +1,37 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux -dwarf-version=5 %p/Inputs/not_used.s -o %t1 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux -dwarf-version=5 %p/Inputs/main.s -o %t2 +# RUN: ld.lld -m elf_x86_64 -o %t3 %t1 %t2 --gc-sections +# RUN: llvm-dwarfdump -a %t3 | sed -n '/DW_AT_low_pc/ s|.*(\(0x[0-9a-f]*\)).*|\1|p' > %t4 +# RUN: llvm-symbolizer -obj=%t3 < %t4 | FileCheck %s + +# When -Wl,--gc-sections used - linker deleted unused text sections. +# But their debug info left in the binary. That could lead in incorrect +# source line information reported. That test checks that source line info +# reported correctly. + +# CHECK: main +# CHECK-NEXT: main.cpp:2:4 +# CHECK: main +# CHECK-NEXT: main.cpp:2:4 + +# The Input/not_used.s and Input/main.s was built +# from the following invocation and source: +# +# // not_used.cpp: +# +# void foo () { +# __asm__(".rept 3300000; nop; .endr"); +# } +# +# clang++ -gdwarf-5 -O not_used.cpp -o not_used.s -S +# +# // main.cpp: +# +# int main(void) { +# return 0; +# } +# +# clang++ -gdwarf-5 -O main.cpp -o main.s -S + + Index: lld/test/ELF/merge-gc-piece2.s =================================================================== --- lld/test/ELF/merge-gc-piece2.s +++ lld/test/ELF/merge-gc-piece2.s @@ -15,7 +15,7 @@ # CHECK-NEXT: AddressAlignment: # CHECK-NEXT: EntrySize: # CHECK-NEXT: SectionData ( -# CHECK-NEXT: 0000: 01000000 00000000 02000000 00000000 +# CHECK-NEXT: 0000: FEFFFFFF FFFFFFFF FEFFFFFF FFFFFFFF # CHECK-NEXT: ) .section .foo,"aM",@progbits,8 Index: lld/test/ELF/relocation-copy-flags.s =================================================================== --- lld/test/ELF/relocation-copy-flags.s +++ lld/test/ELF/relocation-copy-flags.s @@ -62,7 +62,7 @@ // CHECK-NEXT: AddressAlignment: 1 // CHECK-NEXT: EntrySize: 0 // CHECK-NEXT: SectionData ( -// CHECK-NEXT: 0000: 00000000 +// CHECK-NEXT: 0000: FEFFFFFF FFFFFFFF // CHECK-NEXT: ) // CHECK: Relocations [