在Linux内核分析和驱动开发课上,有这么一个实验,认真完成起来,其实还是需要很多知识的。
在这里把资料简单整理一下,以便日后查阅。
以下是源码和实验过程:
源程序lkpsinglefoo.c
mylibfoo()
{
int libvar;
printf("variable libvar /t location:0x%x/n", &libvar);
}
#include <fcntl.h>
int globalvar1;
int globalvar2 = 3;
void mylocalfoo()
{
int functionvar;
printf("variable functionvar /t location:0x%x/n", &functionvar);
}
int main()
{
void *localvar1 = (void*)malloc(2048);
printf("variable globalvar1 /t location:0x%x/n", &globalvar1);
printf("variable globalvar2 /t location:0x%x/n", &globalvar2);
printf("variable localvar1 /t location:0x%x/n", &localvar1);
mylibfoo();
mylocalfoo();
while(1);
return(0);
}
#gcc -o lkpmem lkpmem.c liblkpsinglefoo.so
#readelf -S lkpmem
There are 36 section headers, starting at offset 0x1450:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg 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] .note.gnu.build-i NOTE 08048168 000168 000024 00 A 0 0 4
[ 4] .hash HASH 0804818c 00018c 000048 04 A 6 0 4
[ 5] .gnu.hash GNU_HASH 080481d4 0001d4 00003c 04 A 6 0 4
[ 6] .dynsym DYNSYM 08048210 000210 0000d0 10 A 7 1 4
[ 7] .dynstr STRTAB 080482e0 0002e0 0000a7 00 A 0 0 1
[ 8] .gnu.version VERSYM 08048388 000388 00001a 02 A 6 0 2
[ 9] .gnu.version_r VERNEED 080483a4 0003a4 000020 00 A 7 1 4
[10] .rel.dyn REL 080483c4 0003c4 000008 08 A 6 0 4
[11] .rel.plt REL 080483cc 0003cc 000028 08 A 6 13 4
[12] .init PROGBITS 080483f4 0003f4 000030 00 AX 0 0 4
[13] .plt PROGBITS 08048424 000424 000060 04 AX 0 0 4
[14] .text PROGBITS 08048490 000490 0001cc 00 AX 0 0 16
[15] .fini PROGBITS 0804865c 00065c 00001c 00 AX 0 0 4
[16] .rodata PROGBITS 08048678 000678 0000a4 00 A 0 0 4
[17] .eh_frame PROGBITS 0804871c 00071c 000004 00 A 0 0 4
[18] .ctors PROGBITS 08049f04 000f04 000008 00 WA 0 0 4
[19] .dtors PROGBITS 08049f0c 000f0c 000008 00 WA 0 0 4
[20] .jcr PROGBITS 08049f14 000f14 000004 00 WA 0 0 4
[21] .dynamic DYNAMIC 08049f18 000f18 0000d8 08 WA 7 0 4
[22] .got PROGBITS 08049ff0 000ff0 000004 04 WA 0 0 4
[23] .got.plt PROGBITS 08049ff4 000ff4 000020 04 WA 0 0 4
[24] .data PROGBITS 0804a014 001014 00000c 00 WA 0 0 4
[25] .bss NOBITS 0804a020 001020 00000c 00 WA 0 0 4
[26] .comment PROGBITS 00000000 001020 000046 01 MS 0 0 1
[27] .debug_aranges PROGBITS 00000000 001068 000020 00 0 0 8
[28] .debug_pubnames PROGBITS 00000000 001088 000025 00 0 0 1
[29] .debug_info PROGBITS 00000000 0010ad 0000ef 00 0 0 1
[30] .debug_abbrev PROGBITS 00000000 00119c 00005f 00 0 0 1
[31] .debug_line PROGBITS 00000000 0011fb 000082 00 0 0 1
[32] .debug_str PROGBITS 00000000 00127d 000092 01 MS 0 0 1
[33] .shstrtab STRTAB 00000000 00130f 00013e 00 0 0 1
[34] .symtab SYMTAB 00000000 0019f0 0004d0 10 35 52 4
[35] .strtab STRTAB 00000000 001ec0 000241 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)
# readelf -x 24 lkpmem
Hex dump of section '.data':
0x0804a014 00000000 00000000 03000000 ............
要想对编译产生的ELF可执行文件格式有个清楚认识,有两个命令可以提供帮助。
1,Understanding ELF using readelf and objdump
http://www.linuxforums.org/articles/understanding-elf-using-readelf-and-objdump_125.html
2,Linux 查看 elf可执行文件格式的两个命令
http://hi.baidu.com/widebright/blog/item/2acbf536ec3c12390b55a927.html
http://blogold.chinaunix.net/u3/114733/showart_2252992.html
3,ld中文使用手册完全版
http://sanecat.bokee.com/1777309.html
4,关于text段,data段,bss段
http://www.dicder.com/bbs/forum.php?mod=viewthread&tid=2794
http://www.programfan.com/blog/article.asp?id=36605
http://hi.chinaunix.net/?752725/viewspace-24204