Linker LD Madness.

This commit is contained in:
Oscar 2017-02-01 18:04:07 -08:00
parent 1788338f10
commit 3a88034c22
5 changed files with 94 additions and 112 deletions

View File

@ -3,15 +3,15 @@ plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1
config_interface: win32config
display_library: win32, options="gui_debug"
memory: host=256, guest=256
romimage: file="bochs/BIOS-bochs-latest"
vgaromimage: file="bochs/VGABIOS-lgpl-latest"
romimage: file="C:\Program Files (x86)\Bochs-2.6.8\BIOS-bochs-latest"
vgaromimage: file="C:\Program Files (x86)\Bochs-2.6.8\VGABIOS-lgpl-latest"
boot: cdrom
magic_break: enabled=1
floppy_bootsig_check: disabled=0
# no floppya
# no floppyb
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=cdrom, path="HOS.iso", status=inserted, model="Generic 1234", biosdetect=auto
ata0-master: type=cdrom, path="secX.iso", status=inserted, model="Generic 1234", biosdetect=auto
ata0-slave: type=none
ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
ata1-master: type=none

View File

@ -1,3 +1,3 @@
menuentry "secX x64 [Dev]" {
multiboot2 /secX/kernel.bin
multiboot2 /secX/kernel.elf
}

View File

@ -1,38 +1,33 @@
OUTPUT_FORMAT(elf64-x86-64)
ENTRY(HAL_ENTRY32_PADDR)
OUTPUT_ARCH(i386:x86-64)
SECTIONS
{
. = 0xFFFFFFFF80000000;
. = HAL_KERNEL_BASE_VADDR + HAL_KERNEL_BASE_PADDR;
kernel_start = .;
.entry ALIGN(0x1000) : AT(ADDR(.entry) - 0xFFFFFFFF80000000)
.multiboot_header ALIGN(0x1000) : AT(ADDR(.multiboot_header) - HAL_KERNEL_BASE_VADDR)
{
*(.entry)
*(.multiboot_header)
}
.text ALIGN(0x1000) : AT(ADDR(.text) - 0xFFFFFFFF80000000)
.text ALIGN(0x1000) : AT(ADDR(.text) - HAL_KERNEL_BASE_VADDR)
{
*(.text)
}
.data ALIGN(0x1000) : AT(ADDR(.data) - 0xFFFFFFFF80000000)
.data ALIGN(0x1000) : AT(ADDR(.data) - HAL_KERNEL_BASE_VADDR)
{
*(.data)
*(.rodata)
*(.rodata*)
}
.bss ALIGN(0x1000) : AT(ADDR(.bss) - 0xFFFFFFFF80000000)
.bss ALIGN(0x1000) : AT(ADDR(.bss) - HAL_KERNEL_BASE_VADDR)
{
*(.bss)
}
.heap ALIGN(0x1000) : AT(ADDR(.heap) - 0xFFFFFFFF80000000)
{
*(.heap)
}
.stack ALIGN(0x1000) : AT(ADDR(.stack) - 0xFFFFFFFF80000000)
{
*(.stack)
*(COMMON)
}
kernel_end = .;

View File

@ -15,14 +15,13 @@ TEMP_DIR := tmp
HEADER_DIRS := $(call rdircardex, *,*.h)
ALL_TEMP_DIRS := $(addprefix $(TEMP_DIR)/,$(call rdircard,*))
C_FLAGS := -m64 -std=c11 -g -c $(addprefix -I, $(HEADER_DIRS)) -mcmodel=kernel -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Werror -Wextra -Wno-comment
ASM_FLAGS := -g -f elf64 -I $(ASM_SRC_PATH_64)/
C_FLAGS := -m64 -std=c11 -g -c $(addprefix -I, $(HEADER_DIRS)) -mcmodel=kernel -ffreestanding -nostdlib -lgcc -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Werror -Wextra -Wno-comment
ASM_FLAGS := -f elf64 -g -I $(ASM_SRC_PATH_64)/
LD_FLAGS := -melf_x86_64 -I
C_SRC := $(call rwildcard, ./, *.c)
ASM_SRC := $(call rwildcard, ./, *.asm)
KERNEL_BIN := kernel.bin
KERNEL_ELF := kernel.elf
KERNEL_DASM := kernel.dasm
@ -40,13 +39,14 @@ init:
compile: $(C_OBJ) $(ASM_OBJ)
link: $(KERNEL_BIN) $(KERNEL_ELF)
link: $(KERNEL_ELF)
buildiso:
mkdir -p $(TEMP_DIR)/temp_iso/secX
mkdir -p $(TEMP_DIR)/temp_iso/boot
mkdir -p $(TEMP_DIR)/temp_iso/boot/grub
cp $(TEMP_DIR)/$(KERNEL_BIN) $(TEMP_DIR)/temp_iso/secX/$(KERNEL_BIN)
cp $(TEMP_DIR)/$(KERNEL_ELF) $(TEMP_DIR)/temp_iso/secX/$(KERNEL_ELF)
cp $(TEMP_DIR)/$(KERNEL_ELF) ./
cp $(GRUB_CFG) $(TEMP_DIR)/temp_iso/boot/grub/
grub-mkrescue -o secX.iso $(TEMP_DIR)/temp_iso
@ -57,6 +57,7 @@ clean: clean_temp
rm -rf $(KERNEL_DASM)
rm -rf secX.iso
rm -rf *.log
rm -rf $(KERNEL_ELF)
%.o: %.c
$(CC) $(C_FLAGS) -o $(TEMP_DIR)/$@ $^
@ -64,11 +65,8 @@ clean: clean_temp
%.oasm: %.asm
$(ASM) $(ASM_FLAGS) -o $(TEMP_DIR)/$@ $^
$(KERNEL_BIN): $(ALL_OBJ)
$(LD) $(LD_FLAGS) -T $(LD_SCRIPT) --oformat binary -o $(TEMP_DIR)/$(KERNEL_BIN) $(ALL_OBJ)
$(KERNEL_ELF): $(ALL_OBJ)
$(LD) $(LD_FLAGS) -T $(LD_SCRIPT) --oformat elf64-x86-64 -o $(TEMP_DIR)/$(KERNEL_ELF) $(ALL_OBJ)
$(LD) $(LD_FLAGS) -T $(LD_SCRIPT) -o $(TEMP_DIR)/$(KERNEL_ELF) $(ALL_OBJ)
disasm:
objdump -M intel -D $(TEMP_DIR)/$(KERNEL_ELF) > $(KERNEL_DASM)

View File

@ -4,63 +4,52 @@
extern hal_main
; IMPORTANT: This module should be 4k-page aliened
[SECTION .entry]
[BITS 32]
; MultiBoot Header
HAL_KERNEL_BASE_VADDR equ 0xFFFFFFFF80000000
global HAL_KERNEL_BASE_VADDR
HAL_KERNEL_BASE_PADDR equ 0x4000000
global HAL_KERNEL_BASE_PADDR
HAL_ENTRY32_PADDR equ hal_entry_32 - HAL_KERNEL_BASE_VADDR
global HAL_ENTRY32_PADDR
MULTIBOOT_TAG_ALIGNMENT equ 8
MULTIBOOT_HEADER_ALIGNMENT equ 8
MULTIBOOT_LOADED_MAGIC equ 0x36d76289
MULTIBOOT_MAGIC_NUMBER equ 0xE85250D6
MULTIBOOT_ARCH equ 0
MULTIBOOT_CHECK_SUM equ (0xFFFFFFFF - (MULTIBOOT_MAGIC_NUMBER + MULTIBOOT_HEADER_SIZE + MULTIBOOT_ARCH) + 1)
MULTIBOOT_CHECK_SUM equ (0 - (MULTIBOOT_MAGIC_NUMBER + MULTIBOOT_HEADER_SIZE + MULTIBOOT_ARCH))
MULTIBOOT_REQ_MINFO equ 4
MULTIBOOT_REQ_MMAP equ 6
MULTIBOOT_REQ_APM equ 10
;align MULTIBOOT_HEADER_ALIGNMENT
MULTIBOOT_HEADER:
[SECTION .multiboot_header]
[BITS 32]
;====================
;header tag
align MULTIBOOT_HEADER_ALIGNMENT
multiboot_header_tag:
dd MULTIBOOT_MAGIC_NUMBER
dd MULTIBOOT_ARCH
dd MULTIBOOT_HEADER_SIZE
dd MULTIBOOT_CHECK_SUM
;====================
;INFO_REQUEST_TAG
MULTIBOOT_REQ_MINFO equ 4
MULTIBOOT_REQ_MMAP equ 6
MULTIBOOT_REQ_APM equ 10
MULTIBOOT_INFO_TAG:
align MULTIBOOT_TAG_ALIGNMENT
multiboot_info_tag:
dw 0x1 ; type=1
dw 0x0 ; flag=0
dd MULTIBOOT_INFO_TAG_SIZE
;dd MULTIBOOT_REQ_MINFO
dd MULTIBOOT_REQ_MMAP
dd MULTIBOOT_REQ_APM
MULTIBOOT_INFO_TAG_SIZE equ ($ - MULTIBOOT_INFO_TAG)
;====================
;Address_tag
align MULTIBOOT_TAG_ALIGNMENT
MULTIBOOT_ADDRESS_TAG:
dw 0x2 ;type=2
dw 0x0 ;flag=0
dd MULTIBOOT_ADDRESS_TAG_SIZE; size
dd MULTIBOOT_HEADER ; Since at the beginning of the file
dd MULTIBOOT_HEADER ; load start
dd 0 ; load end
dd 0 ; bss
MULTIBOOT_ADDRESS_TAG_SIZE equ ( $ - MULTIBOOT_ADDRESS_TAG)
;====================
;Entry_tag
align MULTIBOOT_TAG_ALIGNMENT
MULTIBOOT_ENTRY_TAG:
dw 0x3; type=3
dw 0x0; flag=0
dd MULTIBOOT_ENTRY_TAG_SIZE
dd entry_32
MULTIBOOT_ENTRY_TAG_SIZE equ ($ - MULTIBOOT_ENTRY_TAG)
MULTIBOOT_INFO_TAG_SIZE equ ($ - multiboot_info_tag)
;====================
;MODULE ALIGNMENT TAG
align MULTIBOOT_TAG_ALIGNMENT
dw 0x6; type=6
dw 0x0; flag=0
dd 8
dd 0x8
;====================
;End_tag
align MULTIBOOT_TAG_ALIGNMENT
@ -68,48 +57,13 @@ dw 0x0
dw 0x0
dd 0x8
;====================
MULTIBOOT_HEADER_SIZE equ ($ - MULTIBOOT_HEADER)
MULTIBOOT_HEADER_SIZE equ ($ - multiboot_header_tag)
[SECTION .text]
[BITS 32]
align 4096
; temporary page table
PML4_BASE:
times 512 dq 0 ;reserved the rest for page entries
align 4096
PDPT_BASE:
times 512 dq 0 ;reserved the rest for page entries
align 4096
; long mode gdt
GDT64: ; Global Descriptor Table (64-bit).
; NULL
dw 0 ; Limit (low).
dw 0 ; Base (low).
db 0 ; Base (middle)
db 0 ; Access.
db 0 ; Granularity.
db 0 ; Base (high).
SLCT_CODE equ $ - GDT64 ; The code descriptor.
dw 0 ; Limit (low).
dw 0 ; Base (low).
db 0 ; Base (middle)
db 10011010b ; Access.
db 00100000b ; Granularity.
db 0 ; Base (high).
SLCT_DATA equ $ - GDT64 ; The data descriptor.
dw 0 ; Limit (low).
dw 0 ; Base (low).
db 0 ; Base (middle)
db 10010010b ; Access.
db 00000000b ; Granularity.
db 0 ; Base (high).
.GDT64_PTR: ; The GDT-pointer.
dw $ - GDT64 - 1 ; Limit.
dq GDT64 ; Base.
align 4096
entry_32:
hal_entry_32:
; close interrupt
cli
cld
@ -127,7 +81,7 @@ mov esp, 0
mov esi,ebx
; check x64 support
call _ensure_support_x64
call halp_ensure_support_x64
cmp eax,1
je .init_x64
hlt
@ -183,10 +137,10 @@ mov cr0, eax ; Set control register 0 to the A
; enter x64
lgdt [GDT64.GDT64_PTR]
jmp SLCT_CODE:entry
jmp SLCT_CODE:halp_entry_64
hlt
_ensure_support_x64:
halp_ensure_support_x64:
push ebp
mov ebp,esp
pushfd
@ -220,7 +174,7 @@ ret
[SECTION .text]
[BITS 64]
entry:
halp_entry_64:
cli
mov ax,SLCT_DATA
mov ds,ax
@ -235,15 +189,50 @@ mov rdi,rsi ; multiboot_info*
call hal_main
hlt
[SECTION .data]
[BITS 64]
KERNEL_HEAP_SIZE equ 8192
KERNEL_STACK_SIZE equ 8192
[SECTION .heap]
[BITS 64]
align 4096 ;4k alignment
times KERNEL_HEAP_SIZE db 0 ; initially 8k heap
[SECTION .stack]
[BITS 64]
align 4096 ;4k alignment
times KERNEL_STACK_SIZE db 0 ; initially 8k stack
align 4096
; temporary page table
PML4_BASE:
times 512 dq 0 ;reserved the rest for page entries
align 4096
PDPT_BASE:
times 512 dq 0 ;reserved the rest for page entries
align 4096
; long mode gdt
GDT64: ; Global Descriptor Table (64-bit).
; NULL
dw 0 ; Limit (low).
dw 0 ; Base (low).
db 0 ; Base (middle)
db 0 ; Access.
db 0 ; Granularity.
db 0 ; Base (high).
SLCT_CODE equ $ - GDT64 ; The code descriptor.
dw 0 ; Limit (low).
dw 0 ; Base (low).
db 0 ; Base (middle)
db 10011010b ; Access.
db 00100000b ; Granularity.
db 0 ; Base (high).
SLCT_DATA equ $ - GDT64 ; The data descriptor.
dw 0 ; Limit (low).
dw 0 ; Base (low).
db 0 ; Base (middle)
db 10010010b ; Access.
db 00000000b ; Granularity.
db 0 ; Base (high).
.GDT64_PTR: ; The GDT-pointer.
dw $ - GDT64 - 1 ; Limit.
dq GDT64 ; Base.