Code: Select all
ENTRY (_start)
SECTIONS
{
. = 0x00100000;
/* The kernel will live at 3GB + 1MB in the virtual address space, */
/* which will be mapped to 1MB in the physical address space. */
/* Note that we page-align the sections. */
.multiboot.data : {
*(.multiboot.data)
}
.multiboot.text : {
*(.multiboot.text)
}
. += 0xC0100000;
/* Add a symbol that indicates the start address of the kernel. */
kernel_start = .;
.text ALIGN (4K) : AT (ADDR (.text) - 0xC0000000)
{
*(.text)
}
.rodata ALIGN (4K) : AT (ADDR (.rodata) - 0xC0000000)
{
*(.rodata)
}
.data ALIGN (4K) : AT (ADDR (.data) - 0xC0000000)
{
*(.data)
}
.bss ALIGN (4K) : AT (ADDR (.bss) - 0xC0000000)
{
*(COMMON)
*(.bss)
*(.bootstrap_stack)
}
/* Add a symbol that indicates the end address of the kernel. */
kernel_end = .;
}
Code: Select all
// Defined in `linker.ld`
extern const void * const kernel_start, * const kernel_end;
void whatever_function(void) {
printf("Kernel is at %u-%u\n", kernel_start, kernel_end);
}
Code: Select all
i386-elf-gcc -T linker.ld -ffreestanding -O2 -nostdlib -o kernel.elf boot.o obj/kernel/ACPI.o obj/kernel/RSDP.o obj/kernel/SDT.o obj/kernel/VGA.o obj/kernel/display.o obj/kernel/gdt.o obj/kernel/interrupt_handlers.o obj/kernel/kernel.o obj/kernel/keyboard.o obj/kernel/memory.o obj/kernel/paging.o obj/kernel/serial.o obj/libc/libc.o obj/libc/qsort.o -lgcc
Code: Select all
mov ebx, physical_address(boot_page_table1)
add ebx, 0x03 ; Set "present" and other flags
; Identity map (virtual 0xbeef -> physical 0xbeef)
mov [physical_address(boot_page_directory) + 0], ebx
; Map for virtual address 0xC0000000 (virtual 0xC0000000 -> physical 0x00)
mov [physical_address(boot_page_directory) + 768 * 4], ebx
And this prints "Kernel is at 0-0"! But it clearly can't occupy zero bytes!
Why is that?