今天不小心把一个目标文件当成了可执行文件放到开发板上进行执行,结果出现了这样一个问题:./hello_qt: line 1: syntax error: word unexpected (expecting ")"),因为以前没有碰到过这事,一时间有点蒙,就是一个简单的hello world按道理不会有问题才对。于是google了一下,原来是一个小小的-c编译选项搞得鬼。顺带也扩展学习总结了一下。

arm和pc上执行目标文件的区别

一般来说,gcc -c选项编译出来的目标文件是不可执行的,因此也就不会遇到这种问题,尤其是在PC上就更是如此。我这边是因为把文件转windows工作台,再通过tftp下载的开发板上,然后文件就全部是普通文件,都是自己chmod +x改的可执行,一时大意才难得碰上这问题。

  • PC上执行目标文件的错误提示
1
2
~/test$ ./zh_display.o
-bash: ./zh_display.o: cannot execute binary file
  • ARM上执行交叉编译目标文件的错误提示
1
2
$ ./hello_qt
./hello_qt: line 1: syntax error: word unexpected (expecting ")")

PC上的提示信息一看就懂,而ARM上的就会让人莫名奇妙了。一开始我怀疑是自己代码有问题,还反复检查了一遍,幸好只是一个简单的hello world程序,不然够我郁闷的。也多亏我及时google,否则还不知道要浪费多少时间在这小问题上,有时候google真的很重要呀!!

区分目标文件和可执行文件

目标文件和可执行文件平时都是很容易区分的,所以一般也不会注意这个。不过从今天的问题上,我又学到了不少关于两者差别的东西,还有区分两者的Linux工具。

  • file工具:查看文件的基本属性信息
1
2
3
4
5
~/test$ file hello_qt
hello_qt: ELF 32-bit LSB executable, ARM, version 1, dynamically linked (uses shared libs), not stripped
 
~/test$ file hello_qt.o
hello_qt.o: ELF 32-bit LSB relocatable, ARM, version 1, not stripped

两者均是ELF文件,但是目标文件是:relocatable, 而可执行文件是: executable

  • readelf工具:查看ELF文件的详细信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
~/test$ readelf -h hello_qt
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:               0x87f8
  Start of program headers:          52 (bytes into file)
  Start of section headers:          3948 (bytes into file)
  Flags:                             0x202, has entry point, GNU EABI, software FP
  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:         27
  Section header string table index: 24
 
~/test$ readelf -h hello_qt.o
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:                              REL (Relocatable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          1040 (bytes into file)
  Flags:                             0x200, GNU EABI, software FP
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         16
  Section header string table index: 13

-h选项读取ELF文件的文件头信息,注意其中的两项值:Type 和 Entry point address。Type信息就是file中的文件类型,而 Entry point address表示文件的执行入口点,只有可执行文件该项才有值,而目标文件是可重定向文件,还不可以直接执行,因此该项值为0.

目标文件两者为:

1
2
Type:                              REL (Relocatable file)
Entry point address:               0x0

而可执行文件两者为:

1
2
Type:                              EXEC (Executable file)
Entry point address:               0x87f8

最新文章

  1. PHP中PDO事务的使用方法
  2. maven 快照
  3. php计算字符串长度
  4. 辛巴学院-Unity-剑英陪你零基础学c#系列(一)Hello World
  5. [iOS] Delphi for iOS 版本需求
  6. MMORPG大型游戏设计与开发(客户端架构 part7 of vegine)
  7. Codeforces 611C New Year and Domino(二维前缀和)
  8. Kivy A to Z -- 怎样从python代码中直接訪问Android的Service
  9. JavaScript实现遮罩层
  10. Codeforces 571B Minimization
  11. expect实现ssh自动登录
  12. C编译: makefile基础
  13. const与readonly的区别
  14. 将WebApi Host到控制台和IIS
  15. 关于使用mybatis的几点总结
  16. 【smart-transform】取自 Atom 的 babeljs/coffeescript/typescript 智能转 es5 库
  17. codeforge免费下载账号 积分账号 共享账号
  18. SpringMVC 框架系列之初识与入门实例
  19. Saltstack_使用指南07_远程执行-执行模块
  20. ASP.NET页面之间传值的方式之Server.Transfer(个人整理)

热门文章

  1. 网络编程中的CAP & 有趣的存储框架(关系型、NoSQL)全图
  2. Java学习需要掌握的一些知识
  3. hdoj 1518 Square 【dfs】
  4. Spring+EhCache缓存实例(具体解说+源代码下载)
  5. zoj3822 Domination 概率dp --- 2014 ACM-ICPC Asia Mudanjiang Regional Contest
  6. spring注入对象类型的属性
  7. 找新朋友(hdoj--1286--欧拉函数)
  8. cxf调用WebService
  9. org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的systemPropertiesModeName属性
  10. 登录linux,输入ls显示anaconda-ks.cfg cobbler.ks ....., 原因在于root@ ~ / 区别