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.

C/C++ > devkitARM r3 and static constructors

#16755 - dushan42 - Tue Feb 24, 2004 2:51 pm

Hi,

I'm trying (and failing) to get static constructors to work in the latest devkitARM (http://homepage.ntlworld.com/wintermute2002/)

I'm using Jeff's lnkscript and crt0.s (1.28) with '__CPPSupport' uncommented and '__MultiBootInclude' commented out.

I can compile and link if I don't try to link in crtbegin.o/crtend.o - but then none of the constructors for global objects get called.
If I link crtbegin.o and crtend.o, I get the following link error:

Code:

d:/gba/dkarm/bin/arm-agb-elf-g++ -mthumb-interwork -mthumb -O2 -Wall -T lnkscript -nostartfiles crt0.o crtbegin.o crtend.o test.o -o test.elf
d:\gba\dkarm\bin\..\lib\gcc-lib\arm-agb-elf\3.3.3\..\..\..\..\arm-agb-elf\bin\ld.exe: section .data [08000330 -> 0800033b] overlaps section .dtors [08000330 -> 08000337]
collect2: ld returned 1 exit status
d:\gba\dkarm\bin\..\lib\gcc-lib\arm-agb-elf\3.3.3\..\..\..\..\arm-agb-elf\bin\ld.exe: section .data [08000330 -> 0800033b] overlaps section .dtors [08000330 -> 08000337]


Any ideas?

I've got a fairly minimal makefile:

Code:

DKARM=d:/gba/dkarm

GCC_BIN      =$(DKARM)/bin
GCC_PREFIX   =arm-agb-elf-

CXX          =$(GCC_BIN)/$(GCC_PREFIX)g++
OBJCOPY      =$(GCC_BIN)/$(GCC_PREFIX)objcopy

CFLAGS       =-mthumb-interwork -mthumb -O2 -Wall

LDFLAGS      =-T lnkscript -nostartfiles

all:
   $(CXX) $(CFLAGS) -c test.cpp -o test.o
   $(CXX) -c crt0.s -o crt0.o
   $(CXX) $(CFLAGS) $(LDFLAGS) crt0.o crtbegin.o crtend.o test.o -o test.elf
   $(OBJCOPY) -O binary test.elf test.gba


And even more minimal test program (test.cpp):
Code:

class Test
{
public:
   Test(int value) : mValue(value) {}

private:
   int mValue;
};

Test gTest1(1);
Test gTest2(2);
int gInt = 3;

int main()
{
   Test test(4);
   gTest1 = test;

   while(true); // Halt
}


thanks,

Dushan

#17388 - dushan42 - Sun Mar 07, 2004 5:10 am

The problem is actually a bit more generic - I couldn't get global constructors to work at all with Jeff's crt0s/lnscript (I tried dka, devkitarm and my own build).

I did a bit of googling, and it seems that gcc has two ways of invoking the constructors:

- if it doesn't find .init section, the compiler automatically generates function _main which invokes all the constructors, and inserts a call to it in main()
- if .init exists, crt0.s should call _init / _fini, defined in crt[i|n|begin|end].o

This is of course vastly simplified - check out http://www.delorie.com/gnu/docs/gcc/gccint_149.html for more detail.

For some reason the first method doesn't work - if anyone knows why no _main is generated even though there clearly isn't any .init section, please enlighten me.

The second method involves linking in the various crt* files, which as I pointed out in the previous message caused various linker errors. These, after a bit more googling about linker scripts, turned out to be quite easy to fix - simply add .init, .fini and the somewhat mysterious .jcr sections to the lnkscript. With the crt[i|n|begin|end].o linked in, you just have to insert a call to _init in your crt0.s (I didn't bother with _fini) and voil? - the global constructors get called..

Finally I can use custom crt0/lnscript and have my project compile with all the various gcc builds out there *and* have global constructors working. I know it's probably not a big deal for most of you out there (judging by the lack of responses), but it's been bugging me ever since I started coding for GBA :-).

If this is at all useful to anyone, let me know and I'll clean up & post the hacked crt0.s/lnscript.

#26678 - DialogPimp - Wed Sep 22, 2004 11:44 pm

I'd be interested in the crt0/lnkscript. The same problem had me stuffed for 3 nights this week and I never understood what the problem was (although I knew it was something in that area).