]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: use a .def file to mark exported symbols on Windows
authorqmuntal <quimmuntal@gmail.com>
Mon, 22 Sep 2025 13:48:36 +0000 (15:48 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Mon, 29 Sep 2025 19:24:13 +0000 (12:24 -0700)
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>

src/cmd/cgo/internal/testcshared/cshared_test.go
src/cmd/cgo/out.go
src/cmd/link/internal/ld/lib.go
src/cmd/link/internal/ld/pe.go
src/cmd/link/internal/ld/xcoff.go
src/cmd/link/internal/loader/loader.go
src/runtime/cgo/gcc_libinit_windows.c
src/runtime/cgo/windows.go [new file with mode: 0644]

index f1c30f8f9a2b2ae3ba50ad71556af8e4776b0660..2ce705adba44f387eb385fbc6d85f2e602802f03 100644 (file)
@@ -8,6 +8,7 @@ import (
        "bufio"
        "bytes"
        "cmd/cgo/internal/cgotest"
+       "cmp"
        "debug/elf"
        "debug/pe"
        "encoding/binary"
@@ -272,7 +273,7 @@ func createHeaders() error {
                // which results in the linkers output implib getting overwritten at each step. So instead build the
                // import library the traditional way, using a def file.
                err = os.WriteFile("libgo.def",
-                       []byte("LIBRARY libgo.dll\nEXPORTS\n\tDidInitRun\n\tDidMainRun\n\tDivu\n\tFromPkg\n\t_cgo_dummy_export\n"),
+                       []byte("LIBRARY libgo.dll\nEXPORTS\n\tDidInitRun\n\tDidMainRun\n\tDivu\n\tFromPkg\n"),
                        0644)
                if err != nil {
                        return fmt.Errorf("unable to write def file: %v", err)
@@ -375,9 +376,23 @@ func TestExportedSymbols(t *testing.T) {
        }
 }
 
-func checkNumberOfExportedFunctionsWindows(t *testing.T, prog string, exportedFunctions int, wantAll bool) {
+func checkNumberOfExportedSymbolsWindows(t *testing.T, exportedSymbols int, wantAll bool) {
+       t.Parallel()
        tmpdir := t.TempDir()
 
+       prog := `
+package main
+import "C"
+func main() {}
+`
+
+       for i := range exportedSymbols {
+               prog += fmt.Sprintf(`
+//export GoFunc%d
+func GoFunc%d() {}
+`, i, i)
+       }
+
        srcfile := filepath.Join(tmpdir, "test.go")
        objfile := filepath.Join(tmpdir, "test.dll")
        if err := os.WriteFile(srcfile, []byte(prog), 0666); err != nil {
@@ -443,18 +458,19 @@ func checkNumberOfExportedFunctionsWindows(t *testing.T, prog string, exportedFu
                t.Fatalf("binary.Read failed: %v", err)
        }
 
-       // Only the two exported functions and _cgo_dummy_export should be exported.
+       exportedSymbols = cmp.Or(exportedSymbols, 1) // _cgo_stub_export is exported if there are no other symbols exported
+
        // NumberOfNames is the number of functions exported with a unique name.
        // NumberOfFunctions can be higher than that because it also counts
        // functions exported only by ordinal, a unique number asigned by the linker,
        // and linkers might add an unknown number of their own ordinal-only functions.
        if wantAll {
-               if e.NumberOfNames <= uint32(exportedFunctions) {
-                       t.Errorf("got %d exported names, want > %d", e.NumberOfNames, exportedFunctions)
+               if e.NumberOfNames <= uint32(exportedSymbols) {
+                       t.Errorf("got %d exported names, want > %d", e.NumberOfNames, exportedSymbols)
                }
        } else {
-               if e.NumberOfNames > uint32(exportedFunctions) {
-                       t.Errorf("got %d exported names, want <= %d", e.NumberOfNames, exportedFunctions)
+               if e.NumberOfNames != uint32(exportedSymbols) {
+                       t.Errorf("got %d exported names, want %d", e.NumberOfNames, exportedSymbols)
                }
        }
 }
@@ -470,43 +486,14 @@ func TestNumberOfExportedFunctions(t *testing.T) {
 
        t.Parallel()
 
-       const prog0 = `
-package main
-
-import "C"
-
-func main() {
-}
-`
-
-       const prog2 = `
-package main
-
-import "C"
-
-//export GoFunc
-func GoFunc() {
-       println(42)
-}
-
-//export GoFunc2
-func GoFunc2() {
-       println(24)
-}
-
-func main() {
-}
-`
-       // All programs export _cgo_dummy_export, so add 1 to the expected counts.
-       t.Run("OnlyExported/0", func(t *testing.T) {
-               checkNumberOfExportedFunctionsWindows(t, prog0, 0+1, false)
-       })
-       t.Run("OnlyExported/2", func(t *testing.T) {
-               checkNumberOfExportedFunctionsWindows(t, prog2, 2+1, false)
-       })
-       t.Run("All", func(t *testing.T) {
-               checkNumberOfExportedFunctionsWindows(t, prog2, 2+1, true)
-       })
+       for i := range 3 {
+               t.Run(fmt.Sprintf("OnlyExported/%d", i), func(t *testing.T) {
+                       checkNumberOfExportedSymbolsWindows(t, i, false)
+               })
+               t.Run(fmt.Sprintf("All/%d", i), func(t *testing.T) {
+                       checkNumberOfExportedSymbolsWindows(t, i, true)
+               })
+       }
 }
 
 // test1: shared library can be dynamically loaded and exported symbols are accessible.
index dfa54e41d33399919155bcbb8e2010f68e76f38d..622d35ac7b3babbe5140386a5a89933843f6c1b7 100644 (file)
@@ -1005,12 +1005,8 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
                }
 
                // Build the wrapper function compiled by gcc.
-               gccExport := ""
-               if goos == "windows" {
-                       gccExport = "__declspec(dllexport) "
-               }
                var s strings.Builder
-               fmt.Fprintf(&s, "%s%s %s(", gccExport, gccResult, exp.ExpName)
+               fmt.Fprintf(&s, "%s %s(", gccResult, exp.ExpName)
                if fn.Recv != nil {
                        s.WriteString(p.cgoType(fn.Recv.List[0].Type).C.String())
                        s.WriteString(" recv")
index c7596d535e0ee5153dc6ab16ff76d8ff98f8a4b8..79d3d37835e9ad302c6d82023eee66e2ae187ad2 100644 (file)
@@ -1772,7 +1772,8 @@ func (ctxt *Link) hostlink() {
        }
 
        // Force global symbols to be exported for dlopen, etc.
-       if ctxt.IsELF {
+       switch {
+       case ctxt.IsELF:
                if ctxt.DynlinkingGo() || ctxt.BuildMode == BuildModeCShared || !linkerFlagSupported(ctxt.Arch, argv[0], altLinker, "-Wl,--export-dynamic-symbol=main") {
                        argv = append(argv, "-rdynamic")
                } else {
@@ -1783,10 +1784,12 @@ func (ctxt *Link) hostlink() {
                        sort.Strings(exports)
                        argv = append(argv, exports...)
                }
-       }
-       if ctxt.HeadType == objabi.Haix {
+       case ctxt.IsAIX():
                fileName := xcoffCreateExportFile(ctxt)
                argv = append(argv, "-Wl,-bE:"+fileName)
+       case ctxt.IsWindows() && !slices.Contains(flagExtldflags, "-Wl,--export-all-symbols"):
+               fileName := peCreateExportFile(ctxt, filepath.Base(outopt))
+               argv = append(argv, fileName)
        }
 
        const unusedArguments = "-Qunused-arguments"
index 5219a98dd47cf4bd2f449d150a131c08f9c4a416..0f0650e5e149e37dbba1805eb3af51745f71cd97 100644 (file)
@@ -8,6 +8,7 @@
 package ld
 
 import (
+       "bytes"
        "cmd/internal/objabi"
        "cmd/internal/sys"
        "cmd/link/internal/loader"
@@ -17,6 +18,8 @@ import (
        "fmt"
        "internal/buildcfg"
        "math"
+       "os"
+       "path/filepath"
        "slices"
        "sort"
        "strconv"
@@ -1748,3 +1751,44 @@ func asmbPe(ctxt *Link) {
 
        pewrite(ctxt)
 }
+
+// peCreateExportFile creates a file with exported symbols for Windows .def files.
+// ld will export all symbols, even those not marked for export, unless a .def file is provided.
+func peCreateExportFile(ctxt *Link, libName string) (fname string) {
+       fname = filepath.Join(*flagTmpdir, "export_file.def")
+       var buf bytes.Buffer
+
+       fmt.Fprintf(&buf, "LIBRARY %s\n", libName)
+       buf.WriteString("EXPORTS\n")
+
+       ldr := ctxt.loader
+       var exports []string
+       for s := range ldr.ForAllCgoExportStatic() {
+               extname := ldr.SymExtname(s)
+               if !strings.HasPrefix(extname, "_cgoexp_") {
+                       continue
+               }
+               if ldr.IsFileLocal(s) {
+                       continue // Only export non-static symbols
+               }
+               // Retrieve the name of the initial symbol
+               // exported by cgo.
+               // The corresponding Go symbol is:
+               // _cgoexp_hashcode_symname.
+               name := strings.SplitN(extname, "_", 4)[3]
+               exports = append(exports, name)
+       }
+       if len(exports) == 0 {
+               // See runtime/cgo/windows.go for details.
+               exports = append(exports, "_cgo_stub_export")
+       }
+       sort.Strings(exports)
+       buf.WriteString(strings.Join(exports, "\n"))
+
+       err := os.WriteFile(fname, buf.Bytes(), 0666)
+       if err != nil {
+               Errorf("WriteFile %s failed: %v", fname, err)
+       }
+
+       return fname
+}
index 1bce2cf9b6124d41e33ba26385bcd31a8a4e9f9c..da728e2545561845ea80a4def26e6910e1121ac0 100644 (file)
@@ -1779,10 +1779,7 @@ func xcoffCreateExportFile(ctxt *Link) (fname string) {
        var buf bytes.Buffer
 
        ldr := ctxt.loader
-       for s, nsym := loader.Sym(1), loader.Sym(ldr.NSym()); s < nsym; s++ {
-               if !ldr.AttrCgoExport(s) {
-                       continue
-               }
+       for s := range ldr.ForAllCgoExportStatic() {
                extname := ldr.SymExtname(s)
                if !strings.HasPrefix(extname, "._cgoexp_") {
                        continue
index 9f3ea3e553dd7ced38bd0478d9b5a1c598df07a4..103dad0300126343199fb3a4963955c14e8aa060 100644 (file)
@@ -16,6 +16,7 @@ import (
        "fmt"
        "internal/abi"
        "io"
+       "iter"
        "log"
        "math/bits"
        "os"
@@ -1109,6 +1110,18 @@ func (l *Loader) SetAttrCgoExportStatic(i Sym, v bool) {
        }
 }
 
+// ForAllCgoExportStatic returns an iterator over all symbols
+// marked with the "cgo_export_static" compiler directive.
+func (l *Loader) ForAllCgoExportStatic() iter.Seq[Sym] {
+       return func(yield func(Sym) bool) {
+               for s := range l.attrCgoExportStatic {
+                       if !yield(s) {
+                               break
+                       }
+               }
+       }
+}
+
 // IsGeneratedSym returns true if a symbol's been previously marked as a
 // generator symbol through the SetIsGeneratedSym. The functions for generator
 // symbols are kept in the Link context.
index 926f9168434638b38af612841db6ad34373f2dab..7e7ff3e667266f2aaa61307f44db3e9d71c3fd68 100644 (file)
 #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 (file)
index 0000000..7ba6175
--- /dev/null
@@ -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