Page 1 of 4

Get error trying to compile the kernel

Posted: Mon Sep 03, 2007 4:23 pm
by Loufang
Hi
I'm pretty new to OS Developement, but I'm quite interested and I'm trying to start with a simple kernel, which isn't made by me, but which I copied from the tutorial in OS Developing from http://www.osdever.net/bkerndev/index.php
I can compile it, but I can't link it correctly, so whenever I try to link it I get the error message:
kernel.o: file not recognized: File format not recognized
Actually I don't really understand what the problem could be, since I followed the tutorial step by step. I'm using NASM and LD GNU Linker(latest version I found on the net). Anyway, I'll post here the ASM code I'm using and my makefile.

kernel.asm:

Code: Select all

; This is the kernel's entry point. We could either call main here,
; or we can use this to setup the stack or other nice stuff, like
; perhaps setting up the GDT and segments. Please note that interrupts
; are disabled at this point: More on interrupts later!
[BITS 32]
global start
start:
    mov esp, _sys_stack     ; This points the stack to our new stack area
    jmp stublet

; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
    ; Multiboot macros to make a few lines later more readable
    MULTIBOOT_PAGE_ALIGN	equ 1<<0
    MULTIBOOT_MEMORY_INFO	equ 1<<1
    MULTIBOOT_AOUT_KLUDGE	equ 1<<16
    MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
    EXTERN code, bss, end

    ; This is the GRUB Multiboot header. A boot signature
    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM
    
    ; AOUT kludge - must be physical addresses. Make a note of these:
    ; The linker script fills in the data for these ones!
    dd mboot
    dd code
    dd bss
    dd end
    dd start

; This is an endless loop here. Make a note of this: Later on, we
; will insert an 'extern _main', followed by 'call _main', right
; before the 'jmp $'.
stublet:
    jmp $


; Shortly we will add code for loading the GDT right here!


; In just a few pages in this tutorial, we will add our Interrupt
; Service Routines (ISRs) right here!



; Here is the definition of our BSS section. Right now, we'll use
; it just to store the stack. Remember that a stack actually grows
; downwards, so we declare the size of the data before declaring
; the identifier '_sys_stack'
SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
_sys_stack:
Makefile:

Code: Select all

echo Now assembling, compiling, and linking your kernel:
nasmw -f aout kernel.asm -o kernel.o
rem Remember this spot here: We will add 'gcc' commands here to compile C sources
rem This links all your files. Remember that as you add *.o files, you need to
rem add them after start.o. If you don't add them at all, they won't be in your kernel!
ld -T link.ld -o kernel kernel.o
echo Done!
pause
Reading again the makefile, I just noticed that actually I'm using nasmw...and I don't know if it's the right assembler or if I should find another one...Because the 'w' may stand for windows, which could be the problem. Is it so? Actually I can't verify by myself because it's late and I'm incredibly tired, so I'll check tomorrow this theory.
Any help is welcome :D

Posted: Mon Sep 03, 2007 5:30 pm
by vhg119
What happens if you do:
nasmw -f bin kernel.asm -o kernel.o

Posted: Mon Sep 03, 2007 5:41 pm
by frank
You may have to build a cross compiler. Instructions are here GCC Cross-Compiler.

Posted: Tue Sep 04, 2007 12:21 am
by Loufang
What happens if you do:
nasmw -f bin kernel.asm -o kernel.o
I tried too, I get an error message from NASM:
kernel.asm:31: error: binary output format does not support external references
kernel.asm:32: error: binary output format does not support external references
kernel.asm:33: error: binary output format does not support external references
You may have to build a cross compiler. Instructions are here GCC Cross-Compiler.
Ok, I'm gonna try that now.

EDIT:
Frank I read the guide but now I haven't time to compile the cross-compiler, so I don't know if I'm gonna have problems. Anyway, I read about Cygwin: it's necessary to have it or it's possible to compile everything without it? (It may sound a stupid question since I read the wiki, but since I'm not a pro at english I miss that part)

Posted: Tue Sep 04, 2007 12:44 pm
by Combuster
The trick is to use the file format your linker uses. Instead of using a.out you can try another one. One of these variants should work for your linker.

Code: Select all

-f aout
-f elf
-f coff
-f win32
-f rdf
-f bin is _wrong_ as this generates a flat binary which can not hold any information the linker would need. (the errors are the result of that)

Posted: Wed Sep 05, 2007 1:55 am
by Loufang
The trick is to use the file format your linker uses. Instead of using a.out you can try another one. One of these variants should work for your linker.

Code: Select all

-f aout 
-f elf 
-f coff 
-f win32 
-f rdf 
-f bin is _wrong_ as this generates a flat binary which can not hold any information the linker would need. (the errors are the result of that)
Combuster I already tried all the possible formats (I read them from DOS prompt of NASM), but none works. On some formats I get the error:
ld: cannot perform PE operations on non PE output file 'kernel.bin'
Maybe there is a way to change the format of output file of linker, or something which may make it PE anyway? (What does PE mean by the way?)

I'm also trying to compile the cross compiler, using Cygwin, but I have problems when, following the guide in OSDev Wiki (http://www.osdev.org/wiki/GCC_Cross-Compiler), I try to build binutils:
cd /usr/src/build-binutils
../binutils-x.xx/configure --target=$TARGET --prefix=$PREFIX --disable-nls
make all
make install
It starts the app 'configure', but this is what I get:
$ ../binutils-2.18/configure
loading cache ./config.cache
checking host system type... i686-pc-cygwin
checking target system type... i586-elf
checking build system type... i686-pc-cygwin
checking for a BSD compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for gcc... no
checking for cc... no
configure: error: no acceptable cc found in $PATH
Then I checked the variable $PATH to see what it was...:
usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/programmi/pc
Actually since I don't know what cc stands for...I can't really solve this problem.

Posted: Wed Sep 05, 2007 4:16 am
by urxae
Loufang wrote:
$ ../binutils-2.18/configure
loading cache ./config.cache
checking host system type... i686-pc-cygwin
checking target system type... i586-elf
checking build system type... i686-pc-cygwin
checking for a BSD compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for gcc... no
checking for cc... no
configure: error: no acceptable cc found in $PATH
Then I checked the variable $PATH to see what it was...:
usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/programmi/pc
Actually since I don't know what cc stands for...I can't really solve this problem.
That would be the C compiler. Cygwin includes a setup.exe (IIRC), use it to install gcc.
You need a regular C compiler to compile your cross-compiler...
(By the way, if you only use nasm + ld, you can probably stop after compiling cross-binutils; you shouldn't need cross-gcc)

Posted: Wed Sep 05, 2007 2:19 pm
by HJED
i also have the same problem
but i use windows and the cross compile tutorial is clearly for linux :cry:
is there one 4 windows becouse i am no linux expert?
also is there anywhere i can get one whithout building it becose i will probley not be avile to do it
:cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry:

Posted: Wed Sep 05, 2007 3:23 pm
by HJED
i tried to follow the linux tutorial
../binutils-x.xx/configure --target=$TARGET --prefix=$PREFIX --disable-nls
and yes i do no what the x stand for and stuf
but my comand promt dose no reconize this comand
is it a linux thing?

Posted: Wed Sep 05, 2007 4:02 pm
by Pyrofan1
is it a linux thing?
obviously

Posted: Wed Sep 05, 2007 4:07 pm
by HJED
well how do i do it in windows XP?
or what is the equvilant
plz help :cry:

Posted: Wed Sep 05, 2007 6:13 pm
by frank
HJED wrote:well how do i do it in windows XP?
or what is the equvilant
plz help :cry:
You can use Cygwin. You just need to make sure you get GCC and binutils, flex, bison, make, and automake. The instructions are mostly the same. The x.xx is the version number. When you extract the contents of the archive that contains the source there should be a directory that looks like binutils-x.xx with numbers for the Xs.

Posted: Thu Sep 06, 2007 12:08 am
by Combuster
HJED wrote:the cross compile tutorial is clearly for linux
READ READ READ READ and READ AGAIN :cry:

It tells you to use (among others) Cygwin, which is a Windows program.

Posted: Thu Sep 06, 2007 2:23 am
by HJED
C:\cross\binutils-2.17.50-20060824-1-src>configure --target=$TARGET --prefix=$PR
EFIX --disable-nls
'configure' is not recognized as an internal or external command,
operable program or batch file.

C:\cross\binutils-2.17.50-20060824-1-src>
c
am i doing something wrong or dose this just not work in windows
if so what do i do instead?
i use MinGW as my c c++ ld tool is this why it is not working?

Posted: Thu Sep 06, 2007 2:54 am
by urxae
HJED wrote:am i doing something wrong or dose this just not work in windows
if so what do i do instead?
i use MinGW as my c c++ ld tool is this why it is not working?
You can't run configure from a Windows (or DOS) command prompt.
If you're using MinGW you should try installing MSYS (download here) and using the included shell to run it.