Beispiel #1
0
        public unsafe (string, byte[]) Disassemble(byte[] instr)
        {
            sbyte[] buf = new sbyte[80];
            uint    instrSize;
            string? disassembled;

            using (DisposableGCHandle hBytes = DisposableGCHandle.Pin(instr))
                using (DisposableGCHandle hBuf = DisposableGCHandle.Pin(buf)) {
                    instrSize = LLVM.DisasmInstruction(
                        hDasm.Handle.ToPointer(),
                        (byte *)hBytes.AddrOfPinnedObject(),
                        (ulong)instr.Length,
                        0,
                        (sbyte *)hBuf.AddrOfPinnedObject(),
                        new UIntPtr((uint)buf.Length)
                        ).ToUInt32();

                    disassembled = Marshal.PtrToStringAnsi(hBuf.AddrOfPinnedObject());
                }

            byte[] ibytes = new byte[instrSize];
            Array.Copy(instr, 0, ibytes, 0, instrSize);

            disassembled = (disassembled ?? "").TrimStart(new[] { ' ', '\t' });
            return(disassembled, ibytes);
        }
Beispiel #2
0
        public unsafe (string, byte[]?) Disassemble(byte[] bytes)
        {
            buf.Clear();

            ulong pc = 0;

            byte[]? ibytes = null;

            using (DisposableGCHandle hBytes = DisposableGCHandle.Pin(bytes))
            {
                dasmInfo.Buffer       = (byte *)hBytes.AddrOfPinnedObject();
                dasmInfo.BufferLength = (ulong)bytes.Length;

                while (pc < (ulong)bytes.Length)
                {
                    int insn_size = dasm(pc, dasmInfo.__Instance);

                    ibytes = new byte[insn_size];
                    Array.Copy(bytes, (long)pc, ibytes, 0, insn_size);

                    pc += (ulong)insn_size;
                    break; //only first instruction
                }
            }

            string sInstr = SanitizeObjdumpOutput();

            return(sInstr, ibytes);
        }
Beispiel #3
0
        private int fprintf(IntPtr h, string fmt, IntPtr args)
        {
            StringBuilder sb;

            using (DisposableGCHandle argsH = DisposableGCHandle.Pin(args)) {
                IntPtr pArgs = argsH.AddrOfPinnedObject();
                sb = new StringBuilder(_vscprintf(fmt, pArgs) + 1);
                vsprintf(sb, fmt, pArgs);
            }

            var formattedMessage = sb.ToString().Replace("(null)", "\t");

            buf.Append(formattedMessage);
            return(0);
        }
Beispiel #4
0
        private static unsafe LLVMDisasmContextRef Initialize(string triple)
        {
            NativeLibrary.Load(@"C:\msys64\mingw64\bin\libLLVM");

            LLVM.InitializeAllTargetMCs();
            LLVM.InitializeAllTargets();
            LLVM.InitializeAllTargetInfos();
            LLVM.InitializeAllAsmParsers();
            LLVM.InitializeAllAsmPrinters();
            LLVM.InitializeAllDisassemblers();

            byte[] tripleBytes = Encoding.ASCII.GetBytes(triple);

            LLVMDisasmContextRef hDasm;

            using (DisposableGCHandle hTriple = DisposableGCHandle.Pin(tripleBytes)) {
                hDasm = new LLVMDisasmContextRef(
                    new IntPtr(LLVM.CreateDisasm(
                                   (sbyte *)hTriple.AddrOfPinnedObject(),
                                   null, 0, IntPtr.Zero, IntPtr.Zero
                                   ))
                    );
            }

            if (hDasm == null)
            {
                throw new Exception("CreateDisasm failed");
            }

            LLVM.SetDisasmOptions(
                hDasm.Handle.ToPointer(),
                // use alternate variant (Intel) with hex immediates
                (ulong)(LLVMDisassemblerOption.PrintImmHex | LLVMDisassemblerOption.AsmPrinterVariant)
                );

            return(hDasm);
        }