Page 1 of 1
Setting File Offset of Section/Program Header
Posted: Sun Feb 08, 2009 9:45 pm
by JohnnyTheDon
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?
Re: Setting File Offset of Section/Program Header
Posted: Mon Feb 09, 2009 3:31 am
by AJ
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:
Code: Select all
.text phys : AT(0x100000)
{
code = .;
*(.text)
. = ALIGN(0x1000);
}
Cheers,
Adam
Re: Setting File Offset of Section/Program Header
Posted: Mon Feb 09, 2009 4:44 am
by xlq
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
Re: Setting File Offset of Section/Program Header
Posted: Mon Feb 09, 2009 5:05 am
by AJ
Hi,
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 with
Surely that should be 0x1000 for page alignment.
Cheers,
Adam
Re: Setting File Offset of Section/Program Header
Posted: Mon Feb 09, 2009 5:29 am
by xlq
AJ wrote:Surely that should be 0x1000 for page alignment.
Yes
I wonder what I was thinking of.
Re: Setting File Offset of Section/Program Header
Posted: Mon Feb 09, 2009 2:50 pm
by JohnnyTheDon
Of course, probably should have included them in the origional post
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*)
}
}
Linker command line:
Code: Select all
ld -o kernel.elf -T kernel/link.ld --strip-all -nostdlib -z nodefaultlib kernel/kmain.o
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.
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*)
}
}
Re: Setting File Offset of Section/Program Header
Posted: Wed Feb 11, 2009 5:56 pm
by JohnnyTheDon
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.
Re: Setting File Offset of Section/Program Header
Posted: Mon Feb 23, 2009 11:43 am
by Laksen
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)