<div dir="ltr">I figured it out. I was using this implementation of __chkstk from compiler-rt:<div><br></div><div><div>DEFINE_COMPILERRT_FUNCTION(___chkstk)</div><div> push %rcx</div><div> cmp $0x1000,%rax</div><div> lea 16(%rsp),%rcx // rsp before calling this routine -> rcx</div><div> jb 1f</div><div>2:</div><div> sub $0x1000,%rcx</div><div> test %rcx,(%rcx)</div><div> sub $0x1000,%rax</div><div> cmp $0x1000,%rax</div><div> ja 2b</div><div>1:</div><div> sub %rax,%rcx</div><div> test %rcx,(%rcx)</div><div><br></div><div> lea 8(%rsp),%rax // load pointer to the return address into rax</div><div> mov %rcx,%rsp // install the new top of stack pointer into rsp</div><div> mov -8(%rax),%rcx // restore rcx</div><div> push (%rax) // push return address onto the stack</div><div> sub %rsp,%rax // restore the original value in rax</div><div> ret</div><div>END_COMPILERRT_FUNCTION(___chkstk)</div><div><br></div><div>(source <a href="https://github.com/llvm-project/llvm-project-20170507/blob/release_50/compiler-rt/lib/builtins/x86_64/chkstk2.S">https://github.com/llvm-project/llvm-project-20170507/blob/release_50/compiler-rt/lib/builtins/x86_64/chkstk2.S</a>)</div><div><br></div><div>When I replaced it with a simple `ret`, everything worked.</div><div><br></div><div>The disassembled ntdll implementation is:</div><div><br></div><div><div>__chkstk:</div><div>1800a9f60: 48 83 ec 10 subq $16, %rsp</div><div>1800a9f64: 4c 89 14 24 movq %r10, (%rsp)</div><div>1800a9f68: 4c 89 5c 24 08 movq %r11, 8(%rsp)</div><div>1800a9f6d: 4d 33 db xorq %r11, %r11</div><div>1800a9f70: 4c 8d 54 24 18 leaq 24(%rsp), %r10</div><div>1800a9f75: 4c 2b d0 subq %rax, %r10</div><div>1800a9f78: 4d 0f 42 d3 cmovbq %r11, %r10</div><div>1800a9f7c: 65 4c 8b 1c 25 10 00 00 00 movq %gs:16, %r11</div><div>1800a9f85: 4d 3b d3 cmpq %r11, %r10</div><div>1800a9f88: 73 15 jae 21 <__chkstk+0x3F></div><div>1800a9f8a: 66 41 81 e2 00 f0 andw $61440, %r10w</div><div>1800a9f90: 4d 8d 9b 00 f0 ff ff leaq -4096(%r11), %r11</div><div>1800a9f97: 45 84 1b testb (%r11), %r11b</div><div>1800a9f9a: 4d 3b d3 cmpq %r11, %r10</div><div>1800a9f9d: 75 f1 jne -15 <__chkstk+0x30></div><div>1800a9f9f: 4c 8b 14 24 movq (%rsp), %r10</div><div>1800a9fa3: 4c 8b 5c 24 08 movq 8(%rsp), %r11</div><div>1800a9fa8: 48 83 c4 10 addq $16, %rsp</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 2, 2017 at 1:37 PM, Reid Kleckner <span dir="ltr"><<a href="mailto:rnk@google.com" target="_blank">rnk@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Can you post test.obj somewhere, and maybe the LLVM IR if you can get it? If it really was reading address <span style="font-size:12.8px">0xFFFFFFFFFFFFFFFF, then RBP must have been completely corrupted, probably by the prologue.</span></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="gmail-h5">On Sat, Sep 30, 2017 at 6:27 PM, Andrew Kelley via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="gmail-h5"><div dir="ltr">I suspect that there are 2 issues here:<div><br></div><div> * I have incorrect alignment somewhere</div><div> * MSVC / .pdb / CodeView debugging is not working correctly.</div><div><br></div><div>I think the latter would help solve the former.</div><div><br></div><div>I will send out a new email later talking about the issues I'm having debugging llvm-generated binaries with MSVC.</div><div><div class="gmail-m_227795374200833685h5"><div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 30, 2017 at 3:33 PM, Andrew Kelley <span dir="ltr"><<a href="mailto:superjoe30@gmail.com" target="_blank">superjoe30@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I have this code, which works fine on MacOS and Linux hosts:<br></div><div><br></div><div><div> const char *target_specific_cpu_args;</div><div> const char *target_specific_features;</div><div> if (g->is_native_target) {</div><div> target_specific_cpu_args = ZigLLVMGetHostCPUName();</div><div> target_specific_features = ZigLLVMGetNativeFeatures();</div><div> } else {</div><div> target_specific_cpu_args = "";</div><div> target_specific_features = "";</div><div> }</div></div><div><br></div><div><div> g->target_machine = LLVMCreateTargetMachine(target<wbr>_ref, buf_ptr(&g->triple_str),</div><div> target_specific_cpu_args, target_specific_features, opt_level, reloc_mode, LLVMCodeModelDefault);</div></div><div><br></div><div><br></div><div><br></div><div>char *ZigLLVMGetHostCPUName(void) {</div><div> std::string str = sys::getHostCPUName();</div><div> return strdup(str.c_str());</div><div>}</div><div><br></div><div>char *ZigLLVMGetNativeFeatures(void<wbr>) {</div><div> SubtargetFeatures features;</div><div><br></div><div> StringMap<bool> host_features;</div><div> if (sys::getHostCPUFeatures(host_<wbr>features)) {</div><div> for (auto &F : host_features)</div><div> features.AddFeature(F.first(), F.second);</div><div> }</div><div><br></div><div> return strdup(features.getString().c_<wbr>str());</div><div>}</div><div><br></div><div>On this windows laptop that I am testing on, I get these values:</div><div><br></div><div>target_specific_cpu_args: skylake<br></div><div><br></div><div>target_specific_features: +sse2,+cx16,-tbm,-avx512ifma,-<wbr>avx512dq,-fma4,+prfchw,+bmi2,+<wbr>xsavec,+fsgsbase,+popcnt,+aes,<wbr>+xsaves,-avx512er,-avx512vpopc<wbr>ntdq,-clwb,-avx512f,-clzero,-p<wbr>ku,+mmx,-lwp,-xop,+rdseed,-sse<wbr>4a,-avx512bw,+clflushopt,+xsav<wbr>e,-avx512vl,-avx512cd,+avx,-<wbr>rtm,+fma,+bmi,+rdrnd,-mwaitx,+<wbr>sse4.1,+sse4.2,+avx2,+sse,+<wbr>lzcnt,+pclmul,-prefetchwt1,+<wbr>f16c,+ssse3,+sgx,+cmov,-<wbr>avx512vbmi,+movbe,+xsaveopt,-<wbr>sha,+adx,-avx512pf,+sse3<br></div><div><br></div><div><br></div><div>It successfully creates a binary, but the binary when run crashes with:</div><div><br></div><div>Unhandled exception at 0x00007FF7C9913BA7 in test.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.</div><div><br></div><div>The disassembly of the crashed instruction is:</div><div><br></div><div><div>00007FF7C9913BA7 vmovdqa xmmword ptr [rbp-20h],xmm0 </div></div><div><br></div><div>There is no callstack or source in the MSVC debugger. The .pdb produced is 64KB exactly. The file was linked with:</div><div><br></div><div>lld -NOLOGO -DEBUG -MACHINE:X64 /SUBSYSTEM:console -OUT:.\test.exe -NODEFAULTLIB -ENTRY:_start ./zig-cache/test.obj ./zig-cache/builtin.obj ./zig-cache/compiler_rt.obj ./zig-cache/kernel32.lib<br></div><div><br></div><div><br></div><div>When I change the call to LLVMCreateTargetMachine so that both target_specific_cpu_args and target_specific_features are the empty string, the produced binary is valid and runs successfully.</div><div><br></div><div>Is this an LLVM bug? Am I using the API incorrectly? Is there more information I can provide to LLVM-dev mailing list that would make it easier to help me?</div></div>
</blockquote></div><br></div></div></div></div></div>
<br></div></div>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div></div></div>