On Mon, Jan 14, 2019 at 3:21 AM Gaier, Bjoern via llvm-dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>> wrote: Hello LLVM-World, currently I play around with a „llvm::Module” and an external function defined there “puts”. Normally this function gets resolved in the JIT-Process but I wonder about two things: 1. Can I resolve the function already in this step? I used “replaceAllUsesWith” and passed a “llvm::ConstantInt” to the function. But this didn’t worked. I'm not sure if I followed correctly, but you can't replace a function declaration with a constant int. The value for the function will be used (mostly) as an argument to the call instruction, which won't really be able to operate on an int. It isn't clear to me what you're trying to do exactly, replace the call to puts with an indirect call to the pointer value that correspond to puts in your current process? You would need to at least cast this pointer value to the right type, and use an indirect call. Hey Mehdi, I simply try overloading “puts” with a custom address before I start the JIT process. I want to replace the call to “puts” with my own address. For a test purpose I tried this now: mainModue->getFunction("puts")->replaceAllUsesWith( llvm::ConstantExpr::getPointerCast( llvm::ConstantInt::get(llvm::IntegerType::get(context, 64), 0xF), mainModue->getFunction("puts")->getType() ) ); This code will crash the application though. I know that 0xF is not a correct address and that there can be stuff optimized, but currently I just want to ‘see’ that “puts” was overloaded with 0xF. Kind greetings Björn Best, -- Mehdi 1. 2. What might happen if I have two modules and use “replaceAllUsesWith” on a function of Module A, passing a function of Module B. Thank you for the help in advance! Kind greetings Björn Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika _______________________________________________ LLVM Developers mailing list llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190115/fbccbcab/attachment.html>
Stefan Gränitz via llvm-dev
2019-Jan-15 11:41 UTC
[llvm-dev] Function - replaceAllUsesWith
Hi Björn What does the IR code look like when you dump your module? llvm::outs() << mainModule; I guess for the external function "puts" it will have a declaration like this: declare i32 @puts(i8*) When loading your module in the JIT, RuntimeDyld will resolve it to an actual address. This lookup is done by name and it happens in a stage called external symbol resolution. You should be able to inspect how this works by setting a breakpoint here: https://github.com/llvm/llvm-project/blob/cce1c2eb0e87e84be6345c870d3334f748bdc5bb/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp#L1084 Hope it helps. Stefan Am 15.01.19 um 08:02 schrieb Gaier, Bjoern via llvm-dev:> > On Mon, Jan 14, 2019 at 3:21 AM Gaier, Bjoern via llvm-dev > <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > > Hello LLVM-World, > > > > currently I play around with a „llvm::Module” and an external > function defined there “puts”. Normally this function gets > resolved in the JIT-Process but I wonder about two things: > > > > 1. Can I resolve the function already in this step? I used > “replaceAllUsesWith” and passed a “llvm::ConstantInt” to the > function. But this didn’t worked. > > > > I'm not sure if I followed correctly, but you can't replace a function > declaration with a constant int. The value for the function will be > used (mostly) as an argument to the call instruction, which won't > really be able to operate on an int. It isn't clear to me what you're > trying to do exactly, replace the call to puts with an indirect call > to the pointer value that correspond to puts in your current process? > You would need to at least cast this pointer value to the right type, > and use an indirect call. > > > > Hey Mehdi, > > > > I simply try overloading “puts” with a custom address before I start > the JIT process. I want to replace the call to “puts” with my own > address. For a test purpose I tried this now: > mainModue->getFunction("puts")->replaceAllUsesWith( > > llvm::ConstantExpr::getPointerCast( > > > llvm::ConstantInt::get(llvm::IntegerType::get(context, 64), 0xF), > > mainModue->getFunction("puts")->getType() > > ) > > ); > > This code will crash the application though. I know that 0xF is not a > correct address and that there can be stuff optimized, but currently I > just want to ‘see’ that “puts” was overloaded with 0xF. > > > > Kind greetings > > Björn > > > > Best, > > > > -- > > Mehdi > > > > > > > > 1. > 2. What might happen if I have two modules and use > “replaceAllUsesWith” on a function of Module A, passing a > function of Module B. > > > > Thank you for the help in advance! > > > > Kind greetings > > Björn > > Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB > 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi > Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi > Nagano, Takeshi Fukushima. Junichi Tajika > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > > Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, > USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. > Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi > Fukushima. Junichi Tajika > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-- https://weliveindetail.github.io/blog/ https://cryptup.org/pub/stefan.graenitz at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190115/a5319a77/attachment-0001.html>
Hey Stefan, The function looks like this: “; Function Attrs: nounwind declare i32 @puts(i8* nocapture readonly) local_unnamed_addr #2” I wondered if I can do the external symbol resolution without using the JIT and RuntimeDyld (for now). For me it sounds like something that could be possible… Kind greetings Björn From: Stefan Gränitz <stefan.graenitz at gmail.com> Sent: Dienstag, 15. Januar 2019 12:42 To: Gaier, Bjoern <Bjoern.Gaier at horiba.com>; Mehdi AMINI <joker.eph at gmail.com> Cc: llvm-dev <llvm-dev at lists.llvm.org> Subject: Re: [llvm-dev] Function - replaceAllUsesWith Hi Björn What does the IR code look like when you dump your module? llvm::outs() << mainModule; I guess for the external function "puts" it will have a declaration like this: declare i32 @puts(i8*) When loading your module in the JIT, RuntimeDyld will resolve it to an actual address. This lookup is done by name and it happens in a stage called external symbol resolution. You should be able to inspect how this works by setting a breakpoint here: https://github.com/llvm/llvm-project/blob/cce1c2eb0e87e84be6345c870d3334f748bdc5bb/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp#L1084 Hope it helps. Stefan Am 15.01.19 um 08:02 schrieb Gaier, Bjoern via llvm-dev: On Mon, Jan 14, 2019 at 3:21 AM Gaier, Bjoern via llvm-dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>> wrote: Hello LLVM-World, currently I play around with a „llvm::Module” and an external function defined there “puts”. Normally this function gets resolved in the JIT-Process but I wonder about two things: 1. Can I resolve the function already in this step? I used “replaceAllUsesWith” and passed a “llvm::ConstantInt” to the function. But this didn’t worked. I'm not sure if I followed correctly, but you can't replace a function declaration with a constant int. The value for the function will be used (mostly) as an argument to the call instruction, which won't really be able to operate on an int. It isn't clear to me what you're trying to do exactly, replace the call to puts with an indirect call to the pointer value that correspond to puts in your current process? You would need to at least cast this pointer value to the right type, and use an indirect call. Hey Mehdi, I simply try overloading “puts” with a custom address before I start the JIT process. I want to replace the call to “puts” with my own address. For a test purpose I tried this now: mainModue->getFunction("puts")->replaceAllUsesWith( llvm::ConstantExpr::getPointerCast( llvm::ConstantInt::get(llvm::IntegerType::get(context, 64), 0xF), mainModue->getFunction("puts")->getType() ) ); This code will crash the application though. I know that 0xF is not a correct address and that there can be stuff optimized, but currently I just want to ‘see’ that “puts” was overloaded with 0xF. Kind greetings Björn Best, -- Mehdi 1. 2. What might happen if I have two modules and use “replaceAllUsesWith” on a function of Module A, passing a function of Module B. Thank you for the help in advance! Kind greetings Björn Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika _______________________________________________ LLVM Developers mailing list llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika _______________________________________________ LLVM Developers mailing list llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev -- https://weliveindetail.github.io/blog/ https://cryptup.org/pub/stefan.graenitz at gmail.com Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190115/4a410d9e/attachment.html>