Skip to content

Commit

Permalink
elf: add support for csky arch (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirreke committed Jul 18, 2023
1 parent b237c5c commit 5754050
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Architecture {
Arm,
Avr,
Bpf,
Csky,
I386,
X86_64,
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/read/elf/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ fn parse_relocation<Elf: FileHeader>(
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),
Expand Down
12 changes: 12 additions & 0 deletions src/write/elf/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
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 @@ -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),
Expand Down

0 comments on commit 5754050

Please sign in to comment.