Wednesday, May 25, 2022

[SOLVED] LLVM cannot see static function

Issue

I am trying to instrument the linux kernel code to insert a function call in every function right after a BitCast instruction.

So I modify the C code to #include <linux/my_header.h> where I have my printer function.

The header looks something like this.

#ifndef __header_ID
#define __header_ID

static inline void print_typecast(...){
    printk(...);
}
#endif

Then I use Xclang to load my FunctionPass, which looks something like this.

// M is of type llvm::Module*
Function* f = M->getFunction("print_typecast");
if (f == nullptr) {
    errs() << "Function not found in the module\n";
}
else {
    // insert function in the code
}

However, my pass never finds the function in the module. When I remove the static it will find the function but then the linker in the final compilation step will complain of duplicate definition.

Anyone knows how to make LLVM "see" static imported/included functions?

Edit: I have also gone to the extreme where I have the same function directly written in every c file of the kernel code (the ones that #include <linux/kernel.h>)


Solution

static means that all calls to this function will be visible to this compiller now, and by implication that if the compiler sees no such calls, then it can skip compiling any output for the function, because you as programmer have promised that noone will want it.



Answered By - arnt
Answer Checked By - Senaida (WPSolving Volunteer)