Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -99,7 +99,7 @@ } template void elf::reportDiscarded(InputSectionBase *IS) { - if (!Config->PrintGcSections || !IS || IS->Live) + if (!Config->PrintGcSections || !IS || !IS->getFile() || IS->Live) return; errs() << "removing unused section from '" << IS->Name << "' in file '" << IS->getFile()->getName() << "'\n"; Index: test/ELF/lto/dynsym.ll =================================================================== --- test/ELF/lto/dynsym.ll +++ test/ELF/lto/dynsym.ll @@ -23,3 +23,8 @@ define void @foo() { ret void } + +; Check that we don't crash when gc'ing sections and printing the result. +; RUN: ld.lld -m elf_x86_64 %t2.o %t.so --gc-sections --print-gc-sections \ +; RUN: -o %t +; RUN: llvm-readobj -dyn-symbols %t | FileCheck %s