[LLVMdev] How to construct parameters for a insert function? one from a return value from previous call, the other one is a string....

Shuying Liang shuying.liang at gmail.com
Wed Nov 17 17:04:57 PST 2010


Hi,
I want to insert a write operation to a fd opened, but I have some
questions to construct parameters to pass to function that I want to
insert, because one from a return value from previous call, the other
one is a string, The detailed description of the problem is as
follows;

I have:  int fd = open(g_settings_path, O_RDONLY);

and I want to insert this function after the instruction above:

write(fd, "something", strlen("something"));

note: for the write function prototype in linux system is:  ssize_t
write(int fd, const void *buf, size_t count);

Then I first insert function declaration into module using following,
which I am not quite sure:

fcall = M.getOrInsertFunction("write",
                    IntegerType::get(M.getContext(), 64),
                    IntegerType::get(M.getContext(), 32),
                    PointerType::get(IntegerType::get(Context, 8), 0),
                    IntegerType::get(M.getContext(), 64),
                    NULL);

After I find the position where I can insert the function,

I have questions and so many uncertainties to construct the parameters:

Firstly,  for the first argument construction, it is the value
returned from the open call, how can I use that as value to construct
Args[0]?
Args[0] = ConstantInt::get(Type::getInt32Ty(func->getContext()), 555);
// the 555 is the dummy value here, how can I get returned value from
open call and to replace it?

Secondly, How can I construct the second parameter? Am I doing right as follows?

Args[1] = ConstantArray::get(func->getContext(), "something", true);

The last Argument I can insert is the length of the string
"something", I think it should be right....
Args[2] = ConstantInt::get(Type::getInt32Ty(func->getContext()), 9);

and Lastly, I create a CallInstruction as follows,
CallInst::Create(fcall, Args.begin(), Args.end(), "", insertPos); //
insertPos is the Instruction * type, the position I want to insert
write call.

Does any one have some insight about it?
Thanks for your time and any suggestions in advance....


--Shuying



More information about the llvm-dev mailing list