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 here). My pass is as follows:
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#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 &M) override {
// Rename all functions
for (auto &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<FunctionRename> X("functionrename", "Function Rename Pass");
// ===-------------------------------------------------------==//
//
// Function Renamer - Renames all functions
//
After running the pass over a bitcode file, file.bc
, I output the result to a new file file_renamed.bc
, as follows
opt -load /path/to/libFunctionRenamePass.so -functionrename < file.bc > file_renamed.bc
I then attempt to link the two files as follows
llvm-link file.bc file_renamed.bc -o file_linked.bc
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
F.setLinkage(GlobalValue::LinkOnceAnyLinkage)
would prevent symbol clashes occurring for any symbols defined in file.bc
and file_renamed.bc
.
What am I doing wrong?