Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use crt0.o to setup .bss and .data in my bare metal C program?

I successfully wrote a bare metal C program which is running on my STM32F4. It's nothing fancy, just the usual led-blinky-program. In this project I have written the initialization routines which is clearing the .bss section and initializing the .data section myself.

This wasn't really complicated. In the linker script, I just instructed the linker to create some symbols which are marking the start and the end of the .data and .bss section.

.data 0x20001000 :
    ALIGN(4)
    {
        __etext = LOADADDR(.data);
        __data_start__ = ADDR(.data) ;
        *(.data*) ;
        __data_end__ = ADDR(.data) + SIZEOF(.data) ;
    } >RAM AT>ROM


.bss :
    ALIGN(4)
    {
        __bss_start__ = ADDR(.bss) ;
        *(.bss*) ;
        __bss_end__ = ADDR(.bss) + SIZEOF(.bss) ;
    } >RAM

Then I was using these symbols in my code:

extern unsigned int __etext;
extern unsigned int __data_start__;
extern unsigned int __data_end__;
extern unsigned int __bss_start__;
extern unsigned int __bss_end__;


void Reset_Handler()
{
    unsigned int * src;
    unsigned int * dest;

    src = &__etext;
    dest = &__data_start__;

    /* copy .data */
    while (dest < &__data_end__)
        *(dest++) = *(src++);

    /* Zero bss.  */
    for (dest = &__bss_start__; dest < &__bss_end__; dest++)
        *dest = 0;
}

Now I would like to use the crt0 for the purpose of setting-up .bss and .data. (I heard setting up things is the main purpose of crt0.)

How can I do that? Is the basic principle of defining symbols in the linker script and using the in the code the same?


TL;DR

How can I use crt0 to setup my .bss and .data section?

like image 315
Multisync Avatar asked Nov 10 '22 16:11

Multisync


1 Answers

in general, in a linker command file/script...

The posted script has a few problems.

Suggest something similar to the following. (use the actual origin and length parameters) (note that .text, .data, .bss are not the only sections created. there are many more and they should be listed appropriately )

You should look at http://www.math.utah.edu/docs/info/ld_3.html#SEC18 for the details and examples for linker command files

/* this is a very simple memory layout */
/* usually there are separate memory items */
/* for each memory mapped peripheral */
/* external RAM, etc etc etc */
MEMORY {
    rom : ORIGIN = 0, LENGTH = 256K
    ram : ORIGIN = 0x40000000, LENGTH = 4M
}

SECTIONS {
    rom :0 (NOLOAD) BLOCK(4) {
    }

    ram : {
        .text : BLOCK(4) {
        .textStart = .;
        *(.text)
        .textEnd = .;
        }

        .bss : BLOCK(4) {
        .bssStart = .;
        *(.bss)
        .bssEnd = .;
        }

        .data : BLOCK(4) {
        .dataStart = .;
        *(.data)
        .dataEnd = .;
        }
    }
}
like image 152
user3629249 Avatar answered Nov 14 '22 22:11

user3629249