How can I generate an ELF file with GCC? - 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 ?

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.

Related

About the meaning of "./a" in gcc

When I run
$ gcc hello.c
$ ./a
Hello, World.
I don't know what ./a exactly indicates.
What is it? What does it stand for?
If you know the meaning of it, I'd really appreciate that you would share.
./ is the current directory when using a Unix-like shell (like bash.) The name of the executable GCC produces is a.exe.
So to run the produced executable, you need to specify the path to to it, in this case "the current directory", which is ./, and the name of the executable, which is a.exe. Since you can omit the .exe when running executables on Windows, instead of ./a.exe you can just run it with ./a.
If you were to use the Windows command-line shell (like cmd.exe or PowerShell) you would instead just type a, because the current directory (.\ in this case, Windows uses \ instead of / for the directory separator character) is searched for executables by default. Unix shells do not, which is why you need ./.
If you want to give the produced executable a different name, for example hello.exe, you can:
gcc hello.c -o hello.exe
You would then run that with:
./hello
or:
./hello.exe
.a is the standard/default output of the compiled program, when no output name is provided.
When you've compiled C program and given no name to the output file, gcc will automatically set the output file name as a.
The file name is overwritten for the last compiled C program, when no output name is provided.
This standard is same in both Unix and Windows.
To set name for the output program, Use -o argument followed by the output name sum_program
gcc sum_program.c -o sum_program
Depending on the library use, and other linkers, additional arguments like -o can be added for compilation.

gcc / (g)as ignore breakpoint trap (x86)

I've written a C program which includes some assembler code in which I have some instructions which lead to a breakpoint exception (INT3) This is nice when debugging (since you don't have to save and restore the breakpoints when restarting the gdb session).
But now I want to disable these traps, which is possible by the -no-break or the -no-trap option of as (the gnu assembler), but I don't find a way to specify these options as options of gcc so that gcc passes the option down to as.
Is there a way to do so? (somehow gcc -g -o file -Xassembler -no-trap main.c file.S does not work)

How to run a c++ program if unordered_map is used

I have used unordered_map in my c++ program. I complied program successfully by using :
g++ -std=c++0x source.cpp
But after that i have entered source to run program but prompt show me that source is not internal or external command.
Default output filename is a.out (a.exe on Windows)
Use -o option to specify file name, like this:
g++ -std=c++0x source.cpp -o source

how to use gcc like assembler?

is it possible? I want to use gcc like assembler and after compile it to executable on ubuntu.
I tried it:
gcc a.asm -o out.o
and from out.o file compiler it to .out executable file.
but I get the following error:
file format not recognized; treating as linker script.
I'm new on linux environment. I hope this is clean for you. Any help is very appreciadted. Thanks in advance.
Change file name a.asm to a.s and let gcc autodetect assembler (by extension).
Read the documentation for the -x option to gcc. It allows you to specify the language of the source file.

How to do source level debugging of x86 code with GDB inside QEMU?

I wrote a x86 assembly program for MBR section.
I compile it as follows:
nasm hellombr.asm -f bin -o hellombr.img
Then I run it in qemu:
qemu -fda hellombr.img -boot a
The question is how can I debug my program at source level?
You should let nasm create the debugging symbols in an ELF file and then dump this to a flat binary to be used in the MBR. You can then instruct GDB to read the necessary symbols from the ELF file.
The complete procedure would then become something like this:
$ nasm hellombr.asm -f elf -g -o hellombr.elf
$ objcopy -O binary hellombr.elf hellombr.img
$ qemu -s -S -fda hellombr.img -boot a
$ gdb
(gdb) symbol-file hellombr.elf
(gdb) target remote localhost:1234
For an explanation of the flags I pass to qemu see this answer.
Instead of using qemu, use bochs. It is completely compatible, albeit slower. It is also an emulator but if you make it from sources, using these flags and build it like this:
./configure --enable-debugger --enable-disasm --disable-docbook
make
make install
you can place breakpoints in your code, step through it, view GDT, IDT and everything you needed to know.
A really good (and simple) way is to use IDA with bochs, you find an excellent blog post on it here, along with some other hints/suggestions for bootloader development.

Resources