Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate an ELF file with GCC?

I am writing C and C++ code on Linux OS and I am using GCC. After finishing my code, I would like to generate an ELF file. I just can generate "a.out" file and I don't need it. How can I get ELF file ? ELF file occurs as a result of what ? or Is it possible to generate this file with this program ?

like image 797
Caner Bacaksız Avatar asked Feb 10 '14 22:02

Caner Bacaksız


People also ask

How do you create an ELF file?

To generate ELF production file, first combine the bootloader and application firmware HEX files in a single HEX file. To combine the bootloader and application HEX files, use a command line tool like srec_cat (http://srecord.sourceforge.net/). This tool is part of the srecord utilities that are available with WinAVR.

Does GCC output ELF?

gcc produces executable files in the ELF file format. you can use readelf and objdump to read parts of an elf file. You can also use 'hexdump filename' to get a hexdump of the contents of a binary file (this is likely only useful if you like reading machine code or you are writing an assembler).

Where is ELF file located?

To find them the ELF header is used, which is located at the very start of the file. The first bytes contain the elf magic "\x7fELF" , followed by the class ID (32 or 64 bit ELF file), the data format ID (little endian/big endian), the machine type, etc. Finally, the entry point of this file is at address 0x0.


1 Answers

The compiler (i.e. gcc or g++) will invoke the linker (ld) which produces an ELF executable.

In practice, you will use a builder program (like make) to drive gcc commands. See this answer.

The default output file for gcc is still named a.out (for historical reasons) but is an ELF file. And you really want to ask gcc to output an executable with a more fancy name.

Simple example, you code a single-file hello-world.c program. You can compile it with e.g.

 gcc -Wall -g hello-world.c -o hello-world-bin

(order of arguments to gcc matters a lot!)

and the produced hello-world-bin is an ELF executable. Check with

 file hello-world-bin

then run it with

 ./hello-world-bin your arguments to it

Later, learn how to use the gdb debugger on it.

See also this and that answers.

like image 155
Basile Starynkevitch Avatar answered Oct 03 '22 12:10

Basile Starynkevitch