Skip to content

Commit bb0bfd0

Browse files
committed
cmd/dist, cmd/link, cmd/go: make c-shared work on windows
Thanks to Christopher Nelson for spearheading the effort. Fixes #11058 Change-Id: Icafabac8dc697626ff1bd943cc577b0b1cc6b349 Reviewed-on: https://go-review.googlesource.com/69091 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 0174858 commit bb0bfd0

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/cmd/dist/test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ func (t *tester) supportedBuildmode(mode string) bool {
817817
switch pair {
818818
case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le",
819819
"darwin-amd64", "darwin-386",
820-
"android-arm", "android-arm64", "android-386":
820+
"android-arm", "android-arm64", "android-386",
821+
"windows-amd64", "windows-386":
821822
return true
822823
}
823824
return false

src/cmd/go/internal/work/build.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ func BuildModeInit() {
301301
"android/amd64", "android/arm", "android/arm64", "android/386":
302302
codegenArg = "-shared"
303303
case "darwin/amd64", "darwin/386":
304+
case "windows/amd64", "windows/386":
305+
// Do not add usual .exe suffix to the .dll file.
306+
cfg.ExeSuffix = ""
304307
default:
305308
base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
306309
}
@@ -997,12 +1000,14 @@ func (b *Builder) action1(mode BuildMode, depMode BuildMode, p *load.Package, lo
9971000
name := "a.out"
9981001
if p.Internal.ExeName != "" {
9991002
name = p.Internal.ExeName
1000-
} else if cfg.Goos == "darwin" && cfg.BuildBuildmode == "c-shared" && p.Internal.Target != "" {
1003+
} else if (cfg.Goos == "darwin" || cfg.Goos == "windows") && cfg.BuildBuildmode == "c-shared" && p.Internal.Target != "" {
10011004
// On OS X, the linker output name gets recorded in the
10021005
// shared library's LC_ID_DYLIB load command.
10031006
// The code invoking the linker knows to pass only the final
10041007
// path element. Arrange that the path element matches what
10051008
// we'll install it as; otherwise the library is only loadable as "a.out".
1009+
// On Windows, DLL file name is recorded in PE file
1010+
// export section, so do like on OS X.
10061011
_, name = filepath.Split(p.Internal.Target)
10071012
}
10081013
a.Target = a.Objdir + filepath.Join("exe", name) + cfg.ExeSuffix
@@ -2641,8 +2646,10 @@ func (gcToolchain) ld(b *Builder, root *Action, out, importcfg string, allaction
26412646
// (and making the resulting shared library useless),
26422647
// run the link in the output directory so that -o can name
26432648
// just the final path element.
2649+
// On Windows, DLL file name is recorded in PE file
2650+
// export section, so do like on OS X.
26442651
dir := "."
2645-
if cfg.Goos == "darwin" && cfg.BuildBuildmode == "c-shared" {
2652+
if (cfg.Goos == "darwin" || cfg.Goos == "windows") && cfg.BuildBuildmode == "c-shared" {
26462653
dir, out = filepath.Split(out)
26472654
}
26482655

src/cmd/internal/obj/x86/asm6.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ func prefixof(ctxt *obj.Link, p *obj.Prog, a *obj.Addr) int {
22902290
return 0x26
22912291

22922292
case REG_TLS:
2293-
if ctxt.Flag_shared {
2293+
if ctxt.Flag_shared && ctxt.Headtype != objabi.Hwindows {
22942294
// When building for inclusion into a shared library, an instruction of the form
22952295
// MOV 0(CX)(TLS*1), AX
22962296
// becomes

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ func (ctxt *Link) loadlib() {
592592
}
593593

594594
if ctxt.Arch == sys.Arch386 {
595-
if (ctxt.BuildMode == BuildModeCArchive && Iself) || ctxt.BuildMode == BuildModeCShared || ctxt.BuildMode == BuildModePIE || ctxt.DynlinkingGo() {
595+
if (ctxt.BuildMode == BuildModeCArchive && Iself) || (ctxt.BuildMode == BuildModeCShared && Headtype != objabi.Hwindows) || ctxt.BuildMode == BuildModePIE || ctxt.DynlinkingGo() {
596596
got := ctxt.Syms.Lookup("_GLOBAL_OFFSET_TABLE_", 0)
597597
got.Type = sym.SDYNIMPORT
598598
got.Attr |= sym.AttrReachable
@@ -1126,9 +1126,12 @@ func (ctxt *Link) hostlink() {
11261126
if ctxt.UseRelro() {
11271127
argv = append(argv, "-Wl,-z,relro")
11281128
}
1129-
// Pass -z nodelete to mark the shared library as
1130-
// non-closeable: a dlclose will do nothing.
1131-
argv = append(argv, "-shared", "-Wl,-z,nodelete")
1129+
argv = append(argv, "-shared")
1130+
if Headtype != objabi.Hwindows {
1131+
// Pass -z nodelete to mark the shared library as
1132+
// non-closeable: a dlclose will do nothing.
1133+
argv = append(argv, "-Wl,-z,nodelete")
1134+
}
11321135
}
11331136
case BuildModeShared:
11341137
if ctxt.UseRelro() {

0 commit comments

Comments
 (0)