feat(asm): add RISC-V ELF relocatable object emission and SB relocation

support
This commit is contained in:
2026-08-03 08:51:00 +02:00
parent 7721353d44
commit f41a86b660
8 changed files with 824 additions and 43 deletions
+18 -3
View File
@@ -19,7 +19,20 @@ import (
// keyed by the function's short name (the part after the middle dot).
// This is the universal oracle: any file that `go tool asm` accepts can
// be verified, with no hand-written reference.
//
// For RISC-V sources the assembler is invoked with GOARCH=riscv64;
// the caller must set the architecture via GroundTruthArch.
func GroundTruth(path string) (map[string][]byte, error) {
return groundTruthArch(path, "")
}
// GroundTruthRISCV assembles the given .s file with the Go toolchain in
// RISC-V cross-assembly mode (GOARCH=riscv64).
func GroundTruthRISCV(path string) (map[string][]byte, error) {
return groundTruthArch(path, "riscv64")
}
func groundTruthArch(path, goarch string) (map[string][]byte, error) {
goroot := runtime.GOROOT()
asmBin := filepath.Join(goroot, "pkg", "tool", runtime.GOOS+"_"+runtime.GOARCH, "asm")
if _, err := os.Stat(asmBin); err != nil {
@@ -27,7 +40,6 @@ func GroundTruth(path string) (map[string][]byte, error) {
}
includeDir := filepath.Join(goroot, "pkg", "include")
// Create a temp file for the object output.
tmpDir, err := os.MkdirTemp("", "gasm-verify-*")
if err != nil {
return nil, fmt.Errorf("verify: tempdir: %w", err)
@@ -35,14 +47,17 @@ func GroundTruth(path string) (map[string][]byte, error) {
defer os.RemoveAll(tmpDir)
objPath := filepath.Join(tmpDir, "out.o")
// Derive a package name from the file name (the assembler needs -p).
base := filepath.Base(path)
pkg := strings.TrimSuffix(base, ".s")
pkg = strings.TrimSuffix(pkg, "_amd64")
pkg = strings.TrimSuffix(pkg, "_riscv64")
cmd := exec.Command(asmBin, "-I", includeDir, "-p", pkg, "-o", objPath, path)
if goarch != "" {
cmd.Env = append(os.Environ(), "GOARCH="+goarch)
}
if out, err := cmd.CombinedOutput(); err != nil {
return nil, fmt.Errorf("verify: go tool asm: %w\n%s", err, out)
return nil, fmt.Errorf("verify: go tool asm (%s): %w\n%s", goarch, err, out)
}
objData, err := os.ReadFile(objPath)