Question about my linker script

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
chris13524
Member
Member
Posts: 45
Joined: Sat Feb 27, 2016 10:52 pm

Question about my linker script

Post by chris13524 »

I was following a tutorial, and copied the linker script they gave me. Down the road, I needed to figure out the memory location of my kernel (as well as the end) so I can remove it from the available memory given to me by GRUB.

The internet suggested adding the lines kernelStart = .; and kernelEnd = .; at the appropriate positions and access them via extern variables from C. I did this and I am not sure where to put kernelEnd.

Could somebody please explain what all the different sections are as well as the best place for kernelEnd?

Code: Select all

/* The bootloader will look at this image and start execution at the symbol
   designated as the entry point. */
ENTRY(_start)

/* Tell where the various sections of the object files will be put in the final
   kernel image. */
SECTIONS
{
	/* Begin putting sections at 1 MiB, a conventional place for kernels to be
	   loaded at by the bootloader. */
	. = 1M;
	kernelStart = .;

	/* First put the multiboot header, as it is required to be put very early
	   early in the image or the bootloader won't recognize the file format.
	   Next we'll put the .text section. */
	.text BLOCK(4K) : ALIGN(4K)
	{
		*(.multiboot)
		*(.text)
	}
	
	kernelEnd = .; // my code works with this here, but I'm not sure if it's the best spot

	/* Read-only data. */
	.rodata BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}

	/* Read-write data (initialized) */
	.data BLOCK(4K) : ALIGN(4K)
	{
		*(.data)
	}

	/* Read-write data (uninitialized) and stack */
	.bss BLOCK(4K) : ALIGN(4K)
	{
		*(COMMON)
		*(.bss)
		*(.bootstrap_stack)
	}

	/* The compiler may produce other sections, by default it will put them in
	   a segment with the same name. Simply add stuff here as needed. */
}
My operating system:
https://github.com/neonorb/aura
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

Re: Question about my linker script

Post by AndrewBuckley »

.text = code
.rodata = read only data
.data = variables initialized(stored in binary)
.bss = variable space not initialized(not stored in binary)

kernelStart and kernelEnd are just location labels.
Does your memory manager need to skip just your code? Then it's fine there.
Do you want to know where the variables are that your code expects to stay fixed(strings)? then track that too.
How about variables that change? I would track that as well somehow.
How about the stuff that needs initialization? It would be wise not to allow your code to trash .bootstrap_stack until you know you no longer need it.
None of this stuff is needed until you actually have a use for the Label.
chris13524
Member
Member
Posts: 45
Joined: Sat Feb 27, 2016 10:52 pm

Re: Question about my linker script

Post by chris13524 »

When I was creating the memory manager, I assumed the map GRUB gave to me was "correct", that being that 1 MB and above was "free". I took that to mean I could write whatever to there. I ended up getting all kinds of weird errors (as I was overwriting my kernel).

I'm also writing my own stdlib (so not using anybody else's code), so if anything here is "standardized", I don't need it.

All I need is to figure out what big chunk of RAM I can read and write from without effecting anything. I don't care about the permissions or whatever :)

What is the bootstrap stack? Where is the stack actually stored? (or is the bootstrap stack and the stack the same?) Currently, I'm just using one "thread", the initial one from kernel_main.
My operating system:
https://github.com/neonorb/aura
chris13524
Member
Member
Posts: 45
Joined: Sat Feb 27, 2016 10:52 pm

Re: Question about my linker script

Post by chris13524 »

It's likely I will say with this "one thread" model for a long time as the design of my OS doesn't require threads (and my complicate things).
My operating system:
https://github.com/neonorb/aura
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Question about my linker script

Post by kzinti »

The map that grubs give you is straight from the BIOS. It does not include your kernel or any memory allocated / used at boot time (such as multiboot structures and modules).

I have no idea why this isn't included, but it isn't.

What you want is put your "kernelEnd" at the end of the linker script (after all sections).

Here is an example:
https://github.com/kiznit/rainbow-os/bl ... ltiboot.ld
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

Re: Question about my linker script

Post by AndrewBuckley »

chris13524 wrote:What is the bootstrap stack? Where is the stack actually stored? (or is the bootstrap stack and the stack the same?) Currently, I'm just using one "thread", the initial one from kernel_main.
Everything in the linker script is involving your own code. You are expected to setup your own stack before you use it. I expect whatever code you copied from created a temporary stack for use while it brings itself online. If you failed to setup a stack then it would be using the stack that grub left in place.
Post Reply