"Error 13: Invalid or unsupported executable format" error

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.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

"Error 13: Invalid or unsupported executable format" error

Post by dc740 »

Hi, I was doing tome test and added a really basic IRQ handling to my test OS.

I changed only ONE file (isr.asm) which compiles without problem... but after linking the OS and trying to run it I get:
"Error 13: Invalid or unsupported executable format"

(I'm using GRUB)

If I roll back to a previous isr.asm file version. then everything works again. I'm not being able to debug the issue cause I don't know what is causing it. since the compiler and the linker are not giving me any errors.

I'm attaching 3 files.

The working isr.asm file
the non working isr.asm file
and the linker file

objdump -f gives me this output on both files
src/isr.o: file format elf32-i386
architecture: i386, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000


I guess it's just an issue with the file size and the linker script. But I'm not that experienced with the linker scripts. so any help is appreciated.

the working kernel is 8KB
and the non working kernel is 12KB... so I think it's not related to the kernel size either.

thanks for reading this far!!!


This is the entire source code (maybe it helps):
http://rapidshare.com/files/303752205/ ... ar.gz.html

Code: Select all

MD5: B01A8C0900962BFD80A016B8B1469ED0 
Attachments
link.ld
link file
(374 Bytes) Downloaded 207 times
isr.asm
Modified and non working file
(6.34 KiB) Downloaded 212 times
isr.asm
working file
(3.84 KiB) Downloaded 165 times
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: "Error 13: Invalid or unsupported executable format" error

Post by piranha »

SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: "Error 13: Invalid or unsupported executable format" error

Post by iocoder »

This problem has faced me before with grub, it was because of some static declerations in kernel.c, when i moved them to another place in the kernel "Specifically, In the Memory Manager". every-thing went in the right way.

the declerations were:

Code: Select all

unsigned volatile static int	magic_value;	// Boot Loader Magic Value.
unsigned volatile static int	mem_size = 1024;	// Memory Size from GRUB.
unsigned volatile static int	cmd_line_address;	// Points to string specifies path of kernel and parameters.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: "Error 13: Invalid or unsupported executable format" error

Post by dc740 »

thank you so much. I didn't see that article before posting. I didn't try it yet. But be sure I will. And sorry for posting about something that was documented in the wiki.

I'll post later with the results. :)
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: "Error 13: Invalid or unsupported executable format" error

Post by dc740 »

no luck :(

This is my link.ld file:

Code: Select all

OUTPUT_ARCH ( "i386" )
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{

/*
 * http://wiki.osdev.org/Grub_Error_13
 */
   .__mbHeader : {
      mboot = .;
      *(.__mbHeader)
      . = ALIGN(4096);
   }

  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
I also tried with "AT( ADDR(.__mbHeader) - phys )"... but it tries to store the header in a negative value

Code: Select all

ld: Warning: Writing section `.__mbHeader' to huge (ie negative) file offset 0xffffffffffe00000.
ld: final link failed: File truncated
Here is the mboot code:

Code: Select all

[BITS 32]
SECTION .__mbHeader
; 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

SECTION .data

;;GDT entry
... code continues here...

Any ideas? I guess I'm doing something wrong with the linker script¿?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: "Error 13: Invalid or unsupported executable format" error

Post by gravaera »

dc740 wrote:no luck :(

This is my link.ld file:

Code: Select all

...
Any ideas? I guess I'm doing something wrong with the linker script¿?
Hi

While I've only read very little of the LD manuals, I'm very sure that if you edit the linker script to look like this: (Not tested. Will most likely have syntax errors; please see the LD manuals.)

Code: Select all

OUTPUT_ARCH ( "i386" )
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
/*Set the virtual address:*/
. = phys
/* If you are not using a higher half kernel, then ti should be the same as your physical base address. I see in your script you defined a symbol to indicate the physical load address, and then you do calculations below which point to a non-virtually linked kernel.*/
/*
 * http://wiki.osdev.org/Grub_Error_13
 */

/* .__mbHeader will begin @ 'phys' */
   .__mbHeader : AT(ADDR(.__mbHeader)){
      mboot = .;
      *(.__mbHeader)
/*For future reference, these ALIGN() directives would make more sense at the beginning of a section command. Commented out.*/
      /*. = ALIGN(4096); */
   }

/*So they should be more like this...*/
  .text ALIGN(4096) : AT(ADDR(.text)) {
    code = .;
    *(.text)
    *(.rodata)
  }
/*On the next line, that bit of arithmetic (phys + (data-code)) is unnecessary. LD is a lot smarter than you think, and it can insert the address of the section you are *currently defining* such that...*/
  .data ALIGN(4096) : AT(ADDR(.data)) /*Voila*/
  {
    data = .;
    *(.data)
  }
/*Insert the ALIGN directive (just like above) in the Vaddress placeholder according to the LD manual version I browsed; Do that for the rest of the sections.*/
  .bss ALIGN(4096) : AT((ADDR.bss))
  {
    bss = .;
    *(.bss)
  }
  end = .;
}
Technically, this is spoonfeeding, since you should probably have given the LD manuals a go, but this problem occurs enough with newcomers that it bears deliberate, clear detailing at least once; After this is solved, I'll add this topic to the "See related forum threads" on the Grub Error 13 article, and well as place a link to this topic at the top of the same.

So newcomers will have an entire article, and an example case.

--All the best
gravaera.
Attachments
__klinkRules.ld
I didn't in any way edit this script before uploading, so there's a trick in there that I use for my own kernel which you may find useful if you can figure it out.
(1.99 KiB) Downloaded 199 times
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: "Error 13: Invalid or unsupported executable format" error

Post by dc740 »

thanks very much but... I'm starting to believe that this is a problem with the kernel itself

This is the final link script
OUTPUT_ARCH ( "i386" )
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
/*Set the virtual address:*/
. = phys;
/*
* http://wiki.osdev.org/Grub_Error_13
*/

/* .__mbHeader will begin @ 'phys' */
.__mbHeader : AT ( ADDR( .__mbHeader ) ) {
/* mboot = .;*/
*(.__mbHeader)
}

.text ALIGN(4096) : AT(ADDR(.text)) {
code = .;
*(.text)
*(.rodata)
}
.data ALIGN(4096) : AT(ADDR(.data)) /*Voila*/
{
data = .;
*(.data)
}
.bss ALIGN(4096) : AT ( ADDR (.bss) )
{
bss = .;
*(.bss)
}
end = .;
}

But now it just doesn't work with neither isr.asm versions. Maybe it's a problem with the multiboot header? it's strange, since I can make it work with the original script and the original isr.asm file
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Re: "Error 13: Invalid or unsupported executable format" error

Post by ChristianF »

The multiboot header should be within the first 8KB, what you have done with the section "__mbHeader".
The rest behind the header should be within the section ".text", so your asm code should look something like this:

Code: Select all

    [BITS 32]
    SECTION .__mbHeader
    ; 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

    SECTION .data

    ;;GDT entry
    ... some data ...
    ... code continues here...

   SECTION .text
   start:
      ... startup code here ...

Hope it helps...
Cheers Christian

PS: Do you get an GRUB Error or what happens now?
42
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: "Error 13: Invalid or unsupported executable format" error

Post by dc740 »

I thought I had replied to this topic a few days ago and I was waiting for replies in my inbox. Sorry for the late response. I must have had a network problem when I replied or something like that, I can't remember.

That's exactly how my code looks like. And I can't still boot the kernel. it just doesn't boot when the kernel size is greater than 8kb (which tells us that this problem is related to this: http://wiki.osdev.org/Grub_Error_13)
But the fix doesn't seem to work. I even tried to align the code from the linker script instead of the asm code. But I had no luck.

Any other posible fix? :S clearly the mboot header is not in the first 8kb. but I don't know what could be wrong. The entire source code is posted in the first post. and the modified linker script is posted a few replies ago too. I double checked that the .__mbHeader sections are defined in my start.asm file as OdinPG pointed in his reply.

Any help is really appreciated. I'm kind of stuck at this point.

thanks for reading this far
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: "Error 13: Invalid or unsupported executable format" error

Post by AJ »

Hi,

If you objdump your binary, where is the MB header?

Cheers,
Adam
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: "Error 13: Invalid or unsupported executable format" error

Post by dc740 »

the "objdump -s" output from my kernel.bin gives me an unrecognised format. But that's already linked. is that ok?


The objdump from the start.o file (the one with the header is:

Code: Select all

objdump -x start.o

start.o:     file format elf32-i386
start.o
architecture: i386, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .mbHeader     00000020  00000000  00000000  00000200  2**0
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
  1 .data         00000030  00000000  00000000  00000220  2**2
                  CONTENTS, ALLOC, LOAD, RELOC, DATA
  2 .text         00000032  00000000  00000000  00000250  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  3 .bss          00002000  00000000  00000000  00000290  2**2
                  ALLOC
SYMBOL TABLE:
00000000 l    df *ABS*	00000000 start.asm
00000000 l    d  .mbHeader	00000000 .mbHeader
00000000 l    d  .data	00000000 .data
00000000 l    d  .text	00000000 .text
00000000 l    d  .bss	00000000 .bss
00000000 l       .mbHeader	00000000 mboot
00000001 l       *ABS*	00000000 MULTIBOOT_PAGE_ALIGN
00000002 l       *ABS*	00000000 MULTIBOOT_MEMORY_INFO
00010000 l       *ABS*	00000000 MULTIBOOT_AOUT_KLUDGE
1badb002 l       *ABS*	00000000 MULTIBOOT_HEADER_MAGIC
00010003 l       *ABS*	00000000 MULTIBOOT_HEADER_FLAGS
e4514ffb l       *ABS*	00000000 MULTIBOOT_CHECKSUM
00000000 l       .data	00000000 gdt_ptr_struct
00000008 l       .data	00000000 gdt
00000030 l       .data	00000000 gdt_end
0000001c l       .text	00000000 start.flush
00002000 l       .bss	00000000 _sys_stack
00000000         *UND*	00000000 code
00000000         *UND*	00000000 bss
00000000         *UND*	00000000 end
00000000         *UND*	00000000 main
00000000 g       .text	00000000 start


RELOCATION RECORDS FOR [.mbHeader]:
OFFSET   TYPE              VALUE 
0000000c R_386_32          .mbHeader
00000010 R_386_32          code
00000014 R_386_32          bss
00000018 R_386_32          end
0000001c R_386_32          .text


RELOCATION RECORDS FOR [.data]:
OFFSET   TYPE              VALUE 
00000002 R_386_32          .data


RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE 
00000003 R_386_32          .data
00000016 R_386_32          .text
0000001d R_386_32          .bss
00000029 R_386_PC32        main

Is that correct?
Note: I changed .__mbHeader to .mbHeader in my linker and assembly files. It's pretty obvious but I wanted to make it clear just in case...

I found this post:
http://forum.osdev.org/viewtopic.php?f=1&t=21126
and he checked for the header using mbcheck. what is that tool? I searched in google but I get always results that point to posts from this forum

thanks in advance
Grunt
Member
Member
Posts: 37
Joined: Fri Nov 06, 2009 1:05 am

Re: "Error 13: Invalid or unsupported executable format" error

Post by Grunt »

http://geezer.osdevbrasil.net/osd/index.htm

If you download the code, you'll find a mbchk.c|exe in boot/grub.
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Re: "Error 13: Invalid or unsupported executable format" error

Post by ChristianF »

I have tested your code, with the changed linker script and the changed start.asm! I tried to load it with GRUB and got a message like "unsupported executable"...
After this I changed the linker script again and set the OUTPUT_FORMAT to "elf32-i386" instead of "binary" and it works:

Code: Select all

/*OUTPUT_ARCH ( "i386" )
OUTPUT_FORMAT("binary")*/
OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
	/*Set the virtual address:*/
	. = phys;
	/*
	* http://wiki.osdev.org/Grub_Error_13
	*/

	/* .__mbHeader will begin @ 'phys' */
	.__mbHeader : AT ( ADDR( .__mbHeader ) ) {
		/* mboot = .;*/
		*(.__mbHeader)
	}

	.text ALIGN(4096) : AT(ADDR(.text)) {
		code = .;
		*(.text)
		*(.rodata)
	}
	.data ALIGN(4096) : AT(ADDR(.data)) /*Voila*/
	{
		data = .;
		*(.data)
	}
	.bss ALIGN(4096) : AT ( ADDR (.bss) )
	{
		bss = .;
		*(.bss)
	}
	end = .;
}
start.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]

SECTION .__mbHeader
; 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

SECTION .data
;;this is our GDT entry
gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT
	;dw 0x0;the gdt structure must be 8 bytes aligned?
ALIGN 4
gdt:
;we set five gdt entries.Why 5? well, we have a code and data segment descriptor for the kernel, code and data segment descriptors for user mode, and a null entry. This must be present, or bad things will happen. 

        dd 0, 0                                                 ; null gate
        db 0xFF, 0xFF, 0, 0, 0, 0x9A, 0xCF, 0x00       ; code selector 0x08: base 0x00000000, limit 0xFFFFFFFF, type 0x9A, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; data selector 0x10: base 0x00000000, limit 0xFFFFFFFF, type 0x92, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; user mode code selector 0x18: base 0x00000000, limit 0xFFFFFFFF, type 0xFA, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; user mode data selector 0x20: base 0x00000000, limit 0xFFFFFFFF, type 0xF2, granularity 0xCF

gdt_end:




section .text
	global start
	extern main
;...


Hope it helps...
Cheers Christian

PS:
You have to use "aout" when you use flat binaries. So you could build your kernel as elf by changing your linker script or as flat binary but then you have to correct the file called compile.sh:

Code: Select all

#...
nasm -f aout -o start.o start.asm
#...
42
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: "Error 13: Invalid or unsupported executable format" error

Post by dc740 »

thank you very much!

but I'm using a 64 bits linux to develop a 32 bit OS. I cannot take "OUTPUT_ARCH(i386)" out. cause it won't link. (yes, it doesn't work if I specify the architecture... maybe I found a bug?)

Another problem I have is that I cannot use nams -aout cause it doesn't recognise the ".__mbHeader" as a valid header. Which is fine since this is documented as the expected behaviour:
http://www.nasm.us/doc/nasmdoc7.html#section-7.10
So it won't let me put the multiboot header at the first 8KB
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Re: "Error 13: Invalid or unsupported executable format" error

Post by ChristianF »

I am using a 64 bit linux at home too, but tried your code on a 32 Bit machine, so I forgot this. ;)

You can link your kernel with the Option "-melf_i386". This causes ld to emulate 32 bit, so it should work with this option. :)
42
Post Reply