idalib/
lib.rs

1//! # idalib
2//!
3//! idalib is a Rust library providing idiomatic bindings for the IDA SDK, enabling the development
4//! of standalone analysis tools using IDA v9.x’s idalib.
5//!
6//! ## Usage
7//!
8//! To use idalib, add it as a dependency in your `Cargo.toml` and include a `build.rs` file in
9//! your project to properly link against IDA:
10//!
11//! ```toml
12//! [dependencies]
13//! idalib = "0.7"
14//!
15//! [build-dependencies]
16//! idalib-build = "0.7"
17//! ```
18//!
19//! Here is a basic example of a `build.rs` file:
20//!
21//! ```rust,ignore
22//! fn main() -> Result<(), Box<dyn std::error::Error>> {
23//!     idalib_build::configure_linkage()?;
24//!     Ok(())
25//! }
26//! ```
27//!
28//! This script uses the `idalib-build` crate to automatically configure the linkage against IDA.
29//! If IDA is installed in a non-default location, ensure that `IDADIR` is set to point to your
30//! installation directory, if you are linking against IDA's shared libraries, as opposed to the
31//! stub libraries distributed with the SDK.
32//!
33//! ## Setting Environment Variables
34//!
35//! ### On Linux/macOS
36//!
37//! You can set the environment variables in your terminal session or add them to your shell
38//! configuration file (e.g., `.bashrc`, `.zshrc`):
39//!
40//! ```sh,ignore
41//! export IDADIR=/path/to/ida/installation
42//! ```
43//!
44//! ### On Windows
45//!
46//! Set environment variables using Command Prompt, PowerShell, or System Properties.
47//!
48//! **Command Prompt:**
49//! ```cmd
50//! set IDADIR=C:\path\to\ida\installation
51//! ```
52//!
53//! **PowerShell:**
54//! ```powershell,ignore
55//! $env:IDADIR = "C:\path\to\ida\installation"
56//! ```
57//!
58//! **System Properties:**
59//! Go to "Environment Variables" in System Properties and add `IDADIR`.
60//!
61//! ## Example
62//!
63//! Here's a simple example of how to use idalib:
64//!
65//! ```rust,ignore
66//! use idalib::idb::IDB;
67//!
68//! fn main() -> Result<(), Box<dyn std::error::Error>> {
69//!     let idb = IDB::open("/path/to/binary")?;
70//!     // Perform analysis...
71//!     Ok(())
72//! }
73//! ```
74//!
75#![allow(clippy::needless_lifetimes)]
76
77use std::marker::PhantomData;
78use std::sync::{Mutex, MutexGuard, OnceLock};
79
80pub mod bookmarks;
81pub mod decompiler;
82pub mod func;
83pub mod idb;
84pub mod insn;
85pub mod license;
86pub mod meta;
87pub mod name;
88pub mod plugin;
89pub mod processor;
90pub mod segment;
91pub mod strings;
92pub mod xref;
93
94pub use idalib_sys as ffi;
95
96pub use ffi::IDAError;
97pub use idb::{IDB, IDBOpenOptions};
98pub use license::{LicenseId, is_valid_license, license_id};
99
100pub type Address = u64;
101pub struct AddressFlags<'a> {
102    flags: ffi::bytes::flags64_t,
103    _marker: PhantomData<&'a IDB>,
104}
105
106impl<'a> AddressFlags<'a> {
107    pub(crate) fn new(flags: ffi::bytes::flags64_t) -> Self {
108        Self {
109            flags,
110            _marker: PhantomData,
111        }
112    }
113
114    pub fn is_code(&self) -> bool {
115        unsafe { ffi::bytes::is_code(self.flags) }
116    }
117
118    pub fn is_data(&self) -> bool {
119        unsafe { ffi::bytes::is_data(self.flags) }
120    }
121}
122
123#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
124pub struct IDAVersion {
125    major: i32,
126    minor: i32,
127    build: i32,
128}
129
130impl IDAVersion {
131    pub fn major(&self) -> i32 {
132        self.major
133    }
134
135    pub fn minor(&self) -> i32 {
136        self.minor
137    }
138
139    pub fn build(&self) -> i32 {
140        self.build
141    }
142}
143
144static INIT: OnceLock<Mutex<()>> = OnceLock::new();
145
146#[cfg(not(target_os = "windows"))]
147unsafe extern "C" {
148    static mut batch: std::ffi::c_char;
149}
150
151pub(crate) type IDARuntimeHandle = MutexGuard<'static, ()>;
152
153pub fn force_batch_mode() {
154    #[cfg(not(target_os = "windows"))]
155    unsafe {
156        batch = 1;
157    }
158}
159
160pub fn init_library() -> &'static Mutex<()> {
161    INIT.get_or_init(|| {
162        force_batch_mode();
163        ffi::ida::init_library().expect("IDA initialised successfully");
164        Mutex::new(())
165    })
166}
167
168pub(crate) fn prepare_library() -> IDARuntimeHandle {
169    let mutex = init_library();
170    mutex.lock().unwrap()
171}
172
173pub fn enable_console_messages(enabled: bool) {
174    init_library();
175    ffi::ida::enable_console_messages(enabled);
176}
177
178pub fn version() -> Result<IDAVersion, IDAError> {
179    ffi::ida::library_version().map(|(major, minor, build)| IDAVersion {
180        major,
181        minor,
182        build,
183    })
184}