Page 1 of 1
Can't access Global Variables
Posted: Thu Feb 19, 2004 1:28 pm
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
Re:Can't access Global Variables
Posted: Thu Feb 19, 2004 2:29 pm
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
Re:Can't access Global Variables
Posted: Thu Feb 19, 2004 2:51 pm
by srg
work with C++
No , I just work with C
Just in case that changes anything
Re:Can't access Global Variables
Posted: Fri Feb 20, 2004 2:22 pm
by Adek336
Well if it's C, it has to be something different. Aren't you overwriting your data by any chance?
Re:Can't access Global Variables
Posted: Mon Feb 23, 2004 3:57 am
by Pype.Clicker
i'm not sure but i would be trying without the 'AT' statement for data and bss ...
Re:Can't access Global Variables
Posted: Mon Feb 23, 2004 4:06 am
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.
Re:Can't access Global Variables
Posted: Wed Feb 25, 2004 1:33 pm
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
Re:Can't access Global Variables
Posted: Wed Feb 25, 2004 2:24 pm
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
.