[bitlug] Re: C Startup files...

  • From: Pavan T C <Pavan.Chandrashekar@xxxxxxx>
  • To: bitlug@xxxxxxxxxxxxx
  • Date: Fri, 23 Dec 2005 12:59:43 +0530

Peeyush Prasad wrote:

Hi again,
  Thought I should start another thread for this discussion...
Does anybody know the functions of the various startup files ?
I see the following on my system: (RH 7.3, gcc 2.96)

/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o
/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbeginS.o
/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtendS.o

dint objdump -d help?? Havent tried it though.


and

/usr/lib/crt1.o
/usr/lib/crti.o
/usr/lib/crtn.o

These guys get linked in if you do a default gcc build with the c library:

Best thing to do is to find the files in the gcc sources and go through them, but I want to stay away from that....

Now,lets see...

[vlbi@rcs1 bfin]$ cat hello.c
#include <stdio.h>

int main(int argc,char *argv[])
{
   int i = 0;
   for(i=0; i<10; i++)
    printf("Go AwaY!! >:-(\n");
   getchar();
   return 0;
}

[vlbi@rcs1 bfin]$ gcc -v -o hello hello.c

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)
/usr/lib/gcc-lib/i386-redhat-linux/2.96/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_MINOR__=96 -D__GNUC_PATCHLEVEL__=0 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__NO_INLINE__ -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -D__tune_i386__ hello.c /tmp/ccLkKeK6.i

cpp0 is the C preprocessor. Note /tmp/ccLkKeK6.i - intermediate file which is input to the c compiler.

GNU CPP version 2.96 20000731 (Red Hat Linux 7.3 2.96-110) (cpplib) (i386 Linux/ELF)

ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
  /usr/local/include
  /usr/lib/gcc-lib/i386-redhat-linux/2.96/include
  /usr/include
End of search list.

// =========== Compile command ==============
/usr/lib/gcc-lib/i386-redhat-linux/2.96/cc1 /tmp/ccLkKeK6.i -quiet
note the intermediate file :                    ^^^^^^^^^^^^^^^^

-dumpbase hello.c -version -o /tmp/ccu7NLr5.s
GNU C version 2.96 20000731 (Red Hat Linux 7.3 2.96-110) (i386-redhat-linux) compiled by GNU C version 2.96 20000731 (Red Hat Linux 7.3 2.96-110).


// ========== Assemble command ==============
as -V -Qy -o /tmp/ccpaFlN5.o /tmp/ccu7NLr5.s
GNU assembler version 2.11.93.0.2 (i386-redhat-linux) using BFD version 2.11.93.0.2 20020207
// ========== Linker called from within the assembler =========
/usr/lib/gcc-lib/i386-redhat-linux/2.96/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o hello /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o -L/usr/lib/gcc-lib/i386-redhat-linux/2.96 -L/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../.. /tmp/ccpaFlN5.o -lgcc -lc -lgcc /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crtn.o



[vlbi@rcs1 vlbi]$ pgrep hello 20713 [vlbi@rcs1 vlbi]$ cat /proc/20713/ cmdline cwd exe maps mounts stat status cpu environ fd mem root statm [vlbi@rcs1 vlbi]$ cat /proc/20713/mem cat: /proc/20713/mem: No such process [vlbi@rcs1 vlbi]$ cat /proc/20713/maps 08048000-08049000 r-xp 00000000 03:05 229541 /home/vlbi/bfin/hello 08049000-0804a000 rw-p 00000000 03:05 229541 /home/vlbi/bfin/hello 40000000-40013000 r-xp 00000000 03:07 67421 /lib/ld-2.2.5.so 40013000-40014000 rw-p 00013000 03:07 67421 /lib/ld-2.2.5.so 40014000-40016000 rw-p 00000000 00:00 0 42000000-4212c000 r-xp 00000000 03:07 114259 /lib/i686/libc-2.2.5.so 4212c000-42131000 rw-p 0012c000 03:07 114259 /lib/i686/libc-2.2.5.so 42131000-42135000 rw-p 00000000 00:00 0 bfffe000-c0000000 rwxp fffff000 00:00 0


Questions: 1. What is cpp0?

Answered before.

2. What does collect2 do ??

?? always been a bane :-(. Never could figure out why it is even called so. Will try and look it up by the next follow up.


3. The following are linked in: crt1.o crti.o crtbegin.o crtend.o crtn.o
    -lc -lgcc. Why??

-lc and -lgcc are the linking of the c library and and the gcc library that are implicit requirements for any c program and c program compiled with gcc respectively. They mostly have initialization and uninitialization code.


4. The dynamic linker ? /lib/ld-linux.so.2 ??

yep. This is the linker that would do the runtime linking of all the shared object dependencies of the process. The ld (called the link editor) specifies the list of dependencies of the program on runtime libraries via the -l option. (Examples - -lc -lgcc). If you look at the output of "dump -Lvp" on the executable under the "NEEDED" section, you can see the dependencies of the executable (on shared objects).
The dynamic linker (ld-linux.so.2 - which is an executable !!) runs when the hello program is run, and resolves the dependencies on the shared objects mentioned during link edit.


Pavan

-- P ps - output :^) [vlbi@rcs1 bfin]$ ./hello Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-( Go AwaY!! >:-(




Other related posts: