Skip to content

Commit cc6554f

Browse files
spetrovic77ianlancetaylor
authored andcommitted
cmd/link/internal/ld, cmd/go: -buildmode=c-shared support for darwin/amd64
All of the heavy-lifting was done by minux@, with his external-linking support for darwin/arm64: golang.org/cl/8781 Change-Id: I7c9fbc19246f418c065c92fb2c13c00026ff0f82 Reviewed-on: https://go-review.googlesource.com/11127 Run-TryBot: Srdjan Petrovic <spetrovic@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent dc89350 commit cc6554f

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

misc/cgo/testcshared/test.bash

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ goarch=$(go env GOARCH)
2020
# Directory where cgo headers and outputs will be installed.
2121
# The installation directory format varies depending on the platform.
2222
installdir=pkg/${goos}_${goarch}_testcshared_shared
23-
if [ "${goos}/${goarch}" == "android/arm" ]; then
23+
if [ "${goos}/${goarch}" == "android/arm" ] || [ "${goos}/${goarch}" == "darwin/amd64" ]; then
2424
installdir=pkg/${goos}_${goarch}_testcshared
2525
fi
2626

@@ -70,15 +70,20 @@ rm -rf pkg
7070

7171
suffix="-installsuffix testcshared"
7272

73+
libext="so"
74+
if [ "$goos" == "darwin" ]; then
75+
libext="dylib"
76+
fi
77+
7378
# Create the header files.
7479
GOPATH=$(pwd) go install -buildmode=c-shared $suffix libgo
7580

76-
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.so src/libgo/libgo.go
77-
binpush libgo.so
81+
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.$libext src/libgo/libgo.go
82+
binpush libgo.$libext
7883

7984
# test0: exported symbols in shared lib are accessible.
8085
# TODO(iant): using _shared here shouldn't really be necessary.
81-
$(go env CC) $(go env GOGCCFLAGS) -I ${installdir} -o testp main0.c libgo.so
86+
$(go env CC) $(go env GOGCCFLAGS) -I ${installdir} -o testp main0.c libgo.$libext
8287
binpush testp
8388

8489
output=$(run LD_LIBRARY_PATH=. ./testp)
@@ -87,19 +92,23 @@ if [ "$output" != "PASS" ]; then
8792
exit 1
8893
fi
8994

90-
# test1: .so can be dynamically loaded and exported symbols are accessible.
95+
# test1: shared library can be dynamically loaded and exported symbols are accessible.
9196
$(go env CC) $(go env GOGCCFLAGS) -o testp main1.c -ldl
9297
binpush testp
93-
output=$(run ./testp ./libgo.so)
98+
output=$(run ./testp ./libgo.$libext)
9499
if [ "$output" != "PASS" ]; then
95100
echo "FAIL test1 got ${output}"
96101
exit 1
97102
fi
98103

99-
# test2: tests libgo2.so which does not export any functions.
100-
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.so src/libgo2/libgo2.go
101-
binpush libgo2.so
102-
$(go env CC) $(go env GOGCCFLAGS) -o testp2 main2.c -Wl,--no-as-needed libgo2.so
104+
# test2: tests libgo2 which does not export any functions.
105+
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.$libext src/libgo2/libgo2.go
106+
binpush libgo2.$libext
107+
linkflags="-Wl,--no-as-needed"
108+
if [ "$goos" == "darwin" ]; then
109+
linkflags=""
110+
fi
111+
$(go env CC) $(go env GOGCCFLAGS) -o testp2 main2.c $linkflags libgo2.$libext
103112
binpush testp2
104113
output=$(run LD_LIBRARY_PATH=. ./testp2)
105114
if [ "$output" != "PASS" ]; then

src/cmd/dist/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func (t *tester) supportedBuildmode(mode string) bool {
571571
case "c-shared":
572572
// TODO(hyangah): add linux-386.
573573
switch pair {
574-
case "linux-amd64", "android-arm":
574+
case "linux-amd64", "darwin-amd64", "android-arm":
575575
return true
576576
}
577577
return false

src/cmd/go/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ func buildModeInit() {
334334
codegenArg = "-shared"
335335
case "linux/arm":
336336
buildAsmflags = append(buildAsmflags, "-shared")
337+
case "darwin/amd64":
337338
case "android/arm":
338339
default:
339340
fatalf("-buildmode=c-shared not supported on %s\n", platform)

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ func hostlink() {
926926
}
927927

928928
if HEADTYPE == obj.Hdarwin {
929-
argv = append(argv, "-Wl,-no_pie,-pagezero_size,4000000,-headerpad,1144")
929+
argv = append(argv, "-Wl,-no_pie,-headerpad,1144")
930930
}
931931
if HEADTYPE == obj.Hopenbsd {
932932
argv = append(argv, "-Wl,-nopie")
@@ -944,9 +944,17 @@ func hostlink() {
944944
}
945945

946946
switch Buildmode {
947+
case BuildmodeExe:
948+
if HEADTYPE == obj.Hdarwin {
949+
argv = append(argv, "-Wl,-pagezero_size,4000000")
950+
}
947951
case BuildmodeCShared:
948-
argv = append(argv, "-Wl,-Bsymbolic")
949-
argv = append(argv, "-shared")
952+
if HEADTYPE == obj.Hdarwin {
953+
argv = append(argv, "-dynamiclib")
954+
} else {
955+
argv = append(argv, "-Wl,-Bsymbolic")
956+
argv = append(argv, "-shared")
957+
}
950958
case BuildmodeShared:
951959
// TODO(mwhudson): unless you do this, dynamic relocations fill
952960
// out the findfunctab table and for some reason shared libraries

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ func machoshbits(mseg *MachoSeg, sect *Section, segname string) {
356356
buf := "__" + strings.Replace(sect.Name[1:], ".", "_", -1)
357357

358358
var msect *MachoSect
359-
if Thearch.Thechar == '7' && sect.Rwx&1 == 0 {
360-
// darwin/arm64 forbids absolute relocs in __TEXT, so if
361-
// the section is not executable, put it in __DATA segment.
359+
if sect.Rwx&1 == 0 && (Thearch.Thechar == '7' || (Thearch.Thechar == '6' && Buildmode == BuildmodeCShared)) {
360+
// Darwin external linker on arm64 and on amd64 in c-shared buildmode
361+
// complains about absolute relocs in __TEXT, so if the section is not
362+
// executable, put it in __DATA segment.
362363
msect = newMachoSect(mseg, buf, "__DATA")
363364
} else {
364365
msect = newMachoSect(mseg, buf, segname)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var machHeader *macho.FileHeader
2222
var mappedHeader []byte
2323

2424
const (
25+
LC_ID_DYLIB = 0xd
2526
LC_LOAD_DYLINKER = 0xe
2627
LC_PREBOUND_DYLIB = 0x10
2728
LC_LOAD_WEAK_DYLIB = 0x18
@@ -246,7 +247,7 @@ func machoCombineDwarf(inexe, dsym, outexe string) error {
246247
err = machoUpdateLoadCommand(reader, &linkEditDataCmd{}, "DataOff")
247248
case LC_ENCRYPTION_INFO, LC_ENCRYPTION_INFO_64:
248249
err = machoUpdateLoadCommand(reader, &encryptionInfoCmd{}, "CryptOff")
249-
case macho.LoadCmdDylib, macho.LoadCmdThread, macho.LoadCmdUnixThread, LC_PREBOUND_DYLIB, LC_UUID, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_SOURCE_VERSION, LC_MAIN, LC_LOAD_DYLINKER, LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_RPATH:
250+
case macho.LoadCmdDylib, macho.LoadCmdThread, macho.LoadCmdUnixThread, LC_PREBOUND_DYLIB, LC_UUID, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_SOURCE_VERSION, LC_MAIN, LC_LOAD_DYLINKER, LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_RPATH, LC_ID_DYLIB:
250251
// Nothing to update
251252
default:
252253
err = fmt.Errorf("Unknown load command 0x%x (%s)\n", int(cmd.Cmd), cmd.Cmd)

0 commit comments

Comments
 (0)