[llvm-dev] How to inject calls to external functions via an LLVM plugin?

Alberto Barbaro via llvm-dev llvm-dev at lists.llvm.org
Mon Dec 27 00:38:09 PST 2021


Hi all,
I have been working on an llvm plugin which should create a .dot file
containing the execute IR code by a specific program. My idea is to inject
calls to external functions which would take care of the .dot file
creation. I would like to give you an idea on the structure of my project
so far:

libGraph: This is small part of the project which is in charge of creating
the .dot file

libMiddle: This is a sort wrapper which is able to use the libGraph project

plugin: This is the real .so which is passed to clang with -fpass-plugin
which is in charge of injecting the function calls to the functions exposed
by libMiddle.


libMiddle has a simple function called addNode with the following code:

```
extern "C" void addNodeMiddle(std::string name) {
    std::cout << name << "\n";
}
```

This function should take the LLVM IR code of a specific basic block and
print it on the screen.

The call to this function is injected by the plugin in this way:

```
//heavily based on the libInjectPrint from the llvm-tutor project
PointerType *GraphArgTy = PointerType::getUnqual(Type::getInt8Ty(CTX));

FunctionType *GraphTy = FunctionType::get(
            IntegerType::getVoidTy(CTX),
            GraphArgTy,
            false);

....

auto code = Builder.CreateGlobalStringPtr(OS.str());

Builder.CreateCall(
       GraphFunction, {code}
);
```

I can successfully run the plugin but unfortunately the output elf file
crashes. In my opinion the problem is related to how I pass the parameter
but I cannot fix it.

Can you suggest to me how to pass the content of code to an external
function? Do you think my way to call external functions is too complicated
and maybe I could simplify it?

Thanks
Alberto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20211227/38e8e317/attachment.html>


More information about the llvm-dev mailing list