[llvm] r343742 - [llvm-nm] Print an explicit "no symbols" message when an object file has no symbols

Jordan Rupprecht via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 3 16:39:50 PDT 2018


Author: rupprecht
Date: Wed Oct  3 16:39:49 2018
New Revision: 343742

URL: http://llvm.org/viewvc/llvm-project?rev=343742&view=rev
Log:
[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

Added:
    llvm/trunk/test/tools/llvm-nm/X86/nm-no-symbols.test
Modified:
    llvm/trunk/test/Object/nm-shared-object.test
    llvm/trunk/test/ThinLTO/X86/empty-module.ll
    llvm/trunk/test/tools/gold/X86/bcsection.ll
    llvm/trunk/test/tools/gold/X86/thinlto.ll
    llvm/trunk/tools/llvm-nm/llvm-nm.cpp

Modified: llvm/trunk/test/Object/nm-shared-object.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/nm-shared-object.test?rev=343742&r1=343741&r2=343742&view=diff
==============================================================================
--- llvm/trunk/test/Object/nm-shared-object.test (original)
+++ llvm/trunk/test/Object/nm-shared-object.test Wed Oct  3 16:39:49 2018
@@ -30,4 +30,5 @@ RUN:         | FileCheck %s -check-prefi
 
 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

Modified: llvm/trunk/test/ThinLTO/X86/empty-module.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ThinLTO/X86/empty-module.ll?rev=343742&r1=343741&r2=343742&view=diff
==============================================================================
--- llvm/trunk/test/ThinLTO/X86/empty-module.ll (original)
+++ llvm/trunk/test/ThinLTO/X86/empty-module.ll Wed Oct  3 16:39:49 2018
@@ -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
 

Modified: llvm/trunk/test/tools/gold/X86/bcsection.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/gold/X86/bcsection.ll?rev=343742&r1=343741&r2=343742&view=diff
==============================================================================
--- llvm/trunk/test/tools/gold/X86/bcsection.ll (original)
+++ llvm/trunk/test/tools/gold/X86/bcsection.ll Wed Oct  3 16:39:49 2018
@@ -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
 

Modified: llvm/trunk/test/tools/gold/X86/thinlto.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/gold/X86/thinlto.ll?rev=343742&r1=343741&r2=343742&view=diff
==============================================================================
--- llvm/trunk/test/tools/gold/X86/thinlto.ll (original)
+++ llvm/trunk/test/tools/gold/X86/thinlto.ll Wed Oct  3 16:39:49 2018
@@ -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}}

Added: llvm/trunk/test/tools/llvm-nm/X86/nm-no-symbols.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/X86/nm-no-symbols.test?rev=343742&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/X86/nm-no-symbols.test (added)
+++ llvm/trunk/test/tools/llvm-nm/X86/nm-no-symbols.test Wed Oct  3 16:39:49 2018
@@ -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{{$}}

Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=343742&r1=343741&r2=343742&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Wed Oct  3 16:39:49 2018
@@ -757,6 +757,24 @@ static void sortAndPrintSymbolList(Symbo
     }
   }
 
+  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 @@ static void sortAndPrintSymbolList(Symbo
         (!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) {




More information about the llvm-commits mailing list