2010年1月20日 星期三

分析變數宣告在ELF所在的sections

分析變數宣告在ELF所在的sections

1. static 變數a 在assemble階段會allocates storage in the .bss section給該變數
2. global 變數b是Common symbol, 在linking階段才會allocates storage給該變數(The linker merges two or more common symbol declarations for the same symbol.)
3. const global變數c在assemble階段會allocates storage in the .rodata section給該變數
4. local static變數sum 在assemble階段會allocates storage in the .bss section給該變數


1.
//elf_sections_test.c

static int a;
int b;
const int c=9;

int func(int d){
int j=d;
int i=0;
static int sum;
sum+=j;
return sum;
}

2. /usr/local/arm/4.2.2-eabi/usr/bin/arm-unknown-linux-gnueabi-gcc -fomit-frame-pointer -c elf_sections_test.c

3. /usr/local/arm/4.2.2-eabi/usr/bin/arm-unknown-linux-gnueabi-nm elf_sections_test.o
00000004 b a
00000004 C b
00000000 R c
00000000 T func
00000000 b sum.1537

4./usr/local/arm/4.2.2-eabi/usr/bin/arm-unknown-linux-gnueabi-objdump -t elf_sections_test.o | less

elf_sections_test.o: file format elf32-littlearm

SYMBOL TABLE:
00000000 l df *ABS* 00000000 tmp.c
00000000 l d .text 00000000 .text
00000000 l d .data 00000000 .data
00000000 l d .bss 00000000 .bss
00000000 l d .rodata 00000000 .rodata
00000000 l O .bss 00000004 sum.1537
00000004 l O .bss 00000004 a
00000000 l d .comment 00000000 .comment
00000000 l d .ARM.attributes 00000000 .ARM.attributes
00000000 g O .rodata 00000004 c
00000000 g F .text 00000048 func
00000004 O *COM* 00000004 b

5./usr/local/arm/4.2.2-eabi/usr/bin/arm-unknown-linux-gnueabi-objdump -S elf_sections_test.o | less

elf_sections_test.o: file format elf32-littlearm

Disassembly of section .text:

00000000 :
0: e24dd010 sub sp, sp, #16 ; 0x10
4: e58d0004 str r0, [sp, #4]
8: e59d3004 ldr r3, [sp, #4]
c: e58d3008 str r3, [sp, #8]
10: e3a03000 mov r3, #0 ; 0x0
14: e58d300c str r3, [sp, #12]
18: e59f3024 ldr r3, [pc, #36] ; 44
1c: e5932000 ldr r2, [r3]
20: e59d3008 ldr r3, [sp, #8]
24: e0822003 add r2, r2, r3
28: e59f3014 ldr r3, [pc, #20] ; 44
2c: e5832000 str r2, [r3]
30: e59f300c ldr r3, [pc, #12] ; 44
34: e5933000 ldr r3, [r3]
38: e1a00003 mov r0, r3
3c: e28dd010 add sp, sp, #16 ; 0x10
40: e12fff1e bx lr
44: 00000000 .word 0x00000000

6.

There are three kinds of global symbols, illustrated here by C examples:

int i = 1;
A definition, which goes in the initialized data section of the output file.
extern int i;
An undefined reference, which does not allocate space. There must be either a definition or a common symbol for the variable somewhere.
int i;
A common symbol. If there are only (one or more) common symbols for a variable, it goes in the uninitialized data area of the output file. The linker merges multiple common symbols for the same variable into a single symbol. If they are of different sizes, it picks the largest size. The linker turns a common symbol into a declaration, if there is a definition of the same variable.



Reference:

http://sourceware.org/binutils/docs-2.16/ld/Options.html#Options
http://www.gursey.gov.tr/Gilgamesh-WWW/IntelCompDocs/ref/decl_com.htm
http://www.math.utah.edu/docs/info/ld_3.html#SEC15

沒有留言: