}
2)编译如下:
[quietheart@lv-kbak]$g++-cmyfile.cpp
[quietheart@lv-kbak]$g++-shared-fPCI-olibmy.somyfile.o
[quietheart@lv-kbak]$ar-rlibmy.amyfile.o
ar:
creatinglibmy.a
3)编译之后,查看生成的文件:
[quietheart@lv-kbak]$ls-l
总计44
-rw-r--r--1quietheartquietheart215407-0816:
14libmy.a
-rwxr-xr-x1quietheartquietheart570707-0816:
08libmy.so
-rwxr-xr-x1quietheartquietheart11707-0816:
06myfile.cpp
-rwxr-xr-x1quietheartquietheart6307-0816:
08myfile.h
-rw-r--r--1quietheartquietheart200407-0816:
08myfile.o
libmy.alibmy.somyfile.cppmyfile.hmyfile.o
这里,分别生成目标文件myfile.o,共享库文件libmy.so,和静态库文件libmy.a。
基于以上可执行文件和库,这里给出一些常用的命令。
*读取可执行文件形式的elf文件头信息:
[quietheart@lv-kcppDemo]$readelf-hmain
输入之后,输出信息如下:
ELFHeader:
Magic:
7f454c46010101000000000000000000
Class:
ELF32
Data:
2'scomplement,littleendian
Version:
1(current)
OS/ABI:
UNIX-SystemV
ABIVersion:
0
Type:
EXEC(Executablefile)
Machine:
Intel80386
Version:
0x1
Entrypointaddress:
0x8048580
Startofprogramheaders:
52(bytesintofile)
Startofsectionheaders:
3232(bytesintofile)
Flags:
0x0
Sizeofthisheader:
52(bytes)
Sizeofprogramheaders:
32(bytes)
Numberofprogramheaders:
8
Sizeofsectionheaders:
40(bytes)
Numberofsectionheaders:
29
Sectionheaderstringtableindex:
26
这里,可见可执行文件的elf文件,其类型为EXEC(可执行文件)。
另外,含调试信息的"main.debug"和不含调试信息的"main"除了一些大小信息之外,其内容是一样的。
并且由此可见文件的体系结构为Intel80386。
*读取目标文件形式的elf文件头信息:
[quietheart@lv-kbak]$readelf-hmyfile.o
输入之后,输出信息大致如下:
ELFHeader:
Magic:
7f454c46010101000000000000000000
Class:
ELF32
Data:
2'scomplement,littleendian
Version:
1(current)
OS/ABI:
UNIX-SystemV
ABIVersion:
0
Type:
REL(Relocatablefile)
Machine:
Intel80386
Version:
0x1
Entrypointaddress:
0x0
Startofprogramheaders:
0(bytesintofile)
Startofsectionheaders:
516(bytesintofile)
Flags:
0x0
Sizeofthisheader:
52(bytes)
Sizeofprogramheaders:
0(bytes)
Numberofprogramheaders:
0
Sizeofsectionheaders:
40(bytes)
Numberofsectionheaders:
15
Sectionheaderstringtableindex:
12
这里,可见目标文件的elf文件,其类型为REL(可重定位文件)。
*读取静态库文件形式的elf文件头信息:
[quietheart@lv-kbak]$readelf-hlibmy.a
输入之后,输出信息大致如下:
File:
libmy.a(myfile.o)
ELFHeader:
Magic:
7f454c46010101000000000000000000
Class:
ELF32
Data:
2'scomplement,littleendian
Version:
1(current)
OS/ABI:
UNIX-SystemV
ABIVersion:
0
Type:
REL(Relocatablefile)
Machine:
Intel80386
Version:
0x1
Entrypointaddress:
0x0
Startofprogramheaders:
0(bytesintofile)
Startofsectionheaders:
516(bytesintofile)
Flags:
0x0
Sizeofthisheader:
52(bytes)
Sizeofprogramheaders:
0(bytes)
Numberofprogramheaders:
0
Sizeofsectionheaders:
40(bytes)
Numberofsectionheaders:
15
Sectionheaderstringtableindex:
12
这里,可见静态库文件的elf文件,其类型为REL(可重定位文件)。
*读取动态库文件形式的elf文件头信息:
[quietheart@lv-kbak]$readelf-hlibmy.so
ELFHeader:
Magic:
7f454c46010101000000000000000000
Class:
ELF32
Data:
2'scomplement,littleendian
Version:
1(current)
OS/ABI:
UNIX-SystemV
ABIVersion:
0
Type:
DYN(Sharedobjectfile)
Machine:
Intel80386
Version:
0x1
Entrypointaddress:
0x550
Startofprogramheaders:
52(bytesintofile)
Startofsectionheaders:
2768(bytesintofile)
Flags:
0x0
Sizeofthisheader:
52(bytes)
Sizeofprogramheaders:
32(bytes)
Numberofprogramheaders:
5
Sizeofsectionheaders:
40(bytes)
Numberofsectionheaders:
27
Sectionheaderstringtableindex:
24
这里,可见动态库文件的elf文件,其类型为DYN(共享目标文件)。
*查看可执行的elf文件程序头表信息:
[quietheart@lv-kcppDemo]$readelf-lmain
输入之后,输出信息如下:
ElffiletypeisEXEC(Executablefile)
Entrypoint0x8048580
Thereare8programheaders,startingatoffset52
ProgramHeaders:
TypeOffsetVirtAddrPhysAddrFileSizMemSizFlgAlign
PHDR0x0000340x080480340x080480340x001000x00100RE0x4
INTERP0x0001340x080481340x080481340x000130x00013R0x1
[Requestingprograminterpreter:
/lib/ld-linux.so.2]
LOAD0x0000000x080480000x080480000x009700x00970RE0x1000
LOAD0x0009700x080499700x080499700x001300x001c8RW0x1000
DYNAMIC0x0009880x080499880x080499880x000e00x000e0RW0x4
NOTE0x0001480x080481480x080481480x000200x00020R0x4
GNU_EH_FRAME0x0008200x080488200x080488200x000440x00044R0x4
GNU_STACK0x0000000x000000000x000000000x000000x00000RW0x4
SectiontoSegmentmapping:
SegmentSections...
00
01.interp
02.interp.note.ABI-tag.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rel.dyn.rel.plt.init.plt.text.fini.rodata.eh_frame_hdr.eh_frame
03.ctors.dtors.jcr.dynamic.got.got.plt.data.bss
04.dynamic
05.note.ABI-tag
06.eh_frame_hdr
07
这里,含调试信息的"main.debug"和不含调试信息的"main"其内容是一样的。
*查看目标文件的elf文件程序头表信息:
[quietheart@lv-kbak]$readelf-lmyfile.o
输入之后,输出信息类似如下:
Therearenoprogramheadersinthisfile.
这里可知,可重定位的目标文件,它没程序头表。
*查看静态库文件的elf文件程序头表信息:
[quietheart@lv-kbak]$readelf-llibmy.a
输入之后,输出信息类似如下:
File:
libmy.a(myfile.o)
Therearenoprogramheadersinthisfile.
这里可知,可重定位的静态库文件,它没程序头表。
*查看动态库文件的elf文件程序头表信息:
[quietheart@lv-kbak]$readelf-llibmy.so
ElffiletypeisDYN(Sharedobjectfile)
Entrypoint0x550
Thereare5programheaders,startingatoffset52
ProgramHeaders:
TypeOffsetVirtAddrPhysAddrFileSizMemSizFlgAlign
LOAD0x0000000x000000000x000000000x007f40x007f4RE0x1000
LOAD0x0007f40x000017f40x000017f40x0011c0x00128RW0x1000
DYNAMIC0x0008100x000018100x000018100x000e00x000e0RW0x4
GNU_EH_FRAME0x0007380x000007380x000007380x0002c0x0002cR0x4
GNU_STACK0x0000000x000000000x000000000x000000x00000RW0x4
SectiontoSegmentmapping:
SegmentSections...
00.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rel.dyn.rel.plt.init.plt.text.fini.rodata.eh_frame_hdr.eh_frame
01.ctors.dtors.jcr.data.rel.ro.dynamic.got.got.plt.bss
02.dynamic
03.eh_frame_hdr
04
这里可知,做为共享目标文件的动态库,它程序头表。
*查看一个可执行的elf文件的节信息:
[quietheart@lv-kcppDemo]$readelf-Smain
输入之后,输出信息如下:
Thereare29sectionheaders,startingatoffset0xca0:
SectionHeaders:
[Nr]NameTypeAddrOffSizeESFlgLkInfAl
[0]NULL0000000000000000000000000
[1].interpPROGBITS0804813400013400001300A001
[2].note.ABI-tagNOTE0804814800014800002000A004
[3].gnu.hashGNU_HASH0804816800016800003004A404
[4].dynsymDYNSYM080481980001980000d010A514
[5].dynstrSTRTAB0804826800026800018300A001
[6].gnu.versionVERSYM080483ec0003ec00001a02A402
[7].gnu.version_rVERNEED080484080004080000600