How to build this file to elf format.

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
huxuelei
Member
Member
Posts: 35
Joined: Tue May 27, 2008 8:32 am

How to build this file to elf format.

Post by huxuelei »

Hi, I have written a small program with nasm which display a string on the screen.
Here is my code:

Code: Select all

MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO  equ 1<<1

MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
CHECKSUM               equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; The Multiboot header (in NASM syntax)
align 4
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd CHECKSUM

section .data
BootMessage db "Booting ....", 0
BootMessageLength equ $ - BootMessage
section .text
global _start
_start:
	xor ax, ax
	mov es, ax
	mov ds, ax
	mov ss, ax

	mov bp, BootMessage
	mov cx, BootMessageLength
	mov ax, 01301h
	mov bx, 000Eh
	mov dl, 0
	int 10h
	
	jmp $
First, I compile and link this program like this:

Code: Select all

nasm -f elf -o kernel.o kernel.s
ld -s -Ttext 0x30400 -o kernel.elf kernel.o
But I got a error information like this:

kernel.o(.text+0x15): In function '_start':
kernel.s:relocation truncated to fit:R_386_16 against '.data'


Then, I change the link command to this:

Code: Select all

nasm -f elf -o kernel.o kernel.s
ld -s -Ttext 0 -o kernel.elf kernel.o
At this time, complie and link passed.

I copy the elf file to floppy, ready to use grub to load it to memory.
Here is the grub command I use:

Code: Select all

root (fd0)
kernel /boot.elf
But then I got a error:
Error 7: Loading below 1MB is not supported.

I am so confusing about these.
Why can not use the flag '-Ttext 0x30400' when I link the program?
Why grub say 'Loading below 1MB is not supported'?

Please help me. Thanks.

[Edit by AJ - Code Tags Added]
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post by inflater »

GRUB enters protected mode and you are trying to execute real mode instrunctions. If you want your OS in real mode, I suggest you strip off the multiboot stuff and go here: http://www.osdever.net/downloads.php and download some of real mode bootloaders. And I highly doubt you could make a 16bit elf executable.

I suggest reading the osdev wikipedia.

BTW don't use colors. Especially the dark blue font color in the mtdark2 theme is... disgusting.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
huxuelei
Member
Member
Posts: 35
Joined: Tue May 27, 2008 8:32 am

Post by huxuelei »

inflater wrote:GRUB enters protected mode and you are trying to execute real mode instrunctions. If you want your OS in real mode, I suggest you strip off the multiboot stuff and go here: http://www.osdever.net/downloads.php and download some of real mode bootloaders. And I highly doubt you could make a 16bit elf executable.

I suggest reading the osdev wikipedia.

BTW don't use colors. Especially the dark blue font color in the mtdark2 theme is... disgusting.
Oh, sorry. I just want to make things clearly.But I will not use color in the future.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Hi,

Firstly, as mentioned this forum has [ code ] tags, please use them as it ensures that code is shown in a monospace font and on a nice background for all theme users.

On to your problem - You've got two problems here.

Firstly, your compilation problem. I assume it's because you're not specifying the location for your .data section. I assume that NASM is choosing a default location for it which is outside the range of the 16-bit relocation entries you're using.

So specify a .data start location on your command line.

The second is that you're trying to execute real mode code. You're trying to use BIOS interrupt int 10h, which is real mode only. GRUB loads you in protected mode, so you're going to get into difficulties there. (I.e. nothing will work).

If you want to use GRUB, you'll be in 32-bit protected mode. There are several tutorials out there to help you on that, but if you prefer to stick with 16-bit real mode, you'll have to ditch GRUB - again there are several tutorials to help you there too.

Cheers,

James
huxuelei
Member
Member
Posts: 35
Joined: Tue May 27, 2008 8:32 am

Post by huxuelei »

Hi, I am sorry. I am a new here, I did not know the rule about list the code.

I won't do that any more.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

huxuelei wrote:Hi, I am sorry. I am a new here, I did not know the rule about list the code.
Then would you please read the rules so you know about all the other things as well, thanks.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Combuster wrote:
huxuelei wrote:Hi, I am sorry. I am a new here, I did not know the rule about list the code.
Then would you please read the rules so you know about all the other things as well, thanks.
Combuster, thats OTT - his post was perfectly fine with the exception of colour.

A simple "There are code tags" would suffice IMHO.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

JamesM wrote:but if you prefer to stick with 16-bit real mode, you'll have to ditch GRUB
I don't see why. Just switch back to real mode after some relocation of code, and you should be fine (not that I think that's within the capabilities of the OP, but one could).


JAL
Post Reply