golang/go · error

corrupt object file

Error message

corrupt object file

What it means

Defined as errCorruptObject in cmd/internal/archive. Returned while parsing an individual object file inside an archive (or a standalone object) when the Go object file format is recognized as a Go object (via the goobj magic) but its internal structure — symbol table, relocation records, section headers — fails to parse. The container was fine; the contents are malformed.

Source

Thrown at src/cmd/internal/archive/archive.go:106

	Arch       string
	Data
}

const (
	entryHeader = "%s%-12d%-6d%-6d%-8o%-10d`\n"
	// In entryHeader the first entry, the name, is always printed as 16 bytes right-padded.
	entryLen   = 16 + 12 + 6 + 6 + 8 + 10 + 1 + 1
	timeFormat = "Jan _2 15:04 2006"
)

var (
	archiveHeader = []byte("!<arch>\n")
	archiveMagic  = []byte("`\n")
	goobjHeader   = []byte("go objec") // truncated to size of archiveHeader

	errCorruptArchive   = errors.New("corrupt archive")
	errTruncatedArchive = errors.New("truncated archive")
	errCorruptObject    = errors.New("corrupt object file")
	errNotObject        = errors.New("unrecognized object file format")
)

type ErrGoObjOtherVersion struct{ magic []byte }

func (e ErrGoObjOtherVersion) Error() string {
	return fmt.Sprintf("go object of a different version: %q", e.magic)
}

// An objReader is an object file reader.
type objReader struct {
	a      *Archive
	b      *bio.Reader
	err    error
	offset int64
	limit  int64
	tmp    [256]byte
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run `go clean -cache` and rebuild — the most common cause is a stale cross-version cache entry.
  2. Make sure every contributor and CI step uses the same Go version (pin via go.mod's `go` directive and `toolchain` line).
  3. If linking prebuilt objects, rebuild them with the current toolchain.
  4. Check for filesystem corruption with `fsck` or by reading the file twice and comparing checksums.

Example fix

# before
$ go1.20 build ./...
$ go1.23 build ./...
# -> corrupt object file

# after
$ go clean -cache
$ go1.23 build ./...
Defensive patterns

Strategy: retry

Validate before calling

// Reject cross-version cache reuse up front.
func cacheVersionMatches(goroot, cacheTag string) error {
    v := runtime.Version()
    if !strings.HasPrefix(cacheTag, v) {
        return fmt.Errorf("cache tagged %q but toolchain is %q — run go clean -cache", cacheTag, v)
    }
    return nil
}

Try / catch

if err != nil && strings.Contains(err.Error(), "corrupt object file") {
    // most common fix: clear cache and rebuild with the current toolchain
    exec.Command("go", "clean", "-cache").Run()
    // retry build
}

Prevention

When it happens

Trigger: An object file generated by one Go version being consumed by a different, incompatible Go version's linker (cache poisoning across versions). A partially-overwritten object in the build cache. A hand-edited or byte-corrupted .o file. Mixing GOEXPERIMENT or GOARCH incompatibly within one build.

Common situations: Switching Go versions without clearing the build cache. Concurrent builds with different GOFLAGS/GOARCH writing the same cache slot (rare with modern cache keys but possible with custom -gcflags). Disk corruption. Copying object files between machines with different endianness.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/37772212030271fe. Report an issue: GitHub.