Professional Documents
Culture Documents
static static
extern
static extern int a
static static
static care
.symtab
extern int a int a
2
3
.rel.text .rel.data
textdata
(
.rel.text .rel.data
gcc
-v
-E,-S,-c - O cpp,cc1,as,ld
1
C C
#ifdef DEBUG
printf("this code is for debugging\n");
#endif
-DDEBUG printf
-DDEBUG
[1] C :
http://www.bc-cn.net/Article/kfyy/cyy/jc/200409/9.html
[2]
http://www.hemee.com/kfyy/c/6626.html
http://www.91linux.com/html/article/program/cpp/20071203/8745.html
http://www.janker.org/bbs/programmer/2006-10-13/327.html
2
C (-fsyntax-only)
-S
shell
-std
(-O)
-fsyntax-only
gcc -std - pedantic-pedantic-erros)
C
$ cat hello.c
#include <stdio.h>
int main()
{
printf("hello, world\n")
return 0;
}
$ gcc -fsyntax-only hello.c
hello.c: In function main:
hello.c:5: error: expected ; before return
$ vim hello.c
$ cat hello.c
#include <stdio.h>
int main()
{
printf("hello, world\n");
int i;
return 0;
}
$ gcc -std=c89 -pedantic-errors hello.c #gcc
turbo c
hello.c: In function main:
hello.c:5: error: ISO C90 forbids mixed declarations and code
<C Traps&Pitfalls>[12]
gcc -O
Quote:
$ gcc -o hello hello.c
#
$ gcc -O2 -o hello2 hello.c
# 2
$ gcc -Os -o hellos hello.c
#
$ ls -S hello hello2 hellos
#hellos ,hello2
hello2 hello hellos
$ time ./hello
hello, world
real 0m0.001s
user 0m0.000s
sys 0m0.000s
$ time ./hello2
hello, world
real 0m0.001s
user 0m0.000s
sys 0m0.000s
$ time ./hellos
hello, world
real 0m0.002s
user 0m0.000s
sys 0m0.000s
gcc
-S hello.c
$ gcc -S hello.c
# hello.s-o --o
file
$ cat hello.s
cat hello.s
.file "hello.c"
.section
.rodata
.LC0:
.string "hello, world"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
movl $.LC0, (%esp)
call puts
movl $0, %eax
addl $4, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)"
.section
.note.GNU-stack,"",@progbits
intel AT&T
AT&T [2] Linux
Linux
C
()
("Linux_Assembly_Language_Programming"
)()
resolve binding
External Symbol,
readelf -s xxx.o
bind GLOBAL
__executable_start
__etext, _etext, etext
_edata edata
_end end
class C {
int func(int);
};
}
gcc
int func(int)
_Z4funci
float func(float)
_Z4funcf
int C::func(int)
_ZN1C4funcEi
int C::C2::func(int)
_ZN1C2C24funcEi
int N::func(int)
_ZN1N4funcEi
int N::C::func(int)
_ZN1N1C4funcEi
binutils c++filt
c++filt _ZN1N1C4funcEi N::C::func(int)
VC
int func(int)
?func@@YAHH@Z
float func(float)
?func@@YAMM@Z
int C::func(int)
?func@C@@AAEHH@Z
int C::C2::func(int)
?func@C2@C@@AAEHH@Z
int N::func(int)
?func@N@@YAHH@Z
int N::C::func(int)
?func@C@N@@AAEHH@Z
api UnDecorateSymbolName().
linux extern C gcc C++ C++
C++
a)
b)
c)
ld
.o
global_uninit_var readelf -s
st_name = "global_uninit_var"
st_value = 4
st_size = 4
st_info = 0x11 STB_GLOBAL STT_OBJECT
st_other = 0
st_shndx = 0xfff2 SHN_COMMON
SHN_COMMON Common Block
.o global_uninit_var 8
double common block
global_uninit_var
global_uninit_var 8
COMMON
ld
ld: warning: alignment 4 of symbol `global' in a.o is smaller than 8 in b.o
.bss
bss
.bss
GCC -fno-common COMMON
.o .s .o .s
.o .s
21
21A B
C D
()
A
C 22
22A B
C D
A A
48
48A
B
C
D
()
C
A
B
C
D
D
A B A
B B A
C B A
D A B C
A CPascal
48
48A
B
C
D
()
C
C
A C B 21
21A B A
B B A
C B A
D A B C
A CPascal
20
20A B C D
A
Logical Address
C (&)
Linear AddressPhysical
Address CPU
42
42A B C D
49
49A B C D
20
20A B
C D
2-4
C
1. s
.c . GCC -s .
.
2. S .
S .
( Linux u-boot),
,, .
: #include, #define, #ifdef, #else, #if, #elif, #endif .
Linux u-boot .S .