Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of linux cross-compilation

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

The main content of this article is to explain "what is the principle of linux cross-compilation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the principle of linux cross-compilation"?

In linux, cross-compilation refers to generating executable code on one platform on another, that is, the platform on which the source code is compiled and the platform on which the compiled program is executed are two different platforms. The reasons for using cross-compilation are: 1, the target system does not have the ability to compile locally on it; 2, the platform that has the ability to compile source code is different from the target platform.

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Cross compilation

The so-called "Cross_Compile" means that the platform on which the source code is compiled and the platform on which the compiled program is executed are two different platforms. For example, under the Intel x86 architecture / Linux (Ubuntu) platform, the executable generated using the cross-compilation tool chain runs under the ARM architecture / Linux.

Simply put, it is to generate executable code on one platform on another. The same architecture can run different operating systems; similarly, the same operating system can run on different architectures.

Cross-compilation is relatively complex, and the following issues must be considered:

CPU architecture: such as ARM,x86,MIPS, etc.

Byte order: big end (big-endian) and small end (little-endian)

Support for floating point numbers

Application binary interface (Application Binary Interface,ABI)

Why use cross-compilation? There are two main reasons:

The target system of cross-compilation is generally small memory, poor display device or even no, and does not have the ability to compile locally on it.

The CPU architecture or operating system of the platform with the ability to compile source code is different from the target platform

Cross-compilation tool chain is an indispensable tool for cross-compilation, and it is a skill that embedded developers must master skillfully.

Why is cross-compilation so difficult?

Portable native compilation is difficult.

Most programs are developed on x86 hardware and compiled locally. Cross-compilation will encounter two types of problems: the problem of the program itself and the problem of building the system.

The first category of problems affects all non-x86 targets, including native and cross-builds. Most programs make assumptions about the type of machine they are running and must match the relevant platform, otherwise the program will not run. Common assumptions include:

Word size-copying pointers to int may lose data on 64-bit platforms and determine the size of the malloc by multiplying it by 4 instead of sizeof (long). Integer overflows lead to minor security vulnerabilities, ala "if (x < size) memset (src+x,0,y);", which produces a 4-GB memset yellow0xFFFFFFFFFFFFF0 on 32-bit hardware.

Endianness-different systems store binary data internally in different ways, and reading int or float data from disk or network may require conversion.

Alignment-some platforms, such as arm, can only read or write integers from an even multiple of 4 bytes, otherwise a segment error occurs. For any alignment processing, data that is not alignment is slow, and the compiler usually populates the structured alignment variable. Thinking of a structure as a block of data that can be sent to disk or over the network requires additional work to ensure a consistent representation.

The default signature-the "char" data type, which defaults to signed or unsigned, varies from platform to platform (from compiler to compiler), resulting in some very surprising errors. A simple solution is to provide a compiler parameter, such as "- funsigned-char", to force the default to a known value.

NOMMU-if the target platform does not have a memory management unit, there are several things that need to be changed. Vfork () is required, not fork (), only some types of mmap () work (shared or read-only, but cannot be replicated on write), and the stack does not grow dynamically.

The goal of most packages is to be portable at local compile time, and at least accept patches to fix any of the above problems submitted to the appropriate development mailing list (with the exception of NOMMU issues).

Then there is cross-compilation.

In addition to native compilation problems, cross-compilation has a series of problems of its own:

Configuration issues-packages with separate configuration steps (the ". / configure" section of the standard configure/make/make install) are usually tested for things such as byte order or page size and are portable at native compilation time. When cross-compiling, these values differ between the host system and the target system, and the test is run on the host system to give the wrong answer. When the target does not have the package or the version is not compatible, the configuration detects whether there is package support on the host.

HOSTCC vs TARGETCC-the build process requires compilation to run on the host system, such as the configuration test above, or the program that generates the code (such as the C program that creates the .h file, # included during the main build). Replace the host compiler with the target compiler, destroying the runtime during the build process. Such a library requires access to the host and target compiler and requires instructions on when to use it.

Tool chain leakage-an improperly configured cross-compilation tool chain that leaks some contents of the host system into compiled programs, resulting in faults that are usually easy to detect but difficult to diagnose and correct. The toolchain may # include the wrong header file, or search for the wrong library path when linking. Shared libraries usually rely on other shared libraries and may be referenced when sneaking into unexpected links to host systems.

Libraries-dynamically linked programs must access the appropriate shared libraries at compile time. The shared library of the target system needs to be added to the cross-compilation tool chain so that the program can link to.

Testing-based on our organization, the development system provides a convenient testing environment. When cross-compiling, confirming that the "hello world" is built successfully may require (at least) configuring the boot loader, kernel, root file system, and shared libraries.

Footnote 1: the most significant difference between computer types is the processor that executes the program. Other differences include libraries ABI (such as glibc and uClibc) machines with configurable byte order (arm and armeb) or machines in different modes that can run 32-bit and 64-bit code (such as x86-64 on x86).

Footnote 2: when building compilers, the third type is called "Canadian Cross", a cross compiler that does not run on the host system. Canada cross-builds a compiler that runs on one target platform and generates code on another target machine. First, create a temporary cross-compiler from the host to the first target, and build another cross-compiler as the second target to build such an external compiler. The first cross-compiler aims to be the host running the new compiler, and the second goal is the platform on which the new compiler generates output. This technique is often used to cross-compile new native compilers for the target platform.

Footnote 3: modern desktop systems are fast enough to simulate targets compiled locally under the simulator, which is actually a viable strategy. Much slower than cross-compilation, you need to build an environment for target lookups or cost institutions (cross-compilers must be set up anyway) and may crash due to differences between the simulator and the real hardware to be deployed.

Footnote 4: the cross-compilation tool chain tends to prefix the name of its utility, ala "armv5l-linux-gcc". If you simply call it "gcc", the host and native compilers cannot be in $PATH at the same time.

At this point, I believe you have a deeper understanding of "what is the principle of linux cross-compilation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report