OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 7:06 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: 64 Bit Kernel Linker Script
PostPosted: Thu Jul 31, 2014 7:25 pm 
Offline
Member
Member

Joined: Fri Jan 31, 2014 8:21 am
Posts: 32
I am trying to make a 64-bit kernel, and so, I looked at: http://wiki.osdev.org/Creating_a_64-bit_kernel
I am using GRUB2, so, I looked at the section about the 32-bit bootstrap, and it gave the following snippet for the linker script:

Code:
...
ENTRY(bootstrap)
...
SECTIONS
{
    . = KERNEL_LMA;

    .bootstrap :
    {
        <path of bootstrap object> (.text)
    }

    . += KERNEL_VMA;

    .text : AT(ADDR(.text) - KERNEL_VMA)
    {
        _code = .;
        *(EXCLUDE_FILE(*<path of bootstrap object>) .text)
        *(.rodata*)
        . = ALIGN(4096);
    }
...

However, what exactly is <path of bootstrap object>?


Top
 Profile  
 
 Post subject: Re: 64 Bit Kernel Linker Script
PostPosted: Thu Jul 31, 2014 11:03 pm 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
You could have read the manual to learn the link script format.

Anyway, its *filename*, for example, my own link script I put the bootstrap on the beginning of text section:
Code:
.text : AT(ADDR(.text) - KERNEL_VMA)
    {
        *bootstrap*(.text)
        *(.text)
        *(.gnu.linkonce.t*)
        . = ALIGN(4096);
    }


and I have bootstrap.o to link with.


Top
 Profile  
 
 Post subject: Re: 64 Bit Kernel Linker Script
PostPosted: Fri Aug 01, 2014 1:48 pm 
Offline
Member
Member

Joined: Sun Sep 06, 2009 3:54 am
Posts: 169
Location: Brighton, United Kingdom
The ld scripting language is simpler than it looks.

You'll need to specify ENTRY(), which is your entry point (mine is a function called start32) and then possibly OUTPUT_FORMAT. The next thing you need is the SECTIONS. Here, you create the layout of your executable's segments and which sections to put in each segment. For example, my bootstrap header segment looks like this:
Code:
. = PHYS_ADDR;
.boot : {
    *(.multiboot)
    __boot_start__ = .;
    *(.boot_text)
    *(.boot_rodata)
    *(.boot_data)
    . = ALIGN(4096);
    __pml4__ = .;
    . += 0x1000;
    __pdpt__ = .;
    . += 0x1000;
    __pdir__ = .;
    . += 0x2000;
    __boot_stack__ = .;
    __boot_end__  = .;
}

What this does is it tells ld to set the current relocation (the . variable) to PHYS_ADDR (which in my case, as with a lot of kernels, is 0x100000). You can think of this as similar to the ORG directive most assemblers have, except you can change it further down the file. It's also similar to NASM's $ variable except that it's writeable. Next I'm specifying that the .boot segment goes first. This is because my kernel is for x86_64 but GRUB 2 doesn't set up long mode, so I have to have 32-bit code first. In the .boot segment, the first section is .multiboot so that the multiboot header is right at the start of the file (immediately after the ELF header) because multiboot bootloaders only search the first 32 kiB of your file. The next line is defining a symbol which is placed at the given address (which here would be PHYS_ADDR plus the length of the .multiboot section). ld script symbols are different to variables in C because they're literally just named addresses; they don't have a value. When you want to use the symbol, you must take its address, e.g. &__boot_start__ is the address of the start of my .boot_text section. The value of __boot_start__ is whatever happens to be at that address. After that there are some more sections and then I tell ld to align the . variable to a page boundary. After that there are more symbols which are one page (4 kiB) apart from each other (except that the stack is 2 kiB after __pdir__).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 86 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group