Index: lld/ELF/InputSection.cpp =================================================================== --- lld/ELF/InputSection.cpp +++ lld/ELF/InputSection.cpp @@ -805,6 +805,14 @@ if (Expr == R_NONE) continue; + // 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. + // https://bugs.llvm.org/show_bug.cgi?id=41124 + uint64_t OutOfModuleOffset = 0; + if (!Config->Relocatable && !Sym.getOutputSection() && Name.startswith(".debug_")) + OutOfModuleOffset = Out::ModuleHighPC; + if (Expr != R_ABS) { std::string Msg = getLocation(Offset) + ": has non-ABS relocation " + toString(Type) + @@ -822,15 +830,18 @@ // address 0. For bug-compatibilty, we accept them with warnings. We // know Steel Bank Common Lisp as of 2018 have this bug. warn(Msg); - Target->relocateOne(BufLoc, Type, - SignExtend64(Sym.getVA(Addend - Offset))); + Target->relocateOne( + BufLoc, Type, + SignExtend64(OutOfModuleOffset + Sym.getVA(Addend - Offset))); continue; } if (Sym.isTls() && !Out::TlsPhdr) Target->relocateOne(BufLoc, Type, 0); else - Target->relocateOne(BufLoc, Type, SignExtend64(Sym.getVA(Addend))); + Target->relocateOne( + BufLoc, Type, + SignExtend64(OutOfModuleOffset + Sym.getVA(Addend))); } } Index: lld/ELF/OutputSections.h =================================================================== --- lld/ELF/OutputSections.h +++ lld/ELF/OutputSections.h @@ -124,6 +124,7 @@ static OutputSection *PreinitArray; static OutputSection *InitArray; static OutputSection *FiniArray; + static uint64_t ModuleHighPC; }; } // namespace elf Index: lld/ELF/OutputSections.cpp =================================================================== --- lld/ELF/OutputSections.cpp +++ lld/ELF/OutputSections.cpp @@ -38,6 +38,7 @@ OutputSection *Out::PreinitArray; OutputSection *Out::InitArray; OutputSection *Out::FiniArray; +uint64_t Out::ModuleHighPC; std::vector elf::OutputSections; Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -503,6 +503,17 @@ if (Config->CheckSections) checkSections(); + // When all offsets already assigned - calculate Module High PC value. + // It would be used for relocations which point to deleted code. + // It is neccessary to set these broken relocations out of module range. + // To not to overlap with existing ranges. + // https://bugs.llvm.org/show_bug.cgi?id=41124 + Out::ModuleHighPC = 0; + for (auto *Sec : InputSections) + if (Sec && Sec->Live) + Out::ModuleHighPC = + std::max(Out::ModuleHighPC, Sec->getVA() + Sec->getSize()); + // It does not make sense try to open the file if we have error already. if (errorCount()) return; 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" md5 0xd1d454d67c808459c043506186a40ea8 + .file 1 "main.cpp" md5 0xd1d454d67c808459c043506186a40ea8 + .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 (https://github.com/llvm/llvm-project.git 29993f06f268f283e60f7f236d6f9aec63de1b7c)" # 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" md5 0x8d05a9888237934870a43c9699eda715 + .file 1 "not_used.cpp" md5 0x8d05a9888237934870a43c9699eda715 + .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 (https://github.com/llvm/llvm-project.git 29993f06f268f283e60f7f236d6f9aec63de1b7c)" # 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-debuginfo.s =================================================================== --- /dev/null +++ lld/test/ELF/gc-sections-debuginfo.s @@ -0,0 +1,41 @@ +# 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 | grep "DW_AT_low_pc.(" | sed -e 's|[_a-wyzA-WYZ() \t]||g' > %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: foo +# CHECK-NEXT: not_used.cpp:2:5 +# CHECK: foo +# CHECK: not_used.cpp:2:5 +# 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 + +