From 57540508ab8654cec7ca20fa6609929785d1286b Mon Sep 17 00:00:00 2001 From: Dirreck Date: Tue, 18 Jul 2023 18:29:54 +0800 Subject: [PATCH] elf: add support for csky arch (#561) --- src/common.rs | 2 ++ src/read/elf/file.rs | 1 + src/read/elf/relocation.rs | 5 +++++ src/write/elf/object.rs | 12 ++++++++++++ tests/round_trip/mod.rs | 1 + 5 files changed, 21 insertions(+) diff --git a/src/common.rs b/src/common.rs index 7d789a71..0e6af091 100644 --- a/src/common.rs +++ b/src/common.rs @@ -10,6 +10,7 @@ pub enum Architecture { Arm, Avr, Bpf, + Csky, I386, X86_64, #[allow(non_camel_case_types)] @@ -43,6 +44,7 @@ impl Architecture { Architecture::Arm => Some(AddressSize::U32), Architecture::Avr => Some(AddressSize::U8), Architecture::Bpf => Some(AddressSize::U64), + Architecture::Csky => Some(AddressSize::U32), Architecture::I386 => Some(AddressSize::U32), Architecture::X86_64 => Some(AddressSize::U64), Architecture::X86_64_X32 => Some(AddressSize::U32), diff --git a/src/read/elf/file.rs b/src/read/elf/file.rs index aac66e7c..67be37e2 100644 --- a/src/read/elf/file.rs +++ b/src/read/elf/file.rs @@ -161,6 +161,7 @@ where (elf::EM_ARM, _) => Architecture::Arm, (elf::EM_AVR, _) => Architecture::Avr, (elf::EM_BPF, _) => Architecture::Bpf, + (elf::EM_CSKY, _) => Architecture::Csky, (elf::EM_386, _) => Architecture::I386, (elf::EM_X86_64, false) => Architecture::X86_64_X32, (elf::EM_X86_64, true) => Architecture::X86_64, diff --git a/src/read/elf/relocation.rs b/src/read/elf/relocation.rs index 8443dbc7..78032dfd 100644 --- a/src/read/elf/relocation.rs +++ b/src/read/elf/relocation.rs @@ -276,6 +276,11 @@ fn parse_relocation( elf::R_BPF_64_32 => (RelocationKind::Absolute, 32), r_type => (RelocationKind::Elf(r_type), 0), }, + elf::EM_CSKY => match reloc.r_type(endian, false) { + elf::R_CKCORE_ADDR32 => (RelocationKind::Absolute, 32), + elf::R_CKCORE_PCREL32 => (RelocationKind::Relative, 32), + r_type => (RelocationKind::Elf(r_type), 0), + }, elf::EM_386 => match reloc.r_type(endian, false) { elf::R_386_32 => (RelocationKind::Absolute, 32), elf::R_386_PC32 => (RelocationKind::Relative, 32), diff --git a/src/write/elf/object.rs b/src/write/elf/object.rs index 8b9eadaf..acc820c9 100644 --- a/src/write/elf/object.rs +++ b/src/write/elf/object.rs @@ -126,6 +126,7 @@ impl<'a> Object<'a> { Architecture::Arm => false, Architecture::Avr => true, Architecture::Bpf => false, + Architecture::Csky => true, Architecture::I386 => false, Architecture::X86_64 => true, Architecture::X86_64_X32 => true, @@ -329,6 +330,7 @@ impl<'a> Object<'a> { Architecture::Arm => elf::EM_ARM, Architecture::Avr => elf::EM_AVR, Architecture::Bpf => elf::EM_BPF, + Architecture::Csky => elf::EM_CSKY, Architecture::I386 => elf::EM_386, Architecture::X86_64 => elf::EM_X86_64, Architecture::X86_64_X32 => elf::EM_X86_64, @@ -548,6 +550,16 @@ impl<'a> Object<'a> { return Err(Error(format!("unimplemented relocation {:?}", reloc))); } }, + Architecture::Csky => match (reloc.kind, reloc.encoding, reloc.size) { + (RelocationKind::Absolute, _, 32) => elf::R_CKCORE_ADDR32, + (RelocationKind::Relative, RelocationEncoding::Generic, 32) => { + elf::R_CKCORE_PCREL32 + } + (RelocationKind::Elf(x), _, _) => x, + _ => { + return Err(Error(format!("unimplemented relocation {:?}", reloc))); + } + }, Architecture::I386 => match (reloc.kind, reloc.size) { (RelocationKind::Absolute, 32) => elf::R_386_32, (RelocationKind::Relative, 32) => elf::R_386_PC32, diff --git a/tests/round_trip/mod.rs b/tests/round_trip/mod.rs index 8f8dd79c..cd696f60 100644 --- a/tests/round_trip/mod.rs +++ b/tests/round_trip/mod.rs @@ -235,6 +235,7 @@ fn elf_any() { (Architecture::Arm, Endianness::Little), (Architecture::Avr, Endianness::Little), (Architecture::Bpf, Endianness::Little), + (Architecture::Csky, Endianness::Little), (Architecture::I386, Endianness::Little), (Architecture::X86_64, Endianness::Little), (Architecture::X86_64_X32, Endianness::Little),