[llvm-nm] Print an explicit "no symbols" message when an object file has no symbols
Summary:
GNU nm (and other nm implementations, such as "go tool nm") prints an explicit "no symbols" message when an object file has no symbols. Currently llvm-nm just doesn't print anything. Adding an explicit "no symbols" message will allow llvm-nm to be used in place of nm: some scripts and build processes use `nm <file> | grep "no symbols"` as a test to see if a file has no symbols. It will also be more familiar to anyone used to nm.
That said, the format implemented here is slightly different, in that it doesn't print the tool name in the message (which IMHO is not useful to include).
Demo:
```
$ for nm in nm bin/llvm-nm ; do echo "nm implementation: $nm"; $nm /tmp/foo{1,2}.o; echo; done
nm implementation: nm
/tmp/foo1.o:
nm: /tmp/foo1.o: no symbols
/tmp/foo2.o:
0000000000000000 T foo2
nm implementation: bin/llvm-nm
/tmp/foo1.o:
no symbols
/tmp/foo2.o:
0000000000000000 T foo2
```
Reviewers: MaskRay
Reviewed By: MaskRay
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D52810
llvm-svn: 343742
diff --git a/llvm/test/Object/nm-shared-object.test b/llvm/test/Object/nm-shared-object.test
index 975cf76..678e871 100644
--- a/llvm/test/Object/nm-shared-object.test
+++ b/llvm/test/Object/nm-shared-object.test
@@ -30,4 +30,5 @@
ERROR: File format has no dynamic symbol table.
-RUN: llvm-nm -D %p/Inputs/trivial-object-test.elf-i386 | count 0
+RUN: llvm-nm -D %p/Inputs/trivial-object-test.elf-i386 | FileCheck %s -check-prefix=NO-SYMBOLS
+NO-SYMBOLS: no symbols
diff --git a/llvm/test/ThinLTO/X86/empty-module.ll b/llvm/test/ThinLTO/X86/empty-module.ll
index 65edca2..63a09fb 100644
--- a/llvm/test/ThinLTO/X86/empty-module.ll
+++ b/llvm/test/ThinLTO/X86/empty-module.ll
@@ -3,7 +3,8 @@
; RUN: rm -f %t2.0
; RUN: llvm-lto2 run %t.bc -r %t.bc,foo,pl -o %t2 -thinlto-distributed-indexes
; RUN: llvm-readobj -h %t2.0 | FileCheck %s
-; RUN: llvm-nm %t2.0 | count 0
+; RUN: llvm-nm %t2.0 | FileCheck %s -check-prefix=NO-SYMBOLS
+; NO-SYMBOLS: no symbols
; CHECK: Format: ELF64-x86-64
diff --git a/llvm/test/tools/gold/X86/bcsection.ll b/llvm/test/tools/gold/X86/bcsection.ll
index 5527a1d..872e0e2 100644
--- a/llvm/test/tools/gold/X86/bcsection.ll
+++ b/llvm/test/tools/gold/X86/bcsection.ll
@@ -2,7 +2,9 @@
; RUN: llvm-as -o %t/bcsection.bc %s
; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-unknown-unknown -o %t/bcsection.bco %p/Inputs/bcsection.s
-; RUN: llvm-nm -no-llvm-bc %t/bcsection.bco | count 0
+; RUN: llvm-nm -no-llvm-bc %t/bcsection.bco | FileCheck %s -check-prefix=NO-SYMBOLS
+; NO-SYMBOLS: no symbols
+
; RUN: %gold -r -o %t/bcsection.o -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold%shlibext %t/bcsection.bco
; RUN: llvm-nm -no-llvm-bc %t/bcsection.o | FileCheck %s
diff --git a/llvm/test/tools/gold/X86/thinlto.ll b/llvm/test/tools/gold/X86/thinlto.ll
index c669a0b..19ef0a5 100644
--- a/llvm/test/tools/gold/X86/thinlto.ll
+++ b/llvm/test/tools/gold/X86/thinlto.ll
@@ -83,7 +83,8 @@
; RUN: --plugin-opt=obj-path=%t5.o \
; RUN: -shared %t.o %t2.o -o %t4
; RUN: llvm-readobj -h %t5.o | FileCheck %s --check-prefix=FORMAT
-; RUN: llvm-nm %t5.o | count 0
+; RUN: llvm-nm %t5.o | FileCheck %s -check-prefix=NO-SYMBOLS
+; NO-SYMBOLS: no symbols
; NM: T f
; NM2: T {{f|g}}
diff --git a/llvm/test/tools/llvm-nm/X86/nm-no-symbols.test b/llvm/test/tools/llvm-nm/X86/nm-no-symbols.test
new file mode 100644
index 0000000..1ae8308
--- /dev/null
+++ b/llvm/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{{$}}