Skip to content

Commit

Permalink
Add support for AArch64 ILP32 for ELF (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu authored Jan 4, 2023
1 parent b3096ce commit d4482ec
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
pub enum Architecture {
Unknown,
Aarch64,
#[allow(non_camel_case_types)]
Aarch64_Ilp32,
Arm,
Avr,
Bpf,
Expand Down Expand Up @@ -36,6 +38,7 @@ impl Architecture {
match self {
Architecture::Unknown => None,
Architecture::Aarch64 => Some(AddressSize::U64),
Architecture::Aarch64_Ilp32 => Some(AddressSize::U32),
Architecture::Arm => Some(AddressSize::U32),
Architecture::Avr => Some(AddressSize::U8),
Architecture::Bpf => Some(AddressSize::U64),
Expand Down
3 changes: 2 additions & 1 deletion src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ where
self.header.e_machine(self.endian),
self.header.is_class_64(),
) {
(elf::EM_AARCH64, _) => Architecture::Aarch64,
(elf::EM_AARCH64, true) => Architecture::Aarch64,
(elf::EM_AARCH64, false) => Architecture::Aarch64_Ilp32,
(elf::EM_ARM, _) => Architecture::Arm,
(elf::EM_AVR, _) => Architecture::Avr,
(elf::EM_BPF, _) => Architecture::Bpf,
Expand Down
33 changes: 21 additions & 12 deletions src/read/elf/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,28 @@ fn parse_relocation<Elf: FileHeader>(
let mut encoding = RelocationEncoding::Generic;
let is_mips64el = header.is_mips64el(endian);
let (kind, size) = match header.e_machine(endian) {
elf::EM_AARCH64 => match reloc.r_type(endian, false) {
elf::R_AARCH64_ABS64 => (RelocationKind::Absolute, 64),
elf::R_AARCH64_ABS32 => (RelocationKind::Absolute, 32),
elf::R_AARCH64_ABS16 => (RelocationKind::Absolute, 16),
elf::R_AARCH64_PREL64 => (RelocationKind::Relative, 64),
elf::R_AARCH64_PREL32 => (RelocationKind::Relative, 32),
elf::R_AARCH64_PREL16 => (RelocationKind::Relative, 16),
elf::R_AARCH64_CALL26 => {
encoding = RelocationEncoding::AArch64Call;
(RelocationKind::PltRelative, 26)
elf::EM_AARCH64 => {
if header.is_type_64() {
match reloc.r_type(endian, false) {
elf::R_AARCH64_ABS64 => (RelocationKind::Absolute, 64),
elf::R_AARCH64_ABS32 => (RelocationKind::Absolute, 32),
elf::R_AARCH64_ABS16 => (RelocationKind::Absolute, 16),
elf::R_AARCH64_PREL64 => (RelocationKind::Relative, 64),
elf::R_AARCH64_PREL32 => (RelocationKind::Relative, 32),
elf::R_AARCH64_PREL16 => (RelocationKind::Relative, 16),
elf::R_AARCH64_CALL26 => {
encoding = RelocationEncoding::AArch64Call;
(RelocationKind::PltRelative, 26)
}
r_type => (RelocationKind::Elf(r_type), 0),
}
} else {
match reloc.r_type(endian, false) {
elf::R_AARCH64_P32_ABS32 => (RelocationKind::Absolute, 32),
r_type => (RelocationKind::Elf(r_type), 0),
}
}
r_type => (RelocationKind::Elf(r_type), 0),
},
}
elf::EM_ARM => match reloc.r_type(endian, false) {
elf::R_ARM_ABS32 => (RelocationKind::Absolute, 32),
r_type => (RelocationKind::Elf(r_type), 0),
Expand Down
16 changes: 16 additions & 0 deletions src/write/elf/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl<'a> Object<'a> {
fn elf_has_relocation_addend(&self) -> Result<bool> {
Ok(match self.architecture {
Architecture::Aarch64 => true,
Architecture::Aarch64_Ilp32 => true,
Architecture::Arm => false,
Architecture::Avr => true,
Architecture::Bpf => false,
Expand Down Expand Up @@ -266,6 +267,7 @@ impl<'a> Object<'a> {
let e_type = elf::ET_REL;
let e_machine = match self.architecture {
Architecture::Aarch64 => elf::EM_AARCH64,
Architecture::Aarch64_Ilp32 => elf::EM_AARCH64,
Architecture::Arm => elf::EM_ARM,
Architecture::Avr => elf::EM_AVR,
Architecture::Bpf => elf::EM_BPF,
Expand Down Expand Up @@ -454,6 +456,20 @@ impl<'a> Object<'a> {
return Err(Error(format!("unimplemented relocation {:?}", reloc)));
}
},
Architecture::Aarch64_Ilp32 => {
match (reloc.kind, reloc.encoding, reloc.size) {
(RelocationKind::Absolute, RelocationEncoding::Generic, 32) => {
elf::R_AARCH64_P32_ABS32
}
(RelocationKind::Elf(x), _, _) => x,
_ => {
return Err(Error(format!(
"unimplemented relocation {:?}",
reloc
)));
}
}
}
Architecture::Arm => match (reloc.kind, reloc.encoding, reloc.size) {
(RelocationKind::Absolute, _, 32) => elf::R_ARM_ABS32,
(RelocationKind::Elf(x), _, _) => x,
Expand Down
1 change: 1 addition & 0 deletions tests/round_trip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ fn elf_x86_64() {
fn elf_any() {
for (arch, endian) in [
(Architecture::Aarch64, Endianness::Little),
(Architecture::Aarch64_Ilp32, Endianness::Little),
(Architecture::Arm, Endianness::Little),
(Architecture::Avr, Endianness::Little),
(Architecture::Bpf, Endianness::Little),
Expand Down

0 comments on commit d4482ec

Please sign in to comment.