Git Product home page Git Product logo

Comments (11)

LuoRongLuoRong avatar LuoRongLuoRong commented on July 22, 2024 2

Thank you, sir! 😁😁 Today, I finnally understand what you told me.

image

(Because I am a green hand in C and C++, so I didn't understand what extern "C" means the other day.)

Hope it will help other students like me :)

from llvm-pass-skeleton.

sampsyo avatar sampsyo commented on July 22, 2024

I've never done it, but if I did, I would probably use extern "C" to avoid mangling in the run-time hooks. (You can of course still call into general C++ from there.)

from llvm-pass-skeleton.

 avatar commented on July 22, 2024

Well I'll give that a try.

from llvm-pass-skeleton.

 avatar commented on July 22, 2024

It worked! Thank you.

from llvm-pass-skeleton.

sampsyo avatar sampsyo commented on July 22, 2024

Woohoo!

from llvm-pass-skeleton.

LuoRongLuoRong avatar LuoRongLuoRong commented on July 22, 2024

And I world like to share my experiment.
I changed content of rtlib.c and example.c from c to cpp.
Then I run:

(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton$ ls
build  CMakeLists.txt  example.cpp  LICENSE  README.md  rtlib.cpp  skeleton
(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton$ g++ -c rtlib.cpp 
(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton$ clang++  -flegacy-pass-manager  -Xclang -load -Xclang build/skeleton/libSkeletonPass.so -c example.cpp
(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton$ ls
build           example.cpp  LICENSE    rtlib.cpp  skeleton
CMakeLists.txt  example.o    README.md  rtlib.o
(base) fdse@ubuntu:~/luorong/LLVM/test/llvm-pass-skeleton$ g++ example.o rtlib.o
example.o: In function `main':
example.cpp:(.text+0x35): undefined reference to `logop'
collect2: error: ld returned 1 exit status

Yes, it cannot find logop. So I check the difference between the ll file of rtlib.c and that of rtlib.cpp.

rtlib.c 's  ll
define dso_local void @logop(i32 %0) #0 !dbg !9

rtlib.cpp 's  ll
define dso_local void @_Z5logopi(i32 %0) #0 !dbg !9

So I changed the Skeleton.cpp from F.getParent()->getOrInsertFunction("logop", logFuncType); to F.getParent()->getOrInsertFunction("_Z5logopi", logFuncType);. And then it worked.

Hope it's helpful.

from llvm-pass-skeleton.

sampsyo avatar sampsyo commented on July 22, 2024

If I were writing a run-time library in C++, I would use extern "C" to export a non-mangled name.

from llvm-pass-skeleton.

LuoRongLuoRong avatar LuoRongLuoRong commented on July 22, 2024

If I were writing a run-time library in C++, I would use extern "C" to export a non-mangled name.

Thank you for your reply! I don't understand export a non-mangled name. What is a non-mangled name? My translator told me it means 'an unchanged name'. Does it mean that if I use extern C, the name of functions in LLVM IR won't change? For example, in rtlib.cpp's ll file, the function name logop is still logop?

And another problem: My application is to analyse C++ by using LLVM pass. So, will extern "c" destroy other functions of C++?

Thank you!😉

from llvm-pass-skeleton.

sampsyo avatar sampsyo commented on July 22, 2024

I unfortunately can't help walk through the mechanics here, but the relevant term to Google is "C++ name mangling".

An extern "C" runtime function should not prevent you from instrumenting C++ programs.

from llvm-pass-skeleton.

LuoRongLuoRong avatar LuoRongLuoRong commented on July 22, 2024

Hi, I still have some primary problems about using string in llvm pass.
I wanna use string as argument, like that:

extern "C" void logvar(int i, char* name) {
    std::cout << "Num: " << i << "; Name: " << name << std::endl;
}

And pass.cpp

// Get the function to call from our runtime library.
LLVMContext &Ctx = F.getContext();
std::vector<Type*> paramTypes = {  // Param Types
  Type::getInt32Ty(Ctx),
  Type::getInt8PtrTy(Ctx) 
};
Type *retType = Type::getVoidTy(Ctx);
FunctionType *logFuncType = FunctionType::get(retType, paramTypes, false);
FunctionCallee logFunc = F.getParent()->getOrInsertFunction("logvar", logFuncType);

Insert logvar in pass.cpp
for store instruction such as store i32 0, i32* %retval, align 4

// for store instruction
void insertLogvar(StoreInst *inst, BasicBlock &B, FunctionCallee logFunc, LLVMContext &Ctx) {
  IRBuilder<> builder(inst);
  builder.SetInsertPoint(&B, ++builder.GetInsertPoint());

  Value *argi = inst->getOperand(0);  // integer
  if (auto constant_int = dyn_cast<ConstantInt>(argi)) {
    Value* argstr = ???   // HERE
    Value* args[] = {argi, argstr}; 
    builder.CreateCall(logFunc, args);
  } else {
    errs() << "store inst has no instance number" << "\n";
  }
}

My question is how to accomplish argstr? To match string type.

Best wishes!

from llvm-pass-skeleton.

LuoRongLuoRong avatar LuoRongLuoRong commented on July 22, 2024

I found the solution Value *argstr = builder.CreateGlobalString(arg2ins->getName()); 😁

from llvm-pass-skeleton.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.