[PATCH] D26417: Use debug info to print out the first line of symbol conflict message.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 8 12:55:31 PST 2016


ruiu created this revision.
ruiu added a reviewer: evgeny777.
ruiu added a subscriber: llvm-commits.

If there is a symbol conflict, we will print out an error message like this.

  foo.o (.text+0x100): duplicate symbol 'foobar'
  bar.o (.text+0x250): previous definition was here

We pass three arguments to reportDuplciate function, namely, the existing
symbol, the new conflicting symbol's section, and the offset of the new symbol.
If the existing is not a regular symbol, we fall back to a less user-friendly
message.

I found that we don't need the existing symbol to print out the first line of
the error message, so that error check was too strict. This patch simplifies
the function and relaxed the restriction.


https://reviews.llvm.org/D26417

Files:
  ELF/SymbolTable.cpp


Index: ELF/SymbolTable.cpp
===================================================================
--- ELF/SymbolTable.cpp
+++ ELF/SymbolTable.cpp
@@ -372,26 +372,29 @@
     error(Msg);
 }
 
-static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) {
+static void reportConflict(SymbolBody *Existing, InputFile *NewFile) {
   print("duplicate symbol " + conflictMsg(Existing, NewFile));
 }
 
+// Print out an error message, e.g.
+//
+//   foo.o (.text+0x100): duplicate symbol 'foobar'
+//   bar.o (.text+0x250): previous definition was here
+//
+// Unlike non-regular symbols, we may be able to use DWARF debug info to
+// get the precise error location to print out a user-friendly error message.
+// So, we have a separate function for regular symbols created from ELF.
 template <class ELFT>
-static void reportDuplicate(SymbolBody *Existing,
-                            InputSectionBase<ELFT> *ErrSec,
-                            typename ELFT::uint ErrOffset) {
-  DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing);
-  if (!D || !D->Section || !ErrSec) {
-    reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr);
-    return;
-  }
-
-  std::string OldLoc = getLocation(*D->Section, D->Value);
-  std::string NewLoc = getLocation(*ErrSec, ErrOffset);
+static void reportConflictRegular(SymbolBody *Existing,
+                                  InputSectionBase<ELFT> *ErrSec,
+                                  typename ELFT::uint ErrOffset) {
+  print(getLocation(*ErrSec, ErrOffset) + ": duplicate symbol '" +
+        maybeDemangle(Existing->getName()) + "'");
 
-  print(NewLoc + ": duplicate symbol '" + maybeDemangle(Existing->getName()) +
-        "'");
-  print(OldLoc + ": previous definition was here");
+  if (auto *D = dyn_cast<DefinedRegular<ELFT>>(Existing))
+    if (D->Section)
+      print(getLocation(*D->Section, D->Value) +
+            ": previous definition was here");
 }
 
 template <typename ELFT>
@@ -416,7 +419,7 @@
     replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, Value, Size,
                                       Section);
   else if (Cmp == 0)
-    reportDuplicate(S->body(), Section, Value);
+    reportConflictRegular(S->body(), Section, Value);
   return S;
 }
 
@@ -440,7 +443,7 @@
   if (Cmp > 0)
     replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
   else if (Cmp == 0)
-    reportDuplicate(S->body(), nullptr);
+    reportConflict(S->body(), nullptr);
   return S;
 }
 
@@ -477,7 +480,7 @@
   if (Cmp > 0)
     replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, F);
   else if (Cmp == 0)
-    reportDuplicate(S->body(), F);
+    reportConflict(S->body(), F);
   return S;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26417.77244.patch
Type: text/x-patch
Size: 2693 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161108/c7910f60/attachment.bin>


More information about the llvm-commits mailing list