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

Jordan Rupprecht via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 2 16:28:52 PDT 2018


rupprecht created this revision.
Herald added a subscriber: llvm-commits.

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


Repository:
  rL LLVM

https://reviews.llvm.org/D52810

Files:
  test/tools/llvm-nm/X86/nm-no-symbols.test
  tools/llvm-nm/llvm-nm.cpp


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) {
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{{$}}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52810.168051.patch
Type: text/x-patch
Size: 2149 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181002/b370f296/attachment.bin>


More information about the llvm-commits mailing list