Cross Compilation Techniques for Embedded Systems Based on Buildroot

1. Introduction

Nowadays, IOT device security research needs to address products with different architectures. Given that compiling for different architectures is a skill that practitioners need to master. The mainstream device architectures today mainly include x86_64, arm, and mips. The compilation for different instruction sets and how to handle errors is also the topic discussed in this article.

1.1 Tools

Buildroot is a simple, efficient, and easy-to-use tool for generating embedded Linux systems through cross-compilation.Download link: https://buildroot.org/download.html

1.2 Interface

Configure the cross-compilation tool settings (makemenuconfig), as shown in Figure 1-1 configuration interface

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-1 configuration interface2.3 Option Functions

Target Options device architecture (as shown in Figure 1-2 device architecture)

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-2 device architectureBuild Options compilation options (as shown in Figure 1-3 compilation options)Mostly set in libraries (both static and shared), static and dynamic compilation

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-3 compilation optionsToolchain compilation toolchain (as shown in Figure 1-4 compilation toolchain)Primarily settings for gcc version, kernel version, wide character, and C++ configuration options

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-4 compilation toolchainSystem configuration system configuration (as shown in Figure 1-5 system configuration) Mainly configuration needed for building the file system, default options

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-5 system configurationLinux Kernel kernel configuration (as shown in Figure 1-6 kernel configuration) Mainly configuration for the system kernel, default options

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-6 kernel configurationTarget Packages program dependency libraries (as shown in Figure 1-7 program dependency libraries) Mainly configuration for compiling program dependency libraries

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-7 program dependency librariesFilesystem image file system imageBootloaders BootbootHost utilities local toolsLegacy config Options Legacy configuration The above four do not need specific configuration and will not be elaborated on.

1.3 Compilation

Select target options and save after specifying the architectureYou cannot use make-jN, because Buildroot does not support top-level parallel make. Instead, use BR2_JLEVEL option to tell Buildroot to run the compilation of each package using make -JN.Make BR_JLEVEL=4 V=sAfter compilation, the generated cross-compilation toolchain is mainly stored in the output/host/bin directory (as shown in Figure 1-8 cross-compilation toolchain)

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 1-8 cross-compilation toolchain

2. Cross Compilation Practical Test

In actual work, the focus is mainly on compiling static programs (this example focuses on MIPS, ARM is similar)

2.1 Hello World

Hello.c

#include <stdio.h> int main(){printf(“Hello World!\n”);return 0;}

Cross compilation (as shown in Figure 2-1 hello)

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 2-1 hello

2.2 NetCat

Download link: https://github.com/mirror/busybox.gitMake menuconfig to save the generated configuration file!!!Note that all compilation information exists in the Makefile fileThe tool supports CROSS_COMPILE and does not require separate modification of compilation optionsMakefile (as shown in Figure 2-2 Makefile file)

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 2-2 Makefile fileCompilation command (as shown in Figure 2-3 compilation command)

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 2-3 compilation commandView compilation files (as shown in Figure 2-4 Busybox_MIPS)

Cross Compilation Techniques for Embedded Systems Based on Buildroot

Figure 2-4 Busybox_MIPS

2.3 Cleverly Obtaining Compilation Information

Some products have many dependencies during the compilation process, so so files may lead to not knowing which dependencies are required. You can print detailed gcc compilation information to obtain the parameters passed to the make build process for the gcc compiler.Golang

package main import ( “fmt” “os” “os/exec” “strings”) func CheckExists(path string) bool { _, err := os.Stat(path) if err == nil { return true } if os.IsNotExist(err) { return false } return false} func main() { var file *os.File var err error name := “/tmp/cmd.txt” cmds := os.Args fmt.Println(“print gcc args:”, cmds) // Print the parameters content to the file if CheckExists(name) { file, err = os.OpenFile(name, os.O_APPEND|os.O_WRONLY, os.ModePerm) if err != nil { fmt.Println(err.Error()) return } defer file.Close() } else { file, err = os.Create(name) if err != nil { fmt.Println(err.Error()) } } file.WriteString(strings.Join(cmds, ” “)) // Keep the original logic of gcc unchanged cmd := exec.Command(“gcc”, cmds[1:]…) cmd.Stdout = os.Stdout // Normal output cmd.Stderr = os.Stderr // Error output err = cmd.Run() if err != nil { fmt.Println(err.Error()) return } out, err := cmd.Output() if err != nil { fmt.Println(err.Error()) return } fmt.Println(string(out))} # Compilego build main.go -o gcc# Replace the current gcc with this program command, to intercept the compilation information

3. Conclusion

The knowledge points involved in cross compilation are numerous. This article serves only as an introductory guide, giving everyone a basic understanding of the concept of cross compilation.

Leave a Comment