<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>