Page 1 of 1

Re-ordering linker sections in ELF file

Posted: Sat Aug 08, 2020 11:59 pm
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.

Re: Re-ordering linker sections in ELF file

Posted: Mon Aug 10, 2020 7:40 am
by nexos
Did you pass -z max-page-size=4096 to the linker?

Re: Re-ordering linker sections in ELF file

Posted: Tue Aug 11, 2020 9:30 pm
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

Re: Re-ordering linker sections in ELF file

Posted: Wed Aug 12, 2020 10:11 pm
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?