[PATCH] D34993: Hack to keep __real_foo

Rafael Ávila de Espíndola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 4 19:07:08 PDT 2017


rafael created this revision.
Herald added a subscriber: emaste.

This is a bad hack and should not be committed as is.

What it does is keep a copy of the __real_foo symbol when implementing --wrap.

It then creates a *new* symbol with the contents of __real_foo and puts it in the slot used by __wrap_foo. The reason for the new symbol is so that anything that was using __wrap_foo is not changed.

The net result is that

- Anything using foo now uses __wrap_foo
- Anything using __real_foo now uses foo.
- Anything using __wrap_foo still does.

And

- The slot for foo now has __wrap_foo
- The slot for __real_foo now has foo
- The slot for __wrap_foo now has __real_foo

Which I think is the desired behavior.


https://reviews.llvm.org/D34993

Files:
  ELF/Config.h
  ELF/SymbolTable.cpp
  test/ELF/lto/wrap-2.ll
  test/ELF/wrap.s


Index: test/ELF/wrap.s
===================================================================
--- test/ELF/wrap.s
+++ test/ELF/wrap.s
@@ -19,8 +19,8 @@
 // RUN: llvm-readobj -t -s %t3 | FileCheck -check-prefix=SYM %s
 // SYM:      Name: foo
 // SYM-NEXT: Value: 0x11000
-// SYM:      Name: __wrap_foo
-// SYM-NEXT: Value: 0x11010
+// SYM:      Name: __real_foo
+// SYM-NEXT: Value: 0x11020
 // SYM:      Name: __wrap_foo
 // SYM-NEXT: Value: 0x11010
 
Index: test/ELF/lto/wrap-2.ll
===================================================================
--- test/ELF/lto/wrap-2.ll
+++ test/ELF/lto/wrap-2.ll
@@ -14,11 +14,11 @@
 ; CHECK-NEXT: callq{{.*}}<bar>
 
 ; Check that bar and __wrap_bar retain their original binding.
-; BIND:      Name: bar
+; BIND:      Name: __wrap_bar
 ; BIND-NEXT: Value:
 ; BIND-NEXT: Size:
 ; BIND-NEXT: Binding: Local
-; BIND:      Name: __wrap_bar
+; BIND:      Name: bar
 ; BIND-NEXT: Value:
 ; BIND-NEXT: Size:
 ; BIND-NEXT: Binding: Local
Index: ELF/SymbolTable.cpp
===================================================================
--- ELF/SymbolTable.cpp
+++ ELF/SymbolTable.cpp
@@ -169,6 +169,11 @@
 
   Config->RenamedSymbols[Real] = {Sym, Real->Binding};
   Config->RenamedSymbols[Sym] = {Wrap, Sym->Binding};
+
+  auto It = Symtab.find(CachedHashStringRef(Wrap->body()->getName()));
+  std::pair<unsigned, Symbol *> Foo =
+      std::make_pair<unsigned, Symbol *>(It->second.Idx, (Symbol *)Real);
+  Config->WrapSymbols.push_back(Foo);
 }
 
 // Creates alias for symbol. Used to implement --defsym=ALIAS=SYM.
@@ -192,12 +197,23 @@
 // LTO (if LTO is running) not to include these symbols in IPO. Now that the
 // symbols are finalized, we can perform the replacement.
 template <class ELFT> void SymbolTable<ELFT>::applySymbolRenames() {
+  std::vector<Symbol> Origs;
+  Origs.resize(Config->WrapSymbols.size());
+  for (unsigned I = 0, N = Config->WrapSymbols.size(); I < N; ++I) {
+    Symbol *S = Config->WrapSymbols[I].second;
+    memcpy(&Origs[I], S, sizeof(Symbol));
+  }
   for (auto &KV : Config->RenamedSymbols) {
     Symbol *Dst = KV.first;
     Symbol *Src = KV.second.Target;
     Dst->body()->copy(Src->body());
     Dst->Binding = KV.second.OriginalBinding;
   }
+  for (unsigned I = 0, N = Config->WrapSymbols.size(); I < N; ++I) {
+    unsigned Idx = Config->WrapSymbols[I].first;
+    SymVector[Idx] = make<Symbol>();
+    memcpy(SymVector[Idx], &Origs[I], sizeof(Symbol));
+  }
 }
 
 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
Index: ELF/Config.h
===================================================================
--- ELF/Config.h
+++ ELF/Config.h
@@ -105,6 +105,7 @@
   std::vector<SymbolVersion> VersionScriptLocals;
   std::vector<uint8_t> BuildIdVector;
   llvm::MapVector<Symbol *, RenamedSymbol> RenamedSymbols;
+  std::vector<std::pair<unsigned, Symbol *>> WrapSymbols;
   bool AllowMultipleDefinition;
   bool AsNeeded = false;
   bool Bsymbolic;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34993.105207.patch
Type: text/x-patch
Size: 2928 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170705/f4400bb5/attachment-0001.bin>


More information about the llvm-commits mailing list