From 54801d6fa52a184ec0be19a5a2bd3b33238c5ed4 Mon Sep 17 00:00:00 2001 From: Meador Inge Date: Wed, 23 Nov 2016 20:17:15 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287802 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Object/nm-trivial-object.test | 5 ++-- .../llvm-nm/X86/Inputs/weak.obj.elf-x86_64 | Bin 0 -> 1448 bytes test/tools/llvm-nm/X86/weak.test | 6 +++++ tools/llvm-nm/llvm-nm.cpp | 22 ++++++++++++------ 4 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 create mode 100644 test/tools/llvm-nm/X86/weak.test diff --git a/test/Object/nm-trivial-object.test b/test/Object/nm-trivial-object.test index def6549d656..c1f4d9e1f96 100644 --- a/test/Object/nm-trivial-object.test +++ b/test/Object/nm-trivial-object.test @@ -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 diff --git a/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 b/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 new file mode 100644 index 0000000000000000000000000000000000000000..8aa41bf4f37f0894b33366f5ecec8c2bd0bee6b4 GIT binary patch literal 1448 zcmb_bJ4*vW5S~kXzfiFdVsToCf-WiuA|jEX5ebT-g@uGmyb#btxSY?%P7oV^f~7x0 z@V5xI_JYoLw?j7TrSm&>cjnu9?9R^JZx*&vjza>6)~TrpOZ1v*%GpTF(g5|+(e~rV zC-kRI%qQ=d^SRurJCol%nst}el3HMD3_QcRJ0Ry~*vSlZ_dIi=3Fa_>5lkCH!w;$4 zU{`?+ptk|e*g;=EN!#hf8xW=WOG26>B_T~+Ni;|4Zo@}4XxYOZCXM)#33r_`&KcdE zxeO1NhmAQVT$_Y`?R#gc94*BwFNlT8WnEMLP3Q;p;vWL0fSef5+sEVkCq_Ru8Q>uc z@3IVlH(G*7HEwI6SK>7c;@@RoB0(u-k4UtEBcN`>R zng@8h1NX6+0N9$q`y;09f2fV5uf)gJ7kL9Q<(y(C{-Z+ZRr1Ta%Dl*?X~k3w*|y!& z{w+PjTvhw57`_mtenRiCkW~K#9Z>3w`s42uf8p`IcI}v=ZS8*y#ejW&==RxR{(_0& YY6tu)$ZhBEWiBcIoAz7deG_*6U%3EYssI20 literal 0 HcmV?d00001 diff --git a/test/tools/llvm-nm/X86/weak.test b/test/tools/llvm-nm/X86/weak.test new file mode 100644 index 00000000000..19bcd846ddd --- /dev/null +++ b/test/tools/llvm-nm/X86/weak.test @@ -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 diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 5e6dbcbf662..e89d1d31048 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -597,6 +597,10 @@ static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) { 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(SymbolicFile &Obj, bool printName, 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 -- 2.50.1