[ELF] Only print symbol name when it is available

Not only symbols (like sections) have names, in case where we
fail to create relocation against such symbol, we should not
print out an empty string, instead we should print a generic
message.

Differential Revision: https://reviews.llvm.org/D23731

llvm-svn: 279459
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 0f03857..9a29483 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -401,9 +401,13 @@
 }
 
 template <class ELFT>
-static StringRef getLocalSymbolName(const elf::ObjectFile<ELFT> &File,
-                                    SymbolBody &Body) {
-  return File.getStringTable().data() + Body.getNameOffset();
+static StringRef getSymbolName(const elf::ObjectFile<ELFT> &File,
+                               SymbolBody &Body) {
+  if (Body.isLocal() && Body.getNameOffset())
+    return File.getStringTable().data() + Body.getNameOffset();
+  if (!Body.isLocal())
+    return Body.getName();
+  return "";
 }
 
 template <class ELFT>
@@ -428,10 +432,9 @@
   // only memory. We can hack around it if we are producing an executable and
   // the refered symbol can be preemepted to refer to the executable.
   if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
-    StringRef Name = Body.isLocal() ? getLocalSymbolName(File, Body)
-                                    : Body.getName();
+    StringRef Name = getSymbolName(File, Body);
     error("can't create dynamic relocation " + getRelName(Type) +
-          " against symbol " + Name);
+          " against " + (Name.empty() ? "readonly segment" : "symbol " + Name));
     return Expr;
   }
   if (Body.getVisibility() != STV_DEFAULT) {