-1

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.

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • Why do you need to step into [crt0](https://en.wikipedia.org/wiki/Crt0)? Please **edit your question** to motivate it – Basile Starynkevitch Sep 14 '17 at 09:41
  • please *improve your question* to motivate it. Otherwise, it might be closed since too broad or unclear. – Basile Starynkevitch Sep 14 '17 at 09:53
  • What is the relation between `gdb` and shrinking the executable size? – Basile Starynkevitch Sep 14 '17 at 10:02
  • @BasileStarynkevitch well, I need to understand what the code does before throwing it away. Running code in debugger is the easiest way to understand it. – Gregory Balko Sep 14 '17 at 10:05
  • Can you afford spending more than a lifetime to answer your question? – Basile Starynkevitch Sep 14 '17 at 10:07
  • @BasileStarynkevitch no no no, I'm not going deeper than winapi calls, I'm not going to disassemble user32.dll or something like that. Just creating small exe, nothing more. – Gregory Balko Sep 14 '17 at 10:24
  • But then you absolutely don't need to step into `printf` or crt0 so the question is very wrong. You should have asked "How to make a small executable on Windows" and show some source code and the commands you have tried. – Basile Starynkevitch Sep 14 '17 at 10:25
  • BTW, reverse-engineering DLLs provided by MicroSoft could be legally forbidden. – Basile Starynkevitch Sep 14 '17 at 10:30
  • @BasileStarynkevitch I already know the basics of "how to compile small exe", but this left me without some library functions. I know they are not in some dll somewhere, they are somewhere in my exe. I'll ask "how to recompile mingw runtime with debug information", if I'll not manage to do so by myself. – Gregory Balko Sep 14 '17 at 10:38
  • If I could I would downvote your question more than once. The actual question "how to make small Windows executable" has absolutely nothing to do with `printf` or `gdb` or `rand`. So next time you ask something, give a motivation and show what you have tried – Basile Starynkevitch Sep 14 '17 at 10:42
  • @BasileStarynkevitch strange you don't get it. How can I optimize code that I don't understand, or even more, can't even find or compile? – Gregory Balko Sep 14 '17 at 10:49
  • You probably cannot (and even are not allowed) to optimize MicroSoft code. You can only act on your own source code. – Basile Starynkevitch Sep 14 '17 at 10:51
  • BTW are you under European or under US juridiction? In Europe I heard that you are allowed some reverse engineering for interoperability purposes, but you need to consult a lawyer – Basile Starynkevitch Sep 14 '17 at 10:55
  • @BasileStarynkevitch it's not written by microsoft, mingw runtime is written by mingw authors. When I say runtime, I mean a static library that is linked into my exe. Maybe I'm using a wrong word here? I know there is a msvcrt.dll that is also called runtime, I'm not talking about that. – Gregory Balko Sep 14 '17 at 10:58

1 Answers1

0

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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547