Page 1 of 1

[Solved] Can't access my global variables!

Posted: Sat Mar 03, 2018 9:23 am
by GarbageCollections
Hello!
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);
}
Yup! Its failed, it didn't call my clear_screen().

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
Thanks! :D

** 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
During the troubleshooting, I had tried cross-compiled linker(x86_64-elf-ld) as my linker, but it cannot find libgcc.
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

Re: Can't access my global variables!

Posted: Sat Mar 03, 2018 10:01 am
by iansjack
Are you sure you are loading the .data section of your elf file?

Re: Can't access my global variables!

Posted: Sat Mar 03, 2018 10:55 am
by GarbageCollections
iansjack wrote:Are you sure you are loading the .data section of your elf file?
I use flat binary with a simple header, and the bootloader did loaded all sections in kernel.