Can't access 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
srg

Can't access Global Variables

Post by srg »

Right, I now have a small ELF kernel file wich loads up and executes.

The stack seems to work ok, I have have functions and global variables that work.

The trouble is that global variables don't work, if I access them I get 0.

here is my linker script, anything wrong here, it is taken from the grub tutorial.

/* SG-OS kernel linker script: kernel.ld */

OUTPUT_FORMAT("elf32-i386")
ENTRY(kernel_start)
virt = 0x80000000;
phys = 0x100000;
SECTIONS
{ .text virt : AT(phys)
{
code = .;
*(.text)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4096);
}
end = .;
}

Thanks
Adek336

Re:Can't access Global Variables

Post by Adek336 »

As I understand it, you:
1) work with C++
2) try to initialize global variables but they contain zero,
then this is a known thing.

The variables should be initialised on runtime. You have to write your own routines which do that, there is an online course at invalidsoftware.com.something.

Cheers,
Adrian
srg

Re:Can't access Global Variables

Post by srg »

work with C++
No , I just work with C

Just in case that changes anything
Adek336

Re:Can't access Global Variables

Post by Adek336 »

Well if it's C, it has to be something different. Aren't you overwriting your data by any chance?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Can't access Global Variables

Post by Pype.Clicker »

i'm not sure but i would be trying without the 'AT' statement for data and bss ...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Can't access Global Variables

Post by Candy »

srg wrote: SECTIONS
{ .text virt : AT(phys)
{
code = .;
*(.text)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4096);
}
end = .;
}
{ .text virt : AT(phys)
{
code = .;
*(.text)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))

So, you put the .data at the physical location plus the data offset minus the code. Code is at phys, so you put data at data. Seems weird?

I think you need to work these AT's out again, or let the compiler think out where you're going to put them. I would say myself that data = at phys + sizeof(.text) + .ALIGN(4096) or however you write that. As Pype suggested, exactly where the compiler would guess you put it.
srg

Re:Can't access Global Variables

Post by srg »

Sorry for not replying for ages.

Anyway, I got this from the GRUB tutorial.

Anyway, I've fixed the problem by using the idea in Tim's Memory Management tutorial part1. The first thing I do in my kernel memory initalisation is set my data segment base address to 0x80000000 and suddenly everything works, because there is a wrap arround at the top of memory.

Once I enable paging and set the pages at 0x100000 to a virt adress of 0x80000000, I can then set my data segments base back to 0.

Also, about AMD64 system and this, well it dawned on me, I simply do all this in pmode, setup paging, put this back, and then switch to long mode.

srg
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Can't access Global Variables

Post by Candy »

srg wrote: Once I enable paging and set the pages at 0x100000 to a virt adress of 0x80000000, I can then set my data segments base back to 0.

Also, about AMD64 system and this, well it dawned on me, I simply do all this in pmode, setup paging, put this back, and then switch to long mode.
That's a thinking error. Long mode cannot be enabled when paging is enabled. You have to turn off paging to turn on long mode (see AMD docs for more info).

Also, you need PAE :).
Post Reply