From f74d99950d3f3e2f67859d190e404b27adfd41f7 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 llvm-svn: 287802 --- llvm/test/Object/nm-trivial-object.test | 5 ++--- .../tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 | Bin 0 -> 1448 bytes llvm/test/tools/llvm-nm/X86/weak.test | 6 ++++++ llvm/tools/llvm-nm/llvm-nm.cpp | 22 ++++++++++++++------- 4 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 llvm/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 create mode 100644 llvm/test/tools/llvm-nm/X86/weak.test diff --git a/llvm/test/Object/nm-trivial-object.test b/llvm/test/Object/nm-trivial-object.test index def6549..c1f4d9e 100644 --- a/llvm/test/Object/nm-trivial-object.test +++ b/llvm/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/llvm/test/tools/llvm-nm/X86/Inputs/weak.obj.elf-x86_64 b/llvm/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/llvm/test/tools/llvm-nm/X86/weak.test b/llvm/test/tools/llvm-nm/X86/weak.test new file mode 100644 index 0000000..19bcd84 --- /dev/null +++ b/llvm/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/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index 5e6dbcb..e89d1d3 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/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.7.4