Module token

Expand description

Commonly used metadata token type Metadata token implementation for .NET assembly analysis.

This module provides the crate::metadata::token::Token type for representing metadata table references within .NET assemblies. Tokens are fundamental to the .NET metadata system, providing a standardized way to reference entries across different metadata tables according to the ECMA-335 specification.

§Architecture

The token system implements the ECMA-335 metadata addressing scheme, enabling precise and efficient referencing of metadata elements throughout .NET assemblies.

§Token Structure

.NET metadata tokens are 32-bit values with a specific bit layout:

┌─────────────┬───────────────────────────────────┐
│ Table (8b)  │        Row Index (24b)            │
├─────────────┼───────────────────────────────────┤
│   31-24     │            23-0                   │
└─────────────┴───────────────────────────────────┘
  • Table Identifier (bits 24-31): Identifies the metadata table type
  • Row Index (bits 0-23): 1-based index within the specified table

§Key Components

  • crate::metadata::token::Token - Core token implementation with table and row addressing
  • Table Identification - 8-bit table IDs according to ECMA-335 specification
  • Row Addressing - 24-bit 1-based indexing supporting up to 16M entries per table
  • Null References - Special token value (0) indicating absent references

§Usage Examples

§Creating and Inspecting Tokens

use dotscope::metadata::token::Token;

// Create a MethodDef token (table 0x06, row 1)
let method_token = Token::new(0x06000001);

println!("Token: {}", method_token);        // Displays: 0x06000001
println!("Table: 0x{:02x}", method_token.table()); // Table: 0x06
println!("Row: {}", method_token.row());           // Row: 1

// Check for null references
let null_token = Token::new(0);
assert!(null_token.is_null());

§Working with Different Token Types

use dotscope::metadata::token::Token;

// Common .NET metadata token types
let typedef_token = Token::new(0x02000001);     // TypeDef
let typeref_token = Token::new(0x01000001);     // TypeRef  
let methoddef_token = Token::new(0x06000001);   // MethodDef
let memberref_token = Token::new(0x0A000001);   // MemberRef
let fielddef_token = Token::new(0x04000001);    // FieldDef

// Convert between Token and u32
let raw_value: u32 = methoddef_token.into();
let token_from_raw = Token::from(raw_value);
assert_eq!(methoddef_token, token_from_raw);

§Common Token Types

Table IDNameDescriptionExample
0x01TypeRefExternal type references0x01000001
0x02TypeDefType definitions0x02000001
0x04FieldDefField definitions0x04000001
0x06MethodDefMethod definitions0x06000001
0x08ParamParameter definitions0x08000001
0x09InterfaceImplInterface implementations0x09000001
0x0AMemberRefExternal member references0x0A000001
0x1BTypeSpecType specifications0x1B000001
0x2BMethodSpecGeneric method instantiations0x2B000001

§Integration

This module integrates with:

§Usage in .NET Metadata

Tokens serve multiple purposes in the .NET metadata system:

  • IL Instructions: Many IL opcodes reference metadata through tokens
  • Cross-References: Tables reference entries in other tables via tokens
  • Debugging: Debug information uses tokens to reference metadata
  • Reflection: The .NET reflection API uses tokens internally

§Standards Compliance

  • ECMA-335: Full compliance with metadata token specification (§II.24.2.6)
  • Bit Layout: Exact implementation of 8-bit table + 24-bit row format
  • Addressing: Support for 1-based indexing with null reference semantics
  • Type Safety: Strong typing prevents misuse of raw integer values

§References

Structs§

Token
A metadata token representing a reference to a specific entry within a metadata table.