Skip to content

Commit

Permalink
Avoid using reexports within the crate (#621)
Browse files Browse the repository at this point in the history
The exception is reusing common definitions that are reexported
in the read module (preexisting) and write module (newly added).

This is for consistency, and makes it easier to experiment with a
restructure.
  • Loading branch information
philipc authored Jan 7, 2024
1 parent 6da606b commit 28f2bc6
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 67 deletions.
11 changes: 5 additions & 6 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::fmt;
use alloc::vec::Vec;
use core::marker::PhantomData;

use crate::endian::{Endian, Endianness};
#[cfg(feature = "coff")]
use crate::read::coff;
#[cfg(feature = "elf")]
Expand All @@ -18,11 +19,9 @@ use crate::read::{
self, Architecture, BinaryFormat, CodeView, ComdatKind, CompressedData, CompressedFileRange,
Error, Export, FileFlags, FileKind, Import, Object, ObjectComdat, ObjectKind, ObjectMap,
ObjectSection, ObjectSegment, ObjectSymbol, ObjectSymbolTable, ReadRef, Relocation, Result,
SectionFlags, SectionIndex, SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind,
SymbolMap, SymbolMapName, SymbolScope, SymbolSection,
SectionFlags, SectionIndex, SectionKind, SegmentFlags, SubArchitecture, SymbolFlags,
SymbolIndex, SymbolKind, SymbolMap, SymbolMapName, SymbolScope, SymbolSection,
};
#[allow(unused_imports)]
use crate::{AddressSize, Endian, Endianness, SubArchitecture};

/// Evaluate an expression on the contents of a file format enum.
///
Expand Down Expand Up @@ -272,10 +271,10 @@ impl<'data, R: ReadRef<'data>> File<'data, R> {
image: &macho::DyldCacheImage<'data, 'cache, E, R>,
) -> Result<Self> {
Ok(match image.cache.architecture().address_size() {
Some(AddressSize::U64) => {
Some(read::AddressSize::U64) => {
File::MachO64(macho::MachOFile64::parse_dyld_cache_image(image)?)
}
Some(AddressSize::U32) => {
Some(read::AddressSize::U32) => {
File::MachO32(macho::MachOFile32::parse_dyld_cache_image(image)?)
}
_ => return Err(Error("Unsupported file format")),
Expand Down
4 changes: 3 additions & 1 deletion src/read/coff/file.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use alloc::vec::Vec;
use core::fmt::Debug;

use crate::endian::LittleEndian as LE;
use crate::pe;
use crate::pod::Pod;
use crate::read::{
self, Architecture, Export, FileFlags, Import, NoDynamicRelocationIterator, Object, ObjectKind,
ObjectSection, ReadError, ReadRef, Result, SectionIndex, SubArchitecture, SymbolIndex,
};
use crate::{pe, LittleEndian as LE, Pod};

use super::{
CoffComdat, CoffComdatIterator, CoffSection, CoffSectionIterator, CoffSegment,
Expand Down
7 changes: 5 additions & 2 deletions src/read/coff/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
//! These are used by some Windows linkers as a more compact way to describe
//! dynamically imported symbols.
use crate::read::{Architecture, Error, ReadError, ReadRef, Result};
use crate::{pe, ByteString, Bytes, LittleEndian as LE, SubArchitecture};
use crate::endian::LittleEndian as LE;
use crate::pe;
use crate::read::{
Architecture, ByteString, Bytes, Error, ReadError, ReadRef, Result, SubArchitecture,
};

/// A Windows short form description of a symbol to import.
///
Expand Down
4 changes: 3 additions & 1 deletion src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use core::convert::TryInto;
use core::fmt::Debug;
use core::mem;

use crate::elf;
use crate::endian::{self, Endian, Endianness, U32};
use crate::pod::Pod;
use crate::read::{
self, util, Architecture, ByteString, Bytes, Error, Export, FileFlags, Import, Object,
ObjectKind, ReadError, ReadRef, SectionIndex, StringTable, SymbolIndex,
};
use crate::{elf, endian, Endian, Endianness, Pod, U32};

use super::{
CompressionHeader, Dyn, ElfComdat, ElfComdatIterator, ElfDynamicRelocationIterator, ElfSection,
Expand Down
2 changes: 1 addition & 1 deletion src/read/elf/hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::mem;

use crate::elf;
use crate::endian::{U32, U64};
use crate::read::{ReadError, ReadRef, Result};
use crate::{U32, U64};

use super::{FileHeader, Sym, SymbolTable, Version, VersionTable};

Expand Down
4 changes: 2 additions & 2 deletions src/read/elf/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use core::fmt::Debug;
use core::slice;
use core::str;

use crate::endian::{self, Endianness};
use crate::elf;
use crate::endian::{self, Endianness, U32};
use crate::pod::Pod;
use crate::read::util::StringTable;
use crate::read::{
self, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, SectionIndex, SymbolFlags,
SymbolIndex, SymbolKind, SymbolMap, SymbolMapEntry, SymbolScope, SymbolSection,
};
use crate::{elf, U32};

use super::{FileHeader, SectionHeader, SectionTable};

Expand Down
5 changes: 3 additions & 2 deletions src/read/macho/dyld_cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloc::vec::Vec;
use core::slice;

use crate::read::{Error, File, ReadError, ReadRef, Result};
use crate::{macho, Architecture, Endian, Endianness};
use crate::endian::{Endian, Endianness};
use crate::macho;
use crate::read::{Architecture, Error, File, ReadError, ReadRef, Result};

/// A parsed representation of the dyld shared cache.
#[derive(Debug)]
Expand Down
4 changes: 3 additions & 1 deletion src/read/macho/fat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::endian::BigEndian;
use crate::macho;
use crate::pod::Pod;
use crate::read::{Architecture, Error, ReadError, ReadRef, Result};
use crate::{macho, BigEndian, Pod};

pub use macho::{FatArch32, FatArch64, FatHeader};

Expand Down
10 changes: 6 additions & 4 deletions src/read/macho/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use alloc::vec::Vec;
use core::fmt::Debug;
use core::{mem, str};

use crate::endian::{self, BigEndian, Endian, Endianness};
use crate::macho;
use crate::pod::Pod;
use crate::read::{
self, Architecture, ComdatKind, Error, Export, FileFlags, Import, NoDynamicRelocationIterator,
Object, ObjectComdat, ObjectKind, ObjectMap, ObjectSection, ReadError, ReadRef, Result,
SectionIndex, SubArchitecture, SymbolIndex,
self, Architecture, ByteString, ComdatKind, Error, Export, FileFlags, Import,
NoDynamicRelocationIterator, Object, ObjectComdat, ObjectKind, ObjectMap, ObjectSection,
ReadError, ReadRef, Result, SectionIndex, SubArchitecture, SymbolIndex,
};
use crate::{endian, macho, BigEndian, ByteString, Endian, Endianness, Pod};

use super::{
DyldCacheImage, LoadCommandIterator, MachOSection, MachOSectionInternal, MachOSectionIterator,
Expand Down
12 changes: 7 additions & 5 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::{fmt, result};

use crate::common::*;
pub use crate::common::*;

mod read_ref;
pub use read_ref::*;
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<T> ReadError<T> for Option<T> {
target_pointer_width = "32",
feature = "elf"
))]
pub type NativeFile<'data, R = &'data [u8]> = elf::ElfFile32<'data, crate::Endianness, R>;
pub type NativeFile<'data, R = &'data [u8]> = elf::ElfFile32<'data, crate::endian::Endianness, R>;

/// The native executable file for the target platform.
#[cfg(all(
Expand All @@ -160,15 +160,17 @@ pub type NativeFile<'data, R = &'data [u8]> = elf::ElfFile32<'data, crate::Endia
target_pointer_width = "64",
feature = "elf"
))]
pub type NativeFile<'data, R = &'data [u8]> = elf::ElfFile64<'data, crate::Endianness, R>;
pub type NativeFile<'data, R = &'data [u8]> = elf::ElfFile64<'data, crate::endian::Endianness, R>;

/// The native executable file for the target platform.
#[cfg(all(target_os = "macos", target_pointer_width = "32", feature = "macho"))]
pub type NativeFile<'data, R = &'data [u8]> = macho::MachOFile32<'data, crate::Endianness, R>;
pub type NativeFile<'data, R = &'data [u8]> =
macho::MachOFile32<'data, crate::endian::Endianness, R>;

/// The native executable file for the target platform.
#[cfg(all(target_os = "macos", target_pointer_width = "64", feature = "macho"))]
pub type NativeFile<'data, R = &'data [u8]> = macho::MachOFile64<'data, crate::Endianness, R>;
pub type NativeFile<'data, R = &'data [u8]> =
macho::MachOFile64<'data, crate::endian::Endianness, R>;

/// The native executable file for the target platform.
#[cfg(all(target_os = "windows", target_pointer_width = "32", feature = "pe"))]
Expand Down
3 changes: 2 additions & 1 deletion src/read/pe/data_directory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::slice;

use crate::endian::LittleEndian as LE;
use crate::pe;
use crate::read::{Error, ReadError, ReadRef, Result};
use crate::{pe, LittleEndian as LE};

use super::{
DelayLoadImportTable, ExportTable, ImportTable, RelocationBlockIterator, ResourceDirectory,
Expand Down
3 changes: 2 additions & 1 deletion src/read/pe/export.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloc::vec::Vec;
use core::fmt::Debug;

use crate::endian::{LittleEndian as LE, U16Bytes, U32Bytes};
use crate::pe;
use crate::read::{ByteString, Bytes, Error, ReadError, ReadRef, Result};
use crate::{pe, LittleEndian as LE, U16Bytes, U32Bytes};

/// Where an export is pointing to.
#[derive(Clone, Copy)]
Expand Down
9 changes: 6 additions & 3 deletions src/read/pe/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use core::{mem, str};

use core::convert::TryInto;

use crate::endian::{LittleEndian as LE, U32};
use crate::pe;
use crate::pod::Pod;
use crate::read::coff::{CoffCommon, CoffSymbol, CoffSymbolIterator, CoffSymbolTable, SymbolTable};
use crate::read::{
self, Architecture, ComdatKind, Error, Export, FileFlags, Import, NoDynamicRelocationIterator,
Object, ObjectComdat, ObjectKind, ReadError, ReadRef, Result, SectionIndex, SymbolIndex,
self, Architecture, ByteString, Bytes, CodeView, ComdatKind, Error, Export, FileFlags, Import,
NoDynamicRelocationIterator, Object, ObjectComdat, ObjectKind, ReadError, ReadRef, Result,
SectionIndex, SubArchitecture, SymbolIndex,
};
use crate::{pe, ByteString, Bytes, CodeView, LittleEndian as LE, Pod, SubArchitecture, U32};

use super::{
DataDirectories, ExportTable, ImageThunkData, ImportTable, PeSection, PeSectionIterator,
Expand Down
4 changes: 3 additions & 1 deletion src/read/pe/import.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::fmt::Debug;
use core::mem;

use crate::endian::{LittleEndian as LE, U16Bytes};
use crate::pe;
use crate::pod::Pod;
use crate::read::{Bytes, ReadError, Result};
use crate::{pe, LittleEndian as LE, Pod, U16Bytes};

use super::ImageNtHeaders;

Expand Down
3 changes: 2 additions & 1 deletion src/read/pe/resource.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloc::string::String;
use core::char;

use crate::endian::{LittleEndian as LE, U16Bytes};
use crate::pe;
use crate::read::{ReadError, ReadRef, Result};
use crate::{pe, LittleEndian as LE, U16Bytes};

/// The `.rsrc` section of a PE file.
///
Expand Down
5 changes: 3 additions & 2 deletions src/read/pe/rich.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
use core::mem;

use crate::endian::{LittleEndian as LE, U32};
use crate::pe;
use crate::pod::bytes_of_slice;
use crate::read::Bytes;
use crate::{pe, LittleEndian as LE, ReadRef, U32};
use crate::read::{Bytes, ReadRef};

/// Parsed information about a Rich Header.
#[derive(Debug, Clone, Copy)]
Expand Down
2 changes: 1 addition & 1 deletion src/read/traits.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use alloc::borrow::Cow;
use alloc::vec::Vec;

use crate::endian::Endianness;
use crate::read::{
self, Architecture, CodeView, ComdatKind, CompressedData, CompressedFileRange, Export,
FileFlags, Import, ObjectKind, ObjectMap, Relocation, Result, SectionFlags, SectionIndex,
SectionKind, SegmentFlags, SubArchitecture, SymbolFlags, SymbolIndex, SymbolKind, SymbolMap,
SymbolMapName, SymbolScope, SymbolSection,
};
use crate::Endianness;

/// An object file.
///
Expand Down
2 changes: 1 addition & 1 deletion src/read/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::fmt;
use core::marker::PhantomData;

use crate::pod::{from_bytes, slice_from_bytes, Pod};
use crate::ReadRef;
use crate::read::ReadRef;

/// A newtype for byte slices.
///
Expand Down
3 changes: 1 addition & 2 deletions src/read/xcoff/comdat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
use core::fmt::Debug;

use crate::xcoff;

use crate::read::{self, ComdatKind, ObjectComdat, ReadRef, Result, SectionIndex, SymbolIndex};
use crate::xcoff;

use super::{FileHeader, XcoffFile};

Expand Down
17 changes: 9 additions & 8 deletions src/read/xcoff/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use core::mem;

use alloc::vec::Vec;

use crate::read::{self, Error, NoDynamicRelocationIterator, Object, ReadError, ReadRef, Result};

use crate::{
xcoff, Architecture, BigEndian as BE, FileFlags, ObjectKind, ObjectSection, Pod, SectionIndex,
SymbolIndex,
use crate::endian::BigEndian as BE;
use crate::pod::Pod;
use crate::read::{
self, Architecture, Error, Export, FileFlags, Import, NoDynamicRelocationIterator, Object,
ObjectKind, ObjectSection, ReadError, ReadRef, Result, SectionIndex, SymbolIndex,
};
use crate::xcoff;

use super::{
CsectAux, FileAux, SectionHeader, SectionTable, Symbol, SymbolTable, XcoffComdat,
Expand Down Expand Up @@ -100,7 +101,7 @@ where
type SymbolTable = XcoffSymbolTable<'data, 'file, Xcoff, R>;
type DynamicRelocationIterator = NoDynamicRelocationIterator;

fn architecture(&self) -> crate::Architecture {
fn architecture(&self) -> Architecture {
if self.is_64() {
Architecture::PowerPc64
} else {
Expand Down Expand Up @@ -211,12 +212,12 @@ where
None
}

fn imports(&self) -> Result<alloc::vec::Vec<crate::Import<'data>>> {
fn imports(&self) -> Result<alloc::vec::Vec<Import<'data>>> {
// TODO: return the imports in the STYP_LOADER section.
Ok(Vec::new())
}

fn exports(&self) -> Result<alloc::vec::Vec<crate::Export<'data>>> {
fn exports(&self) -> Result<alloc::vec::Vec<Export<'data>>> {
// TODO: return the exports in the STYP_LOADER section.
Ok(Vec::new())
}
Expand Down
7 changes: 4 additions & 3 deletions src/read/xcoff/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use alloc::fmt;
use core::fmt::Debug;
use core::slice;

use crate::endian::BigEndian as BE;
use crate::pod::Pod;
use crate::{xcoff, BigEndian as BE, Relocation};

use crate::read::{
ReadRef, RelocationEncoding, RelocationFlags, RelocationKind, RelocationTarget, SymbolIndex,
ReadRef, Relocation, RelocationEncoding, RelocationFlags, RelocationKind, RelocationTarget,
SymbolIndex,
};
use crate::xcoff;

use super::{FileHeader, SectionHeader, XcoffFile};

Expand Down
10 changes: 6 additions & 4 deletions src/read/xcoff/section.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::fmt::Debug;
use core::{iter, result, slice, str};

use crate::{
xcoff, BigEndian as BE, CompressedData, CompressedFileRange, Pod, SectionFlags, SectionKind,
use crate::endian::BigEndian as BE;
use crate::pod::Pod;
use crate::read::{
self, CompressedData, CompressedFileRange, Error, ObjectSection, ReadError, ReadRef, Result,
SectionFlags, SectionIndex, SectionKind,
};

use crate::read::{self, Error, ObjectSection, ReadError, ReadRef, Result, SectionIndex};
use crate::xcoff;

use super::{AuxHeader, FileHeader, Rel, XcoffFile, XcoffRelocationIterator};

Expand Down
4 changes: 2 additions & 2 deletions src/read/xcoff/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use core::fmt::Debug;
use core::str;

use crate::read::{self, ObjectSegment, ReadRef, Result};
use crate::read::{self, ObjectSegment, ReadRef, Result, SegmentFlags};
use crate::xcoff;

use super::{FileHeader, XcoffFile};
Expand Down Expand Up @@ -111,7 +111,7 @@ where
unreachable!();
}

fn flags(&self) -> crate::SegmentFlags {
fn flags(&self) -> SegmentFlags {
unreachable!();
}
}
Loading

0 comments on commit 28f2bc6

Please sign in to comment.