73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
// Copyright (c) StellaOps. All rights reserved.
|
|
// Licensed under BUSL-1.1. See LICENSE in the project root.
|
|
using B2R2;
|
|
using B2R2.FrontEnd;
|
|
using B2R2.FrontEnd.BinFile;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace StellaOps.BinaryIndex.Disassembly.B2R2;
|
|
|
|
public sealed partial class B2R2DisassemblyPlugin
|
|
{
|
|
/// <inheritdoc />
|
|
public BinaryInfo LoadBinary(Stream stream, CpuArchitecture? archHint = null, BinaryFormat? formatHint = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(stream);
|
|
|
|
using var memStream = new MemoryStream();
|
|
stream.CopyTo(memStream);
|
|
return LoadBinary(memStream.ToArray(), archHint, formatHint);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public BinaryInfo LoadBinary(ReadOnlySpan<byte> bytes, CpuArchitecture? archHint = null, BinaryFormat? formatHint = null)
|
|
{
|
|
var byteArray = bytes.ToArray();
|
|
|
|
_logger.LogDebug("Loading binary with B2R2 plugin (size: {Size} bytes)", byteArray.Length);
|
|
|
|
var isa = archHint.HasValue
|
|
? MapToB2R2Isa(archHint.Value)
|
|
: new ISA(Architecture.Intel, WordSize.Bit64); // Default to x64
|
|
|
|
var binHandle = new BinHandle(byteArray, isa, null, true);
|
|
var binFile = binHandle.File;
|
|
|
|
var format = MapFromB2R2Format(binFile.Format);
|
|
var architecture = MapFromB2R2Architecture(binFile.ISA);
|
|
var bitness = GetBitness(binFile.ISA.WordSize);
|
|
var endianness = binFile.ISA.Endian == Endian.Little ? Endianness.Little : Endianness.Big;
|
|
var abi = DetectAbi(format);
|
|
|
|
var entryPointOpt = binFile.EntryPoint;
|
|
var entryPoint = Microsoft.FSharp.Core.FSharpOption<ulong>.get_IsSome(entryPointOpt)
|
|
? entryPointOpt.Value
|
|
: (ulong?)null;
|
|
|
|
_logger.LogInformation(
|
|
"Loaded binary with B2R2: Format={Format}, Architecture={Architecture}, Endian={Endian}",
|
|
format, architecture, endianness);
|
|
|
|
var metadata = new Dictionary<string, object>
|
|
{
|
|
["size"] = byteArray.Length,
|
|
["b2r2_isa"] = binFile.ISA.Arch.ToString()
|
|
};
|
|
if (entryPoint.HasValue)
|
|
{
|
|
metadata["entry_point"] = entryPoint.Value;
|
|
}
|
|
|
|
return new BinaryInfo(
|
|
Format: format,
|
|
Architecture: architecture,
|
|
Bitness: bitness,
|
|
Endianness: endianness,
|
|
Abi: abi,
|
|
EntryPoint: entryPoint,
|
|
BuildId: null,
|
|
Metadata: metadata,
|
|
Handle: new B2R2BinaryHandle(binHandle, byteArray));
|
|
}
|
|
}
|