Get error trying to compile the kernel

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.
Loufang
Posts: 3
Joined: Mon Sep 03, 2007 4:08 pm

Get error trying to compile the kernel

Post 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
vhg119
Member
Member
Posts: 71
Joined: Fri Aug 24, 2007 5:56 pm
Location: CA, USA

Post by vhg119 »

What happens if you do:
nasmw -f bin kernel.asm -o kernel.o
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

You may have to build a cross compiler. Instructions are here GCC Cross-Compiler.
Loufang
Posts: 3
Joined: Mon Sep 03, 2007 4:08 pm

Post 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)
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 »

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)
"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 ]
Loufang
Posts: 3
Joined: Mon Sep 03, 2007 4:08 pm

Post 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.
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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)
HJED
Member
Member
Posts: 61
Joined: Tue Sep 04, 2007 4:18 am
Location: the world wide web
Contact:

Post 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:
.................................. um what should i put here .............................?..........................................
..... :D................
HJED
Member
Member
Posts: 61
Joined: Tue Sep 04, 2007 4:18 am
Location: the world wide web
Contact:

Post 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?
.................................. um what should i put here .............................?..........................................
..... :D................
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

is it a linux thing?
obviously
HJED
Member
Member
Posts: 61
Joined: Tue Sep 04, 2007 4:18 am
Location: the world wide web
Contact:

Post by HJED »

well how do i do it in windows XP?
or what is the equvilant
plz help :cry:
.................................. um what should i put here .............................?..........................................
..... :D................
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post 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.
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 »

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.
"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 ]
HJED
Member
Member
Posts: 61
Joined: Tue Sep 04, 2007 4:18 am
Location: the world wide web
Contact:

Post 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?
.................................. um what should i put here .............................?..........................................
..... :D................
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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.
Post Reply