Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bin: Support macOS universal binaries #120

Open
nscuro opened this issue Feb 5, 2022 · 0 comments
Open

bin: Support macOS universal binaries #120

nscuro opened this issue Feb 5, 2022 · 0 comments
Labels
enhancement New feature or request go:1.18
Milestone

Comments

@nscuro
Copy link
Member

nscuro commented Feb 5, 2022

go version -m can't currently deal with macOS universal binaries.

However, with Go 1.18, we will get the necessary tools to implement support for them ourselves, using buildinfo.Read(io.ReaderAt).
Also, Go has had support for reading fat mach-o binaries since 1.3 using macho.OpenFat.

I tinkered a bit, and it's now almost trivial to get go version -m results for all embedded binaries:

import (
	"debug/buildinfo"
	"debug/macho"
	"io"
	"log"
	"os"
)

func LoadBuildInfo118(binaryPath string) error {
	ff, err := macho.OpenFat(binaryPath)
	if err != nil {
		return err
	}
	ff.Close()

	binaryFile, err := os.Open(binaryPath)
	if err != nil {
		return err
	}
	defer binaryFile.Close()

	for i, arch := range ff.Arches {
		header := ff.Arches[i].FatArchHeader

		bi, err := buildinfo.Read(io.NewSectionReader(binaryFile, int64(header.Offset), int64(header.Size)))
		if err != nil {
			return err
		}

		log.Printf("%s: %s@%s (%s)", arch.Cpu, bi.Main.Path, bi.Main.Version, bi.Main.Sum)
	}

	return nil
}

Example output for the universal binary of goreleaser:

2022/02/05 12:34:35 CpuAmd64: github.com/goreleaser/[email protected] (h1:gW8sdjDEo2H2ZgcJmWsNZUcaJSD4MLvA/bw7+GYQ8kU=)
2022/02/05 12:34:35 CpuArm64: github.com/goreleaser/[email protected] (h1:gW8sdjDEo2H2ZgcJmWsNZUcaJSD4MLvA/bw7+GYQ8kU=)

Still torn on what the correct output would be though. Two SBOMs? A merged SBOM?

@nscuro nscuro added enhancement New feature or request go:1.18 labels Feb 5, 2022
@nscuro nscuro added this to the v1.3.0 milestone Feb 11, 2022
@nscuro nscuro modified the milestones: v1.3.0, v1.4.0 Aug 10, 2022
@nscuro nscuro modified the milestones: v1.4.0, v1.5.0 Apr 13, 2023
@nscuro nscuro modified the milestones: v1.6.0, v1.7.0 Jan 30, 2024
@nscuro nscuro modified the milestones: v1.7.0, v2.0.0 Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request go:1.18
Projects
None yet
Development

No branches or pull requests

1 participant