{"record":{"id":"ac2d78e485d3cf52","repo":"golang/go","slug":"malformed-object-file","errorCode":null,"errorMessage":"malformed object file","messagePattern":"malformed object file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/buildid/buildid.go","lineNumber":20,"sourceCode":"// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage buildid\n\nimport (\n\t\"bytes\"\n\t\"debug/elf\"\n\t\"fmt\"\n\t\"internal/xcoff\"\n\t\"io\"\n\t\"io/fs\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n)\n\nvar (\n\terrBuildIDMalformed = fmt.Errorf(\"malformed object file\")\n\n\tbangArch = []byte(\"!<arch>\")\n\tpkgdef   = []byte(\"__.PKGDEF\")\n\tgoobject = []byte(\"go object \")\n\tbuildid  = []byte(\"build id \")\n)\n\n// ReadFile reads the build ID from an archive or executable file.\nfunc ReadFile(name string) (id string, err error) {\n\tf, err := os.Open(name)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tdefer f.Close()\n\n\tbuf := make([]byte, 8)\n\tif _, err := f.ReadAt(buf, 0); err != nil {\n\t\treturn \"\", err","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/buildid/buildid.go#L2-L38","documentation":"Defined as errBuildIDMalformed in buildid.go and returned when ReadFile cannot find a recognizable Go object, archive, or build-id structure in a file. The package first tries the `!<arch>` archive header, `__.PKGDEF`, `go object `, and `build id ` markers; if none line up with the expected layout, the file is treated as malformed.","triggerScenarios":"Passing a non-Go object file (random binary, corrupted object, truncated build artifact, foreign-toolchain output) to a buildid function such as ReadFile, ReadFile or the rewrite helpers. Also when a cached object in the build cache is partially written or truncated.","commonSituations":"Corrupted build cache entries (e.g. after a crash, disk full, or `kill -9` during compile), mixing objects from incompatible Go versions, hand-editing .a files, or pointing the toolchain at a non-Go archive.","solutions":["Clean the build cache: `go clean -cache`.","Rebuild the affected package from source: `go build -a <pkg>`.","Verify the file is actually a Go-produced object (e.g. `file <obj>` and inspect for the `go object` magic).","If reproducible, check for disk space issues, concurrent writers, or failing hardware during the build."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before handing an object to buildid.ReadFile, sanity-check the magic:\nf, _ := os.Open(obj)\nhdr := make([]byte, 8)\n_, _ = io.ReadFull(f, hdr)\n_ = f.Close()\nif !bytes.HasPrefix(hdr, []byte(\"!<arch>\")) && !bytes.HasPrefix(hdr, []byte(\"\\x7fELF\")) {\n    return errors.New(\"not a recognizable Go object/archive\")\n}","typeGuard":null,"tryCatchPattern":"id, err := buildid.ReadFile(obj)\nif err != nil {\n    if errors.Is(err, buildid.ErrBuildIDMalformed) || strings.Contains(err.Error(), \"malformed object file\") {\n        // rebuild from source\n        _ = os.Remove(obj)\n    }\n    return err\n}","preventionTips":["Treat build cache as disposable: `go clean -cache` on any unexplained buildid error.","Don't mix object files across Go versions.","Avoid kill -9 during compile/link; let steps finish."],"tags":["buildid","object-file","toolchain","corruption"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}