c++ - How to link two LLVM bitcode modules? -
i have simple llvm pass renames every function defined within current translation unit (i.e: source file in question, after preprocessing steps have taken place - see here). pass 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 functions (auto &f : m) { stringref name = f.getname(); // leave library functions alone because presence or absence // affect 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 functions //
after running pass on bitcode file, file.bc
, output result new file file_renamed.bc
, follows
opt -load /path/to/libfunctionrenamepass.so -functionrename < file.bc > file_renamed.bc
i attempt link 2 files follows
llvm-link file.bc file_renamed.bc -o file_linked.bc
however, still symbol clashes c++ source files (from initial bitcode file generated) constructors , destructors involved. expectation line
f.setlinkage(globalvalue::linkonceanylinkage)
would prevent symbol clashes occurring symbols defined in file.bc
, file_renamed.bc
.
what doing wrong?
when tried running code on sample bitcode file, llvm-link step failed due global variables:
error: linking globals named 'my_global': symbol multiply defined!
after adding second loop runonmodule routine process global variables, llvm-link succeeds , code linked.
for (auto git = m.global_begin(), = m.global_end(); git != get; ++git) { globalvalue* gv = &*git; gv->setlinkage(globalvalue::linkonceanylinkage); }
however, simple test of c++ code constructors worked both , without change.
Comments
Post a Comment