[PATCH] D45516: [ELF] - Refactor lazy symbol duplicated code.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 11 05:51:06 PDT 2018


grimar created this revision.
grimar added reviewers: ruiu, espindola.
Herald added subscribers: arichardson, emaste.

Our code for LazyObject and LazyArchive duplicates.

This patch extracts the common part to remove
the duplication.


https://reviews.llvm.org/D45516

Files:
  ELF/SymbolTable.cpp
  ELF/SymbolTable.h


Index: ELF/SymbolTable.h
===================================================================
--- ELF/SymbolTable.h
+++ ELF/SymbolTable.h
@@ -97,6 +97,9 @@
                           StringRef VersionName);
   void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId);
 
+  using ReplaceFn = llvm::function_ref<void(Symbol *S, uint8_t Type)>;
+  bool replaceOrIgnoreLazy(StringRef Name, ReplaceFn Replace);
+
   // The order the global symbols are in is not defined. We can use an arbitrary
   // order, but it has to be reproducible. That is true even when cross linking.
   // The default hashing of StringRef produces different results on 32 and 64
Index: ELF/SymbolTable.cpp
===================================================================
--- ELF/SymbolTable.cpp
+++ ELF/SymbolTable.cpp
@@ -598,47 +598,47 @@
 template <class ELFT>
 void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F,
                                  const object::Archive::Symbol Sym) {
-  Symbol *S;
-  bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-  if (WasInserted) {
-    replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType);
-    return;
-  }
-  if (!S->isUndefined())
-    return;
+  auto ReplaceFn = [&](Symbol *S, uint8_t Type) {
+    replaceSymbol<LazyArchive>(S, F, Sym, Type);
+  };
 
-  // An undefined weak will not fetch archive members. See comment on Lazy in
-  // Symbols.h for the details.
-  if (S->isWeak()) {
-    replaceSymbol<LazyArchive>(S, F, Sym, S->Type);
-    S->Binding = STB_WEAK;
-    return;
-  }
-  if (InputFile *File = F.fetch(Sym))
-    addFile<ELFT>(File);
+  if (!replaceOrIgnoreLazy(Name, ReplaceFn))
+    if (InputFile *File = F.fetch(Sym))
+      addFile<ELFT>(File);
 }
 
 template <class ELFT>
 void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
+  auto ReplaceFn = [&](Symbol *S, uint8_t Type) {
+    replaceSymbol<LazyObject>(S, Obj, Name, Type);
+  };
+
+  if (!replaceOrIgnoreLazy(Name, ReplaceFn))
+    if (InputFile *F = Obj.fetch())
+      addFile<ELFT>(F);
+}
+
+// This is used to handle lazy symbols. Returns true if symbol was replaced
+// with Replace or should be ignored.
+bool SymbolTable::replaceOrIgnoreLazy(StringRef Name, ReplaceFn Replace) {
   Symbol *S;
   bool WasInserted;
   std::tie(S, WasInserted) = insert(Name);
   if (WasInserted) {
-    replaceSymbol<LazyObject>(S, Obj, Name, Symbol::UnknownType);
-    return;
+    Replace(S, Symbol::UnknownType);
+    return true;
   }
   if (!S->isUndefined())
-    return;
+    return true;
 
-  // See comment for addLazyArchive above.
+  // An undefined weak will not fetch archive members. See comment on Lazy in
+  // Symbols.h for the details.
   if (S->isWeak()) {
-    replaceSymbol<LazyObject>(S, Obj, Name, S->Type);
+    Replace(S, S->Type);
     S->Binding = STB_WEAK;
-    return;
+    return true;
   }
-  if (InputFile *F = Obj.fetch())
-    addFile<ELFT>(F);
+  return false;
 }
 
 template <class ELFT> void SymbolTable::fetchLazy(Symbol *Sym) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45516.141984.patch
Type: text/x-patch
Size: 3012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180411/bf09fae7/attachment.bin>


More information about the llvm-commits mailing list