<div dir="ltr">Hi,<div><br></div><div>I created a global char array, char buffer[1024]. I want to call a function to append the string information to the buffer repeatedly. For example, I need to implement the following code, where length, a, b, c, are global variables.</div><div><br></div><pre class="" style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;word-wrap:normal;background-color:rgb(238,238,238)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;white-space:inherit"><span class="" style="color:rgb(43,145,175);margin:0px;padding:0px;border:0px">int</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> length </span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">=</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> </span><span class="" style="color:rgb(128,0,0);margin:0px;padding:0px;border:0px">0</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">;</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">
length </span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">+=</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> sprintf</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">(</span><span class="" style="color:rgb(43,145,175);margin:0px;padding:0px;border:0px">buffer</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">+</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">length</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">,</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> "str%d</span><span class="" style="margin:0px;padding:0px;border:0px"><font color="#800000">", a</font></span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">);</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">
length </span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">+=</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> sprintf</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">(</span><span class="" style="color:rgb(43,145,175);margin:0px;padding:0px;border:0px">buffer</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">+</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">length</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">,</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> </span><span class="" style="color:rgb(128,0,0);margin:0px;padding:0px;border:0px">"str%c", b</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">);</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">
length </span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">+=</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> sprintf</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">(</span><span class="" style="color:rgb(43,145,175);margin:0px;padding:0px;border:0px">buffer</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">+</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">length</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">,</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px"> </span><span class="" style="color:rgb(128,0,0);margin:0px;padding:0px;border:0px">"str%d", c</span><span class="" style="color:rgb(0,0,0);margin:0px;padding:0px;border:0px">);</span></code></pre><div>I did it in the following way.</div><div>1. Create the global array:</div><div> IntegerType CHARTYPE = IntegerType::get(llvm::getGlobalContext(), 8);</div><div> ArrayType* ty = ArrayType::get(CHARTYPE, 1024);</div><div> </div><div> GlobalVariable* buffer = new GlobalVariable(mod, ty, false, GlobalValue::ExternalLinkage, 0, "buffer");</div><div><br></div><div>2. Create sprintf</div><div> std::vector<Type*> agrs(2, Type::getInt8PtrTy(ctxt));</div><div> FunctionType* sprintfFT = FunctionType::get(Type::getInt32Ty(ctxt), args, true);</div><div> Constant* sprintf = mod->getOrInsertFunction("sprintf", sprintfFT);</div><div><br></div><div> Now, I got stuck to create teh function call for sprintf.</div><div><br></div><div> I didn't it like the following:</div><div> Value* str = builder.CreateGlobalStringPtr("str", "");</div><div> Value* data = builder.CreateLoad(a); //load a, b, or c</div><div> Value* buf = builder.CreateLoad(buffer);</div><div> builder.CreateCall3((value*)sprintf, buf, str, data);</div><div><br></div><div>I want to test the sprintf function before adding the offset, but it didn't work. The error information is "llvm.sys:PrintStackTrace(_IO_FILE)". Any help will be appreciated. </div><div><br></div><div>Best,</div><div>Zhi</div></div>