{"record":{"id":"ab538852539fc5eb","repo":"golang/go","slug":"failed-to-open-shared-library-v","errorCode":null,"errorMessage":"failed to open shared library: %v","messagePattern":"failed to open shared library: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/work/action.go","lineNumber":404,"sourceCode":"//\n// NewObjdir must be called only from a single goroutine at a time,\n// so it is safe to call during action graph construction, but it must not\n// be called during action graph execution.\nfunc (b *Builder) NewObjdir() string {\n\tb.objdirSeq++\n\treturn str.WithFilePathSeparator(filepath.Join(b.WorkDir, fmt.Sprintf(\"b%03d\", b.objdirSeq)))\n}\n\n// readpkglist returns the list of packages that were built into the shared library\n// at shlibpath. For the native toolchain this list is stored, newline separated, in\n// an ELF note with name \"Go\\x00\\x00\" and type 1. For GCCGO it is extracted from the\n// .go_export section.\nfunc readpkglist(s *modload.Loader, shlibpath string) (pkgs []*load.Package) {\n\tvar stk load.ImportStack\n\tif cfg.BuildToolchainName == \"gccgo\" {\n\t\tf, err := elf.Open(shlibpath)\n\t\tif err != nil {\n\t\t\tbase.Fatal(fmt.Errorf(\"failed to open shared library: %v\", err))\n\t\t}\n\t\tdefer f.Close()\n\t\tsect := f.Section(\".go_export\")\n\t\tif sect == nil {\n\t\t\tbase.Fatal(fmt.Errorf(\"%s: missing .go_export section\", shlibpath))\n\t\t}\n\t\tdata, err := sect.Data()\n\t\tif err != nil {\n\t\t\tbase.Fatal(fmt.Errorf(\"%s: failed to read .go_export section: %v\", shlibpath, err))\n\t\t}\n\t\tpkgpath := []byte(\"pkgpath \")\n\t\tfor _, line := range bytes.Split(data, []byte{'\\n'}) {\n\t\t\tif path, found := bytes.CutPrefix(line, pkgpath); found {\n\t\t\t\tpath = bytes.TrimSuffix(path, []byte{';'})\n\t\t\t\tpkgs = append(pkgs, load.LoadPackageWithFlags(s, string(path), base.Cwd(), &stk, nil, 0))\n\t\t\t}\n\t\t}\n\t} else {","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/work/action.go#L386-L422","documentation":"readpkglist calls elf.Open on a gccgo-built shared library path to extract its embedded package list. If elf.Open returns an error (file missing, not ELF, truncated), the go command calls base.Fatal with this wrapped error, terminating the build.","triggerScenarios":"Building with -buildmode=shared under gccgo, or consuming a prior -shared gccgo artifact, where the recorded shlib path is unreadable, deleted, or not an ELF file. Also triggered by a stale build cache pointing at a removed .so.","commonSituations":"Build cache corruption; the .so was garbage-collected (GOCACHE cleanup) but its cache entry remained; cross-mounted filesystem where the .so isn't visible; a non-gccgo .so being interpreted as one.","solutions":["Run 'go clean -cache' to discard stale shared-library references.","Rebuild the shared library with -buildmode=shared so its path is fresh.","Verify the shlibpath exists and is readable: 'file <path>' should report ELF.","Ensure GOFLAGS/-pkgdir isn't pointing at a stale output tree."],"exampleFix":"// before: stale cache references a deleted .so\n$ go build -buildmode=shared -linkshared ./...\n// error: failed to open shared library: open /tmp/.../libstd.so: no such file\n\n// after\n$ go clean -cache\n$ go build -buildmode=shared -o libstd.so std\n$ go build -linkshared ./...","handlingStrategy":"try-catch","validationCode":"// Verify a gccgo shared library is a readable ELF before referencing it.\nfunc checkShlib(path string) error {\n    f, err := elf.Open(path)\n    if err != nil { return fmt.Errorf(\"%s: not a readable ELF\", path) }\n    defer f.Close()\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Treat a missing/corrupt shlib as a cache reset, not a fatal error.\nif _, err := readpkglist(loader, path); err != nil {\n    log.Printf(\"shlib unreadable, resetting cache: %v\", err)\n    _ = os.RemoveAll(cfg.GOCACHE)\n    // rebuild shared libs and retry\n}","preventionTips":["Run 'go clean -cache' when switching between -buildmode=shared runs.","Keep GOCACHE on a stable local filesystem.","Don't manually delete .so files out from under the build cache."],"tags":["gccgo","shared-library","build-cache","elf"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}