GNU 交叉工具链的介绍与使用

3.1 常用工具介绍

 

名称

归属

作用

arm­linux­as

binutils

编译 ARM 汇编程序

arm­linux­ar

binutils

把多个.o 合并成一个.o 或静态库(.a)

arm­linux­ranlib

binutils

为库文件建立索引,相当于 arm­linux­ar ­s

arm­linux­ld

binutils

连接器(Linker), 把多个.o 或库文件连接成一个可执行文件

 

名称

归属

作用

arm­linux­objdump

binutils

查看目标文件(.o)和库(.a)的信息

arm­linux­objcopy

binutils

转换可执行文件的格式

arm­linux­strip

binutils

去掉 elf 可执行文件的信息. 使可执行文件变小

arm­linux­readelf

binutils

读 elf 可执行文件的信息

arm­linux­gcc

gcc

编译.c 或.S 开头的 C 程序或汇编程序

arm­linux­g++

gcc

编译 c++程序

 

3.2 主要工具的使用

3.2.1  arm-linux-gcc 的使用

1. 编译 C 文件,生成 elf 可执行文件 h1.c 源文件

#include <stdio.h> void hellofirst(void)

{

printf("The first hello! \n");

}

 

h2.c 源文件

#include <stdio.h> void hellosecond(void)

{

printf("The second hello! \n");

}

 

hello.c 源文件

#include  <stdio.h> void hellosecond(void); void hellofirst(void);

 

int main(int argc, char *argv[])

{

hellofirst(); hellosecond(); return(0);

}

编译以上 3 个文件,有如下几种方法:

方法 1:

[arm@localhost gcc]#arm­linux­gcc ­c h1.c [arm@localhost gcc]#arm­linux­gcc ­c h2.c

[arm@localhost gcc]#arm­linux­gcc ­o hello hello.c h1.o h2.o

方法 2:

[arm@localhost gcc]#arm­linux­gcc ­c h1.c h2.c [arm@localhost gcc]#arm­linux­gcc ­o hello hello.c h1.o h2.o 方法 3:

[arm@localhost gcc]#arm­linux­gcc ­c ­o h1.o h1.c

 

[arm@localhost gcc]#arm­linux­gcc ­c ­o h1.o h1.c [arm@localhost gcc]#arm­linux­gcc ­o hello hello.c h1.o h2.o 方法 4:

[arm@localhost gcc]#arm­linux­gcc ­o hello hello.c h1.c h2.c

­c: 只编译不连接。

­o: 编译且连接。

 

2. 产生一个预处理文件 当要看一个宏在源文件中产生的结果时,比较合适。

[arm@localhost gcc]#arm­linux­gcc ­E h1.i h1.c

­E: 产生一个预处理文件.

 

3. 产生一个动态库

动态库是在运行时需要的库。

[arm@localhost gcc]#arm­linux­gcc ­c ­fpic h1.c h2.c [arm@localhost gcc]#arm­linux­gcc ­shared h1.o h2.o ­o hello.so [arm@localhost gcc]#arm­linux­gcc ­o hello hello.c hello.so

把 hello.so 拷贝到目标板的/lib 目录下,把可执行文件拷贝目标板的/tmp 目录下,在目标板上运行 hello.

#/tmp/hello

或把 hello.so 和 hello 一起拷贝到/tmp 目标下,并设置 LD_LIBRARY_PATH 环境变量

#export LD_LIBRARY_PATH =/tmp:$LD_LIBRARY_PATH

#/tmp/hello

 

3.2.2  arm-linux-ar 和 arm-linux-ranlib 的使用

静态库是在编译时需要的库。

1. 建立一个静态库

[arm@localhost gcc]#arm­linux­ar ­r libhello.a h1.o h2.o

 

2. 为静态库建立索引

[arm@localhost gcc]#arm­linux­ar ­s libhello.a [arm@localhost gcc]#arm­linux­ranlib libhello.a

 

3. 由静态库产生可执行文件

[arm@localhost gcc]#arm­linux­gcc ­o hello hello.c ­lhello ­L./ [arm@localhost gcc]#arm­linux­gcc ­o hello hello.c libhello.a hello 文件可以直接拷贝到/tmp 目录下运行,不需 libhello.a.

 

 

3.2.3   arm-linux-objdump 的使用

1. 查看静态库或.o 文件的组成文件

[arm@localhost gcc]$ arm­linux­objdump ­a libhello.a

 

2. 查看静态库或.o 文件的络组成部分的头部分 [arm@localhost gcc]$ arm­linux­objdump ­h libhello.a

 

3. 把目标文件代码反汇编

[arm@localhost gcc]$ arm­linux­objdump ­d libhello.a

 

3.2.4   arm-linux-readelf 的使用

1. 读 elf 文件开始的文件头部

 

[arm@localhost gcc]$ arm­linux­readelf ­h hello ELF Header:

Magic:   7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00

Class:                                         ELF32

Data:                                         2's complement, little endian

Version:                                    1 (current)

OS/ABI:                                    ARM

ABI Version:                            0

Type:                                         EXEC (Executable file)

Machine:                                   ARM

Version:                                     0x1

Entry point address:                0x82b4

Start of program headers:      52 (bytes into file) Start of section headers:                                    10240 (bytes into file) Flags:  0x2, has entry point

Size of this header:                 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 6

Size of section headers:          40 (bytes) Number of section headers:                        28

Section header string table index: 25

 

2. 读 elf 文件中所有 ELF 的头部: [arm@localhost gcc]#arm­linux­readelf ­e hello

......

 

3. 显示整个文件的符号表

[arm@localhost gcc]#arm­linux­readelf ­s hello

......

 

4. 显示使用的动态库

[arm@localhost gcc]#arm­linux­readelf ­d hello

......

 

3.2.5  arm-linux-strip 的使用 1. 移除所有的符号信息 [arm@localhost gcc]#cp hello hello1

[arm@localhost gcc]#arm­linux­strip ­strip­all hello

­­strip­all: 是移除所有符号信息

[arm@localhost gcc]#ll

­rwxr­xr­x  1 arm root  2856  7 月  3 15:14 hello

­rwxr­xr­x  1 arm root 13682  7 月  3 15:13 hello1

被 strip 后的 hello 程序比原来的 hello1 程序要小很多。

 

2. 移除调试符号信息

[arm@localhost gcc]#arm­linux­strip ­g hello [arm@localhost gcc]#ll

­rwxr­xr­x  1 arm root  4501  7 月  3 15:17 hello

­rwxr­xr­x  1 arm root 13682  7 月  3 15:13 hello1

 

3.2.6  arm-linux-copydump  的使用

生成可以执行的 2 进制代码

[arm@localhost gcc]#arm­linux­copydump ­O binary hello hello.bin

posted @ 2019-06-26 10:58  聂忠乐  阅读(366)  评论(0编辑  收藏  举报