Issue with linker.ld in "User /Bare Bones" Tutorial – Incorrect Section Order for Multiboot and .text in ELF Kernel

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
jjzhai
Posts: 1
Joined: Thu Sep 26, 2024 11:31 pm

Issue with linker.ld in "User /Bare Bones" Tutorial – Incorrect Section Order for Multiboot and .text in ELF Kernel

Post by jjzhai »

Content:
In the "User
/Bare Bones" tutorial within the Kernel Basics section, there is an issue with the linker.ld file that causes the kernel to not load correctly when following the provided instructions. Specifically, the section placement in the linker.ld file needs to be adjusted for proper execution.

Currently, the tutorial suggests the following structure in the linker.ld file:

Code: Select all

.rodata BLOCK(4K) : ALIGN(4K)
{
    *(.multiboot)
}

.text BLOCK(4K) : ALIGN(4K)
{
    *(.text)
}
However, this configuration does not ensure that the Multiboot header will be located at the start of the executable, which is required for the bootloader (e.g., GRUB) to recognize the kernel as a valid multiboot-compliant kernel.

Solution:
To resolve this issue, the linker.ld file should be modified so that the Multiboot header is placed within the .text section to ensure it appears at the start of the kernel binary. The corrected linker.ld configuration is as follows:

Code: Select all

.text BLOCK(4K) : ALIGN(4K)
{
    *(.multiboot)
    *(.text)
}
Reason:
The Multiboot header must appear within the first 8 KB of the binary and should be placed at the beginning of the .text section to be properly recognized by the bootloader. By combining the .multiboot section with the .text section, we ensure that the Multiboot header is loaded correctly, and the kernel can boot as expected.

This change fixes the kernel loading issue and allows the tutorial to work as intended.
Octocontrabass
Member
Member
Posts: 5501
Joined: Mon Mar 25, 2013 7:01 pm

Re: Issue with linker.ld in "User /Bare Bones" Tutorial – Incorrect Section Order for Multiboot and .text in ELF Kernel

Post by Octocontrabass »

jjzhai wrote: Thu Sep 26, 2024 11:43 pmIn the "User
/Bare Bones" tutorial
You mean this one?
jjzhai wrote: Thu Sep 26, 2024 11:43 pmTo resolve this issue, the linker.ld file should be modified so that the Multiboot header is placed within the .text section to ensure it appears at the start of the kernel binary.
I prefer making the .multiboot section allocatable. Instead of changing linker.ld, change this line of start.s:

Code: Select all

.section .multiboot
To this:

Code: Select all

.section .multiboot, "a"
The linker can't rearrange allocatable sections, so you can ensure the .multiboot section comes first without combining it into the .text section.
Post Reply