{"record":{"id":"fcd72e64f2626acc","repo":"golang/go","slug":"wrong-magic-not-a-go-object-file","errorCode":null,"errorMessage":"wrong magic, not a Go object file","messagePattern":"wrong magic, not a Go object file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/goobj/objfile.go","lineNumber":233,"sourceCode":"\tOffsets     [NBlk]uint32\n}\n\nconst Magic = \"\\x00go120ld\"\n\nfunc (h *Header) Write(w *Writer) {\n\tw.RawString(h.Magic)\n\tw.Bytes(h.Fingerprint[:])\n\tw.Uint32(h.Flags)\n\tfor _, x := range h.Offsets {\n\t\tw.Uint32(x)\n\t}\n}\n\nfunc (h *Header) Read(r *Reader) error {\n\tb := r.BytesAt(0, len(Magic))\n\th.Magic = string(b)\n\tif h.Magic != Magic {\n\t\treturn errors.New(\"wrong magic, not a Go object file\")\n\t}\n\toff := uint32(len(h.Magic))\n\tcopy(h.Fingerprint[:], r.BytesAt(off, len(h.Fingerprint)))\n\toff += 8\n\th.Flags = r.uint32At(off)\n\toff += 4\n\tfor i := range h.Offsets {\n\t\th.Offsets[i] = r.uint32At(off)\n\t\toff += 4\n\t}\n\treturn nil\n}\n\nfunc (h *Header) Size() int {\n\treturn len(h.Magic) + len(h.Fingerprint) + 4 + 4*len(h.Offsets)\n}\n\n// Autolib","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/goobj/objfile.go#L215-L251","documentation":"Thrown by (*goobj.Header).Read when the first bytes of a file do not equal the gc object-file magic constant (\"\\x00go120ld\"). The goobj package reads the export data / object index of files emitted by the gc compiler's object writer, and the magic both identifies the format and pins the on-disk version. Any byte mismatch (truncation, wrong format, version skew) aborts the parse before any block offsets are read.","triggerScenarios":"Calling goobj.Parse or (*Reader) on a path whose content is not a gc '.o' object: a source file, an archive unpacked wrong, a PE/ELF executable, or an object produced by a different/newer Go whose Magic constant changed. Also fires on a zero-length or truncated file where BytesAt returns fewer than len(Magic) bytes that do not match.","commonSituations":"Mixing Go toolchain versions (the magic string is bumped, e.g. go120ld, across major object-format revisions), feeding a text/source file to a loader that expects an object file, or a corrupted build cache / partial write left by a killed compiler process. Often seen in CI after a Go upgrade without rebuilding dependencies.","solutions":["Verify the file is actually a gc object file: `go tool nm FILE` or `head -c8 FILE | xxd` and check it begins with the magic byte 0x00 followed by 'go120ld'.","Rebuild the object with the toolchain version that matches the goobj reader (clean the build cache: `go clean -cache`).","Confirm the path passed to goobj.Parse is the compiled object, not the source, archive wrapper, or linked executable.","If consuming third-party objects, ensure they were produced by gc (this package does not read gccgo/GoLLVM objects)."],"exampleFix":"// before: feeding an arbitrary file to the goobj reader\nf, _ := os.Open(path)\nr := goobj.NewReader(f, ...) \nhdr.Read(r) // -> \"wrong magic, not a Go object file\"\n\n// after: sniff the magic before parsing so the caller gets a clear signal\nconst Magic = \"\\x00go120ld\"\nhead := make([]byte, len(Magic))\nif _, err := io.ReadFull(f, head); err != nil || string(head) != Magic {\n    return fmt.Errorf(\"%s: not a gc object file\", path)\n}\nf.Seek(0, io.SeekStart)\nhdr.Read(r)","handlingStrategy":"validation","validationCode":"// Validate the file is a gc object before handing it to goobj.\nfunc isGcObject(path string) bool {\n    f, err := os.Open(path)\n    if err != nil { return false }\n    defer f.Close()\n    head := make([]byte, 8) // len(goobj.Magic)\n    if _, err := io.ReadFull(f, head); err != nil { return false }\n    return string(head) == \"\\x00go120ld\"\n}","typeGuard":"// Narrow on the goobj.Read error after the fact.\nfunc isWrongMagic(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"wrong magic\")\n}","tryCatchPattern":"if err := hdr.Read(r); err != nil {\n    if strings.Contains(err.Error(), \"wrong magic\") {\n        return fmt.Errorf(\"%s is not a gc object file: %w\", path, err)\n    }\n    return err\n}","preventionTips":["Sniff the 8-byte magic before parsing to fail fast with a clear message.","Pin the Go toolchain version in CI (go.mod's `go` directive / toolchain) so object formats match.","Run `go clean -cache` after toolchain upgrades to purge incompatible cached objects."],"tags":["go-object","goobj","magic","binary-parsing","go-toolchain"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}