Grub error 13

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
User avatar
samoz
Member
Member
Posts: 59
Joined: Sun Jun 01, 2008 1:16 pm

Grub error 13

Post 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...
Hexciting: An open source hex editor for the command line.
https://sourceforge.net/projects/hexciting/
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Grub error 13

Post 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
User avatar
samoz
Member
Member
Posts: 59
Joined: Sun Jun 01, 2008 1:16 pm

Re: Grub error 13

Post 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.
Hexciting: An open source hex editor for the command line.
https://sourceforge.net/projects/hexciting/
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Grub error 13

Post by JohnnyTheDon »

What are you using to link the file? Could you post any makefiles, linker scripts, etc. ?
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

Post 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.
"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 ]
User avatar
samoz
Member
Member
Posts: 59
Joined: Sun Jun 01, 2008 1:16 pm

Re: Grub error 13

Post 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.
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

Re: Grub error 13

Post 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)'
Post Reply