Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function in LLVM

I would like to ask you on the proper way to call/bind own c++ function into LLVM.

I have coded simple function:

void writeSomething() {
    std::cout << "Awesome" << std::endl;
} 

In LLVM I am trying to register the function. I have created the external linkage to it.

      // Void type
llvm::FunctionType* fccType =
        llvm::FunctionType::get(
            llvm::Type::getVoidTy(getGlobalContext()), false
        );

// External - c++
Function *fcc = (Function*) module->getOrInsertFunction("writeSomething",
        fccType
        );

// Call
std::vector<Value*> emptyArgs;
CallInst::Create(fcc, makeArrayRef(emptyArgs));

LLVM Output for just calling this function is ( // comments are mine input how do I understand the output )

// External linkage
declare void @writeSomething()

define internal void @main() {
entry:
  // Call my function
  call void @writeSomething()
  ret void
}

The program ends with message: LLVM ERROR: Program used external function 'writeSomething' which could not be resolved!

like image 390
Přemysl Fiala Avatar asked Apr 27 '15 18:04

Přemysl Fiala


1 Answers

Due to C++ name mangling, the name of that function is actually something like _Z14writeSomethingv - C++ supports overloading by encoding type information in the function name.

You can disable this by declaring the function as extern "C" void writeSomething() { ... }, or figure out what it should be called under your compiler's name mangling scheme and use that.

like image 126
Emily Avatar answered Oct 14 '22 16:10

Emily