将elf文件中的程序按段重新放置
;initKernel ---------------------------------------------------------------------------------
; 将 KERNEL.BIN 的内容经过整理对齐后放到新的位置
; 遍历每一个 Program Header,根据 Program Header 中的信息来确定把什么放进内存,放到什么位置,以及放多少。
; --------------------------------------------------------------------------------------------
InitKernel:
xor esi, esi
mov cx, word [BaseOfKernelFilePhyAddr+2Ch];`. ecx <- pELFHdr->e_phnum
movzx ecx, cx ;/
mov esi, [BaseOfKernelFilePhyAddr + 1Ch] ; esi <- pELFHdr->e_phoff
add esi, BaseOfKernelFilePhyAddr;esi<-OffsetOfKernel+pELFHdr->e_phoff
.Begin:
mov eax, [esi + 0]
cmp eax, 0 ; PT_NULL
jz .NoAction
push dword [esi + 010h] ;size ;`.
mov eax, [esi + 04h] ; |
add eax, BaseOfKernelFilePhyAddr; | memcpy((void*)(pPHdr->p_vaddr),
push eax ;src ; | uchCode + pPHdr->p_offset,
push dword [esi + 08h] ;dst ; | pPHdr->p_filesz;
call MemCpy ; |
add esp, 12 ;/
.NoAction:
add esi, 020h ; esi += pELFHdr->e_phentsize
dec ecx
jnz .Begin
ret
; InitKernel
之后跳转到内核
call InitKernel jmp SelectorFlatC:KernelEntryPointPhyAddr ; 正式进入内核
elf 文件编译需要设置下入口地址
nasm -f elf -o kernel.o kernel.asm
ld -m elf_i386 -s -Ttext 0x30400 -o kernel.bin kernel.o