<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"MS Gothic";
        panose-1:2 11 6 9 7 2 5 8 2 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@MS Gothic";
        panose-1:2 11 6 9 7 2 5 8 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:1836071891;
        mso-list-type:hybrid;
        mso-list-template-ids:330587148 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Two issues:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<ol style="margin-top:0in" start="1" type="1">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">You usually want to use Module:: getOrInsertFunction to construct a function declaration, so you get the existing declaration if it already exists. This should avoid the “.1” suffix.<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">If you’re using Intrinsic::getDeclaration to get an intrinsic declaration, you have to pass the overloaded type (an i8*) as the third argument to the function.<o:p></o:p></li></ol>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Eli<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org>
<b>On Behalf Of </b>y liu via llvm-dev<br>
<b>Sent:</b> Monday, April 6, 2020 12:08 AM<br>
<b>To:</b> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> [EXT] [llvm-dev] How to use llvm.addressofreturnaddress() function<span style="font-family:"MS Gothic"">?</span><o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<div>
<p class="MsoNormal" style="margin-left:.5in">Hi, I want to check the return address of function.<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">I use the following code to build pass. <o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">    FunctionType *func_type = FunctionType::get(ty_ptr, false);          
<br>
    Function *func = Function::Create(func_type, GlobalValue::ExternalLinkage, "<b>llvm.addressofreturnaddress</b>", F->getParent());
<br>
    IRBuilder<> Builder(inst);<br>
    Builder.CreateCall(func);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">When I compiled it, the following error message appeared:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   Intrinsic name not mangled correctly for type arguments! Should be: llvm.addressofreturnaddress.p0i8<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in">   i8* ()* @llvm.addressofreturnaddress<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">So I made the following modification:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">    FunctionType *func_type = FunctionType::get(ty_ptr, false);           <o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in">    Function *func = Function::Create(func_type, GlobalValue::ExternalLinkage, "l<b>lvm.addressofreturnaddress.p0i8</b>", F->getParent());
<br>
    IRBuilder<> Builder(inst);<br>
    Builder.CreateCall(func);<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Compile through, I get the compiled so file.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">However, the following error occurred when I used it to optimize my code:<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">    Intrinsic name not mangled correctly for type arguments! Should be: llvm.addressofreturnaddress.p0i8<br>
    i8* ()*<b> @llvm.addressofreturnaddress.p0i8.1</b> in function main<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:10.5pt;font-family:"Arial",sans-serif;color:#666666">I can't fix it. What should I do?</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Then, I tried another way.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"> CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID,<br>
751  ArrayRef<Type *> Types,<br>
752  ArrayRef<Value *> Args,<br>
753  Instruction *FMFSource,<br>
754  const Twine &Name) {<br>
755  Module *M = BB->getModule();<br>
756  Function *Fn = Intrinsic::getDeclaration(M, ID, Types);<br>
757  return createCallHelper(Fn, Args, this, Name, FMFSource);<br>
758 }<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-left:.5in">I use the following code.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">    Function *Fn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::addressofreturnaddress);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">But,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Stack dump:<br>
0. Program arguments: opt -load ../build/lib/LLVMMyPass.so -hello <br>
1. Running pass 'Function Pass Manager' on module '<stdin>'.<br>
2. Running pass 'Hello World Pass' on function '@_Z4vulnv'<br>
 #0 0x0000563d2d976bba llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/opt/llvm/bin/opt+0x2458bba)<br>
 #1 0x0000563d2d974954 llvm::sys::RunSignalHandlers() (/opt/llvm/bin/opt+0x2456954)<br>
 #2 0x0000563d2d974a92 SignalHandler(int) (/opt/llvm/bin/opt+0x2456a92)<br>
 #3 0x00007fce4a662890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)<br>
 #4 0x0000563d2d238bd4 DecodeFixedType(llvm::ArrayRef<llvm::Intrinsic::IITDescriptor>&, llvm::ArrayRef<llvm::Type*>, llvm::LLVMContext&) (/opt/llvm/bin/opt+0x1d1abd4)<br>
 #5 0x0000563d2d23b576 llvm::Intrinsic::getType(llvm::LLVMContext&, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Type*>) (/opt/llvm/bin/opt+0x1d1d576)<br>
 #6 0x0000563d2d240ab5 llvm::Intrinsic::getDeclaration(llvm::Module*, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Type*>) (/opt/llvm/bin/opt+0x1d22ab5)<br>
 #7 0x00007fce492f095a (anonymous namespace)::Hello::runOnFunction(llvm::Function&) (../build/lib/LLVMMyPass.so+0x195a)<br>
 #8 0x0000563d2d27db08 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/llvm/bin/opt+0x1d5fb08)<br>
 #9 0x0000563d2d27e1f1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/llvm/bin/opt+0x1d601f1)<br>
#10 0x0000563d2d27ce61 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/llvm/bin/opt+0x1d5ee61)<br>
#11 0x0000563d2bc5c193 main (/opt/llvm/bin/opt+0x73e193)<br>
#12 0x00007fce49513b97 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:344:0<br>
#13 0x0000563d2bce06ba _start (/opt/llvm/bin/opt+0x7c26ba)<br>
Segmentation fault (core dumped)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">"<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">I can't understand it.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">I tried to use "returnaddress" to replace "addressofreturnaddress". It's OK!<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">They are all Intrinsic function. Why?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Plese help me out. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Thanks.<o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>