Cross-compile GDB toolchain

1. Cross compilation

Cross-compilation refers to compiling a program on one platform to run on another platform. The platform here involves hardware and software. The hardware platform refers to the CPU architecture, and the software platform refers to the operating system. Cross-compilation is mainly aimed at the embedded field. Because embedded system resources are limited, there is no way to run a set of compilation environment on the embedded platform. Therefore, it is necessary to use the cross-compilation tool chain on other platforms with stronger performance to make Programs running on the platform.

The basic steps of cross compilation are the same as ordinary compilation:

[1] configure configures
before compiling. If the --host parameter is different from the --build parameter, it is cross-compilation. Otherwise, it is normal compilation.
[2] Make
compile. According to the parameters generated by the configure configuration in the previous step, call the corresponding compilation tool chain to compile and generate the target program.
[3] Make install
installation. Install the target program generated by make to the specified directory. If you do not run make install, you can manually copy to the specified directory.

1.1 --build --host --target

Take a look at the definition of the three parameters --build, --host and --target in the configure step. Next, run'./configure --help' in the gdb source directory

./configure --help
System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET   configure for building compilers for TARGET [HOST]

The source code is compiled to generate an executable program. According to the platform that executes the compilation operation, the execution platform of the executable program, and the processing platform of the executable program, the compilation operation can be divided into multiple types, and the corresponding three configuration parameters are as follows:
--build : the platform to run the compilation tool chain , Which is the platform on which the compilation operation is being performed. If this parameter is not specified, it will be guessed through config.guess. Usually this parameter is not specified.

--host : The platform on which the executable program will run. If this parameter is not specified, it is the same as --build. If --host and --build are different, it is cross compilation; otherwise, it is normal compilation.

--target : The platform the executable program will handle. If this parameter is not specified, it is the same as --host. Generally speaking, on what platform the program is running on, what platform is being processed. This parameter value is the same as the --host parameter and does not need to be explicitly specified, so this parameter is usually not paid attention to. But in the special case of making cross-compilation tools (such as gcc, gdb, etc.), this value is different from --host, for example, the cross-compiler arm-linux-gcc, which runs on the x86-linux platform (--host parameter) , But it deals with the arm-linux platform (--target parameter). If it is cross-compiling an ordinary application, such as a tftp program running on the arm-linux platform, its running platform and processing platform are both arm-linux platforms.

1.2 Three types of compilation

Compilation types can be divided into three types, which are mainly reflected in different configuration parameters in the configure phase: (Assuming that the cross-compilation tool chain is installed on the x86-linux platform, and the target embedded platform to be compiled is the arm-linux platform, this is only to illustrate the intent. (The platform naming is not rigorous)

[1] Compile ordinary programs for PC platform
Generally, the following configuration commands are executed:

./configure

Completing the default parameters is actually equivalent to the following command:

./configure --build=x86_linux --host=x86_linux --target=x86_linux

[2] Compiling the cross
-compilation tool chain The special thing about the cross-compilation tool chain is that it runs on the x86-linux platform, but it deals with the arm-linux platform. Making a cross-compilation tool chain does not require cross-compilation. Generally execute the following configuration commands:

./configure --target=arm_linux

Completing the default parameters is actually equivalent to the following command:

./configure --build=x86_linux --host=x86_linux --target=arm_linux

[3] Cross-compilation of embedded platform programs Cross-compilation
is required to generate embedded platform programs. Generally execute the following configuration commands:

./configure --host=arm_linux CC=arm-linux-gcc

Completing the default parameters is actually equivalent to the following command:

./configure --build=x86_linux --host=arm_linux --target=arm_linux CC=arm-linux-gcc

In fact, if we want to make compilation tools that run on embedded platforms (such as gcc, gdb, etc.), we can also execute the above configure command and then execute make, but the generated tools such as gcc, gdb, etc. basically cannot be used on the target embedded platform Because the hardware cannot be moved, the compiler tool chain of the embedded platform is made according to the method in [2], not according to the method in [3].

2. GDB cross-compilation and usage examples

When using GDB in an embedded platform, GDB's remote debugging mode will be used: run the program to be debugged through gdbserver on the target board, run gdb on the host computer and pass'target remote [ip]:[port]' To connect to the gdbserver on the target board to start remote debugging. Various debugging commands are input on the host computer, and the program execution effects (including printing) are displayed on the target board. This is easy to understand, because the program being debugged is originally running on the target board. Do not run gdb directly on the target board for debugging because the target board's hardware configuration is low, and gdb cannot run. Of course, it does not rule out that some embedded platforms have strong performance and can run gdb normally. When the hardware of the embedded platform becomes more and more powerful, the boundary between the embedded platform and the general-purpose computer platform is becoming more and more blurred. The actual situation is exactly like this, the hardware performance is getting stronger and stronger, and the problem of resource shortage is getting less and less. Under this development situation, the decline of embedded technology is an inevitable result.

Setting up the gdb debugging environment under the embedded platform is very helpful for understanding the three parameters --build, --host and --target in the previous cross-compilation process.

2.1 Environmental description

Virtual machine: CPU: x86_64, system: openSUSE Leap 42.3, IP: 192.168.202.240
Development board: CPU: mips mt7688, system: openwrt linux, IP: 192.168.202.141
The C cross compiler installed on the virtual machine is mipsel-openwrt- linux-gcc, the prefix of the cross-compilation tool chain is mipsel-openwrt-linux.

2.2 Download source code

Download the latest version of the source code gdb-8.2.tar.gz from ftp://ftp.gnu.org/gnu/gdb, and unzip it:

[1] tar zxvf ./gdb-8.2.tar.gz
[2] cd gdb-8.2

2.3 Compile gdb

[1] cd gdb-8.2
[2] ./configure --target=mipsel-openwrt-linux
[3] make

gdb runs on a virtual machine, so it does not require cross compilation. Leave the --build and --host parameters blank, the actual platform parameters of the virtual machine are used. Although gdb runs on a virtual machine, it processes the programs of the development board platform, so specify --target as mipsel-openwrt-linux, and the value is the cross-compilation tool chain prefix.

2.4 Cross compile gdbserver

[1] cd gdb-8.2/gdb/gdbserver
[2] ./configure --host=mipsel-openwrt-linux CC=mipsel-openwrt-linux-gcc
[3] make

gdbserver runs on the development board, so cross-compilation is required. Leave the --build parameter blank, the actual use is the platform parameter of the virtual machine. The --host parameter specifies the virtual machine platform and the value is mipsel-openwrt-linux. The --target parameter is left blank, so its value will be equal to the value of the --host parameter.

2.5 Use gdb in remote mode

Development board: The IP of the development board is 192.168.202.141, you can enter any of the following commands

gdbserver :1234 test
gdbserver 127.0.0.1:1234 test
gdbserver 192.168.202.141:1234 test

Host: First run the gdb application in the SHELL command line

./gdb

After running the previous command, SHELL will enter the gdb mode. Among the following commands,'(gdb)' is the prompt:

(gdb) target remote 192.168.202.141
(gdb) b main.c:55
(gdb) c

The first command above is to remotely connect to the gdbserver on the development board. After connecting, it is used normally. The second command is to set a breakpoint. The third command is to run the program. Note that the program to be debugged is actually running on the development board, so use the'c' command instead of the'r' command. If you enter the'r' command, you can see the prompt remote The mode does not support the'r' command:

(gdb) r
The "remote" target does not support "run".  Try "help target" or "continue".

3. Legacy issues

During the cross-compilation process, the prefix of the cross-compilation tool chain is generally used as the value of --host and --target; the --build parameter is generally not specified, and it is automatically guessed during compilation. These are all on a practical level. Generally, even if they are written incorrectly, they will eventually compile after a few tries. For the definition of the platform description, that is, the specific value rules for the values ​​of --build, --host, and --target, I have not found the official source and definition. Yes, I haven't found it online.

4. ./configure -build,-host,-target设置

1. Build: The host that executes the code compilation, normally your host system. This parameter is generally guessed by config.guess. Of course, you can specify it yourself.

2. Host: The host where the compiled binary program is executed, because most of it is executed locally if compiled locally. So this value is equal to build. Only when cross-compiling (that is, compiling locally and executing on other system machines) will build be different from host. Use host to specify the running host.

3. Target: This option is only used when setting up a cross-compilation environment, neither normal compilation nor cross-compilation. He uses the compiler on the build host to compile a new compiler (binutils, gcc, gdb, etc.), and other programs compiled by this new compiler in the future will run on the system specified by the target.

4. Let us take the example of compiling gcc:

1)命令:./configure --build=powerpc-linux --host=powerpc-linux --target=powerpc-linux' 

Note: Use the powerpc-linux compiler (--build) to compile gcc. The compiled gcc runs on powerpc-linux (--host). This gcc is used to compile and run on powerpc-linux (--target) Code.

Role: Of course no one will use this option to compile gcc.

2)命令:./configure --build=i386-linux --host=powerpc-linux --target=powerpc-linux

Description: Use the i386-linux (--build) compiler to compile gcc, the compiled gcc runs on powerpc-linux (--host), this gcc is used to compile and can run on powerpc-linux (--target) Code.

Function: This option can be used to compile its compiler for other machines.

3)命令:./configure --build=i386-linux --host=i386-linux --target=powerpc-linux

Description: Use the i386-linux(--build) compiler to compile gcc, the compiled gcc runs on i386-linux(--host), this gcc is used to compile and can run on powerpc-linux(--target) Code.

Function: This option is used to build a powerpc-linux cross-compilation environment on the i386 host.

4)命令:./configure --build=powerpc-linux --host=i386-linux --target=powerpc-linux

Note: Use the powerpc-linux (--build) compiler to compile gcc. The compiled gcc runs on i386-linux (--host). This gcc is used to compile and run on powerpc-linux (--target) Code.

Function: This option can be used to build a POWrpc-linux cross-compilation environment on the i386 host, but the cross-compilation environment is compiled on powerpc-linux and installed on the i386-linux host. It is estimated that not many people will use it like this.

In general, only when the host !=build compile is cross-compilation. Otherwise, it is compiled normally.

5. References

Embedded Linux GDB debugging environment establishes
Python Exception abnormal information
QT remote debugging of python problems in the ARM board

Guess you like

Origin blog.csdn.net/u013318019/article/details/104391632