<div dir="ltr">Update: smeenai from #llvm has been helping me with this, and we narrowed it down to kernel32.lib being no good. kernel32.lib from the 14393 SDK worked fine.<div><br></div><div>My goal is to not depend on .lib files from the SDK, so there's still a problem to solve here.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 4, 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Here's some C code:<div><br></div><div><div>extern void *GetStdHandle(unsigned int nStdHandle);</div><div>extern void ExitProcess(unsigned int exit_code);</div><div>extern char WriteFile(void *HANDLE, const void * lpBuffer, unsigned int nNumberOfBytesToWrite,</div><div>    unsigned int *lpNumberOfBytesWritten, void *lpOverlapped);</div><div><br></div><div>static const char *message_ptr = "hello\n";</div><div>static const unsigned int message_len = 6;</div><div><br></div><div>__attribute__((stdcall)) int _start(void) {</div><div>    void *hStdOut = GetStdHandle(-11);</div><div>    WriteFile(hStdOut, message_ptr, message_len, 0, 0);</div><div>    ExitProcess(0);</div><div>}</div></div><div><br></div><div><br></div><div>I use mingw-w64 like this:</div><div><br></div><div>gcc -c test.c</div><div><br></div><div><br></div><div>Create kernel32.def:</div><div><br></div><div><div>EXPORTS</div><div>ExitProcess</div><div>GetStdHandle<br></div><div>WriteFile<br></div></div><div><br></div><div><br></div><div>Use dlltool to create kernel32.lib:</div><div><br></div><div>dlltool -d kernel32.def -l kernel32.lib<br></div><div><br></div><div><br></div><div>Then link with lld-link:</div><div><br></div><div><div>lld-link -NOLOGO -SUBSYSTEM:console test.o kernel32.lib  -OUT:test.exe -NODEFAULTLIB -ENTRY:_start</div></div><div><br></div><div>This succeeds, but then I get a segfault when running test.exe. When I step through the code, what's happening is it's getting to</div><div>    140005068:   ff 25 ce cf ff ff       jmpq   *-0x3032(%rip)        # 0x14000203c</div><div>which is trying to jump into .idata section where presumably the stub for GetStdHandle lives. However after this jump, $rip ends up getting assigned a bogus address and then the segfault happens.</div><div><br></div><div>Any ideas how to make these library calls hook up correctly?</div></div>
</blockquote></div><br></div>