Page 1 of 1

Grub error 13

Posted: Wed Dec 17, 2008 1:17 pm
by samoz
Hi guys, I've been working on my OS and trying to add keyboard support. Anyways, my last build has failed to even load, with GRUB saying:

"Error 13: Invalid or unsupported executable"

I've used the UNIX file command to check on my kernel file and here is what I got:

"build/kernel: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped"

However, when I try to use the "objdump -h" command, I get this:

"objdump: build/kernel: File format not recognized"

Can anyone help me on this? I really don't know how to proceed...

Re: Grub error 13

Posted: Thu Dec 18, 2008 5:50 am
by jal
samoz wrote:However, when I try to use the "objdump -h" command, I get this:
"objdump: build/kernel: File format not recognized"
Can anyone help me on this? I really don't know how to proceed...
1) you have a bad executable, with a bad format, hence GRUB and objdump not recognizing it
2) we cannot help you, since you do not provide any detail of how you created the executable


JAL

Re: Grub error 13

Posted: Tue Dec 30, 2008 10:21 pm
by samoz
Hi guys, I'm back and this problem with GRUB error 13 is still vexing me.

I'm pretty much able to duplicate the error now in the following way. Under a certain number of lines, the code will run on GRUB perfectly fine. However, when I add an extra line of code (even something simple, such as i++), the format becomes unrecognizable. Does anyone have any idea why this could be happening?

I tried modifying my multi-boot header but that didn't seem to work. I also tried copying verbatim the header from barebones on the wiki.

I know I'm not being very specific here, but I'm not really sure what code to post to illustrate my problem...

Could this be due to my code not fitting on the floppy? Currently my kernel is 21k in size, so I don't see why that would be an issue, but it seems like the size of code is a factor here.

Re: Grub error 13

Posted: Tue Dec 30, 2008 11:11 pm
by JohnnyTheDon
What are you using to link the file? Could you post any makefiles, linker scripts, etc. ?

Re: Grub error 13

Posted: Wed Dec 31, 2008 8:23 am
by Combuster
The multiboot structure should be within the first few kb of the output binary. You should tell the linker that it should come first.

Re: Grub error 13

Posted: Wed Dec 31, 2008 9:03 am
by samoz
You asked for makefiles, linker scripts, etc, so here they are:

Makefile #1

Code: Select all

[email protected]
LD=i386-elf-ld
LDFLAGS=-Tsrc/link.ld
BUILDDIR=/Volumes/No\ NAME/boot

all: source link

source:
	@cd src && make 

clean:
	@rm src/*.o build/kernel build/*.o

link:
	@$(LD) $(LDFLAGS) -o build/kernel build/*.o
	@cp build/kernel $(BUILDDIR)
Makefile #2 ( in the src directory)

Code: Select all

CC=i386-elf-gcc-3.4.6
ASM=nasm

LEVEL1SOURCE=loader.o load_gdt.o  load_idt.o interrupts.o interrupt.o
LEVEL2SOURCE=string.o common.o descriptor_tables.o memory.o paging.o monitor.o 
KERNEL=keyboard.o timer.o kernel.o

LDFLAGS=-T../src/link.ld
CFLAGS=-nostdlib -nostdinc -fno-builtin -Wall -Wextra -nostdinc
ASFLAGS=-felf

all: $(LEVEL1SOURCE) $(LEVEL2SOURCE) $(KERNEL)


.s.o:
	@$(ASM) $(ASFLAGS) $< -o ../build/$@

.c.o:
	@$(CC) $(CFLAGS) $< -c -o ../build/$@

Here is my loader code:

Code: Select all

; 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
MultiBootHeader:
dd MAGIC
dd FLAGS
dd CHECKSUM

; reserve initial kernel stack space
STACKSIZE equ 0x4000                  ; that's 16k.

global _loader           ; making entry point visible to linker
extern kmain			 ; _kmain is defined elsewhere

_loader:
   mov esp, stack+STACKSIZE           ; set up the stack
   push eax                           ; pass Multiboot magic number
   push ebx                           ; pass Multiboot info structure

   cli
   call  kmain                       ; call kernel proper
   jmp $

section .bss
align 32
stack:
   resb STACKSIZE                     ; reserve 16k stack on a quadword boundary

And finally here is the linker code:

Code: Select all

ENTRY(kmain)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
} 

I played around with the linker code a little bit and changed the start address of the text segment with varying degrees of success, but I was unable to figure out why or how to make sure it works consistently.

Re: Grub error 13

Posted: Wed Dec 31, 2008 9:46 am
by xyzzy
Makefile #2 looks dodgy... Try replacing:

Code: Select all

.s.o:
   @$(ASM) $(ASFLAGS) $< -o ../build/$@

.c.o:
   @$(CC) $(CFLAGS) $< -c -o ../build/$@
with:

Code: Select all

%.o: %.s:
   @$(ASM) $(ASFLAGS) $< -o ../build/$@

%.o: %.c
   @$(CC) $(CFLAGS) $< -c -o ../build/$@
The original rules won't match your sources, so your kernel could be ending up being linked with no object files

EDIT: Combuster is right, too - the multiboot header probably isn't being included at the start of the file because you are not making sure that the object file containing the multiboot header is linked first. Put the header in a seperate section by placing something like '.section .multiboot' before it (and moving the existing '.section .text' after it), then modify the linker script to include '*(.multiboot)' before '*(.text)'