[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help-grub Digest, Vol 22, Issue 5

From: Dr. Dov Bulka
Subject: Re: Help-grub Digest, Vol 22, Issue 5
Date: Thu, 12 Nov 2009 11:33:55 -0800 (PST)

At the risk of answering my own qusestion... Here's what I did to run the example kernel from Grub2, as a 64-bit image. Maybe somebofy will find this useful.

There's an example OS kernel that ships with the Grub Legacy tarball, in the docs directory. Grub2 will run that example okay if you compile it as an ELF32 image. I wanted to test Grub2 with an ELF64 image of the example kernel, and that required a few minor, but important changes.

If you compile the example kernel on a "x86_64" platform, you'll get an ELF64 image with 64-bit executable code. The problem is, when Grub jumps to the entry point of the OS, the machine is in 32-bit mode and cannot execute code that was compiled for 64-bit.

The key is to create 32-bit entry point code and stuff it into an ELF64 image.

The example kernel comes in three files: boot.S, kernel.c and multiboot.h. The following changes are required:

  1. The assembler directive ".code32" needs to be added to boot.S.
  2. You also need to compile kernel.c to generate the assemler version kernel.s.
  3. Then you need to add the ".code32" directive to kernel.s
  4. Compile the modified boot.S and kernel.s to create object files.
  5. Finally, compile the object files to create an ELF64 image.

If you want to automate this stuff with a script, start by creating a file called "dot_code32" that contains two lines, the first of which is the string ".code32" The second line is blank.

Then do the following in a script:

cat dot_code32 boot.S > boot32.S
gcc -c boot32.S
gcc -S -m32 -nostdlib kernel.c
cat dot_code32 kernel.s > kernel32.s
gcc -c kernel32.s
gcc -o kernel64 -nostdlib kernel32.o boot32.o

Now you got an ELF64 executable called "kernel64" that runs 32-bit code at the entry point. Grub2 will bootload this one without a problem.

Dov Bulka

--- On Wed, 11/11/09, address@hidden <address@hidden> wrote:

From: address@hidden <address@hidden>
Subject: Help-grub Digest, Vol 22, Issue 5
To: address@hidden
Date: Wednesday, November 11, 2009, 12:03 PM

Send Help-grub mailing list submissions to

To subscribe or unsubscribe via the World Wide Web, visit
or, via email, send a message with subject or body 'help' to

You can reach the person managing the list at

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Help-grub digest..."

Today's Topics:

   1. multiboot kernel example in Grub2 tarball? (Dr. Dov Bulka)


Message: 1
Date: Tue, 10 Nov 2009 18:22:54 -0800 (PST)
From: "Dr. Dov Bulka" <address@hidden>
Subject: multiboot kernel example in Grub2 tarball?
To: address@hidden
Message-ID: <address@hidden>
Content-Type: text/plain; charset="iso-8859-1"

The Grub Legacy tarball included an example multiboot kernel. The sources were in the "docs" directory (boot.S, multiboot.h and kernel.c) This was a 32-bit example.

Does the Grub2 tarball contain such an example kernel?

The example source code from Grub Legacy does not compile on an "x86_64" platform since the assembly language syntax for "x86_64" is different than "i386". Does anyone have a link to such an "x86_64" multiboot kernel example?   


-------------- next part --------------
An HTML attachment was scrubbed...


Help-grub mailing list

End of Help-grub Digest, Vol 22, Issue 5

reply via email to

[Prev in Thread] Current Thread [Next in Thread]