{"record":{"id":"66b8ab2645170a27","repo":"golang/go","slug":"dynamic-relocation-to-unreachable-symbol-s","errorCode":null,"errorMessage":"dynamic relocation to unreachable symbol %s","messagePattern":"dynamic relocation to unreachable symbol (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/link/internal/ld/data.go","lineNumber":849,"sourceCode":"// Note that for a given symbol (as above) it is perfectly legal to\n// have both direct and indirect references.\nfunc windynrelocsym(ctxt *Link, rel *loader.SymbolBuilder, s loader.Sym) error {\n\tvar su *loader.SymbolBuilder\n\trelocs := ctxt.loader.Relocs(s)\n\tfor ri := 0; ri < relocs.Count(); ri++ {\n\t\tr := relocs.At(ri)\n\t\tif r.IsMarker() {\n\t\t\tcontinue // skip marker relocations\n\t\t}\n\t\ttarg := r.Sym()\n\t\tif targ == 0 {\n\t\t\tcontinue\n\t\t}\n\t\tif !ctxt.loader.AttrReachable(targ) {\n\t\t\tif r.Weak() {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn fmt.Errorf(\"dynamic relocation to unreachable symbol %s\",\n\t\t\t\tctxt.loader.SymName(targ))\n\t\t}\n\t\ttgot := ctxt.loader.SymGot(targ)\n\t\tif tgot == loadpe.RedirectToDynImportGotToken {\n\n\t\t\t// Consistency check: name should be __imp_X\n\t\t\tsname := ctxt.loader.SymName(targ)\n\t\t\tif !strings.HasPrefix(sname, \"__imp_\") {\n\t\t\t\treturn fmt.Errorf(\"internal error in windynrelocsym: redirect GOT token applied to non-import symbol %s\", sname)\n\t\t\t}\n\n\t\t\t// Locate underlying symbol (which originally had type\n\t\t\t// SDYNIMPORT but has since been retyped to SWINDOWS).\n\t\t\tds, err := loadpe.LookupBaseFromImport(targ, ctxt.loader, ctxt.Arch)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdstyp := ctxt.loader.SymType(ds)","sourceCodeStart":831,"sourceCodeEnd":867,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/link/internal/ld/data.go#L831-L867","documentation":"During Windows PE dynamic relocation processing in windynrelocsym, this error fires when a relocation references a symbol that the linker's reachability analysis has marked as unreachable (dead-code eliminated) and the relocation is not weak. An unreachable symbol should not have live relocations pointing to it; this condition indicates a mismatch between the reachability pass and the relocation pass.","triggerScenarios":"The function iterates relocations for a symbol, calls ctxt.loader.AttrReachable(targ) on each relocation target. If the target is unreachable and r.Weak() is false, the error is returned with the symbol name. Marker relocations and zero-target relocations are skipped before this check.","commonSituations":"Building a Windows DLL or c-shared binary where CGO dependencies reference symbols that the linker incorrectly garbage-collected; a bug in the dead-code elimination pass that marks a needed symbol as unreachable; mixed object files from different compilation stages with inconsistent reachability metadata.","solutions":["Try switching to external linking: go build -ldflags='-linkmode=external'","Clean rebuild: go clean -cache && go build","Report as a Go linker bug at https://github.com/golang/go/issues with GOOS=windows and the buildmode","If using CGO, check for symbol visibility issues in C/C++ dependencies","Try disabling dead code elimination to isolate the issue"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// In linker driver code, catch internal relocation errors\n// and fall back to external linking\nif err := windynrelocsym(ctxt, s, r, ri); err != nil {\n    if !externalLinking {\n        log.Printf(\"internal link failed (%v), retrying with -linkmode=external\", err)\n        return retryWithExternalLinker()\n    }\n    return err\n}","preventionTips":["Keep -linkmode=external as a fallback for Windows PE builds","Ensure CGO dependencies export symbols correctly (no hidden visibility issues)","Clean build cache when encountering unexpected reachability errors","Report persistent occurrences as Go linker bugs"],"tags":["relocation","windows","pe","linker","go-toolchain","dead-code"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}