diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -167,10 +167,12 @@ if (reloc.Type != R_WASM_TYPE_INDEX_LEB) { sym = symbols[reloc.Index]; - // We can end up with relocations against non-live symbols. For example - // in debug sections. + // We can end up with relocations against non-live symbols. For example + // in debug sections. We return reloc.Addend because always returning zero + // causes the generation of spurious range-list terminators in the + // .debug_ranges section. if ((isa(sym) || isa(sym)) && !sym->isLive()) - return 0; + return reloc.Addend; } switch (reloc.Type) { diff --git a/llvm/test/DebugInfo/WebAssembly/Inputs/nonlive-symbols1.c b/llvm/test/DebugInfo/WebAssembly/Inputs/nonlive-symbols1.c new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/WebAssembly/Inputs/nonlive-symbols1.c @@ -0,0 +1,5 @@ +extern int foo(); + +int _start() { + return foo(); +} diff --git a/llvm/test/DebugInfo/WebAssembly/Inputs/nonlive-symbols2.c b/llvm/test/DebugInfo/WebAssembly/Inputs/nonlive-symbols2.c new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/WebAssembly/Inputs/nonlive-symbols2.c @@ -0,0 +1,9 @@ +int foo() { + int b = 10; + return b + 2; +} + +int bar() { + int a = 10; + return a + 5; +} diff --git a/llvm/test/DebugInfo/WebAssembly/dbg-ranges-nonlive-symbols.test b/llvm/test/DebugInfo/WebAssembly/dbg-ranges-nonlive-symbols.test new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/WebAssembly/dbg-ranges-nonlive-symbols.test @@ -0,0 +1,16 @@ +RUN: rm -rf %t && mkdir -p %t +RUN: clang -gfull --target=wasm32 -ffunction-sections -fdata-sections -c %S/Inputs/nonlive-symbols1.c -o %t/nonlive-symbols1.o +RUN: clang -gfull --target=wasm32 -ffunction-sections -fdata-sections -c %S/Inputs/nonlive-symbols2.c -o %t/nonlive-symbols2.o +RUN: clang -Wl,--gc-sections %t/nonlive-symbols1.o %t/nonlive-symbols2.o -nostdlib --target=wasm32 -o %t/nonlive-symbols2.wasm +RUN: llvm-dwarfdump -v %t/nonlive-symbols2.wasm | FileCheck %s + +The second compilation unit (Inputs/nonlive-symbols2.c) contains a function +(bar) which can be GC'd by --gc-sections. When this happens, references to that +function will still exist in the `.debug_ranges` section. In this case the +linker should not emit a spurious range-list terminator entry (an entry with +Start==0 and End==0). + +CHECK: .debug_ranges contents: +CHECK: 00000000 {{[0-9]+}} {{[0-9]+}} +CHECK: 00000000 {{[0-9]+}} {{[0-9]+}} +CHECK: