Page 1 of 1

linking crt1.o

Posted: Thu Jun 07, 2007 11:50 am
by cheeky
Nowadays, I'm studying L4 kernel. It uses user-level applications on it.


As a newbie I found that I have to write init codes like crt0-ia32.S for L4 applications. I was really wondering where crt0 is from.

I found the following command in ld 2.9.1 manual.

$ ld -o output /lib/crt0.o hello.o -lc


My debian stable etch doesn't have /lib/crt0.o, instead, has /usr/lib/crt1.o

I thought my application with the next source working on Linux will be built as shown below:

Code: Select all

#include <stdio.h>

int main(void)
{
    return 0;
}

Code: Select all

$ ld m.o /usr/lib/crt1.o /usr/lib/libc.a
/usr/lib/libc.a(elf-init.o): In function `__libc_csu_init': undefined reference to `_init'
/usr/lib/libc.a(elf-init.o): In function `__libc_csu_fini': undefined reference to `_fini'


Now it says I can't resolve the symbols, '_init' and '_fini'. Which object file has those symbols?? I'd like to ask this out of my curiosity. :oops: Thank you.









aeeee! one more thing I'd like to know is about ld script.

Does anyone know good tutorial how to write a ld script? ld manual isn't enough, it's too rough because I'm a beginner. I need help.

Posted: Thu Jun 07, 2007 12:05 pm
by Pyrofan1

Thank you for your reference

Posted: Thu Jun 07, 2007 12:25 pm
by cheeky
The reference was really helpful to get to know a bit more about crt0.

The C compiler link in the object file crt0.o to provide startup capabilities and environments for program execution.

No problem.


I don't want to build easy command gcc m.c like this. I'd like to know steps how the application is built.

That's why I commanded as follows :

Code: Select all

$ gcc -c m.c
$ ld m.o /usr/lib/crt1.o -lc
/usr/lib/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init': undefined reference to `_init'
/usr/lib/libc_nonshared.a(elf-init.oS): In function `__libc_csu_fini': undefined reference to `_fini'



ps. Today I'm happy to boot with grub and easy c++ and c tutorial 'hello-world' kernels.

Re: Thank you for your reference

Posted: Sat Jun 09, 2007 10:37 pm
by binutils
cheeky wrote: I don't want to build easy command gcc m.c like this. I'd like to know steps how the application is built.
http://csapp.cs.cmu.edu/

Thank you

Posted: Mon Jun 11, 2007 12:07 am
by cheeky
I was able to see lots of illustrative descriptions about linking. :D

Posted: Thu Jun 14, 2007 10:03 am
by nick8325
If you run gcc -Wl,-v m.c, ld will print the linker command. On my machine it's:

Code: Select all

/usr/bin/ld --eh-frame-hdr -m elf_i386 --hash-style=both
    -dynamic-linker /lib/ld-linux.so.2 usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crti.o /usr/lib/gcc/i486-linux-gnu/4.1.2/crtbegin.o
    -L/usr/lib/gcc/i486-linux-gnu/4.1.2 -L/usr/lib/gcc/i486-linux-gnu/4.1.2
    -L/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib -L/lib/../lib -L/usr/lib/../lib
    -v /tmp/cck1Ady4.o -lgcc --as-needed -lgcc_s --no-as-needed
    -lc -lgcc --as-needed -lgcc_s --no-as-needed
    /usr/lib/gcc/i486-linux-gnu/4.1.2/crtend.o
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crtn.o
If you ignore all the paths and ELF stuff, it's:

Code: Select all

ld crt1.o crti.o crtbegin.o m.o -lgcc -lgcc_s -lc -lgcc -lgcc_s crtend.o crtn.o
crt1.o is the startup code; crti.o and crtn.o define a few things to help with running constructors (crti defines _init and _fini), and crtbegin.o and crtend.o run C++ constructors and destructors for global variables.

another link

Posted: Thu Jun 14, 2007 10:11 pm
by cheeky
http://www.lisha.ufsc.br/~guto/teaching ... hello.html

It was also very helpful for me to understand something more detail. Thanks a lot everbody!