[LLVMdev] How to insert a self-written function to a piece of programme

15102925731 zhenkaixd at 126.com
Fri Apr 6 19:50:14 PDT 2012


 
Hi all,
         I wrote a function pass. In the pass, it ran through all the instruction that call to functions. When arriving at the exact function that I m interested in, it would insert a self-made function.

         Now I've finished the pass, the compilation is successful. The pass can find the function I like and can insert the CallInst to call my check function. BUT, when running the pass, it breaks down. The Diagnose is as follow,

************************************************************************************
Referencing function in another module!
  %CallCheck = call i32 @check()
Broken module found, compilation aborted!
0  opt       0x087aa95b
1  opt       0x087aa6e8
2            0x00d54400 __kernel_sigreturn + 0
3  libc.so.6 0x009c2a82 abort + 386
4  opt       0x08756f15
5  opt       0x08756c2d
6  opt       0x0873a18a llvm::FPPassManager::runOnFunction(llvm::Function&) + 306
7  opt       0x0873a34a llvm::FPPassManager::runOnModule(llvm::Module&) + 114
8  opt       0x0873a62a llvm::MPPassManager::runOnModule(llvm::Module&) + 400
9  opt       0x0873aae0 llvm::PassManagerImpl::run(llvm::Module&) + 122
10 opt       0x0873ada5 llvm::PassManager::run(llvm::Module&) + 39
11 opt       0x0827cec3 main + 4475
12 libc.so.6 0x009abbd6 __libc_start_main + 230
13 opt       0x0826e811
Stack dump:
0.    Program arguments: opt -load ../../../Debug+Asserts/lib/Hello.so -hello
1.    Running pass 'Function Pass Manager' on module '<stdin>'.
2.    Running pass 'Module Verifier' on function '@main'
*****************************************************************************************************************
*****************************************************************************************************************
The source code is like,

// my "check function"
int check()
{
    printf("Hello me!!\n");
    return 0;
}


//Create a function prototype exactly like "int check()", then create a call instruction to that function.
Module * M;
LLVMContext Context;
  FunctionType *STy=FunctionType::get(Type::getInt32Ty(Context), false);
  Function *check = Function::Create(STy, Function::InternalLinkage, "check" ,M);
  CallInst *callcheck = CallInst::Create(check,"CallCheck");

namespace {
struct Hello : public FunctionPass
    {
                     static char ID;
                      Hello() : FunctionPass(ID) {}
                      virtual bool runOnFunction(Function &F)
                      {
// run through all the instruction and find the one that call
"puts
                            for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)
                             {
                                for(BasicBlock::iterator II = BI->begin(),IE = BI->end();II != IE; ++II)
                                 {
                                        if(CallInst * III = dyn_cast<CallInst>(II))
                                         {

                                             if(III->getCalledFunction()!=NULL&&III->getCalledFunction()->getName()=="puts")
                                             {
                                                     errs() <<III->getCalledFunction()->getName()<<" function found!\n";
                                                callcheck->insertBefore(II);
                                                      errs() <<"INSERT SUCCEEDED!!!!!!!!\n";
                                              }
                                              else
                                             {
                                                  errs() <<"it's not main function!\n"<<"it is:"<<III->getCalledFunction()->getName()<<'\n';
                                              }
                                        }/**/
                           
                            }
************************************************************************************************************************************************************************************************************************************************************************************************
QUESTION: IS IT BECAUSE THE PASS CAN'T RECOGNIZE MY CHECK FUNCTION OR WHAT?? HOW TO MAKE THE PASS WORK AS EXPECTED???
THANK YOU!!


--

            祝好!
 
  甄凯
------------------------------------------------------------------------------------------------------
2012-04-07
------------------------------------------------------------------------------------------------------
Name: 甄凯(ZhenKai)
Homepage:http://www.renren.com/262729393
Email: zhenkaixd at 126.com or 846227103 at qq.com
TEL: 15810729006(Beijing)
Address: Room I-406, Central Building, Tsinghua University, Beijing, China. 100084.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120407/821935d4/attachment.html>


More information about the llvm-dev mailing list