GRUB and ELF

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
Vladaz

GRUB and ELF

Post by Vladaz »

I have a problem, I can't link my kernel files.

Linker:
OUTPUT_FORMAT("elf32-i386")
ENTRY(entry)
virt = 0xC0000000; /* 3 gig */
phys = 0x100000; /* 1 meg */
SECTIONS
{ .text virt : AT(phys)
{ code = .;
*(.text)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{ data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{ bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4096);
}
end = .;
}

makefile:
SRC = kernel.c

OBJ = kernel_asm.o kernel.o

all:   asm link delfiles
asm:
   nasmw -f elf kernel_asm.asm

   gcc -c $(SRC)
link:
   ld-elf -T link.ld --cref -Map krnl.map -o ktnl.elf $(OBJ)

delfiles:
   del *.o

kernel_asm.asm:
[BITS 32]
[GLOBAL start]
[EXTERN _k_main]

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

start:

   call _k_main

   cli
   hlt


kernel.c:
k_main () {
   
   while(1);
}

That is shown when i execute my makefile.
Error message:
nasmw -f elf kernel_asm.asm
gcc -c kernel.c
ld-elf -T link.ld --cref -Map krnl.map -o ktnl.elf
Exiting due to signal SIGSEGV
General Protection Fault at eip=00067ac7
eax=fff50200 ebx=00000007 ecx=000002b5 edx=fff50000 esi=00000200 edi=00000006
ebp=000b989c esp=000b988c program=L☺♥
cs: sel=0387 base=02b50000 limit=7d49ffff
ds: sel=038f base=02b50000 limit=7d49ffff
es: sel=038f base=02b50000 limit=7d49ffff
fs: sel=035f base=00014160 limit=0000ffff
gs: sel=039f base=00000000 limit=ffffffff
ss: sel=038f base=02b50000 limit=7d49ffff

Call frame traceback EIPs:
0x00067ac7
0x00067a4d
0x00067136
0x000671ed
make.exe: *** [link] Error -1
AR

Re:GRUB and ELF

Post by AR »

That's a problem with your linker build. What OS and Binutils version are you using? You may want to rebuild binutils.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:GRUB and ELF

Post by Pype.Clicker »

i'd say you're missing some sections (.rodata ?) in your linker script. that means your linker may be trying to create a 3GB file to please you ... and that may not please the OS.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GRUB and ELF

Post by Solar »

Since your code doesn't seem to do anything beyond the most basic setup, you might want to check the HigherHalfBareBones and see if that works for you.
Every good solution is obvious once you've found it.
Vladaz

Re:GRUB and ELF

Post by Vladaz »

i've downloaded my binutils from here http://membres.lycos.fr/placr/binutils.html
There are special binutils for making ELF, because, i think default ld file doesn't support elf, so I thought to use these.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GRUB and ELF

Post by Solar »

Please consider setting up a GCC Cross-Compiler environment. That's well-tested and works for many of us.
Every good solution is obvious once you've found it.
Post Reply