Add support for C++

This was a doozy. I had to recompile gcc with a custom target to get it to output the CRT{BEGIN/END}.o files with proper 64 bit relocations.

The CMakeLists.txt file was also edited to allow these files to be linked (thereby actually adding the support) as well as to automatically create the boot image upon build.
This commit is contained in:
Curle 2020-08-27 01:39:56 +01:00
parent aa9600b2b0
commit b3e3f153b5
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
14 changed files with 100 additions and 15 deletions

View File

@ -9,26 +9,46 @@ project(chroma)
SET(src_files SET(src_files
chroma/kernel.c ${CMAKE_SOURCE_DIR}/chroma/kernel.c
chroma/video/draw.c ${CMAKE_SOURCE_DIR}/chroma/video/draw.c
chroma/video/print.c ${CMAKE_SOURCE_DIR}/chroma/video/print.c
chroma/system/cpu.c ${CMAKE_SOURCE_DIR}/chroma/system/cpu.c
chroma/system/rw.c ${CMAKE_SOURCE_DIR}/chroma/system/rw.c
chroma/system/serial.c ${CMAKE_SOURCE_DIR}/chroma/system/serial.c
chroma/system/memory/paging.c ${CMAKE_SOURCE_DIR}/chroma/system/pci.c
chroma/system/memory/physmem.c ${CMAKE_SOURCE_DIR}/chroma/system/memory/paging.c
chroma/system/drivers/keyboard.c ${CMAKE_SOURCE_DIR}/chroma/system/memory/physmem.c
${CMAKE_SOURCE_DIR}/chroma/system/drivers/keyboard.c
) )
include_directories("chroma/inc") include_directories("chroma/inc")
SET(src_no_sse SET(src_no_sse
chroma/system/interrupts.c ${CMAKE_SOURCE_DIR}/chroma/system/interrupts.c
)
SET(src_preamble
${CMAKE_SOURCE_DIR}/global/crt0.o
${CMAKE_SOURCE_DIR}/global/crti.o
${CMAKE_SOURCE_DIR}/global/crtbegin.o
)
set(src_epilogue
${CMAKE_SOURCE_DIR}/global/crtend.o
${CMAKE_SOURCE_DIR}/global/crtn.o
) )
set_property(SOURCE ${src_no_sse} PROPERTY COMPILE_FLAGS -mgeneral-regs-only) set_property(SOURCE ${src_no_sse} PROPERTY COMPILE_FLAGS -mgeneral-regs-only)
add_executable(kernel.elf ${src_files} ${src_no_sse} font.o) add_executable(kernel)
target_compile_options(kernel.elf PRIVATE -ffreestanding -O2 -Wall -Wextra -Wall -Werror -pedantic -fPIC)
target_link_options(kernel.elf PRIVATE -T linker.ld -ffreestanding -O2 -nostdlib -lgcc)
target_sources(kernel PUBLIC ${src_preamble} PUBLIC ${src_files} PUBLIC ${src_no_sse} PUBLIC ${CMAKE_SOURCE_DIR}/font.o PUBLIC ${src_epilogue})
target_compile_options(kernel PRIVATE -ffreestanding -O2 -Wall -Wextra -Wall -Werror -pedantic -fPIC -fno-exceptions)
target_link_options(kernel PRIVATE -T linker.ld -ffreestanding -O2 -nostdlib -nostartfiles -lgcc)
add_custom_command(
TARGET kernel
POST_BUILD
COMMAND cp ${CMAKE_SOURCE_DIR}/kernel ${CMAKE_SOURCE_DIR}/img/boot/exe
COMMAND ${CMAKE_SOURCE_DIR}/mkbootimg chroma.json chroma.img
)

View File

@ -58,3 +58,7 @@ void InitPrint();
void SetupInitialGDT(); void SetupInitialGDT();
void SetupIDT(); void SetupIDT();
int Main();
void Exit();

View File

@ -16,12 +16,16 @@ size_t KernelAddr = (size_t) &LoadAddr;
size_t KernelEnd = (size_t) &end; size_t KernelEnd = (size_t) &end;
void _start(void) { int Main(void) {
SerialPrintf("\r\nBooting Chroma..\r\n"); SerialPrintf("\r\nBooting Chroma..\r\n");
SerialPrintf("Kernel loaded at 0x%p, ends at 0x%p, is %d bytes long.\r\n", KernelAddr, KernelEnd, KernelEnd - KernelAddr); SerialPrintf("Kernel loaded at 0x%p, ends at 0x%p, is %d bytes long.\r\n", KernelAddr, KernelEnd, KernelEnd - KernelAddr);
SerialPrintf("The bootloader has put the paging tables at 0x%p.\r\n", ReadControlRegister(3));
TraversePageTables();
ListMemoryMap(); ListMemoryMap();
InitPrint(); InitPrint();
@ -42,4 +46,12 @@ void _start(void) {
for(;;) { } for(;;) { }
return 0;
}
void Exit(int ExitCode) {
SerialPrintf("Kernel stopped with code %x\r\n", ExitCode);
} }

BIN
global/a.out Normal file

Binary file not shown.

11
global/crt0.c Normal file
View File

@ -0,0 +1,11 @@
extern void Exit(int code);
extern int Main();
extern void _init();
void _start() {
_init();
int ex = Main();
Exit(ex);
}

BIN
global/crt0.o Normal file

Binary file not shown.

10
global/crt0.s Normal file
View File

@ -0,0 +1,10 @@
.section .text
.global _start
.global _init
.global main
.global _fini
.type _start, @function
_start:
call _init
call main
call _fini

BIN
global/crtbegin.o Normal file

Binary file not shown.

BIN
global/crtend.o Normal file

Binary file not shown.

BIN
global/crti.o Normal file

Binary file not shown.

13
global/crti.s Normal file
View File

@ -0,0 +1,13 @@
.section .init
.global _init
.type _init, @function
_init:
push %rbp
movq %rsp, %rbp
.section .fini
.global _fini
.type _fini, @function
_fini:
push %rbp
movq %rsp, %rbp

BIN
global/crtn.o Normal file

Binary file not shown.

9
global/crtn.s Normal file
View File

@ -0,0 +1,9 @@
.section .init
popq %rbp
ret
.section .fini
popq %rbp
ret

View File

@ -26,7 +26,13 @@ SECTIONS
*(COMMON) *(COMMON)
} :boot } :boot
/DISCARD/ : { *(.eh_frame) *(.comment) } .eh_frame_hdr : {
*(.eh_frame_hdr)
*(.eh_frame)
*(.gcc_except_table)
} :boot
/DISCARD/ : {*(.comment) }
end = .; end = .;
} }