Skip to content

Commit

Permalink
write/macho: Support the arm64e architecture for macOS/iOS (gimli-rs#574
Browse files Browse the repository at this point in the history
)
  • Loading branch information
arttet committed Sep 3, 2023
1 parent a2a00b0 commit 6fd8ee8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,7 @@ pub const GNU_PROPERTY_HIUSER: u32 = 0xffffffff;

/// AArch64 specific GNU properties.
pub const GNU_PROPERTY_AARCH64_FEATURE_1_AND: u32 = 0xc0000000;
pub const GNU_PROPERTY_AARCH64_FEATURE_PAUTH: u32 = 0xc0000001;

pub const GNU_PROPERTY_AARCH64_FEATURE_1_BTI: u32 = 1 << 0;
pub const GNU_PROPERTY_AARCH64_FEATURE_1_PAC: u32 = 1 << 1;
Expand Down
2 changes: 1 addition & 1 deletion src/write/coff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a> Object<'a> {
name
}

pub(crate) fn coff_fixup_relocation(&mut self, mut relocation: &mut Relocation) -> i64 {
pub(crate) fn coff_fixup_relocation(&mut self, relocation: &mut Relocation) -> i64 {
if relocation.kind == RelocationKind::GotRelative {
// Use a stub symbol for the relocation instead.
// This isn't really a GOT, but it's a similar purpose.
Expand Down
2 changes: 1 addition & 1 deletion src/write/elf/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<'a> Object<'a> {
})
}

pub(crate) fn elf_fixup_relocation(&mut self, mut relocation: &mut Relocation) -> Result<i64> {
pub(crate) fn elf_fixup_relocation(&mut self, relocation: &mut Relocation) -> Result<i64> {
// Return true if we should use a section symbol to avoid preemption.
fn want_section_symbol(relocation: &Relocation, symbol: &Symbol) -> bool {
if symbol.scope != SymbolScope::Dynamic {
Expand Down
16 changes: 14 additions & 2 deletions src/write/macho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ impl MachOBuildVersion {

// Public methods.
impl<'a> Object<'a> {
/// Specify the Mach-O CPU subtype.
///
/// Requires `feature = "macho"`.
#[inline]
pub fn set_macho_cpu_subtype(&mut self, cpu_subtype: u32) {
self.macho_cpu_subtype = Some(cpu_subtype);
}

/// Specify information for a Mach-O `LC_BUILD_VERSION` command.
///
/// Requires `feature = "macho"`.
Expand Down Expand Up @@ -243,7 +251,7 @@ impl<'a> Object<'a> {
init_symbol_id
}

pub(crate) fn macho_fixup_relocation(&mut self, mut relocation: &mut Relocation) -> i64 {
pub(crate) fn macho_fixup_relocation(&mut self, relocation: &mut Relocation) -> i64 {
let constant = match relocation.kind {
// AArch64Call relocations have special handling for the addend, so don't adjust it
RelocationKind::Relative if relocation.encoding == RelocationEncoding::AArch64Call => 0,
Expand Down Expand Up @@ -385,7 +393,7 @@ impl<'a> Object<'a> {
.map_err(|_| Error(String::from("Cannot allocate buffer")))?;

// Write file header.
let (cputype, cpusubtype) = match self.architecture {
let (cputype, mut cpusubtype) = match self.architecture {
Architecture::Arm => (macho::CPU_TYPE_ARM, macho::CPU_SUBTYPE_ARM_ALL),
Architecture::Aarch64 => (macho::CPU_TYPE_ARM64, macho::CPU_SUBTYPE_ARM64_ALL),
Architecture::Aarch64_Ilp32 => {
Expand All @@ -403,6 +411,10 @@ impl<'a> Object<'a> {
}
};

if let Some(cpu_subtype) = self.macho_cpu_subtype {
cpusubtype = cpu_subtype;
}

let flags = match self.flags {
FileFlags::MachO { flags } => flags,
_ => 0,
Expand Down
5 changes: 5 additions & 0 deletions src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub struct Object<'a> {
pub mangling: Mangling,
/// Mach-O "_tlv_bootstrap" symbol.
tlv_bootstrap: Option<SymbolId>,
/// Mach-O CPU subtype.
#[cfg(feature = "macho")]
macho_cpu_subtype: Option<u32>,
#[cfg(feature = "macho")]
macho_build_version: Option<MachOBuildVersion>,
}
Expand All @@ -96,6 +99,8 @@ impl<'a> Object<'a> {
mangling: Mangling::default(format, architecture),
tlv_bootstrap: None,
#[cfg(feature = "macho")]
macho_cpu_subtype: None,
#[cfg(feature = "macho")]
macho_build_version: None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/write/xcoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a> Object<'a> {
}
}

pub(crate) fn xcoff_fixup_relocation(&mut self, mut relocation: &mut Relocation) -> i64 {
pub(crate) fn xcoff_fixup_relocation(&mut self, relocation: &mut Relocation) -> i64 {
let constant = match relocation.kind {
RelocationKind::Relative => relocation.addend + 4,
_ => relocation.addend,
Expand Down

0 comments on commit 6fd8ee8

Please sign in to comment.