{"record":{"id":"24793903da21640c","repo":"golang/go","slug":"s-failed-to-open-host-object-v","errorCode":null,"errorMessage":"%s: failed to open host object: %v\n","messagePattern":"(.+?): failed to open host object: (.+?)\n","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/link/internal/ld/macho.go","lineNumber":1313,"sourceCode":"\t}\n\tfor i := 0; i < len(Segdwarf.Sections); i++ {\n\t\tsect := Segdwarf.Sections[i]\n\t\tsi := dwarfp[i]\n\t\tif si.secSym() != sect.Sym ||\n\t\t\tctxt.loader.SymSect(si.secSym()) != sect {\n\t\t\tpanic(\"inconsistency between dwarfp and Segdwarf\")\n\t\t}\n\t\trelocSect(ctxt, sect, si.syms)\n\t}\n\twg.Wait()\n}\n\n// hostobjMachoPlatform returns the first platform load command found\n// in the host object, if any.\nfunc hostobjMachoPlatform(h *Hostobj) (*MachoPlatformLoad, error) {\n\tf, err := os.Open(h.file)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%s: failed to open host object: %v\\n\", h.file, err)\n\t}\n\tdefer f.Close()\n\tsr := io.NewSectionReader(f, h.off, h.length)\n\tm, err := macho.NewFile(sr)\n\tif err != nil {\n\t\t// Not a valid Mach-O file.\n\t\treturn nil, nil\n\t}\n\treturn peekMachoPlatform(m)\n}\n\n// peekMachoPlatform returns the first LC_VERSION_MIN_* or LC_BUILD_VERSION\n// load command found in the Mach-O file, if any.\nfunc peekMachoPlatform(m *macho.File) (*MachoPlatformLoad, error) {\n\tfor _, cmd := range m.Loads {\n\t\traw := cmd.Raw()\n\t\tml := MachoLoad{\n\t\t\ttype_: m.ByteOrder.Uint32(raw),","sourceCodeStart":1295,"sourceCodeEnd":1331,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/link/internal/ld/macho.go#L1295-L1331","documentation":"When linking against host objects (C archives/objects from cgo), the linker opens each host object file by path to inspect its Mach-O load commands (e.g. to determine the deployment target via `hostobjMachoPlatform`). If `os.Open` fails — file missing, permission denied, path too long — the link fails with this message, naming the offending file and the OS error.","triggerScenarios":"A cgo build where a recorded host-object path no longer exists (deleted, moved, or never installed); the object file is present but unreadable due to permissions; a relative path that resolves incorrectly because the build ran from a different working directory; a stale build cache referencing a removed file.","commonSituations":"Running `go clean -cache` mid-build (or `rm` on `_cgo_*` files); building on one machine and copying the cache to another without the C objects; container builds where a bind-mounted dependency was removed; cgo package whose `.a` was built outside the module cache and not redistributed.","solutions":["Rebuild with `go build -a` to regenerate all host objects.","Verify the named file exists and is readable: `ls -l <path>` and `file <path>`.","If the path is relative, run `go build` from the module root so relative cgo paths resolve.","Reinstall the C dependency or re-run `go mod download` to restore missing artifacts."],"exampleFix":"# before: stale cache references deleted host object\ngo build ./...\n\n# after\ngo clean -cache && go build -a ./...","handlingStrategy":"validation","validationCode":"# Ensure all cgo host objects exist and are readable before linking\nfor obj in $(go list -export -f '{{.ImportPath}}' ./... 2>/dev/null); do :; done\n# Or simply verify the build cache is intact:\ngo env GOCACHE && test -d \"$(go env GOCACHE)\" && echo 'cache present'","typeGuard":null,"tryCatchPattern":"set +e\ngo build ./...\nrc=$?\nset -e\nif [ $rc -ne 0 ]; then\n  echo 'link failed; rebuilding all host objects'\n  go clean -cache && go build -a ./...\nfi","preventionTips":["Do not delete `_cgo_*` files or run `go clean -cache` mid-build.","Run `go build` from the module root so relative cgo paths resolve.","When redistributing a build, include all C objects, not just Go sources.","Keep host-object paths absolute or module-relative."],"tags":["linker","go-toolchain","macos","cgo","host-object"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}