[lld] r361123 - Make replaceSymbol a member function of Symbol.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sun May 19 20:36:33 PDT 2019


Author: ruiu
Date: Sun May 19 20:36:33 2019
New Revision: 361123

URL: http://llvm.org/viewvc/llvm-project?rev=361123&view=rev
Log:
Make replaceSymbol a member function of Symbol.

This is a mechanical rewrite of replaceSymbol(A, B) to A->replace(B).
I also added a comment to Symbol::replace().

Technically this change is not necessary, but this change makes code a
bit more concise.

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

Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/LTO.cpp
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/Relocations.cpp
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/ELF/Symbols.h

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=361123&r1=361122&r2=361123&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Sun May 19 20:36:33 2019
@@ -1338,9 +1338,8 @@ static void replaceCommonSymbols() {
     Bss->File = S->File;
     Bss->Live = !Config->GcSections;
     InputSections.push_back(Bss);
-    replaceSymbol(S, Defined{S->File, S->getName(), S->Binding, S->StOther,
-                             S->Type,
-                             /*Value=*/0, S->Size, Bss});
+    S->replace(Defined{S->File, S->getName(), S->Binding, S->StOther, S->Type,
+                       /*Value=*/0, S->Size, Bss});
   }
 }
 
@@ -1355,8 +1354,7 @@ static void demoteSharedSymbols() {
       continue;
 
     bool Used = S->Used;
-    replaceSymbol(
-        S, Undefined{nullptr, S->getName(), STB_WEAK, S->StOther, S->Type});
+    S->replace(Undefined{nullptr, S->getName(), STB_WEAK, S->StOther, S->Type});
     S->Used = Used;
   }
 }

Modified: lld/trunk/ELF/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=361123&r1=361122&r2=361123&view=diff
==============================================================================
--- lld/trunk/ELF/LTO.cpp (original)
+++ lld/trunk/ELF/LTO.cpp Sun May 19 20:36:33 2019
@@ -196,8 +196,8 @@ void BitcodeCompiler::add(BitcodeFile &F
         !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf()));
 
     if (R.Prevailing)
-      replaceSymbol(Sym, Undefined{nullptr, Sym->getName(), STB_GLOBAL,
-                                   STV_DEFAULT, Sym->Type});
+      Sym->replace(Undefined{nullptr, Sym->getName(), STB_GLOBAL, STV_DEFAULT,
+                             Sym->Type});
 
     // We tell LTO to not apply interprocedural optimization for wrapped
     // (with --wrap) symbols because otherwise LTO would inline them while

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=361123&r1=361122&r2=361123&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Sun May 19 20:36:33 2019
@@ -188,7 +188,7 @@ void LinkerScript::addSymbol(SymbolAssig
 
   Symbol *Sym = Symtab->insert(Cmd->Name);
   mergeSymbolProperties(Sym, New);
-  replaceSymbol(Sym, New);
+  Sym->replace(New);
   Cmd->Sym = cast<Defined>(Sym);
 }
 
@@ -205,7 +205,7 @@ static void declareSymbol(SymbolAssignme
   // We can't calculate final value right now.
   Symbol *Sym = Symtab->insert(Cmd->Name);
   mergeSymbolProperties(Sym, New);
-  replaceSymbol(Sym, New);
+  Sym->replace(New);
 
   Cmd->Sym = cast<Defined>(Sym);
   Cmd->Provide = false;

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=361123&r1=361122&r2=361123&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Sun May 19 20:36:33 2019
@@ -530,8 +530,8 @@ static void replaceWithDefined(Symbol &S
                                uint64_t Size) {
   Symbol Old = Sym;
 
-  replaceSymbol(&Sym, Defined{Sym.File, Sym.getName(), Sym.Binding, Sym.StOther,
-                              Sym.Type, Value, Size, Sec});
+  Sym.replace(Defined{Sym.File, Sym.getName(), Sym.Binding, Sym.StOther,
+                      Sym.Type, Value, Size, Sec});
 
   Sym.PltIndex = Old.PltIndex;
   Sym.GotIndex = Old.GotIndex;

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=361123&r1=361122&r2=361123&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Sun May 19 20:36:33 2019
@@ -136,7 +136,7 @@ static void addUndefined(Symbol *Old, co
   // An undefined symbol with non default visibility must be satisfied
   // in the same DSO.
   if (Old->isShared() && New.Visibility != STV_DEFAULT) {
-    replaceSymbol(Old, New);
+    Old->replace(New);
     return;
   }
 
@@ -284,7 +284,7 @@ static void addCommon(Symbol *Old, const
     return;
 
   if (Cmp > 0) {
-    replaceSymbol(Old, New);
+    Old->replace(New);
     return;
   }
 
@@ -335,7 +335,7 @@ static void reportDuplicate(Symbol *Sym,
 static void addDefined(Symbol *Old, const Defined &New) {
   int Cmp = compare(Old, &New);
   if (Cmp > 0)
-    replaceSymbol(Old, New);
+    Old->replace(New);
   else if (Cmp == 0)
     reportDuplicate(Old, New.File,
                     dyn_cast_or_null<InputSectionBase>(New.Section), New.Value);
@@ -346,7 +346,7 @@ static void addShared(Symbol *Old, const
     // An undefined symbol with non default visibility must be satisfied
     // in the same DSO.
     uint8_t Binding = Old->Binding;
-    replaceSymbol(Old, New);
+    Old->replace(New);
     Old->Binding = Binding;
   }
 }
@@ -368,7 +368,7 @@ template <class LazyT> static void addLa
   // Symbols.h for the details.
   if (Old->isWeak()) {
     uint8_t Type = Old->Type;
-    replaceSymbol(Old, New);
+    Old->replace(New);
     Old->Type = Type;
     Old->Binding = STB_WEAK;
     return;
@@ -578,7 +578,7 @@ void elf::resolveSymbol(Symbol *Old, con
   mergeSymbolProperties(Old, New);
 
   if (Old->isPlaceholder()) {
-    replaceSymbol(Old, New);
+    Old->replace(New);
     return;
   }
 

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=361123&r1=361122&r2=361123&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Sun May 19 20:36:33 2019
@@ -83,7 +83,7 @@ public:
   // An index into the .branch_lt section on PPC64.
   uint16_t PPC64BranchltIndex = -1;
 
-  // Symbol binding. This is not overwritten by replaceSymbol to track
+  // Symbol binding. This is not overwritten by replace() to track
   // changes during resolution. In particular:
   //  - An undefined weak is still weak when it resolves to a shared library.
   //  - An undefined weak will not fetch archive members, but we have to
@@ -120,6 +120,8 @@ public:
   // True if this symbol is specified by --trace-symbol option.
   unsigned Traced : 1;
 
+  inline void replace(const Symbol &New);
+
   bool includeInDynsym() const;
   uint8_t computeBinding() const;
   bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
@@ -179,6 +181,8 @@ private:
     return Config->Shared || Config->ExportDynamic;
   }
 
+  inline size_t getSymbolSize() const;
+
 protected:
   Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
          uint8_t StOther, uint8_t Type)
@@ -427,27 +431,30 @@ static inline void assertSymbols() {
 
 void printTraceSymbol(Symbol *Sym);
 
-static size_t getSymbolSize(const Symbol &Sym) {
-  switch (Sym.kind()) {
-  case Symbol::CommonKind:
+size_t Symbol::getSymbolSize() const {
+  switch (kind()) {
+  case CommonKind:
     return sizeof(CommonSymbol);
-  case Symbol::DefinedKind:
+  case DefinedKind:
     return sizeof(Defined);
-  case Symbol::LazyArchiveKind:
+  case LazyArchiveKind:
     return sizeof(LazyArchive);
-  case Symbol::LazyObjectKind:
+  case LazyObjectKind:
     return sizeof(LazyObject);
-  case Symbol::SharedKind:
+  case SharedKind:
     return sizeof(SharedSymbol);
-  case Symbol::UndefinedKind:
+  case UndefinedKind:
     return sizeof(Undefined);
-  case Symbol::PlaceholderKind:
+  case PlaceholderKind:
     return sizeof(Symbol);
   }
   llvm_unreachable("unknown symbol kind");
 }
 
-inline void replaceSymbol(Symbol *Sym, const Symbol &New) {
+// replace() replaces "this" object with a given symbol by memcpy'ing
+// it over to "this". This function is called as a result of name
+// resolution, e.g. to replace an undefind symbol with a defined symbol.
+void Symbol::replace(const Symbol &New) {
   using llvm::ELF::STT_TLS;
 
   // Symbols representing thread-local variables must be referenced by
@@ -455,31 +462,30 @@ inline void replaceSymbol(Symbol *Sym, c
   // non-TLS relocations, so there's a clear distinction between TLS
   // and non-TLS symbols. It is an error if the same symbol is defined
   // as a TLS symbol in one file and as a non-TLS symbol in other file.
-  if (Sym->SymbolKind != Symbol::PlaceholderKind && !Sym->isLazy() &&
-      !New.isLazy()) {
-    bool TlsMismatch = (Sym->Type == STT_TLS && New.Type != STT_TLS) ||
-                       (Sym->Type != STT_TLS && New.Type == STT_TLS);
+  if (SymbolKind != PlaceholderKind && !isLazy() && !New.isLazy()) {
+    bool TlsMismatch = (Type == STT_TLS && New.Type != STT_TLS) ||
+                       (Type != STT_TLS && New.Type == STT_TLS);
     if (TlsMismatch)
-      error("TLS attribute mismatch: " + toString(*Sym) + "\n>>> defined in " +
-            toString(New.File) + "\n>>> defined in " + toString(Sym->File));
+      error("TLS attribute mismatch: " + toString(*this) + "\n>>> defined in " +
+            toString(New.File) + "\n>>> defined in " + toString(File));
   }
 
-  Symbol Old = *Sym;
-  memcpy(Sym, &New, getSymbolSize(New));
+  Symbol Old = *this;
+  memcpy(this, &New, New.getSymbolSize());
 
-  Sym->VersionId = Old.VersionId;
-  Sym->Visibility = Old.Visibility;
-  Sym->IsUsedInRegularObj = Old.IsUsedInRegularObj;
-  Sym->ExportDynamic = Old.ExportDynamic;
-  Sym->CanInline = Old.CanInline;
-  Sym->Traced = Old.Traced;
-  Sym->IsPreemptible = Old.IsPreemptible;
-  Sym->ScriptDefined = Old.ScriptDefined;
+  VersionId = Old.VersionId;
+  Visibility = Old.Visibility;
+  IsUsedInRegularObj = Old.IsUsedInRegularObj;
+  ExportDynamic = Old.ExportDynamic;
+  CanInline = Old.CanInline;
+  Traced = Old.Traced;
+  IsPreemptible = Old.IsPreemptible;
+  ScriptDefined = Old.ScriptDefined;
 
   // Print out a log message if --trace-symbol was specified.
   // This is for debugging.
-  if (Sym->Traced)
-    printTraceSymbol(Sym);
+  if (Traced)
+    printTraceSymbol(this);
 }
 
 void maybeWarnUnorderableSymbol(const Symbol *Sym);




More information about the llvm-commits mailing list