How do I generate a binary executable?

Asked

Viewed 206 times

0

Hi, I wanted to know how to make a program in C generate another binary executable eg: the binary Elf x-Elf.bin when running creates another binary executable y-Elf.bin

  • This other binary executable created would be a copy of itself ?

1 answer

0


You can use standard C functions: fopen, fwrite, fread and fclose using the flags wb to write or rb to read binary data.

The mode string can also include the Letter 'b' either as a last Character or as a Character between the characters in any of the two-Character strings described above. This is Strictly for Compatibility with C89 and has no Effect; the 'b' is Ignored on all POSIX conforming sys tems, including Linux. (Other systems may Treat text files and Binary files differently, and Adding the 'b' may be a good idea if you do I/O to a Binary file and expect that your program may be Ported to non-UNIX Environments.)

Source: man 3 fopen

If Voce is making a Linux-only program simply use a fopen with the flag w and write the binary data inside the file.

Example:

#include <stdio.h>

int main()
{
        /* Criando arquivo binario */

        int dados_binarios = 1;

        FILE *file = fopen ("arquivo.bin", "wb");

        if (file != NULL)
        {
            fwrite (&dados_binarios, sizeof (dados_binarios), 1, file);
            fclose (file);
        }

    return 0;
}

More information type man 3 fopen on your terminal.

  • in case I have to put the hexadecimal of a binary?

  • Exactly, you can install a binary file editor to open a file and see the contents, I advise you to use the hexedit, which can be easily installed via the apt install hexedit if your distribution is based on Debian (Ubuntu). Then just type in your terminal hexedit <nome do arquivo>.

  • Thank you very much man, managed to clarify everything I wanted (y)

  • Relax, if I answered your question do not forget to mark the question as answered. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.