[SOLVED]Problem when linking bootloader and kernel.

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.
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

[SOLVED]Problem when linking bootloader and kernel.

Post by bashcommando »

Well, I tried to link my two binaries together and...

Code: Select all

boot.o: In function `skip':
boot.asm:(.sfbootloader+0x1e): relocation truncated to fit: R_386_16 against `.sfbootloader'
boot.asm:(.sfbootloader+0x29): relocation truncated to fit: R_386_16 against `.sfbootloader'
The code in question is:

Code: Select all

skip:
	cli
	lgdt [gdtr]
	mov eax, cr0
	or al, 1
	mov cr0, eax
	jmp 08h:pmode
BITS 32
pmode:
	extern kmain
	call kmain
	jmp $
Maybe it has to do with the gdt or protected bit?
Here is my GDT:

Code: Select all

gdt: dw 0, 0, 0, 0
codesel: dw 207, 39424, 0, 65535
datasel: dw 207, 37376, 0, 65535
gdt_end:
gdtr: dw gdt_end - gdt - 1
      dd gdt
Also I added my own text section called .sfbootloader but that didn't seem to effect anything. Help?
Last edited by bashcommando on Fri Jan 30, 2015 6:58 am, edited 1 time in total.
Building an operating system is like building an airplane, you don't want it to crash.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Problem when linking bootloader and kernel.

Post by BrightLight »

Actually, a boot loader loads and executes a kernel. A kernel is not usually linked to a boot loader. Try parsing a file system instead, load your kernel to memory and jump/call its entry point.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: Problem when linking bootloader and kernel.

Post by bashcommando »

omarrx024 wrote:Actually, a boot loader loads and executes a kernel. A kernel is not usually linked to a boot loader. Try parsing a file system instead, load your kernel to memory and jump/call its entry point.
Noted for future reference.
Building an operating system is like building an airplane, you don't want it to crash.
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Problem when linking bootloader and kernel.

Post by no92 »

omarrx024 basically told you what you have to do. I'm a little bit worried about the fact that you have to ask this question - do you have to knowledge you have to have to be able to program such a thing?
omarrx024 wrote:A kernel is not usually linked to a boot loader.
I'd even say that a kernel is never linked against the bootloader.

Typically, for a bootloader (assuming a BIOS environment), the first stage (which is 512 bytes big) gets loaded first. It only reads the second one from the disk and sets ip accordingly. The second stage does all the fancy stuff - like reading the file system, loading the kernel into memory and passing execution to it.

A bootloader is a little OS of its own. Many OSdevers here once wanted to create their own bootloader, but stopped it in order to be able to do the more exciting kernel development instead. Maybe that's something to think about for you - relying on GRUB sounds scary for a lot of newbies (for a reason I haven't understood yet), but it's not as bad as you think it is. Having Linux or a BSD is crucial for that (GRUB doesn't run on Windows).
psychobeagle12
Member
Member
Posts: 41
Joined: Wed Oct 26, 2011 9:31 am

Re: Problem when linking bootloader and kernel.

Post by psychobeagle12 »

I had an issue like this once myself. If you google it you should find the solution. Here is the code that caused my problem:

Code: Select all

push byte _someLabel
Notice what I did wrong? The 'byte' specifier was trying to truncate the 32-bit address to 8 bits. The linker was complaining because I was creating an 8 bit value from the 32-bit value. Don't be surprised if you find your issue somewhere other than the code you posted, because I don't see any such problem in what you have there. I'll say again that you should not be trying to link your kernel to your bootloader, but I don't believe that this is why you were getting this particular warning. I think it has to do with the truncation of a 32-bit address to a smaller size, specifically a 16-bit value.
My i386-based kernel: https://github.com/bmelikant/missy-kernel
Picking a name for my kernel was harder than picking my wife, so I just used her name until I decide!
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem when linking bootloader and kernel.

Post by iansjack »

Having Linux or a BSD is crucial for that (GRUB doesn't run on Windows).
That is incorrect.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Problem when linking bootloader and kernel.

Post by BrightLight »

bashcommando wrote:Noted for future reference.
Take a look at Required Knowledge in the wiki. And what I told you is not even needed for reference; it is just part of the standard boot process.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Problem when linking bootloader and kernel.

Post by BrightLight »

no92 wrote:Having Linux or a BSD is crucial for that (GRUB doesn't run on Windows).
GRUB can chain load Windows and other non-multiboot compliant OSes.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem when linking bootloader and kernel.

Post by iansjack »

Of course Grub can chainload Windows, but that's not what no92 was talking about. He said that it doesn't run on Windows; it does.
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: Problem when linking bootloader and kernel.

Post by bashcommando »

no92 wrote:Maybe that's something to think about for you - relying on GRUB sounds scary for a lot of newbies (for a reason I haven't understood yet), but it's not as bad as you think it is.
Unfortunatly I have already used grub enough times to know that I don't want it packaged in my OS(because seriously who wants a bootloader bigger than thier own OS?). I have had to recover linux a bunch of times after messing around with the kernel. So I am interested mostly in the bootloader.
omarrx024 wrote:Actually, a boot loader loads and executes a kernel. A kernel is not usually linked to a boot loader.
Let's just call it a unusual OS then, it's only temporary.
psychobeagle12 wrote:If you google it you should find the solution.
No solution which is why I am posting it here.
omarrx024 wrote: Take a look at Required Knowledge in the wiki. And what I told you is not even needed for reference; it is just part of the standard boot process.
How many times do I have to look at that page because somebody didn't understand that I want something different than a "Normal OS". I do have knowledge of that, but you don't get what I am doing.
Building an operating system is like building an airplane, you don't want it to crash.
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem when linking bootloader and kernel.

Post by iansjack »

https://www.technovelty.org/c/relocatio ... t-wtf.html

(This is talking about 64-bit code, but the principles are the same.) Use objdump to inspect your object files and the problem should be obvious. The lesson is that if something isn't working right you should use the tools provided to inspect the generated code, or if it's at runtime use a debugger to inspect what is actually happening. That way you will learn a lot more than you will by running to the forums every time you have a silly error. Try to research and work out explanations for yourself rather than always asking others to provide them; if you don't understand the processor you are going to have bigger problems down the line.
you don't get what I am doing
I'm not really convinced that you get what you are doing. It's nothing out of the ordinary; in fact it seems to be based on some standard tutorials.
psychobeagle12
Member
Member
Posts: 41
Joined: Wed Oct 26, 2011 9:31 am

Re: Problem when linking bootloader and kernel.

Post by psychobeagle12 »

So my guess is that the solution I posted was NOT found on google? Cuz' like I said, I had this same problem and googled it, and solved it WITH MY POSTED SOLUTION! Not only did I tell you how to find the solution, I posted it for you!
My i386-based kernel: https://github.com/bmelikant/missy-kernel
Picking a name for my kernel was harder than picking my wife, so I just used her name until I decide!
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: Problem when linking bootloader and kernel.

Post by bashcommando »

psychobeagle12 wrote:So my guess is that the solution I posted was NOT found on google? Cuz' like I said, I had this same problem and googled it, and solved it WITH MY POSTED SOLUTION! Not only did I tell you how to find the solution, I posted it for you!
Umm, you realize that...
1. I am using nasm
2. Google produces certain results for different people
iansjack wrote:Use objdump to inspect your object files and the problem should be obvious. The lesson is that if something isn't working right you should use the tools provided to inspect the generated code, or if it's at runtime use a debugger to inspect what is actually happening.
Thank you, I will try that next time.
Building an operating system is like building an airplane, you don't want it to crash.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Problem when linking bootloader and kernel.

Post by Roman »

Read this.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: Problem when linking bootloader and kernel.

Post by bashcommando »

Roman wrote:Read this.
Appearently I was "Geek loading". :D I am guessing that is bad?
Building an operating system is like building an airplane, you don't want it to crash.
Post Reply