2. I know that I have a topic previously pertaining to such a topic, but I don't wish to necropost either.
Now then; Using this code,
Code: Select all
for(unsigned int *i = 0; (unsigned int)i < KernelHeader->ElfSectionHeaderEntrySize * KernelHeader->ElfProgramHeaderEntries; i += KernelHeader->ElfSectionHeaderEntrySize)
{
// 3.1: Get ourselves a section header
TElf64SectionHeader *SectionHeader = (TElf64SectionHeader*)((unsigned int)KernelHeader + (KernelHeader->ElfSectionHeaderOffset + i));
if(SectionHeader->SectionHeaderAddress)
{
// 3.2: Check for BSS section:
if(SectionHeader->SectionHeaderType == SectionHeaderTypeNoBits)
{
// This is the BSS section, zero it
memset((void*)SectionHeader->SectionHeaderAddress, 0, SectionHeader->SectionHeaderSize);
}
else
{
// Copy it somewhere.
memcpy((void*)SectionHeader->SectionHeaderAddress, (void*)((unsigned int)KernelHeader + SectionHeader->SectionHeaderOffset), SectionHeader->SectionHeaderSize);
}
}
}
But anyway, tests have shown that ElfProgramHeaderEntries is 0. That code never gets executed, and nothing gets copied.
However, if I jump to the entry point anyway (I'm not actually doing that yet, but I need to know if I copied the file properly), it works, but is all kinds of screwed (presumably due to a lack of a 64-bit GDT). (Although it clears the screen as expected, I suspect all kinds of memory errors)
Here's my point: I'm pretty sure there are supposed to be more than 0 ProgramHeaders. I over-verify the ELF file (4 times), including before copying it to memory (it's in the way of the GRUB memory map) and after copying it. The header is there.
This is my ELF file:
start.s
Code: Select all
[BITS 64]
section .text
global Execute
extern main
Execute:
; Indicate something to the outside world that we're here.
cli
call main
hlt
main.c
Code: Select all
void main()
{
//unsigned int i = 6 / 0;
unsigned char *videoram = (unsigned char*)0xB8000;
unsigned int f = 0;
for(int i = 0; i < 4001; i++)
{
videoram[i] = (char)(32);
videoram[i + 1] = 0x0D;
i++;
}
}
Code: Select all
ENTRY(Execute)
OUTPUT_FORMAT(elf64-x86-64)
phys = 0x00200000;
SECTIONS
{
.text phys :
{
code = .;
*(.text)
. = ALIGN(4096);
}
.data :
{
data = .;
*(.data)
*(.rodata)
. = ALIGN(4096);
}
.bss :
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .; _end = .; __end = .;
}
Code: Select all
kernel.bin: file format elf64-x86-64
kernel.bin
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000200000
Program Header:
LOAD off 0x0000000000200000 vaddr 0x0000000000200000 paddr 0x0000000000200000 align 2**21
filesz 0x0000000000002000 memsz 0x0000000000002000 flags rwx
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00001000 0000000000200000 0000000000200000 00200000 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .eh_frame 00000030 0000000000201000 0000000000201000 00201000 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .data 00000fd0 0000000000201030 0000000000201030 00201030 2**2
CONTENTS, ALLOC, LOAD, DATA
3 .comment 00000011 0000000000000000 0000000000000000 00202000 2**0
CONTENTS, READONLY
Thanks.