Linker Scripts and Bootloaders

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
karim
Posts: 21
Joined: Mon Jun 23, 2025 8:03 pm

Linker Scripts and Bootloaders

Post by karim »

Let's say I've written a bootloader that fetches the kernel from a specific sector on a hard drive or flash drive. This kernel, when compiled, consists of three files:

The boot.s file, which is responsible for setting up the stack, as any C code requires the stack to be initialized correctly. This file also calls the kernel_main function, which is located in the kernel.c file.

Inside the kernel.c file, there's a function that calls printf("hello").

The implementation of the printf function itself is in a separate file named print.c.

Now, if the bootloader is going to load this compiled kernel (which is made up of these three files) into memory at a specific address, for example, 0x10000, then yes, I absolutely need to create a linker script.

This linker script must explicitly tell the linker that the kernel, composed of these three files, will start at the 0x10000 address. This is crucial because the linker modifies the machine code. For instance, it will replace the symbolic name of the printf("hello") function with a direct CALL instruction to a specific absolute memory address (for example, CALL 0x10020, assuming 0x10020 is the actual memory location of printf relative to the kernel's base address).

Furthermore, I must configure the linker script to ensure that the kernel's execution begins at boot.s, because this is the file that performs the necessary stack setup, allowing the C code to run correctly. is what i said is correct?
Octocontrabass
Member
Member
Posts: 6245
Joined: Mon Mar 25, 2013 7:01 pm

Re: Linker Scripts and Bootloaders

Post by Octocontrabass »

You can include a link to your Reddit post.
karim wrote: Sun Jul 20, 2025 2:11 amFurthermore, I must configure the linker script to ensure that the kernel's execution begins at boot.s, because this is the file that performs the necessary stack setup, allowing the C code to run correctly.
This part depends on the bootloader. You could use a bootloader that performs the stack setup before it jumps to your kernel's entry point.
karim wrote: Sun Jul 20, 2025 2:11 amis what i said is correct?
I think everything else is correct.
Post Reply