diff --git a/lib/elftools/constants.rb b/lib/elftools/constants.rb index 2e2f77d..67bf2fe 100644 --- a/lib/elftools/constants.rb +++ b/lib/elftools/constants.rb @@ -376,6 +376,7 @@ module EM EM_FT32 = 222 # FTDI Chip FT32 high performance 32-bit RISC architecture EM_MOXIE = 223 # Moxie processor family EM_AMDGPU = 224 # AMD GPU architecture + EM_RISCV = 243 # RISC-V EM_LANAI = 244 # Lanai 32-bit processor EM_CEVA = 245 # CEVA Processor Architecture Family EM_CEVA_X2 = 246 # CEVA X2 Processor Family @@ -436,6 +437,7 @@ def self.mapping(val) when EM_IA_64 then 'Intel IA-64' when EM_AARCH64 then 'AArch64' when EM_X86_64 then 'Advanced Micro Devices X86-64' + when EM_RISCV then 'RISC-V' else format(': 0x%x', val) end end diff --git a/spec/files/hello.c b/spec/files/hello.c new file mode 100644 index 0000000..964aa1b --- /dev/null +++ b/spec/files/hello.c @@ -0,0 +1,7 @@ +#include + +int main(int argc, char **argv) +{ + printf("Hello world, from a RISC-V binary!\n"); + return 0; +} diff --git a/spec/files/riscv64.elf b/spec/files/riscv64.elf new file mode 100755 index 0000000..6eea9fc Binary files /dev/null and b/spec/files/riscv64.elf differ diff --git a/spec/full_test/riscv64_spec.rb b/spec/full_test/riscv64_spec.rb new file mode 100644 index 0000000..c826e35 --- /dev/null +++ b/spec/full_test/riscv64_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'elftools' + +describe 'Full test for riscv64' do + before(:all) do + path = File.join(__dir__, '..', 'files', 'riscv64.elf') + @elf = ELFTools::ELFFile.new(File.open(path)) + end + + it 'elf_file' do + expect(@elf.endian).to be :little + expect(@elf.elf_class).to be 64 + expect(@elf.build_id).to eq 'c352d488d3467ababc488ab28758e968d84f8db8' + expect(@elf.machine).to eq 'RISC-V' + expect(@elf.elf_type).to eq 'DYN' + end + + it 'sections' do + expect(@elf.sections.size).to be 28 + expect(@elf.section_by_name('.dynsym').symbols.size).to eq 8 + expect(@elf.section_by_name('.symtab').symbols.size).to eq 66 + expect(@elf.section_by_name('.symtab').symbol_by_name('puts@GLIBC_2.27')).to be_a ELFTools::Sections::Symbol + end + + it 'segments' do + expect(@elf.segments.size).to be 10 + expect(@elf.segment_by_type(:interp).interp_name).to eq '/lib/ld-linux-riscv64-lp64d.so.1' + expect(@elf.segment_by_type(:note).notes.size).to be 2 + expect(@elf.segment_by_type(:gnu_stack).executable?).to be false + expect(@elf.segment_by_type(:load).offset_in?(0x12345678)).to be false + expect(@elf.segment_by_type(:load).offset_to_vma(0)).to be 0 + end +end