1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! # dotnetdll
//!
//! A Rust library for reading and writing .NET assembly metadata, implementing the ECMA-335 (CLI) standard.
//!
//! ## Overview
//!
//! `dotnetdll` provides a complete toolkit for working with .NET metadata at both high and low levels.
//! You can parse existing DLLs, inspect their contents, modify metadata, and generate new assemblies from scratch.
//!
//! The library is organized into several layers, each serving different use cases:
//!
//! - **[`resolution`]**: High-level API for parsing and writing DLLs
//! - **[`resolved`]**: Semantic metadata types (types, methods, IL instructions)
//! - **[`binary`]**: Low-level ECMA-335 binary structures
//! - **[`dll`]**: PE file parsing
//!
//! Most users will work primarily with [`resolution`] and [`resolved`].
//!
//! ## The `Resolution` Struct
//!
//! [`resolution::Resolution`] is the central data structure. It represents all metadata from a DLL:
//!
//! - **Types**: [`type_definitions`](resolution::Resolution::type_definitions) and [`type_references`](resolution::Resolution::type_references)
//! - **Methods/Fields**: [`method_references`](resolution::Resolution::method_references) and [`field_references`](resolution::Resolution::field_references)
//! - **Assemblies**: [`assembly`](resolution::Resolution::assembly) and [`assembly_references`](resolution::Resolution::assembly_references)
//! - **Resources**: [`manifest_resources`](resolution::Resolution::manifest_resources)
//!
//! ### Parsing a DLL
//!
//! ```rust,no_run
//! use dotnetdll::prelude::*;
//!
//! let bytes = std::fs::read("MyLibrary.dll").unwrap();
//! let res = Resolution::parse(&bytes, ReadOptions::default()).unwrap();
//!
//! // Access the assembly name
//! if let Some(assembly) = &res.assembly {
//! println!("Assembly: {}", assembly.name);
//! }
//!
//! // Iterate over all type definitions
//! for (type_idx, typedef) in res.enumerate_type_definitions() {
//! println!("Type: {}", typedef.name);
//! }
//! ```
//!
//! ### Creating a new assembly
//!
//! ```rust,no_run
//! use dotnetdll::prelude::*;
//!
//! let mut res = Resolution::new(Module::new("Example.dll"));
//! res.assembly = Some(Assembly::new("Example"));
//!
//! // Add types, methods, etc.
//! let my_type = res.push_type_definition(
//! TypeDefinition::new(Some("MyNamespace".into()), "MyClass")
//! );
//!
//! let bytes = res.write(WriteOptions::default()).unwrap();
//! std::fs::write("Example.dll", bytes).unwrap();
//! ```
//!
//! ## Typed Indices
//!
//! Instead of using raw `usize` indices, `dotnetdll` uses typed index wrappers like
//! [`resolution::TypeIndex`], [`resolution::MethodIndex`], and [`resolution::FieldIndex`]
//! that automatically index into the correct metadata table from a resolution.
//!
//! ```rust,no_run
//! use dotnetdll::prelude::*;
//! # let bytes = &[];
//! # let res = Resolution::parse(bytes, ReadOptions::default()).unwrap();
//!
//! if let Some(type_idx) = res.type_definition_index(0) {
//! let typedef = &res[type_idx];
//! println!("{}", typedef.name);
//! }
//!
//! // Enumerate with typed indices
//! for (type_idx, typedef) in res.enumerate_type_definitions() {
//! // type_idx is a TypeIndex, not usize
//! for (method_idx, method) in res.enumerate_methods(type_idx) {
//! // method_idx is a MethodIndex
//! println!(" {}", method.name);
//! }
//! }
//! ```
//!
//! ## Type System
//! To prevent certain simple metadata errors at compile time, `dotnetdll` uses a composed type hierarchy:
//! - [`resolved::types::BaseType`] - Core types (primitives, pointers, arrays)
//! - [`resolved::types::MemberType`] - For fields, properties (allows type generics: `T0`, `T1`, ...)
//! - [`resolved::types::MethodType`] - For method signatures (allows both type and method generics: `T0`, `M0`, ...)
//!
//! This design prevents method-level generic variables (`M0`, `M1`) from appearing in field types,
//! where they would be invalid.
//!
//! ```rust
//! use dotnetdll::prelude::*;
//! # let mut res = Resolution::new(Module::new("test"));
//! # let type_idx = res.type_definition_index(0).unwrap();
//!
//! // Fields use MemberType (only type-level generics allowed)
//! let field = Field::instance(
//! Accessibility::Private,
//! "myField",
//! ctype! { string[] } // MemberType
//! );
//!
//! // Method parameters use MethodType (type and method generics allowed)
//! let method = Method::new(
//! Accessibility::Public,
//! msig! { string (int, bool) }, // MethodType in signature
//! "MyMethod",
//! None
//! );
//! ```
//!
//! ## Macros
//!
//! `dotnetdll` provides a small set of convenience macros that are documented where you will
//! typically discover and use them:
//!
//! - Type construction: [`resolved::types::ctype!`]
//! - Type references: [`resolved::types::type_ref!`]
//! - Method signatures: [`resolved::signature::msig!`]
//! - IL instruction lists + labels: [`asm!`] (see [`resolved::il`])
//! - Accessibility keywords: [`access!`] (see [`resolved`])
//! - External member references: [`resolved::members::method_ref!`], [`resolved::members::field_ref!`]
//!
//! The constructor-style macros support *substitution* of existing Rust values via `#var` (move) and
//! `@var` (clone); see the `ctype!`/`msig!` docs for details.
//!
//! ## Working with IL
//!
//! IL instructions are represented with the [`resolved::il::Instruction`] enum. Branch targets use
//! instruction indices (not byte offsets) - the library handles offset calculation automatically.
//!
//! See [`resolved::il`] for the complete instruction set and [`resolved::body::Method`] for method body construction.
//!
//! ## Custom Attributes
//!
//! Custom attributes can be added to most metadata elements. To decode their instantiation data,
//! you need a [`resolved::types::Resolver`] that can locate referenced types.
//!
//! See [`resolved::attribute`] for details.
//!
//! ## Error Handling
//!
//! Operations that can fail return [`Result<T, DLLError>`](dll::DLLError). Common errors include:
//!
//! - Invalid PE format
//! - Malformed metadata tables
//! - Invalid signatures or IL bytecode
//!
//! ## Examples
//!
//! The repository includes two example projects:
//!
//! - **`dump-dll`** - Parses and prints DLL contents (good for learning the API)
//! - **`smolasm`** - Mini-assembler demonstrating metadata construction
//!
//! Run them with:
//! ```bash
//! cargo run -p dump-dll -- path/to/Some.dll
//! cargo run -p smolasm -- --help
//! ```
//!
//! ## ECMA-335 Standard
//!
//! This library implements the Common Language Infrastructure physical format as specified in the
//! [ECMA-335](https://www.ecma-international.org/publications-and-standards/standards/ecma-335/) standard.
//! Familiarity with the standard is very helpful, but ultimately not required.
/// Commonly used types and traits for working with dotnetdll.
///
/// This module re-exports the most frequently used items to simplify imports.
/// Most code using this library should start with:
///
/// ```rust
/// use dotnetdll::prelude::*;
/// ```