{"id":"9e0e78db241d140f","repo":"rust-lang/rust","slug":"could-not-load-libgccjit-so-attempted-paths","errorCode":null,"errorMessage":"Could not load libgccjit.so. Attempted paths: {:#?}","messagePattern":"Could not load libgccjit\\.so\\. Attempted paths: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_codegen_gcc/src/lib.rs","lineNumber":228,"sourceCode":"        // We use all_paths() instead of only path() in case the path specified by --sysroot is\n        // invalid.\n        // This is the case for instance in Rust for Linux where they specify --sysroot=/dev/null.\n        for path in sess.opts.sysroot.all_paths() {\n            let libgccjit_target_lib_file = file_path(path, sess);\n            if let Ok(true) = fs::exists(&libgccjit_target_lib_file) {\n                load_libgccjit_if_needed(&libgccjit_target_lib_file);\n                break;\n            }\n        }\n\n        if !gccjit::is_loaded() {\n            let mut paths = vec![];\n            for path in sess.opts.sysroot.all_paths() {\n                let libgccjit_target_lib_file = file_path(path, sess);\n                paths.push(libgccjit_target_lib_file);\n            }\n\n            panic!(\"Could not load libgccjit.so. Attempted paths: {:#?}\", paths);\n        }\n\n        #[cfg(feature = \"master\")]\n        {\n            gccjit::set_lang_name(c\"GNU Rust\");\n\n            let target_cpu = target_cpu(sess);\n\n            // Get the second TargetInfo with the correct CPU features by setting the arch.\n            let context = Context::default();\n            if target_cpu != \"generic\" {\n                context.add_command_line_option(format!(\"-march={}\", target_cpu));\n            }\n\n            *self.target_info.info.lock().expect(\"lock\") =\n                IntoDynSyncSend(Some(context.get_target_info()));\n        }\n","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_gcc/src/lib.rs#L210-L246","documentation":"`panic!(\"Could not load libgccjit.so. Attempted paths: {:#?}\")` in `GccCodegenBackend::init`, reached after the backend iterated every `--sysroot` path (via `sysroot.all_paths()`), checked for the existence of `rustlib/codegen-backends/lib/<target>/libgccjit.so`, and found none. The panic lists the exact paths tried so the user can see where the backend expected the library. Unlike error 156, here the file was never found at all.","triggerScenarios":"Compiling with `-Ccodegen-backend=gcc` when the libgccjit target library is absent from every sysroot, e.g. the GCC backend component was never installed, or `--sysroot` was overridden to a path that lacks the codegen-backends directory (Rust for Linux passes `--sysroot=/dev/null`).","commonSituations":"Fresh rustc_codegen_gcc setup where the backend crate is built but the runtime `libgccjit.so` was not placed under `rustlib/codegen-backends/lib/<target>/`; cross-compiling to a target with no matching triplet directory; rust-toolchain override pointing at a minimal sysroot.","solutions":["Install/place `libgccjit.so` under `<sysroot>/lib/rustlib/<host>/codegen-backends/lib/<target>/libgccjit.so` exactly as the panic's path list shows.","Drop the `--sysroot` override (or `RUST_SYSROOT`) so the default toolchain sysroot is used.","Install the rustc_codegen_gcc toolchain / component that ships the runtime library.","Verify `rustc --print sysroot` resolves to the directory that actually contains the listed subpath."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Pre-check all candidate sysroot paths the backend would search\nuse std::path::Path;\n\nfn find_libgccjit(sysroots: &[&str], host_triple: &str, target_triple: &str) -> Result<String, String> {\n    let mut attempted: Vec<String> = Vec::new();\n    for sysroot in sysroots {\n        let p = format!(\n            \"{}/lib/rustlib/{}/codegen-backends/lib/{}/libgccjit.so\",\n            sysroot, host_triple, target_triple\n        );\n        attempted.push(p.clone());\n        if Path::new(&p).exists() {\n            return Ok(p);\n        }\n    }\n    // Check GCC_BACKEND_PATH override\n    if let Ok(override_path) = std::env::var(\"GCC_BACKEND_PATH\") {\n        if Path::new(&override_path).exists() {\n            return Ok(override_path);\n        }\n        attempted.push(override_path);\n    }\n    // Check ldconfig\n    let ldc = std::process::Command::new(\"sh\")\n        .args([\"-c\", \"ldconfig -p 2>/dev/null | grep libgccjit | head -1\"])\n        .output()\n        .ok()\n        .and_then(|o| String::from_utf8(o.stdout).ok())\n        .unwrap_or_default();\n    if !ldc.trim().is_empty() {\n        return Ok(ldc.trim().to_string());\n    }\n    Err(format!(\"Could not find libgccjit.so. Attempted paths: {:#?}\", attempted))\n}\n\n// Usage in build.rs:\n// let sysroot = std::process::Command::new(\"rustc\").arg(\"--print\").arg(\"sysroot\").output().unwrap();\n// let sr = String::from_utf8_lossy(&sysroot.stdout).trim().to_string();\n// find_libgccjit(&[&sr], std::env::consts::ARCH, std::env::consts::ARCH)?;","typeGuard":null,"tryCatchPattern":"// Catch exhaustive-load failure and provide actionable diagnostics\nuse std::process::Command;\n\nfn build_with_gcc_backend(crate_path: &str) -> Result<(), String> {\n    let out = Command::new(\"rustc\")\n        .args([\"-Z\", \"codegen-backend=gcc\"])\n        .arg(crate_path)\n        .output()\n        .map_err(|e| e.to_string())?;\n    if !out.status.success() {\n        let stderr = String::from_utf8_lossy(&out.stderr);\n        if stderr.contains(\"Could not load libgccjit\") {\n            // Provide remediation\n            return Err(format!(\n                \"libgccjit.so not found in any sysroot. Fix:\\n\\\n                 1. Install libgccjit-dev\\n\\\n                 2. Or set GCC_BACKEND_PATH=/path/to/libgccjit.so\\n\\\n                 3. Or use the LLVM backend instead\\n\\\n                 Original error: {}\",\n                stderr.lines().find(|l| l.contains(\"Could not load\")).unwrap_or(\"unknown\")\n            ));\n        }\n        return Err(stderr.to_string());\n    }\n    Ok(())\n}","preventionTips":["Run: rustc --print sysroot to find where rustc expects libgccjit.so, then verify the full path exists","If --sysroot is overridden (e.g. Rust-for-Linux uses --sysroot=/dev/null), set GCC_BACKEND_PATH to an absolute path","Symlink the versioned file to the unversioned name: ln -s libgccjit.so.0 libgccjit.so","Add the libgccjit directory to /etc/ld.so.conf.d/ and run ldconfig so the dynamic linker can resolve it","In Dockerfiles, install libgccjit in the same RUN layer as the Rust toolchain to avoid layer ordering issues"],"tags":["rustc-codegen-gcc","libgccjit","sysroot","toolchain","codegen-backend","panic"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}