Skip to content

Commit

Permalink
Produces PIE kernel for AArch64 (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Aug 15, 2024
1 parent 9ac31cd commit 5042bdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ jobs:
if: ${{ steps.qt-cache.outputs.cache-hit != 'true' }}
- name: Update Rust
run: rustup update stable
- name: Add additional Rust targets
- name: Install Rust nightly
run: rustup toolchain install nightly
- name: Install additional Rust components
run: rustup component add rust-src --toolchain nightly
- name: Install additional Rust targets
run: rustup target add ${{ inputs.kernel-target }}
- name: Run CMake
run: cmake --preset mac-release .
Expand Down
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ else()
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
set(KERNEL_TARGET x86_64-unknown-none)
elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
# Pre-compiled core crate for aarch64-unknown-none-softfloat does not support
# Position-Independent Executable so we need nightly toolchain for build-std feature to
# re-build core crate to support Position-Independent Executable.
set(KERNEL_TARGET aarch64-unknown-none-softfloat)
set(KERNEL_TOOLCHAIN +nightly)
set(KERNEL_OPTS -Z build-std=core,alloc)
else()
message(FATAL_ERROR "Target CPU is not supported")
endif()
Expand All @@ -39,7 +44,7 @@ add_custom_target(core
BYPRODUCTS ${LIBCORE})

add_custom_target(kernel
COMMAND cargo build $<IF:$<CONFIG:Debug>,--profile=dev,--release> --target ${KERNEL_TARGET}
COMMAND cargo ${KERNEL_TOOLCHAIN} build $<IF:$<CONFIG:Debug>,--profile=dev,--release> --target ${KERNEL_TARGET} ${KERNEL_OPTS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/obkrnl
BYPRODUCTS ${KERNEL})

Expand Down
2 changes: 2 additions & 0 deletions src/obkrnl/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.aarch64-unknown-none-softfloat]
rustflags = ["-C", "relocation-model=pic"]
16 changes: 12 additions & 4 deletions src/obkrnl/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
fn main() {
let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let target = std::env::var("TARGET").unwrap();

if os == "none" {
println!("cargo::rustc-link-arg-bins=-zcommon-page-size=0x4000");
println!("cargo::rustc-link-arg-bins=-zmax-page-size=0x4000");
match target.as_str() {
"aarch64-unknown-none-softfloat" => {
println!("cargo::rustc-link-arg-bins=--pie");
println!("cargo::rustc-link-arg-bins=-zcommon-page-size=0x4000");
println!("cargo::rustc-link-arg-bins=-zmax-page-size=0x4000");
}
"x86_64-unknown-none" => {
println!("cargo::rustc-link-arg-bins=-zcommon-page-size=0x4000");
println!("cargo::rustc-link-arg-bins=-zmax-page-size=0x4000");
}
_ => {}
}
}

0 comments on commit 5042bdc

Please sign in to comment.