Skip to content

Commit 4cf3b3c

Browse files
eliasnaurtmm1
authored andcommitted
cmd/link/internal/ld: enable bitcode builds for iOS, tvOS, watchOS
The Go toolchain cannot output bitcode, but there is a trick where object code can be marked with an __asm section, persuading the Apple toolchain to include our object code in bitcode builds. This enables Go builds with bitcode enabled; the next CL adds the necessary plumbing for building on tvOS and watchOS. Thanks to Aman Gupta for the trick. Test is added two CLs from here. Fixes golang#22395 (at least until Apple tightens bitcode requirements.) Change-Id: Ic1c1448c4d46222bb3dd097b1f4df80848051e5f Reviewed-on: https://go-review.googlesource.com/c/go/+/168320 Reviewed-by: Cherry Zhang <[email protected]> (cherry picked from commit ba96564)
1 parent feb5211 commit 4cf3b3c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ func (ctxt *Link) domacho() {
395395
s.Type = sym.SMACHOINDIRECTGOT
396396
s.Attr |= sym.AttrReachable
397397
}
398+
399+
// Add a dummy symbol that will become the __asm marker section.
400+
if ctxt.LinkMode == LinkExternal {
401+
s := ctxt.Syms.Lookup(".llvmasm", 0)
402+
s.Type = sym.SMACHO
403+
s.Attr |= sym.AttrReachable
404+
s.AddUint8(0)
405+
}
398406
}
399407

400408
func machoadddynlib(lib string, linkmode LinkMode) {
@@ -481,6 +489,17 @@ func machoshbits(ctxt *Link, mseg *MachoSeg, sect *sym.Section, segname string)
481489
msect.flag = S_MOD_INIT_FUNC_POINTERS
482490
}
483491

492+
// Some platforms such as watchOS and tvOS require binaries with
493+
// bitcode enabled. The Go toolchain can't output bitcode, so use
494+
// a marker section in the __LLVM segment, "__asm", to tell the Apple
495+
// toolchain that the Go text came from assembler and thus has no
496+
// bitcode. This is not true, but Kotlin/Native, Rust and Flutter
497+
// are also using this trick.
498+
if sect.Name == ".llvmasm" {
499+
msect.name = "__asm"
500+
msect.segname = "__LLVM"
501+
}
502+
484503
if segname == "__DWARF" {
485504
msect.flag |= S_ATTR_DEBUG
486505
}

0 commit comments

Comments
 (0)