Basic concepts of cross-compilation

1 What is cross compilation

1.1 Local compilation

Before explaining what cross-compilation is, it is necessary to understand what local compilation is.

Local compilation can be understood as, under the current compilation platform, the compiled program can only be run on the current platform. Our common software development usually belongs to local compilation.

For example, we write programs on the X86 platform and compile them into executable files. In this way, we use the tools of the X86 platform to develop executable programs for the X86 platform itself. This compilation process is called local compilation.

1.2 Cross Compilation

Cross-compilation can be understood as, under the current compilation platform, the compiled program can run on another target platform with a different architecture, but the compilation platform itself cannot run the modified program.

For example, we write a program on the x86 platform and compile it into a program that can run on the ARM platform. The compiled program cannot run on the x86 platform and must be placed on the ARM platform to run.

1.3 Why is there cross-compilation?

The main reasons for cross-compilation are:

  • Speed: The target platform tends to run much slower than the host, and many dedicated embedded hardware are designed to be low-cost and low-power, without too much performance
  • Capability: The entire compilation process is very resource-intensive, and embedded systems often do not have enough memory or disk space
  • Availability: Even if the target platform has sufficient resources and can be compiled locally, the first local compiler that runs on the target platform always needs to be obtained through cross-compilation
  • Flexibility: A complete Linux compilation environment requires many support packages, cross-compilation saves us from spending time porting various support packages to the target board

1.4 Why is cross-compilation difficult?

The difficulty of cross-compilation lies in two aspects:

Different architectures have different machine characteristics

  • Word size: Is it a 64-bit or 32-bit system?
  • Endianness: Is it a big-endian or little-endian system
  • Alignment: Whether access according to 4-byte alignment is compulsory
  • Default signedness: The default data type is signed or unsigned
  • NOMMU: Whether to support MMU

The host environment when cross-compiling is different from the target environment

  • Configuration issues:
  • HOSTCC vs TARGETCC:
  • Toolchain Leaks:
  • Libraries:
  • Testing:

2 Cross-compilation chain

2.1 What is a cross-compilation chain?

Now that we understand what cross-compilation is, let's take a look at what a cross-compilation chain is.

First of all, the compilation process is a complex process composed of different sub-functions in sequence, as shown in the figure below:

Insert image description here

Then the compilation process includes functions such as preprocessing, compiling, assembling, and linking. Since there are different sub-functions, each sub-function is implemented by a separate tool, and together they form a complete tool set.

At the same time, the compilation process is a sequential process, which inevitably involves the use order of the tools. Each tool is connected in series according to the sequence relationship, which forms a chain structure.

Therefore, the cross-compilation chain is a complete tool set composed of multiple sub-tools for compiling program code for cross-platform architectures. At the same time, it hides the details of preprocessing, compilation, assembly, linking, etc. When we specify the source file (.c), it will automatically call different subtools according to the compilation process, and automatically generate the final binary program image (.bin ).

Note: Strictly speaking, a cross-compiler just refers to the cross-compiled gcc, but in fact, for convenience, the cross-compiler we often refer to is the cross-toolchain. This article does not distinguish between these two concepts. They both refer to the compilation chain.

2.2 Naming rules for cross-compilation chains

When we use the cross-compilation chain, we often see such names:

arm-none-linux-gnueabi-gcc
arm-cortex_a8-linux-gnueabi-gcc
mips-malta-linux-gnu-gcc

Among them, the corresponding prefix is:

arm-none-linux-gnueabi-
arm-cortex_a8-linux-gnueabi-
mips-malta-linux-gnu-

The naming rules of these cross-compilation chains seem to be general, and there are certain rules:

arch-core-kernel-system
  • arch: Which target platform to use for.
  • core: Which CPU Core is used, such as Cortex
    A8, but the naming of this group seems to be more flexible. In the cross-compilation chain provided by other manufacturers, some are named after the manufacturer’s name, and some are named after the development board, or directly none or cross.
  • kernel: The operating OS, Linux, uclinux, and bare (no OS) have been seen.
  • system: Specifications of library functions and target images selected by the cross-compilation chain, such as gnu, gnueabi, etc. Among them, gnu is equivalent to glibc+oabi; gnueabi is equivalent to glibc+eabi.

3 Included Tools

3.1 Binutils

Binutils is one of the GNU tools, which includes linkers, assemblers, and other tools for object files and archives, and it is a processing and maintenance tool for binary code.

The subroutines included in the Binutils tool are as follows:

  • ld GNU linker the GNU linker.
  • as the GNU assembler.
  • addr2line converts addresses into file names and line numbers
  • ar A utility for creating, modifying and extracting from archives.
  • c++filt Filter to demangle encoded C++ symbols.
  • dlltool Creates files for building and using DLLs.
  • gold A new, faster, ELF only linker, still in beta test.
  • gprof Displays profiling information.
  • nlmconv Converts object code into an NLM.
  • nm Lists symbols from object files.
  • objcopy Copys and translates object files.
  • objdump Displays information from object files.
  • ranlib Generates an index to the contents of an archive.
  • readelf Displays information from any ELF format object file.
  • size Lists the section sizes of an object or archive file.
  • strings Lists printable strings from files.
  • strip Discards symbols

3.2 GCC
GNU compiler suite supports C, C++, Java, Ada, Fortran, Objective-C and many other languages.

3.3 GLibc

The C function library commonly used on Linux is glibc. Glibc is the lowest-level API in the Linux system, and almost any other running library depends on glibc. In addition to encapsulating the system services provided by the Linux operating system, glibc itself also provides the implementation of many other necessary functional services.

Because the resources of the embedded environment are extremely tight, in addition to glibc, there are now uClibc and eglibc to choose from.

Guess you like

Origin blog.csdn.net/qq_41483419/article/details/132302741