Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -451,6 +451,8 @@ if (auto *S = dyn_cast(Sec)) if (!S->getSectionPiece(D->Value)->Live) return false; + } else if (auto *D = dyn_cast(&B)) { + return !Config->DefineCommon || (InX::Common && InX::Common->Live); } return true; } Index: test/ELF/linkerscript/discard-common.s =================================================================== --- test/ELF/linkerscript/discard-common.s +++ test/ELF/linkerscript/discard-common.s @@ -0,0 +1,10 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -triple x86_64-pc-linux %s -o %t.o -filetype=obj +# RUN: echo "SECTIONS { /DISCARD/ : { *(COMMON) } }" > %t.script +# RUN: ld.lld -o %t -T %t.script %t.o +# RUN: llvm-readobj -symbols -sections %t | FileCheck %s +# CHECK-NOT: .bss +# CHECK-NOT: foo + +.comm foo,4,4