detached 4f3cc27

internal/fakecgo: use C ABI in amd64 trampolines (#394)

Go might expect (in the future) that x_cgo_init_trampoline trampolines and
friends honor the C calling convention, i.e. callee-saved (aka non-volatile)
registers shouldn't be clobbered by the called function.

It is more future proof, safer, and more standard, to use R11 as a temporary
register.
commit
4f3cc2789f2e2d70a6fe3cda1d183244c90804bd
author
Quim Muntal · · 2026-01-23 04:02 UTC
committer
GitHub · · 2026-01-23 04:02 UTC
parent
a280c0d
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
1 file · +22 -29
M internal/fakecgo/trampolines_amd64.s +22 -29
@@ -7,12 +7,6 @@
77 trampoline for emulating required C functions for cgo in go (see cgo.go)
88 (we convert cdecl calling convention to go and vice-versa)
99
10-Since we're called from go and call into C we can cheat a bit with the calling conventions:
11- - in go all the registers are caller saved
12- - in C we have a couple of callee saved registers
13-
14-=> we can use BX, R12, R13, R14, R15 instead of the stack
15-
1610 C Calling convention cdecl used here (we only need integer args):
1711 1. arg: DI
1812 2. arg: SI
@@ -22,6 +16,7 @@ C Calling convention cdecl used here (we only need integer args):
2216 6. arg: R9
2317 We don't need floats with these functions -> AX=0
2418 return value will be in AX
19+temporary register is R11
2520 */
2621 #include "textflag.h"
2722 #include "go_asm.h"
@@ -32,46 +27,44 @@ return value will be in AX
3227 TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16
3328 MOVQ DI, AX
3429 MOVQ SI, BX
35- MOVQ ·x_cgo_init_call(SB), DX
36- MOVQ (DX), CX
37- CALL CX
30+ MOVQ ·x_cgo_init_call(SB), R11
31+ MOVQ (R11), R11
32+ CALL R11
3833 RET
3934
4035 TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $8
4136 MOVQ DI, AX
42- MOVQ ·x_cgo_thread_start_call(SB), DX
43- MOVQ (DX), CX
44- CALL CX
37+ MOVQ ·x_cgo_thread_start_call(SB), R11
38+ MOVQ (R11), R11
39+ CALL R11
4540 RET
4641
4742 TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $8
4843 MOVQ DI, AX
49- MOVQ ·x_cgo_setenv_call(SB), DX
50- MOVQ (DX), CX
51- CALL CX
44+ MOVQ ·x_cgo_setenv_call(SB), R11
45+ MOVQ (R11), R11
46+ CALL R11
5247 RET
5348
5449 TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $8
5550 MOVQ DI, AX
56- MOVQ ·x_cgo_unsetenv_call(SB), DX
57- MOVQ (DX), CX
58- CALL CX
51+ MOVQ ·x_cgo_unsetenv_call(SB), R11
52+ MOVQ (R11), R11
53+ CALL R11
5954 RET
6055
6156 TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $0
62- CALL ·x_cgo_notify_runtime_init_done(SB)
63- RET
57+ JMP ·x_cgo_notify_runtime_init_done(SB)
6458
6559 TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $0
66- CALL ·x_cgo_bindm(SB)
67- RET
60+ JMP ·x_cgo_bindm(SB)
6861
6962 // func setg_trampoline(setg uintptr, g uintptr)
7063 TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
7164 MOVQ G+8(FP), DI
72- MOVQ setg+0(FP), BX
65+ MOVQ setg+0(FP), R11
7366 XORL AX, AX
74- CALL BX
67+ CALL R11
7568 RET
7669
7770 TEXT threadentry_trampoline(SB), NOSPLIT, $0
@@ -83,15 +76,15 @@ TEXT threadentry_trampoline(SB), NOSPLIT, $0
8376 PXOR X15, X15
8477
8578 MOVQ DI, AX
86- MOVQ ·threadentry_call(SB), DX
87- MOVQ (DX), CX
88- CALL CX
79+ MOVQ ·threadentry_call(SB), R11
80+ MOVQ (R11), R11
81+ CALL R11
8982
9083 POP_REGS_HOST_TO_ABI0()
9184 RET
9285
9386 TEXT ·call5(SB), NOSPLIT, $0-56
94- MOVQ fn+0(FP), BX
87+ MOVQ fn+0(FP), R11
9588 MOVQ a1+8(FP), DI
9689 MOVQ a2+16(FP), SI
9790 MOVQ a3+24(FP), DX
@@ -105,7 +98,7 @@ TEXT ·call5(SB), NOSPLIT, $0-56
10598 SUBQ $16, SP // allocate space for alignment
10699 ANDQ $-16, SP // align on 16 bytes for SSE
107100
108- CALL BX
101+ CALL R11
109102
110103 MOVQ BP, SP // get SP back
111104 POPQ BP // restore BP
@@ -7,12 +7,6 @@
7 trampoline for emulating required C functions for cgo in go (see cgo.go)7 trampoline for emulating required C functions for cgo in go (see cgo.go)
8 (we convert cdecl calling convention to go and vice-versa)8 (we convert cdecl calling convention to go and vice-versa)
9 9
10-Since we're called from go and call into C we can cheat a bit with the calling conventions:
11- - in go all the registers are caller saved
12- - in C we have a couple of callee saved registers
13-
14-=> we can use BX, R12, R13, R14, R15 instead of the stack
15-
16 C Calling convention cdecl used here (we only need integer args):10 C Calling convention cdecl used here (we only need integer args):
17 1. arg: DI11 1. arg: DI
18 2. arg: SI12 2. arg: SI
@@ -22,6 +16,7 @@ C Calling convention cdecl used here (we only need integer args):
22 6. arg: R916 6. arg: R9
23 We don't need floats with these functions -> AX=017 We don't need floats with these functions -> AX=0
24 return value will be in AX18 return value will be in AX
19+temporary register is R11
25 */20 */
26 #include "textflag.h"21 #include "textflag.h"
27 #include "go_asm.h"22 #include "go_asm.h"
@@ -32,46 +27,44 @@ return value will be in AX
32 TEXT x_cgo_init_trampoline(SB), NOSPLIT, $1627 TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16
33 MOVQ DI, AX28 MOVQ DI, AX
34 MOVQ SI, BX29 MOVQ SI, BX
35- MOVQ ·x_cgo_init_call(SB), DX30+ MOVQ ·x_cgo_init_call(SB), R11
36- MOVQ (DX), CX31+ MOVQ (R11), R11
37- CALL CX32+ CALL R11
38 RET33 RET
39 34
40 TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $835 TEXT x_cgo_thread_start_trampoline(SB), NOSPLIT, $8
41 MOVQ DI, AX36 MOVQ DI, AX
42- MOVQ ·x_cgo_thread_start_call(SB), DX37+ MOVQ ·x_cgo_thread_start_call(SB), R11
43- MOVQ (DX), CX38+ MOVQ (R11), R11
44- CALL CX39+ CALL R11
45 RET40 RET
46 41
47 TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $842 TEXT x_cgo_setenv_trampoline(SB), NOSPLIT, $8
48 MOVQ DI, AX43 MOVQ DI, AX
49- MOVQ ·x_cgo_setenv_call(SB), DX44+ MOVQ ·x_cgo_setenv_call(SB), R11
50- MOVQ (DX), CX45+ MOVQ (R11), R11
51- CALL CX46+ CALL R11
52 RET47 RET
53 48
54 TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $849 TEXT x_cgo_unsetenv_trampoline(SB), NOSPLIT, $8
55 MOVQ DI, AX50 MOVQ DI, AX
56- MOVQ ·x_cgo_unsetenv_call(SB), DX51+ MOVQ ·x_cgo_unsetenv_call(SB), R11
57- MOVQ (DX), CX52+ MOVQ (R11), R11
58- CALL CX53+ CALL R11
59 RET54 RET
60 55
61 TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $056 TEXT x_cgo_notify_runtime_init_done_trampoline(SB), NOSPLIT, $0
62- CALL ·x_cgo_notify_runtime_init_done(SB)57+ JMP ·x_cgo_notify_runtime_init_done(SB)
63- RET
64 58
65 TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $059 TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $0
66- CALL ·x_cgo_bindm(SB)60+ JMP ·x_cgo_bindm(SB)
67- RET
68 61
69 // func setg_trampoline(setg uintptr, g uintptr)62 // func setg_trampoline(setg uintptr, g uintptr)
70 TEXT ·setg_trampoline(SB), NOSPLIT, $0-1663 TEXT ·setg_trampoline(SB), NOSPLIT, $0-16
71 MOVQ G+8(FP), DI64 MOVQ G+8(FP), DI
72- MOVQ setg+0(FP), BX65+ MOVQ setg+0(FP), R11
73 XORL AX, AX66 XORL AX, AX
74- CALL BX67+ CALL R11
75 RET68 RET
76 69
77 TEXT threadentry_trampoline(SB), NOSPLIT, $070 TEXT threadentry_trampoline(SB), NOSPLIT, $0
@@ -83,15 +76,15 @@ TEXT threadentry_trampoline(SB), NOSPLIT, $0
83 PXOR X15, X1576 PXOR X15, X15
84 77
85 MOVQ DI, AX78 MOVQ DI, AX
86- MOVQ ·threadentry_call(SB), DX79+ MOVQ ·threadentry_call(SB), R11
87- MOVQ (DX), CX80+ MOVQ (R11), R11
88- CALL CX81+ CALL R11
89 82
90 POP_REGS_HOST_TO_ABI0()83 POP_REGS_HOST_TO_ABI0()
91 RET84 RET
92 85
93 TEXT ·call5(SB), NOSPLIT, $0-5686 TEXT ·call5(SB), NOSPLIT, $0-56
94- MOVQ fn+0(FP), BX87+ MOVQ fn+0(FP), R11
95 MOVQ a1+8(FP), DI88 MOVQ a1+8(FP), DI
96 MOVQ a2+16(FP), SI89 MOVQ a2+16(FP), SI
97 MOVQ a3+24(FP), DX90 MOVQ a3+24(FP), DX
@@ -105,7 +98,7 @@ TEXT ·call5(SB), NOSPLIT, $0-56
105 SUBQ $16, SP // allocate space for alignment98 SUBQ $16, SP // allocate space for alignment
106 ANDQ $-16, SP // align on 16 bytes for SSE99 ANDQ $-16, SP // align on 16 bytes for SSE
107 100
108- CALL BX101+ CALL R11
109 102
110 MOVQ BP, SP // get SP back103 MOVQ BP, SP // get SP back
111 POPQ BP // restore BP104 POPQ BP // restore BP

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close