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() {
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!