Index: test/tools/llvm-nm/X86/nm-no-symbols.test =================================================================== --- /dev/null +++ test/tools/llvm-nm/X86/nm-no-symbols.test @@ -0,0 +1,14 @@ +# RUN: yaml2obj %s > %t.o +# RUN: llvm-nm %t.o | FileCheck %s +# RUN: llvm-nm --print-file-name %t.o | FileCheck %s --check-prefix=CHECK-PRINT-FILE-NAME + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + +# CHECK: {{^}}no symbols{{$}} + +# CHECK-PRINT-FILE-NAME: nm-no-symbols.test{{.*}}.o: no symbols{{$}} Index: tools/llvm-nm/llvm-nm.cpp =================================================================== --- tools/llvm-nm/llvm-nm.cpp +++ tools/llvm-nm/llvm-nm.cpp @@ -757,6 +757,24 @@ } } + auto writeFileName = [&]() { + if (!ArchitectureName.empty()) + outs() << "(for architecture " << ArchitectureName << "):"; + if (OutputFormat == posix && !ArchiveName.empty()) + outs() << ArchiveName << "[" << CurrentFilename << "]: "; + else { + if (!ArchiveName.empty()) + outs() << ArchiveName << ":"; + outs() << CurrentFilename << ": "; + } + }; + + if (SymbolList.empty()) { + if (PrintFileName) + writeFileName(); + outs() << "no symbols\n"; + } + for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end(); I != E; ++I) { uint32_t SymFlags; @@ -778,17 +796,8 @@ (!Global && ExternalOnly) || (SizeSort && !PrintAddress) || (Weak && NoWeakSymbols)) continue; - if (PrintFileName) { - if (!ArchitectureName.empty()) - outs() << "(for architecture " << ArchitectureName << "):"; - if (OutputFormat == posix && !ArchiveName.empty()) - outs() << ArchiveName << "[" << CurrentFilename << "]: "; - else { - if (!ArchiveName.empty()) - outs() << ArchiveName << ":"; - outs() << CurrentFilename << ": "; - } - } + if (PrintFileName) + writeFileName(); if ((JustSymbolName || (UndefinedOnly && MachO && OutputFormat != darwin)) && OutputFormat != posix) {