title: How to link two LLVM bitcode modules?
body: <p>I have a simple LLVM pass that renames every function defined within the current translation unit (i.e: the source file in question, after all preprocessing steps have taken place - see <a href="https://stackoverflow.com/questions/7146425/llvm-translation-unit">here</a>). My pass is as follows:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  #include "llvm/Pass.h"
  #include "llvm/IR/Function.h"
  #include "llvm/Support/raw_ostream.h"
  #include "llvm/ADT/STLExtras.h"
  #include "llvm/ADT/SmallString.h"
  #include "llvm/IR/DerivedTypes.h"
  #include "llvm/IR/Module.h"
  #include "llvm/IR/Type.h"
  #include "llvm/IR/TypeFinder.h"
  #include "llvm/Transforms/IPO.h"
  #include "llvm/IR/Argument.h"
  #include "llvm/IR/GlobalValue.h"
  
  using namespace llvm;
  
  namespace {
  
    struct FunctionRename : public ModulePass {
      static char ID; // Pass identification
      FunctionRename() : ModulePass(ID) {}
  
      bool runOnModule(Module &amp;M) override {
        // Rename all functions
        for (auto &amp;F : M) {
          StringRef Name = F.getName();
          // Leave library functions alone because their presence or absence
          // could affect the behaviour of other passes.
          if (F.isDeclaration())
            continue;
          F.setLinkage(GlobalValue::LinkOnceAnyLinkage);
          F.setName(Name + "_renamed");
        }
        return true;
      }
    };
  }
  
  char FunctionRename::ID = 0;
  static RegisterPass&lt;FunctionRename&gt; X("functionrename", "Function Rename Pass");
  // ===-------------------------------------------------------==//
  //
  // Function Renamer - Renames all functions
  //
  </code></pre>
  
  <p>After running the pass over a bitcode file, <code>file.bc</code>, I output the result to a new file <code>file_renamed.bc</code>, as follows</p>
  
  <p><code>opt -load /path/to/libFunctionRenamePass.so -functionrename &lt; file.bc &gt; file_renamed.bc</code></p>
  
  <p>I then attempt to link the two files as follows</p>
  
  <p><code>llvm-link file.bc file_renamed.bc -o file_linked.bc</code></p>
  
  <p>However, I still get symbol clashes for C++ source files (from which the initial bitcode file is generated) where constructors and destructors are involved. My expectation was that the line</p>
  
  <p><code>F.setLinkage(GlobalValue::LinkOnceAnyLinkage)</code></p>
  
  <p>would prevent symbol clashes occurring for any symbols defined in <code>file.bc</code> and <code>file_renamed.bc</code>.</p>
  
  <p>What am I doing wrong? </p>