Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the gcc flag -nostartfiles used?

Tags:

gcc

I can't seem to figure out the purpose of the flag -nostartfiles. It prevents main() from being called immediately, and typically you'd also specify the program entry point. However, why would I want to do that? I have written a kernel that required it, since everything was up to me to get started, but other than that, is there a non-OS reason to specify a different program entry point?

like image 801
Michael Stachowsky Avatar asked Mar 27 '17 15:03

Michael Stachowsky


People also ask

What are the purpose of the GCC flags?

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. The compiler performs optimization based on the knowledge it has of the program.

Where is GCC used?

It is a compiler system for the various programming languages. It is mainly used to compile the C and C++ programs. It takes the name of the source program as a necessary argument; rest arguments are optional such as debugging, warning, object file, and linking libraries. GCC is a core component of the GNU toolchain.

Does GCC have a flag?

GCC and Clang have several warning flags which will enable a collection of useful checks which we will explore in more detail below.

What is GCC wall flag?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.


2 Answers

You'd use -nostartfiles (and -nostdlib) when you do not want any standard libraries (like libc and libgcc) to be linked in and standard initialization actions, like calling C++ constructors, to be run. Typical use-cases are writing system software like kernels or firmwares.

like image 199
yugr Avatar answered Sep 27 '22 18:09

yugr


I think that with -nostartfiles command line arguments will not be passed to main() and environment variables will not be available to the executable, among other things. Docker uses -nostartfiles in an example where a single binary is run within a simple container, i.e., where the container is run like a single binary: https://docs.docker.com/develop/develop-images/baseimages/, so that is another use case.

Slightly related is Source for 'startup files' in gcc/mingw, which lists functions that are linked into to the code if -nostartfiles option is not used.

like image 30
Bjorn Avatar answered Sep 27 '22 18:09

Bjorn