您的位置:首页 > 运维架构 > Linux

arm-linux-gcc交叉工具链的安装和使用

2016-03-10 14:41 627 查看
转自:/article/7785144.html

1、安装arm-linux-gcc交叉工具链

[root@localhost Denny]# ls
arm-linux-gcc-4.3.2.tgz Desktop gcc kernel modules shell实验 smb.conf tftp安装包 wireshark软件包

at_remind.c file gdb makefiles samba安装包 smb test wireless

[root@localhost Denny]# tar zxvf arm-linux-gcc-4.3.2.tgz-C / // -C 参数指的是解压到根目录下面

[root@localhost /]# cd /usr/local/

[root@localhost local]# ls

arm bin etc games include lib libexec sbin share src

[root@localhost local]# cd arm/4.3.2/

arm-none-linux-gnueabi/ bin/ lib/ libexec/ share/

[root@localhost local]# cd arm/4.3.2/bin/ // 安装在 /usr/local/arm/4.3.2/bin/ 的“bin”目录下面

[root@localhost bin]#

[root@localhost bin]# /usr/local/arm/4.3.2/bin/arm-linux-gcc // arm-linux-gcc 使用方法1:跟上“全路径”

[[root@localhost bin]# echo $PATH

/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

[root@localhost bin]# export $PATH

bash: export: `/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin': not a valid identifier

[root@localhost bin]# vi /etc/pro

profile profile.d/ protocols

[root@localhost bin]# vi /etc/profile // arm-linux-gcc 使用方法2:添加路径到环境变量中去 ,在系统的时候就可以“任何地方”使用 arm-linux-gcc

[root@localhost bin]#

# Path manipulation

if [ "$EUID" = "0" ]; then

pathmunge /sbin

pathmunge /usr/sbin

pathmunge /usr/local/sbin

pathmunge /usr/local/arm/4.3.2/bin //环境变量添加的位置

fi

[root@localhost file]# cat hello.c

#include <stdio.h>

int main()

{

char *p="hello world!!";

printf("%s:",*p); // 错误:字符串的输出 printf("%s:",p); 不用加*p(字符串指针),和其他指针不同

return 0;

}

[root@localhost file]# vi hello.c

[root@localhost file]# gcc hello.c -o hello // x86编译方式

[root@localhost file]# ./hello // 在x86上能够运行

hello world!!:[root@localhost file]# arm-linux-gc

arm-linux-gcc arm-linux-gcc-4.3.2 arm-linux-gcov

[root@localhost file]# arm-linux-gcc hello.c -o hello1 // ARM编译方式

[root@localhost file]# ls

fork hello hello1 hello.c lib_file sys_file time_file

[root@localhost file]# ./hello1 // 在x86上不能够运行

bash: ./hello1: cannot execute binary file

[root@localhost file]#

2、arm-linux-gcc交叉编译工具的使用

今晚用了:arm-linux-objdump -S -D hello 反汇编指令时,遇到以下提示的错误:

arm-linux-objdump: Can't disassemble for architecture UNKNOWN!

现在还不知道是什么错误引起的,等着明天再解决了!!!!!!!

原因是:由于上面用了“gcc hello.c -o hello x86编译方式” 和 “arm-linux-gcc hello.c -o hello1 ARM编译方式”

产生了hello 和 hello1 对应不同平台的文件,当然用“arm-linux-objdump -S -D hello”用arm反汇编指令对x86平台产生的bin文件进行反汇编,

肯定出现错误

(1)、反汇编arm-linux-objdump 使用

[root@localhost file]arm-linux-objdump -S -D hello

arm-linux-objdump: Can't disassemble for architecture UNKNOWN!

[root@localhost file]# ls

fork hello hello1 hello.c lib_file sys_file time_file // hello 是x86编译出来的,hello1是arm编译出来的

[root@localhost file]# arm-linux-gcc -g hello.c -o hello1 // -g编译产生带有调试的信息的文件(反汇编后:c语言才能和汇编语言对应上)

[root@localhost file]# arm-linux-objdump -D -S hello1 >log

[root@localhost file]# ls

fork hello hello1 hello.c lib_file log sys_file time_file

[root@localhost file]# vi log // 查看对应的文件如下

00008380 <main>:

#include <stdio.h>

int main()

{

8380: e92d4800 push {fp, lr}

8384: e28db004 add fp, sp, #4 ; 0x4

8388: e24dd008 sub sp, sp, #8 ; 0x8

char *p="hello world!!";

838c: e59f3020 ldr r3, [pc, #32] ; 83b4 <main+0x34>

8390: e50b3008 str r3, [fp, #-8]

printf("%s:",p);

8394: e59f001c ldr r0, [pc, #28] ; 83b8 <main+0x38>

8398: e51b1008 ldr r1, [fp, #-8]

839c: ebffffc7 bl 82c0 <_init+0x48>

return 0;

83a0: e3a03000 mov r3, #0 ; 0x0

}

(2)arm-linux-readelf 文件查看工具

[root@localhost file]# arm-linux-readelf -a hello1 >log

ELF Header: //主要查看这个头文件

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

Class: ELF32

Data: 2's complement, little endian //采用的是小端模式(程序运行不起来,硬件平台是打断格式,交叉工具链是小端格式)

Version: 1 (current)

OS/ABI: UNIX - System V

ABI Version: 0

Type: EXEC (Executable file)

Machine: ARM //程序运行在ARM平台上

Version: 0x1

Entry point address: 0x82cc

Start of program headers: 52 (bytes into file)

Start of section headers: 2488 (bytes into file)

Flags: 0x5000002, has entry point, Version5 EABI

Size of this header: 52 (bytes)

Size of program headers: 32 (bytes)

Number of program headers: 8

Size of section headers: 40 (bytes)

Number of section headers: 37

Section header string table index: 34

[root@localhost file]# arm-linux-readelf -d hello1 // -d 指的是查看程序所用的共享链接库

Dynamic section at offset 0x470 contains 24 entries:

Tag Type Name/Value

0x00000001 (NEEDED) Shared library: [libc.so.6] // 程序所用到的共享链接库(程序运行不起来,可能找不到共享链接库,把所需要的库考到路径下)

0x0000000c (INIT) 0x8278

0x0000000d (FINI) 0x8434

0x00000019 (INIT_ARRAY) 0x10464

0x0000001b (INIT_ARRAYSZ) 4 (bytes)

0x0000001a (FINI_ARRAY) 0x10468

0x0000001c (FINI_ARRAYSZ) 4 (bytes)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: