Skip to main content

imping_ir/
module.rs

1//! Modules and their identity.
2
3use crate::{Function, Global, TypeDef, Vtable};
4use serde::{Deserialize, Serialize};
5
6/// One module (image) of a target build: an executable or a shared library.
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9pub struct Module {
10    /// The module's file name (e.g. `"app.exe"`, `"d3d11.dll"`).
11    pub name: String,
12    /// The module's architecture.
13    pub architecture: Architecture,
14    /// The module's preferred load address, from its image header.
15    #[serde(with = "crate::hex::int")]
16    #[cfg_attr(feature = "schema", schemars(with = "String"))]
17    pub image_base: u64,
18    /// The module's build timestamp, from its image header.
19    #[serde(with = "crate::hex::int")]
20    #[cfg_attr(feature = "schema", schemars(with = "String"))]
21    pub timestamp: u32,
22    /// The module's loaded image size in bytes, from its image header.
23    #[serde(with = "crate::hex::int")]
24    #[cfg_attr(feature = "schema", schemars(with = "String"))]
25    pub image_size: u32,
26    /// A content hash of the module file, for a stronger identity check.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub hash: Option<ModuleHash>,
29    /// The module's type table.
30    #[serde(default)]
31    pub types: Vec<TypeDef>,
32    /// The module's functions.
33    #[serde(default)]
34    pub functions: Vec<Function>,
35    /// The module's global variables.
36    #[serde(default)]
37    pub globals: Vec<Global>,
38    /// The module's vtables.
39    #[serde(default)]
40    pub vtables: Vec<Vtable>,
41}
42
43/// A module's instruction-set architecture.
44///
45/// This also fixes the ABI: x64 always uses Win64, while x86 picks a
46/// [`CallingConvention`](crate::CallingConvention) per function.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
48#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
49#[serde(rename_all = "lowercase")]
50pub enum Architecture {
51    /// 32-bit x86.
52    X86,
53    /// 64-bit x86-64.
54    X64,
55}
56
57/// A content hash of a module file.
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
59#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
60pub struct ModuleHash {
61    /// The hash algorithm.
62    pub algorithm: HashAlgorithm,
63    /// The hash digest as a lowercase hex string.
64    pub value: String,
65}
66
67/// A hash algorithm used by [`ModuleHash`].
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
70#[serde(rename_all = "snake_case")]
71#[non_exhaustive]
72pub enum HashAlgorithm {
73    /// SHA-256.
74    Sha256,
75}