Search code examples
gccgdbruntimemsys2

gdb, how to step into c runtime? Where is crt_c.c?


When I'm stepping into debugged program, it says that it can't find crt/crt_c.c file. I have sources of gcc 6.3.0 downloaded, but where is crt_c.c in there?

Also how can I find source code for printf and rand in there? I'd like to step through them in debugger.

Ide is codeblocks, if that's important.

Edit: I'm trying to do so because I'm trying to decrease size of my executable. Going straight into freestanding leaves me with a lot of missing functions, so I intend to study and replace them one by one. I'm trying to do that to make my program a little smaller and faster, and to be able to study assembly output a bit easier.

Also, forgot to mention, I'm on windows, msys2. But answer is still helpful.


Solution

  • How can I find source code for printf and rand in there?

    They (printf, rand, etc....) are part of your C standard library which (on Linux) is outside of the GCC compiler. But crt0 is provided by GCC (however, is often not compiled with debug information) and some C files there are generated in the build tree during compilation of GCC.

    (on Windows, most of the C standard library is proprietary -inside some DLL provided by MicroSoft- and you are probably forbidden to look into the implementation or to reverse-engineer it; AFAIK EU laws might mention some exception related to interoperability¸ but then you need to consult a lawyer and I am not a lawyer)

    Look into GNU glibc (or perhaps musl-libc) if you want to study its source code. libc is generally using system calls (listed in syscalls(2)) provided by the Linux kernel.

    I'd like to step through them in debugger.

    In practice you won't be able to do that easily, because the libc is provided by your distribution and has generally been compiled without debug information in DWARF format.

    Some Linux distributions provide a debuggable variant of libc, perhaps as some libc6-dbg package.

    (your question lacks motivation and smells like some XY problem)

    I intend to study and replace them one by one.

    This is very unrealistic (particularly on Windows, whose system call interface is not well documented) and could take you many years (or perhaps more than a lifetime). Do you have that much time?

    Read also Operating Systems: Three Easy Pieces and look into OsDev wiki.

    I'm trying to do so because I'm trying to decrease size of my executable.

    Wrong approach. A debugger needs debug info (e.g. in DWARF) which will increase the size of the executable (but could later be stripped). BTW standard C functions are in some common shared library (or DLL on Windows) which is used by many processes.

    I'm on windows, msys2.

    Bad choice. Windows is proprietary. Linux is made of free software (more than ten billions lines of source code, if you consider all useful packages inside a typical Linux distribution), whose source code you could study (even if it would take several lifetimes).