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
Can't access Global Variables
Re:Can't access Global Variables
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
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
No , I just work with Cwork with C++
Just in case that changes anything
Re:Can't access Global Variables
Well if it's C, it has to be something different. Aren't you overwriting your data by any chance?
- Pype.Clicker
- 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
i'm not sure but i would be trying without the 'AT' statement for data and bss ...
Re:Can't access Global Variables
{ .text virt : AT(phys)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 = .;
}
{
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
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
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
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).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.
Also, you need PAE .