From 132d25e5bf39aae8561149eceb9cf08c3eea03a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Holz?= Date: Fri, 18 Aug 2023 18:39:53 +0200 Subject: [PATCH] Kernel: Add linker script for riscv64 --- Kernel/Arch/riscv64/linker.ld | 97 +++++++++++++++++++++++++++++++++++ Kernel/CMakeLists.txt | 11 ++-- 2 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 Kernel/Arch/riscv64/linker.ld diff --git a/Kernel/Arch/riscv64/linker.ld b/Kernel/Arch/riscv64/linker.ld new file mode 100644 index 00000000000..63d759ba5a6 --- /dev/null +++ b/Kernel/Arch/riscv64/linker.ld @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2023, Sönke Holz + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +ENTRY(start) + +KERNEL_MAPPING_BASE = 0x2000000000; + +/* TODO: Add FLAGS to the program headers */ +PHDRS +{ + text PT_LOAD ; + data PT_LOAD ; + ksyms PT_LOAD ; + bss PT_LOAD ; +} + +SECTIONS +{ + . = KERNEL_MAPPING_BASE; + + start_of_kernel_image = .; + + .text ALIGN(4K) : + { + start_of_kernel_text = .; + *(.text.first) + + start_of_safemem_text = .; + KEEP(*(.text.safemem)) + end_of_safemem_text = .; + start_of_safemem_atomic_text = .; + KEEP(*(.text.safemem.atomic)) + end_of_safemem_atomic_text = .; + + *(.text*) + end_of_kernel_text = .; + } :text + + .rodata ALIGN(4K) : + { + start_heap_ctors = .; + *libkernel_heap.a:*(.init_array) + end_heap_ctors = .; + + start_ctors = .; + *(.init_array) + end_ctors = .; + + *(.rodata*) + } :data + + .data ALIGN(4K) : + { + start_of_kernel_data = .; + *(.data*) + end_of_kernel_data = .; + } :data + + .ksyms ALIGN(4K) : + { + start_of_kernel_ksyms = .; + *(.kernel_symbols) + end_of_kernel_ksyms = .; + } :ksyms + + .bss ALIGN(4K) (NOLOAD) : + { + start_of_bss = .; + *(.bss) + end_of_bss = .; + + . = ALIGN(4K); + *(.heap) + } :bss + + /* + FIXME: 8MB is enough space for all of the tables required to identity map + physical memory. 8M is wasteful, so this should be properly calculated. + */ + + /* FIXME: Placeholder to satisfy linker */ + start_of_unmap_after_init = .; + end_of_unmap_after_init = .; + start_of_ro_after_init = .; + end_of_ro_after_init = .; + + . = ALIGN(4K); + page_tables_phys_start = .; + + . += 8M; + page_tables_phys_end = .; + + end_of_kernel_image = .; +} diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 51e7c759bd7..ce3349dbab5 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -713,7 +713,13 @@ add_dependencies(kernel_heap install_libc_headers) add_executable(Kernel ${SOURCES}) add_dependencies(Kernel generate_EscapeSequenceStateMachine.h generate_version_header install_libc_headers) -if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64") +if("${SERENITY_ARCH}" STREQUAL "aarch64") + target_link_options(Kernel PRIVATE LINKER:-T ${CMAKE_CURRENT_SOURCE_DIR}/Arch/aarch64/linker.ld -nostdlib LINKER:--no-pie) + set_target_properties(Kernel PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Arch/aarch64/linker.ld) +elseif("${SERENITY_ARCH}" STREQUAL "riscv64") + target_link_options(Kernel PRIVATE LINKER:-T ${CMAKE_CURRENT_SOURCE_DIR}/Arch/riscv64/linker.ld -nostdlib LINKER:--no-pie) + set_target_properties(Kernel PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Arch/riscv64/linker.ld) +elseif ("${SERENITY_ARCH}" STREQUAL "x86_64") add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linker.ld COMMAND "${CMAKE_CXX_COMPILER}" ${TARGET_STRING} -E -P -x c -I${CMAKE_CURRENT_SOURCE_DIR}/.. "${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86_64/linker.ld" -o "${CMAKE_CURRENT_BINARY_DIR}/linker.ld" @@ -730,9 +736,6 @@ if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64") elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang$") target_compile_options(Kernel PRIVATE -mstack-alignment=8) endif() -else() - target_link_options(Kernel PRIVATE LINKER:-T ${CMAKE_CURRENT_SOURCE_DIR}/Arch/aarch64/linker.ld -nostdlib LINKER:--no-pie) - set_target_properties(Kernel PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Arch/aarch64/linker.ld) endif() if (ENABLE_KERNEL_LTO)