Skip to content

Commit

Permalink
Adding aarch64-darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
Evian-Zhang committed Jul 25, 2024
1 parent 9de0815 commit 6b31886
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 11 deletions.
68 changes: 68 additions & 0 deletions src/arch/aarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! AArch64 arch-sepcific implementations
use crate::{JumpEntry, JumpLabelType};

/// Length of jump instruction to be replaced
pub const ARCH_JUMP_INS_LENGTH: usize = 4;

/// New instruction generated according to jump label type and jump entry
#[inline(always)]
pub fn arch_jump_entry_instruction(
jump_label_type: JumpLabelType,
jump_entry: &JumpEntry,
) -> [u8; ARCH_JUMP_INS_LENGTH] {
match jump_label_type {
JumpLabelType::Jmp => {
// Note that aarch64 only supports relative address within +/-128MB.
// In current implementation, this assumption is always hold.
let relative_addr =
(jump_entry.target_addr() - (jump_entry.code_addr() + ARCH_JUMP_INS_LENGTH)) as u32;
let [a, b, c, d] = (relative_addr / 4).to_ne_bytes();
[a, b, c, d | 0b00010100]
}
JumpLabelType::Nop => [0x1f, 0x20, 0x03, 0xd5],
}
}

#[doc(hidden)]
#[macro_export]
macro_rules! arch_static_key_init_nop_asm_template {
() => {
::core::concat!(
r#"
2:
nop
.pushsection "#,
$crate::os_static_key_sec_name_attr!(),
r#"
.balign 8
.quad 2b - .
.quad {0} - .
.quad {1} + {2} - .
.popsection
"#
)
};
}

// The `0x90,0x90,0x90` are three NOPs, which is to make sure the `jmp {0}` is at least 5 bytes long.
#[doc(hidden)]
#[macro_export]
macro_rules! arch_static_key_init_jmp_asm_template {
() => {
::core::concat!(
r#"
2:
b {0}
.pushsection "#,
$crate::os_static_key_sec_name_attr!(),
r#"
.balign 8
.quad 2b - .
.quad {0} - .
.quad {1} + {2} - .
.popsection
"#
)
};
}
5 changes: 5 additions & 0 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pub use x86_64::*;
mod x86;
#[cfg(target_arch = "x86")]
pub use x86::*;

#[cfg(target_arch = "aarch64")]
mod aarch64;
#[cfg(target_arch = "aarch64")]
pub use aarch64::*;
8 changes: 4 additions & 4 deletions src/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ macro_rules! arch_static_key_init_nop_asm_template {
2:
.byte 0x3e,0x8d,0x74,0x26,0x00
.pushsection "#,
$crate::os_static_key_sec_name!(),
r#", "awR"
$crate::os_static_key_sec_name_attr!(),
r#"
.balign 4
.long 2b - .
.long {0} - .
Expand All @@ -54,8 +54,8 @@ macro_rules! arch_static_key_init_jmp_asm_template {
jmp {0}
.byte 0x90,0x90,0x90
.pushsection "#,
$crate::os_static_key_sec_name!(),
r#", "awR"
$crate::os_static_key_sec_name_attr!(),
r#"
.balign 4
.long 2b - .
.long {0} - .
Expand Down
8 changes: 4 additions & 4 deletions src/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ macro_rules! arch_static_key_init_nop_asm_template {
2:
.byte 0x0f,0x1f,0x44,0x00,0x00
.pushsection "#,
$crate::os_static_key_sec_name!(),
r#", "awR"
$crate::os_static_key_sec_name_attr!(),
r#"
.balign 8
.quad 2b - .
.quad {0} - .
Expand All @@ -54,8 +54,8 @@ macro_rules! arch_static_key_init_jmp_asm_template {
jmp {0}
.byte 0x90,0x90,0x90
.pushsection "#,
$crate::os_static_key_sec_name!(),
r#", "awR"
$crate::os_static_key_sec_name_attr!(),
r#"
.balign 8
.quad 2b - .
.quad {0} - .
Expand Down
7 changes: 4 additions & 3 deletions src/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
use crate::JumpEntry;

/// Section name of
// See https://sourceware.org/binutils/docs/as/Section.html
/// Name and attribute of section storing jump entries
#[doc(hidden)]
#[macro_export]
macro_rules! os_static_key_sec_name {
macro_rules! os_static_key_sec_name_attr {
() => {
"__static_keys"
"__static_keys, \"awR\""
};
}

Expand Down
23 changes: 23 additions & 0 deletions src/os/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! macOS-specific implementations
use crate::JumpEntry;

// See https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html#//apple_ref/doc/uid/TP30000823-CJBIFBJG
/// Name and attribute of section storing jump entries
#[doc(hidden)]
#[macro_export]
macro_rules! os_static_key_sec_name_attr {
() => {
"__DATA,__static_keys,regular,no_dead_strip"
};
}

// See https://stackoverflow.com/q/17669593/10005095 and https://github.com/apple-opensource-mirror/ld64/blob/master/unit-tests/test-cases/section-labels/main.c
extern "Rust" {
/// Address of this static is the start address of __static_keys section
#[link_name = "\x01section$start$__DATA$__static_keys"]
pub static mut JUMP_ENTRY_START: JumpEntry;
/// Address of this static is the end address of __static_keys section (excluded)
#[link_name = "\x01section$end$__DATA$__static_keys"]
pub static mut JUMP_ENTRY_STOP: JumpEntry;
}
5 changes: 5 additions & 0 deletions src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
mod linux;
#[cfg(target_os = "linux")]
pub use linux::*;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;

0 comments on commit 6b31886

Please sign in to comment.