Static variables not initialized

I am developing a small core for Raspberry Pi as a school project. We are faced with the problem of static initialization of variables: it seems that they are not initialized at all. I found several related topics, but so far nothing came of it, although it helped me understand the problem (at least I think).

All code can be found in this repository , but I will try to summarize the corresponding code here.

Code extracted from the project showing the problem: (kernel / src / kernel.cpp)

static int staticVal = 42; void doStuff() { // Prevent the compiler from optimizing the value of staticVal staticVal++; } __attribute__((section(".init"))) int main(void) { //... gpio::blinkValue(staticVal); // Displays the value through LEDs //... } 

Then the code is compiled using (for example)

 arm-none-eabi-g++ -O2 -std=c++11 -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -nostartfiles -Wall -Wextra -pedantic -I src/ -I uspi/include -DHW_PI_1B -c src/[file].cpp -o _build/[file].o 

and embedded in a binary file using the same parameters, and finally compiled into kernel.img using

 arm-none-eabi-ld --no-undefined _build/master.bin -Map _build/kernel.map -o _build/output.elf -T kernel.ld arm-none-eabi-objcopy _build/output.elf -O binary kernel.img 

(you can read the Makefile directly for more information: github.com/tobast/sysres-pikern/blob/staticNotWorking/kernel/Makefile).

The script builder used can also be a problem, as we tried to figure out how to write a worker without knowing what we were doing (we are using a modified script builder from another project): github.com/tobast/sysres-pikern/blob/staticNotWorking/kernel/ kernel.ld

 _start = 0x8000; ENTRY(_start) SECTIONS { .init 0x8000 : { *(.init) } .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss) } } 

The program generated in this way displays an explicitly random value (which, in any case, has nothing to do with the expected value - 42).

What I have understood so far is that the operating system must be responsible for initializing the static variables before the actual launch of the program, but when we write the operating system, no one does it for us. Of course, we could manually initialize these variables (by invoking the launch function for this purpose), but that would be an ugly and painful solution ...

Are there any g ++ / linker options that we don’t have, or is there a problem in our script builder?

Thanks!

+5
source share
2 answers

I see a couple of problems:

  • Your sections are not page aligned, i.e. ALIGN(0x1000) in the script builder

  • You do not have a β€œrun” ASM script that sets the stack and zeros in the bss section.

I have no idea how such a script would look like on ARM (I'm used to x86), but here is an example:

  .section "vectors" reset: b start undef: b undef swi: b swi pabt: b pabt dabt: b dabt nop irq: b irq fiq: b fiq .text start: @@ Copy data to RAM. ldr r0, =flash_sdata ldr r1, =ram_sdata ldr r2, =data_size @@ Handle data_size == 0 cmp r2, #0 beq init_bss copy: ldrb r4, [r0], #1 strb r4, [r1], #1 subs r2, r2, #1 bne copy init_bss: @@ Initialize .bss ldr r0, =sbss ldr r1, =ebss ldr r2, =bss_size @@ Handle bss_size == 0 cmp r2, #0 beq init_stack mov r4, #0 zero: strb r4, [r0], #1 subs r2, r2, #1 bne zero init_stack: @@ Initialize the stack pointer ldr sp, =0xA4000000 bl main stop: b stop 

Of course, you will need to change this to match the semantics of your kernel.

+2
source

You are probably also faced with the fact that the .bss section is also not cleared. Before starting main, you must have a special processed (assembler) block that initializes data at runtime with the original data from rom, clearing bss and setting the stack pointer.

This initial blob usually occurs when you contact libc (and embedded compilers like Keil just hide this process when using their IDEs).

If you do not use libc, there should be some files called crt * .o, accessible with a compiler that can be used (and you may need a few details in the script linker to get the destination addresses for the memory to actually be, etc. .)

+1
source

All Articles