This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Error in startup for GCC (FIXED)

Hi

I've detected that when using the NRF51822 under Eclipse and GCC compilers, global variables are not automatically initialized. This is because an error on the startup file provided by Nordic for GCC environments (not IAR). I'm surprised nobody has commented about this problem on this forum, but I guess about all people is working with IAR or Keil environments.

The problems is because BSS memory zone is not being initialized in the startup file. As I understand, the DATA memory zone is reserved for variables initialized to some value different to zero (NULL), but BSS memory zone is reserved for variables initialized to zero value.

While the startup file initializes the DATA memory variables (variables different than zero), the BSS is not being initialized. This is a problem when trying to use a global variable that should be initialized to zero, but not. I've detected for example bool variables initialized to non legal values, such as 12. It is clear this is a problem. FreeRTOS for example is not able to be initialized due to these problems.

To fix this problem, it is necessary to add the following lines to the gcc_startup_nrf51.s file, line 163:

    	/* data section */
    ldr    r1, =__etext
    ldr    r2, =__data_start__
    ldr    r3, =__data_end__

    subs    r3, r2
    ble     .LC0

.LC1:
    subs    r3, 4
    ldr    r0, [r1,r3]
    str    r0, [r2,r3]
    bgt    .LC1
.LC0:
    
    **/* bss section */
    ldr		r2, =__bss_start__
    ldr 	r3, =__bss_end__
    ldr		r0, =0x00
	subs	r3, r2
	ble		.BS0

.BS1:
	subs	r3, 4
	str     r0, [r2,r3]
	bgt		.BS1
.BS0:

    LDR     R0, =SystemInit
    BLX     R0
    LDR     R0, =main
    BX      R0**

The BSS initializing section has been added under the symbol .LC0, and before SystemInit.

I hope you find this helpful.

Regards,

Elena

Parents
  • I'm definitely right - zeroing the BSS is one of the responsibilities of crt0, so if you just entirely removed _start then then you've removed an important part of standard initialisation. That also copies some other data, sets up the heap, runs any static initialisers, then calls main and cleans up afterwards. You may get away without any of those things now, but just removing that call is a large bug in your code waiting to happen as newer versions of gcc may put other init in there which you won't call.

    As the whole thread is actually wrong, and there is not an error in the startup file Nordic distributes, as you state, you might think about correcting or deleting it entirely. People read old threads and try random things from them and get themselves into a worse mess than they started.

    I would also suggest you figure out your problem with gcc's start, it's important.

Reply
  • I'm definitely right - zeroing the BSS is one of the responsibilities of crt0, so if you just entirely removed _start then then you've removed an important part of standard initialisation. That also copies some other data, sets up the heap, runs any static initialisers, then calls main and cleans up afterwards. You may get away without any of those things now, but just removing that call is a large bug in your code waiting to happen as newer versions of gcc may put other init in there which you won't call.

    As the whole thread is actually wrong, and there is not an error in the startup file Nordic distributes, as you state, you might think about correcting or deleting it entirely. People read old threads and try random things from them and get themselves into a worse mess than they started.

    I would also suggest you figure out your problem with gcc's start, it's important.

Children
No Data
Related