Skip to content

Commit 378de1a

Browse files
runtime: unify 386 entry point code
Unify the 386 entry point code as much as possible. The main function could not be unified because on Windows 386 it is called _main. Putting main in asm_386.s caused multiple definition errors when using the external linker. Add the _lib entry point to various operating systems. A future CL will enable c-archive/c-shared mode for those targets. Fix _rt0_386_windows_lib_go--it was passing arguments as though it were amd64. Change-Id: Ic73f1c95cdbcbea87f633f4a29bbc218a5db4f58 Reviewed-on: https://go-review.googlesource.com/70530 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 151c66b commit 378de1a

11 files changed

+136
-181
lines changed

src/cmd/vet/all/whitelist/386.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
runtime/asm_ARCHSUFF.s: [GOARCH] cannot check cross-package assembly function: Compare is in package bytes
44

5+
// startup code uses non-standard calling convention and intentionally
6+
// omits args.
7+
runtime/asm_386.s: [386] rt0_go: use of 4(SP) points beyond argument frame
8+
59
// reflect trampolines intentionally omit arg size. Same for morestack.
610
runtime/asm_386.s: [386] morestack: use of 4(SP) points beyond argument frame
711
runtime/asm_386.s: [386] morestack: use of 8(SP) points beyond argument frame
@@ -21,7 +25,3 @@ runtime/asm_386.s: [386] uint32tofloat64: function uint32tofloat64 missing Go de
2125
runtime/asm_386.s: [386] float64touint32: function float64touint32 missing Go declaration
2226

2327
runtime/asm_386.s: [386] stackcheck: function stackcheck missing Go declaration
24-
25-
// Clearer using FP than SP, but that requires named offsets.
26-
runtime/asm_386.s: [386] rt0_go: unknown variable argc
27-
runtime/asm_386.s: [386] rt0_go: unknown variable argv

src/runtime/asm_386.s

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,87 @@
77
#include "funcdata.h"
88
#include "textflag.h"
99

10+
// _rt0_386 is common startup code for most 386 systems when using
11+
// internal linking. This is the entry point for the program from the
12+
// kernel for an ordinary -buildmode=exe program. The stack holds the
13+
// number of arguments and the C-style argv.
14+
TEXT _rt0_386(SB),NOSPLIT,$8
15+
MOVL 8(SP), AX // argc
16+
LEAL 12(SP), BX // argv
17+
MOVL AX, 0(SP)
18+
MOVL BX, 4(SP)
19+
JMP runtime·rt0_go(SB)
20+
21+
// _rt0_386_lib is common startup code for most 386 systems when
22+
// using -buildmode=c-archive or -buildmode=c-shared. The linker will
23+
// arrange to invoke this function as a global constructor (for
24+
// c-archive) or when the shared library is loaded (for c-shared).
25+
// We expect argc and argv to be passed on the stack following the
26+
// usual C ABI.
27+
TEXT _rt0_386_lib(SB),NOSPLIT,$0
28+
PUSHL BP
29+
MOVL SP, BP
30+
PUSHL BX
31+
PUSHL SI
32+
PUSHL DI
33+
34+
MOVL 8(BP), AX
35+
MOVL AX, _rt0_386_lib_argc<>(SB)
36+
MOVL 12(BP), AX
37+
MOVL AX, _rt0_386_lib_argv<>(SB)
38+
39+
// Synchronous initialization.
40+
CALL runtime·libpreinit(SB)
41+
42+
SUBL $8, SP
43+
44+
// Create a new thread to do the runtime initialization.
45+
MOVL _cgo_sys_thread_create(SB), AX
46+
TESTL AX, AX
47+
JZ nocgo
48+
MOVL $_rt0_386_lib_go(SB), BX
49+
MOVL BX, 0(SP)
50+
MOVL $0, 4(SP)
51+
52+
// TODO: We are calling a C function here so we should be
53+
// aligning the stack.
54+
55+
CALL AX
56+
JMP restore
57+
58+
nocgo:
59+
MOVL $0x800000, 0(SP) // stacksize = 8192KB
60+
MOVL $_rt0_386_lib_go(SB), AX
61+
MOVL AX, 4(SP) // fn
62+
CALL runtime·newosproc0(SB)
63+
64+
restore:
65+
ADDL $8, SP
66+
POPL DI
67+
POPL SI
68+
POPL BX
69+
POPL BP
70+
RET
71+
72+
// _rt0_386_lib_go initializes the Go runtime.
73+
// This is started in a separate thread by _rt0_386_lib.
74+
TEXT _rt0_386_lib_go(SB),NOSPLIT,$8
75+
MOVL _rt0_386_lib_argc<>(SB), AX
76+
MOVL AX, 0(SP)
77+
MOVL _rt0_386_lib_argv<>(SB), AX
78+
MOVL AX, 4(SP)
79+
JMP runtime·rt0_go(SB)
80+
81+
DATA _rt0_386_lib_argc<>(SB)/4, $0
82+
GLOBL _rt0_386_lib_argc<>(SB),NOPTR, $4
83+
DATA _rt0_386_lib_argv<>(SB)/4, $0
84+
GLOBL _rt0_386_lib_argv<>(SB),NOPTR, $4
85+
1086
TEXT runtime·rt0_go(SB),NOSPLIT,$0
11-
// copy arguments forward on an even stack
12-
MOVL argc+0(FP), AX
13-
MOVL argv+4(FP), BX
87+
// Copy arguments forward on an even stack.
88+
// Users of this function jump to it, they don't call it.
89+
MOVL 0(SP), AX
90+
MOVL 4(SP), BX
1491
SUBL $128, SP // plenty of scratch
1592
ANDL $~15, SP
1693
MOVL AX, 120(SP) // save argc, argv away

src/runtime/rt0_android_386.s

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_386_android(SB),NOSPLIT,$8
8-
MOVL 8(SP), AX // argc
9-
LEAL 12(SP), BX // argv
10-
MOVL AX, 0(SP)
11-
MOVL BX, 4(SP)
12-
CALL main(SB)
13-
INT $3
7+
TEXT _rt0_386_android(SB),NOSPLIT,$0
8+
JMP _rt0_386(SB)
149

1510
TEXT _rt0_386_android_lib(SB),NOSPLIT,$0
1611
PUSHL $_rt0_386_android_argv(SB) // argv
1712
PUSHL $1 // argc
18-
CALL _rt0_386_linux_lib(SB)
19-
POPL AX
20-
POPL AX
21-
RET
13+
JMP _rt0_386_lib(SB)
2214

2315
DATA _rt0_386_android_argv+0x00(SB)/4,$_rt0_386_android_argv0(SB)
2416
DATA _rt0_386_android_argv+0x04(SB)/4,$0 // argv terminate

src/runtime/rt0_darwin_386.s

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,14 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_386_darwin(SB),NOSPLIT,$8
8-
MOVL 8(SP), AX
9-
LEAL 12(SP), BX
10-
MOVL AX, 0(SP)
11-
MOVL BX, 4(SP)
12-
CALL main(SB)
13-
INT $3
7+
TEXT _rt0_386_darwin(SB),NOSPLIT,$0
8+
JMP _rt0_386(SB)
149

15-
// With -buildmode=c-archive, this symbol is called from a global constructor.
1610
TEXT _rt0_386_darwin_lib(SB),NOSPLIT,$0
17-
PUSHL BP
18-
MOVL SP, BP
19-
PUSHL BX
20-
PUSHL SI
21-
PUSHL DI
22-
23-
MOVL 8(BP), AX
24-
MOVL AX, _rt0_386_darwin_lib_argc<>(SB)
25-
MOVL 12(BP), AX
26-
MOVL AX, _rt0_386_darwin_lib_argv<>(SB)
27-
28-
// Synchronous initialization.
29-
MOVL $runtime·libpreinit(SB), AX
30-
CALL AX
31-
32-
SUBL $12, SP
33-
34-
// Create a new thread to do the runtime initialization and return.
35-
MOVL _cgo_sys_thread_create(SB), AX
36-
TESTL AX, AX
37-
JZ nocgo
38-
MOVL $_rt0_386_darwin_lib_go(SB), BX
39-
MOVL BX, 0(SP)
40-
MOVL $0, 4(SP)
41-
CALL AX
42-
JMP restore
43-
44-
nocgo:
45-
MOVL $0x800000, 0(SP) // stacksize = 8192KB
46-
MOVL $_rt0_386_darwin_lib_go(SB), AX
47-
MOVL AX, 4(SP) // fn
48-
MOVL $0, 8(SP) // fnarg
49-
MOVL $runtime·newosproc0(SB), AX
50-
CALL AX
51-
52-
restore:
53-
ADDL $12, SP
54-
POPL DI
55-
POPL SI
56-
POPL BX
57-
POPL BP
58-
RET
59-
60-
TEXT _rt0_386_darwin_lib_go(SB),NOSPLIT,$12
61-
MOVL _rt0_386_darwin_lib_argc<>(SB), AX
62-
MOVL AX, 0(SP)
63-
MOVL _rt0_386_darwin_lib_argv<>(SB), AX
64-
MOVL AX, 4(SP)
65-
MOVL $runtime·rt0_go(SB), AX
66-
CALL AX
67-
RET
68-
69-
DATA _rt0_386_darwin_lib_argc<>(SB)/4, $0
70-
GLOBL _rt0_386_darwin_lib_argc<>(SB),NOPTR, $4
71-
DATA _rt0_386_darwin_lib_argv<>(SB)/4, $0
72-
GLOBL _rt0_386_darwin_lib_argv<>(SB),NOPTR, $4
11+
JMP _rt0_386_lib(SB)
7312

7413
TEXT main(SB),NOSPLIT,$0
14+
// Remove the return address from the stack.
15+
// rt0_go doesn't expect it to be there.
16+
ADDL $4, SP
7517
JMP runtime·rt0_go(SB)

src/runtime/rt0_freebsd_386.s

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_386_freebsd(SB),NOSPLIT,$8
8-
MOVL 8(SP), AX
9-
LEAL 12(SP), BX
10-
MOVL AX, 0(SP)
11-
MOVL BX, 4(SP)
12-
CALL main(SB)
13-
INT $3
7+
TEXT _rt0_386_freebsd(SB),NOSPLIT,$0
8+
JMP _rt0_386(SB)
9+
10+
TEXT _rt0_386_freebsd_lib(SB),NOSPLIT,$0
11+
JMP _rt0_386_lib(SB)
1412

1513
TEXT main(SB),NOSPLIT,$0
14+
// Remove the return address from the stack.
15+
// rt0_go doesn't expect it to be there.
16+
ADDL $4, SP
1617
JMP runtime·rt0_go(SB)

src/runtime/rt0_linux_386.s

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,14 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_386_linux(SB),NOSPLIT,$8
8-
MOVL 8(SP), AX
9-
LEAL 12(SP), BX
10-
MOVL AX, 0(SP)
11-
MOVL BX, 4(SP)
12-
CALL main(SB)
13-
INT $3
7+
TEXT _rt0_386_linux(SB),NOSPLIT,$0
8+
JMP _rt0_386(SB)
149

15-
// When building with -buildmode=c-shared, this symbol is called when the shared
16-
// library is loaded.
1710
TEXT _rt0_386_linux_lib(SB),NOSPLIT,$0
18-
PUSHL BP
19-
MOVL SP, BP
20-
PUSHL BX
21-
PUSHL SI
22-
PUSHL DI
23-
24-
MOVL 8(BP), AX
25-
MOVL AX, _rt0_386_linux_lib_argc<>(SB)
26-
MOVL 12(BP), AX
27-
MOVL AX, _rt0_386_linux_lib_argv<>(SB)
28-
29-
// Synchronous initialization.
30-
MOVL $runtime·libpreinit(SB), AX
31-
CALL AX
32-
33-
SUBL $8, SP
34-
35-
// Create a new thread to do the runtime initialization.
36-
MOVL _cgo_sys_thread_create(SB), AX
37-
TESTL AX, AX
38-
JZ nocgo
39-
MOVL $_rt0_386_linux_lib_go(SB), BX
40-
MOVL BX, 0(SP)
41-
MOVL $0, 4(SP)
42-
CALL AX
43-
JMP restore
44-
45-
nocgo:
46-
MOVL $0x800000, 0(SP) // stacksize = 8192KB
47-
MOVL $_rt0_386_linux_lib_go(SB), AX
48-
MOVL AX, 4(SP) // fn
49-
MOVL $runtime·newosproc0(SB), AX
50-
CALL AX
51-
52-
restore:
53-
ADDL $8, SP
54-
POPL DI
55-
POPL SI
56-
POPL BX
57-
POPL BP
58-
RET
59-
60-
TEXT _rt0_386_linux_lib_go(SB),NOSPLIT,$12
61-
MOVL _rt0_386_linux_lib_argc<>(SB), AX
62-
MOVL AX, 0(SP)
63-
MOVL _rt0_386_linux_lib_argv<>(SB), AX
64-
MOVL AX, 4(SP)
65-
MOVL $runtime·rt0_go(SB), AX
66-
CALL AX
67-
RET
68-
69-
DATA _rt0_386_linux_lib_argc<>(SB)/4, $0
70-
GLOBL _rt0_386_linux_lib_argc<>(SB),NOPTR, $4
71-
DATA _rt0_386_linux_lib_argv<>(SB)/4, $0
72-
GLOBL _rt0_386_linux_lib_argv<>(SB),NOPTR, $4
11+
JMP _rt0_386_lib(SB)
7312

7413
TEXT main(SB),NOSPLIT,$0
14+
// Remove the return address from the stack.
15+
// rt0_go doesn't expect it to be there.
16+
ADDL $4, SP
7517
JMP runtime·rt0_go(SB)

src/runtime/rt0_nacl_386.s

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ TEXT _rt0_386_nacl(SB),NOSPLIT,$8
1515
LEAL argv+16(FP), BX
1616
MOVL AX, 0(SP)
1717
MOVL BX, 4(SP)
18-
CALL main(SB)
19-
INT $3
18+
JMP runtime·rt0_go(SB)
2019

2120
TEXT main(SB),NOSPLIT,$0
21+
// Remove the return address from the stack.
22+
// rt0_go doesn't expect it to be there.
23+
ADDL $4, SP
2224
JMP runtime·rt0_go(SB)

src/runtime/rt0_netbsd_386.s

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_386_netbsd(SB),NOSPLIT,$8
8-
MOVL 8(SP), AX
9-
LEAL 12(SP), BX
10-
MOVL AX, 0(SP)
11-
MOVL BX, 4(SP)
12-
CALL main(SB)
13-
INT $3
7+
TEXT _rt0_386_netbsd(SB),NOSPLIT,$0
8+
JMP _rt0_386(SB)
9+
10+
TEXT _rt0_386_netbsd_lib(SB),NOSPLIT,$0
11+
JMP _rt0_386_lib(SB)
1412

1513
TEXT main(SB),NOSPLIT,$0
14+
// Remove the return address from the stack.
15+
// rt0_go doesn't expect it to be there.
16+
ADDL $4, SP
1617
JMP runtime·rt0_go(SB)

src/runtime/rt0_openbsd_386.s

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_386_openbsd(SB),NOSPLIT,$8
8-
MOVL 8(SP), AX
9-
LEAL 12(SP), BX
10-
MOVL AX, 0(SP)
11-
MOVL BX, 4(SP)
12-
CALL main(SB)
13-
INT $3
7+
TEXT _rt0_386_openbsd(SB),NOSPLIT,$0
8+
JMP _rt0_386(SB)
9+
10+
TEXT _rt0_386_openbsd_lib(SB),NOSPLIT,$0
11+
JMP _rt0_386_lib(SB)
1412

1513
TEXT main(SB),NOSPLIT,$0
14+
// Remove the return address from the stack.
15+
// rt0_go doesn't expect it to be there.
16+
ADDL $4, SP
1617
JMP runtime·rt0_go(SB)

src/runtime/rt0_plan9_386.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ TEXT _rt0_386_plan9(SB),NOSPLIT,$12
1414
MOVL AX, 0(SP)
1515
LEAL inargv+0(FP), AX
1616
MOVL AX, 4(SP)
17-
CALL runtime·rt0_go(SB)
17+
JMP runtime·rt0_go(SB)
1818

1919
GLOBL _tos(SB), NOPTR, $4
2020
GLOBL _privates(SB), NOPTR, $4

0 commit comments

Comments
 (0)