Split Generic System V Application Binary Interface ELF File Header constants out into gabi.rs

This patch starts changing the type representations for our ELF types. It splits out the raw constants defined in
the Generic System V Application Binary Interface out into its own file (gabi.rs) as native type constants. The
type definitions in types.rs are now left to be higher level semantic representations of these fields which may
or may not directly map to the on-disk ELF data layout.

The intent here is to structure the code such that the opinionated rust representation of these parsed ELF structures
live separately from the official gabi definitions. This could set us up for a future where consumers of this library that
desire their own different rust representations of parsed ELF structures to consume the gabi definitions without
also having to much about with our representations here.
This commit is contained in:
Christopher Cole 2022-10-03 20:34:23 -07:00
parent 4965746e79
commit 4db2b9065e
4 changed files with 682 additions and 593 deletions

328
src/gabi.rs Normal file
View File

@ -0,0 +1,328 @@
/// This file contains constants defined in the ELF GABI
/// See http://www.sco.com/developers/gabi/latest/ch4.eheader.html#elfid
/// Note: At least in 2022, it seems like the above site is not being updated. Official communication
/// occurs on the Generic System V Application Binary Interface mailing list:
/// https://groups.google.com/g/generic-abi
/// EI_* define indexes into the ELF File Header's e_ident[] byte array.
/// We define them as usize in order to use them to easily index into [u8].
/// Location of first ELF magic number byte
pub const EI_MAG0: usize = 0;
/// Location of second ELF magic number byte
pub const EI_MAG1: usize = 1;
/// Location of third ELF magic number byte
pub const EI_MAG2: usize = 2;
/// Location of fourth ELF magic number byte
pub const EI_MAG3: usize = 3;
/// Location of ELF class field in ELF file header ident array
pub const EI_CLASS: usize = 4;
/// Location of data format field in ELF file header ident array
pub const EI_DATA: usize = 5;
/// Location of ELF version field in ELF file header ident array
pub const EI_VERSION: usize = 6;
/// Location of OS ABI field in ELF file header ident array
pub const EI_OSABI: usize = 7;
/// Location of ABI version field in ELF file header ident array
pub const EI_ABIVERSION: usize = 8;
/// Start of padding bytes
pub const EI_PAD: usize = 9;
/// Length of ELF file header platform-independent identification fields (e_ident[])
pub const EI_NIDENT: usize = 16;
/// ELF magic number byte 1
pub const ELFMAG0: u8 = 0x7f;
/// ELF magic number byte 2
pub const ELFMAG1: u8 = 0x45;
/// ELF magic number byte 3
pub const ELFMAG2: u8 = 0x4c;
/// ELF magic number byte 4
pub const ELFMAG3: u8 = 0x46;
/// ELFCLASS* define constants for e_ident[EI_CLASS]
/// Invalid ELF file class
pub const ELFCLASSNONE: u8 = 0;
/// 32-bit ELF file
pub const ELFCLASS32: u8 = 1;
/// 64-bit ELF file
pub const ELFCLASS64: u8 = 2;
/// ELFDATA* define constants for e_ident[EI_DATA]
/// Invalid ELF data format
pub const ELFDATANONE: u8 = 0;
/// 2's complement values, with the least significant byte occupying the lowest address.
pub const ELFDATA2LSB: u8 = 1;
/// 2's complement values, with the most significant byte occupying the lowest address.
pub const ELFDATA2MSB: u8 = 2;
/// ELFOSABI* define constants for e_ident[EI_OSABI]
/// No extensions or unspecified
pub const ELFOSABI_NONE: u8 = 0;
/// Alias of unspecified for UNIX System V ABI
pub const ELFOSABI_SYSV: u8 = 0;
/// Hewlett-Packard HP-UX
pub const ELFOSABI_HPUX: u8 = 1;
/// NetBSD
pub const ELFOSABI_NETBSD: u8 = 2;
/// GNU
pub const ELFOSABI_GNU: u8 = 3;
/// Linux historical - alias for ELFOSABI_GNU
pub const ELFOSABI_LINUX: u8 = 3;
/// Sun Solaris
pub const ELFOSABI_SOLARIS: u8 = 6;
/// AIX
pub const ELFOSABI_AIX: u8 = 7;
/// IRIX
pub const ELFOSABI_IRIX: u8 = 8;
/// FreeBSD
pub const ELFOSABI_FREEBSD: u8 = 9;
/// Compaq TRU64 UNIX
pub const ELFOSABI_TRU64: u8 = 10;
/// Novell Modesto
pub const ELFOSABI_MODESTO: u8 = 11;
/// Open BSD
pub const ELFOSABI_OPENBSD: u8 = 12;
/// Open VMS
pub const ELFOSABI_OPENVMS: u8 = 13;
/// Hewlett-Packard Non-Stop Kernel
pub const ELFOSABI_NSK: u8 = 14;
/// Amiga Research OS
pub const ELFOSABI_AROS: u8 = 15;
/// The FenixOS highly scalable multi-core OS
pub const ELFOSABI_FENIXOS: u8 = 16;
/// Nuxi CloudABI
pub const ELFOSABI_CLOUDABI: u8 = 17;
/// Stratus Technologies OpenVOS
pub const ELFOSABI_OPENVOS: u8 = 18;
/// 64-255 Architecture-specific value range
/// ET_* define constants for the ELF File Header's e_type field.
/// Represented as Elf32_Half in Elf32_Ehdr and Elf64_Half in Elf64_Ehdr which
/// are both are 2-byte unsigned integers with 2-byte alignment
/// No file type
pub const ET_NONE: u16 = 0;
/// Relocatable file
pub const ET_REL: u16 = 1;
/// Executable file
pub const ET_EXEC: u16 = 2;
/// Shared object file
pub const ET_DYN: u16 = 3;
/// Core file
pub const ET_CORE: u16 = 4;
/// Operating system-specific
pub const ET_LOOS: u16 = 0xfe00;
/// Operating system-specific
pub const ET_HIOS: u16 = 0xfeff;
/// Processor-specific
pub const ET_LOPROC: u16 = 0xff00;
/// Processor-specific
pub const ET_HIPROC: u16 = 0xffff;
/// EM_* define constants for the ELF File Header's e_machine field.
/// Represented as Elf32_Half in Elf32_Ehdr and Elf64_Half in Elf64_Ehdr which
/// are both 2-byte unsigned integers with 2-byte alignment
pub const EM_NONE: u16 = 0; // No machine
pub const EM_M32: u16 = 1; // AT&T WE 32100
pub const EM_SPARC: u16 = 2; // SPARC
pub const EM_386: u16 = 3; // Intel 80386
pub const EM_68K: u16 = 4; // Motorola 68000
pub const EM_88K: u16 = 5; // Motorola 88000
pub const EM_IAMCU: u16 = 6; // Intel MCU
pub const EM_860: u16 = 7; // Intel 80860
pub const EM_MIPS: u16 = 8; // MIPS I Architecture
pub const EM_S370: u16 = 9; // IBM System/370 Processor
pub const EM_MIPS_RS3_LE: u16 = 10; // MIPS RS3000 Little-endian
// 11-14 Reserved for future use
pub const EM_PARISC: u16 = 15; // Hewlett-Packard PA-RISC
// 16 Reserved for future use
pub const EM_VPP500: u16 = 17; // Fujitsu VPP500
pub const EM_SPARC32PLUS: u16 = 18; // Enhanced instruction set SPARC
pub const EM_960: u16 = 19; // Intel 80960
pub const EM_PPC: u16 = 20; // PowerPC
pub const EM_PPC64: u16 = 21; // 64-bit PowerPC
pub const EM_S390: u16 = 22; // IBM System/390 Processor
pub const EM_SPU: u16 = 23; // IBM SPU/SPC
// 24-35 Reserved for future use
pub const EM_V800: u16 = 36; // NEC V800
pub const EM_FR20: u16 = 37; // Fujitsu FR20
pub const EM_RH32: u16 = 38; // TRW RH-32
pub const EM_RCE: u16 = 39; // Motorola RCE
pub const EM_ARM: u16 = 40; // ARM 32-bit architecture (AARCH32)
pub const EM_ALPHA: u16 = 41; // Digital Alpha
pub const EM_SH: u16 = 42; // Hitachi SH
pub const EM_SPARCV9: u16 = 43; // SPARC Version 9
pub const EM_TRICORE: u16 = 44; // Siemens TriCore embedded processor
pub const EM_ARC: u16 = 45; // Argonaut RISC Core, Argonaut Technologies Inc.
pub const EM_H8_300: u16 = 46; // Hitachi H8/300
pub const EM_H8_300H: u16 = 47; // Hitachi H8/300H
pub const EM_H8S: u16 = 48; // Hitachi H8S
pub const EM_H8_500: u16 = 49; // Hitachi H8/500
pub const EM_IA_64: u16 = 50; // Intel IA-64 processor architecture
pub const EM_MIPS_X: u16 = 51; // Stanford MIPS-X
pub const EM_COLDFIRE: u16 = 52; // Motorola ColdFire
pub const EM_68HC12: u16 = 53; // Motorola M68HC12
pub const EM_MMA: u16 = 54; // Fujitsu MMA Multimedia Accelerator
pub const EM_PCP: u16 = 55; // Siemens PCP
pub const EM_NCPU: u16 = 56; // Sony nCPU embedded RISC processor
pub const EM_NDR1: u16 = 57; // Denso NDR1 microprocessor
pub const EM_STARCORE: u16 = 58; // Motorola Star*Core processor
pub const EM_ME16: u16 = 59; // Toyota ME16 processor
pub const EM_ST100: u16 = 60; // STMicroelectronics ST100 processor
pub const EM_TINYJ: u16 = 61; // Advanced Logic Corp. TinyJ embedded processor family
pub const EM_X86_64: u16 = 62; // AMD x86-64 architecture
pub const EM_PDSP: u16 = 63; // Sony DSP Processor
pub const EM_PDP10: u16 = 64; // Digital Equipment Corp. PDP-10
pub const EM_PDP11: u16 = 65; // Digital Equipment Corp. PDP-11
pub const EM_FX66: u16 = 66; // Siemens FX66 microcontroller
pub const EM_ST9PLUS: u16 = 67; // STMicroelectronics ST9+ 8/16 bit microcontroller
pub const EM_ST7: u16 = 68; // STMicroelectronics ST7 8-bit microcontroller
pub const EM_68HC16: u16 = 69; // Motorola MC68HC16 Microcontroller
pub const EM_68HC11: u16 = 70; // Motorola MC68HC11 Microcontroller
pub const EM_68HC08: u16 = 71; // Motorola MC68HC08 Microcontroller
pub const EM_68HC05: u16 = 72; // Motorola MC68HC05 Microcontroller
pub const EM_SVX: u16 = 73; // Silicon Graphics SVx
pub const EM_ST19: u16 = 74; // STMicroelectronics ST19 8-bit microcontroller
pub const EM_VAX: u16 = 75; // Digital VAX
pub const EM_CRIS: u16 = 76; // Axis Communications 32-bit embedded processor
pub const EM_JAVELIN: u16 = 77; // Infineon Technologies 32-bit embedded processor
pub const EM_FIREPATH: u16 = 78; // Element 14 64-bit DSP Processor
pub const EM_ZSP: u16 = 79; // LSI Logic 16-bit DSP Processor
pub const EM_MMIX: u16 = 80; // Donald Knuth's educational 64-bit processor
pub const EM_HUANY: u16 = 81; // Harvard University machine-independent object files
pub const EM_PRISM: u16 = 82; // SiTera Prism
pub const EM_AVR: u16 = 83; // Atmel AVR 8-bit microcontroller
pub const EM_FR30: u16 = 84; // Fujitsu FR30
pub const EM_D10V: u16 = 85; // Mitsubishi D10V
pub const EM_D30V: u16 = 86; // Mitsubishi D30V
pub const EM_V850: u16 = 87; // NEC v850
pub const EM_M32R: u16 = 88; // Mitsubishi M32R
pub const EM_MN10300: u16 = 89; // Matsushita MN10300
pub const EM_MN10200: u16 = 90; // Matsushita MN10200
pub const EM_PJ: u16 = 91; // picoJava
pub const EM_OPENRISC: u16 = 92; // OpenRISC 32-bit embedded processor
pub const EM_ARC_COMPACT: u16 = 93; // ARC International ARCompact processor (old spelling/synonym: EM_ARC_A5)
pub const EM_XTENSA: u16 = 94; // Tensilica Xtensa Architecture
pub const EM_VIDEOCORE: u16 = 95; // Alphamosaic VideoCore processor
pub const EM_TMM_GPP: u16 = 96; // Thompson Multimedia General Purpose Processor
pub const EM_NS32K: u16 = 97; // National Semiconductor 32000 series
pub const EM_TPC: u16 = 98; // Tenor Network TPC processor
pub const EM_SNP1K: u16 = 99; // Trebia SNP 1000 processor
pub const EM_ST200: u16 = 100; // STMicroelectronics (www.st.com) ST200 microcontroller
pub const EM_IP2K: u16 = 101; // Ubicom IP2xxx microcontroller family
pub const EM_MAX: u16 = 102; // MAX Processor
pub const EM_CR: u16 = 103; // National Semiconductor CompactRISC microprocessor
pub const EM_F2MC16: u16 = 104; // Fujitsu F2MC16
pub const EM_MSP430: u16 = 105; // Texas Instruments embedded microcontroller msp430
pub const EM_BLACKFIN: u16 = 106; // Analog Devices Blackfin (DSP) processor
pub const EM_SE_C33: u16 = 107; // S1C33 Family of Seiko Epson processors
pub const EM_SEP: u16 = 108; // Sharp embedded microprocessor
pub const EM_ARCA: u16 = 109; // Arca RISC Microprocessor
pub const EM_UNICORE: u16 = 110; // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University
pub const EM_EXCESS: u16 = 111; // eXcess: 16/32/64-bit configurable embedded CPU
pub const EM_DXP: u16 = 112; // Icera Semiconductor Inc. Deep Execution Processor
pub const EM_ALTERA_NIOS2: u16 = 113; // Altera Nios II soft-core processor
pub const EM_CRX: u16 = 114; // National Semiconductor CompactRISC CRX microprocessor
pub const EM_XGATE: u16 = 115; // Motorola XGATE embedded processor
pub const EM_C166: u16 = 116; // Infineon C16x/XC16x processor
pub const EM_M16C: u16 = 117; // Renesas M16C series microprocessors
pub const EM_DSPIC30F: u16 = 118; // Microchip Technology dsPIC30F Digital Signal Controller
pub const EM_CE: u16 = 119; // Freescale Communication Engine RISC core
pub const EM_M32C: u16 = 120; // Renesas M32C series microprocessors
// 121-130 Reserved for future use
pub const EM_TSK3000: u16 = 131; // Altium TSK3000 core
pub const EM_RS08: u16 = 132; // Freescale RS08 embedded processor
pub const EM_SHARC: u16 = 133; // Analog Devices SHARC family of 32-bit DSP processors
pub const EM_ECOG2: u16 = 134; // Cyan Technology eCOG2 microprocessor
pub const EM_SCORE7: u16 = 135; // Sunplus S+core7 RISC processor
pub const EM_DSP24: u16 = 136; // New Japan Radio (NJR) 24-bit DSP Processor
pub const EM_VIDEOCORE3: u16 = 137; // Broadcom VideoCore III processor
pub const EM_LATTICEMICO32: u16 = 138; // RISC processor for Lattice FPGA architecture
pub const EM_SE_C17: u16 = 139; // Seiko Epson C17 family
pub const EM_TI_C6000: u16 = 140; // The Texas Instruments TMS320C6000 DSP family
pub const EM_TI_C2000: u16 = 141; // The Texas Instruments TMS320C2000 DSP family
pub const EM_TI_C5500: u16 = 142; // The Texas Instruments TMS320C55x DSP family
pub const EM_TI_ARP32: u16 = 143; // Texas Instruments Application Specific RISC Processor, 32bit fetch
pub const EM_TI_PRU: u16 = 144; // Texas Instruments Programmable Realtime Unit
// 145-159 Reserved for future use
pub const EM_MMDSP_PLUS: u16 = 160; // STMicroelectronics 64bit VLIW Data Signal Processor
pub const EM_CYPRESS_M8C: u16 = 161; // Cypress M8C microprocessor
pub const EM_R32C: u16 = 162; // Renesas R32C series microprocessors
pub const EM_TRIMEDIA: u16 = 163; // NXP Semiconductors TriMedia architecture family
pub const EM_QDSP6: u16 = 164; // QUALCOMM DSP6 Processor
pub const EM_8051: u16 = 165; // Intel 8051 and variants
pub const EM_STXP7X: u16 = 166; // STMicroelectronics STxP7x family of configurable and extensible RISC processors
pub const EM_NDS32: u16 = 167; // Andes Technology compact code size embedded RISC processor family
pub const EM_ECOG1: u16 = 168; // Cyan Technology eCOG1X family
pub const EM_ECOG1X: u16 = 168; // Cyan Technology eCOG1X family
pub const EM_MAXQ30: u16 = 169; // Dallas Semiconductor MAXQ30 Core Micro-controllers
pub const EM_XIMO16: u16 = 170; // New Japan Radio (NJR) 16-bit DSP Processor
pub const EM_MANIK: u16 = 171; // M2000 Reconfigurable RISC Microprocessor
pub const EM_CRAYNV2: u16 = 172; // Cray Inc. NV2 vector architecture
pub const EM_RX: u16 = 173; // Renesas RX family
pub const EM_METAG: u16 = 174; // Imagination Technologies META processor architecture
pub const EM_MCST_ELBRUS: u16 = 175; // MCST Elbrus general purpose hardware architecture
pub const EM_ECOG16: u16 = 176; // Cyan Technology eCOG16 family
pub const EM_CR16: u16 = 177; // National Semiconductor CompactRISC CR16 16-bit microprocessor
pub const EM_ETPU: u16 = 178; // Freescale Extended Time Processing Unit
pub const EM_SLE9X: u16 = 179; // Infineon Technologies SLE9X core
pub const EM_L10M: u16 = 180; // Intel L10M
pub const EM_K10M: u16 = 181; // Intel K10M
// 182 Reserved for future Intel use
pub const EM_AARCH64: u16 = 183; // ARM 64-bit architecture (AARCH64)
// 184 Reserved for future ARM use
pub const EM_AVR32: u16 = 185; // Atmel Corporation 32-bit microprocessor family
pub const EM_STM8: u16 = 186; // STMicroeletronics STM8 8-bit microcontroller
pub const EM_TILE64: u16 = 187; // Tilera TILE64 multicore architecture family
pub const EM_TILEPRO: u16 = 188; // Tilera TILEPro multicore architecture family
pub const EM_MICROBLAZE: u16 = 189; // Xilinx MicroBlaze 32-bit RISC soft processor core
pub const EM_CUDA: u16 = 190; // NVIDIA CUDA architecture
pub const EM_TILEGX: u16 = 191; // Tilera TILE-Gx multicore architecture family
pub const EM_CLOUDSHIELD: u16 = 192; // CloudShield architecture family
pub const EM_COREA_1ST: u16 = 193; // KIPO-KAIST Core-A 1st generation processor family
pub const EM_COREA_2ND: u16 = 194; // KIPO-KAIST Core-A 2nd generation processor family
pub const EM_ARC_COMPACT2: u16 = 195; // Synopsys ARCompact V2
pub const EM_OPEN8: u16 = 196; // Open8 8-bit RISC soft processor core
pub const EM_RL78: u16 = 197; // Renesas RL78 family
pub const EM_VIDEOCORE5: u16 = 198; // Broadcom VideoCore V processor
pub const EM_78KOR: u16 = 199; // Renesas 78KOR family
pub const EM_56800EX: u16 = 200; // Freescale 56800EX Digital Signal Controller (DSC)
pub const EM_BA1: u16 = 201; // Beyond BA1 CPU architecture
pub const EM_BA2: u16 = 202; // Beyond BA2 CPU architecture
pub const EM_XCORE: u16 = 203; // XMOS xCORE processor family
pub const EM_MCHP_PIC: u16 = 204; // Microchip 8-bit PIC(r) family
pub const EM_INTEL205: u16 = 205; // Reserved by Intel
pub const EM_INTEL206: u16 = 206; // Reserved by Intel
pub const EM_INTEL207: u16 = 207; // Reserved by Intel
pub const EM_INTEL208: u16 = 208; // Reserved by Intel
pub const EM_INTEL209: u16 = 209; // Reserved by Intel
pub const EM_KM32: u16 = 210; // KM211 KM32 32-bit processor
pub const EM_KMX32: u16 = 211; // KM211 KMX32 32-bit processor
pub const EM_KMX16: u16 = 212; // KM211 KMX16 16-bit processor
pub const EM_KMX8: u16 = 213; // KM211 KMX8 8-bit processor
pub const EM_KVARC: u16 = 214; // KM211 KVARC processor
pub const EM_CDP: u16 = 215; // Paneve CDP architecture family
pub const EM_COGE: u16 = 216; // Cognitive Smart Memory Processor
pub const EM_COOL: u16 = 217; // Bluechip Systems CoolEngine
pub const EM_NORC: u16 = 218; // Nanoradio Optimized RISC
pub const EM_CSR_KALIMBA: u16 = 219; // CSR Kalimba architecture family
pub const EM_Z80: u16 = 220; // Zilog Z80
pub const EM_VISIUM: u16 = 221; // Controls and Data Services VISIUMcore processor
pub const EM_FT32: u16 = 222; // FTDI Chip FT32 high performance 32-bit RISC architecture
pub const EM_MOXIE: u16 = 223; // Moxie processor family
pub const EM_AMDGPU: u16 = 224; // AMD GPU architecture
pub const EM_RISCV: u16 = 243; // RISC-V
pub const EM_BPF: u16 = 247; // Linux BPF
/// EV_* define constants for the ELF File Header's e_version field.
/// Represented as Elf32_Word in Elf32_Ehdr and Elf64_Word in Elf64_Ehdr which
/// are both 4-byte unsigned integers with 4-byte alignment
/// Invalid version
pub const EV_NONE: u32 = 0;
/// Current version
pub const EV_CURRENT: u32 = 1;

View File

@ -3,6 +3,7 @@ use std::io;
use std::path::Path;
use std::io::{Read, Seek};
pub mod gabi;
pub mod types;
#[macro_use]
@ -66,50 +67,50 @@ impl File {
pub fn open_stream<T: Read + Seek>(io_file: &mut T) -> Result<File, ParseError> {
// Read the platform-independent ident bytes
let mut ident = [0u8; types::EI_NIDENT];
let mut ident = [0u8; gabi::EI_NIDENT];
let nread = io_file.read(ident.as_mut())?;
if nread != types::EI_NIDENT {
if nread != gabi::EI_NIDENT {
return Err(ParseError::InvalidFormat(None));
}
// Verify the magic number
if ident[0] != types::ELFMAG0 || ident[1] != types::ELFMAG1
|| ident[2] != types::ELFMAG2 || ident[3] != types::ELFMAG3 {
if ident[0] != gabi::ELFMAG0 || ident[1] != gabi::ELFMAG1
|| ident[2] != gabi::ELFMAG2 || ident[3] != gabi::ELFMAG3 {
return Err(ParseError::InvalidMagic);
}
// Fill in file header values from ident bytes
let mut elf_f = File::new();
elf_f.ehdr.class = types::Class(ident[types::EI_CLASS]);
elf_f.ehdr.data = types::Data(ident[types::EI_DATA]);
elf_f.ehdr.osabi = types::OSABI(ident[types::EI_OSABI]);
elf_f.ehdr.abiversion = ident[types::EI_ABIVERSION];
elf_f.ehdr.elftype = types::Type(utils::read_u16(elf_f.ehdr.data, io_file)?);
elf_f.ehdr.machine = types::Machine(utils::read_u16(elf_f.ehdr.data, io_file)?);
elf_f.ehdr.version = types::Version(utils::read_u32(elf_f.ehdr.data, io_file)?);
elf_f.ehdr.class = types::Class(ident[gabi::EI_CLASS]);
elf_f.ehdr.endianness = types::Endian(ident[gabi::EI_DATA]);
elf_f.ehdr.osabi = types::OSABI(ident[gabi::EI_OSABI]);
elf_f.ehdr.abiversion = ident[gabi::EI_ABIVERSION];
elf_f.ehdr.elftype = types::ObjectFileType(utils::read_u16(elf_f.ehdr.endianness, io_file)?);
elf_f.ehdr.arch = types::Architecture(utils::read_u16(elf_f.ehdr.endianness, io_file)?);
elf_f.ehdr.version = types::Version(utils::read_u32(elf_f.ehdr.endianness, io_file)?);
let phoff: u64;
let shoff: u64;
// Parse the platform-dependent file fields
if elf_f.ehdr.class == types::ELFCLASS32 {
elf_f.ehdr.entry = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
phoff = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
shoff = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
if elf_f.ehdr.class == gabi::ELFCLASS32 {
elf_f.ehdr.entry = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
phoff = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
shoff = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
} else {
elf_f.ehdr.entry = utils::read_u64(elf_f.ehdr.data, io_file)?;
phoff = utils::read_u64(elf_f.ehdr.data, io_file)?;
shoff = utils::read_u64(elf_f.ehdr.data, io_file)?;
elf_f.ehdr.entry = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
phoff = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
shoff = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
}
let _flags = utils::read_u32(elf_f.ehdr.data, io_file)?;
let _ehsize = utils::read_u16(elf_f.ehdr.data, io_file)?;
let _phentsize = utils::read_u16(elf_f.ehdr.data, io_file)?;
let phnum = utils::read_u16(elf_f.ehdr.data, io_file)?;
let _shentsize = utils::read_u16(elf_f.ehdr.data, io_file)?;
let shnum = utils::read_u16(elf_f.ehdr.data, io_file)?;
let shstrndx = utils::read_u16(elf_f.ehdr.data, io_file)?;
let _flags = utils::read_u32(elf_f.ehdr.endianness, io_file)?;
let _ehsize = utils::read_u16(elf_f.ehdr.endianness, io_file)?;
let _phentsize = utils::read_u16(elf_f.ehdr.endianness, io_file)?;
let phnum = utils::read_u16(elf_f.ehdr.endianness, io_file)?;
let _shentsize = utils::read_u16(elf_f.ehdr.endianness, io_file)?;
let shnum = utils::read_u16(elf_f.ehdr.endianness, io_file)?;
let shstrndx = utils::read_u16(elf_f.ehdr.endianness, io_file)?;
// Parse the program headers
io_file.seek(io::SeekFrom::Start(phoff))?;
@ -123,23 +124,23 @@ impl File {
let flags: types::ProgFlag;
let align: u64;
progtype = types::ProgType(utils::read_u32(elf_f.ehdr.data, io_file)?);
if elf_f.ehdr.class == types::ELFCLASS32 {
offset = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
vaddr = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
paddr = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
filesz = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
memsz = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
flags = types::ProgFlag(utils::read_u32(elf_f.ehdr.data, io_file)?);
align = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
progtype = types::ProgType(utils::read_u32(elf_f.ehdr.endianness, io_file)?);
if elf_f.ehdr.class == gabi::ELFCLASS32 {
offset = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
vaddr = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
paddr = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
filesz = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
memsz = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
flags = types::ProgFlag(utils::read_u32(elf_f.ehdr.endianness, io_file)?);
align = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
} else {
flags = types::ProgFlag(utils::read_u32(elf_f.ehdr.data, io_file)?);
offset = utils::read_u64(elf_f.ehdr.data, io_file)?;
vaddr = utils::read_u64(elf_f.ehdr.data, io_file)?;
paddr = utils::read_u64(elf_f.ehdr.data, io_file)?;
filesz = utils::read_u64(elf_f.ehdr.data, io_file)?;
memsz = utils::read_u64(elf_f.ehdr.data, io_file)?;
align = utils::read_u64(elf_f.ehdr.data, io_file)?;
flags = types::ProgFlag(utils::read_u32(elf_f.ehdr.endianness, io_file)?);
offset = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
vaddr = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
paddr = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
filesz = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
memsz = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
align = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
}
elf_f.phdrs.push(types::ProgramHeader {
@ -169,26 +170,26 @@ impl File {
let addralign: u64;
let entsize: u64;
name_idxs.push(utils::read_u32(elf_f.ehdr.data, io_file)?);
shtype = types::SectionType(utils::read_u32(elf_f.ehdr.data, io_file)?);
if elf_f.ehdr.class == types::ELFCLASS32 {
flags = types::SectionFlag(utils::read_u32(elf_f.ehdr.data, io_file)? as u64);
addr = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
offset = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
size = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
link = utils::read_u32(elf_f.ehdr.data, io_file)?;
info = utils::read_u32(elf_f.ehdr.data, io_file)?;
addralign = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
entsize = utils::read_u32(elf_f.ehdr.data, io_file)? as u64;
name_idxs.push(utils::read_u32(elf_f.ehdr.endianness, io_file)?);
shtype = types::SectionType(utils::read_u32(elf_f.ehdr.endianness, io_file)?);
if elf_f.ehdr.class == gabi::ELFCLASS32 {
flags = types::SectionFlag(utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64);
addr = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
offset = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
size = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
link = utils::read_u32(elf_f.ehdr.endianness, io_file)?;
info = utils::read_u32(elf_f.ehdr.endianness, io_file)?;
addralign = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
entsize = utils::read_u32(elf_f.ehdr.endianness, io_file)? as u64;
} else {
flags = types::SectionFlag(utils::read_u64(elf_f.ehdr.data, io_file)?);
addr = utils::read_u64(elf_f.ehdr.data, io_file)?;
offset = utils::read_u64(elf_f.ehdr.data, io_file)?;
size = utils::read_u64(elf_f.ehdr.data, io_file)?;
link = utils::read_u32(elf_f.ehdr.data, io_file)?;
info = utils::read_u32(elf_f.ehdr.data, io_file)?;
addralign = utils::read_u64(elf_f.ehdr.data, io_file)?;
entsize = utils::read_u64(elf_f.ehdr.data, io_file)?;
flags = types::SectionFlag(utils::read_u64(elf_f.ehdr.endianness, io_file)?);
addr = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
offset = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
size = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
link = utils::read_u32(elf_f.ehdr.endianness, io_file)?;
info = utils::read_u32(elf_f.ehdr.endianness, io_file)?;
addralign = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
entsize = utils::read_u64(elf_f.ehdr.endianness, io_file)?;
}
elf_f.sections.push(Section {
@ -260,20 +261,20 @@ impl File {
let mut info: [u8; 1] = [0u8];
let mut other: [u8; 1] = [0u8];
if self.ehdr.class == types::ELFCLASS32 {
name = utils::read_u32(self.ehdr.data, io_section)?;
value = utils::read_u32(self.ehdr.data, io_section)? as u64;
size = utils::read_u32(self.ehdr.data, io_section)? as u64;
if self.ehdr.class == gabi::ELFCLASS32 {
name = utils::read_u32(self.ehdr.endianness, io_section)?;
value = utils::read_u32(self.ehdr.endianness, io_section)? as u64;
size = utils::read_u32(self.ehdr.endianness, io_section)? as u64;
io_section.read_exact(&mut info)?;
io_section.read_exact(&mut other)?;
shndx = utils::read_u16(self.ehdr.data, io_section)?;
shndx = utils::read_u16(self.ehdr.endianness, io_section)?;
} else {
name = utils::read_u32(self.ehdr.data, io_section)?;
name = utils::read_u32(self.ehdr.endianness, io_section)?;
io_section.read_exact(&mut info)?;
io_section.read_exact(&mut other)?;
shndx = utils::read_u16(self.ehdr.data, io_section)?;
value = utils::read_u64(self.ehdr.data, io_section)?;
size = utils::read_u64(self.ehdr.data, io_section)?;
shndx = utils::read_u16(self.ehdr.endianness, io_section)?;
value = utils::read_u64(self.ehdr.endianness, io_section)?;
size = utils::read_u64(self.ehdr.endianness, io_section)?;
}
symbols.push(types::Symbol {

View File

@ -1,35 +1,17 @@
use std::fmt;
/// Length of ELF file header platform-independent identification fields
pub const EI_NIDENT: usize = 16;
/// ELF magic number byte 1
pub const ELFMAG0: u8 = 0x7f;
/// ELF magic number byte 2
pub const ELFMAG1: u8 = 0x45;
/// ELF magic number byte 3
pub const ELFMAG2: u8 = 0x4c;
/// ELF magic number byte 4
pub const ELFMAG3: u8 = 0x46;
/// Location of ELF class field in ELF file header ident array
pub const EI_CLASS: usize = 4;
/// Location of data format field in ELF file header ident array
pub const EI_DATA: usize = 5;
/// Location of ELF version field in ELF file header ident array
pub const EI_VERSION: usize = 6;
/// Location of OS ABI field in ELF file header ident array
pub const EI_OSABI: usize = 7;
/// Location of ABI version field in ELF file header ident array
pub const EI_ABIVERSION: usize = 8;
use gabi;
/// Represents the ELF file class (32-bit vs 64-bit)
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Class(pub u8);
/// Invalid ELF file class
pub const ELFCLASSNONE : Class = Class(0);
/// 32-bit ELF file
pub const ELFCLASS32 : Class = Class(1);
/// 64-bit ELF file
pub const ELFCLASS64 : Class = Class(2);
// Allows us to do things like (self.ehdr.class == gabi::ELFCLASS32)
impl PartialEq<u8> for Class {
fn eq(&self, other: &u8) -> bool {
self.0 == *other
}
}
impl fmt::Debug for Class {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -39,10 +21,10 @@ impl fmt::Debug for Class {
impl fmt::Display for Class {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
ELFCLASSNONE => "Invalid",
ELFCLASS32 => "32-bit",
ELFCLASS64 => "64-bit",
let str = match self.0 {
gabi::ELFCLASSNONE => "Invalid",
gabi::ELFCLASS32 => "32-bit",
gabi::ELFCLASS64 => "64-bit",
_ => "Unknown",
};
write!(f, "{}", str)
@ -51,26 +33,20 @@ impl fmt::Display for Class {
/// Represents the ELF file data format (little-endian vs big-endian)
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Data(pub u8);
/// Invalid ELF data format
pub const ELFDATANONE : Data = Data(0);
/// little-endian ELF file
pub const ELFDATA2LSB : Data = Data(1);
/// big-endian ELF file
pub const ELFDATA2MSB : Data = Data(2);
pub struct Endian(pub u8);
impl fmt::Debug for Data {
impl fmt::Debug for Endian {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}
impl fmt::Display for Data {
impl fmt::Display for Endian {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
ELFDATANONE => "Invalid",
ELFDATA2LSB => "2's complement, little endian",
ELFDATA2MSB => "2's complement, big endian",
let str = match self.0 {
gabi::ELFDATANONE => "Invalid",
gabi::ELFDATA2LSB => "2's complement, little endian",
gabi::ELFDATA2MSB => "2's complement, big endian",
_ => "Unknown",
};
write!(f, "{}", str)
@ -82,10 +58,6 @@ impl fmt::Display for Data {
/// This field represents the values both found in the e_ident byte array and the e_version field.
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Version(pub u32);
/// Invalid version
pub const EV_NONE : Version = Version(0);
/// Current version
pub const EV_CURRENT : Version = Version(1);
impl fmt::Debug for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -95,9 +67,9 @@ impl fmt::Debug for Version {
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
EV_NONE => "Invalid",
EV_CURRENT => "1 (Current)",
let str = match self.0 {
gabi::EV_NONE => "Invalid",
gabi::EV_CURRENT => "1 (Current)",
_ => "Unknown",
};
write!(f, "{}", str)
@ -107,30 +79,6 @@ impl fmt::Display for Version {
/// Represents the ELF file OS ABI
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct OSABI(pub u8);
/// Defaults to Unix System V
pub const ELFOSABI_NONE : OSABI = OSABI(0);
/// Unix System V
pub const ELFOSABI_SYSV : OSABI = OSABI(0);
/// HP-UX
pub const ELFOSABI_HPUX : OSABI = OSABI(1);
/// NetBSD
pub const ELFOSABI_NETBSD : OSABI = OSABI(2);
/// Linux with GNU extensions
pub const ELFOSABI_LINUX : OSABI = OSABI(3);
/// Solaris
pub const ELFOSABI_SOLARIS : OSABI = OSABI(6);
/// AIX
pub const ELFOSABI_AIX : OSABI = OSABI(7);
/// SGI Irix
pub const ELFOSABI_IRIX : OSABI = OSABI(8);
/// FreeBSD
pub const ELFOSABI_FREEBSD : OSABI = OSABI(9);
/// Compaq TRU64 UNIX
pub const ELFOSABI_TRU64 : OSABI = OSABI(10);
/// Novell Modesto
pub const ELFOSABI_MODESTO : OSABI = OSABI(11);
/// OpenBSD
pub const ELFOSABI_OPENBSD : OSABI = OSABI(12);
impl fmt::Debug for OSABI {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -138,20 +86,27 @@ impl fmt::Debug for OSABI {
}
}
impl fmt::Display for OSABI {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
ELFOSABI_SYSV => "UNIX System V",
ELFOSABI_HPUX => "HP-UX",
ELFOSABI_NETBSD => "NetBSD",
ELFOSABI_LINUX => "Linux with GNU extensions",
ELFOSABI_SOLARIS => "Solaris",
ELFOSABI_AIX => "AIX",
ELFOSABI_IRIX => "SGI Irix",
ELFOSABI_FREEBSD => "FreeBSD",
ELFOSABI_TRU64 => "Compaq TRU64 UNIX",
ELFOSABI_MODESTO => "Novell Modesto",
ELFOSABI_OPENBSD => "OpenBSD",
let str = match self.0 {
gabi::ELFOSABI_SYSV => "UNIX System V",
gabi::ELFOSABI_HPUX => "HP-UX",
gabi::ELFOSABI_NETBSD => "NetBSD",
gabi::ELFOSABI_LINUX => "Linux with GNU extensions",
gabi::ELFOSABI_SOLARIS => "Solaris",
gabi::ELFOSABI_AIX => "AIX",
gabi::ELFOSABI_IRIX => "SGI Irix",
gabi::ELFOSABI_FREEBSD => "FreeBSD",
gabi::ELFOSABI_TRU64 => "Compaq TRU64 UNIX",
gabi::ELFOSABI_MODESTO => "Novell Modesto",
gabi::ELFOSABI_OPENBSD => "OpenBSD",
gabi::ELFOSABI_OPENVMS => "Open VMS",
gabi::ELFOSABI_NSK => "Hewlett-Packard Non-Stop Kernel",
gabi::ELFOSABI_AROS => "Amiga Research OS",
gabi::ELFOSABI_FENIXOS => "The FenixOS highly scalable multi-core OS",
gabi::ELFOSABI_CLOUDABI => "Nuxi CloudABI",
gabi::ELFOSABI_OPENVOS => "Stratus Technologies OpenVOS",
_ => "Unknown",
};
write!(f, "{}", str)
@ -160,32 +115,22 @@ impl fmt::Display for OSABI {
/// Represents the ELF file type (object, executable, shared lib, core)
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Type(pub u16);
/// No file type
pub const ET_NONE : Type = Type(0);
/// Relocatable object file
pub const ET_REL : Type = Type(1);
/// Executable file
pub const ET_EXEC : Type = Type(2);
/// Shared library
pub const ET_DYN : Type = Type(3);
/// Core file
pub const ET_CORE : Type = Type(4);
pub struct ObjectFileType(pub u16);
impl fmt::Debug for Type {
impl fmt::Debug for ObjectFileType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}
impl fmt::Display for Type {
impl fmt::Display for ObjectFileType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
ET_NONE => "No file type",
ET_REL => "Relocatable file",
ET_EXEC => "Executable file",
ET_DYN => "Shared object file",
ET_CORE => "Core file",
let str = match self.0 {
gabi::ET_NONE => "No file type",
gabi::ET_REL => "Relocatable file",
gabi::ET_EXEC => "Executable file",
gabi::ET_DYN => "Shared object file",
gabi::ET_CORE => "Core file",
_ => "Unknown",
};
write!(f, "{}", str)
@ -194,391 +139,200 @@ impl fmt::Display for Type {
/// Represents the ELF file machine architecture
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Machine(pub u16);
pub const EM_NONE: Machine = Machine(0); // No machine
pub const EM_M32: Machine = Machine(1); // AT&T WE 32100
pub const EM_SPARC: Machine = Machine(2); // SPARC
pub const EM_386: Machine = Machine(3); // Intel 80386
pub const EM_68K: Machine = Machine(4); // Motorola 68000
pub const EM_88K: Machine = Machine(5); // Motorola 88000
pub const EM_IAMCU: Machine = Machine(6); // Intel MCU
pub const EM_860: Machine = Machine(7); // Intel 80860
pub const EM_MIPS: Machine = Machine(8); // MIPS I Architecture
pub const EM_S370: Machine = Machine(9); // IBM System/370 Processor
pub const EM_MIPS_RS3_LE: Machine = Machine(10); // MIPS RS3000 Little-endian
// 11-14 Reserved for future use
pub const EM_PARISC: Machine = Machine(15); // Hewlett-Packard PA-RISC
// 16 Reserved for future use
pub const EM_VPP500: Machine = Machine(17); // Fujitsu VPP500
pub const EM_SPARC32PLUS: Machine = Machine(18); // Enhanced instruction set SPARC
pub const EM_960: Machine = Machine(19); // Intel 80960
pub const EM_PPC: Machine = Machine(20); // PowerPC
pub const EM_PPC64: Machine = Machine(21); // 64-bit PowerPC
pub const EM_S390: Machine = Machine(22); // IBM System/390 Processor
pub const EM_SPU: Machine = Machine(23); // IBM SPU/SPC
// 24-35 Reserved for future use
pub const EM_V800: Machine = Machine(36); // NEC V800
pub const EM_FR20: Machine = Machine(37); // Fujitsu FR20
pub const EM_RH32: Machine = Machine(38); // TRW RH-32
pub const EM_RCE: Machine = Machine(39); // Motorola RCE
pub const EM_ARM: Machine = Machine(40); // ARM 32-bit architecture (AARCH32)
pub const EM_ALPHA: Machine = Machine(41); // Digital Alpha
pub const EM_SH: Machine = Machine(42); // Hitachi SH
pub const EM_SPARCV9: Machine = Machine(43); // SPARC Version 9
pub const EM_TRICORE: Machine = Machine(44); // Siemens TriCore embedded processor
pub const EM_ARC: Machine = Machine(45); // Argonaut RISC Core, Argonaut Technologies Inc.
pub const EM_H8_300: Machine = Machine(46); // Hitachi H8/300
pub const EM_H8_300H: Machine = Machine(47); // Hitachi H8/300H
pub const EM_H8S: Machine = Machine(48); // Hitachi H8S
pub const EM_H8_500: Machine = Machine(49); // Hitachi H8/500
pub const EM_IA_64: Machine = Machine(50); // Intel IA-64 processor architecture
pub const EM_MIPS_X: Machine = Machine(51); // Stanford MIPS-X
pub const EM_COLDFIRE: Machine = Machine(52); // Motorola ColdFire
pub const EM_68HC12: Machine = Machine(53); // Motorola M68HC12
pub const EM_MMA: Machine = Machine(54); // Fujitsu MMA Multimedia Accelerator
pub const EM_PCP: Machine = Machine(55); // Siemens PCP
pub const EM_NCPU: Machine = Machine(56); // Sony nCPU embedded RISC processor
pub const EM_NDR1: Machine = Machine(57); // Denso NDR1 microprocessor
pub const EM_STARCORE: Machine = Machine(58); // Motorola Star*Core processor
pub const EM_ME16: Machine = Machine(59); // Toyota ME16 processor
pub const EM_ST100: Machine = Machine(60); // STMicroelectronics ST100 processor
pub const EM_TINYJ: Machine = Machine(61); // Advanced Logic Corp. TinyJ embedded processor family
pub const EM_X86_64: Machine = Machine(62); // AMD x86-64 architecture
pub const EM_PDSP: Machine = Machine(63); // Sony DSP Processor
pub const EM_PDP10: Machine = Machine(64); // Digital Equipment Corp. PDP-10
pub const EM_PDP11: Machine = Machine(65); // Digital Equipment Corp. PDP-11
pub const EM_FX66: Machine = Machine(66); // Siemens FX66 microcontroller
pub const EM_ST9PLUS: Machine = Machine(67); // STMicroelectronics ST9+ 8/16 bit microcontroller
pub const EM_ST7: Machine = Machine(68); // STMicroelectronics ST7 8-bit microcontroller
pub const EM_68HC16: Machine = Machine(69); // Motorola MC68HC16 Microcontroller
pub const EM_68HC11: Machine = Machine(70); // Motorola MC68HC11 Microcontroller
pub const EM_68HC08: Machine = Machine(71); // Motorola MC68HC08 Microcontroller
pub const EM_68HC05: Machine = Machine(72); // Motorola MC68HC05 Microcontroller
pub const EM_SVX: Machine = Machine(73); // Silicon Graphics SVx
pub const EM_ST19: Machine = Machine(74); // STMicroelectronics ST19 8-bit microcontroller
pub const EM_VAX: Machine = Machine(75); // Digital VAX
pub const EM_CRIS: Machine = Machine(76); // Axis Communications 32-bit embedded processor
pub const EM_JAVELIN: Machine = Machine(77); // Infineon Technologies 32-bit embedded processor
pub const EM_FIREPATH: Machine = Machine(78); // Element 14 64-bit DSP Processor
pub const EM_ZSP: Machine = Machine(79); // LSI Logic 16-bit DSP Processor
pub const EM_MMIX: Machine = Machine(80); // Donald Knuth's educational 64-bit processor
pub const EM_HUANY: Machine = Machine(81); // Harvard University machine-independent object files
pub const EM_PRISM: Machine = Machine(82); // SiTera Prism
pub const EM_AVR: Machine = Machine(83); // Atmel AVR 8-bit microcontroller
pub const EM_FR30: Machine = Machine(84); // Fujitsu FR30
pub const EM_D10V: Machine = Machine(85); // Mitsubishi D10V
pub const EM_D30V: Machine = Machine(86); // Mitsubishi D30V
pub const EM_V850: Machine = Machine(87); // NEC v850
pub const EM_M32R: Machine = Machine(88); // Mitsubishi M32R
pub const EM_MN10300: Machine = Machine(89); // Matsushita MN10300
pub const EM_MN10200: Machine = Machine(90); // Matsushita MN10200
pub const EM_PJ: Machine = Machine(91); // picoJava
pub const EM_OPENRISC: Machine = Machine(92); // OpenRISC 32-bit embedded processor
pub const EM_ARC_COMPACT: Machine = Machine(93); // ARC International ARCompact processor (old spelling/synonym: EM_ARC_A5)
pub const EM_XTENSA: Machine = Machine(94); // Tensilica Xtensa Architecture
pub const EM_VIDEOCORE: Machine = Machine(95); // Alphamosaic VideoCore processor
pub const EM_TMM_GPP: Machine = Machine(96); // Thompson Multimedia General Purpose Processor
pub const EM_NS32K: Machine = Machine(97); // National Semiconductor 32000 series
pub const EM_TPC: Machine = Machine(98); // Tenor Network TPC processor
pub const EM_SNP1K: Machine = Machine(99); // Trebia SNP 1000 processor
pub const EM_ST200: Machine = Machine(100); // STMicroelectronics (www.st.com) ST200 microcontroller
pub const EM_IP2K: Machine = Machine(101); // Ubicom IP2xxx microcontroller family
pub const EM_MAX: Machine = Machine(102); // MAX Processor
pub const EM_CR: Machine = Machine(103); // National Semiconductor CompactRISC microprocessor
pub const EM_F2MC16: Machine = Machine(104); // Fujitsu F2MC16
pub const EM_MSP430: Machine = Machine(105); // Texas Instruments embedded microcontroller msp430
pub const EM_BLACKFIN: Machine = Machine(106); // Analog Devices Blackfin (DSP) processor
pub const EM_SE_C33: Machine = Machine(107); // S1C33 Family of Seiko Epson processors
pub const EM_SEP: Machine = Machine(108); // Sharp embedded microprocessor
pub const EM_ARCA: Machine = Machine(109); // Arca RISC Microprocessor
pub const EM_UNICORE: Machine = Machine(110); // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University
pub const EM_EXCESS: Machine = Machine(111); // eXcess: 16/32/64-bit configurable embedded CPU
pub const EM_DXP: Machine = Machine(112); // Icera Semiconductor Inc. Deep Execution Processor
pub const EM_ALTERA_NIOS2: Machine = Machine(113); // Altera Nios II soft-core processor
pub const EM_CRX: Machine = Machine(114); // National Semiconductor CompactRISC CRX microprocessor
pub const EM_XGATE: Machine = Machine(115); // Motorola XGATE embedded processor
pub const EM_C166: Machine = Machine(116); // Infineon C16x/XC16x processor
pub const EM_M16C: Machine = Machine(117); // Renesas M16C series microprocessors
pub const EM_DSPIC30F: Machine = Machine(118); // Microchip Technology dsPIC30F Digital Signal Controller
pub const EM_CE: Machine = Machine(119); // Freescale Communication Engine RISC core
pub const EM_M32C: Machine = Machine(120); // Renesas M32C series microprocessors
// 121-130 Reserved for future use
pub const EM_TSK3000: Machine = Machine(131); // Altium TSK3000 core
pub const EM_RS08: Machine = Machine(132); // Freescale RS08 embedded processor
pub const EM_SHARC: Machine = Machine(133); // Analog Devices SHARC family of 32-bit DSP processors
pub const EM_ECOG2: Machine = Machine(134); // Cyan Technology eCOG2 microprocessor
pub const EM_SCORE7: Machine = Machine(135); // Sunplus S+core7 RISC processor
pub const EM_DSP24: Machine = Machine(136); // New Japan Radio (NJR) 24-bit DSP Processor
pub const EM_VIDEOCORE3: Machine = Machine(137); // Broadcom VideoCore III processor
pub const EM_LATTICEMICO32: Machine = Machine(138); // RISC processor for Lattice FPGA architecture
pub const EM_SE_C17: Machine = Machine(139); // Seiko Epson C17 family
pub const EM_TI_C6000: Machine = Machine(140); // The Texas Instruments TMS320C6000 DSP family
pub const EM_TI_C2000: Machine = Machine(141); // The Texas Instruments TMS320C2000 DSP family
pub const EM_TI_C5500: Machine = Machine(142); // The Texas Instruments TMS320C55x DSP family
pub const EM_TI_ARP32: Machine = Machine(143); // Texas Instruments Application Specific RISC Processor, 32bit fetch
pub const EM_TI_PRU: Machine = Machine(144); // Texas Instruments Programmable Realtime Unit
// 145-159 Reserved for future use
pub const EM_MMDSP_PLUS: Machine = Machine(160); // STMicroelectronics 64bit VLIW Data Signal Processor
pub const EM_CYPRESS_M8C: Machine = Machine(161); // Cypress M8C microprocessor
pub const EM_R32C: Machine = Machine(162); // Renesas R32C series microprocessors
pub const EM_TRIMEDIA: Machine = Machine(163); // NXP Semiconductors TriMedia architecture family
pub const EM_QDSP6: Machine = Machine(164); // QUALCOMM DSP6 Processor
pub const EM_8051: Machine = Machine(165); // Intel 8051 and variants
pub const EM_STXP7X: Machine = Machine(166); // STMicroelectronics STxP7x family of configurable and extensible RISC processors
pub const EM_NDS32: Machine = Machine(167); // Andes Technology compact code size embedded RISC processor family
pub const EM_ECOG1: Machine = Machine(168); // Cyan Technology eCOG1X family
pub const EM_ECOG1X: Machine = Machine(168); // Cyan Technology eCOG1X family
pub const EM_MAXQ30: Machine = Machine(169); // Dallas Semiconductor MAXQ30 Core Micro-controllers
pub const EM_XIMO16: Machine = Machine(170); // New Japan Radio (NJR) 16-bit DSP Processor
pub const EM_MANIK: Machine = Machine(171); // M2000 Reconfigurable RISC Microprocessor
pub const EM_CRAYNV2: Machine = Machine(172); // Cray Inc. NV2 vector architecture
pub const EM_RX: Machine = Machine(173); // Renesas RX family
pub const EM_METAG: Machine = Machine(174); // Imagination Technologies META processor architecture
pub const EM_MCST_ELBRUS: Machine = Machine(175); // MCST Elbrus general purpose hardware architecture
pub const EM_ECOG16: Machine = Machine(176); // Cyan Technology eCOG16 family
pub const EM_CR16: Machine = Machine(177); // National Semiconductor CompactRISC CR16 16-bit microprocessor
pub const EM_ETPU: Machine = Machine(178); // Freescale Extended Time Processing Unit
pub const EM_SLE9X: Machine = Machine(179); // Infineon Technologies SLE9X core
pub const EM_L10M: Machine = Machine(180); // Intel L10M
pub const EM_K10M: Machine = Machine(181); // Intel K10M
// 182 Reserved for future Intel use
pub const EM_AARCH64: Machine = Machine(183); // ARM 64-bit architecture (AARCH64)
// 184 Reserved for future ARM use
pub const EM_AVR32: Machine = Machine(185); // Atmel Corporation 32-bit microprocessor family
pub const EM_STM8: Machine = Machine(186); // STMicroeletronics STM8 8-bit microcontroller
pub const EM_TILE64: Machine = Machine(187); // Tilera TILE64 multicore architecture family
pub const EM_TILEPRO: Machine = Machine(188); // Tilera TILEPro multicore architecture family
pub const EM_MICROBLAZE: Machine = Machine(189); // Xilinx MicroBlaze 32-bit RISC soft processor core
pub const EM_CUDA: Machine = Machine(190); // NVIDIA CUDA architecture
pub const EM_TILEGX: Machine = Machine(191); // Tilera TILE-Gx multicore architecture family
pub const EM_CLOUDSHIELD: Machine = Machine(192); // CloudShield architecture family
pub const EM_COREA_1ST: Machine = Machine(193); // KIPO-KAIST Core-A 1st generation processor family
pub const EM_COREA_2ND: Machine = Machine(194); // KIPO-KAIST Core-A 2nd generation processor family
pub const EM_ARC_COMPACT2: Machine = Machine(195); // Synopsys ARCompact V2
pub const EM_OPEN8: Machine = Machine(196); // Open8 8-bit RISC soft processor core
pub const EM_RL78: Machine = Machine(197); // Renesas RL78 family
pub const EM_VIDEOCORE5: Machine = Machine(198); // Broadcom VideoCore V processor
pub const EM_78KOR: Machine = Machine(199); // Renesas 78KOR family
pub const EM_56800EX: Machine = Machine(200); // Freescale 56800EX Digital Signal Controller (DSC)
pub const EM_BA1: Machine = Machine(201); // Beyond BA1 CPU architecture
pub const EM_BA2: Machine = Machine(202); // Beyond BA2 CPU architecture
pub const EM_XCORE: Machine = Machine(203); // XMOS xCORE processor family
pub const EM_MCHP_PIC: Machine = Machine(204); // Microchip 8-bit PIC(r) family
pub const EM_INTEL205: Machine = Machine(205); // Reserved by Intel
pub const EM_INTEL206: Machine = Machine(206); // Reserved by Intel
pub const EM_INTEL207: Machine = Machine(207); // Reserved by Intel
pub const EM_INTEL208: Machine = Machine(208); // Reserved by Intel
pub const EM_INTEL209: Machine = Machine(209); // Reserved by Intel
pub const EM_KM32: Machine = Machine(210); // KM211 KM32 32-bit processor
pub const EM_KMX32: Machine = Machine(211); // KM211 KMX32 32-bit processor
pub const EM_KMX16: Machine = Machine(212); // KM211 KMX16 16-bit processor
pub const EM_KMX8: Machine = Machine(213); // KM211 KMX8 8-bit processor
pub const EM_KVARC: Machine = Machine(214); // KM211 KVARC processor
pub const EM_CDP: Machine = Machine(215); // Paneve CDP architecture family
pub const EM_COGE: Machine = Machine(216); // Cognitive Smart Memory Processor
pub const EM_COOL: Machine = Machine(217); // Bluechip Systems CoolEngine
pub const EM_NORC: Machine = Machine(218); // Nanoradio Optimized RISC
pub const EM_CSR_KALIMBA: Machine = Machine(219); // CSR Kalimba architecture family
pub const EM_Z80: Machine = Machine(220); // Zilog Z80
pub const EM_VISIUM: Machine = Machine(221); // Controls and Data Services VISIUMcore processor
pub const EM_FT32: Machine = Machine(222); // FTDI Chip FT32 high performance 32-bit RISC architecture
pub const EM_MOXIE: Machine = Machine(223); // Moxie processor family
pub const EM_AMDGPU: Machine = Machine(224); // AMD GPU architecture
pub const EM_RISCV: Machine = Machine(243); // RISC-V
pub const EM_BPF: Machine = Machine(247); // Linux BPF
pub struct Architecture(pub u16);
impl fmt::Debug for Machine {
impl fmt::Debug for Architecture {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#x}", self.0)
}
}
impl fmt::Display for Machine {
impl fmt::Display for Architecture {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
EM_NONE => "No machine",
EM_M32 => "AT&T WE 32100",
EM_SPARC => "SPARC",
EM_386 => "Intel 80386",
EM_68K => "Motorola 68000",
EM_88K => "Motorola 88000",
EM_IAMCU => "Intel MCU",
EM_860 => "Intel 80860",
EM_MIPS => "MIPS I Architecture",
EM_S370 => "IBM System/370 Processor",
EM_MIPS_RS3_LE => "MIPS RS3000 Little-endian",
EM_PARISC => "Hewlett-Packard PA-RISC",
EM_VPP500 => "Fujitsu VPP500",
EM_SPARC32PLUS => "Enhanced instruction set SPARC",
EM_960 => "Intel 80960",
EM_PPC => "PowerPC",
EM_PPC64 => "64-bit PowerPC",
EM_S390 => "IBM System/390 Processor",
EM_SPU => "IBM SPU/SPC",
EM_V800 => "NEC V800",
EM_FR20 => "Fujitsu FR20",
EM_RH32 => "TRW RH-32",
EM_RCE => "Motorola RCE",
EM_ARM => "ARM 32-bit architecture (AARCH32)",
EM_ALPHA => "Digital Alpha",
EM_SH => "Hitachi SH",
EM_SPARCV9 => "SPARC Version 9",
EM_TRICORE => "Siemens TriCore embedded processor",
EM_ARC => "Argonaut RISC Core, Argonaut Technologies Inc.",
EM_H8_300 => "Hitachi H8/300",
EM_H8_300H => "Hitachi H8/300H",
EM_H8S => "Hitachi H8S",
EM_H8_500 => "Hitachi H8/500",
EM_IA_64 => "Intel IA-64 processor architecture",
EM_MIPS_X => "Stanford MIPS-X",
EM_COLDFIRE => "Motorola ColdFire",
EM_68HC12 => "Motorola M68HC12",
EM_MMA => "Fujitsu MMA Multimedia Accelerator",
EM_PCP => "Siemens PCP",
EM_NCPU => "Sony nCPU embedded RISC processor",
EM_NDR1 => "Denso NDR1 microprocessor",
EM_STARCORE => "Motorola Star*Core processor",
EM_ME16 => "Toyota ME16 processor",
EM_ST100 => "STMicroelectronics ST100 processor",
EM_TINYJ => "Advanced Logic Corp. TinyJ embedded processor family",
EM_X86_64 => "AMD x86-64 architecture",
EM_PDSP => "Sony DSP Processor",
EM_PDP10 => "Digital Equipment Corp. PDP-10",
EM_PDP11 => "Digital Equipment Corp. PDP-11",
EM_FX66 => "Siemens FX66 microcontroller",
EM_ST9PLUS => "STMicroelectronics ST9+ 8/16 bit microcontroller",
EM_ST7 => "STMicroelectronics ST7 8-bit microcontroller",
EM_68HC16 => "Motorola MC68HC16 Microcontroller",
EM_68HC11 => "Motorola MC68HC11 Microcontroller",
EM_68HC08 => "Motorola MC68HC08 Microcontroller",
EM_68HC05 => "Motorola MC68HC05 Microcontroller",
EM_SVX => "Silicon Graphics SVx",
EM_ST19 => "STMicroelectronics ST19 8-bit microcontroller",
EM_VAX => "Digital VAX",
EM_CRIS => "Axis Communications 32-bit embedded processor",
EM_JAVELIN => "Infineon Technologies 32-bit embedded processor",
EM_FIREPATH => "Element 14 64-bit DSP Processor",
EM_ZSP => "LSI Logic 16-bit DSP Processor",
EM_MMIX => "Donald Knuth's educational 64-bit processor",
EM_HUANY => "Harvard University machine-independent object files",
EM_PRISM => "SiTera Prism",
EM_AVR => "Atmel AVR 8-bit microcontroller",
EM_FR30 => "Fujitsu FR30",
EM_D10V => "Mitsubishi D10V",
EM_D30V => "Mitsubishi D30V",
EM_V850 => "NEC v850",
EM_M32R => "Mitsubishi M32R",
EM_MN10300 => "Matsushita MN10300",
EM_MN10200 => "Matsushita MN10200",
EM_PJ => "picoJava",
EM_OPENRISC => "OpenRISC 32-bit embedded processor",
EM_ARC_COMPACT => "ARC International ARCompact processor (old spelling/synonym: EM_ARC_A5)",
EM_XTENSA => "Tensilica Xtensa Architecture",
EM_VIDEOCORE => "Alphamosaic VideoCore processor",
EM_TMM_GPP => "Thompson Multimedia General Purpose Processor",
EM_NS32K => "National Semiconductor 32000 series",
EM_TPC => "Tenor Network TPC processor",
EM_SNP1K => "Trebia SNP 1000 processor",
EM_ST200 => "STMicroelectronics (www.st.com) ST200 microcontroller",
EM_IP2K => "Ubicom IP2xxx microcontroller family",
EM_MAX => "MAX Processor",
EM_CR => "National Semiconductor CompactRISC microprocessor",
EM_F2MC16 => "Fujitsu F2MC16",
EM_MSP430 => "Texas Instruments embedded microcontroller msp430",
EM_BLACKFIN => "Analog Devices Blackfin (DSP) processor",
EM_SE_C33 => "S1C33 Family of Seiko Epson processors",
EM_SEP => "Sharp embedded microprocessor",
EM_ARCA => "Arca RISC Microprocessor",
EM_UNICORE => "Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University",
EM_EXCESS => "eXcess: 16/32/64-bit configurable embedded CPU",
EM_DXP => "Icera Semiconductor Inc. Deep Execution Processor",
EM_ALTERA_NIOS2 => "Altera Nios II soft-core processor",
EM_CRX => "National Semiconductor CompactRISC CRX microprocessor",
EM_XGATE => "Motorola XGATE embedded processor",
EM_C166 => "Infineon C16x/XC16x processor",
EM_M16C => "Renesas M16C series microprocessors",
EM_DSPIC30F => "Microchip Technology dsPIC30F Digital Signal Controller",
EM_CE => "Freescale Communication Engine RISC core",
EM_M32C => "Renesas M32C series microprocessors",
EM_TSK3000 => "Altium TSK3000 core",
EM_RS08 => "Freescale RS08 embedded processor",
EM_SHARC => "Analog Devices SHARC family of 32-bit DSP processors",
EM_ECOG2 => "Cyan Technology eCOG2 microprocessor",
EM_SCORE7 => "Sunplus S+core7 RISC processor",
EM_DSP24 => "New Japan Radio (NJR) 24-bit DSP Processor",
EM_VIDEOCORE3 => "Broadcom VideoCore III processor",
EM_LATTICEMICO32 => "RISC processor for Lattice FPGA architecture",
EM_SE_C17 => "Seiko Epson C17 family",
EM_TI_C6000 => "The Texas Instruments TMS320C6000 DSP family",
EM_TI_C2000 => "The Texas Instruments TMS320C2000 DSP family",
EM_TI_C5500 => "The Texas Instruments TMS320C55x DSP family",
EM_TI_ARP32 => "Texas Instruments Application Specific RISC Processor, 32bit fetch",
EM_TI_PRU => "Texas Instruments Programmable Realtime Unit",
EM_MMDSP_PLUS => "STMicroelectronics 64bit VLIW Data Signal Processor",
EM_CYPRESS_M8C => "Cypress M8C microprocessor",
EM_R32C => "Renesas R32C series microprocessors",
EM_TRIMEDIA => "NXP Semiconductors TriMedia architecture family",
EM_QDSP6 => "QUALCOMM DSP6 Processor",
EM_8051 => "Intel 8051 and variants",
EM_STXP7X => "STMicroelectronics STxP7x family of configurable and extensible RISC processors",
EM_NDS32 => "Andes Technology compact code size embedded RISC processor family",
EM_ECOG1X => "Cyan Technology eCOG1X family",
EM_MAXQ30 => "Dallas Semiconductor MAXQ30 Core Micro-controllers",
EM_XIMO16 => "New Japan Radio (NJR) 16-bit DSP Processor",
EM_MANIK => "M2000 Reconfigurable RISC Microprocessor",
EM_CRAYNV2 => "Cray Inc. NV2 vector architecture",
EM_RX => "Renesas RX family",
EM_METAG => "Imagination Technologies META processor architecture",
EM_MCST_ELBRUS => "MCST Elbrus general purpose hardware architecture",
EM_ECOG16 => "Cyan Technology eCOG16 family",
EM_CR16 => "National Semiconductor CompactRISC CR16 16-bit microprocessor",
EM_ETPU => "Freescale Extended Time Processing Unit",
EM_SLE9X => "Infineon Technologies SLE9X core",
EM_L10M => "Intel L10M",
EM_K10M => "Intel K10M",
EM_AARCH64 => "ARM 64-bit architecture (AARCH64)",
EM_AVR32 => "Atmel Corporation 32-bit microprocessor family",
EM_STM8 => "STMicroeletronics STM8 8-bit microcontroller",
EM_TILE64 => "Tilera TILE64 multicore architecture family",
EM_TILEPRO => "Tilera TILEPro multicore architecture family",
EM_MICROBLAZE => "Xilinx MicroBlaze 32-bit RISC soft processor core",
EM_CUDA => "NVIDIA CUDA architecture",
EM_TILEGX => "Tilera TILE-Gx multicore architecture family",
EM_CLOUDSHIELD => "CloudShield architecture family",
EM_COREA_1ST => "KIPO-KAIST Core-A 1st generation processor family",
EM_COREA_2ND => "KIPO-KAIST Core-A 2nd generation processor family",
EM_ARC_COMPACT2 => "Synopsys ARCompact V2",
EM_OPEN8 => "Open8 8-bit RISC soft processor core",
EM_RL78 => "Renesas RL78 family",
EM_VIDEOCORE5 => "Broadcom VideoCore V processor",
EM_78KOR => "Renesas 78KOR family",
EM_56800EX => "Freescale 56800EX Digital Signal Controller (DSC)",
EM_BA1 => "Beyond BA1 CPU architecture",
EM_BA2 => "Beyond BA2 CPU architecture",
EM_XCORE => "XMOS xCORE processor family",
EM_MCHP_PIC => "Microchip 8-bit PIC(r) family",
EM_INTEL205 => "Reserved by Intel",
EM_INTEL206 => "Reserved by Intel",
EM_INTEL207 => "Reserved by Intel",
EM_INTEL208 => "Reserved by Intel",
EM_INTEL209 => "Reserved by Intel",
EM_KM32 => "KM211 KM32 32-bit processor",
EM_KMX32 => "KM211 KMX32 32-bit processor",
EM_KMX16 => "KM211 KMX16 16-bit processor",
EM_KMX8 => "KM211 KMX8 8-bit processor",
EM_KVARC => "KM211 KVARC processor",
EM_CDP => "Paneve CDP architecture family",
EM_COGE => "Cognitive Smart Memory Processor",
EM_COOL => "Bluechip Systems CoolEngine",
EM_NORC => "Nanoradio Optimized RISC",
EM_CSR_KALIMBA => "CSR Kalimba architecture family",
EM_Z80 => "Zilog Z80",
EM_VISIUM => "Controls and Data Services VISIUMcore processor",
EM_FT32 => "FTDI Chip FT32 high performance 32-bit RISC architecture",
EM_MOXIE => "Moxie processor family",
EM_AMDGPU => "AMD GPU architecture",
EM_RISCV => "RISC-V",
EM_BPF => "Linux BPF",
let str = match self.0 {
gabi::EM_NONE => "No machine",
gabi::EM_M32 => "AT&T WE 32100",
gabi::EM_SPARC => "SPARC",
gabi::EM_386 => "Intel 80386",
gabi::EM_68K => "Motorola 68000",
gabi::EM_88K => "Motorola 88000",
gabi::EM_IAMCU => "Intel MCU",
gabi::EM_860 => "Intel 80860",
gabi::EM_MIPS => "MIPS I Architecture",
gabi::EM_S370 => "IBM System/370 Processor",
gabi::EM_MIPS_RS3_LE => "MIPS RS3000 Little-endian",
gabi::EM_PARISC => "Hewlett-Packard PA-RISC",
gabi::EM_VPP500 => "Fujitsu VPP500",
gabi::EM_SPARC32PLUS => "Enhanced instruction set SPARC",
gabi::EM_960 => "Intel 80960",
gabi::EM_PPC => "PowerPC",
gabi::EM_PPC64 => "64-bit PowerPC",
gabi::EM_S390 => "IBM System/390 Processor",
gabi::EM_SPU => "IBM SPU/SPC",
gabi::EM_V800 => "NEC V800",
gabi::EM_FR20 => "Fujitsu FR20",
gabi::EM_RH32 => "TRW RH-32",
gabi::EM_RCE => "Motorola RCE",
gabi::EM_ARM => "ARM 32-bit architecture (AARCH32)",
gabi::EM_ALPHA => "Digital Alpha",
gabi::EM_SH => "Hitachi SH",
gabi::EM_SPARCV9 => "SPARC Version 9",
gabi::EM_TRICORE => "Siemens TriCore embedded processor",
gabi::EM_ARC => "Argonaut RISC Core, Argonaut Technologies Inc.",
gabi::EM_H8_300 => "Hitachi H8/300",
gabi::EM_H8_300H => "Hitachi H8/300H",
gabi::EM_H8S => "Hitachi H8S",
gabi::EM_H8_500 => "Hitachi H8/500",
gabi::EM_IA_64 => "Intel IA-64 processor architecture",
gabi::EM_MIPS_X => "Stanford MIPS-X",
gabi::EM_COLDFIRE => "Motorola ColdFire",
gabi::EM_68HC12 => "Motorola M68HC12",
gabi::EM_MMA => "Fujitsu MMA Multimedia Accelerator",
gabi::EM_PCP => "Siemens PCP",
gabi::EM_NCPU => "Sony nCPU embedded RISC processor",
gabi::EM_NDR1 => "Denso NDR1 microprocessor",
gabi::EM_STARCORE => "Motorola Star*Core processor",
gabi::EM_ME16 => "Toyota ME16 processor",
gabi::EM_ST100 => "STMicroelectronics ST100 processor",
gabi::EM_TINYJ => "Advanced Logic Corp. TinyJ embedded processor family",
gabi::EM_X86_64 => "AMD x86-64 architecture",
gabi::EM_PDSP => "Sony DSP Processor",
gabi::EM_PDP10 => "Digital Equipment Corp. PDP-10",
gabi::EM_PDP11 => "Digital Equipment Corp. PDP-11",
gabi::EM_FX66 => "Siemens FX66 microcontroller",
gabi::EM_ST9PLUS => "STMicroelectronics ST9+ 8/16 bit microcontroller",
gabi::EM_ST7 => "STMicroelectronics ST7 8-bit microcontroller",
gabi::EM_68HC16 => "Motorola MC68HC16 Microcontroller",
gabi::EM_68HC11 => "Motorola MC68HC11 Microcontroller",
gabi::EM_68HC08 => "Motorola MC68HC08 Microcontroller",
gabi::EM_68HC05 => "Motorola MC68HC05 Microcontroller",
gabi::EM_SVX => "Silicon Graphics SVx",
gabi::EM_ST19 => "STMicroelectronics ST19 8-bit microcontroller",
gabi::EM_VAX => "Digital VAX",
gabi::EM_CRIS => "Axis Communications 32-bit embedded processor",
gabi::EM_JAVELIN => "Infineon Technologies 32-bit embedded processor",
gabi::EM_FIREPATH => "Element 14 64-bit DSP Processor",
gabi::EM_ZSP => "LSI Logic 16-bit DSP Processor",
gabi::EM_MMIX => "Donald Knuth's educational 64-bit processor",
gabi::EM_HUANY => "Harvard University machine-independent object files",
gabi::EM_PRISM => "SiTera Prism",
gabi::EM_AVR => "Atmel AVR 8-bit microcontroller",
gabi::EM_FR30 => "Fujitsu FR30",
gabi::EM_D10V => "Mitsubishi D10V",
gabi::EM_D30V => "Mitsubishi D30V",
gabi::EM_V850 => "NEC v850",
gabi::EM_M32R => "Mitsubishi M32R",
gabi::EM_MN10300 => "Matsushita MN10300",
gabi::EM_MN10200 => "Matsushita MN10200",
gabi::EM_PJ => "picoJava",
gabi::EM_OPENRISC => "OpenRISC 32-bit embedded processor",
gabi::EM_ARC_COMPACT => "ARC International ARCompact processor (old spelling/synonym: EM_ARC_A5)",
gabi::EM_XTENSA => "Tensilica Xtensa Architecture",
gabi::EM_VIDEOCORE => "Alphamosaic VideoCore processor",
gabi::EM_TMM_GPP => "Thompson Multimedia General Purpose Processor",
gabi::EM_NS32K => "National Semiconductor 32000 series",
gabi::EM_TPC => "Tenor Network TPC processor",
gabi::EM_SNP1K => "Trebia SNP 1000 processor",
gabi::EM_ST200 => "STMicroelectronics (www.st.com) ST200 microcontroller",
gabi::EM_IP2K => "Ubicom IP2xxx microcontroller family",
gabi::EM_MAX => "MAX Processor",
gabi::EM_CR => "National Semiconductor CompactRISC microprocessor",
gabi::EM_F2MC16 => "Fujitsu F2MC16",
gabi::EM_MSP430 => "Texas Instruments embedded microcontroller msp430",
gabi::EM_BLACKFIN => "Analog Devices Blackfin (DSP) processor",
gabi::EM_SE_C33 => "S1C33 Family of Seiko Epson processors",
gabi::EM_SEP => "Sharp embedded microprocessor",
gabi::EM_ARCA => "Arca RISC Microprocessor",
gabi::EM_UNICORE => "Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University",
gabi::EM_EXCESS => "eXcess: 16/32/64-bit configurable embedded CPU",
gabi::EM_DXP => "Icera Semiconductor Inc. Deep Execution Processor",
gabi::EM_ALTERA_NIOS2 => "Altera Nios II soft-core processor",
gabi::EM_CRX => "National Semiconductor CompactRISC CRX microprocessor",
gabi::EM_XGATE => "Motorola XGATE embedded processor",
gabi::EM_C166 => "Infineon C16x/XC16x processor",
gabi::EM_M16C => "Renesas M16C series microprocessors",
gabi::EM_DSPIC30F => "Microchip Technology dsPIC30F Digital Signal Controller",
gabi::EM_CE => "Freescale Communication Engine RISC core",
gabi::EM_M32C => "Renesas M32C series microprocessors",
gabi::EM_TSK3000 => "Altium TSK3000 core",
gabi::EM_RS08 => "Freescale RS08 embedded processor",
gabi::EM_SHARC => "Analog Devices SHARC family of 32-bit DSP processors",
gabi::EM_ECOG2 => "Cyan Technology eCOG2 microprocessor",
gabi::EM_SCORE7 => "Sunplus S+core7 RISC processor",
gabi::EM_DSP24 => "New Japan Radio (NJR) 24-bit DSP Processor",
gabi::EM_VIDEOCORE3 => "Broadcom VideoCore III processor",
gabi::EM_LATTICEMICO32 => "RISC processor for Lattice FPGA architecture",
gabi::EM_SE_C17 => "Seiko Epson C17 family",
gabi::EM_TI_C6000 => "The Texas Instruments TMS320C6000 DSP family",
gabi::EM_TI_C2000 => "The Texas Instruments TMS320C2000 DSP family",
gabi::EM_TI_C5500 => "The Texas Instruments TMS320C55x DSP family",
gabi::EM_TI_ARP32 => "Texas Instruments Application Specific RISC Processor, 32bit fetch",
gabi::EM_TI_PRU => "Texas Instruments Programmable Realtime Unit",
gabi::EM_MMDSP_PLUS => "STMicroelectronics 64bit VLIW Data Signal Processor",
gabi::EM_CYPRESS_M8C => "Cypress M8C microprocessor",
gabi::EM_R32C => "Renesas R32C series microprocessors",
gabi::EM_TRIMEDIA => "NXP Semiconductors TriMedia architecture family",
gabi::EM_QDSP6 => "QUALCOMM DSP6 Processor",
gabi::EM_8051 => "Intel 8051 and variants",
gabi::EM_STXP7X => "STMicroelectronics STxP7x family of configurable and extensible RISC processors",
gabi::EM_NDS32 => "Andes Technology compact code size embedded RISC processor family",
gabi::EM_ECOG1X => "Cyan Technology eCOG1X family",
gabi::EM_MAXQ30 => "Dallas Semiconductor MAXQ30 Core Micro-controllers",
gabi::EM_XIMO16 => "New Japan Radio (NJR) 16-bit DSP Processor",
gabi::EM_MANIK => "M2000 Reconfigurable RISC Microprocessor",
gabi::EM_CRAYNV2 => "Cray Inc. NV2 vector architecture",
gabi::EM_RX => "Renesas RX family",
gabi::EM_METAG => "Imagination Technologies META processor architecture",
gabi::EM_MCST_ELBRUS => "MCST Elbrus general purpose hardware architecture",
gabi::EM_ECOG16 => "Cyan Technology eCOG16 family",
gabi::EM_CR16 => "National Semiconductor CompactRISC CR16 16-bit microprocessor",
gabi::EM_ETPU => "Freescale Extended Time Processing Unit",
gabi::EM_SLE9X => "Infineon Technologies SLE9X core",
gabi::EM_L10M => "Intel L10M",
gabi::EM_K10M => "Intel K10M",
gabi::EM_AARCH64 => "ARM 64-bit architecture (AARCH64)",
gabi::EM_AVR32 => "Atmel Corporation 32-bit microprocessor family",
gabi::EM_STM8 => "STMicroeletronics STM8 8-bit microcontroller",
gabi::EM_TILE64 => "Tilera TILE64 multicore architecture family",
gabi::EM_TILEPRO => "Tilera TILEPro multicore architecture family",
gabi::EM_MICROBLAZE => "Xilinx MicroBlaze 32-bit RISC soft processor core",
gabi::EM_CUDA => "NVIDIA CUDA architecture",
gabi::EM_TILEGX => "Tilera TILE-Gx multicore architecture family",
gabi::EM_CLOUDSHIELD => "CloudShield architecture family",
gabi::EM_COREA_1ST => "KIPO-KAIST Core-A 1st generation processor family",
gabi::EM_COREA_2ND => "KIPO-KAIST Core-A 2nd generation processor family",
gabi::EM_ARC_COMPACT2 => "Synopsys ARCompact V2",
gabi::EM_OPEN8 => "Open8 8-bit RISC soft processor core",
gabi::EM_RL78 => "Renesas RL78 family",
gabi::EM_VIDEOCORE5 => "Broadcom VideoCore V processor",
gabi::EM_78KOR => "Renesas 78KOR family",
gabi::EM_56800EX => "Freescale 56800EX Digital Signal Controller (DSC)",
gabi::EM_BA1 => "Beyond BA1 CPU architecture",
gabi::EM_BA2 => "Beyond BA2 CPU architecture",
gabi::EM_XCORE => "XMOS xCORE processor family",
gabi::EM_MCHP_PIC => "Microchip 8-bit PIC(r) family",
gabi::EM_INTEL205 => "Reserved by Intel",
gabi::EM_INTEL206 => "Reserved by Intel",
gabi::EM_INTEL207 => "Reserved by Intel",
gabi::EM_INTEL208 => "Reserved by Intel",
gabi::EM_INTEL209 => "Reserved by Intel",
gabi::EM_KM32 => "KM211 KM32 32-bit processor",
gabi::EM_KMX32 => "KM211 KMX32 32-bit processor",
gabi::EM_KMX16 => "KM211 KMX16 16-bit processor",
gabi::EM_KMX8 => "KM211 KMX8 8-bit processor",
gabi::EM_KVARC => "KM211 KVARC processor",
gabi::EM_CDP => "Paneve CDP architecture family",
gabi::EM_COGE => "Cognitive Smart Memory Processor",
gabi::EM_COOL => "Bluechip Systems CoolEngine",
gabi::EM_NORC => "Nanoradio Optimized RISC",
gabi::EM_CSR_KALIMBA => "CSR Kalimba architecture family",
gabi::EM_Z80 => "Zilog Z80",
gabi::EM_VISIUM => "Controls and Data Services VISIUMcore processor",
gabi::EM_FT32 => "FTDI Chip FT32 high performance 32-bit RISC architecture",
gabi::EM_MOXIE => "Moxie processor family",
gabi::EM_AMDGPU => "AMD GPU architecture",
gabi::EM_RISCV => "RISC-V",
gabi::EM_BPF => "Linux BPF",
_ => "Unknown Machine",
};
write!(f, "{}", str)
@ -594,35 +348,35 @@ impl fmt::Display for Machine {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FileHeader {
/// 32-bit vs 64-bit
pub class: Class,
pub class: Class,
/// little vs big endian
pub data: Data,
pub endianness: Endian,
/// elf version
pub version: Version,
pub version: Version,
/// OS ABI
pub osabi: OSABI,
pub osabi: OSABI,
/// Version of the OS ABI
pub abiversion: u8,
/// ELF file type
pub elftype: Type,
pub elftype: ObjectFileType,
/// Target machine architecture
pub machine: Machine,
pub arch: Architecture,
/// Virtual address of program entry point
pub entry: u64,
pub entry: u64,
}
impl FileHeader {
pub fn new() -> FileHeader {
FileHeader { class : ELFCLASSNONE, data : ELFDATANONE, version : EV_NONE,
elftype : ET_NONE, machine : EM_NONE, osabi : ELFOSABI_NONE,
FileHeader { class : Class(gabi::ELFCLASSNONE), endianness : Endian(gabi::ELFDATANONE), version : Version(gabi::EV_NONE),
elftype : ObjectFileType(gabi::ET_NONE), arch : Architecture(gabi::EM_NONE), osabi : OSABI(gabi::ELFOSABI_NONE),
abiversion : 0, entry : 0 }
}
}
impl fmt::Display for FileHeader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "File Header for {} {} Elf {} for {} {}", self.class, self.data,
self.elftype, self.osabi, self.machine)
write!(f, "File Header for {} {} Elf {} for {} {}", self.class, self.endianness,
self.elftype, self.osabi, self.arch)
}
}

View File

@ -1,39 +1,40 @@
use std::io;
use gabi;
use types;
use ParseError;
#[inline]
pub fn read_u16<T: io::Read>(endian: types::Data, io: &mut T) -> Result<u16, ParseError> {
pub fn read_u16<T: io::Read>(endian: types::Endian, io: &mut T) -> Result<u16, ParseError> {
let mut buf = [0u8; 2];
io.read_exact(&mut buf)?;
match endian {
types::ELFDATA2LSB => { Ok(u16::from_le_bytes(buf)) }
types::ELFDATA2MSB => { Ok(u16::from_be_bytes(buf)) }
types::ELFDATANONE => { return Err(ParseError::EndianError); }
match endian.0 {
gabi::ELFDATA2LSB => { Ok(u16::from_le_bytes(buf)) }
gabi::ELFDATA2MSB => { Ok(u16::from_be_bytes(buf)) }
gabi::ELFDATANONE => { return Err(ParseError::EndianError); }
_ => { return Err(ParseError::EndianError); }
}
}
#[inline]
pub fn read_u32<T: io::Read>(endian: types::Data, io: &mut T) -> Result<u32, ParseError> {
pub fn read_u32<T: io::Read>(endian: types::Endian, io: &mut T) -> Result<u32, ParseError> {
let mut buf = [0u8; 4];
io.read_exact(&mut buf)?;
match endian {
types::ELFDATA2LSB => { Ok(u32::from_le_bytes(buf)) }
types::ELFDATA2MSB => { Ok(u32::from_be_bytes(buf)) }
types::ELFDATANONE => { return Err(ParseError::EndianError); }
match endian.0 {
gabi::ELFDATA2LSB => { Ok(u32::from_le_bytes(buf)) }
gabi::ELFDATA2MSB => { Ok(u32::from_be_bytes(buf)) }
gabi::ELFDATANONE => { return Err(ParseError::EndianError); }
_ => { return Err(ParseError::EndianError); }
}
}
#[inline]
pub fn read_u64<T: io::Read>(endian: types::Data, io: &mut T) -> Result<u64, ParseError> {
pub fn read_u64<T: io::Read>(endian: types::Endian, io: &mut T) -> Result<u64, ParseError> {
let mut buf = [0u8; 8];
io.read_exact(&mut buf)?;
match endian {
types::ELFDATA2LSB => { Ok(u64::from_le_bytes(buf)) }
types::ELFDATA2MSB => { Ok(u64::from_be_bytes(buf)) }
types::ELFDATANONE => { return Err(ParseError::EndianError); }
match endian.0 {
gabi::ELFDATA2LSB => { Ok(u64::from_le_bytes(buf)) }
gabi::ELFDATA2MSB => { Ok(u64::from_be_bytes(buf)) }
gabi::ELFDATANONE => { return Err(ParseError::EndianError); }
_ => { return Err(ParseError::EndianError); }
}
}
@ -58,108 +59,113 @@ pub fn get_string(data: &[u8], start: usize) -> Result<String, std::string::From
mod tests {
use super::*;
const NONE: types::Endian = types::Endian(gabi::ELFDATANONE);
const LITTLE: types::Endian = types::Endian(gabi::ELFDATA2LSB);
const BIG: types::Endian = types::Endian(gabi::ELFDATA2MSB);
const INVALID: types::Endian = types::Endian(42);
#[test]
fn test_read_u16_lsb() {
let data = [0x10u8, 0x20u8];
let result = read_u16(types::ELFDATA2LSB, &mut data.as_ref()).unwrap();
let result = read_u16(LITTLE, &mut data.as_ref()).unwrap();
assert_eq!(result, 0x2010u16);
}
#[test]
fn test_read_u16_msb() {
let data = [0x10u8, 0x20u8];
let result = read_u16(types::ELFDATA2MSB, &mut data.as_ref()).unwrap();
let result = read_u16(BIG, &mut data.as_ref()).unwrap();
assert_eq!(result, 0x1020u16);
}
#[test]
fn test_read_u16_none() {
let data = [0x10u8, 0x20u8];
let result: Result<u16, ParseError> = read_u16(types::ELFDATANONE, &mut data.as_ref());
let result: Result<u16, ParseError> = read_u16(NONE, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u16_invalid_endianness() {
let data = [0x10u8, 0x20u8];
let result: Result<u16, ParseError> = read_u16(types::Data(42), &mut data.as_ref());
let result: Result<u16, ParseError> = read_u16(INVALID, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u16_too_short() {
let data = [0x10u8];
let result: Result<u16, ParseError> = read_u16(types::ELFDATA2LSB, &mut data.as_ref());
let result: Result<u16, ParseError> = read_u16(LITTLE, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u32_lsb() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8];
let result = read_u32(types::ELFDATA2LSB, &mut data.as_ref()).unwrap();
let result = read_u32(LITTLE, &mut data.as_ref()).unwrap();
assert_eq!(result, 0x40302010u32);
}
#[test]
fn test_read_u32_msb() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8];
let result = read_u32(types::ELFDATA2MSB, &mut data.as_ref()).unwrap();
let result = read_u32(BIG, &mut data.as_ref()).unwrap();
assert_eq!(result, 0x10203040u32);
}
#[test]
fn test_read_u32_none() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8];
let result: Result<u32, ParseError> = read_u32(types::ELFDATANONE, &mut data.as_ref());
let result = read_u32(NONE, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u32_invalid_endianness() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8];
let result: Result<u32, ParseError> = read_u32(types::Data(42), &mut data.as_ref());
let result: Result<u32, ParseError> = read_u32(INVALID, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u32_too_short() {
let data = [0x10u8, 0x20u8];
let result: Result<u32, ParseError> = read_u32(types::ELFDATA2LSB, &mut data.as_ref());
let result: Result<u32, ParseError> = read_u32(LITTLE, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u64_lsb() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8, 0x50u8, 0x60u8, 0x70u8, 0x80u8];
let result = read_u64(types::ELFDATA2LSB, &mut data.as_ref()).unwrap();
let result = read_u64(LITTLE, &mut data.as_ref()).unwrap();
assert_eq!(result, 0x8070605040302010u64);
}
#[test]
fn test_read_u64_msb() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8, 0x50u8, 0x60u8, 0x70u8, 0x80u8];
let result = read_u64(types::ELFDATA2MSB, &mut data.as_ref()).unwrap();
let result = read_u64(BIG, &mut data.as_ref()).unwrap();
assert_eq!(result, 0x1020304050607080u64);
}
#[test]
fn test_read_u64_none() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8, 0x50u8, 0x60u8, 0x70u8, 0x80u8];
let result: Result<u64, ParseError> = read_u64(types::ELFDATANONE, &mut data.as_ref());
let result: Result<u64, ParseError> = read_u64(NONE, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u64_invalid_endianness() {
let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8, 0x50u8, 0x60u8, 0x70u8, 0x80u8];
let result: Result<u64, ParseError> = read_u64(types::Data(42), &mut data.as_ref());
let result: Result<u64, ParseError> = read_u64(INVALID, &mut data.as_ref());
assert!(result.is_err());
}
#[test]
fn test_read_u64_too_short() {
let data = [0x10u8, 0x20u8];
let result: Result<u64, ParseError> = read_u64(types::ELFDATA2LSB, &mut data.as_ref());
let result: Result<u64, ParseError> = read_u64(LITTLE, &mut data.as_ref());
assert!(result.is_err());
}