Loading elf-i386 from my boot loader

▼魔方 西西 提交于 2019-12-10 12:00:42

问题


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.

来源:https://stackoverflow.com/questions/48229807/loading-elf-i386-from-my-boot-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!