[Solved] Can't access my global variables!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
GarbageCollections
Posts: 2
Joined: Fri Mar 02, 2018 12:34 am

[Solved] Can't access my global variables!

Post 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
Last edited by GarbageCollections on Tue Mar 06, 2018 11:12 pm, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Can't access my global variables!

Post by iansjack »

Are you sure you are loading the .data section of your elf file?
GarbageCollections
Posts: 2
Joined: Fri Mar 02, 2018 12:34 am

Re: Can't access my global variables!

Post 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.
Post Reply