Skip to content

Commit

Permalink
cmd/dist, cmd/link, cmd/go: make c-shared work on windows
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
alexbrainman committed Oct 10, 2017
1 parent 0174858 commit bb0bfd0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/cmd/dist/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,8 @@ func (t *tester) supportedBuildmode(mode string) bool {
switch pair {
case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le",
"darwin-amd64", "darwin-386",
"android-arm", "android-arm64", "android-386":
"android-arm", "android-arm64", "android-386",
"windows-amd64", "windows-386":
return true
}
return false
Expand Down
11 changes: 9 additions & 2 deletions src/cmd/go/internal/work/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ func BuildModeInit() {
"android/amd64", "android/arm", "android/arm64", "android/386":
codegenArg = "-shared"
case "darwin/amd64", "darwin/386":
case "windows/amd64", "windows/386":
// Do not add usual .exe suffix to the .dll file.
cfg.ExeSuffix = ""
default:
base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
}
Expand Down Expand Up @@ -997,12 +1000,14 @@ func (b *Builder) action1(mode BuildMode, depMode BuildMode, p *load.Package, lo
name := "a.out"
if p.Internal.ExeName != "" {
name = p.Internal.ExeName
} else if cfg.Goos == "darwin" && cfg.BuildBuildmode == "c-shared" && p.Internal.Target != "" {
} else if (cfg.Goos == "darwin" || cfg.Goos == "windows") && cfg.BuildBuildmode == "c-shared" && p.Internal.Target != "" {
// On OS X, the linker output name gets recorded in the
// shared library's LC_ID_DYLIB load command.
// The code invoking the linker knows to pass only the final
// path element. Arrange that the path element matches what
// we'll install it as; otherwise the library is only loadable as "a.out".
// On Windows, DLL file name is recorded in PE file
// export section, so do like on OS X.
_, name = filepath.Split(p.Internal.Target)
}
a.Target = a.Objdir + filepath.Join("exe", name) + cfg.ExeSuffix
Expand Down Expand Up @@ -2641,8 +2646,10 @@ func (gcToolchain) ld(b *Builder, root *Action, out, importcfg string, allaction
// (and making the resulting shared library useless),
// run the link in the output directory so that -o can name
// just the final path element.
// On Windows, DLL file name is recorded in PE file
// export section, so do like on OS X.
dir := "."
if cfg.Goos == "darwin" && cfg.BuildBuildmode == "c-shared" {
if (cfg.Goos == "darwin" || cfg.Goos == "windows") && cfg.BuildBuildmode == "c-shared" {
dir, out = filepath.Split(out)
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/internal/obj/x86/asm6.go
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,7 @@ func prefixof(ctxt *obj.Link, p *obj.Prog, a *obj.Addr) int {
return 0x26

case REG_TLS:
if ctxt.Flag_shared {
if ctxt.Flag_shared && ctxt.Headtype != objabi.Hwindows {
// When building for inclusion into a shared library, an instruction of the form
// MOV 0(CX)(TLS*1), AX
// becomes
Expand Down
11 changes: 7 additions & 4 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (ctxt *Link) loadlib() {
}

if ctxt.Arch == sys.Arch386 {
if (ctxt.BuildMode == BuildModeCArchive && Iself) || ctxt.BuildMode == BuildModeCShared || ctxt.BuildMode == BuildModePIE || ctxt.DynlinkingGo() {
if (ctxt.BuildMode == BuildModeCArchive && Iself) || (ctxt.BuildMode == BuildModeCShared && Headtype != objabi.Hwindows) || ctxt.BuildMode == BuildModePIE || ctxt.DynlinkingGo() {
got := ctxt.Syms.Lookup("_GLOBAL_OFFSET_TABLE_", 0)
got.Type = sym.SDYNIMPORT
got.Attr |= sym.AttrReachable
Expand Down Expand Up @@ -1126,9 +1126,12 @@ func (ctxt *Link) hostlink() {
if ctxt.UseRelro() {
argv = append(argv, "-Wl,-z,relro")
}
// Pass -z nodelete to mark the shared library as
// non-closeable: a dlclose will do nothing.
argv = append(argv, "-shared", "-Wl,-z,nodelete")
argv = append(argv, "-shared")
if Headtype != objabi.Hwindows {
// Pass -z nodelete to mark the shared library as
// non-closeable: a dlclose will do nothing.
argv = append(argv, "-Wl,-z,nodelete")
}
}
case BuildModeShared:
if ctxt.UseRelro() {
Expand Down

0 comments on commit bb0bfd0

Please sign in to comment.