Skip to content

gdb cannot access libcc1.so plugin #342904

@PowerUser64

Description

@PowerUser64

Description

When using the compile code feature, GDB produces the following error:

Could not load libcc1.so: libcc1.so: cannot open shared object file: No such file or directory

Steps To Reproduce

Steps to reproduce the behavior:

  1. Make a simple program:
#include <stdio.h>

int main() {
   printf("Hello, world!\n");
   return 0;
}
  1. Compile it gcc -O0 -g main.c
  2. Open the compiled result in GDB gdb a.out
  3. Place a breakpoint in the program b main
  4. Run the program r
  5. Try compiling and running some code from within GDB compile code printf("Hello from GDB!\n")
  6. GDB produces the error listed above

Expected behavior

GDB should compile and execute the code written in the compile code command. You can test this on your system through distrobox if you want.

Screenshots

image

Additional context

First, some background on libcc1.

  • libcc1 is a plugin for gdb that lets it compile C and C++ code through its compile code command.
  • libcc1 is part of gcc, and gdb is supposed to dlopen() it when compile code is called.
  • gcc in nixpkgs has an option called disableGdbPlugin that causes libcc1 to not be compiled, although I think it's false by default.

Now that we're on the same page, let's continue.

gdb dlopen()s libcc1.so (defined here for C and here for c++), which it can't find under NixOS. I tried patching gdb with overrideAttrs (patch is below) to make it open libcc1.so from the nix store, including patching gcc to tell it to include the lib output in the outputsToInstall , which is the output that contains libcc1.so. This seems to get closer to working result, but produces the error Could not find a compiler matching "^(x86_64|i.86)(-[^-]*)?-linux(-gnu[^-]*)?-gcc$", which I found is produced by libcc1 by searching binary files in my nix store with ripgrep for Could not find a compiler matching.

Interestingly, I have noticed that there seem to be a few other places that I find libcc1.so in my nix store:

/nix/store/(sha256)-x86_64-unknown-linux-musl-gcc-13.3.0/lib/gcc/x86_64-unknown-linux-musl/13.3.0/plugin/libcc1plugin.so
/nix/store/(sha256)-gcc-13.2.0/lib/gcc/x86_64-unknown-linux-gnu/13.2.0/plugin/libcc1plugin.so

These are in addition to the path that I'm using in my patch below at /nix/store/(sha256)-gcc-13.2.0-lib/lib/libcc1.so (aka ${pkgs.gcc-unwrapped.lib}/lib/libcc1.so). You'll notice that these places call the file libcc1plugin.so rather than libcc1.so, so I'm not convinced it is the same file or a valid replacement, but I wasn't able to test this because something in the build process replaces the -linux- part of the path with just -l- whenever I set gdb's GCC_C_FE_LIBCC and GCC_CP_FE_LIBCC variables to point to that location. I'm not sure how to disable this behavior or what is causing it, so I don't know how to test this. (Although I presume it's nix.)

Are there any ideas as to what we can do to help it locate it? I would really like to get this functionality working. As I mentioned earlier, it works on other distros, including Arch Linux through distrobox.

            (gdb.overrideAttrs (new: {
              buildInputs = pkgs.gdb.buildInputs ++ [ gcc-unwrapped ];
              # nativeBuildInputs = pkgs.gdb.nativeBuildInputs ++ [ gcc libcxx ];
              env.NIX_LDFLAGS = lib.optionalString stdenv.hostPlatform.isLinux "-rpath ${lib.makeLibraryPath [ gcc-unwrapped ]}";
              dontPatchELF = true; # needed or nix will try to optimize the binary by removing "useless" rpath
              patches = [
                (pkgs.substituteAll {
                  gcc = "${((pkgs.gcc-unwrapped.override {
                      disableGdbPlugin = false;
                    }).overrideAttrs (oldAttrs: {
                    meta.outputsToInstall = oldAttrs.meta.outputsToInstall ++ [ "lib" ];
                  })).lib}";
                  # gcc = pkgs.gcc;
                  src =
                    (writeText "libcc-fix.patch" ''
                      diff --git a/include/gcc-c-interface.h b/include/gcc-c-interface.h
                      index 700d7483a4a..b5c800d3a44 100644
                      --- a/include/gcc-c-interface.h
                      +++ b/include/gcc-c-interface.h
                      @@ -191,7 +191,7 @@ struct gcc_c_context
                       /* The name of the .so that the compiler builds.  We dlopen this
                          later.  */

                      -#define GCC_C_FE_LIBCC libcc1.so
                      +#define GCC_C_FE_LIBCC @gcc@/lib/libcc1.so


                       /* The compiler exports a single initialization function.  This macro
                          holds its name as a symbol.  */
                      diff --git a/include/gcc-cp-interface.h b/include/gcc-cp-interface.h
                      index 15b911cb216..cf69c4f3d4f 100644
                      --- a/include/gcc-cp-interface.h
                      +++ b/include/gcc-cp-interface.h
                      @@ -473,7 +473,7 @@ struct gcc_cp_context
                       /* The name of the .so that the compiler builds.  We dlopen this
                          later.  */

                      -#define GCC_CP_FE_LIBCC libcc1.so
                      +#define GCC_CP_FE_LIBCC @gcc@/lib/libcc1.so

                       /* The compiler exports a single initialization function.  This macro
                          holds its name as a symbol.  */
                    '');
                })
              ];
            }))

Notify maintainers

Pinging gcc and gdb maintainers since I'm not sure whether this should be fixed in GCC or GDB (could be both).

GDB:

GCC:

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.10.9, NixOS, 24.11 (Vicuna), 24.11.20240910.1355a0c`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.5`
 - channels(blake): `""`
 - channels(root): `"nixos"`
 - nixpkgs: `/nix/store/qpg5mwsind2hy35b9vpk6mx4jimnypw0-source`

Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bugSomething is broken2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions