cout<<"hello"<
}
2)编译如下:
[quietheart@lv-k bak]$ g++ -c myfile.cpp
[quietheart@lv-k bak]$ g++ -shared -fPCI -o libmy.so myfile.o
[quietheart@lv-k bak]$ ar -r libmy.a myfile.o
ar: creating libmy.a
3)编译之后,查看生成的文件:
[quietheart@lv-k bak]$ ls -l
总计 44
-rw-r--r-- 1 quietheart quietheart 2154 07-08 16:14 libmy.a
-rwxr-xr-x 1 quietheart quietheart 5707 07-08 16:08 libmy.so
-rwxr-xr-x 1 quietheart quietheart 117 07-08 16:06 myfile.cpp
-rwxr-xr-x 1 quietheart quietheart 63 07-08 16:08 myfile.h
-rw-r--r-- 1 quietheart quietheart 2004 07-08 16:08 myfile.o
libmy.a libmy.so myfile.cpp myfile.h myfile.o
这里,分别生成目标文件myfile.o,共享库文件libmy.so,和静态库文件libmy.a。
基于以上可执行文件和库,这里给出一些常用的命令。
*读取可执行文件形式的elf文件头信息:
[quietheart@lv-k cppDemo]$ readelf -h main
输入之后,输出信息如下:
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: Intel 80386
Version: 0x1
Entry point address: 0x8048580
Start of program headers: 52 (bytes into file)
Start of section headers: 3232 (bytes into file)
Flags: 0x0
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: 29
Section header string table index: 26
这里,可见可执行文件的elf文件,其类型为EXEC(可执行文件)。另外,含调试信息的"main.debug"和不含调试信息的"main"除了一些大小信息之外,其内容是一样的。并且由此可见文件的体系结构为Intel 80386。
*读取目标文件形式的elf文件头信息:
[quietheart@lv-k bak]$ readelf -h myfile.o
输入之后,输出信息大致如下:
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: REL (Relocatable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 516 (bytes into file)
Flags: 0x0
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: 15
Section header string table index: 12
这里,可见目标文件的elf文件,其类型为REL(可重定位文件)。
*读取静态库文件形式的elf文件头信息:
[quietheart@lv-k bak]$ readelf -h libmy.a
输入之后,输出信息大致如下:
File: libmy.a(myfile.o)
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: REL (Relocatable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 516 (bytes into file)
Flags: 0x0
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: 15
Section header string table index: 12
这里,可见静态库文件的elf文件,其类型为REL(可重定位文件)。
*读取动态库文件形式的elf文件头信息:
[quietheart@lv-k bak]$ readelf -h libmy.so
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: DYN (Shared object file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x550
Start of program headers: 52 (bytes into file)
Start of section headers: 2768 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 5
Size of section headers: 40 (bytes)
Number of section headers: 27
Section header string table index: 24
这里,可见动态库文件的elf文件,其类型为DYN(共享目标文件)。
*查看可执行的elf文件程序头表信息:
[quietheart@lv-k cppDemo]$ readelf -l main
输入之后,输出信息如下:
Elf file type EXEC (Executable file)
Entry point 0x8048580
There are 8 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Fl Align
PHDR 0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4
INTERP 0x000134 0x08048134 0x08048134 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x00970 0x00970 R E 0x1000
LOAD 0x000970 0x08049970 0x08049970 0x00130 0x001c8 RW 0x1000
DYNAMIC 0x000988 0x08049988 0x08049988 0x000e0 0x000e0 RW 0x4
NOTE 0x000148 0x08048148 0x08048148 0x00020 0x00020 R 0x4
GNU_EH_FRAME 0x000820 0x08048820 0x08048820 0x00044 0x00044 R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
Section to Segment mapping:
Segment Sections...
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-k bak]$ readelf -l myfile.o
输入之后,输出信息类似如下:
There are no program headers in this file.
这里可知,可重定位的目标文件,它没程序头表。
*查看静态库文件的elf文件程序头表信息:
[quietheart@lv-k bak]$ readelf -l libmy.a
输入之后,输出信息类似如下:
File: libmy.a(myfile.o)
There are no program headers in this file.
这里可知,可重定位的静态库文件,它没程序头表。
*查看动态库文件的elf文件程序头表信息:
[quietheart@lv-k bak]$ readelf -l libmy.so
Elf file type is DYN (Shared object file)
Entry point 0x550
There are 5 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Fl Align
LOAD 0x000000 0x00000000 0x00000000 0x007f4 0x007f4 R E 0x1000
LOAD 0x0007f4 0x000017f4 0x000017f4 0x0011c 0x00128 RW 0x1000
DYNAMIC 0x000810 0x00001810 0x00001810 0x000e0 0x000e0 RW 0x4
GNU_EH_FRAME 0x000738 0x00000738 0x00000738 0x0002c 0x0002c R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
Section to Segment mapping:
Segment Sections...
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-k cppDemo]$ readelf -S main
输入之后,输出信息如下:
There are 29 section headers, starting at offset 0xca0:
Section Headers:
[Nr] Name Type Addr Off Size ES Fl Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 08048134 000134 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048148 000148 000020 00 A 0 0 4
[ 3] .gnu.hash GNU_HASH 08048168 000168 000030 04 A 4 0 4
[ 4] .dynsym DYNSYM 08048198 000198 0000d0 10 A 5 1 4
[ 5] .dynstr STRTAB 08048268 000268 000183 00 A 0 0 1
[ 6] .gnu.version VERSYM 080483ec 0003ec 00001a 02 A 4 0 2
[ 7] .gnu.version_r VERNEED 08048408 000408 000060 00 A 5 2 4
[ 8] .rel.dyn REL 08048468 000468 000010 08 A 4 0 4
[ 9] .rel.plt REL 08048478 000478 000048 08 A 4 11 4
[10] .init PROGBITS 080484c0 0004c0 000017 00 AX 0 0 4
[11] .plt PROGBITS 080484d8 0004d8 0000a0 04 AX 0 0 4
[12] .text PROGBITS 08048580 000580 000268 00 AX 0 0 16
[13] .fini PROGBITS 080487e8 0007e8 00001c 00 AX 0 0 4
[14] .rodata PROGBITS 08048804 000804 00001a 00 A 0 0 4
[15] .eh_frame_hdr PROGBITS 08048820 000820 000044 00 A 0 0 4
[16] .eh_frame PROGBITS 08048864 000864 00010c 00 A 0 0 4
[17] .ctors PROGBITS 08049970 000970 00000c 00 WA 0 0 4
[18] .dtors PROGBITS 0804997c 00097c 000008 00 WA 0 0 4
[19] .jcr PROGBITS 08049984 000984 000004 00 WA 0 0 4
[20] .dynamic DYNAMIC 08049988 000988 0000e0 08 WA 5 0 4
[21] .got PROGBITS 08049a68 000a68 000004 04 WA 0 0 4
[22] .got.plt PROGBITS 08049a6c 000a6c 000030 04 WA 0 0 4
[23] .data PROGBITS 08049a9c 000a9c 000004 00 WA 0 0 4
[24] .bss NOBITS 08049aa0 000aa0 000098 00 WA 0 0 8
[25] .comment PROGBITS 00000000 000aa0 000114 00 0 0 1
[26] .shstrtab STRTAB 00000000 000bb4 0000e9 00 0 0 1
[27] .symtab SYMTAB 00000000 001128 000510 10 28 53 4
[28] .strtab STRTAB 00000000 001638 0003f4 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
这里,main是可执行文件,不含调试信息。
;>
文章评论(0条评论)
登录后参与讨论