Step by step create crosstoolchain based on ARM + Linux

Source: Internet
Author: User
Step by step, create a crosstoolchain based on ARM + Linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. Disclaimer: This article is based on the reference of online materials, and is based on Liang yuanen's "How to Build a cross-compiling environment for Embedded Development. I have explained in detail the problems I encountered during the establishment process. This article will be updated as I learn.

1 Introduction

Due to the limited storage capacity of embedded development systems, a cross-compiling environment for the target machine is usually required on a PC before Linux is customized and used in embedded systems, that is, various binary tool assemblies are used as tool chains, including GNU linker (ld), GNU assembler (as), and ar (generate modifications and unzipping an archive file) C compiler (gcc) and C link library (glibc ). This document describes how to establish a tool chain for cross-platform development on the target arm in Linux.

2 Basic Concepts

2.1 What is cross-compilation?

In short, cross-compilation is the code generated on one platform and executed on another platform. The platform here includes Architecture and Operating System (OS ). The same architecture can run different operating systems, and the same operating system can run on different architectures. For example, the x86 Linux platform is a general term of the Intel x86 architecture and the Linux for x86 operating system.

2.2 why do I use cross-compilation?

There are two reasons. First, cross-compilation is required when the bootloader and OS core required by the target platform are not yet established. Second, the target machine does not have certain processor capabilities and storage space, that is, the program development cannot be completed on the target board separately, so you have to ask for the host machine. In this way, the application program to be run on the target machine can be compiled on the host machine to generate the code format that can be run on the target machine, and then transplanted to the target board, that is, the Host/Target mode developed by embedded programs.

2.3 i386 Comprehension

I386 and i686 refer to the CPU type. In terms of linux kernel design, i386 is the architecture, and i386 is the CPU architecture of i386, so many linux designs are based on i386. Put simply, when i386 is put together with ppc, alpha, and arm, it refers to the architecture. If it is put together with i586 and i686, it refers to the processor model. One is horizontal and the other is vertical.

3. creation process

3.1 selected software version

To select an appropriate version to ensure the availability of the tool chain, you must find a combination suitable for the host and target board. You can test these versions on your own, or search for version combinations that have been tested on the Internet, that is, binutils, gcc, and glibc versions. I used the host machine for the redhat-9.0, the target machine arm, the selected version is as follows:

--------------------------------------------------------------------------------

Binutils-2.11.2.tar.gz contains ld, ar, as, and some other tools for generating or processing binary files.

The gcc-core-2.95.3.tar.gz contains the main part of GCC.

Gcc-g000000002.95.3.tar.gz enables GCC to compile C ++ programs.

Glibc-2.2.4.tar.gz libc is a library that many user-layer applications need to use, that is, C-Linked Library.

The glibc-linuxthreads-2.2.4.tar.gz libc is used to support Posix Threads for separate release of compressed packages.

The kernel of linux-2.4.21.tar.gz + rmk1 Linux and the patch package supporting ARM.

--------------------------------------------------------------------------------

You can select an updated version, and use the older version in turn when the compilation fails. Immediately discover that the new version combination can be compiled successfully, and you still need to test whether the created tool chain can be used.

You can. The download paths of the selected versions are shown below.

--------------------------------------------------------------------------------

Binutils-2.11.2.tar.gz

Ftp://ftp.gnu.org/gnu/binutils/binutils-2.11.2.tar.gz

Gcc-core-2.95.3.tar.gz

Ftp://ftp.gnu.org/gnu/gcc/gcc-2.95.3/gcc-core-2.95.3.tar.gz

Gcc-g000000002.95.3.tar.gz

Ftp://ftp.gnu.org/gnu/gcc/gcc-2.95.3/gcc-g++-2.95.3.tar.gz

Glibc-2.2.4.tar.gz

Ftp://ftp.gnu.org/gnu/glibc/glibc-2.2.4.tar.gz

Glibc-linuxthreads-2.2.4.tar.gz

Ftp://ftp.gnu.org/gnu/glibc/glibc-linuxthreads-2.2.4.tar.gz

Linux-2.4.21.tar.gz + rmk1

Ftp://ftp.kernle.org/pub/linux/kernel/v2.4/linux-2.4.21.tar.gz

Ftp://ftp.arm.linux.org.uk/pub/linux/arm/kernel/v2.4/patch-2.4.21-rmk1.gz

--------------------------------------------------------------------------------

3.2 Create a working directory

My user name is lqm, so all the work is established under/home/lqm.

**************************************** ********************

$ Cd/home/lqm go to the working directory

$ Pwd: view the current directory

/Home/lqm

$ Mkdir embedded-system: Create a toolchain folder

$ Ls view all files created by/home/lqm

Embedded-system

**************************************** ********************

The top-level folder embedded-system has been created. The following directories are created under this folder:

--------------------------------------------------------------------------------

Setup-dir stores the downloaded compressed package

Src-dir stores the unzipped source files of binutils, gcc, and glibc.

The kernel file is stored in the kernel, and kernel configuration and compilation are also completed here

Build-dir: Compile the source files under src-dir. This is the best practice of separating the source and compiled directories recommended by GNU.

Tool-chain Cross-compilation tool chain installation location

Program

Doc instructions and script files

--------------------------------------------------------------------------------

Create a directory and download the source file.

**************************************** ********************

$ Pwd

/Home/lqm/

$ Cd embedded-system

$ Mkdir setup-dir src-dir kernel build-dir tool-chain program doc

$ Ls

Build-dir doc kernel program setup-dir src-dir tool-chain

$ Cd setup-dir

$ Wgetftp: // ftp.gnu.org/gnu/binutils/binutils-2.11.2.tar.gz download the source file

$ Wgetftp: // ftp.gnu.org/gnu/gcc/gcc-2.95.3/gcc-core-2.95.3.tar.gz

$ Wgetftp: // ftp.gnu.org/gnu/gcc/gcc-2.95.3/gcc-g00000000-2.95.3.tar.gz

$ Wgetftp: // ftp.gnu.org/gnu/glibc/glibc-2.2.4.tar.gz

$ Wgetftp: // ftp.gnu.org/gnu/glibc/glibc-linuxthreads-2.2.4.tar.gz

$ Wgetftp: // ftp.kernel.org/pub/linux/kernel/v2.4/linux-2.4.21.tar.gz

$ Wgetftp: // ftp.arm.linux.org. uk/pub/linux/arm/kernel/v2.4/patch-2.4.21-rmk1.gz

$ Ls

Binutils-2.11.2.tar.gz gcc-g00000000-2.95.3.tar.gz glibc-linuxthreads-2.2.4.tar.gz

Patch-2.4.21-rmk1.gz gcc-core-2.95.3.tar.gz glibc-2.2.4.tar.gz linux-2.4.21.tar.gz

$ Cd ../build-dir

$ Mkdir build-binutils build-gcc build-glibc build the compilation directory

**************************************** ********************

3.3 output Environment Variables

When creating and using some tool programs, these directory paths may be used. If you design a short Command Script and set appropriate environment variables, the operation can be simplified. The following is a Command Script hjbl:

**************************************** ********************

$ Pwd

/Home/lqm/embedded-system/build-dir

$ Cd ../doc

$ Mkdir scripts

$ Cd scripts

$ Emacs hjbl use the text editor emacs to compile the environment variable script

--------------------------------------------------------------------------------

Enter the following content in the edit emacs window that will be opened later (if you are using the command line interface, you must use the vi text editor, but not emacs ):

Export PRJROOT =/home/lqm/embedded-system

Export TARGET = arm-linux

Export PREFIX = $ PRJROOT/tool-chain

Export TARGET_PREFIX = $ PREFIX/$ TARGET

Export PATH = $ PREFIX/bin: $ PATH

Save and close the emacs window. If you want to execute this script in the current window to make the environment variable take effect, you also need to execute the following statement:

--------------------------------------------------------------------------------

$. Hjbl (Note: There is a space between the vertex and hjbl)

$ Cd $ PRJROOT verify whether the environment variables take effect

$ Ls

Build-dir doc kernel program setup-dir src-dir tool-chain

**************************************** ********************

This environment variable takes effect only in the current Terminal window. If you close the window and open a new window, the environment variable is effective and you need to execute the following command again:

$./Home/lqm/embedded-system/doc/scripts/hjbl

Note:

The TARGET variable is used to define the type of the TARGET Board. In the future, the tool chain will be established based on the type of the TARGET board. See table 1. The TARGET board definition has nothing to do with the host type, but if you change the TARGET value, the GNU toolchain must be re-established once.

The PREFIX variable provides pointers to the directory where the target board tool program will be installed.

The TARGET_PREFIX variable points to the directory to which the header file and link library related to the target board will be installed.

The PATH variable points to the directory where the binary file (Executable File) will be installed.

Table 1 TARGET variable value
Actual TARGET variable value
[Table = 200] [tr] [td] PowerPC [/td] [td] powerpc-linux
[/Td] [/tr] [tr] [td] ARM
[/Td] [td] arm-linux
[/Td] [/tr] [tr] [td] MIPS (bigendian)
[/Td] [td] mips-linux
[/Td] [/tr] [tr] [td] MIPS (littleendian)
[/Td] [td] mipsel-linux
[/Td] [/tr] [tr] [td] SuperH 4
[/Td] [td] sh4-linux [/td] [/tr] [/table]

3.4 kernel header file configuration

The configuration of the kernel header file is the first step to establish a tool chain. It is similar to other steps to be executed. Most of the steps are as follows:

1. decompress the package

2. Configure packages for cross-platform development

3. Create a package

4. Installation Package

**************************************** ********************

$ Pwd

/Home/lqm/embedded-system/

$ Cd kernel

$ Tar xvzf ../setup-dir/linux-2.4.21.tar.gz Extract

$ Gunzip ../setup-dir/patch-2.4.21-rmk1.gz

$ Linux-2.4.21 cd

$ Patch? P1 <.../../setup-dir/patch-2.4.21-rmk1 patch the Linux Kernel

$ Make ARCH = arm CROSS_COMPILE = arm-linux-menuconfig Configuration

$ Make dep

--------------------------------------------------------------------------------

The values of the ARCH and CROSS_COMPILE variables are related to the architecture type of the target board. If you use the PPC target board, ARCH = ppc CROSS_COMPILE = ppc-linux -. If i386 target board is used, ARCH = i386 CROSS_COMPILE = i386-linux -.

Make menuconfig is configured in text menu mode.

Make xconfig is configured on the GUI.

Make config is configured in plain text mode.

Select make menuconfig. Note that the correct hardware type is selected in System Types. After the configuration is complete, exit and save, check whether the kernel/linux-2.4.21/include/linux/version. h and autoconf. h files in the kernel directory are generated, which is needed to compile glibc. The existence of version. h and autoconf. h files indicates that you have generated the correct header file.

Then, create the include directory required by the toolchain and copy the kernel header file.

--------------------------------------------------------------------------------

$ Cd include

$ Ln-s asm-arm asm # can be viewed and automatically generated after compilation. If a connection has been generated, do not write

$ Cd asm

$ Ln-s arch-epxa arch # Same as above

$ Ln-s proc-armv proc # Same as above

# These are modifications made to the makefile file.

$ Mkdir? P $ TARGET_PREFIX/include

$ Cp? R $ PRJROOT/kernel/linux-2.4.21/include/linux $ TARGET_PREFIX/include

$ Cp? R $ PRJROOT/kernrl/linux-2.4.21/include/asm-arm $ TARGET_PREFIX/include/asm

**************************************** ********************

Note:

1. You don't have to reset the tool chain every time you reset the Kernel configuration, unless you change the type of the processor or system. The toolchain only requires a set of valid header files available for the target board. These header files have been provided in the previous program.

2. When the asm-linux folder is placed in the target folder $ TARGET_PREFIX/include/, you must change the name to asm because the include Inclusion Method of the configuration file is the same. This is also the difference between cross-compilation. Otherwise, an error message similar to the following will appear:

--------------------------------------------------------------------------------

.........

Done

_ Udivsi3

_ Divsi3

_ Umodsi3

_ Modsi3

_ Dwmd_lnx

Libgcc1.s: 438: asm/unistd. h: No such file or directory

Make [1] *** [libgcc1-asm.a] error 1

--------------------------------------------------------------------------------

3.5 binutils (Binary tool program) settings

Tools in the binutils package are often used to operate binary objects. The two most important tools in this package are the GNU assembler as and the linker ld.

**************************************** ********************

$ Cd $ PRJROOT/src-dir

$ Tar xvzf ../setup-dir/binutils-2.11.2.tar.gz

$ Cd $ PRJROOT/build-dir/build-binutils

$.../Src-dir/binutils-2.11.2/configure -- target = $ TARGET -- prefix = $ PREFIX

$ Make

$ Make install

$ Ls $ PREFIX/bin verify that the installation result is correct

Arm-linux-addr2line arm-linux-ld arm-linux-readelf

Arm-linux-ar arm-linux-nm arm-linux-size

Arm-linux-as arm-linux-objcopy arm-linux-strings

Arm-linux-c ++ filt arm-linux-objdump arm-linux-strip

Arm-linux-gasp arm-linux-ranlib

**************************************** ********************

Note: The prefix of each tool's file name is the value previously set for the TARGET variable. If the target board is a i386-linux, the file name prefix for these tools will be i386-linux -. In this way, you can find the correct tool program based on the target board type.

3.6 initial compiler Creation

At the beginning, only C-language-supported boot compilers can be established, because the C-Linked Library (glibc) is not supported. After glibc is compiled, You Can recompile gcc and provide complete C ++ support.

**************************************** ********************

$ Cd $ PRJROOT/setup-dir

$ Mv gcc-core-2.95.3.tar.gz gcc-2.95.3.tar.gz # rename

$ Cd $ PRJROOT/src-dir

$ Tar xvzf ../setup-dir/gcc-2.95.3.tar.gz

$ Cd $ PRJROOT/build-dir/build-gcc

$.../Src-dir/gcc-2.95.3/configure -- target = $ TARGET -- prefix = $ PREFIX -- without-headers
-- Enable-languages ages = c

--------------------------------------------------------------------------------

Because it is a cross compiler and does not need the system header file of the target board, you need to use the -- without-headers option. -- Enable-language = c is used to tell the language supported by the compiler to be generated for script configuration. Currently, only c is supported. -- Disable-threads: threads requires glibc support.

Prepare the Makefile file and modify the src-dir/gcc-2.95.3/gcc/config/arm/t-linux file before compiling. Add two definitions in TARGET_LIBGCC2_CFLAGS: -Dinhibit_libc-D _ gthr_posix_h; otherwise, an error is returned.

--------------------------------------------------------------------------------

$ Make

$ Make install

**************************************** ********************

3.7 create a C library (glibc)

This step is the most tedious process. The target board must be used to execute or develop most applications. Glibc kit is often called a C link library, but glibc actually produces many link libraries, one of which is the C Link Library libc. Due to the limitations of the embedded system, the standard gnu c link library is too large to be applied on the target board. Therefore, we need to find a replacement for the C-Linked Library, such as uClibc. Here, the standard gnu c is used as an example to establish a tool chain.

**************************************** ********************

$ Cd $ PRJROOT/src-dir

$ Tar xvzf ../setup-dir/glibc-2.2.4.tar.gz

$ Tar xvzf ../setup-dir/glibc-linuxthreads-2.2.4.tar.gz -- directory = glibc-2.2.4

$ Cd $ PRJROOT/build-dir/build-glibc

$ CC = arm-linux-gcc.../src-dir/glibc-2.2.4/configure -- host = $ TARGET -- prefix = "/usr"
-- Enable-add-ons -- with-headers = $ TARGET_PREFIX/include

$ Make

$ Make install_root = $ TARGET_PREFIX prefix = "install

--------------------------------------------------------------------------------

The install_root variable is set here to point to the directory currently to be installed by the link library component. This allows the Linked Library and its header files to be installed in the directory related to the target board specified through TARGET_PREFIX, rather than the/usr directory of the system. Because the -- prefix option was used to set the value of the prefix variable, and the value of the prefix will be appended to the value of install_root and become the installation directory of the link library component, therefore, you need to reset the prefix value. In this way, all glibc components will be installed in the directory specified by $ TARGET_PREFIX.

--------------------------------------------------------------------------------

$ Cd $ TARGET_PREFIX/lib

$ Cp./libc. so./libc. so. orig

--------------------------------------------------------------------------------

Edit the libc. so file and change it as follows:

/* GNU ld script

Use the shared library, but some functions are only in

The static library, so try that secondarily .*/

GROUP (libc. so.6 libc_nonshared.a)

--------------------------------------------------------------------------------

**************************************** ********************

3.8 complete compiler settings

Now you can install a complete compiler that supports C and C ++ for the target board. This step is simpler than the previous build process.

**************************************** ********************

$ Cd $ PRJROOT/build-dir/build-gcc

$.../Src-dir/gcc-2.95.3/configure -- target = $ TARGET -- prefix = $ PREFIX
-- Enable-languages ages = c, c ++

$ Make all

$ Make install

**************************************** ********************

3.9 complete tool chain settings

**************************************** ********************

$ Cd $ TARGET_PREFIX/bin

$ File as ar gcc ld nm ranlib strip check whether the file is a binary file

$ Arm-linux-gcc-print-search-dirs: view the default search path

$ Mv as ar gcc ld nm ranlib strip $ PREFIX/lib/gcc-lib/arm-linux/2.95.3 transfer files

$ For file in as ar gcc ld nm ranlib strip

> Do

> Ln-s $ PREFIX/lib/gcc-lib/arm-linux/2.95.3/$ file

> Done

**************************************** ********************

3.10 use tool chain

Write a simple C program and use the established tool chain. ,

**************************************** ********************

$ Cd $ PRJROOT/program

$ Emacs hello. c

--------------------------------------------------------------------------------

Write in the text editor emacs:

# Include

Int main ()
{
Int I;
For (I = 1; I <9; I ++)
Printf ("Hello World % d times! \ N ", I );
}

Save and exit
--------------------------------------------------------------------------------

$ Gcc-g hello. c-o hello

$ Gdb

(Gdb) file hello

(Gdb) l

# Include
Int main ()
{
Int I;
For (I = 1; I <9; I ++)
Printf ("Hello World % d times! \ N ", I );
}

(Gdb) r

(Gdb) q

$ Arm-linux-gcc-g hello. c-o hello-linux

$ File hello-linux

Hello-linux: ELF 32-bit LSB executable, ARM, version 1 (ARM), for GNU/Linux 2.0.0, dynamically linked (uses shared libs), not stripped

**************************************** ********************

The above output indicates that you have compiled a hello-linux system that can run in the arm architecture, proving that your compilation tool has been successful.

4. Summary

Through the above operations, we have been able to establish a full-featured cross-platform development tool chain, which will be frequently used in future embedded development.

Note: ----- Is the description text.

* ** Source code
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.