Skip to content

Commit

Permalink
pref(build): 尝试解决全局构造问题,此版本现象:
Browse files Browse the repository at this point in the history
- riscv

    非 static 对象可以正常构造,static 对象会在 cpp_init 死循环,原因未知

- x8_64

    只要执行 cpp_init 重的 f 就会 GP,开启 -no-pie 会直接进不去 elf

Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Apr 28, 2024
1 parent bfb8fd1 commit 8c865a1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmake/compile_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ list(APPEND DEFAULT_KERNEL_COMPILE_OPTIONS
$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},x86_64>:
# 禁用 red-zone
-mno-red-zone
# 生成位置无关代码
-fPIC
>

Expand Down
9 changes: 9 additions & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ add_dependencies(kernel
set_target_properties(kernel PROPERTIES PREFIX "")
set_target_properties(kernel PROPERTIES OUTPUT_NAME ${KERNEL_ELF_OUTPUT_NAME})

## 获取 crt 文件位置
#execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crti.o OUTPUT_VARIABLE CRTI_O OUTPUT_STRIP_TRAILING_WHITESPACE)
#execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtbegin.o OUTPUT_VARIABLE CRTBEGIN_O OUTPUT_STRIP_TRAILING_WHITESPACE)
#execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtend.o OUTPUT_VARIABLE CRTEND_O OUTPUT_STRIP_TRAILING_WHITESPACE)
#execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtn.o OUTPUT_VARIABLE CRTN_O OUTPUT_STRIP_TRAILING_WHITESPACE)
#
## 设置链接指令
#set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} <CMAKE_C_LINK_FLAGS> <FLAGS> <LINK_FLAGS> ${CRTI_O} ${CRTBEGIN_O} <OBJECTS> <LINK_LIBRARIES> ${CRTEND_O} ${CRTN_O} -o <TARGET>")

# readelf -a
readelf_a(kernel)

Expand Down
2 changes: 1 addition & 1 deletion src/kernel/libcxx/libcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern ctor_t __init_array_end[];
void cpp_init(void) {
ctor_t* f;
for (f = __init_array_start; f < __init_array_end; f++) {
(*f)();
// (*f)();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class aaa {
};

auto class_a = aaa();
auto class_a2 = aaa(1);
static auto class_a2 = aaa(1);

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_a2.a: %d\n", class_a2.a);
// printf("class_a2.a: %d\n", class_a2.a);

printf("------\n");

Expand Down

0 comments on commit 8c865a1

Please sign in to comment.