做一个“好”人... 思想上会思考; 生活上有追求; 技术上不停步; 工作上有担当;
分类: LINUX
2015-11-30 23:46:37
原文地址:可执行文件elf的格式 作者:pascal4123
$:gcc -c hello.c
$:ls
hello.c hello.o
$:strip hello.o
$:gcc hello.o
/usr/lib/gcc/i686-pc-linux-gnu/3.4.5/../../../crt1.o: In function `_start':
init.c: (.text+0x18) : undefined reference to `main' collect2: ld returned 1 exit status
$:cat hello.c
main( )
{
printf("Hello World\n");
}
struct exec
{
unsigned long a_info; /*Use macros N_MAGIC, etc for access */
unsigned a_text; /* length of text, in bytes */
unsigned a_data; /* length of data, in bytes */
unsigned a_bss; /* length of uninitialized data area for file, in bytes*/
unsigned a_syms; /* length of symbol table data in file, in bytes */
unsigned a_entry; /* start address */
unsigned a_trsize; /*length of relocation info for text, in bytes */
unsigned a_drsize; /*length of relocation info for data, in bytes */
};
$:cat hello.c
main()
{
printf("Hello World\n");
}
$:gcc-elf -c hello.c
$:readelf -h hello.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: 256 (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: 11
Section header string table index: 8
$:readelf -S hello.o
There are 11 section headers, starting at offset 0x100:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000034 00002a 00 AX 0 0 4
[ 2] .rel.text REL 00000000 000370 000010 08 9 1 4
[ 3] .data PROGBITS 00000000 000060 000000 00 WA 0 0 4
[ 4] .bss NOBITS 00000000 000060 000000 00 WA 0 0 4
[ 5] .rodata PROGBITS 00000000 000060 00000e 00 A 0 0 1
[ 6] .note.GNU-stack PROGBITS 00000000 00006e 000000 00 0 0 1
[ 7] .comment PROGBITS 00000000 00006e 00003e 00 0 0 1
[ 8] .shstrtab STRTAB 00000000 0000ac 000051 00 0 0 1
[ 9] .symtab SYMTAB 00000000 0002b8 0000a0 10 10 8 4
[10] .strtab STRTAB 00000000 000358 000015 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 -r hello.o
Relocation section '.rel.text' at offset 0x370 contains 2 entries:
Offset Info Type Sym.Value Sym. Name
0000001f 00000501 R_386_32 00000000 .rodata
00000024 00000902 R_386_PC32 00000000 printf
$:gcc hello.o
$:readelf -S a.out
There are 32 section headers, starting at offset 0xbc4:
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] .hash HASH 08048168 000168 00002c 04 A 4 0 4
[ 4] .dynsym DYNSYM 08048194 000194 000060 10 A 5 1 4
[ 5] .dynstr STRTAB 080481f4 0001f4 000060 00 A 0 0 1
[ 6] .gnu.version VERSYM 08048254 000254 00000c 02 A 4 0 2
[ 7] .gnu.version_r VERNEED 08048260 000260 000020 00 A 5 1 4
[ 8] .rel.dyn REL 08048280 000280 000008 08 A 4 0 4
[ 9] .rel.plt REL 08048288 000288 000010 08 A 4 11 4
[10] .init PROGBITS 08048298 000298 000017 00 AX 0 0 4
[11] .plt PROGBITS 080482b0 0002b0 000030 04 AX 0 0 4
[12] .text PROGBITS 080482e0 0002e0 0001b4 00 AX 0 0 16
[13] .fini PROGBITS 08048494 000494 00001a 00 AX 0 0 4
[14] .rodata PROGBITS 080484b0 0004b0 000016 00 A 0 0 4
[15] .eh_frame PROGBITS 080484c8 0004c8 000004 00 A 0 0 4
[16] .ctors PROGBITS 080494cc 0004cc 000008 00 WA 0 0 4
[17] .dtors PROGBITS 080494d4 0004d4 000008 00 WA 0 0 4
[18] .jcr PROGBITS 080494dc 0004dc 000004 00 WA 0 0 4
[19] .dynamic DYNAMIC 080494e0 0004e0 0000c8 08 WA 5 0 4
[20] .got PROGBITS 080495a8 0005a8 000004 04 WA 0 0 4
[21] .got.plt PROGBITS 080495ac 0005ac 000014 04 WA 0 0 4
[22] .data PROGBITS 080495c0 0005c0 00000c 00 WA 0 0 4
[23] .bss NOBITS 080495cc 0005cc 000004 00 WA 0 0 4
[24] .comment PROGBITS 00000000 0005cc 0001b2 00 0 0 1
[25] .debug_aranges PROGBITS 00000000 000780 000058 00 0 0 8
[26] .debug_info PROGBITS 00000000 0007d8 000164 00 0 0 1
[27] .debug_abbrev PROGBITS 00000000 00093c 000020 00 0 0 1
[28] .debug_line PROGBITS 00000000 00095c 00015a 00 0 0 1
[29] .shstrtab STRTAB 00000000 000ab6 00010c 00 0 0 1
[30] .symtab SYMTAB 00000000 0010c4 000510 10 31 56 4
[31] .strtab STRTAB 00000000 0015d4 000322 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)
$:cat Dyn_hello.c
int main(void)
{
hi();
}
$:cat hi.c
#include
hi()
{
printf("Hello world\n");
}
$:gcc -fPIC -c hi.c
$:gcc -shared -o libhi.so hi.o
$:gcc -c Dyn_hello.c
$:gcc -o Dyn_hello Dyn_hello.o -L. -lhi
$:./Dyn_hello
./Dyn_hello: error while loading shared libraries: libhi.so: cannot open shared object file: No such file or directory
$:LD_LIBRARY_PATH=. ./Dyn_hello
Hello world
$:cat Dl_hello.c
#include
int
main (int argc, char *argv[])
{
void (*hi) ();
void *m;
if (argc > 2)
exit (0);
m = dlopen (argv[1], RTLD_LAZY);
if (!m)
exit (0);
hi = dlsym (m, "hi");
if (hi)
{
(*hi) ();
}
dlclose (m);
}
$:gcc -c Dl_hello.c
$:gcc -o Dl_hello Dl_hello.o -ldl
$:./Dl_hello ./libhi.so
Hello world