1

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,

yo3hcv
  • 1,531
  • 2
  • 17
  • 27
  • There is no equivalent to /dev/null in windows. – jdweng Aug 07 '19 at 16:48
  • appears there is 1&2 > nul in cmd.exe but in my case, calling directly the app.exe just does nothing (it just hangs my console) :) – yo3hcv Aug 07 '19 at 16:59
  • The 1&2 all it does is attach the Standard Error (2) to Standard output (1). Your app write to a file and there is no way in windows to redirect the file write. In Linux there is a null file in the operating system which the output can be piped to other applications. The null file is really an application that just takes standard input and redirects it to standard output. – jdweng Aug 07 '19 at 17:12

1 Answers1

1

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.

Andreas
  • 828
  • 4
  • 15
  • Hmm.. is working under console, CON did the trick BIG TAHNKS! But I should modify my wrapper to be able to capture binary if it's possible. Still working... – yo3hcv Aug 07 '19 at 16:54
  • Actually, it works :)) but I haven't suceeded to get the redirected data from within C# :(( But anyhow, this is the correct answer to what I ask for. – yo3hcv Aug 21 '19 at 16:13
  • Use the Event for incoming data and add them to a memorystream. Otherwise, if it‘s too much data, it will stop and you will never get past proc.waitforexit() – Andreas Aug 22 '19 at 17:50