[PATCH] D61117: Fix Bug 41353 - unique symbols printed as D instead of u

Mike Pozulp via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 25 00:13:19 PDT 2019


mmpozulp created this revision.
mmpozulp added a reviewer: rupprecht.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

https://bugs.llvm.org/show_bug.cgi?id=41353

I'm new to LLVM and C++ so please do not hesitate to iterate with me on this fix.


Repository:
  rL LLVM

https://reviews.llvm.org/D61117

Files:
  llvm/include/llvm/Object/ELFTypes.h
  llvm/test/tools/llvm-nm/X86/Inputs/unique.s
  llvm/test/tools/llvm-nm/X86/unique.test
  llvm/tools/llvm-nm/llvm-nm.cpp


Index: llvm/tools/llvm-nm/llvm-nm.cpp
===================================================================
--- llvm/tools/llvm-nm/llvm-nm.cpp
+++ llvm/tools/llvm-nm/llvm-nm.cpp
@@ -923,6 +923,24 @@
   SymbolList.clear();
 }
 
+template <class ELFT>
+static bool isUniqueGlobalSymbol(const ELFObjectFile<ELFT> *Obj,
+                                 const BasicSymbolRef &Sym) {
+  return Obj->getSymbol(Sym.getRawDataRefImpl())->isUniqueGlobal();
+}
+
+static bool isELFUniqueGlobalSymbol(const BasicSymbolRef &Sym) {
+  const SymbolicFile *Obj = Sym.getObject();
+  if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
+    return isUniqueGlobalSymbol(ELF32LE, Sym);
+  if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
+    return isUniqueGlobalSymbol(ELF64LE, Sym);
+  if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
+    return isUniqueGlobalSymbol(ELF32BE, Sym);
+  auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
+  return isUniqueGlobalSymbol(ELF64BE, Sym);
+}
+
 static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
                                 basic_symbol_iterator I) {
   // OK, this is ELF
@@ -944,7 +962,7 @@
         return 't';
       case (ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE):
       case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
-        return 'd';
+        return isELFUniqueGlobalSymbol(*SymI) ? 'u' : 'd';
       case ELF::SHF_ALLOC:
       case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
       case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
@@ -1172,7 +1190,7 @@
   else
     Ret = getSymbolNMTypeChar(cast<ELFObjectFileBase>(Obj), I);
 
-  if (Symflags & object::SymbolRef::SF_Global)
+  if ((Symflags & object::SymbolRef::SF_Global) && !isELFUniqueGlobalSymbol(*I))
     Ret = toupper(Ret);
 
   return Ret;
Index: llvm/test/tools/llvm-nm/X86/unique.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-nm/X86/unique.test
@@ -0,0 +1,5 @@
+# RUN: clang -c %p/Inputs/unique.s -o %t
+# RUN: llvm-nm %t | FileCheck %s
+# Fixes https://bugs.llvm.org/show_bug.cgi?id=41353
+
+CHECK: 0000000000000000 u foo
Index: llvm/test/tools/llvm-nm/X86/Inputs/unique.s
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-nm/X86/Inputs/unique.s
@@ -0,0 +1,4 @@
+        .data
+        .type foo,%gnu_unique_object
+foo:
+        .byte 0
Index: llvm/include/llvm/Object/ELFTypes.h
===================================================================
--- llvm/include/llvm/Object/ELFTypes.h
+++ llvm/include/llvm/Object/ELFTypes.h
@@ -241,6 +241,8 @@
     return getBinding() != ELF::STB_LOCAL;
   }
 
+  bool isUniqueGlobal() const { return getBinding() == ELF::STB_GNU_UNIQUE; }
+
   Expected<StringRef> getName(StringRef StrTab) const;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61117.196584.patch
Type: text/x-patch
Size: 2787 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190425/10c0efe4/attachment.bin>


More information about the llvm-commits mailing list