Using paging, I am trying to directly map my kernel excecutable during boot instead of copying it to an aligned address. This requires the file offset of my program headers (and by extension, sections) to be page aligned.
The problem I've encountered is that ld seems to think that by '. = 0x200000' I mean that I want my file to balloon to 2 megs. The wiki solves this issue in the 'Creating a 64-bit Kernel' tutorial, but this causes my .text section to be located just after the headers, which is not an aligned address. the AT directive doesn't seem to affect this situation at all.
I have come up with a stop-gap measure: gzip my kernel excecutable (which is loaded as a module by grub) and then let grub de-gzip it. This reduces my kernel image to roughly the size it would be if ld was doing what I wanted it to do, but it makes my boot take noticably longer because grub is restoring the gzipped excecutable to its full 2MB size.
Any Ideas?
Setting File Offset of Section/Program Header
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: Setting File Offset of Section/Program Header
Hi,
Is there any chance of seeing your linker script and compile / link command lines?
I have had the problem you describe once in the past and seem to remember that you had to use the -nostdinc directive in the linker command line (as well as the compile command line). Presumably, you've also used . = ALIGN(0x1000).
I've seen your other posts and realised that the next comment is probably a bit patronising, but presumably you are also using an executable format rather than a flat binary?
In case it helps, a section in my linker script looks something like:
Cheers,
Adam
Is there any chance of seeing your linker script and compile / link command lines?
I have had the problem you describe once in the past and seem to remember that you had to use the -nostdinc directive in the linker command line (as well as the compile command line). Presumably, you've also used . = ALIGN(0x1000).
I've seen your other posts and realised that the next comment is probably a bit patronising, but presumably you are also using an executable format rather than a flat binary?
In case it helps, a section in my linker script looks something like:
Code: Select all
.text phys : AT(0x100000)
{
code = .;
*(.text)
. = ALIGN(0x1000);
}
Adam
Re: Setting File Offset of Section/Program Header
Yes, you have some addresses wrong in the linker.
You were lucky with a few megabytes. I made a 3 gigabyte kernel image!
It's just a matter of getting the two addresses right, for the first section. I'm not entirely sure where you want to map the kernel to, however.
With regards to alignment, if you want your kernel's sections or segments to be page-aligned, you can get the linker to do that with
Example of a linker script which links page-enabling startup code at 0x00100000, and the rest at 0xC0000000 (but not using page-alignment): http://repo.or.cz/w/marionette.git?a=bl ... ;hb=master, if that's any help
You were lucky with a few megabytes. I made a 3 gigabyte kernel image!
It's just a matter of getting the two addresses right, for the first section. I'm not entirely sure where you want to map the kernel to, however.
With regards to alignment, if you want your kernel's sections or segments to be page-aligned, you can get the linker to do that with
Code: Select all
. = ALIGN(0x400) ;
Marionette the flexible kernel
Re: Setting File Offset of Section/Program Header
Hi,
Cheers,
Adam
Surely that should be 0x1000 for page alignment.xlq wrote: With regards to alignment, if you want your kernel's sections or segments to be page-aligned, you can get the linker to do that withCode: Select all
. = ALIGN(0x400) ;
Cheers,
Adam
Re: Setting File Offset of Section/Program Header
YesAJ wrote:Surely that should be 0x1000 for page alignment.
I wonder what I was thinking of.
Marionette the flexible kernel
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: Setting File Offset of Section/Program Header
Of course, probably should have included them in the origional post
Linker Script:
Linker command line:
This is called from scons, but I have tried doing it by hand with no change.
Here is my old linker script, which resulted in a small file, but refused to page align my sections within the file.
Linker Script:
Code: Select all
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)
ENTRY(kmain)
BASE = 0x200000;
PHDRS
{
text PT_LOAD;
data PT_LOAD;
bss PT_LOAD;
}
SECTIONS
{
. = BASE;
.text :
{
*(.text)
. = ALIGN(16);
*(.rodata*)
} : text
. = ALIGN(4096);
.data :
{
*(.data)
} : data
. = ALIGN(4096);
.bss :
{
*(.bss)
} : bss
/DISCARD/ :
{
*(.comment)
*(.ehframe*)
*(.eh_frame*)
}
}
Code: Select all
ld -o kernel.elf -T kernel/link.ld --strip-all -nostdlib -z nodefaultlib kernel/kmain.o
Here is my old linker script, which resulted in a small file, but refused to page align my sections within the file.
OUTPUT_FORMAT(elf64-little)
OUTPUT_ARCH(i386:x86-64)
ENTRY(kmain)
BASE = 0x200000;
PHDRS
{
text PT_LOAD;
data PT_LOAD;
bss PT_LOAD;
}
SECTIONS
{
. = BASE;
.text : AT(ALIGN(ADDR(.text)-BASE))
{
*(.text)
. = ALIGN(16);
*(.rodata*)
} : text
. = ALIGN(4096);
.data : AT(ADDR(.data)-BASE)
{
*(.data)
} : data
. = ALIGN(4096);
.bss : AT(ADDR(.bss)-BASE)
{
*(.bss)
} : bss
/DISCARD/ :
{
*(.comment)
*(.ehframe*)
*(.eh_frame*)
}
}
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: Setting File Offset of Section/Program Header
Hackish solution to the rescue!
It turns out I can link x86-64 code into an elf32-i386 file. My file is now only a few KB, properly aligned, and there don't seem to be any negative consequences.
It turns out I can link x86-64 code into an elf32-i386 file. My file is now only a few KB, properly aligned, and there don't seem to be any negative consequences.
Re: Setting File Offset of Section/Program Header
If it's still an issue after the necro and you want a non-hackish solution just call ld with the -n argument. It balloons because all data is page aligned in the output object(2/4 mb pages)
http://j-software.dk | JPasKernel - My Object Pascal kernel