OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:40 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [Solved] Can't access my global variables!
PostPosted: Sat Mar 03, 2018 9:23 am 
Offline

Joined: Fri Mar 02, 2018 12:34 am
Posts: 2
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:
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:
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:
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:
$(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.

Top
 Profile  
 
 Post subject: Re: Can't access my global variables!
PostPosted: Sat Mar 03, 2018 10:01 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Are you sure you are loading the .data section of your elf file?


Top
 Profile  
 
 Post subject: Re: Can't access my global variables!
PostPosted: Sat Mar 03, 2018 10:55 am 
Offline

Joined: Fri Mar 02, 2018 12:34 am
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: 8infy, DotBot [Bot] and 59 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group