链接地址0x0804800
举例分析:
//main.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(“Hello World\n”);
return 0;
}
//math.c
int add(int a, int b) {
return a + b;
}
编译:
gcc -m32 -c math.c
gcc -m32 -c hello.c
gcc -m32 math.o hello.o -o hello
然后执行如下命令:
albert$ readelf -S hello
There are 30 section headers, starting at offset 0x117c:
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 08048154 000154 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048168 000168 000020 00 A 0 0 4
[ 3] .note.gnu.build-i NOTE 08048188 000188 000024 00 A 0 0 4
[ 4] .gnu.hash GNU_HASH 080481ac 0001ac 000020 04 A 5 0 4
[ 5] .dynsym DYNSYM 080481cc 0001cc 000050 10 A 6 1 4
[ 6] .dynstr STRTAB 0804821c 00021c 00004a 00 A 0 0 1
[ 7] .gnu.version VERSYM 08048266 000266 00000a 02 A 5 0 2
[ 8] .gnu.version_r VERNEED 08048270 000270 000020 00 A 6 1 4
[ 9] .rel.dyn REL 08048290 000290 000008 08 A 5 0 4
[10] .rel.plt REL 08048298 000298 000018 08 A 5 12 4
[11] .init PROGBITS 080482b0 0002b0 000023 00 AX 0 0 4
[12] .plt PROGBITS 080482e0 0002e0 000040 04 AX 0 0 16
[13] .text PROGBITS 08048320 000320 0001a2 00 AX 0 0 16
[14] .fini PROGBITS 080484c4 0004c4 000014 00 AX 0 0 4
[15] .rodata PROGBITS 080484d8 0004d8 000014 00 A 0 0 4
[16] .eh_frame_hdr PROGBITS 080484ec 0004ec 000034 00 A 0 0 4
[17] .eh_frame PROGBITS 08048520 000520 0000d0 00 A 0 0 4
[18] .init_array INIT_ARRAY 08049f08 000f08 000004 00 WA 0 0 4
[19] .fini_array FINI_ARRAY 08049f0c 000f0c 000004 00 WA 0 0 4
[20] .jcr PROGBITS 08049f10 000f10 000004 00 WA 0 0 4
[21] .dynamic DYNAMIC 08049f14 000f14 0000e8 08 WA 6 0 4
[22] .got PROGBITS 08049ffc 000ffc 000004 04 WA 0 0 4
[23] .got.plt PROGBITS 0804a000 001000 000018 04 WA 0 0 4
[24] .data PROGBITS 0804a018 001018 000008 00 WA 0 0 4
[25] .bss NOBITS 0804a020 001020 000004 00 WA 0 0 1
[26] .comment PROGBITS 00000000 001020 000056 01 MS 0 0 1
[27] .shstrtab STRTAB 00000000 001076 000106 00 0 0 1
[28] .symtab SYMTAB 00000000 00162c 000450 10 29 46 4
[29] .strtab STRTAB 00000000 001a7c 00025b 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
如上所示:
The linker ld uses the default script that it can find in the system to build the executable binary. In the default script, a segment is assigned a starting address 0x8048000 and sections belong to it. Then:
- 1st section address = starting segment address + section offset = 0x8048000 + 0x154 = 0x08048154
- 2nd section address = starting segment address + section offset = 0x8048000 + 0x168 = 0x08048168
如下所示,32bit链接器script指定text segment为0x08048000。
/usr/lib/ldscripts/elf_i386.x:
1 /* Default linker script, for normal executables */
2 OUTPUT_FORMAT("elf32-i386", "elf32-i386",
3 "elf32-i386")
4 OUTPUT_ARCH(i386)
5 ENTRY(_start)
6 SEARCH_DIR("/usr/i386-linux-gnu/lib32"); SEARCH_DIR("/usr/x86_64-linux-gnu/lib32"); SEARCH_DIR("=/usr/local/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/l
7 SECTIONS
8 {
9 /* Read-only sections, merged into text segment: */
10 PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x08048000)); . = SEGMENT_START("text-segment", 0x08048000) + SIZEOF_HEADERS;
11 .interp : { *(.interp) }
12 .note.gnu.build-id : { *(.note.gnu.build-id) }