[llvm] r287802 - llvm-nm: Don't print value or size for undefined or weak symbols

Meador Inge via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 23 12:17:15 PST 2016


Author: meadori
Date: Wed Nov 23 14:17:15 2016
New Revision: 287802

URL: http://llvm.org/viewvc/llvm-project?rev=287802&view=rev
Log:
llvm-nm: Don't print value or size for undefined or weak symbols

Undefined and weak symbols don't have a meaningful size or value.
As such, nothing should be printed for those attributes (this is
already done for the address with 'U') with the BSD format.  This
matches what GNU nm does.

Note that for the POSIX.2 format [1] zero values are still
printed for the size and value.  This seems in spirit with
the format strings in that specification, but is debatable.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/

Differential Revision: https://reviews.llvm.org/D26936

Added:
    llvm/trunk/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64
    llvm/trunk/test/tools/llvm-nm/X86/weak.test
Modified:
    llvm/trunk/test/Object/nm-trivial-object.test
    llvm/trunk/tools/llvm-nm/llvm-nm.cpp

Modified: llvm/trunk/test/Object/nm-trivial-object.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/nm-trivial-object.test?rev=287802&r1=287801&r2=287802&view=diff
==============================================================================
--- llvm/trunk/test/Object/nm-trivial-object.test (original)
+++ llvm/trunk/test/Object/nm-trivial-object.test Wed Nov 23 14:17:15 2016
@@ -79,10 +79,9 @@ ELF:          U SomeOtherFunction
 ELF: 00000000 T main
 ELF:          U puts
 
-FIXME: we should not print the size of undefined symbols.
-ELF-SIZE:               00000000 U SomeOtherFunction
+ELF-SIZE:                        U SomeOtherFunction
 ELF-SIZE-NEXT: 00000000 00000024 T main
-ELF-SIZE-NEXT:          00000000 U puts
+ELF-SIZE-NEXT:                   U puts
 
 ELF-o: {{.*}}/trivial-object-test.elf-i386:          U SomeOtherFunction
 ELF-o: {{.*}}/trivial-object-test.elf-i386: 00000000 T main

Added: llvm/trunk/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64?rev=287802&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 (added) and llvm/trunk/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 Wed Nov 23 14:17:15 2016 differ

Added: llvm/trunk/test/tools/llvm-nm/X86/weak.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/X86/weak.test?rev=287802&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/X86/weak.test (added)
+++ llvm/trunk/test/tools/llvm-nm/X86/weak.test Wed Nov 23 14:17:15 2016
@@ -0,0 +1,6 @@
+# RUN: llvm-nm -B -S %p/Inputs/weak.obj.elf-x86_64 | FileCheck --match-full-lines %s
+
+CHECK:                  w weak_extern_func
+CHECK:                  w weak_extern_var
+CHECK: 0000000000000000 0000000000000011 W weak_func
+CHECK: 0000000000000000 0000000000000004 V weak_var

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=287802&r1=287801&r2=287802&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Wed Nov 23 14:17:15 2016
@@ -597,6 +597,10 @@ static void darwinPrintStab(MachOObjectF
   outs() << Str;
 }
 
+static bool symbolIsDefined(const NMSymbol &Sym) {
+  return Sym.TypeChar != 'U' && Sym.TypeChar != 'w' && Sym.TypeChar != 'v';
+}
+
 static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
                                    const std::string &ArchiveName,
                                    const std::string &ArchitectureName) {
@@ -683,24 +687,28 @@ static void sortAndPrintSymbolList(Symbo
     char SymbolAddrStr[18] = "";
     char SymbolSizeStr[18] = "";
 
-    if (OutputFormat == sysv || I->TypeChar == 'U') {
-      if (OutputFormat == posix)
+    // If the format is SysV or the symbol isn't defined, then print spaces.
+    if (OutputFormat == sysv || !symbolIsDefined(*I)) {
+      if (OutputFormat == posix) {
         format(printFormat, I->Address)
           .print(SymbolAddrStr, sizeof(SymbolAddrStr));
-      else
+        format(printFormat, I->Size)
+            .print(SymbolSizeStr, sizeof(SymbolSizeStr));
+      } else {
         strcpy(SymbolAddrStr, printBlanks);
+        strcpy(SymbolSizeStr, printBlanks);
+      }
     }
-    if (OutputFormat == sysv)
-      strcpy(SymbolSizeStr, printBlanks);
 
-    if (I->TypeChar != 'U') {
+    // Otherwise, print the symbol address and size.
+    if (symbolIsDefined(*I)) {
       if (Obj.isIR())
         strcpy(SymbolAddrStr, printDashes);
       else
         format(printFormat, I->Address)
           .print(SymbolAddrStr, sizeof(SymbolAddrStr));
+      format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
     }
-    format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
 
     // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
     // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's




More information about the llvm-commits mailing list