golang/go · error

open %s: unrecognized object file

Error message

open %s: unrecognized object file

What it means

`objfile.Open` tries to parse the input as a Go object file, then tries all registered openers (ELF, Mach-O, PE, Plan9, XCOFF). If every opener fails, the file is unrecognizable as any known object or executable format, and this error is returned. The file either is not a binary object at all or uses an unsupported format.

Source

Thrown at src/cmd/internal/objfile/objfile.go:87

// Open opens the named file.
// The caller must call f.Close when the file is no longer needed.
func Open(name string) (*File, error) {
	r, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	if f, err := openGoFile(r); err == nil {
		return f, nil
	} else if _, ok := err.(archive.ErrGoObjOtherVersion); ok {
		return nil, fmt.Errorf("open %s: %v", name, err)
	}
	for _, try := range openers {
		if raw, err := try(r); err == nil {
			return &File{r, []*Entry{{raw: raw}}}, nil
		}
	}
	r.Close()
	return nil, fmt.Errorf("open %s: unrecognized object file", name)
}

func (f *File) Close() error {
	return f.r.Close()
}

func (f *File) Entries() []*Entry {
	return f.entries
}

func (f *File) Symbols() ([]Sym, error) {
	return f.entries[0].Symbols()
}

func (f *File) PCLineTable() (Liner, error) {
	return f.entries[0].PCLineTable()
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Verify the file is actually a binary: `file <path>` to see its format.
  2. Ensure the binary format is one of ELF, Mach-O, PE, Plan9, or XCOFF — or a Go `.o`/`.a` file.
  3. If the file is compressed or in a container (e.g., .tar.gz, .zip), extract it first.
  4. For WebAssembly, use `go tool wasm` or the appropriate WASM reader, not `objfile.Open`.

Example fix

// before — wrong file type
f, err := objfile.Open("main.go")  // fails: not a binary

// after — use the compiled binary
$ go build -o main main.go
f, err := objfile.Open("main")  // succeeds
Defensive patterns

Strategy: validation

Validate before calling

// Validate file is a recognized binary format
func isRecognizedBinary(path string) bool {
    f, err := os.Open(path)
    if err != nil { return false }
    defer f.Close()
    magic := make([]byte, 16)
    n, _ := f.Read(magic)
    magic = magic[:n]
    // ELF
    if bytes.HasPrefix(magic, []byte{0x7f, 'E', 'L', 'F'}) { return true }
    // Mach-O 64
    if bytes.HasPrefix(magic, []byte{0xfe, 0xed, 0xfa, 0xcf}) { return true }
    // Mach-O 32
    if bytes.HasPrefix(magic, []byte{0xce, 0xfa, 0xed, 0xfe}) { return true }
    // PE
    if bytes.HasPrefix(magic, []byte{'M', 'Z'}) { return true }
    // Plan9
    if len(magic) >= 4 && (magic[0] == 0x00 || magic[0] == 0x80) { return true }
    // Go object
    if bytes.HasPrefix(magic, []byte("goobj")) || bytes.HasPrefix(magic, []byte("!<arch>")) { return true }
    return false
}

Try / catch

f, err := objfile.Open(path)
if err != nil && strings.Contains(err.Error(), "unrecognized object file") {
    out, _ := exec.Command("file", path).Output()
    return fmt.Errorf("%s is not a recognized binary format: %s", path, out)
}

Prevention

When it happens

Trigger: Calling `objfile.Open` on a file that is not a Go object, archive, ELF, Mach-O, PE, Plan9, or XCOFF binary. For example, a text file, a script, a WebAssembly binary, a Java `.class` file, or a binary in an unsupported format.

Common situations: Passing the wrong file path to a tool that expects a binary (e.g., passing a source file to `go tool objdump`). Pointing at a compressed or archive-wrapped binary without extracting first. Analyzing a binary format that the Go `objfile` package does not support (e.g., WASM, which has its own separate reader). Corrupted or truncated binaries.

Related errors


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