linking crt1.o

Programming, for all ages and all languages.
Post Reply
cheeky
Posts: 5
Joined: Thu Jun 07, 2007 11:38 am
Contact:

linking crt1.o

Post 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.
riding a bicycle in Seoul
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

cheeky
Posts: 5
Joined: Thu Jun 07, 2007 11:38 am
Contact:

Thank you for your reference

Post 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.
riding a bicycle in Seoul
User avatar
binutils
Member
Member
Posts: 214
Joined: Thu Apr 05, 2007 6:07 am

Re: Thank you for your reference

Post 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/
cheeky
Posts: 5
Joined: Thu Jun 07, 2007 11:38 am
Contact:

Thank you

Post by cheeky »

I was able to see lots of illustrative descriptions about linking. :D
riding a bicycle in Seoul
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post 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.
cheeky
Posts: 5
Joined: Thu Jun 07, 2007 11:38 am
Contact:

another link

Post 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!
riding a bicycle in Seoul
Post Reply