<div dir="ltr">Thanks! <br></div><br><div class="gmail_quote"><div dir="ltr">Le mer. 3 oct. 2018 à 23:51, Kern Handa <<a href="mailto:kern.handa@gmail.com">kern.handa@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Christophe,</div><div><br></div><div>You've run into a problem I and Alastair Murray have run into (thread: <a href="https://lists.llvm.org/pipermail/llvm-dev/2018-October/126683.html" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2018-October/126683.html</a>). <br></div><div><br></div><div>In short, building your project with clang and linking against LLVM libraries built with GCC causes a SEGFAULT because of an ABI incompatibility.</div><div><br></div><div>HTH,</div><div>Kern<br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 3, 2018 at 8:49 AM Christophe Duvernois via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi,<div><br></div><div>I'm just trying to create a TargetMachine.  I'm using llvm 7.0</div><div>The following snippet works as expected with gcc on Ubuntu 16.04 and 18.04 but I have a crash with clang.</div><div>Am I doing something wrong here or is it a clang issue?</div><div><br></div><div><div style="color:rgb(0,0,0);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><memory></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/ADT/Triple.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/CodeGen/CommandFlags.inc></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/Support/CodeGen.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/InitializePasses.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/LinkAllIR.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/LinkAllPasses.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/MC/SubtargetFeature.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/Support/TargetRegistry.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/Support/TargetSelect.h></span></div><div><span style="color:rgb(175,0,219)">#include</span><span style="color:rgb(0,0,255)"> </span><span style="color:rgb(163,21,21)"><llvm/Target/TargetMachine.h></span></div><br><div><span style="color:rgb(175,0,219)">using</span> <span style="color:rgb(0,0,255)">namespace</span> <span style="color:rgb(38,127,153)">llvm</span>;</div><br><div><span style="color:rgb(0,128,0)">// Returns the TargetMachine instance or zero if no triple is provided.</span></div><div><span style="color:rgb(0,0,255)">static</span> TargetMachine* <span style="color:rgb(121,94,38)">GetTargetMachine</span>(Triple TheTriple, StringRef CPUStr,</div><div>                                       StringRef FeaturesStr,</div><div>                                       <span style="color:rgb(0,0,255)">const</span> TargetOptions &Options) {</div><div>  std::string Error;</div><div>  <span style="color:rgb(0,0,255)">const</span> Target *TheTarget = <span style="color:rgb(121,94,38)">TargetRegistry::lookupTarget</span>(MArch, TheTriple,</div><div>                                                         Error);</div><div>  <span style="color:rgb(0,128,0)">// Some modules don't specify a triple, and this is okay.</span></div><div>  <span style="color:rgb(175,0,219)">if</span> (!TheTarget) {</div><div>    <span style="color:rgb(175,0,219)">return</span> <span style="color:rgb(0,0,255)">nullptr</span>;</div><div>  }</div><br><div>  <span style="color:rgb(175,0,219)">return</span> TheTarget-><span style="color:rgb(121,94,38)">createTargetMachine</span>(TheTriple.<span style="color:rgb(121,94,38)">getTriple</span>(), CPUStr,</div><div>                                        FeaturesStr, Options, <span style="color:rgb(121,94,38)">getRelocModel</span>(),</div><div>                                        <span style="color:rgb(121,94,38)">getCodeModel</span>(), CodeGenOpt::Default);</div><div>}</div><br><div><span style="color:rgb(0,128,0)">//-----------------------------------------------------------------------------</span></div><div><span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(121,94,38)">main</span>(<span style="color:rgb(0,0,255)">int</span> argc, <span style="color:rgb(0,0,255)">const</span> <span style="color:rgb(0,0,255)">char</span>** argv) {</div><div>    <span style="color:rgb(121,94,38)">InitializeAllTargets</span>();</div><div>    <span style="color:rgb(121,94,38)">InitializeAllTargetMCs</span>();</div><div>    <span style="color:rgb(121,94,38)">InitializeAllAsmPrinters</span>();</div><div>    <span style="color:rgb(121,94,38)">InitializeAllAsmParsers</span>();</div><div>    </div><div>    Triple <span style="color:rgb(0,16,128)">ModuleTriple</span>(<span style="color:rgb(121,94,38)">sys::getProcessTriple</span>());</div><div>    std::string CPUStr, FeaturesStr;</div><div>    TargetMachine* Machine = <span style="color:rgb(0,0,255)">nullptr</span>;</div><div>    <span style="color:rgb(0,0,255)">const</span> TargetOptions Options = <span style="color:rgb(121,94,38)">InitTargetOptionsFromCodeGenFlags</span>();</div><div>    </div><div>    <span style="color:rgb(175,0,219)">if</span> (ModuleTriple.<span style="color:rgb(121,94,38)">getArch</span>()) {</div><div>        CPUStr = <span style="color:rgb(121,94,38)">getCPUStr</span>();</div><div>        FeaturesStr = <span style="color:rgb(121,94,38)">getFeaturesStr</span>();</div><div>        Machine = <span style="color:rgb(121,94,38)">GetTargetMachine</span>(ModuleTriple, CPUStr, FeaturesStr, Options);</div><div>    }</div><div>    std::unique_ptr<TargetMachine> <span style="color:rgb(121,94,38)">TM</span>(Machine);</div><div>    <span style="color:rgb(121,94,38)">fprintf</span>(stderr, <span style="color:rgb(163,21,21)">"%s:%d %p</span><span style="color:rgb(255,0,0)">\n</span><span style="color:rgb(163,21,21)">"</span>, __FUNCTION__, __LINE__, TM.<span style="color:rgb(121,94,38)">get</span>());</div><div>    </div><div>    <span style="color:rgb(175,0,219)">return</span> <span style="color:rgb(9,136,90)">0</span>;</div><div>}</div><br></div></div><div style="line-height:19px"><font color="#000000" face="Droid Sans Mono, monospace, monospace, Droid Sans Fallback"><span style="font-size:14px;white-space:pre-wrap">OK : g++ -std=c++11 crash.cpp -ocrash -I`llvm-config --includedir` -L`llvm-config --libdir` `llvm-config --libs` `llvm-config --system-libs`
CRASH : clang++ -std=c++11 crash.cpp -ocrash -I`llvm-config --includedir` -L`llvm-config --libdir` `llvm-config --libs` `llvm-config --system-libs`</span></font><br></div><div style="line-height:19px"><font color="#000000" face="Droid Sans Mono, monospace, monospace, Droid Sans Fallback"><span style="font-size:14px;white-space:pre-wrap"><br></span></font></div><div style="line-height:19px"><font color="#000000" face="Droid Sans Mono, monospace, monospace, Droid Sans Fallback"><span style="font-size:14px;white-space:pre-wrap">Here is the valgrind output in case of binary built with clang:</span></font></div><div style="line-height:19px"><span style="font-family:monospace"><span style="color:rgb(0,0,0)">==3931== Memcheck, a memory error detector
</span><br>==3931== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
<br>==3931== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
<br>==3931== Command: ./crash
<br>==3931==  <br>==3931== Use of uninitialised value of size 8
<br>==3931==    at 0xF04483: llvm::RegisterTargetMachine<llvm::X86TargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llv<br>m::CodeGenOpt::Level, bool) (in /home/christophe/crash)
<br>==3931==    by 0x4D52AF: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const (in /home/christophe<br>/crash)
<br>==3931==    by 0x4C027C: GetTargetMachine(llvm::Triple, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /home/christophe/crash)
<br>==3931==    by 0x4BF492: main (in /home/christophe/crash)
<br>==3931==  <br>==3931== Invalid read of size 1
<br>==3931==    at 0xF04483: llvm::RegisterTargetMachine<llvm::X86TargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llv<br>m::CodeGenOpt::Level, bool) (in /home/christophe/crash)
<br>==3931==    by 0x4D52AF: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const (in /home/christophe<br>/crash)
<br>==3931==    by 0x4C027C: GetTargetMachine(llvm::Triple, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /home/christophe/crash)
<br>==3931==    by 0x4BF492: main (in /home/christophe/crash)
<br>==3931==  Address 0xfefffd04 is not stack'd, malloc'd or (recently) free'd
<br>==3931==  <br>==3931==  <br>==3931== Process terminating with default action of signal 11 (SIGSEGV)
<br>==3931==  Access not within mapped region at address 0xFEFFFD04
<br>==3931==    at 0xF04483: llvm::RegisterTargetMachine<llvm::X86TargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llv<br>m::CodeGenOpt::Level, bool) (in /home/christophe/crash)
<br>==3931==    by 0x4D52AF: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const (in /home/christophe<br>/crash)
<br>==3931==    by 0x4C027C: GetTargetMachine(llvm::Triple, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /home/christophe/crash)
<br>==3931==    by 0x4BF492: main (in /home/christophe/crash)
<br>==3931==  If you believe this happened as a result of a stack
<br>==3931==  overflow in your program's main thread (unlikely but
<br>==3931==  possible), you can try to increase the size of the
<br>==3931==  main thread stack using the --main-stacksize= flag.
<br>==3931==  The main thread stack size used in this run was 8388608.
<br>==3931== <br>
<br></span></div><div style="line-height:19px"><font color="#000000" face="Droid Sans Mono, monospace, monospace, Droid Sans Fallback"><span style="font-size:14px;white-space:pre-wrap">Best regards,</span></font></div><div style="line-height:19px"><font color="#000000" face="Droid Sans Mono, monospace, monospace, Droid Sans Fallback"><span style="font-size:14px;white-space:pre-wrap">Christpohe</span></font></div><div style="color:rgb(0,0,0);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;line-height:19px;white-space:pre-wrap"><br></div></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div>