Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of target-lexicon #218

Merged
merged 1 commit into from May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -16,7 +16,6 @@ all-features = true
crc32fast = { version = "1.2", optional = true }
flate2 = { version = "1", optional = true }
indexmap = { version = "1.1", optional = true }
target-lexicon = { version = "0.10" }
wasmparser = { version = "0.52", optional = true }

[dev-dependencies]
Expand Down
6 changes: 5 additions & 1 deletion examples/objcopy.rs
Expand Up @@ -39,7 +39,11 @@ fn main() {
}
};

let mut out_object = write::Object::new(in_object.format(), in_object.architecture());
let mut out_object = write::Object::new(
in_object.format(),
in_object.architecture(),
in_object.endianness(),
);
out_object.mangling = write::Mangling::None;
out_object.flags = in_object.flags();

Expand Down
60 changes: 60 additions & 0 deletions src/common.rs
@@ -1,3 +1,63 @@
/// A CPU architecture.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Architecture {
Unknown,
Aarch64,
Arm,
I386,
Mips,
Wasm32,
X86_64,
}

impl Architecture {
/// The size of an address value for this architecture.
///
/// Returns `None` for unknown architectures.
pub fn address_size(self) -> Option<AddressSize> {
match self {
Architecture::Unknown => None,
Architecture::Aarch64 => Some(AddressSize::U64),
Architecture::Arm => Some(AddressSize::U32),
Architecture::I386 => Some(AddressSize::U32),
Architecture::Mips => Some(AddressSize::U32),
Architecture::Wasm32 => Some(AddressSize::U32),
Architecture::X86_64 => Some(AddressSize::U64),
}
}
}

/// The size of an address value for an architecture.
///
/// This may differ from the address size supported by the file format (such as for COFF).
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum AddressSize {
U32 = 4,
U64 = 8,
}

impl AddressSize {
/// The size in bytes of an address value.
#[inline]
pub fn bytes(self) -> u8 {
self as u8
}
}

/// A binary file format.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BinaryFormat {
Coff,
Elf,
MachO,
Pe,
Wasm,
}

/// The kind of a section.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SectionKind {
Expand Down
20 changes: 10 additions & 10 deletions src/endian.rs
Expand Up @@ -215,40 +215,40 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static {

/// An endianness that is selectable at run-time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RunTimeEndian {
pub enum Endianness {
/// Little endian byte order.
Little,
/// Big endian byte order.
Big,
}

impl Default for RunTimeEndian {
impl Default for Endianness {
#[cfg(target_endian = "little")]
#[inline]
fn default() -> RunTimeEndian {
RunTimeEndian::Little
fn default() -> Endianness {
Endianness::Little
}

#[cfg(target_endian = "big")]
#[inline]
fn default() -> RunTimeEndian {
RunTimeEndian::Big
fn default() -> Endianness {
Endianness::Big
}
}

impl Endian for RunTimeEndian {
impl Endian for Endianness {
#[inline]
fn from_big_endian(big_endian: bool) -> Option<Self> {
Some(if big_endian {
RunTimeEndian::Big
Endianness::Big
} else {
RunTimeEndian::Little
Endianness::Little
})
}

#[inline]
fn is_big_endian(self) -> bool {
self != RunTimeEndian::Little
self != Endianness::Little
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Expand Up @@ -19,9 +19,6 @@ extern crate alloc;
#[macro_use]
extern crate std;

// Re-export since these are used in public signatures.
pub use target_lexicon;

mod common;
pub use common::*;

Expand Down
9 changes: 4 additions & 5 deletions src/read/any.rs
@@ -1,7 +1,6 @@
#[cfg(feature = "compression")]
use alloc::borrow::Cow;
use alloc::fmt;
use target_lexicon::{Architecture, BinaryFormat};

#[cfg(feature = "coff")]
use crate::read::coff;
Expand All @@ -14,8 +13,8 @@ use crate::read::pe;
#[cfg(feature = "wasm")]
use crate::read::wasm;
use crate::read::{
self, Error, FileFlags, Object, ObjectSection, ObjectSegment, Relocation, Result, SectionFlags,
SectionIndex, SectionKind, Symbol, SymbolIndex, SymbolMap,
self, Architecture, BinaryFormat, Error, FileFlags, Object, ObjectSection, ObjectSegment,
Relocation, Result, SectionFlags, SectionIndex, SectionKind, Symbol, SymbolIndex, SymbolMap,
};

/// Evaluate an expression on the contents of a file format enum.
Expand Down Expand Up @@ -226,9 +225,9 @@ impl<'data> File<'data> {
#[cfg(feature = "elf")]
FileInternal::Elf32(_) | FileInternal::Elf64(_) => BinaryFormat::Elf,
#[cfg(feature = "macho")]
FileInternal::MachO32(_) | FileInternal::MachO64(_) => BinaryFormat::Macho,
FileInternal::MachO32(_) | FileInternal::MachO64(_) => BinaryFormat::MachO,
#[cfg(feature = "pe")]
FileInternal::Pe32(_) | FileInternal::Pe64(_) => BinaryFormat::Coff,
FileInternal::Pe32(_) | FileInternal::Pe64(_) => BinaryFormat::Pe,
#[cfg(feature = "wasm")]
FileInternal::Wasm(_) => BinaryFormat::Wasm,
}
Expand Down
10 changes: 4 additions & 6 deletions src/read/coff/file.rs
@@ -1,14 +1,11 @@
use alloc::vec::Vec;
use core::str;
use target_lexicon::Architecture;

use crate::endian::LittleEndian as LE;
use crate::pe;
use crate::pod::Bytes;
use crate::read::{
self, FileFlags, Object, ObjectSection, ReadError, Result, SectionIndex, Symbol, SymbolIndex,
SymbolMap,
self, Architecture, FileFlags, Object, ObjectSection, ReadError, Result, SectionIndex, Symbol,
SymbolIndex, SymbolMap,
};
use crate::{pe, Bytes, LittleEndian as LE};

use super::{
parse_symbol, CoffSection, CoffSectionIterator, CoffSegment, CoffSegmentIterator,
Expand Down Expand Up @@ -69,6 +66,7 @@ where

#[inline]
fn is_64(&self) -> bool {
// Windows COFF is always 32-bit, even for 64-bit architectures. This could be confusing.
false
}

Expand Down
18 changes: 7 additions & 11 deletions src/read/elf/file.rs
Expand Up @@ -2,15 +2,11 @@ use alloc::vec::Vec;
use core::fmt::Debug;
use core::{mem, str};

use target_lexicon::{Aarch64Architecture, Architecture, ArmArchitecture};

use crate::elf;
use crate::endian::{self, Endian, RunTimeEndian, U32};
use crate::pod::{Bytes, Pod};
use crate::read::util::{self, StringTable};
use crate::read::{
self, Error, FileFlags, Object, ReadError, SectionIndex, Symbol, SymbolIndex, SymbolMap,
self, util, Architecture, Error, FileFlags, Object, ReadError, SectionIndex, StringTable,
Symbol, SymbolIndex, SymbolMap,
};
use crate::{elf, endian, Bytes, Endian, Endianness, Pod, U32};

use super::{
parse_symbol, CompressionHeader, ElfSection, ElfSectionIterator, ElfSegment,
Expand All @@ -19,9 +15,9 @@ use super::{
};

/// A 32-bit ELF object file.
pub type ElfFile32<'data, Endian = RunTimeEndian> = ElfFile<'data, elf::FileHeader32<Endian>>;
pub type ElfFile32<'data, Endian = Endianness> = ElfFile<'data, elf::FileHeader32<Endian>>;
/// A 64-bit ELF object file.
pub type ElfFile64<'data, Endian = RunTimeEndian> = ElfFile<'data, elf::FileHeader64<Endian>>;
pub type ElfFile64<'data, Endian = Endianness> = ElfFile<'data, elf::FileHeader64<Endian>>;

/// A partially parsed ELF file.
///
Expand Down Expand Up @@ -112,8 +108,8 @@ where

fn architecture(&self) -> Architecture {
match self.header.e_machine(self.endian) {
elf::EM_ARM => Architecture::Arm(ArmArchitecture::Arm),
elf::EM_AARCH64 => Architecture::Aarch64(Aarch64Architecture::Aarch64),
elf::EM_ARM => Architecture::Arm,
elf::EM_AARCH64 => Architecture::Aarch64,
elf::EM_386 => Architecture::I386,
elf::EM_X86_64 => Architecture::X86_64,
elf::EM_MIPS => Architecture::Mips,
Expand Down
6 changes: 3 additions & 3 deletions src/read/elf/note.rs
@@ -1,7 +1,7 @@
use core::fmt::Debug;

use crate::elf;
use crate::endian::{self, RunTimeEndian};
use crate::endian::{self, Endianness};
use crate::pod::{Bytes, Pod};
use crate::read::util;
use crate::read::{self, Error, ReadError};
Expand Down Expand Up @@ -80,9 +80,9 @@ where
}

/// A parsed `NoteHeader32`.
pub type ElfNote32<'data, Endian = RunTimeEndian> = ElfNote<'data, elf::FileHeader32<Endian>>;
pub type ElfNote32<'data, Endian = Endianness> = ElfNote<'data, elf::FileHeader32<Endian>>;
/// A parsed `NoteHeader64`.
pub type ElfNote64<'data, Endian = RunTimeEndian> = ElfNote<'data, elf::FileHeader64<Endian>>;
pub type ElfNote64<'data, Endian = Endianness> = ElfNote<'data, elf::FileHeader64<Endian>>;

/// A parsed `NoteHeader`.
#[derive(Debug)]
Expand Down
6 changes: 3 additions & 3 deletions src/read/elf/relocation.rs
Expand Up @@ -4,7 +4,7 @@ use core::fmt::Debug;
use core::slice;

use crate::elf;
use crate::endian::{self, RunTimeEndian};
use crate::endian::{self, Endianness};
use crate::pod::Pod;
use crate::read::{
self, Error, Relocation, RelocationEncoding, RelocationKind, RelocationTarget, SymbolIndex,
Expand Down Expand Up @@ -92,10 +92,10 @@ impl<'data, Elf: FileHeader> Iterator for ElfRelaIterator<'data, Elf> {
}

/// An iterator over the relocations for an `ElfSection32`.
pub type ElfRelocationIterator32<'data, 'file, Endian = RunTimeEndian> =
pub type ElfRelocationIterator32<'data, 'file, Endian = Endianness> =
ElfRelocationIterator<'data, 'file, elf::FileHeader32<Endian>>;
/// An iterator over the relocations for an `ElfSection64`.
pub type ElfRelocationIterator64<'data, 'file, Endian = RunTimeEndian> =
pub type ElfRelocationIterator64<'data, 'file, Endian = Endianness> =
ElfRelocationIterator<'data, 'file, elf::FileHeader64<Endian>>;

/// An iterator over the relocations for an `ElfSection`.
Expand Down
10 changes: 5 additions & 5 deletions src/read/elf/section.rs
Expand Up @@ -4,7 +4,7 @@ use core::fmt::Debug;
use core::{iter, mem, slice, str};

use crate::elf;
use crate::endian::{self, RunTimeEndian};
use crate::endian::{self, Endianness};
use crate::pod::{Bytes, Pod};
use crate::read::{
self, ObjectSection, ReadError, SectionFlags, SectionIndex, SectionKind, StringTable,
Expand Down Expand Up @@ -105,10 +105,10 @@ impl<'data, Elf: FileHeader> SectionTable<'data, Elf> {
}

/// An iterator over the sections of an `ElfFile32`.
pub type ElfSectionIterator32<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSectionIterator32<'data, 'file, Endian = Endianness> =
ElfSectionIterator<'data, 'file, elf::FileHeader32<Endian>>;
/// An iterator over the sections of an `ElfFile64`.
pub type ElfSectionIterator64<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSectionIterator64<'data, 'file, Endian = Endianness> =
ElfSectionIterator<'data, 'file, elf::FileHeader64<Endian>>;

/// An iterator over the sections of an `ElfFile`.
Expand All @@ -135,10 +135,10 @@ impl<'data, 'file, Elf: FileHeader> Iterator for ElfSectionIterator<'data, 'file
}

/// A section of an `ElfFile32`.
pub type ElfSection32<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSection32<'data, 'file, Endian = Endianness> =
ElfSection<'data, 'file, elf::FileHeader32<Endian>>;
/// A section of an `ElfFile64`.
pub type ElfSection64<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSection64<'data, 'file, Endian = Endianness> =
ElfSection<'data, 'file, elf::FileHeader64<Endian>>;

/// A section of an `ElfFile`.
Expand Down
10 changes: 5 additions & 5 deletions src/read/elf/segment.rs
Expand Up @@ -2,17 +2,17 @@ use core::fmt::Debug;
use core::{slice, str};

use crate::elf;
use crate::endian::{self, RunTimeEndian};
use crate::endian::{self, Endianness};
use crate::pod::{Bytes, Pod};
use crate::read::{self, ObjectSegment, ReadError};

use super::{ElfFile, ElfNoteIterator, FileHeader};

/// An iterator over the segments of an `ElfFile32`.
pub type ElfSegmentIterator32<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSegmentIterator32<'data, 'file, Endian = Endianness> =
ElfSegmentIterator<'data, 'file, elf::FileHeader32<Endian>>;
/// An iterator over the segments of an `ElfFile64`.
pub type ElfSegmentIterator64<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSegmentIterator64<'data, 'file, Endian = Endianness> =
ElfSegmentIterator<'data, 'file, elf::FileHeader64<Endian>>;

/// An iterator over the segments of an `ElfFile`.
Expand Down Expand Up @@ -43,10 +43,10 @@ impl<'data, 'file, Elf: FileHeader> Iterator for ElfSegmentIterator<'data, 'file
}

/// A segment of an `ElfFile32`.
pub type ElfSegment32<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSegment32<'data, 'file, Endian = Endianness> =
ElfSegment<'data, 'file, elf::FileHeader32<Endian>>;
/// A segment of an `ElfFile64`.
pub type ElfSegment64<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSegment64<'data, 'file, Endian = Endianness> =
ElfSegment<'data, 'file, elf::FileHeader64<Endian>>;

/// A segment of an `ElfFile`.
Expand Down
6 changes: 3 additions & 3 deletions src/read/elf/symbol.rs
Expand Up @@ -4,7 +4,7 @@ use core::slice;
use core::str;

use crate::elf;
use crate::endian::{self, RunTimeEndian};
use crate::endian::{self, Endianness};
use crate::pod::{Bytes, Pod};
use crate::read::util::StringTable;
use crate::read::{
Expand Down Expand Up @@ -126,10 +126,10 @@ impl<'data, Elf: FileHeader> SymbolTable<'data, Elf> {
}

/// An iterator over the symbols of an `ElfFile32`.
pub type ElfSymbolIterator32<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSymbolIterator32<'data, 'file, Endian = Endianness> =
ElfSymbolIterator<'data, 'file, elf::FileHeader32<Endian>>;
/// An iterator over the symbols of an `ElfFile64`.
pub type ElfSymbolIterator64<'data, 'file, Endian = RunTimeEndian> =
pub type ElfSymbolIterator64<'data, 'file, Endian = Endianness> =
ElfSymbolIterator<'data, 'file, elf::FileHeader64<Endian>>;

/// An iterator over the symbols of an `ElfFile`.
Expand Down