C#从GNU objcopy进程捕获原始数据,并将其转储到文件

时间:2019-08-07 16:11:20

标签: c# gnu elf objcopy

我需要在C#中处理OBJCOPY实用程序提供的一些转储中的内存。

在命令提示符下使用时,我会这样使用:

objcopy myprogram.elf --dump-section .text=text_section.txt

这是我将.text部分的RAW内容获取到文件中的方式。

在C#中,我编写了一个小的进程包装程序来启动外部程序

public static int RunProcess(string cmd, string args, out string output)
{
    Process proc = new Process();

    try
    {

        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        proc.StartInfo.FileName = cmd;

        proc.StartInfo.Arguments = args;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;

        proc.Start();
        output = proc.StandardOutput.ReadToEnd();

        proc.WaitForExit(); // wait here, blocking

    }
    catch (Exception ex)
    {
        output = cmd + ": " + ex.Message;
        return 1;
    }

    if (proc.ExitCode != 0)
    {
        output = proc.StandardError.ReadToEnd().Truncate(PROC_TRUNC_OUT).Replace("\r\n", " ").Replace("\r", " ").Replace("\n", " ");
        return 1;
    }

    return 0;
}

我不知道如何在没有外部文件的情况下欺骗OBJDUMP并直接将RAW转储到内存中,然后打开并读取该文件的二进制文件。

这篇文章中的一个聪明人
How can I examine contents of a data section of an ELF file on Linux?

objcopy file /dev/null --dump-section .text=/dev/stdout | cat

给出了一个Linux提示以重定向到stdout(我想我可以捕获),但是我无法在Win上重现。

那么,一个聪明的头脑可以找出一个把戏,这可能吗?

先谢谢您

1 个答案:

答案 0 :(得分:1)

在Windows上,您可以使用设备“ CON”。

objcopy file "someFile" --dump-section .text=CON

我没有测试它,因为我没有OBJCOPY,但是它可以与OpenSSL一起使用。因此,它应该将所有内容输出到控制台。