Page 1 of 1

problem with higher half kernel and grub

Posted: Mon Feb 13, 2006 9:55 am
by McZ
I have restarted with my OS development but now try to use GRUB instead of making my own..

So I have tried to create my new kernel using the OS FAQ but I can't compile it.

this is the source just cut and paste from http://www.osdev.org/osfaq2/index.php/HigherHalfWithGdt but I can't compile it.

Here is my makefile

Code: Select all

ASM=nasm -f elf
GCC=/usr/cross/bin/i586-elf-gcc
LD=/usr/cross/bin/i586-elf-ld
CFLAGS=-Wall -Werror -nostdlib -nostartfiles -nodefaultlibs

all: kernel.bin

loader.o : loader.asm
   $(ASM) loader.asm -o loader.o 
   
gdt.o : gdt.c
   $(GCC) $(CFLAGS) -o gdt.o -c gdt.c
   
paging.o : paging.c
   $(GCC) $(CFLAGS) -o paging.o -c paging.c
   
kernel.o : kernel.c
   $(GCC) $(CFLAGS) -o kernel.o -c kernel.c
   
kernel.bin : kernel.o gdt.o paging.o loader.o
   $(LD) -T linker.ld -o kernel.bin loader.o kernel.o

clean:
   rm -rf *.o
   rm -rf *.bin
I'm using cygwin gcc and nasm in windows XP and my clean crosscompiled GCC compiled by using the crosscompile section in the FAQ.

This is the error I get.

Code: Select all

$ make
/usr/cross/bin/i586-elf-ld -T linker.ld -o kernel.bin loader.o kernel.o
loader.o: In function `gdt_flush':
loader.asm:(.text+0x37): undefined reference to `gp'
make: *** [kernel.bin] Error 1
as you can se in the FAQ example code gp is defined in the gdt.c

Re:problem with higher half kernel and grub

Posted: Mon Feb 13, 2006 10:13 am
by nick8325
I wonder if adding -fno-leading-underscore to CFLAGS would work. Without it, some versions of GCC will put an underscore at the front of every symbol - for example gp will become _gp.

Re:problem with higher half kernel and grub

Posted: Mon Feb 13, 2006 10:16 am
by nick8325
Actually, that would produce an error for kmain as well...so I don't think it can be that.

Can you run objdump -t gdt.o and see what it says? That should tell you what GCC has called all the symbols. objdump -r loader.o might help too, that will tell you what nasm expects the symbols to be called.

Re:problem with higher half kernel and grub

Posted: Mon Feb 13, 2006 1:50 pm
by McZ
Here is the output from objdump for gdt.o and loader.o

Code: Select all

gdt.o:     file format elf32-i386

SYMBOL TABLE:
00000000 l    df *ABS*  00000000 gdt.c
00000000 l    d  .text  00000000 .text
00000000 l    d  .data  00000000 .data
00000000 l    d  .bss   00000000 .bss
00000000 l    d  .comment       00000000 .comment
00000000 g     F .text  00000094 gdt_set_gate
00000018       O *COM*  00000001 gdt
00000094 g     F .text  00000062 gdt_install
00000006       O *COM*  00000001 gp
00000000         *UND*  00000000 gdt_flush

Code: Select all

loader.o:     file format elf32-i386

SYMBOL TABLE:
00000000 l    df *ABS*  00000000 loader.asm
00000000 l    d  *ABS*  00000000
00000000 l    d  .text  00000000 .text
00000000 l    d  .setup 00000000 .setup
00000000 l    d  .bss   00000000 .bss
00000001 l       *ABS*  00000000 MULTIBOOT_PAGE_ALIGN
00000002 l       *ABS*  00000000 MULTIBOOT_MEMORY_INFO
1badb002 l       *ABS*  00000000 MULTIBOOT_HEADER_MAGIC
00000003 l       *ABS*  00000000 MULTIBOOT_HEADER_FLAGS
e4524ffb l       *ABS*  00000000 MULTIBOOT_CHECKSUM
00000000 l       .text  00000000 multiboot_header
00000028 l       .text  00000000 higherhalf
00000050 l       .text  00000000 flush2
00000000 l       .setup 00000000 trickgdt
00000006 l       .setup 00000000 gdt
0000001e l       .setup 00000000 gdt_end
00001000 l       .bss   00000000 sys_stack
00000000         *UND*  00000000 kmain
0000000c g       .text  00000000 start
00000000         *UND*  00000000 gp
00000034 g       .text  00000000 gdt_flush
I have cleaned the kernel project (deleted all files not needed for compiling like source makefile) and recompiled it and now I get this:

This is with -fno-leading-underscore

Code: Select all

$ make
/usr/cross/bin/i586-elf-gcc -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
 -fno-leading-underscore -o kernel.o -c kernel.c
/usr/cross/bin/i586-elf-gcc -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
 -fno-leading-underscore -o gdt.o -c gdt.c
/usr/cross/bin/i586-elf-gcc -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
 -fno-leading-underscore -o paging.o -c paging.c
nasm -f elf loader.asm -o loader.o
/usr/cross/bin/i586-elf-ld -T linker.ld -o kernel.bin loader.o kernel.o
loader.o: In function `gdt_flush':
loader.asm:(.text+0x37): undefined reference to `gp'
kernel.o: In function `kmain':
kernel.c:(.text+0xc1): undefined reference to `init_paging'
kernel.c:(.text+0xc6): undefined reference to `gdt_install'
make: *** [kernel.bin] Error 1
and this is without -fno-leading-underscore

Code: Select all

$ make
/usr/cross/bin/i586-elf-gcc -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
 -o kernel.o -c kernel.c
/usr/cross/bin/i586-elf-gcc -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
 -o gdt.o -c gdt.c
/usr/cross/bin/i586-elf-gcc -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
 -o paging.o -c paging.c
nasm -f elf loader.asm -o loader.o
/usr/cross/bin/i586-elf-ld -T linker.ld -o kernel.bin loader.o kernel.o
loader.o: In function `gdt_flush':
loader.asm:(.text+0x37): undefined reference to `gp'
kernel.o: In function `kmain':
kernel.c:(.text+0xc1): undefined reference to `init_paging'
kernel.c:(.text+0xc6): undefined reference to `gdt_install'
make: *** [kernel.bin] Error 1

Re:problem with higher half kernel and grub

Posted: Mon Feb 13, 2006 2:06 pm
by nick8325
Um. Your linker command doesn't include gdt.o :)

You might want to have something like
OBJFILES=kernel.o paging.o gdt.o loader.o
in your Makefile, and then replace the rules for compiling the .o files with

%.o : %.c
$(GCC) $(CFLAGS) -o $(@) -c $(<)
%.o : %.asm
$(ASM) $(<) -o $(@)

which will tell make that it can produce a .o file from a .c file or a .asm file by running the above commands. Then just change your kernel.bin line so it says
kernel.bin : $(OBJFILES)
and then to tell make about new source files you will just have to add the .o files to the OBJFILES line.

Re:problem with higher half kernel and grub

Posted: Mon Feb 13, 2006 3:06 pm
by McZ
Thank you :)

I have made the changes to the makefile and now it works :)