Skip to content

Commit fdea7da

Browse files
committed
runtime: use common library entry point on windows amd64/386
Windows can reuse the common library entry point instead of implementing a its own version. Note that windows/arm64 already uses the common one. Change-Id: I1a27bbec04bfd1d58a136638bafcdc0583bd106f Reviewed-on: https://go-review.googlesource.com/c/go/+/706235 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent e8a4f50 commit fdea7da

File tree

3 files changed

+23
-53
lines changed

3 files changed

+23
-53
lines changed

src/runtime/asm_amd64.s

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,36 @@ TEXT _rt0_amd64_lib(SB),NOSPLIT|NOFRAME,$0
3737
MOVQ SI, _rt0_amd64_lib_argv<>(SB)
3838

3939
// Synchronous initialization.
40+
#ifndef GOOS_windows
41+
// Avoid calling it on Windows because it is not used
42+
// and it would crash the application due to the autogenerated
43+
// ABI wrapper trying to access a non-existent TLS slot.
4044
CALL runtime·libpreinit(SB)
45+
#endif
4146

4247
// Create a new thread to finish Go runtime initialization.
4348
MOVQ _cgo_sys_thread_create(SB), AX
4449
TESTQ AX, AX
4550
JZ nocgo
4651

4752
// We're calling back to C.
48-
// Align stack per ELF ABI requirements.
53+
// Align stack per C ABI requirements.
4954
MOVQ SP, BX // Callee-save in C ABI
5055
ANDQ $~15, SP
5156
MOVQ $_rt0_amd64_lib_go(SB), DI
5257
MOVQ $0, SI
58+
#ifdef GOOS_windows
59+
// For Windows ABI
60+
MOVQ DI, CX
61+
MOVQ SI, DX
62+
// Leave space for four words on the stack as required
63+
// by the Windows amd64 calling convention.
64+
ADJSP $32
65+
#endif
5366
CALL AX
67+
#ifdef GOOS_windows
68+
ADJSP $-32 // just to make the assembler not complain about unbalanced stack
69+
#endif
5470
MOVQ BX, SP
5571
JMP restore
5672

src/runtime/rt0_windows_386.s

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,8 @@ TEXT _rt0_386_windows(SB),NOSPLIT,$0
1212
// library is loaded. For static libraries it is called when the
1313
// final executable starts, during the C runtime initialization
1414
// phase.
15-
TEXT _rt0_386_windows_lib(SB),NOSPLIT,$0x1C
16-
MOVL BP, 0x08(SP)
17-
MOVL BX, 0x0C(SP)
18-
MOVL AX, 0x10(SP)
19-
MOVL CX, 0x14(SP)
20-
MOVL DX, 0x18(SP)
21-
22-
// Create a new thread to do the runtime initialization and return.
23-
MOVL _cgo_sys_thread_create(SB), AX
24-
MOVL $_rt0_386_windows_lib_go(SB), 0x00(SP)
25-
MOVL $0, 0x04(SP)
26-
27-
// Top two items on the stack are passed to _cgo_sys_thread_create
28-
// as parameters. This is the calling convention on 32-bit Windows.
29-
CALL AX
30-
31-
MOVL 0x08(SP), BP
32-
MOVL 0x0C(SP), BX
33-
MOVL 0x10(SP), AX
34-
MOVL 0x14(SP), CX
35-
MOVL 0x18(SP), DX
36-
RET
37-
38-
TEXT _rt0_386_windows_lib_go(SB),NOSPLIT,$0
39-
PUSHL $0
40-
PUSHL $0
41-
JMP runtime·rt0_go(SB)
15+
TEXT _rt0_386_windows_lib(SB),NOSPLIT,$0
16+
JMP _rt0_386_lib(SB)
4217

4318
TEXT _main(SB),NOSPLIT,$0
4419
// Remove the return address from the stack.

src/runtime/rt0_windows_amd64.s

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,10 @@
66
#include "go_tls.h"
77
#include "textflag.h"
88

9-
TEXT _rt0_amd64_windows(SB),NOSPLIT|NOFRAME,$-8
9+
TEXT _rt0_amd64_windows(SB),NOSPLIT,$0
1010
JMP _rt0_amd64(SB)
1111

1212
// When building with -buildmode=(c-shared or c-archive), this
13-
// symbol is called. For dynamic libraries it is called when the
14-
// library is loaded. For static libraries it is called when the
15-
// final executable starts, during the C runtime initialization
16-
// phase.
17-
// Leave space for four pointers on the stack as required
18-
// by the Windows amd64 calling convention.
19-
TEXT _rt0_amd64_windows_lib(SB),NOSPLIT|NOFRAME,$40
20-
// Create a new thread to do the runtime initialization and return.
21-
MOVQ BX, 32(SP) // callee-saved, preserved across the CALL
22-
MOVQ SP, BX
23-
ANDQ $~15, SP // alignment as per Windows requirement
24-
MOVQ _cgo_sys_thread_create(SB), AX
25-
MOVQ $_rt0_amd64_windows_lib_go(SB), CX
26-
MOVQ $0, DX
27-
CALL AX
28-
MOVQ BX, SP
29-
MOVQ 32(SP), BX
30-
RET
31-
32-
TEXT _rt0_amd64_windows_lib_go(SB),NOSPLIT|NOFRAME,$0
33-
MOVQ $0, DI
34-
MOVQ $0, SI
35-
MOVQ $runtime·rt0_go(SB), AX
36-
JMP AX
13+
// symbol is called.
14+
TEXT _rt0_amd64_windows_lib(SB),NOSPLIT,$0
15+
JMP _rt0_amd64_lib(SB)

0 commit comments

Comments
 (0)