据我所知,当你编译一个文件时,比如一个.cpp文件,它应该把它转换成二进制文件,以便机器理解它。但是,在编译文件并获取.out文件之后,我发现里面是十六进制代码而不是二进制代码。可以请有人向我解释我错过了什么吗?
答案 0 :(得分:0)
Sublime Text会自动将文件检测为二进制图像,并使用十六进制查看器显示该文件。
答案 1 :(得分:0)
也许你可能会喜欢一个积极的'回答,如在'我如何检查作为构建的一部分的目标文件?或者我的构建结果?'
恕我直言,评论(包括我自己的评论)很难说你使用了编辑器,在某些方面,这是一个错误的工具,来检查二进制可执行文件或.o文件。也许是更正确的'工具是objdump。然而,它有一些令人沮丧的选择。不过,你应该尝试一下。
另一方面,如果你想看到一个真正的十六进制转储,linux有一个名为hexdump的工具。可以与编辑器生成的内容进行比较吗?
来自维基百科:
" objdump是用于显示有关目标文件的各种信息的程序。例如,它可以用作反汇编程序以汇编形式查看可执行文件。它是GNU Binutils的一部分,用于对可执行文件和其他二进制数据进行细粒度控制。"
示例:(可链接的.o文件的开头)
objdump -x -T eng_format.o
eng_format.o: file format elf64-x86-64
eng_format.o
architecture: i386:x86-64, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x0000000000000000
Sections:
Idx Name Size VMA LMA File off Algn
0 .group 00000008 0000000000000000 0000000000000000 00000040 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
1 .group 00000008 0000000000000000 0000000000000000 00000048 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
2 .group 00000008 0000000000000000 0000000000000000 00000050 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
3 .group 00000008 0000000000000000 0000000000000000 00000058 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
4 .group 00000008 0000000000000000 0000000000000000 00000060 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
5 .group 00000008 0000000000000000 0000000000000000 00000068 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
6 .group 00000008 0000000000000000 0000000000000000 00000070 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
7 .group 00000008 0000000000000000 0000000000000000 00000078 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
8 .group 00000008 0000000000000000 0000000000000000 00000080 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
9 .group 00000008 0000000000000000 0000000000000000 00000088 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
10 .group 00000008 0000000000000000 0000000000000000 00000090 2**2
CONTENTS, READONLY, EXCLUDE, GROUP, LINK_ONCE_DISCARD
11 .text 00000d85 0000000000000000 0000000000000000 00000098 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
12 .data 00000000 0000000000000000 0000000000000000 00000e1d 2**0
CONTENTS, ALLOC, LOAD, DATA
13 .bss 00000002 0000000000000000 0000000000000000 00000e1d 2**0
ALLOC
14 .rodata 000001f8 0000000000000000 0000000000000000 00000e20 2**5
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
15 .text._ZStanSt13_Ios_FmtflagsS_ 00000014 0000000000000000 0000000000000000 00001018 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
16 .text._ZStorSt13_Ios_FmtflagsS_ 00000014 0000000000000000 0000000000000000 0000102c 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
17 .text._ZStcoSt13_Ios_Fmtflags 0000000e 0000000000000000 0000000000000000 00001040 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
18 .text._ZStoRRSt13_Ios_FmtflagsS_ 0000002f 0000000000000000 0000000000000000 0000104e 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
19 .text._ZStaNRSt13_Ios_FmtflagsS_ 0000002f 0000000000000000 0000000000000000 0000107d 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
20 .text._ZNSt8ios_base4setfESt13_Ios_FmtflagsS0_ 00000062 0000000000000000 0000000000000000 000010ac 2**1
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
21 .text._ZSt5fixedRSt8ios_base 00000028 0000000000000000 0000000000000000 0000110e 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
22 .text._ZSt12setprecisioni 0000000c 0000000000000000 0000000000000000 00001136 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
23 .text._ZSt10fpclassifyd 0000006b 0000000000000000 0000000000000000 00001142 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
24 .gcc_except_table 000000b9 0000000000000000 0000000000000000 000011ad 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
25
.text._ZSt4moveIRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEONSt16remove_referenceIT_E4typeEOS8_
0000000e 0000000000000000 000000000000000 0 00001266 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
26 .text._ZStplIcSt11char_traitsIcESaIcEENSt7__cxx1112basic_stringIT_T0_T1_EEOS8_PKS5_ 00000047 0000000000000000 0000000000000000 00001274 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
27 .debug_info 00007f49 0000000000000000 0000000000000000 000012bb 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
28 .debug_abbrev 00000765 0000000000000000 0000000000000000 00009204 2**0
CONTENTS, READONLY, DEBUGGING
29 .debug_aranges 000000e0 0000000000000000 0000000000000000 00009969 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
30 .debug_ranges 00000130 0000000000000000 0000000000000000 00009a49 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
31 .debug_line 00000742 0000000000000000 0000000000000000 00009b79 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
32 .debug_str 0000b297 0000000000000000 0000000000000000 0000a2bb 2**0
CONTENTS, READONLY, DEBUGGING
33 .comment 00000034 0000000000000000 0000000000000000 00015552 2**0
CONTENTS, READONLY
34 .note.GNU-stack 00000000 0000000000000000 0000000000000000 00015586 2**0
CONTENTS, READONLY
35 .eh_frame 00000388 0000000000000000 0000000000000000 00015588 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
objdump: eng_format.o: not a dynamic object
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 eng_format.cpp
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .rodata 0000000000000000 .rodata
0000000000000000 l O .rodata 0000000000000001 _ZStL19piecewise_construct
0000000000000001 l O .rodata 0000000000000001 _ZL13eng_increment
0000000000000002 l O .rodata 0000000000000001 _ZL13eng_decrement
0000000000000000 l d .text._ZStanSt13_Ios_FmtflagsS_ 0000000000000000 .text._ZStanSt13_Ios_FmtflagsS_
0000000000000000 l d .text._ZStorSt13_Ios_FmtflagsS_ 0000000000000000 .text._ZStorSt13_Ios_FmtflagsS_
0000000000000000 l d .text._ZStcoSt13_Ios_Fmtflags 0000000000000000 .text._ZStcoSt13_Ios_Fmtflags
0000000000000000 l d .text._ZStoRRSt13_Ios_FmtflagsS_ 0000000000000000 .text._ZStoRRSt13_Ios_FmtflagsS_
0000000000000000 l d .text._ZStaNRSt13_Ios_FmtflagsS_ 0000000000000000 .text._ZStaNRSt13_Ios_FmtflagsS_
0000000000000000 l d .text._ZNSt8ios_base4setfESt13_Ios_FmtflagsS0_ 0000000000000000 .text._ZNSt8ios_base4setfESt13_Ios_FmtflagsS0_
0000000000000000 l d .text._ZSt5fixedRSt8ios_base 0000000000000000 .text._ZSt5fixedRSt8ios_base
0000000000000003 l O .rodata 0000000000000001 _ZStL13allocator_arg
0000000000000004 l O .rodata 0000000000000001 _ZStL6ignore
0000000000000000 l d .text._ZSt12setprecisioni 0000000000000000 .text._ZSt12setprecisioni
0000000000000000 l d .text._ZSt10fpclassifyd 0000000000000000 .text._ZSt10fpclassifyd
0000000000000080 l O .rodata 0000000000000120 _ZN12_GLOBAL__N_1L8prefixesE
...总计2300行。你真的想要"搜索一下。
示例:(第1 100行elf可执行文件)
objdump -x -T FMssb_ut
FMssb_ut: file format elf64-x86-64
FMssb_ut
architecture: i386:x86-64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000000402b10
Program Header:
PHDR off 0x0000000000000040 vaddr 0x0000000000400040 paddr 0x0000000000400040 align 2**3
filesz 0x00000000000001f8 memsz 0x00000000000001f8 flags r-x
INTERP off 0x0000000000000238 vaddr 0x0000000000400238 paddr 0x0000000000400238 align 2**0
filesz 0x000000000000001c memsz 0x000000000000001c flags r--
LOAD off 0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**21
filesz 0x00000000000109a8 memsz 0x00000000000109a8 flags r-x
LOAD off 0x0000000000010db0 vaddr 0x0000000000610db0 paddr 0x0000000000610db0 align 2**21
filesz 0x00000000000004e8 memsz 0x0000000000000928 flags rw-
DYNAMIC off 0x0000000000010dd8 vaddr 0x0000000000610dd8 paddr 0x0000000000610dd8 align 2**3
filesz 0x0000000000000220 memsz 0x0000000000000220 flags rw-
NOTE off 0x0000000000000254 vaddr 0x0000000000400254 paddr 0x0000000000400254 align 2**2
filesz 0x0000000000000044 memsz 0x0000000000000044 flags r--
EH_FRAME off 0x000000000000ee48 vaddr 0x000000000040ee48 paddr 0x000000000040ee48 align 2**2
filesz 0x000000000000022c memsz 0x000000000000022c flags r--
STACK off 0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4
filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
RELRO off 0x0000000000010db0 vaddr 0x0000000000610db0 paddr 0x0000000000610db0 align 2**0
filesz 0x0000000000000250 memsz 0x0000000000000250 flags r--
Dynamic Section:
NEEDED librt.so.1
NEEDED libstdc++.so.6
NEEDED libm.so.6
NEEDED libgcc_s.so.1
NEEDED libpthread.so.0
NEEDED libc.so.6
INIT 0x0000000000402358
FINI 0x000000000040e604
INIT_ARRAY 0x0000000000610db0
INIT_ARRAYSZ 0x0000000000000018
FINI_ARRAY 0x0000000000610dc8
FINI_ARRAYSZ 0x0000000000000008
GNU_HASH 0x0000000000400298
STRTAB 0x0000000000400bd0
SYMTAB 0x0000000000400300
STRSZ 0x0000000000000d54
SYMENT 0x0000000000000018
DEBUG 0x0000000000000000
PLTGOT 0x0000000000611000
PLTRELSZ 0x0000000000000750
PLTREL 0x0000000000000007
JMPREL 0x0000000000401c08
RELA 0x0000000000401b30
RELASZ 0x00000000000000d8
RELAENT 0x0000000000000018
VERNEED 0x00000000004019e0
VERNEEDNUM 0x0000000000000006
VERSYM 0x0000000000401924
Version References:
required from librt.so.1:
0x09691a75 0x00 15 GLIBC_2.2.5
required from libgcc_s.so.1:
0x0b792650 0x00 14 GCC_3.0
required from libc.so.6:
0x06969194 0x00 16 GLIBC_2.14
0x0d696914 0x00 12 GLIBC_2.4
0x09691a75 0x00 07 GLIBC_2.2.5
required from libpthread.so.0:
0x09691a75 0x00 05 GLIBC_2.2.5
required from libstdc++.so.6:
0x056bafd3 0x00 13 CXXABI_1.3
0x02297f89 0x00 11 GLIBCXX_3.4.9
0x0297f868 0x00 10 GLIBCXX_3.4.18
0x0bafd179 0x00 09 CXXABI_1.3.9
0x0297f861 0x00 08 GLIBCXX_3.4.11
0x0297f870 0x00 06 GLIBCXX_3.4.20
0x08922974 0x00 04 GLIBCXX_3.4
0x0297f871 0x00 03 GLIBCXX_3.4.21
required from libm.so.6:
0x09691a75 0x00 02 GLIBC_2.2.5
Sections:
Idx Name Size VMA LMA File off Algn
0 .interp 0000001c 0000000000400238 0000000000400238 00000238 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
1 .note.ABI-tag 00000020 0000000000400254 0000000000400254 00000254 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .note.gnu.build-id 00000024 0000000000400274 0000000000400274 00000274 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
3 .gnu.hash 00000064 0000000000400298 0000000000400298 00000298 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .dynsym 000008d0 0000000000400300 0000000000400300 00000300 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .dynstr 00000d54 0000000000400bd0 0000000000400bd0 00000bd0 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
6 .gnu.version 000000bc 0000000000401924 0000000000401924 00001924 2**1
CONTENTS, ALLOC, LOAD, READONLY, DATA
7 .gnu.version_r 00000150 00000000004019e0 00000000004019e0 000019e0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
...前100行500
hexdump的一个例子:
hexdump FMssb_ut
0000000 457f 464c 0102 0001 0000 0000 0000 0000
0000010 0002 003e 0001 0000 2b10 0040 0000 0000
0000020 0040 0000 0000 0000 3bb0 000c 0000 0000
0000030 0000 0000 0040 0038 0009 0040 0026 0023
0000040 0006 0000 0005 0000 0040 0000 0000 0000
0000050 0040 0040 0000 0000 0040 0040 0000 0000
0000060 01f8 0000 0000 0000 01f8 0000 0000 0000
0000070 0008 0000 0000 0000 0003 0000 0004 0000
0000080 0238 0000 0000 0000 0238 0040 0000 0000
0000090 0238 0040 0000 0000 001c 0000 0000 0000
00000a0 001c 0000 0000 0000 0001 0000 0000 0000
00000b0 0001 0000 0005 0000 0000 0000 0000 0000
00000c0 0000 0040 0000 0000 0000 0040 0000 0000
00000d0 09a8 0001 0000 0000 09a8 0001 0000 0000
00000e0 0000 0020 0000 0000 0001 0000 0006 0000
00000f0 0db0 0001 0000 0000 0db0 0061 0000 0000
0000100 0db0 0061 0000 0000 04e8 0000 0000 0000
0000110 0928 0000 0000 0000 0000 0020 0000 0000
0000120 0002 0000 0006 0000 0dd8 0001 0000 0000
0000130 0dd8 0061 0000 0000 0dd8 0061 0000 0000
0000140 0220 0000 0000 0000 0220 0000 0000 0000
0000150 0008 0000 0000 0000 0004 0000 0004 0000
0000160 0254 0000 0000 0000 0254 0040 0000 0000
0000170 0254 0040 0000 0000 0044 0000 0000 0000
0000180 0044 0000 0000 0000 0004 0000 0000 0000
0000190 e550 6474 0004 0000 ee48 0000 0000 0000
00001a0 ee48 0040 0000 0000 ee48 0040 0000 0000
00001b0 022c 0000 0000 0000 022c 0000 0000 0000
00001c0 0004 0000 0000 0000 e551 6474 0006 0000
00001d0 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 0000 0000 0010 0000 0000 0000
0000200 e552 6474 0004 0000 0db0 0001 0000 0000
0000210 0db0 0061 0000 0000 0db0 0061 0000 0000
0000220 0250 0000 0000 0000 0250 0000 0000 0000
0000230 0001 0000 0000 0000 6c2f 6269 3436 6c2f
0000240 2d64 696c 756e 2d78 3878 2d36 3436 732e
0000250 2e6f 0032 0004 0000 0010 0000 0001 0000
0000260 4e47 0055 0000 0000 0002 0000 0006 0000
0000270 0020 0000 0004 0000 0014 0000 0003 0000
0000280 4e47 0055 6242 90f3 5153 ecd4 c394 9df4
0000290 5ffe e03c cd6c 546e 0003 0000 0050 0000
00002a0 0002 0000 0007 0000 2c01 4410 a124 0201
00002b0 1400 0024 0d00 298c 0050 0000 0052 0000
00002c0 0055 0000 6afc 2380 6d25 430c bc72 3a06
00002d0 956a 2db4 f12d 5e5f 1bcc feea 200c feea
00002e0 4528 4cd5 178a feea 9814 430c 1c9e b870
....
... 50,000行文本文件的开头。
注意:某些编辑器的二进制文件模式可能有更多信息。在emacs中,我曾经将objdump连接到一个功能键,emacs可以在另一个缓冲区中显示objdump。
另一方面,我很少调查可执行内容。大约十年前,“嵌入式世界”#39;工具套件仍然需要开发人员创建链接映射作为输入,因此调查这种方式很有用。我在过去十年中没有做过这种调查,可能是因为新的嵌入式系统有更多的问题。
还有其他工具(超越objdump)。谷歌可以帮助找到它们。