// Copyright (c) 2026 Petr BalvĂ­n (https://petrbalvin.org) // SPDX-License-Identifier: BSD-3-Clause package asm import ( "bytes" "fmt" "os" "os/exec" "path/filepath" "sync" ) // GOObjectAARCH64 emits a GOOBJ object file for AArch64. The layout is // the shared one in goobj.go, the toolchain preamble, the go120ld header // with its block offsets, the string table, the symbol definitions and the // reloc/aux/data index arrays, with the arm64 preamble, the MinLC of 4 // for the pc-value deltas, and the arm64 relocation types for the ADRP // pairs and BL calls. func (img *Image) GOObjectAARCH64(pkgPath, srcPath string) ([]byte, error) { pre, err := toolchainObjectPreambleAARCH64() if err != nil { return nil, err } return img.emitGOObject(pkgPath, srcPath, pre, 4, func(r Reloc) (uint16, uint8) { switch r.Kind { case RelArm64Branch: return relocArm64Branch, 4 case RelArm64LDST64: return relocArm64LDST64, 4 default: return relocArm64Addr, 4 } }) } // arm64 relocation types (cmd/internal/objabi). const ( relocArm64Addr = 3 // R_ADDRARM64, ADRP+ADD pair relocArm64Branch = 9 // R_CALLARM64, BL instruction relocArm64LDST64 = 40 // R_ARM64_PCREL_LDST64, ADRP+LDR/STR pair ) // toolchainObjectPreambleAARCH64 returns the "go object ...\n!\n" header // the installed go tool asm writes for arm64, captured by assembling a // one-instruction probe. var ( preambleAARCH64Once sync.Once preambleAARCH64 []byte preambleAARCH64Err error ) func toolchainObjectPreambleAARCH64() ([]byte, error) { preambleAARCH64Once.Do(func() { goBin, err := exec.LookPath("go") if err != nil { preambleAARCH64Err = fmt.Errorf("GOOBJ emission needs the Go toolchain: %w", err) return } dir, err := os.MkdirTemp("", "gasm-preamble-arm64") if err != nil { preambleAARCH64Err = err return } defer os.RemoveAll(dir) src := filepath.Join(dir, "probe_arm64.s") if err := os.WriteFile(src, []byte("TEXT \u00b7x(SB), $0-0\n\tRET\n"), 0o644); err != nil { preambleAARCH64Err = err return } obj := filepath.Join(dir, "probe.o") cmd := exec.Command(goBin, "tool", "asm", "-p", "probe", "-o", obj, src) cmd.Env = append(os.Environ(), "GOARCH=arm64") if out, err := cmd.CombinedOutput(); err != nil { preambleAARCH64Err = fmt.Errorf("probing the assembler for the object header: %v\n%s", err, out) return } data, err := os.ReadFile(obj) if err != nil { preambleAARCH64Err = err return } i := bytes.Index(data, []byte("\n!\n")) if i < 0 || !bytes.HasPrefix(data[i+3:], []byte(goobjMagic)) { preambleAARCH64Err = fmt.Errorf("unrecognised assembler object layout") return } preambleAARCH64 = data[:i+3] }) return preambleAARCH64, preambleAARCH64Err }