Re-ordering linker sections in ELF file

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
23scurtu
Posts: 8
Joined: Sun May 10, 2020 9:47 pm

Re-ordering linker sections in ELF file

Post by 23scurtu »

I'm working on transfering my barebones OS to 64 bit, and was running into issues getting my multiboot header within the first 8k of my ELF file.

I've read countless posts on the forums suggesting that this is the correct way to put your multiboot header at the beginning (assuming your multiboot header is in a section called .multiboot):

Code: Select all

KERNEL_LOW = 0x100000;

SECTIONS
{
    . = KERNEL_LOW;

   .multiboot ALIGN(4K) : AT (ADDR (.multiboot) - KERNEL_LOW)
   {
      KEEP(*(.multiboot))
   }

   .text ALIGN(4K)  : AT (ADDR (.text) - KERNEL_LOW)
   {
      *(.text)
   } 
}
This does not work at all. When using " x86_64-elf-readelf -a kernel/myos.kernel" its clear that the .text section is the first section in the ELF file at an offset of 0x1000, and the multiboot section is being placed near the end at 0xa400, which is well above the 8k required for the multiboot header to be found.

What I found does work is defining .multiboot in the same section as .text like this:

Code: Select all

KERNEL_LOW = 0x100000;

SECTIONS
{
    . = KERNEL_LOW;

   .text ALIGN(4K)  : AT (ADDR (.text) - KERNEL_LOW)
   {
      KEEP(*(.multiboot))
      *(.text)
   } 
}
Here, .text remains the first section at an offset of 0x1000 and .multiboot is at the beginning of it, allowing the multiboot header to be found.

Is the .text section always the first usable section at the lowest offset in an ELF file? If its not, how can I define the layout of the sections so that I can get a custom .multiboot section (like in my first example) at the beginning of my ELF. I've heard all forms of nonsense on how to get the multiboot section at the beginning of an ELF file on this forum and I want to set it straight here so no one has to go through this again.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Re-ordering linker sections in ELF file

Post by nexos »

Did you pass -z max-page-size=4096 to the linker?
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
23scurtu
Posts: 8
Joined: Sun May 10, 2020 9:47 pm

Re: Re-ordering linker sections in ELF file

Post by 23scurtu »

nexos wrote:Did you pass -z max-page-size=4096 to the linker?
Yeah, I've been passing -z max-page-size=0x1000 to the linker
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Re-ordering linker sections in ELF file

Post by kzinti »

Just keep the multiboot header in the .text section... That's what I do and it works just fine. Is this causing you a problem?
Post Reply