Handle --as-needed with symbols, not relocations.

This matches the behavior of both gold and bfd.

llvm-svn: 267558
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 9ee44dc..bf2a4dc 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -521,9 +521,7 @@
   for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
     const RelTy &RI = *I;
     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
-    SymbolBody &OrigBody = File.getSymbolBody(SymIndex);
-    Symbol *Sym = OrigBody.Backref;
-    SymbolBody &Body = Sym ? *Sym->Body : OrigBody;
+    SymbolBody &Body = File.getSymbolBody(SymIndex).repl();
     uint32_t Type = RI.getType(Config->Mips64EL);
 
     // Ignore "hint" relocation because it is for optional code optimization.
@@ -534,11 +532,6 @@
     if (Offset == (uintX_t)-1)
       continue;
 
-    // Set "used" bit for --as-needed.
-    if (OrigBody.isUndefined() && Sym && !Sym->isWeak())
-      if (auto *S = dyn_cast<SharedSymbol<ELFT>>(&Body))
-        S->File->IsUsed = true;
-
     RelExpr Expr = Target->getRelExpr(Type, Body);
 
     // This relocation does not require got entry, but it is relative to got and
@@ -1344,6 +1337,12 @@
   std::vector<DefinedCommon *> CommonSymbols;
   for (Symbol *S : Symtab.getSymbols()) {
     SymbolBody *Body = S->Body;
+
+    // Set "used" bit for --as-needed.
+    if (S->IsUsedInRegularObj && !S->isWeak())
+      if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
+        SS->File->IsUsed = true;
+
     if (Body->isUndefined() && !S->isWeak()) {
       auto *U = dyn_cast<UndefinedElf<ELFT>>(Body);
       if (!U || !U->canKeepUndefined())