aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorqmuntal <quimmuntal@gmail.com>2025-09-22 15:48:36 +0200
committerQuim Muntal <quimmuntal@gmail.com>2025-09-29 12:24:13 -0700
commiteaf2345256613dfbda7e8e69e5f845c4209246c6 (patch)
tree0fb9914a190feb83c14cdb40e5c9d3a24abe0608 /src/runtime
parent4b7773356515c178f0af859b952b4b3a78f0813d (diff)
downloadgo-eaf2345256613dfbda7e8e69e5f845c4209246c6.tar.xz
cmd/link: use a .def file to mark exported symbols on Windows
Binutils defaults to exporting all symbols when building a Windows DLL. To avoid that we were marking symbols with __declspec(dllexport) in the cgo-generated headers, which instructs ld to export only those symbols. However, that approach makes the headers hard to reuse when importing the resulting DLL into other projects, as imported symbols should be marked with __declspec(dllimport). A better approach is to generate a .def file listing the symbols to export, which gets the same effect without having to modify the headers. Updates #30674 Fixes #56994 Change-Id: I22bd0aa079e2be4ae43b13d893f6b804eaeddabf Reviewed-on: https://go-review.googlesource.com/c/go/+/705776 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Junyang Shao <shaojunyang@google.com> Reviewed-by: Than McIntosh <thanm@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/cgo/gcc_libinit_windows.c9
-rw-r--r--src/runtime/cgo/windows.go22
2 files changed, 22 insertions, 9 deletions
diff --git a/src/runtime/cgo/gcc_libinit_windows.c b/src/runtime/cgo/gcc_libinit_windows.c
index 926f916843..7e7ff3e667 100644
--- a/src/runtime/cgo/gcc_libinit_windows.c
+++ b/src/runtime/cgo/gcc_libinit_windows.c
@@ -15,15 +15,6 @@
#include "libcgo.h"
#include "libcgo_windows.h"
-// Ensure there's one symbol marked __declspec(dllexport).
-// If there are no exported symbols, the unfortunate behavior of
-// the binutils linker is to also strip the relocations table,
-// resulting in non-PIE binary. The other option is the
-// --export-all-symbols flag, but we don't need to export all symbols
-// and this may overflow the export table (#40795).
-// See https://sourceware.org/bugzilla/show_bug.cgi?id=19011
-__declspec(dllexport) int _cgo_dummy_export;
-
static volatile LONG runtime_init_once_gate = 0;
static volatile LONG runtime_init_once_done = 0;
diff --git a/src/runtime/cgo/windows.go b/src/runtime/cgo/windows.go
new file mode 100644
index 0000000000..7ba61753df
--- /dev/null
+++ b/src/runtime/cgo/windows.go
@@ -0,0 +1,22 @@
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build windows
+
+package cgo
+
+import _ "unsafe" // for go:linkname
+
+// _cgo_stub_export is only used to ensure there's at least one symbol
+// in the .def file passed to the external linker.
+// If there are no exported symbols, the unfortunate behavior of
+// the binutils linker is to also strip the relocations table,
+// resulting in non-PIE binary. The other option is the
+// --export-all-symbols flag, but we don't need to export all symbols
+// and this may overflow the export table (#40795).
+// See https://sourceware.org/bugzilla/show_bug.cgi?id=19011
+//
+//go:cgo_export_static _cgo_stub_export
+//go:linkname _cgo_stub_export _cgo_stub_export
+var _cgo_stub_export uintptr