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

15102925731 zhenkaixd at 126.com
Sat Apr 7 17:22:32 PDT 2012


Thanks for your reply!  I send the message to the mail list this time~

Did you mean I need to initialize M and the Context? Is that really necessary? Does all the function need to be wrapped in the module? I remember you once said "you can just declare the function (i.e. no need to give it a body), and call it.You can then link with an object file that defines it. This is simpler than injecting the function into each module."
I DEFINITELY GO FOR THE SIMPLER ONE! That is to say, I simply give the declaration and then the body of the function in C language(not in LLVM API), then I just use the CallInst to call that "check function". Could you be kind and  show me how exactly to define that "int check()" function and then "link it with an object file"? I really need your help!!!

Thank you!


--

            祝好!
 
  甄凯
------------------------------------------------------------------------------------------------------
2012-04-08
------------------------------------------------------------------------------------------------------
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.

At 2012-04-07 10:43:06,15102925731 <zhenkaixd at 126.com> wrote:

 
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/20120408/63f4c0bf/attachment.html>


More information about the llvm-dev mailing list