This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: what are the crt files.


On 03/10/2010 04:55 AM, Corinna Vinschen wrote:
On Mar 10 09:55, Sanjeev Mk wrote:
hi,
On compiling a C program, i see it gets linked to crt*  files. I
understand these are the c runtime files.

What functions do these files carry out and how do they work?

The crt files contain the code which is called when the binary gets executed. Their job is initialization of libc datastructures and eventually to call the executable's main() function.


To add to what Corinna has said; crt0 is written in assembler and is the entry point for the application. It does a number of things. For starters, it should set the stack register. The stack and heap typically share a piece of storage and move in opposite directions. For example, the stack register may be set initially to point to the top of the storage and the heap to point to the bottom. The stack will move down as it is used, the heap pointer will move up. The sbrk syscall (that manages the heap) often will look for collision (when one pointer goes into the other's territory).


In addition, there is an area known as the bss. This area contains static and global data that is initialized to zero. For embedded systems, this storage is mapped into uninitialized RAM and so needs to be cleared manually at start-up.

As Corrinna pointed out, the main() routine is called and the _exit() routine is run on return from main to stop execution. If C++ is supported, another call may be made prior to main to run static/global constructors (crt1.c). Destructors would also need to be run prior to exiting. This might be a separate call or buried within the _exit routine.

To see a simple example of crt0.S, crt1.c, and _exit, check out the libgloss/mn10300 directory. You may not understand the assembler bits, but the comments are pretty clear about what is occurring.

In several of my programs,i have not used string operations or print
operations.But still many of them get loaded into memory,as confirmed
by the .map file. How to prevent this from happening? Does this have
anything to do with the c runtime files?

That's possible but not necessarily. Any of the C functions you called could use string functions under the hood. If it's actually the crt file, you have to figure out what it does. If it's one of the newlib crt files, you can find the sources in libc/sys.


or in libgloss/xxxx where xxxx is your platform.


or look in gcc as it may provide the crt* files for your platform.

-- Jeff J.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]