Computer system basic study notes (1)-the use of basic GCC, objdump, GBD commands

Use of basic GCC commands

GCC is a set of programming language compilers developed by the GNU project, which can handle C language,
C ++, Fortran, Pascal, Objective-C, Java and so on. GCC is usually the compiler of choice for cross-platform software. gcc is the name of the compiled driver in the GCC suite.

If the computer is an x86-64-bit system, in order to compile to the IA-32 instruction set,
please run the following command first:

sudo apt-get install build-essential module-assistant 
sudo apt-get install gcc-multilib g++-multilib

Next, we will demonstrate this process by outputting a simple C language program hello.c such as hello world.

#include <stdio.h>
int main()
{
	printf("Hello,world\n");
	return 0;
}

The source file hello.c undergoes four processes: preprocessing, compiling, assembling, and linking, and finally generates an executable object file. The schematic diagram is as follows:


The gcc compiler driver in Linux can realize each step of the above process, which will be explained below:

gcc –E hello.c –o hello.i 

Pre-compile the hello.c program. Pre-compilation is to process the commands whose source program starts with the character #. For this, the includecontent of the .h file is embedded in the source program file. For text files, use .i as the extension, as shown in the following figure:

gcc –S hello.i –o hello.s

Compile hello.i to generate an assembly language source program with .s as the extension. The compiled file is still a text file.

gcc –c hello.s –o hello.o

Assemble the hello.s file to generate a relocatable target file with .o as the extension. The assembled file is a binary file. The content is 0, 1 means machine instructions, data and other information.

gcc hello.o –o hello

This command links multiple relocatable object files and standard library functions into one executable file. In this example, the link links the hello.o file with the relocatable target module printf.o where the standard library function printf resides to generate the executable target file hello.

Run the executable file hello input instruction

./hello

The above is a step-by-step conversion of the C language program into an executable target file, you can also use the command

gcc hello.c –o hello 

Compile hello.c directly into executable object file hello.

When compiling the C program with the gcc command, various options are added, such as the following command:

gcc -o0 -m32 -g hello.c -o hello

For example, if you add -O0, the tower indicates the optimization level used during compilation, 0 means no optimization is used for compilation, and the option -m32 means that the instruction is compiled into x86-32 bits. If the computer is a 64-bit processor, without this option, it will be compiled into an x86-64 instruction set. -g means bring debugging information (single step debugging must be added).

Use of objdump command

The target file is composed of 01 sequence of machine instructions, data and other information. It cannot be opened with a text editor. How can I see the content of the target file? The answer is that you can use the objdump tool to disassemble binary object files. You can disassemble both relocatable object files and executable object files.
We use a c program as an example, the program name gdbtest.c.

#include "stdio.h"
int main()
{
	int x=3,y=5,z;
	z=x+y;
	printf("z=%d\n",z);
	return 0;
}

The gcc command can be compiled into the relocatable object file of gdbtest.o and the executable object file of gdbtest, respectively.

gcc -E -g -m32 gdbtest.c -o gdbtest.i
gcc -S -g -m32 gdbtest.i -o gdbtest.s
gcc -c -g -m32 gdbtest.s -o gdbtest.o
gcc -o0 -m32 -g gdbtest.c -o gdbtest

It is recommended to use the -S option in the objdump command and use it together with the -g option in the gcc command.

Use the following command:

objdump –S gdbtest.o>gdbtesto.txt 
objdump –S gdbtest>gdbtest.txt

To disassemble these two files, -S means to add source code to the disassembled content to facilitate understanding of the correspondence between the C language source program and the IA-32 machine-level instructions. The symbol '>' means to save the disassembled content in the file, here it is saved as a text file, in order to prevent the content from being output to the screen too much to read.

The contents of the disassembly file of the relocatable object file of gdbtest.o are as follows:

The disassembled content of the executable object file:

An important difference between relocatable object files and executable object files is whether the address of the instruction starts at address 0. The relocatable target file is an independent module that completes a subtask, so the address of each module starts at address 0, but the instructions and data in the executable target file have a certain address, which is The fixed memory address mapping allocation is also the address that can be seen during the debugging step. Not a physical address of memory, but a virtual address.

Use of GDB debugging tools

Start GDB debugging tool

Start the GDB debugging tool and load the debugged executable file.

command effect
1 gdb [executable file name] Start the GDB debugging tool and load the executable file
2 1.gdb
2. file [executable file name]
Start the GDB debugging tool to
load the executable file

Set a breakpoint

Set a breakpoint to make the program stop at the breakpoint, which is convenient for checking the status of the program.

command effect
break main Set a breakpoint at the entrance of the main function
break gdbtest.c:3 Set a breakpoint at line 3 of the di sa source program gdbtest.c

Start the program

Start and run the already loaded program, the program will stop at the first breakpoint of the execution settings.

command effect
run Start the program and the program stops at the breakpoint

View the current status of the program while it is running

  1. The current breakpoint position
    of the program Meaning: reflect which instructions the program has executed and which instruction to execute next.
    eip register: save the address of the next instruction to be executed.

    and Display the contents of all registers
    and eip Only display the contents of register eip
  2. General register content: ir eax ebx ecx edx (or ir)

  3. Memory unit content: x / 8xb Oxffd2bc

    The x command is used to view the contents of the storage unit, followed by some parameter options.
    The data indicates the number of data units to be displayed.
    x represents the content of the storage unit, in hexadecimal form.
    b indicates the width of the storage unit to be displayed, and the
    following data indicates the starting address of the storage unit to be displayed by byte (w: display by 4 bytes)

    The content of x / 8xb Oxffd2bc means that starting from the address unit of Oxffd2bc, the content of the storage unit of 8 bytes is displayed and expressed in hexadecimal.

  4. View the current status of the runtime

    Note: IA-32 uses the stack to support nested calls of procedures, procedure entry parameters, return addresses, saved register values, non-static local variables in the called procedure, etc. will be saved on the stack.

    Stack frame information:
    current stack frame range: ir esp ebp (esp stack top pointer and ebp stack bottom pointer)

    Current stack frame bytes: y = R [ebp] -R [esp] +4 (Not a command, but a calculation method)

    Display the current stack frame content:

    x/yxb $esp
    x/zxw $esp //z=y/4
    

Continue to execute the next instruction or statement

command effect
and Execute a machine instruction
s Execute a c statement

Exit debugging

command effect
quit Exit the GDB debugging process

The content I shared with you this time is here. I think it's a good idea to support the editor. Your sure is your motivation. In addition, if you want to learn more about the computer professional knowledge and skills, present my personal blog Beiluo . In addition, children's shoes that need various materials, you can follow my WeChat public account Beiluo , free PPT template, various materials waiting for you Come to lead.
Beilu

Guess you like

Origin www.cnblogs.com/xiangjunhong/p/12749675.html