执行目标文件引发的问题:syntax error: word unexpected


执行目标文件引发的问题:syntax error: word unexpected
 
今天不小心把一个目标文件当成了可执行文件放到开发板上进行执行,结果出现了这样一个问题:./hello_qt: line 1: syntax error: word unexpected (expecting ")"),因为以前没有碰到过这事,一时间有点蒙,就是一个简单的hello world按道理不会有问题才对。于是google了一下,原来是一个小小的-c编译选项搞得鬼。顺带也扩展学习总结了一下。
 
arm和pc上执行目标文件的区别
一般来说,gcc -c选项编译出来的目标文件是不可执行的,因此也就不会遇到这种问题,尤其是在PC上就更是如此。我这边是因为把文件转windows工作台,再通过tftp下载的开发板上,然后文件就全部是普通文件,都是自己chmod +x改的可执行,一时大意才难得碰上这问题。
 
PC上执行目标文件的错误提示
1
~/test$ ./zh_display.o
2
-bash: ./zh_display.o: cannot execute binary file
ARM上执行交叉编译目标文件的错误提示
1
$ ./hello_qt
2
./hello_qt: line 1: syntax error: word unexpected (expecting ")")
PC上的提示信息一看就懂,而ARM上的就会让人莫名奇妙了。一开始我怀疑是自己代码有问题,还反复检查了一遍,幸好只是一个简单的hello world程序,不然够我郁闷的。也多亏我及时google,否则还不知道要浪费多少时间在这小问题上,有时候google真的很重要呀!!
区分目标文件和可执行文件
目标文件和可执行文件平时都是很容易区分的,所以一般也不会注意这个。不过从今天的问题上,我又学到了不少关于两者差别的东西,还有区分两者的Linux工具。
 
file工具:查看文件的基本属性信息
1
~/test$ file hello_qt
2
hello_qt: ELF 32-bit LSB executable, ARM, version 1, dynamically linked (uses shared libs), not stripped
3
 
4
~/test$ file hello_qt.o
5
hello_qt.o: ELF 32-bit LSB relocatable, ARM, version 1, not stripped
两者均是ELF文件,但是目标文件是:relocatable, 而可执行文件是: executable。
 
readelf工具:查看ELF文件的详细信息
01
~/test$ readelf -h hello_qt
02
ELF Header:
03
  Magic:   7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00
04
  Class:                             ELF32
05
  Data:                              2's complement, little endian
06
  Version:                           1 (current)
07
  OS/ABI:                            ARM
08
  ABI Version:                       0
09
  Type:                              EXEC (Executable file)
10
  Machine:                           ARM
11
  Version:                           0x1
12
  Entry point address:               0x87f8
13
  Start of program headers:          52 (bytes into file)
14
  Start of section headers:          3948 (bytes into file)
15
  Flags:                             0x202, has entry point, GNU EABI, software FP
16
  Size of this header:               52 (bytes)
17
  Size of program headers:           32 (bytes)
18
  Number of program headers:         6
19
  Size of section headers:           40 (bytes)
20
  Number of section headers:         27
21
  Section header string table index: 24
22
 
23
~/test$ readelf -h hello_qt.o
24
ELF Header:
25
  Magic:   7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00
26
  Class:                             ELF32
27
  Data:                              2's complement, little endian
28
  Version:                           1 (current)
29
  OS/ABI:                            ARM
30
  ABI Version:                       0
31
  Type:                              REL (Relocatable file)
32
  Machine:                           ARM
33
  Version:                           0x1
34
  Entry point address:               0x0
35
  Start of program headers:          0 (bytes into file)
36
  Start of section headers:          1040 (bytes into file)
37
  Flags:                             0x200, GNU EABI, software FP
38
  Size of this header:               52 (bytes)
39
  Size of program headers:           0 (bytes)
40
  Number of program headers:         0
41
  Size of section headers:           40 (bytes)
42
  Number of section headers:         16
43
  Section header string table index: 13
-h选项读取ELF文件的文件头信息,注意其中的两项值:Type 和 Entry point address。Type信息就是file中的文件类型,而 Entry point address表示文件的执行入口点,只有可执行文件该项才有值,而目标文件是可重定向文件,还不可以直接执行,因此该项值为0.
目标文件两者为:
 
1
Type:                              REL (Relocatable file)
2
Entry point address:               0x0
而可执行文件两者为:
1
Type:                              EXEC (Executable file)
2
Entry point address:               0x87f8
 

相关内容

    暂无相关文章