F
F
Fedor Lapshin2019-04-14 21:44:25
C++ / C#
Fedor Lapshin, 2019-04-14 21:44:25

Error loading uncompressed kernel without PVH ELF Note ( osdev ) !?

trying to figure out why qemu doesn't want to run the kernel....
i have WIN64
and also cygwin
tried everything, nothing helps here is the ld
code

ENTRY(_loadkernel)

SECTIONS {
    . = 1M;
  .boot : ALIGN(4) {
    *(.multiboot)
  }
    .text : ALIGN(4) {
    	*(.text)
    }
  .data : ALIGN(4) {
        *(.data)
    }
    .bss : ALIGN(4) {
        *(.bss)
    } 

}

LOADER
MAGIC equ 0x1BADB002
MEMINFO equ 1<<1
MBALIGN equ 1<<0
FLAGS equ 0 | MBALIGN | MEMINFO

STACK_SIZE equ 600

bits 32

section .bss

align 4

stack_end:
  resb STACK_SIZE
stack_top:
section .multiboot

align 4

 grubBoot:
  dd MAGIC
  dd FLAGS
;	dd 0
  dd (end_grubBoot - grubBoot)
  dd -(MAGIC + FLAGS + (end_grubBoot - grubBoot))
;	dd -(MAGIC + 0 + (end_grubBoot - grubBoot))
  
;	dw 0
;	dw FLAGS
;	dd 8
end_grubBoot:

section .text

align 4

global _loadkernel

extern __kmain


_loadkernel:
  finit
  mov esp,stack_top
  
  push ebx
  push eax

  call __kmain

  cli
  hlt

MAIN
void print_str(char* str) {
  unsigned short* videoMemory = (unsigned short*)0xb8000;

  for (int i = 0; i < str[i] != '\0'; i++) {
    videoMemory[i] = (videoMemory[i] & 0xFF00) | str[i];
  }

}

void _kmain(int magic,void *boot_ptr) {
  print_str("hello world!");
  while(1) {}
}

and makefile
CC=gcc
LD=ld
ASM=nasm -f elf32
CSTD=11
CEMU=-m32
LDEMU=-mi386pe # elf_i386
LDFILE=link.ld
OUTBIN=kernel
CSOURCES ?=
ASMSOURCES ?= 
NASMSOURCES ?= 
CSOURCES += $(shell find . -name "*.c" -type f -print )
ASMSOURCES += $(shell find . -name "*.s" -type f -print )
NASMSOURCES += $(shell find . -name "*.asm" -type f -print )


all: build test

build: kernel img iso

kernel:
  $(CC) $(CEMU) -std=c$(CSTD) -c $(CSOURCES) -ffreestanding -nostdlib -nostdinc -fno-pic
  $(ASM) $(NASMSOURCES)
  mv *.o build/
  $(LD) $(LDEMU) --nmagic -T$(LDFILE) -o build/$(OUTBIN).bin build/*.o
  objcopy -O elf32-i386 build/$(OUTBIN).bin $(OUTBIN)
img:

iso:

hex_info:
  @echo --HEX INFO--
  @echo loader hex info
  hexdump -x build/loader.o
  @echo kernel hex info
  hexdump -x $(OUTBIN)
# dis_asm:
#	@echo --DIS ASM--
#	@echo loader disasm
#	ndisasm -b 32 build/loader.o
#	@echo kernel disasm
#	ndisasm -b 32 $(OUTBIN)
obj_info:
  @echo --OBJ INFO--
  @echo loader obj info
  objdump -f -h build/loader.o
  @echo kernel obj info
  objdump -f -h $(OUTBIN)
info: hex_info obj_info # dis_asm

test:
  qemu-system-i386 -nographic -kernel $(OUTBIN)
clean: kmain.o $(OUTBIN)
  rm build/*.o
  rm build/*.bin
  rm $(OUTBIN)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-04-15
@FedLapshin06

trying to figure out why qemu doesn't want to run the kernel....

Because it's not a multiboot header, MAGIC is taken from the old version of the format, and the structure is from version 2:
MAGIC equ 0x1BADB002
...
grubBoot:
  dd MAGIC
  dd FLAGS
;	dd 0
  dd (end_grubBoot - grubBoot)
  dd -(MAGIC + FLAGS + (end_grubBoot - grubBoot))
;	dd -(MAGIC + 0 + (end_grubBoot - grubBoot))
  
;	dw 0
;	dw FLAGS
;	dd 8
end_grubBoot:

QEMU understands the old format header perfectly:
MAGIC equ 0x1BADB002

  dd MAGIC
  dd FLAGS
  dd -(MAGIC + FLAGS)

And with this change, your code loads and runs just fine:
trace
...

Trace 0: 0x7f2a71e084c0 [00000000/000caa1a/0xb0]
----------------
IN:
0x00102060:  9b                       wait
0x00102061:  db e3                    fninit
0x00102063:  bc 44 23 10 00           movl     $0x102344, %esp
0x00102068:  53                       pushl    %ebx
0x00102069:  50                       pushl    %eax
0x0010206a:  e8 68 00 00 00           calll    0x1020d7

Trace 0: 0x7f2a71e08800 [00000000/00102060/0xb0]
----------------
IN:
0x001020d7:  55                       pushl    %ebp
0x001020d8:  89 e5                    movl     %esp, %ebp
0x001020da:  68 00 10 10 00           pushl    $0x101000
0x001020df:  e8 8d ff ff ff           calll    0x102071

Linking TBs 0x7f2a71e08800 [00102060] index 0 -> 0x7f2a71e08a40 [001020d7]
Trace 0: 0x7f2a71e08a40 [00000000/001020d7/0xb0]
----------------
IN:
0x00102071:  55                       pushl    %ebp
0x00102072:  89 e5                    movl     %esp, %ebp
0x00102074:  53                       pushl    %ebx
0x00102075:  83 ec 10                 subl     $0x10, %esp
0x00102078:  c7 45 f4 00 80 0b 00     movl     $0xb8000, -0xc(%ebp)
0x0010207f:  c7 45 f8 00 00 00 00     movl     $0, -8(%ebp)
0x00102086:  eb 35                    jmp      0x1020bd

Linking TBs 0x7f2a71e08a40 [001020d7] index 0 -> 0x7f2a71e08c80 [00102071]
Trace 0: 0x7f2a71e08c80 [00000000/00102071/0xb0]
----------------
IN:
0x001020bd:  8b 55 f8                 movl     -8(%ebp), %edx
0x001020c0:  8b 45 08                 movl     8(%ebp), %eax
0x001020c3:  01 d0                    addl     %edx, %eax
0x001020c5:  0f b6 00                 movzbl   (%eax), %eax
0x001020c8:  0f be c0                 movsbl   %al, %eax
0x001020cb:  3b 45 f8                 cmpl     -8(%ebp), %eax
0x001020ce:  7f b8                    jg       0x102088

Linking TBs 0x7f2a71e08c80 [00102071] index 0 -> 0x7f2a71e08f00 [001020bd]
Trace 0: 0x7f2a71e08f00 [00000000/001020bd/0xb0]
----------------
IN:
0x00102088:  8b 45 f8                 movl     -8(%ebp), %eax
0x0010208b:  8d 14 00                 leal     (%eax, %eax), %edx
0x0010208e:  8b 45 f4                 movl     -0xc(%ebp), %eax
0x00102091:  01 d0                    addl     %edx, %eax
0x00102093:  8b 55 f8                 movl     -8(%ebp), %edx
0x00102096:  8d 0c 12                 leal     (%edx, %edx), %ecx
0x00102099:  8b 55 f4                 movl     -0xc(%ebp), %edx
0x0010209c:  01 ca                    addl     %ecx, %edx
0x0010209e:  0f b7 12                 movzwl   (%edx), %edx
0x001020a1:  89 d3                    movl     %edx, %ebx
0x001020a3:  b3 00                    movb     $0, %bl
0x001020a5:  8b 4d f8                 movl     -8(%ebp), %ecx
0x001020a8:  8b 55 08                 movl     8(%ebp), %edx
0x001020ab:  01 ca                    addl     %ecx, %edx
0x001020ad:  0f b6 12                 movzbl   (%edx), %edx
0x001020b0:  66 0f be d2              movsbw   %dl, %dx
0x001020b4:  09 da                    orl      %ebx, %edx
0x001020b6:  66 89 10                 movw     %dx, (%eax)
0x001020b9:  83 45 f8 01              addl     $1, -8(%ebp)
0x001020bd:  8b 55 f8                 movl     -8(%ebp), %edx
0x001020c0:  8b 45 08                 movl     8(%ebp), %eax
0x001020c3:  01 d0                    addl     %edx, %eax
0x001020c5:  0f b6 00                 movzbl   (%eax), %eax
0x001020c8:  0f be c0                 movsbl   %al, %eax
0x001020cb:  3b 45 f8                 cmpl     -8(%ebp), %eax
0x001020ce:  7f b8                    jg       0x102088

Linking TBs 0x7f2a71e08f00 [001020bd] index 1 -> 0x7f2a71e091c0 [00102088]
Trace 0: 0x7f2a71e091c0 [00000000/00102088/0xb0]
Linking TBs 0x7f2a71e091c0 [00102088] index 1 -> 0x7f2a71e091c0 [00102088]
Trace 0: 0x7f2a71e091c0 [00000000/00102088/0xb0]
----------------
IN:
0x001020d0:  90                       nop
0x001020d1:  83 c4 10                 addl     $0x10, %esp
0x001020d4:  5b                       popl     %ebx
0x001020d5:  5d                       popl     %ebp
0x001020d6:  c3                       retl

Linking TBs 0x7f2a71e091c0 [00102088] index 0 -> 0x7f2a71e098c0 [001020d0]
Trace 0: 0x7f2a71e098c0 [00000000/001020d0/0xb0]
----------------
IN:
0x001020e4:  83 c4 04                 addl     $4, %esp
0x001020e7:  eb fe                    jmp      0x1020e7

Trace 0: 0x7f2a71e09b00 [00000000/001020e4/0xb0]
----------------
IN:
0x001020e7:  eb fe                    jmp      0x1020e7

Linking TBs 0x7f2a71e09b00 [001020e4] index 0 -> 0x7f2a71e09c40 [001020e7]
Trace 0: 0x7f2a71e09c40 [00000000/001020e7/0xb0]
Linking TBs 0x7f2a71e09c40 [001020e7] index 0 -> 0x7f2a71e09c40 [001020e7]

...

It is still not clear why such games with the kernel format, it could have been directly:
LDEMU=-melf_i386
kernel: $(CSOURCES) $(NASMSOURCES) $(LDFILE)
        $(CC) $(CEMU) -std=c$(CSTD) -c $(CSOURCES) -ffreestanding -nostdlib -nostdinc -fno-pic
        $(ASM) $(NASMSOURCES)
        $(LD) $(LDEMU) --nmagic -T$(LDFILE) -o kernel *.o

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question