1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:50:05 -07:00

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>
This commit is contained in:
Srdjan Petrovic 2015-06-16 10:07:45 -07:00 committed by Ian Lance Taylor
parent dc89350fed
commit cc6554f750
6 changed files with 38 additions and 18 deletions

View File

@ -20,7 +20,7 @@ goarch=$(go env GOARCH)
# Directory where cgo headers and outputs will be installed. # Directory where cgo headers and outputs will be installed.
# The installation directory format varies depending on the platform. # The installation directory format varies depending on the platform.
installdir=pkg/${goos}_${goarch}_testcshared_shared installdir=pkg/${goos}_${goarch}_testcshared_shared
if [ "${goos}/${goarch}" == "android/arm" ]; then if [ "${goos}/${goarch}" == "android/arm" ] || [ "${goos}/${goarch}" == "darwin/amd64" ]; then
installdir=pkg/${goos}_${goarch}_testcshared installdir=pkg/${goos}_${goarch}_testcshared
fi fi
@ -70,15 +70,20 @@ rm -rf pkg
suffix="-installsuffix testcshared" suffix="-installsuffix testcshared"
libext="so"
if [ "$goos" == "darwin" ]; then
libext="dylib"
fi
# Create the header files. # Create the header files.
GOPATH=$(pwd) go install -buildmode=c-shared $suffix libgo GOPATH=$(pwd) go install -buildmode=c-shared $suffix libgo
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.so src/libgo/libgo.go GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.$libext src/libgo/libgo.go
binpush libgo.so binpush libgo.$libext
# test0: exported symbols in shared lib are accessible. # test0: exported symbols in shared lib are accessible.
# TODO(iant): using _shared here shouldn't really be necessary. # TODO(iant): using _shared here shouldn't really be necessary.
$(go env CC) $(go env GOGCCFLAGS) -I ${installdir} -o testp main0.c libgo.so $(go env CC) $(go env GOGCCFLAGS) -I ${installdir} -o testp main0.c libgo.$libext
binpush testp binpush testp
output=$(run LD_LIBRARY_PATH=. ./testp) output=$(run LD_LIBRARY_PATH=. ./testp)
@ -87,19 +92,23 @@ if [ "$output" != "PASS" ]; then
exit 1 exit 1
fi fi
# test1: .so can be dynamically loaded and exported symbols are accessible. # test1: shared library can be dynamically loaded and exported symbols are accessible.
$(go env CC) $(go env GOGCCFLAGS) -o testp main1.c -ldl $(go env CC) $(go env GOGCCFLAGS) -o testp main1.c -ldl
binpush testp binpush testp
output=$(run ./testp ./libgo.so) output=$(run ./testp ./libgo.$libext)
if [ "$output" != "PASS" ]; then if [ "$output" != "PASS" ]; then
echo "FAIL test1 got ${output}" echo "FAIL test1 got ${output}"
exit 1 exit 1
fi fi
# test2: tests libgo2.so which does not export any functions. # test2: tests libgo2 which does not export any functions.
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.so src/libgo2/libgo2.go GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.$libext src/libgo2/libgo2.go
binpush libgo2.so binpush libgo2.$libext
$(go env CC) $(go env GOGCCFLAGS) -o testp2 main2.c -Wl,--no-as-needed libgo2.so linkflags="-Wl,--no-as-needed"
if [ "$goos" == "darwin" ]; then
linkflags=""
fi
$(go env CC) $(go env GOGCCFLAGS) -o testp2 main2.c $linkflags libgo2.$libext
binpush testp2 binpush testp2
output=$(run LD_LIBRARY_PATH=. ./testp2) output=$(run LD_LIBRARY_PATH=. ./testp2)
if [ "$output" != "PASS" ]; then if [ "$output" != "PASS" ]; then

View File

@ -571,7 +571,7 @@ func (t *tester) supportedBuildmode(mode string) bool {
case "c-shared": case "c-shared":
// TODO(hyangah): add linux-386. // TODO(hyangah): add linux-386.
switch pair { switch pair {
case "linux-amd64", "android-arm": case "linux-amd64", "darwin-amd64", "android-arm":
return true return true
} }
return false return false

View File

@ -334,6 +334,7 @@ func buildModeInit() {
codegenArg = "-shared" codegenArg = "-shared"
case "linux/arm": case "linux/arm":
buildAsmflags = append(buildAsmflags, "-shared") buildAsmflags = append(buildAsmflags, "-shared")
case "darwin/amd64":
case "android/arm": case "android/arm":
default: default:
fatalf("-buildmode=c-shared not supported on %s\n", platform) fatalf("-buildmode=c-shared not supported on %s\n", platform)

View File

@ -926,7 +926,7 @@ func hostlink() {
} }
if HEADTYPE == obj.Hdarwin { if HEADTYPE == obj.Hdarwin {
argv = append(argv, "-Wl,-no_pie,-pagezero_size,4000000,-headerpad,1144") argv = append(argv, "-Wl,-no_pie,-headerpad,1144")
} }
if HEADTYPE == obj.Hopenbsd { if HEADTYPE == obj.Hopenbsd {
argv = append(argv, "-Wl,-nopie") argv = append(argv, "-Wl,-nopie")
@ -944,9 +944,17 @@ func hostlink() {
} }
switch Buildmode { switch Buildmode {
case BuildmodeExe:
if HEADTYPE == obj.Hdarwin {
argv = append(argv, "-Wl,-pagezero_size,4000000")
}
case BuildmodeCShared: case BuildmodeCShared:
argv = append(argv, "-Wl,-Bsymbolic") if HEADTYPE == obj.Hdarwin {
argv = append(argv, "-shared") argv = append(argv, "-dynamiclib")
} else {
argv = append(argv, "-Wl,-Bsymbolic")
argv = append(argv, "-shared")
}
case BuildmodeShared: case BuildmodeShared:
// TODO(mwhudson): unless you do this, dynamic relocations fill // TODO(mwhudson): unless you do this, dynamic relocations fill
// out the findfunctab table and for some reason shared libraries // out the findfunctab table and for some reason shared libraries

View File

@ -356,9 +356,10 @@ func machoshbits(mseg *MachoSeg, sect *Section, segname string) {
buf := "__" + strings.Replace(sect.Name[1:], ".", "_", -1) buf := "__" + strings.Replace(sect.Name[1:], ".", "_", -1)
var msect *MachoSect var msect *MachoSect
if Thearch.Thechar == '7' && sect.Rwx&1 == 0 { if sect.Rwx&1 == 0 && (Thearch.Thechar == '7' || (Thearch.Thechar == '6' && Buildmode == BuildmodeCShared)) {
// darwin/arm64 forbids absolute relocs in __TEXT, so if // Darwin external linker on arm64 and on amd64 in c-shared buildmode
// the section is not executable, put it in __DATA segment. // complains about absolute relocs in __TEXT, so if the section is not
// executable, put it in __DATA segment.
msect = newMachoSect(mseg, buf, "__DATA") msect = newMachoSect(mseg, buf, "__DATA")
} else { } else {
msect = newMachoSect(mseg, buf, segname) msect = newMachoSect(mseg, buf, segname)

View File

@ -22,6 +22,7 @@ var machHeader *macho.FileHeader
var mappedHeader []byte var mappedHeader []byte
const ( const (
LC_ID_DYLIB = 0xd
LC_LOAD_DYLINKER = 0xe LC_LOAD_DYLINKER = 0xe
LC_PREBOUND_DYLIB = 0x10 LC_PREBOUND_DYLIB = 0x10
LC_LOAD_WEAK_DYLIB = 0x18 LC_LOAD_WEAK_DYLIB = 0x18
@ -246,7 +247,7 @@ func machoCombineDwarf(inexe, dsym, outexe string) error {
err = machoUpdateLoadCommand(reader, &linkEditDataCmd{}, "DataOff") err = machoUpdateLoadCommand(reader, &linkEditDataCmd{}, "DataOff")
case LC_ENCRYPTION_INFO, LC_ENCRYPTION_INFO_64: case LC_ENCRYPTION_INFO, LC_ENCRYPTION_INFO_64:
err = machoUpdateLoadCommand(reader, &encryptionInfoCmd{}, "CryptOff") err = machoUpdateLoadCommand(reader, &encryptionInfoCmd{}, "CryptOff")
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: 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:
// Nothing to update // Nothing to update
default: default:
err = fmt.Errorf("Unknown load command 0x%x (%s)\n", int(cmd.Cmd), cmd.Cmd) err = fmt.Errorf("Unknown load command 0x%x (%s)\n", int(cmd.Cmd), cmd.Cmd)