Skip to main content

Module metadata

Module metadata 

Expand description

.NET metadata parsing, loading, and type system based on ECMA-335.

This module implements the complete ECMA-335 metadata system for .NET assemblies. It provides comprehensive parsing and access to all metadata tables, streams, and type system constructs defined in the Common Language Infrastructure specification.

§Architecture

The metadata system is organized into several layers:

  • Physical Layer: Raw binary data access and stream parsing
  • Logical Layer: Structured access to metadata tables and heaps
  • Type System Layer: High-level representation of .NET types and signatures
  • Validation Layer: Configurable validation and integrity checking

§Key Components

§Assembly Analysis

§Type System

§Method Body Analysis

§Metadata Streams

§Metadata Tables

§Import/Export Analysis

§Security and Identity

§Usage Examples

use dotscope::CilObject;
use std::path::Path;

// Load assembly and examine metadata
let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;

// Access basic information
if let Some(module) = assembly.module() {
    println!("Module: {}", module.name);
}

// Examine metadata tables
if let Some(tables) = assembly.tables() {
    println!("Tables present: {}", tables.table_count());
}

// Access type system
let methods = assembly.methods();
println!("Methods found: {}", methods.len());

§Integration

The metadata system provides the foundation for the disassembler module, supplying token resolution, type information, and method body access for comprehensive analysis.

§Thread Safety

All metadata types are std::marker::Send and std::marker::Sync for safe concurrent access. Metadata parsing and representation for .NET assemblies.

This module contains the core metadata parsing infrastructure for .NET PE files, providing comprehensive support for parsing metadata tables, streams, type systems, and IL code according to the ECMA-335 standard. It serves as the foundation for all .NET assembly analysis capabilities in dotscope.

§Architecture Overview

The metadata system is organized into several interconnected layers:

§Core Assembly Representation

§Type System and Signatures

§Method and Code Analysis

§Metadata Streams and Tables

  • Stream Management: crate::metadata::streams handles all metadata heap types (strings, blobs, GUIDs, user strings)
  • Table Processing: crate::metadata::tables provides access to all ECMA-335 metadata tables
  • Data Loading: Internal loader implements the metadata loading and caching infrastructure

§Interoperability and Resources

§Quality and Validation

§Key Components

§Primary Assembly Interface

§Core Infrastructure

§Usage Patterns

§Basic Assembly Loading and Analysis

use dotscope::CilObject;
use std::path::Path;

// Load and parse a .NET assembly
let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;

// Access basic assembly information
if let Some(assembly_info) = assembly.assembly() {
    println!("Assembly: {} (Version: {}.{})",
             assembly_info.name, assembly_info.major_version, assembly_info.minor_version);
}

// Get counts of major metadata elements
println!("Methods: {}", assembly.methods().len());
println!("Types: {}", assembly.types().len());
if let Some(module) = assembly.module() {
    println!("Module name: {}", module.name);
}

§Method Analysis and IL Code Inspection

use dotscope::CilObject;
use std::path::Path;

let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;

// Analyze methods with IL code
for entry in assembly.methods().iter().take(10) {
    let method = entry.value();
     
    if method.instruction_count() > 0 {
        println!("Method: {} ({} instructions)",
                 method.name, method.instruction_count());
         
        // Examine method characteristics
        if method.flags_modifiers.contains(
            dotscope::metadata::method::MethodModifiers::VIRTUAL) {
            println!("  - Virtual method");
        }
         
        if method.flags_modifiers.contains(
            dotscope::metadata::method::MethodModifiers::STATIC) {
            println!("  - Static method");
        }
    }
}

§Type System Exploration

use dotscope::CilObject;
use std::path::Path;

let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;

// Explore the type system
for entry in assembly.types().iter().take(10) {
    let type_def = entry.value();
     
    println!("Type: {} (Namespace: {})",
             type_def.name, type_def.namespace);
     
    // Show type characteristics using flags
    if type_def.flags_visibility.contains(TypeVisibility::PUBLIC) {
        println!("  - Public visibility");
    }
     
    if type_def.flags_layout.contains(TypeLayout::ABSTRACT) {
        println!("  - Abstract type");
    }
     
    if type_def.flags_layout.contains(TypeLayout::SEALED) {
        println!("  - Sealed type");
    }
     
    println!("  - {} methods, {} fields",
             type_def.methods.len(), type_def.fields.len());
}

§Thread Safety

All metadata types are designed for safe concurrent access:

  • Immutable Data: Most metadata structures are read-only after parsing
  • Arc-based Sharing: Reference counting enables safe multi-threaded access
  • Lazy Initialization: OnceLock ensures thread-safe lazy loading

§Error Handling

The metadata system provides comprehensive error handling for malformed assemblies:

  • Format Validation: ECMA-335 compliance checking
  • Bounds Checking: Safe access to all metadata structures
  • Graceful Degradation: Partial parsing when possible
  • Detailed Diagnostics: Clear error messages for debugging

§References

  • ECMA-335 6th Edition - Common Language Infrastructure (CLI)
  • Microsoft .NET Framework PE Format Specification
  • Windows PE/COFF Specification

Modules§

cilassemblyview
Implementation of a raw assembly view for editing operations Raw assembly view for editing and modification operations.
cilobject
Implementation of a loaded + parsed CIL binary High-level .NET assembly abstraction and metadata access.
cor20header
Implementation of the Header of CIL CLR 2.0 (Cor20) header parsing for .NET assemblies.
customattributes
Implementation of custom attribute parsing and representation Custom attribute parsing and representation for .NET metadata.
customdebuginformation
Implementation of custom debug information parsing for Portable PDB format Custom debug information parsing for Portable PDB format.
dependencies
Multi-assembly dependency tracking and analysis system Multi-assembly dependency tracking and analysis system.
diagnostics
Diagnostics collection for assembly loading and analysis Diagnostics collection for assembly loading and analysis.
exports
Implementation of ‘Exports’ by the loaded binary Analysis and representation of exported types in .NET assemblies.
identity
Assembly identity and cryptographic verification system Assembly identity and cryptographic verification for .NET assemblies.
imports
Implementation of methods that are imported from other binaries (native or .net) Analysis and representation of imported types and methods in .NET assemblies.
importscope
Implementation of import scope parsing for Portable PDB format Import scope parsing and representation for Portable PDB debugging metadata.
marshalling
Implementation of the type marshalling for native code invokations Type marshalling for native code invocations and COM interop in .NET assemblies.
method
Implementation of the MethodHeader of CIL Method representation and analysis for .NET assemblies.
query
Composable query system for types and methods Composable query system for types and methods.
resources
Implementation of the .NET resources Embedded resources and manifest resource management for .NET assemblies.
root
Implementation of the root metadata structure Metadata root header and stream directory for .NET assemblies.
security
Implementation of the .NET security model .NET Code Access Security (CAS) implementation.
sequencepoints
Implementation of sequence points in methods
signatures
Implementation of method and type signatures Method and type signature parsing for .NET metadata according to ECMA-335.
streams
Implementation of all metadata streams (tables, heaps, etc.) ECMA-335 Metadata Streams for .NET Assembly Processing
tablefields
Table field layout definitions for heap references Table field layout definitions for heap references.
tables
Implementation of the .NET metadata tables
token
Commonly used metadata token type Metadata token implementation for .NET assembly analysis.
typesystem
Implementation of the .NET type system .NET type system implementation for CIL analysis.
validation
Metadata validation utilities Metadata validation system for .NET assemblies.