Grub error #13 Invalid or unsupported executable format

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.
Post Reply
SH4773R
Posts: 3
Joined: Mon Apr 16, 2012 5:35 pm

Grub error #13 Invalid or unsupported executable format

Post by SH4773R »

I get grub error 13 when ever I try to boot my kernel using the command "kernel 200+8" I have read this: http://wiki.osdev.org/Grub_Error_13
And made modifications to my project but to no avail. I followed the bare bones and bare bones c++ tutorials

Help would be greatly appreciated!

Here's my loader.asm

Code: Select all

global loader                           ; making entry point visible to linker
 
extern kernel_main                            ; kmain is defined in kmain.cpp
 
extern start_ctors                      ; beginning and end
extern end_ctors                        ; of the respective
extern start_dtors                      ; ctors and dtors section,
extern end_dtors                        ; declared by the linker script

section .__mbHeader
align 0x4
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
 
align 4
    dd MAGIC
    dd FLAGS
    dd CHECKSUM
 
; reserve initial kernel stack space
STACKSIZE equ 0x4000                    ; that's 16k.
 
loader:
    mov  esp, stack + STACKSIZE         ; set up the stack
    push eax                            ; Multiboot magic number
    push ebx                            ; Multiboot info structure
 
    mov  ebx, start_ctors               ; call the constructors
    jmp  .ctors_until_end
.call_constructor:
    call [ebx]
    add  ebx,4
.ctors_until_end:
    cmp  ebx, end_ctors
    jb   .call_constructor
 
    call kernel_main                          ; call kernel proper
 
    mov  ebx, end_dtors                 ; call the destructors
    jmp  .dtors_until_end
.call_destructor:
    sub  ebx, 4
    call [ebx]
.dtors_until_end:
    cmp  ebx, start_dtors
    ja   .call_destructor
 
    cli
.hang:
    hlt                                 ; halt machine should kernel return
    jmp  .hang
 
section .bss
 
align 4
stack:
    resb STACKSIZE                      ; reserve 16k stack on a doubleword boundary
And my linker script:

Code: Select all

OUTPUT_FORMAT(elf32-i386)
ENTRY (loader)

SECTIONS
{
    . = 0x00100000;

   .__mbHeader : {
      *(.__mbHeader)
   }
    

.text ALIGN (0x1000) :
    {
        *(.text)
        *(.gnu.linkonce.t*)
    }

    .rodata ALIGN (0x1000) :
    {
        start_ctors = .;
        *(.ctor*)
        end_ctors = .;

        start_dtors = .;
        *(.dtor*)
        end_dtors = .;

        *(.rodata*)
        *(.gnu.linkonce.r*)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
        *(.gnu.linkonce.d*)
    }

    .bss :
    {
        sbss = .;
        *(COMMON)
        *(.bss)
        *(.gnu.linkonce.b*)
        ebss = .;
    }

    /DISCARD/ :
    {
        *(.comment)
        *(.eh_frame) /* discard this, unless you are implementing runtime support for C++ exceptions. */
    }
}
And the batch file to build it all:

Code: Select all

@echo off
echo Building PIKoS...
echo.
call clean
echo.
echo C++/ASM Compiler And Linker Errors/Warnings:
echo ______________________________________________________________________
i586-elf-g++ -o obj/kernel.o -c kernel.cpp -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
nasm -f elf -o obj/loader.o loader.asm
i586-elf-ld -T link.ld -o bin/kernel.bin obj/kernel.o obj/loader.o --oformat=binary
echo ______________________________________________________________________
if exist "bin/kernel.bin" (
cd bin
echo.
echo Creating Bootable Floppy Image...
makeimage.bat
echo.
echo Build Complete!
pause
exit
) else (
echo Build Failure!
pause
exit
)
I'm sure this is a very noobish question and I am sorry for that ;)
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Grub error #13 Invalid or unsupported executable format

Post by Nessphoro »

Compile as ELF - problem solved.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Grub error #13 Invalid or unsupported executable format

Post by xenos »

Have you already checked the Grub Error 13 wiki page?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
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:

Re: Grub error #13 Invalid or unsupported executable format

Post by Combuster »

Nessphoro wrote:Compile as ELF - problem solved.
Specifically, --oformat=binary contradicts with OUTPUT_FORMAT(elf32-i386). The former is never mentioned in the tutorials which makes me wonder if there are any further deviations to be found...
"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 ]
SH4773R
Posts: 3
Joined: Mon Apr 16, 2012 5:35 pm

Re: Grub error #13 Invalid or unsupported executable format

Post by SH4773R »

Thanks a ton guys! I will try the things you suggested as soon as I get home.

Oh an Xenos, check my first post ;)
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Grub error #13 Invalid or unsupported executable format

Post by xenos »

SH4773R wrote:Oh an Xenos, check my first post ;)
I should have asked whether you understood the wiki page. As the Nessphoro and Combuster pointed out, you need to compile your kernel to an ELF file (remove the --oformat binary from your script), unless you include an a.out kluge in your multiboot header. That's why the wiki entry says:
Wiki wrote:The following is a solution for GrUB's infamous Error 13 pertaining to ELF files.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
SH4773R
Posts: 3
Joined: Mon Apr 16, 2012 5:35 pm

Re: Grub error #13 Invalid or unsupported executable format

Post by SH4773R »

Yeah I get it now, thank you guys for the help. One last thing, when I boot my image it gives me a message stating with Multiboot-elf and my kernel does not run. It says something about my shtab being "bad"? Any pointers?

EDIT:
Duh! I feel stupid, I forgot to type "boot" after loading the kernel, never mind guys!
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Grub error #13 Invalid or unsupported executable format

Post by xenos »

Indeed - the multiboot header ends up in the .text section instead of .__mbheader:
SH4773R wrote:

Code: Select all

section .__mbHeader
align 0x4
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
 
align 4
    dd MAGIC
    dd FLAGS
    dd CHECKSUM
It should rather look like this:

Code: Select all

section .__mbHeader
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
align 4
    dd MAGIC
    dd FLAGS
    dd CHECKSUM 

section .text
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply