Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1354898
  • 博文数量: 1334
  • 博客积分: 645
  • 博客等级: 上士
  • 技术积分: 5762
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-25 16:56
文章分类

全部博文(1334)

文章存档

2014年(108)

2013年(1059)

2012年(169)

分类: LINUX

2013-01-05 16:34:53

3 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
void hellofirst(void)
{
printf(“The first hello! \n”);
}
h2.c 源文件
#include
void hellosecond(void)
{
printf(“The second hello! \n”);
}
hello.c 源文件
#include
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-obj dump 的使用
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:                               0×1
Entry point address:                   0x82b4
Start of program headers:              52 (bytes into file)
Start of section headers:              10240 (bytes into file)
Flags:                                 0×2, 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

阅读(858) | 评论(0) | 转发(1) |
0

上一篇:ARM协处理器

下一篇:arm elf 文件 反汇编

给主人留下些什么吧!~~