Skip to content

Commit

Permalink
feat(cmake): add USE_NO_RELAX flag
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed May 24, 2024
1 parent 7c54112 commit 9b28252
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
"CMAKE_SYSTEM_PROCESSOR": {
"type": "STRING",
"value": "riscv64"
},
"USE_NO_RELAX": {
"type": "BOOL",
"value": "OFF"
}
}
},
Expand Down
17 changes: 16 additions & 1 deletion cmake/compile_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ list(APPEND COMMON_LINK_OPTIONS
list(APPEND COMMON_LINK_LIB
)

# 通用宏定义
list(APPEND COMMON_DEFINITIONS
)

list(APPEND DEFAULT_BOOT_DEFINITIONS
${COMMON_DEFINITIONS}
)

list(APPEND DEFAULT_BOOT_COMPILE_OPTIONS
${COMMON_COMPILE_OPTIONS}
# 使用 2 字节 wchar_t
Expand Down Expand Up @@ -109,6 +117,12 @@ list(APPEND DEFAULT_BOOT_LINK_LIB
>
)

list(APPEND DEFAULT_KERNEL_DEFINITIONS
${COMMON_DEFINITIONS}

USE_NO_RELAX=$<BOOL:${USE_NO_RELAX}>
)

list(APPEND DEFAULT_KERNEL_COMPILE_OPTIONS
${COMMON_COMPILE_OPTIONS}

Expand Down Expand Up @@ -145,8 +159,9 @@ list(APPEND DEFAULT_KERNEL_LINK_OPTIONS
$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},riscv64>:
# 链接脚本
-T ${CMAKE_SOURCE_DIR}/src/kernel/arch/${CMAKE_SYSTEM_PROCESSOR}/link.ld

# 禁用 relax 优化
-mno-relax
$<$<BOOL:${USE_NO_RELAX}>:-mno-relax>
>
)

Expand Down
5 changes: 5 additions & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ add_header_kernel(kernel)
add_header_driver(kernel)
add_header_3rd(kernel)

# 添加定义
target_compile_definitions(kernel PRIVATE
${DEFAULT_KERNEL_DEFINITIONS}
)

# 添加编译参数
target_compile_options(kernel PRIVATE
${DEFAULT_KERNEL_COMPILE_OPTIONS}
Expand Down
5 changes: 5 additions & 0 deletions src/kernel/arch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ add_library(${PROJECT_NAME} OBJECT
${PROJECT_SOURCE_DIR}/${CMAKE_SYSTEM_PROCESSOR}/arch_main.cpp
)

# 添加定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_DEFINITIONS}
)

# 添加头文件
add_header_libc(${PROJECT_NAME})
add_header_libcxx(${PROJECT_NAME})
Expand Down
13 changes: 13 additions & 0 deletions src/kernel/arch/riscv64/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@
.global _boot
.type _boot, @function
.extern _start
#if USE_NO_RELAX == 0
.extern __global_pointer$
#endif
_boot:

#if USE_NO_RELAX == 0
// 初始化 gp 寄存器
// 如果不初始化,则需要在编译时添加 -mno-relax 选项
.option push
.option norelax
la gp, __global_pointer$
.option pop
#endif

// 设置栈地址
la sp, stack_top

Expand Down
5 changes: 5 additions & 0 deletions src/kernel/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ add_library(${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/driver.cpp
)

# 添加定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_DEFINITIONS}
)

# 添加头文件
add_header_driver(${PROJECT_NAME})

Expand Down
5 changes: 5 additions & 0 deletions src/kernel/driver/ns16550a/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ add_library(${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/ns16550a.cpp
)

# 添加定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_DEFINITIONS}
)

# 添加头文件
add_header_driver(${PROJECT_NAME})

Expand Down
5 changes: 5 additions & 0 deletions src/kernel/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ add_library(${PROJECT_NAME} OBJECT
${PROJECT_SOURCE_DIR}/stdio.c
)

# 添加定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_DEFINITIONS}
)

# 添加头文件
add_header_libc(${PROJECT_NAME})

Expand Down
5 changes: 5 additions & 0 deletions src/kernel/libcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ add_library(${PROJECT_NAME} OBJECT
${PROJECT_SOURCE_DIR}/libcxx.cpp
)

# 添加定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
${DEFAULT_KERNEL_DEFINITIONS}
)

# 添加头文件
add_header_libc(${PROJECT_NAME})
add_header_libcxx(${PROJECT_NAME})
Expand Down
1 change: 1 addition & 0 deletions src/kernel/libcxx/libcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern ctor_t __init_array_end[];

void cpp_init(void) {
for (auto ctor = __init_array_start; ctor < __init_array_end; ctor++) {
/// @todo x86_64 crashed
(*ctor)();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ class aaa {
aaa(int _a) : a(_a) { printf("aaa init %d\n", _a); }
};

// auto class_a = aaa(2);
// static aaa class_a2 = aaa(3);
auto class_a = aaa(2);
static aaa class_a2 = aaa(3);
int i32;
static int si32;

int main(int _argc, char** _argv) {
// 架构相关初始化
auto arch_init_ret = arch_init(_argc, reinterpret_cast<uint8_t**>(_argv));

// printf("class_a.a: %d\n", class_a.a);
// printf("&class_a: %p\n", &class_a);
// printf("&class_a2: %p\n", &class_a2);
printf("class_a.a: %d\n", class_a.a);
printf("&class_a: %p\n", &class_a);
printf("&class_a2: %p\n", &class_a2);
printf("&i32: %p\n", &i32);
printf("&si32: %p\n", &si32);
printf("*(&si32): %p\n", *(&si32));
Expand Down

0 comments on commit 9b28252

Please sign in to comment.