LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2007, 11:23 AM   #1
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Rep: Reputation: 15
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.
 
Old 08-02-2007, 02:15 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-02-2007, 03:02 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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
 
Old 08-04-2007, 01:26 AM   #4
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Hmmm, OK I will go through the website and will try. Thanks for the info.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Booting Vista via grub: Invalid or unsupported executable format jot Linux - Kernel 3 01-07-2007 04:03 AM
xen installation error: error 13: invalid or unsupported executable format Niceman2005 Linux - Software 0 01-05-2007 01:51 AM
Error 13 : Invalid or unsupported Executed Format. linuxnewbie82 Linux - Newbie 1 08-01-2006 07:19 AM
Trouble Compiling a simple Hellow World on Solaris 9 with GCC 3.4.2 jitendra64 Solaris / OpenSolaris 4 10-11-2004 02:57 PM
kernel 2.6.3 compile ends in grub error 13: unsupported executable format(?) browny_amiga Linux - General 4 03-06-2004 02:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration