4

Take the following C program:

int main(){}

It's not using anything from the C standard library.
I assumed we could disable linkage through -nostdlib.

However, this results in the following error:

$ gcc -nostdlib -o main main.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400150

Can someone clarify what the _start symbol is and who is responsible for providing it?
How would one go about using -nostdlib?


Per the docs, I have also tried:

$ gcc -nostdlib -o main main.c -lgcc

Same behaviour is seen with clang.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • 1
    First thing to notice is that that's a *linker* error -- it doesn't mean it failed to compile. – Dmitri Jun 14 '16 at 22:44
  • @Dmitri Yes, I'll fix the title. – Trevor Hickey Jun 14 '16 at 22:45
  • 2
    `-lgcc` must go at the end. Gcc processes libraries at the point it encounters them, so they are only used to resolve symbols from previously mentioned objects. – rici Jun 14 '16 at 22:47
  • 1
    Why do you think it is? It might be required for linking, but not for compilation! And even for the latter it is not required. – too honest for this site Jun 14 '16 at 22:48
  • Does [this answer](http://stackoverflow.com/a/29694977/417501) answer your question? If it does, I would close your question as a duplicate. If it doesn't, it might help to highlight the things that are different in your question. – fuz Jun 14 '16 at 22:54

1 Answers1

1

_start is the entry point of the program, cf. this answer. The libc contains a little stub that reads the command line arguments off the place where the operating system puts them and then calls main. Thus, it is required to link against the libc in order to run a normal C program on a normal platform.

You can try to write a program without the libc, but then you have to provide your own _start. You can't do much though, you can't even return as there is nothing you return to. Your only option is to use assembly to painstakingly recreate the parts of the libc you need, like the _exit function.

Community
  • 1
  • 1
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    "Thus, it is required to link against the libc in order to run any C program." - Sorry, but that is plain wrong! It depends on the environment if it is required. On most embedded systems it is not and there is not even a standard library to link with. – too honest for this site Jun 14 '16 at 22:49
  • @Olaf An embedded system without a C standard library is not in compliance with the standard. Anyway, I reworded the answer slightly. Is this acceptable to you? – fuz Jun 14 '16 at 22:50
  • 1
    I honestly think my embedded code is very well "ordinary C programs". In fact, don't find that term in the standard. – too honest for this site Jun 14 '16 at 22:51
  • 1
    It very well is! Please read the standard **carefully** (translation and execution environment). – too honest for this site Jun 14 '16 at 22:52
  • @Olaf The standard specifies that the standard library must be available even in free-standing environments (except for those functions that do not have to be available). An implementation of the C programming language that does not provide this standard library is not in compliance with the standard. Programs written for such an environment are certainly not ordinary. – fuz Jun 14 '16 at 22:53
  • 1
    A freestanding environment is not required to provide the standard library per se, but only specific **headers** which do not declare any objects/(functions with external linkage. Briefly: there is nothing to link for a freestanding implementation, as these headers only provide types, and macros. – too honest for this site Jun 14 '16 at 22:56
  • Actually gcc **is** a freestanding implementation. It does not provide the standard library even for Linux. The glibc is a seperate project of the GCC (that name has two meanings). Alternatives exist. – too honest for this site Jun 14 '16 at 22:58
  • @Olaf You convinced me. I hope the updated wording is appropriate. – fuz Jun 14 '16 at 23:00
  • As a proud embedded systems engineer (whatever that means;-), I cannot accept my primary field of work is something abnormal. Less as these outnumber PCs etc. by decades. How about deleting the answer and let time delete the question as dup (hope it does delete, does it?) – too honest for this site Jun 14 '16 at 23:03