0

I am doing operating system project, until now I have my bootloader running. I can load binary file using bios interuppt, but I am unable to load and call C function from ELF file format:

Here is my C program that I want to finally execute:

//build :: cc -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -c -o kmain.o kmain.c
void kmain(){
   int a = 5;
   for(;;);
}

Here is assembly code to call kmain()

; build :: nasm -f elf loader.asm
[BITS 32]
[GLOBAL start]
[EXTERN kmain]

section .text
start: 
   mov eax, 0
   call kmain

This is my linker script

ENTRY(start)

and this how I am linking everything together

ld -m elf_i386 -T link.ld -o kernel loader.o kmain.o

Now to call start from my bootloader, I am using e_entry offset field from elf header( 24 byte away from starting address) :

xor edx, edx
mov edx, 24
add edx, IMAGE_PMODE_BASE
add ebx, dword[edx]
add ebx, IMAGE_PMODE_BASE 
call ebx 

where IMAGE_PMODE_BASE is address of elf file loaded in memory. My question is "Is This the correct way of loading and calling a function in C in ELF file format?".

Thank you for reading, please help.

snehm
  • 223
  • 3
  • 13
  • is your kmain residing on userspace? – Raman Jan 12 '18 at 16:00
  • No, every thing is in ring 0. There is no user space, until now. – snehm Jan 12 '18 at 16:03
  • Also I have properly set stack before moving out of bootloader. – snehm Jan 12 '18 at 16:04
  • When you say `you` have a boot loader, is it one that you've written from scratch and if so, it must setup protected mode IVT and a GDT mapped for a flat memory model. If not, none of what you're doing in your question is of any consequence. I know nothing about compilers, but any linker script, I think, needs to have more information than just `entry (start)`. – Shift_Left Jan 16 '18 at 21:09

0 Answers0