newb question: gcc, elf, and grub...

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
bpaterni
Posts: 12
Joined: Sat May 30, 2009 4:38 pm

newb question: gcc, elf, and grub...

Post by bpaterni »

OK, I've looked around on the web for the past 2 hours looking for the answer, but so far I've come up empty.

I'm following Brandon Friesen's kernel dev tutorial and what I'm trying to do is port his kernel start up code from nasm to gas. The program is very simple. All it does is create a stack and jmp into an infinite loop. So here's what my gas syntax looks like:

Code: Select all

#include <start.h>	
.file "start.S"
.text
.code32
.globl _start; 
_start:
	movl $_sys_stack, %esp
	jmp stublet
mboot:
	.long MB_HEADER_MAGIC
	.long MB_HEADER_FLAGS
	.long MB_CHECKSUM
stublet:
loop:
	jmp loop
.bss
	.skip	8192
_sys_stack:
Everything compiles and links okay, but where I'm running into trouble is when grub attempts to load this simple kernel. I get an "invalid or unsupported executable format" error. So I think my problem is that I'm not compiling/linking correctly:

/* remove c stub, set to compile for 32bit i386 architecture */
gcc -I./ -c -nostdlib -nodefaultlibs -m32 -mtune=i386 -o start.o start.S

/* code at 1 MB, use script link.ld (below), use elf_i386 emulation */
ld -Ttext=0x100000 -T link.ld -m elf_i386 -o start start.o

Link script from tutorial:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(_start)
phys = 0x00100000;
SECTIONS
{
	.text phys : AT(phys) {
		code = .;
		*(.text)
		*(.rodata)
		. = ALIGN(4096);
	}
	.data : AT(phys + (data - code))
	{
		data = .;
		*(.data)
		. = ALIGN(4096);
	}
	.bss : AT(phys + (bss - code))
	{
		bss = .;
		*(.bss)
		. = ALIGN(4096);
	}
	end = .;
}
What am I missing if grub doesn't look the above compiled and linked kernel image?

edit: I have also checked the multiboot header with mbchk:

start.elf: The Multiboot header is found at the offset 7.
start.elf: Page alignment is turned on.
start.elf: Memory information is turned on.
start.elf: Address fields is turned off.
start.elf: All checks passed.

So I don't think the problem is caused by an incorrect mb header
tarrox
Posts: 19
Joined: Wed Dec 31, 2008 8:40 am

Re: newb question: gcc, elf, and grub...

Post by tarrox »

First write OUTPUT_FORMAT(elf32-i386) instead of OUTPUT_FORMAT("binary").
Second you don't need -m elf_i386 anymore because the type is defined in the script.
Third you don't need -Ttext=0x100000, because this information is also in the script.
Forth you only have to give -o start.o to ld and not -o start start.o.
bpaterni
Posts: 12
Joined: Sat May 30, 2009 4:38 pm

Re: newb question: gcc, elf, and grub...

Post by bpaterni »

1,2,3: check

4 must be a typo though because if I just enter 'ld -T link.ld -o start.o', then it wants me to pass an input file

But grub still does not load the kernel. My new gcc and ld arguments are as follows:

gcc -I./ -c -nostdlib -nodefaultlibs -o start.o start.S
ld -T link.ld -o start start.o

If I include '-m32 -mtune=i386', then i get the following error when I try and link:
ld: i386 architecture of input file `start.elf.o' is incompatible with i386:x86-64 output
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: newb question: gcc, elf, and grub...

Post by pcmattman »

Are you using a cross-compiler? If not, I highly suggest you build one and use it.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: newb question: gcc, elf, and grub...

Post by frank »

Also doesn't the multiboot header have to be 16 byte aligned?
bpaterni
Posts: 12
Joined: Sat May 30, 2009 4:38 pm

Re: newb question: gcc, elf, and grub...

Post by bpaterni »

Hey, ya thanks for the help!

As it turns out, I did have to build a cross compiler. I thought I could just issue the right commands to gcc and it would make the switch for me, but apparently that's not the case.

Also looking back at my port it seems I've forgot to add the '.align 4' assembler directive before the multiboot header.

Thanks to you guys my kernel now loads and does absolutely nothing useful!
Post Reply