// Copyright (c) StellaOps. All rights reserved. // Licensed under BUSL-1.1. See LICENSE in the project root. using System.Collections.Immutable; namespace StellaOps.BinaryIndex.Disassembly; /// /// CPU architecture identifier. /// public enum CpuArchitecture { /// Unknown architecture. Unknown = 0, /// Intel/AMD 32-bit x86. X86 = 1, /// Intel/AMD 64-bit x86-64 (amd64). X86_64 = 2, /// ARM 32-bit (ARMv7). ARM32 = 3, /// ARM 64-bit (AArch64/ARMv8). ARM64 = 4, /// MIPS 32-bit. MIPS32 = 5, /// MIPS 64-bit. MIPS64 = 6, /// RISC-V 64-bit. RISCV64 = 7, /// PowerPC 32-bit. PPC32 = 8, /// PowerPC 64-bit. PPC64 = 9, /// SPARC. SPARC = 10, /// SuperH SH4. SH4 = 11, /// AVR microcontroller. AVR = 12, /// Ethereum Virtual Machine. EVM = 13, /// WebAssembly. WASM = 14 } /// /// Binary executable format. /// public enum BinaryFormat { /// Unknown format. Unknown = 0, /// Raw binary data (no format metadata). Raw = 1, /// Executable and Linkable Format (Linux, BSD, etc.). ELF = 2, /// Portable Executable (Windows). PE = 3, /// Mach-O (macOS, iOS). MachO = 4, /// WebAssembly module. WASM = 5 } /// /// Describes the capabilities of a disassembly plugin. /// public sealed record DisassemblyCapabilities { /// /// The unique identifier of the plugin. /// public required string PluginId { get; init; } /// /// Display name of the disassembly engine. /// public required string Name { get; init; } /// /// Version of the underlying disassembly library. /// public required string Version { get; init; } /// /// Supported CPU architectures. /// public required ImmutableHashSet SupportedArchitectures { get; init; } /// /// Supported binary formats. /// public required ImmutableHashSet SupportedFormats { get; init; } /// /// Whether the plugin supports lifting to intermediate representation. /// public bool SupportsLifting { get; init; } /// /// Whether the plugin supports control flow graph recovery. /// public bool SupportsCfgRecovery { get; init; } /// /// Priority for plugin selection when multiple plugins support the same arch/format. /// Higher values indicate higher priority. /// public int Priority { get; init; } = 0; /// /// Checks if this plugin supports the given architecture. /// public bool SupportsArchitecture(CpuArchitecture arch) => SupportedArchitectures.Contains(arch); /// /// Checks if this plugin supports the given format. /// public bool SupportsFormat(BinaryFormat format) => SupportedFormats.Contains(format); /// /// Checks if this plugin can handle the given architecture and format combination. /// public bool CanHandle(CpuArchitecture arch, BinaryFormat format) => SupportsArchitecture(arch) && SupportsFormat(format); } /// /// Information about a loaded binary. /// /// Binary format: ELF, PE, MachO, etc. /// CPU architecture. /// 32 or 64 bit. /// Byte order. /// Application binary interface hint (gnu, musl, msvc, darwin). /// Entry point address if available. /// Build identifier if present (e.g., GNU build-id). /// Additional metadata from the binary. /// Internal handle for the disassembly engine (engine-specific). public sealed record BinaryInfo( BinaryFormat Format, CpuArchitecture Architecture, int Bitness, Endianness Endianness, string? Abi, ulong? EntryPoint, string? BuildId, IReadOnlyDictionary Metadata, object Handle); /// /// Byte order. /// public enum Endianness { /// Little-endian (LSB first). Little, /// Big-endian (MSB first). Big } /// /// Represents a code region (section) in a binary. /// /// Section name: .text, .rodata, etc. /// Virtual address in memory. /// Offset in the binary file. /// Size in bytes. /// Whether the region contains executable code. /// Whether the region is readable. /// Whether the region is writable. public sealed record CodeRegion( string Name, ulong VirtualAddress, ulong FileOffset, ulong Size, bool IsExecutable, bool IsReadable, bool IsWritable); /// /// Information about a symbol in the binary. /// /// Symbol name. /// Virtual address of the symbol. /// Size in bytes (0 if unknown). /// Symbol type. /// Symbol binding. /// Section containing the symbol. public sealed record SymbolInfo( string Name, ulong Address, ulong Size, SymbolType Type, SymbolBinding Binding, string? Section); /// /// Type of symbol. /// public enum SymbolType { /// Unknown or unspecified type. Unknown, /// Function/procedure. Function, /// Data object. Object, /// Section symbol. Section, /// Source file name. File, /// Common block symbol. Common, /// Thread-local storage. Tls } /// /// Symbol binding/visibility. /// public enum SymbolBinding { /// Unknown binding. Unknown, /// Local symbol (not visible outside the object). Local, /// Global symbol (visible to other objects). Global, /// Weak symbol (can be overridden). Weak } /// /// A disassembled instruction. /// /// Virtual address of the instruction. /// Raw bytes of the instruction. /// Instruction mnemonic (e.g., MOV, ADD, JMP). /// Text representation of operands. /// Classification of the instruction. /// Parsed operands. public sealed record DisassembledInstruction( ulong Address, ImmutableArray RawBytes, string Mnemonic, string OperandsText, InstructionKind Kind, ImmutableArray Operands); /// /// Classification of instruction types. /// public enum InstructionKind { /// Unknown or unclassified instruction. Unknown, /// Arithmetic operation (ADD, SUB, MUL, DIV). Arithmetic, /// Logical operation (AND, OR, XOR, NOT). Logic, /// Data movement (MOV, PUSH, POP). Move, /// Memory load operation. Load, /// Memory store operation. Store, /// Unconditional branch (JMP). Branch, /// Conditional branch (JE, JNE, JL, etc.). ConditionalBranch, /// Function call. Call, /// Function return. Return, /// No operation. Nop, /// System call. Syscall, /// Software interrupt. Interrupt, /// Compare operation. Compare, /// Shift operation. Shift, /// Vector/SIMD operation. Vector, /// Floating point operation. FloatingPoint } /// /// An instruction operand. /// /// Operand type. /// Text representation. /// Immediate value if applicable. /// Register name if applicable. /// Base register for memory operand. /// Index register for memory operand. /// Scale factor for indexed memory operand. /// Displacement for memory operand. public sealed record Operand( OperandType Type, string Text, long? Value = null, string? Register = null, string? MemoryBase = null, string? MemoryIndex = null, int? MemoryScale = null, long? MemoryDisplacement = null); /// /// Type of operand. /// public enum OperandType { /// Unknown operand type. Unknown, /// CPU register. Register, /// Immediate value. Immediate, /// Memory reference. Memory, /// Address/label. Address }