Skip to content

Commit ed8f0e5

Browse files
nadiasvertexianlancetaylor
authored andcommitted
cmd/go: fix -buildmode=c-archive should work on windows
Add supporting code for runtime initialization, including both 32- and 64-bit x86 architectures. Add .ctors section on Windows to PE .o files, and INITENTRY to .ctors section to plug in to the GCC C/C++ startup initialization mechanism. This allows the Go runtime to initialize itself. Add .text section symbol for .ctor relocations. Note: This is unlikely to be useful for MSVC-based toolchains. Fixes #13494 Change-Id: I4286a96f70e5f5228acae88eef46e2bed95813f3 Reviewed-on: https://go-review.googlesource.com/18057 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent 386c0e6 commit ed8f0e5

File tree

10 files changed

+248
-18
lines changed

10 files changed

+248
-18
lines changed

misc/cgo/testcarchive/carchive_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ func goEnv(key string) string {
118118
}
119119

120120
func compilemain(t *testing.T, libgo string) {
121-
ccArgs := append(cc, "-o", "testp"+exeSuffix)
121+
ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c")
122122
if GOOS == "windows" {
123-
ccArgs = append(ccArgs, "main_windows.c")
123+
ccArgs = append(ccArgs, "main_windows.c", libgo, "-lntdll", "-lws2_32")
124124
} else {
125-
ccArgs = append(ccArgs, "main_unix.c")
125+
ccArgs = append(ccArgs, "main_unix.c", libgo)
126126
}
127-
ccArgs = append(ccArgs, "main.c", libgo)
128127
t.Log(ccArgs)
129128

130129
if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {

src/cmd/dist/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ func (t *tester) supportedBuildmode(mode string) bool {
667667
}
668668
switch pair {
669669
case "darwin-386", "darwin-amd64", "darwin-arm", "darwin-arm64",
670-
"linux-amd64", "linux-386":
670+
"linux-amd64", "linux-386", "windows-amd64", "windows-386":
671671
return true
672672
}
673673
return false

src/cmd/link/internal/ld/data.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,9 @@ func textaddress() {
17451745
sect.Align = int32(Funcalign)
17461746
Linklookup(Ctxt, "runtime.text", 0).Sect = sect
17471747
Linklookup(Ctxt, "runtime.etext", 0).Sect = sect
1748+
if HEADTYPE == obj.Hwindows {
1749+
Linklookup(Ctxt, ".text", 0).Sect = sect
1750+
}
17481751
va := uint64(INITTEXT)
17491752
sect.Vaddr = va
17501753
for sym := Ctxt.Textp; sym != nil; sym = sym.Next {
@@ -1891,6 +1894,9 @@ func address() {
18911894

18921895
xdefine("runtime.text", obj.STEXT, int64(text.Vaddr))
18931896
xdefine("runtime.etext", obj.STEXT, int64(text.Vaddr+text.Length))
1897+
if HEADTYPE == obj.Hwindows {
1898+
xdefine(".text", obj.STEXT, int64(text.Vaddr))
1899+
}
18941900
xdefine("runtime.rodata", obj.SRODATA, int64(rodata.Vaddr))
18951901
xdefine("runtime.erodata", obj.SRODATA, int64(rodata.Vaddr+rodata.Length))
18961902
xdefine("runtime.typelink", obj.SRODATA, int64(typelink.Vaddr))

src/cmd/link/internal/ld/lib.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ func (mode *BuildMode) Set(s string) error {
324324
case "c-archive":
325325
switch goos {
326326
case "darwin", "linux":
327+
case "windows":
328+
switch goarch {
329+
case "amd64", "386":
330+
default:
331+
return badmode()
332+
}
327333
default:
328334
return badmode()
329335
}
@@ -1020,6 +1026,15 @@ func archive() {
10201026
}
10211027

10221028
mayberemoveoutfile()
1029+
1030+
// Force the buffer to flush here so that external
1031+
// tools will see a complete file.
1032+
Cflush()
1033+
if err := coutbuf.f.Close(); err != nil {
1034+
Exitf("close: %v", err)
1035+
}
1036+
coutbuf.f = nil
1037+
10231038
argv := []string{extar, "-q", "-c", "-s", outfile}
10241039
argv = append(argv, filepath.Join(tmpdir, "go.o"))
10251040
argv = append(argv, hostobjCopy()...)
@@ -1890,7 +1905,6 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
18901905
// These symbols won't show up in the first loop below because we
18911906
// skip STEXT symbols. Normal STEXT symbols are emitted by walking textp.
18921907
s := Linklookup(Ctxt, "runtime.text", 0)
1893-
18941908
if s.Type == obj.STEXT {
18951909
put(s, s.Name, 'T', s.Value, s.Size, int(s.Version), nil)
18961910
}

src/cmd/link/internal/ld/pe.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"cmd/internal/obj"
99
"encoding/binary"
1010
"fmt"
11+
"os"
1112
"sort"
1213
"strconv"
1314
"strings"
@@ -820,7 +821,7 @@ func perelocsect(sect *Section, first *LSym) int {
820821
}
821822

822823
// peemitreloc emits relocation entries for go.o in external linking.
823-
func peemitreloc(text, data *IMAGE_SECTION_HEADER) {
824+
func peemitreloc(text, data, ctors *IMAGE_SECTION_HEADER) {
824825
for Cpos()&7 != 0 {
825826
Cput(0)
826827
}
@@ -870,6 +871,22 @@ func peemitreloc(text, data *IMAGE_SECTION_HEADER) {
870871
data.PointerToRelocations += 10 // skip the extend reloc entry
871872
}
872873
data.NumberOfRelocations = uint16(n - 1)
874+
875+
dottext := Linklookup(Ctxt, ".text", 0)
876+
ctors.NumberOfRelocations = 1
877+
ctors.PointerToRelocations = uint32(Cpos())
878+
sectoff := ctors.VirtualAddress
879+
Lputl(uint32(sectoff))
880+
Lputl(uint32(dottext.Dynid))
881+
switch obj.Getgoarch() {
882+
default:
883+
fmt.Fprintf(os.Stderr, "link: unknown architecture for PE: %q\n", obj.Getgoarch())
884+
os.Exit(2)
885+
case "386":
886+
Wputl(IMAGE_REL_I386_DIR32)
887+
case "amd64":
888+
Wputl(IMAGE_REL_AMD64_ADDR64)
889+
}
873890
}
874891

875892
func dope() {
@@ -929,7 +946,11 @@ func writePESymTableRecords() int {
929946
}
930947

931948
// only windows/386 requires underscore prefix on external symbols
932-
if Thearch.Thechar == '8' && Linkmode == LinkExternal && (s.Type == obj.SHOSTOBJ || s.Attr.CgoExport()) && s.Name == s.Extname {
949+
if Thearch.Thechar == '8' &&
950+
Linkmode == LinkExternal &&
951+
(s.Type != obj.SDYNIMPORT || s.Attr.CgoExport()) &&
952+
s.Name == s.Extname &&
953+
s.Name != "_main" {
933954
s.Name = "_" + s.Name
934955
}
935956

@@ -984,6 +1005,11 @@ func writePESymTableRecords() int {
9841005
put(s, s.Name, 'U', 0, int64(Thearch.Ptrsize), 0, nil)
9851006
}
9861007
}
1008+
1009+
s := Linklookup(Ctxt, ".text", 0)
1010+
if s.Type == obj.STEXT {
1011+
put(s, s.Name, 'T', s.Value, s.Size, int(s.Version), nil)
1012+
}
9871013
}
9881014

9891015
genasmsym(put)
@@ -1066,6 +1092,42 @@ func addpersrc() {
10661092
dd[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = h.VirtualSize
10671093
}
10681094

1095+
func addinitarray() (c *IMAGE_SECTION_HEADER) {
1096+
// The size below was determined by the specification for array relocations,
1097+
// and by observing what GCC writes here. If the initarray section grows to
1098+
// contain more than one constructor entry, the size will need to be 8 * constructor_count.
1099+
// However, the entire Go runtime is initialized from just one function, so it is unlikely
1100+
// that this will need to grow in the future.
1101+
var size int
1102+
switch obj.Getgoarch() {
1103+
default:
1104+
fmt.Fprintf(os.Stderr, "link: unknown architecture for PE: %q\n", obj.Getgoarch())
1105+
os.Exit(2)
1106+
case "386":
1107+
size = 4
1108+
case "amd64":
1109+
size = 8
1110+
}
1111+
1112+
c = addpesection(".ctors", size, size)
1113+
c.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
1114+
c.SizeOfRawData = uint32(size)
1115+
1116+
Cseek(int64(c.PointerToRawData))
1117+
chksectoff(c, Cpos())
1118+
init_entry := Linklookup(Ctxt, INITENTRY, 0)
1119+
addr := uint64(init_entry.Value) - init_entry.Sect.Vaddr
1120+
1121+
switch obj.Getgoarch() {
1122+
case "386":
1123+
Lputl(uint32(addr))
1124+
case "amd64":
1125+
Vputl(addr)
1126+
}
1127+
1128+
return c
1129+
}
1130+
10691131
func Asmbpe() {
10701132
switch Thearch.Thechar {
10711133
default:
@@ -1087,6 +1149,7 @@ func Asmbpe() {
10871149
textsect = pensect
10881150

10891151
var d *IMAGE_SECTION_HEADER
1152+
var c *IMAGE_SECTION_HEADER
10901153
if Linkmode != LinkExternal {
10911154
d = addpesection(".data", int(Segdata.Length), int(Segdata.Filelen))
10921155
d.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
@@ -1102,6 +1165,8 @@ func Asmbpe() {
11021165
b.Characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_ALIGN_32BYTES
11031166
b.PointerToRawData = 0
11041167
bsssect = pensect
1168+
1169+
c = addinitarray()
11051170
}
11061171

11071172
if Debug['s'] == 0 {
@@ -1116,7 +1181,7 @@ func Asmbpe() {
11161181
addpesymtable()
11171182
addpersrc()
11181183
if Linkmode == LinkExternal {
1119-
peemitreloc(t, d)
1184+
peemitreloc(t, d, c)
11201185
}
11211186

11221187
fh.NumberOfSections = uint16(pensect)

src/runtime/cgo/gcc_libinit_windows.c

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,89 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// +build cgo
6+
#define WIN64_LEAN_AND_MEAN
7+
#include <windows.h>
8+
#include <process.h>
9+
510
#include <stdio.h>
611
#include <stdlib.h>
712

13+
static volatile long runtime_init_once_gate = 0;
14+
static volatile long runtime_init_once_done = 0;
15+
16+
static CRITICAL_SECTION runtime_init_cs;
17+
18+
static HANDLE runtime_init_wait;
19+
static int runtime_init_done;
20+
21+
// Pre-initialize the runtime synchronization objects
22+
void
23+
_cgo_preinit_init() {
24+
runtime_init_wait = CreateEvent(NULL, TRUE, FALSE, NULL);
25+
if (runtime_init_wait == NULL) {
26+
fprintf(stderr, "runtime: failed to create runtime initialization wait event.\n");
27+
abort();
28+
}
29+
30+
InitializeCriticalSection(&runtime_init_cs);
31+
}
32+
33+
// Make sure that the preinit sequence has run.
34+
void
35+
_cgo_maybe_run_preinit() {
36+
if (!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
37+
if (InterlockedIncrement(&runtime_init_once_gate) == 1) {
38+
_cgo_preinit_init();
39+
InterlockedIncrement(&runtime_init_once_done);
40+
} else {
41+
// Decrement to avoid overflow.
42+
InterlockedDecrement(&runtime_init_once_gate);
43+
while(!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
44+
Sleep(0);
45+
}
46+
}
47+
}
48+
}
49+
850
void
9-
x_cgo_sys_thread_create(void* (*func)(void*), void* arg) {
10-
fprintf(stderr, "x_cgo_sys_thread_create not implemented");
11-
abort();
51+
x_cgo_sys_thread_create(void (*func)(void*), void* arg) {
52+
uintptr_t thandle;
53+
54+
thandle = _beginthread(func, 0, arg);
55+
if(thandle == -1) {
56+
fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);
57+
abort();
58+
}
59+
}
60+
61+
int
62+
_cgo_is_runtime_initialized() {
63+
EnterCriticalSection(&runtime_init_cs);
64+
int status = runtime_init_done;
65+
LeaveCriticalSection(&runtime_init_cs);
66+
return status;
1267
}
1368

1469
void
1570
_cgo_wait_runtime_init_done() {
16-
// TODO(spetrovic): implement this method.
71+
_cgo_maybe_run_preinit();
72+
while (!_cgo_is_runtime_initialized()) {
73+
WaitForSingleObject(runtime_init_wait, INFINITE);
74+
}
1775
}
1876

1977
void
2078
x_cgo_notify_runtime_init_done(void* dummy) {
21-
// TODO(spetrovic): implement this method.
22-
}
79+
_cgo_maybe_run_preinit();
80+
81+
EnterCriticalSection(&runtime_init_cs);
82+
runtime_init_done = 1;
83+
LeaveCriticalSection(&runtime_init_cs);
84+
85+
if (!SetEvent(runtime_init_wait)) {
86+
fprintf(stderr, "runtime: failed to signal runtime initialization complete.\n");
87+
abort();
88+
}
89+
}
90+

src/runtime/os1_windows.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,11 @@ func semacreate(mp *m) {
396396
mp.waitsema = stdcall4(_CreateEventA, 0, 0, 0, 0)
397397
}
398398

399-
// May run with m.p==nil, so write barriers are not allowed.
400-
//go:nowritebarrier
399+
// May run with m.p==nil, so write barriers are not allowed. This
400+
// function is called by newosproc0, so it is also required to
401+
// operate without stack guards.
402+
//go:nowritebarrierc
403+
//go:nosplit
401404
func newosproc(mp *m, stk unsafe.Pointer) {
402405
const _STACK_SIZE_PARAM_IS_A_RESERVATION = 0x00010000
403406
thandle := stdcall6(_CreateThread, 0, 0x20000,
@@ -409,6 +412,15 @@ func newosproc(mp *m, stk unsafe.Pointer) {
409412
}
410413
}
411414

415+
// Used by the C library build mode. On Linux this function would allocate a
416+
// stack, but that's not necessary for Windows. No stack guards are present
417+
// and the GC has not been initialized, so write barriers will fail.
418+
//go:nowritebarrierc
419+
//go:nosplit
420+
func newosproc0(mp *m, stk unsafe.Pointer) {
421+
newosproc(mp, stk)
422+
}
423+
412424
// Called to initialize a new m (including the bootstrap m).
413425
// Called on the parent thread (main thread in case of bootstrap), can allocate memory.
414426
func mpreinit(mp *m) {

src/runtime/rt0_windows_386.s

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,39 @@ TEXT _rt0_386_windows(SB),NOSPLIT,$12
1212
MOVL $-1, 0(SP) // return PC for main
1313
JMP _main(SB)
1414

15+
// When building with -buildmode=(c-shared or c-archive), this
16+
// symbol is called. For dynamic libraries it is called when the
17+
// library is loaded. For static libraries it is called when the
18+
// final executable starts, during the C runtime initialization
19+
// phase.
20+
TEXT _rt0_386_windows_lib(SB),NOSPLIT,$0x1C
21+
MOVL BP, 0x08(SP)
22+
MOVL BX, 0x0C(SP)
23+
MOVL AX, 0x10(SP)
24+
MOVL CX, 0x14(SP)
25+
MOVL DX, 0x18(SP)
26+
27+
// Create a new thread to do the runtime initialization and return.
28+
MOVL _cgo_sys_thread_create(SB), AX
29+
MOVL $_rt0_386_windows_lib_go(SB), 0x00(SP)
30+
MOVL $0, 0x04(SP)
31+
32+
// Top two items on the stack are passed to _cgo_sys_thread_create
33+
// as parameters. This is the calling convention on 32-bit Windows.
34+
CALL AX
35+
36+
MOVL 0x08(SP), BP
37+
MOVL 0x0C(SP), BX
38+
MOVL 0x10(SP), AX
39+
MOVL 0x14(SP), CX
40+
MOVL 0x18(SP), DX
41+
RET
42+
43+
TEXT _rt0_386_windows_lib_go(SB),NOSPLIT,$0
44+
MOVL $0, DI
45+
MOVL $0, SI
46+
MOVL $runtime·rt0_go(SB), AX
47+
JMP AX
48+
1549
TEXT _main(SB),NOSPLIT,$0
1650
JMP runtime·rt0_go(SB)

0 commit comments

Comments
 (0)