Search code examples
c#gnuelfobjcopy

c# capture raw data from a GNU objcopy process, that dumps to a file


I need to process in-memory within C# some dump offered by OBJCOPY utility.

When used from command prompt, I use it like this:

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

This is how I obtain the RAW content of .text section into a file.

Within C#, I wrote a small process wrapper to launch external programs

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;
}

I have no clue how to fool OBJDUMP and obtain the RAW dump directly into the memory, without having a external file, then open and read binary that file.

A smart guy from this post
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

gave a linux hint to redirect on stdout (which I presume I can capture), but I wasn't able to reproduce on Win.

So, a brilliant mind can figure out a trick, is it possible?

Thank you in advance,


Solution

  • On Windows there is a device "CON" which you might leverage.

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

    I did not test it, because I do not have OBJCOPY, but it worked with OpenSSL. So it should output everything to the console.