Setting File Offset of Section/Program Header

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
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Setting File Offset of Section/Program Header

Post 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?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Setting File Offset of Section/Program Header

Post 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
xlq
Member
Member
Posts: 36
Joined: Mon Dec 11, 2006 7:51 am

Re: Setting File Offset of Section/Program Header

Post 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! :oops:

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) ;
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
Marionette the flexible kernel
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Setting File Offset of Section/Program Header

Post 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

Code: Select all

. = ALIGN(0x400) ;
Surely that should be 0x1000 for page alignment.

Cheers,
Adam
xlq
Member
Member
Posts: 36
Joined: Mon Dec 11, 2006 7:51 am

Re: Setting File Offset of Section/Program Header

Post by xlq »

AJ wrote:Surely that should be 0x1000 for page alignment.
Yes :oops:

I wonder what I was thinking of.
Marionette the flexible kernel
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Setting File Offset of Section/Program Header

Post by JohnnyTheDon »

Of course, probably should have included them in the origional post :P

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*)
}
}
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Setting File Offset of Section/Program Header

Post 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.
Laksen
Member
Member
Posts: 140
Joined: Fri Nov 09, 2007 3:30 am
Location: Aalborg, Denmark

Re: Setting File Offset of Section/Program Header

Post 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)
http://j-software.dk | JPasKernel - My Object Pascal kernel
Post Reply