gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > crtbegin.o and crtend.o

#780 - anli - Fri Jan 10, 2003 4:12 pm

Hello!

I think its a "mess" to use the gnu utils actually, so probably I am doing
something wrong. Do I really need to do all this?

First of all, I am using crt0.s and lnkscript by Jeff. I put those two files in the same directory as my project.

Secondly, I copy this files to my project dir:

For arm:
e:\devkitadv\lib\gcc-lib\arm-agb-elf\3.0.2\crtbegin.o and
e:\devkitadv\lib\gcc-lib\arm-agb-elf\3.0.2\crtend.o

For thumb:
e:\devkitadv\lib\gcc-lib\arm-agb-elf\3.0.2\thumb\crtbegin.o and
e:\devkitadv\lib\gcc-lib\arm-agb-elf\3.0.2\thumb\crtend.o

For interwork:
e:\devkitadv\lib\gcc-lib\arm-agb-elf\3.0.2\interwork\crtbegin.o and
e:\devkitadv\lib\gcc-lib\arm-agb-elf\3.0.2\interwork\crtend.o

I also put a main.cpp or main.c in the directory with my code.
(I have not done something big enough for splitting up in many files yet, or maybe I have without actually doing it). The entry function is called AgbMain.

Then I create a Makefile and put in the same directory. It typically contains something like this:

Code:

hello.gba: hello.elf
   objcopy -O binary hello.elf hello.gba

hello.elf: crt0.o main.o
   ld -L${DEVKITADV}/lib/gcc-lib/arm-agb-elf/3.0.2 \
       -L${DEVKITADV}/arm-agb-elf/lib -o hello.elf -T lnkscript \
       crt0.o crtbegin.o crtend.o main.o -lc -lm -lgcc

main.o: main.cpp
   g++ -c -O3 -marm -o main.o main.cpp

crt0.o:   crt0.s
   as crt0.s -o crt0.o


(I have an environment variable named DEVKITADV containing
"d:\devkitadv").

Now

1. Is there a way to just tell the linker "I am linking for the thumb
instruction set" instead of copying the files crtbegin.o and crtend.o to
the project directory, or in any way mention crtbegin.o and crtend.o in
my Makefile?
2. Is there a way to get rid of giving long paths to ld with the -L switch to
the C/C++/math runtime libraries? The paths I have now is clearly
version dependent, so when I am using a newer version of gcc some
time, I need to change all my makefiles.

3. If I dont put crt0.o first in the command line to ld, but something else,
the ROM will not start with crt0, and therefore be unusable. Is there a
way to configure the lnkscript so the ROM will always start with crt0.o?
This is of course of academic interest, since I am always able to assure
that crt0.o is first on the command line to ld, but I am just curious.

over and out
/anli


Last edited by anli on Fri Jan 10, 2003 5:04 pm; edited 4 times in total

#781 - anli - Fri Jan 10, 2003 4:15 pm

Hmm, for some reason, tabs are not working correctly in the forum, even
if i am in the [code] environment...

Of course, there are tabs at some places in the Makefile, else it would
not be useable...

:)

#782 - Touchstone - Fri Jan 10, 2003 4:37 pm

What's in crtbegin.o and crtend.o that you need? I'd recon you don't need them, atleast not if you are using plain C. Just a plain crt0.s that initialize stack pointers, program status registers and interrupts then followed by a call to your AgbMain would be sufficient. If you are using C++ and have any static objects you would want to extend your start vector to include initialization of malloc and a call to the static objects initialization but that should be it really.
_________________
You can't beat our meat

#783 - anli - Fri Jan 10, 2003 4:51 pm

I think crtbegin.o and crtend.o is some addresses of the stuff that
crt0.s must copy in the start of the execution, in other words, global
initialized variables.

But I may be wrong.

And actually, I dont see how that can be contained in an object file...
I mean, then I need to put them in the correct order to the linker, or?

Evil ducks should definitiely rule the world!
/anli