cmd/link/internal/ld: extract Mach-O load command parsing

We're going to need the ability to extract the LC_VERSION_MIN_* and
LC_BUILD_VERSION load commands. This CL adds peekMachoPlatform to do
that and in the process simplifies machoCombineDwarf.

While here, disable DWARF combining for Apple platforms other than
macOS (watchOS, tvOS, bridgeOS), not just iOS.

Updates #22395

Change-Id: I4862b0f15ccc87b7be1a6532b4d37b47c8f7f243
Reviewed-on: https://go-review.googlesource.com/c/go/+/168459
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Elias Naur 2019-03-20 20:10:38 +01:00
parent ba965640a4
commit 0fe1986a72
3 changed files with 109 additions and 58 deletions

View file

@ -44,6 +44,7 @@ import (
"cmd/link/internal/sym"
"crypto/sha1"
"debug/elf"
"debug/macho"
"encoding/base64"
"encoding/binary"
"encoding/hex"
@ -1449,11 +1450,24 @@ func (ctxt *Link) hostlink() {
}
// For os.Rename to work reliably, must be in same directory as outfile.
combinedOutput := *flagOutfile + "~"
isIOS, err := machoCombineDwarf(ctxt, *flagOutfile, dsym, combinedOutput)
exef, err := os.Open(*flagOutfile)
if err != nil {
Exitf("%s: combining dwarf failed: %v", os.Args[0], err)
}
if !isIOS {
defer exef.Close()
exem, err := macho.NewFile(exef)
if err != nil {
Exitf("%s: parsing Mach-O header failed: %v", os.Args[0], err)
}
load, err := peekMachoPlatform(exem)
if err != nil {
Exitf("%s: failed to parse Mach-O load commands: %v", os.Args[0], err)
}
// Only macOS supports unmapped segments such as our __DWARF segment.
if load == nil || load.platform == PLATFORM_MACOS {
if err := machoCombineDwarf(ctxt, exef, exem, dsym, combinedOutput); err != nil {
Exitf("%s: combining dwarf failed: %v", os.Args[0], err)
}
os.Remove(*flagOutfile)
if err := os.Rename(combinedOutput, *flagOutfile); err != nil {
Exitf("%s: %v", os.Args[0], err)