From 9bb71af6f977d34e22d6273c23140c97d7c2eeb5 Mon Sep 17 00:00:00 2001 From: Dirreke Date: Fri, 14 Jul 2023 15:42:27 +0800 Subject: [PATCH 1/3] add initial support for csky elf --- src/common.rs | 2 ++ src/read/elf/relocation.rs | 5 +++++ src/write/elf/object.rs | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/common.rs b/src/common.rs index 7d789a71..1cab9f6c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -29,6 +29,7 @@ pub enum Architecture { Wasm32, Wasm64, Xtensa, + CSKY, } impl Architecture { @@ -61,6 +62,7 @@ impl Architecture { Architecture::Wasm32 => Some(AddressSize::U32), Architecture::Wasm64 => Some(AddressSize::U64), Architecture::Xtensa => Some(AddressSize::U32), + Architecture::CSKY => Some(AddressSize::U32), } } } diff --git a/src/read/elf/relocation.rs b/src/read/elf/relocation.rs index 8443dbc7..34c57bd5 100644 --- a/src/read/elf/relocation.rs +++ b/src/read/elf/relocation.rs @@ -411,6 +411,11 @@ fn parse_relocation( elf::R_XTENSA_32_PCREL => (RelocationKind::Relative, 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), + }, _ => (RelocationKind::Elf(reloc.r_type(endian, false)), 0), }; let sym = reloc.r_sym(endian, is_mips64el) as usize; diff --git a/src/write/elf/object.rs b/src/write/elf/object.rs index 8b9eadaf..8f4055f0 100644 --- a/src/write/elf/object.rs +++ b/src/write/elf/object.rs @@ -142,6 +142,7 @@ impl<'a> Object<'a> { Architecture::Sbf => false, Architecture::Sparc64 => true, Architecture::Xtensa => true, + Architecture::CSKY => true, _ => { return Err(Error(format!( "unimplemented architecture {:?}", @@ -345,6 +346,7 @@ impl<'a> Object<'a> { Architecture::Sbf => elf::EM_SBF, Architecture::Sparc64 => elf::EM_SPARCV9, Architecture::Xtensa => elf::EM_XTENSA, + Architecture::CSKY => elf::EM_CSKY, _ => { return Err(Error(format!( "unimplemented architecture {:?}", @@ -773,6 +775,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))); + } + }, _ => { if let RelocationKind::Elf(x) = reloc.kind { x From 1e59e68b1bd21f572fca53ee97b76a9f350a8fac Mon Sep 17 00:00:00 2001 From: Dirreke Date: Fri, 14 Jul 2023 19:12:10 +0800 Subject: [PATCH 2/3] change name CSKY to Csky --- src/common.rs | 4 ++-- src/read/elf/relocation.rs | 10 +++++----- src/write/elf/object.rs | 24 ++++++++++++------------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/common.rs b/src/common.rs index 1cab9f6c..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)] @@ -29,7 +30,6 @@ pub enum Architecture { Wasm32, Wasm64, Xtensa, - CSKY, } impl Architecture { @@ -44,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), @@ -62,7 +63,6 @@ impl Architecture { Architecture::Wasm32 => Some(AddressSize::U32), Architecture::Wasm64 => Some(AddressSize::U64), Architecture::Xtensa => Some(AddressSize::U32), - Architecture::CSKY => Some(AddressSize::U32), } } } diff --git a/src/read/elf/relocation.rs b/src/read/elf/relocation.rs index 34c57bd5..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), @@ -411,11 +416,6 @@ fn parse_relocation( elf::R_XTENSA_32_PCREL => (RelocationKind::Relative, 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), - }, _ => (RelocationKind::Elf(reloc.r_type(endian, false)), 0), }; let sym = reloc.r_sym(endian, is_mips64el) as usize; diff --git a/src/write/elf/object.rs b/src/write/elf/object.rs index 8f4055f0..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, @@ -142,7 +143,6 @@ impl<'a> Object<'a> { Architecture::Sbf => false, Architecture::Sparc64 => true, Architecture::Xtensa => true, - Architecture::CSKY => true, _ => { return Err(Error(format!( "unimplemented architecture {:?}", @@ -330,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, @@ -346,7 +347,6 @@ impl<'a> Object<'a> { Architecture::Sbf => elf::EM_SBF, Architecture::Sparc64 => elf::EM_SPARCV9, Architecture::Xtensa => elf::EM_XTENSA, - Architecture::CSKY => elf::EM_CSKY, _ => { return Err(Error(format!( "unimplemented architecture {:?}", @@ -550,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, @@ -775,16 +785,6 @@ 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))); - } - }, _ => { if let RelocationKind::Elf(x) = reloc.kind { x From cb62876be1f0cfc8a754a8765897694e6eae675b Mon Sep 17 00:00:00 2001 From: Dirreke Date: Tue, 18 Jul 2023 17:05:30 +0800 Subject: [PATCH 3/3] add missing impl --- src/read/elf/file.rs | 1 + tests/round_trip/mod.rs | 1 + 2 files changed, 2 insertions(+) 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/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),