Recently, I start writing my own os in c++ and nasm. Just after I finished the efi bootloader, I decide to write a simple print() function that plot pixels according to the font bitmaps which stored in a array. sadly I see nothing on screen, then I start checking each related functions and they work fine as they should. So I do a checking to the global variables by the following code:
Code: Select all
const u16 a[24] = {0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0,
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0,
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0};
extern "C" void kernel_init(SOS_BOOT_INFO *boot_info) {
graphics_init(boot_info->VideoInfo); //works
//It work!
if (a[0] == 0xff) {
//should call clear_screen()
clear_screen(DEFAULT_COLOR); //also work
}
while (true);
}
This code works fine, but once I try this:
Code: Select all
const u16 a[24] = {0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0,
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0,
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0};
extern "C" void kernel_init(SOS_BOOT_INFO *boot_info) {
graphics_init(boot_info->VideoInfo); //works
//NOT WORKING ANYMORE!
u32 i = 0;
if (a[i] == 0xff) {
//should call clear_screen()
clear_screen(DEFAULT_COLOR); //also work
}
while (true);
}
I had tried compile with clang and g++, also tried convert all the code into C and still failed.
Did anyone know how to solve this problem? (call global constructors?)
Here is my clang and linker flags:
Code: Select all
clang:
-target x86_64-unknown-elf -ffreestanding -fno-stack-protector -nostdlib -fno-rtti -mno-red-zone -Wall -std=c++11 -masm=intel -O2
ld:
-nostdlib
** Problem Solved! **
The reason that I can't access global variables, is the initialize code didn't linked into the file.
To solve this problem, make sure -lgcc are next behind your object files.
Somethings like:
Code: Select all
$(LD) -T $(LINKER_SCRIPT) $(LD_FLAGS) -o hello.bin foo.o bar.o -lgcc
After several failures, turns out your have to use the cross-compiler(gcc or g++) you compile your source file to link your kernel *facepalm*
Hope this could help
GarbageCollections