Page 1 of 1

Using --gc-sections puts __data_end__ before __data_start__

Posted: Wed Oct 19, 2011 7:24 pm
by Dwedit
I was just spending the last few hours trying to track down a problem in devkitarm.
I was having a problem where "__data_start__" came in memory after "__data_end__", so when the crt0 code copies data into ram, it copies a negative number (big number) of bytes, and crashes.
After searching for the cause, I found that using the "--gc-sections" switch on the linker was causing problems when using EWRAM BSS variables.

I've attached a minimal example project that demonstrates the problem. The program just turns the screen red, but if it crashes in crt0 before the program starts, the screen remains white.

Re: Using --gc-sections puts __data_end__ before __data_star

Posted: Wed Oct 19, 2011 7:49 pm
by Dwedit
Also, there's another problem.
In crt0, it overwrites the SBSS section with junk data, because it's copying from __ewram_start__ to __ewram_end__, which is past the end of the SBSS section. It should be copying up to __sbss_start__ instead.

In the crt0.s file, change this:

Code: Select all

@---------------------------------------------------------------------------------
CIW0Skip:
@---------------------------------------------------------------------------------
@ Copy external work ram (ewram section) from LMA to VMA (ROM to RAM)
@---------------------------------------------------------------------------------
	ldr	r1, =__ewram_lma
	ldr	r2, =__ewram_start
	ldr	r4, =__ewram_end
	bl	CopyMemChk
to this:

Code: Select all

@---------------------------------------------------------------------------------
CIW0Skip:
@---------------------------------------------------------------------------------
@ Copy external work ram (ewram section) from LMA to VMA (ROM to RAM)
@---------------------------------------------------------------------------------
	ldr	r1, =__ewram_lma
	ldr	r2, =__ewram_start
	ldr	r4, =__sbss_start__
	bl	CopyMemChk

Re: Using --gc-sections puts __data_end__ before __data_star

Posted: Wed Oct 19, 2011 10:39 pm
by WinterMute
Those are both linkscript problems by the looks of things - __ewram_end shouldn't have been after the sbss section and there seems to have been some oddities in the sections which go in the data section.

I just committed some changes to gba_cart.ld and gba_mb.ld, grab them from SVN and copy to devkitARM/arm-eabi/lib

http://devkitpro.svn.sourceforge.net/vi ... abi/crtls/

These changes seem to fix your testcase, shout if you spot any more issues.