LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting error in Hellow World Kernel: Error 13: Invalid or unsupported executable for (https://www.linuxquestions.org/questions/programming-9/getting-error-in-hellow-world-kernel-error-13-invalid-or-unsupported-executable-for-574246/)

piyush.kansal 08-02-2007 11:23 AM

Getting error in Hellow World Kernel: Error 13: Invalid or unsupported executable for
 
I was trying to make a hello world kernel as follows:
kernel.asm
Code:

;States that the code is a 32 bit code
[BITS 32]
[global _start]

;Calls an external C function os_main.
[extern os_main]

_start:
        call os_main

;Turns off interrupts
cli

;Halts the CPU
hlt

I compiled this code as follows:
nasm -f aout kernel.asm -o kernel_a.o

It worked fine.
Then I wrote this file:
kernel.h
Code:

#ifndef _KERNEL_H_
#define _KERNEL_H_

#define TOTAL_ROWS                      25
#define TOTAL_COLUMNS                  80
#define CHAR_SIZE                      2
#define VIDEOMEM_ADD                    0xb8000
#define WHITE_TEXT_ON_BLACK_SCREEN      0x07

#endif

kernel.c
Code:

#include "kernel.h"

void cls();
int print( char *msg, unsigned int line );

os_main()
{
        cls();
        print("Hello World!!\n", 0);
}

void cls()
{
        char *videomemory = (char *)VIDEOMEM_ADD;
        unsigned int i = 0;
       
        while( i < (TOTAL_COLUMNS * TOTAL_ROWS * CHAR_SIZE) )
        {
                videomemory[i] = ' ';
                ++i;
                videomemory[i] = WHITE_TEXT_ON_BLACK_SCREEN;
                ++i;
        }
}

int print( char *msg, unsigned int line )
{
        char *videomemory = (char *)VIDEOMEM_ADD;
        unsigned int i = line * TOTAL_COLUMNS * CHAR_SIZE;

        while( *msg != 0 )
        {
                if( '\n' == *msg)
                {
                        ++line;
                        i = line * TOTAL_COLUMNS * CHAR_SIZE;
                        ++msg;
                }
                else
                {
                        videomemory[i] = *msg;
                        ++msg;
                        ++i;
                        videomemory[i] = WHITE_TEXT_ON_BLACK_SCREEN;
                        ++i;
                }
        }

        return 1;
}

I compiled this as follows:
gcc -c kernel.c

Then I linked the both the .o files:
ld kernel.o kernel_a.o -o kernel.bin

Then I copied this to /boot folder and created a new entry in GRUB as follows along with existing one:
title Fedora Core 1 (2.4.22-1.2115.nptl)
root (hd0,6)
kernel /vmlinuz-2.4.22-1.2115.nptl ro root=LABEL=/ hdc=ide-scsi rhgb
initrd /initrd-2.4.22-1.2115.nptl.img
title Windows XP Professional SP2
rootnoverify (hd0,0)
chainloader +1
title My Kernel
root (hd0,6)
kernel /vmlinuz.mykernel

and I booted the kernel with My Kernel. At that time it gave me the error as:
Error 13: Invalid or unsupported executable format

I am not able to get, why this error is coming. I also tried searching but could not get much info. Please assist.

ta0kira 08-02-2007 02:15 PM

I'm pretty sure when compiling a kernel you need special compilation options for gcc. This is because normal executables don't directly access the hardware; they are interpreted through the kernel. It has to be compiled as something that can run the machine itself. I don't know exactly how to do this, however. I've never written a kernel, but I know even modules can't be compiled like normal ELF programs.
ta0kira

exvor 08-02-2007 03:02 PM

Actually doing this was what prompted me to try and start learning assembly. Yes T is correct you need to compile the c program with special gcc stuff so that it does not link to standard library.

Below is from a online resource regarding this its actually a very good resource.

Code:

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c

Got from http://www.osdever.net/bkerndev/index.php

piyush.kansal 08-04-2007 01:26 AM

Hmmm, OK I will go through the website and will try. Thanks for the info.


All times are GMT -5. The time now is 01:37 PM.