[PATCH] D69592: [ELF] Suggest unmangled names as an alternative spelling

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 15:40:39 PDT 2019


MaskRay created this revision.
MaskRay added reviewers: dblaikie, grimar, peter.smith, ruiu.
Herald added subscribers: llvm-commits, erik.pilkington, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

When missing an extern "C" declaration, an undefined reference may be
mangled while the definition is mangled. Suggest the unmangled name.
This kind of errors sometimes happen for FFI.

Use a simple heuristic that just extracts the string before `(`.

The reverse scenarios is possible but it is time consuming to demangle
every defined symbol and extract the name part. It is not implemented in
this patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69592

Files:
  lld/ELF/Relocations.cpp
  lld/test/ELF/undef-spell-corrector.s


Index: lld/test/ELF/undef-spell-corrector.s
===================================================================
--- lld/test/ELF/undef-spell-corrector.s
+++ lld/test/ELF/undef-spell-corrector.s
@@ -63,6 +63,14 @@
 # CONST-NEXT: >>> referenced by {{.*}}
 # CONST-NEXT: >>> did you mean: foo(int const*)
 
+## The reference may be mangled while the definition is not.
+# RUN: echo 'call _Z5abcdei' | llvm-mc -filetype=obj -triple=x86_64 - -o %t1.o
+# RUN: not ld.lld %t.o %t1.o -o /dev/null 2>&1 | FileCheck --check-prefix=MANGLED %s
+
+# MANGLED:      error: undefined symbol: abcde(int)
+# MANGLED-NEXT: >>> referenced by {{.*}}
+# MANGLED-NEXT: >>> did you mean: abcde
+
 .globl _start, abcde, _Z3fooPKi
 _start:
 abcde:
Index: lld/ELF/Relocations.cpp
===================================================================
--- lld/ELF/Relocations.cpp
+++ lld/ELF/Relocations.cpp
@@ -759,6 +759,16 @@
       return s;
   }
 
+  // The reference may be a mangled name while the definition is unmangled. Try
+  // suggesting the part before (.
+  std::string demangled = toString(sym);
+  auto paren = demangled.find('(');
+  if (paren != StringRef::npos) {
+    StringRef newName = demangled.substr(0, paren);
+    if (const Symbol *s = suggest(newName))
+      return s;
+  }
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69592.226984.patch
Type: text/x-patch
Size: 1299 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191029/4bee589a/attachment.bin>


More information about the llvm-commits mailing list