You are on page 1of 13

export import

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

#define, #include #ifdef ... #endif

#define #ifdef ... #endif

#include man copy <> okay


<C Traps &
Pitfalls>
? gcc -E hello.c

gcc cpp ( gcc -v cc1)cpp The C


Preprocessor
-D
? gcc -Dmacro hello.c
#define maco

#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

Name Decoration Name Mangling


gcc "-fleading-underscore" "-fno-leading-underscore"
C
C++
int func(int);
float func(float);
class C {
int func(int);
class C2 {
int func(int);
};
};
namespace N {
int func(int);

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

int global __attribute__((no_common));


extern

.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 .

You might also like

  • LNX NFS 1
    LNX NFS 1
    Document9 pages
    LNX NFS 1
    liuyl
    No ratings yet
  • TC 0
    TC 0
    Document2 pages
    TC 0
    liuyl
    No ratings yet
  • LNX KM 1
    LNX KM 1
    Document5 pages
    LNX KM 1
    liuyl
    No ratings yet
  • Hp-Ux KC 10
    Hp-Ux KC 10
    Document1 page
    Hp-Ux KC 10
    liuyl
    No ratings yet
  • WL WS 1
    WL WS 1
    Document3 pages
    WL WS 1
    liuyl
    No ratings yet
  • Data SS1
    Data SS1
    Document17 pages
    Data SS1
    liuyl
    No ratings yet
  • Sed 2
    Sed 2
    Document22 pages
    Sed 2
    liuyl
    No ratings yet
  • Sed 1
    Sed 1
    Document11 pages
    Sed 1
    liuyl
    No ratings yet
  • LNX KM 1
    LNX KM 1
    Document5 pages
    LNX KM 1
    liuyl
    No ratings yet
  • Hds Emc迁移测试方案
    Hds Emc迁移测试方案
    Document4 pages
    Hds Emc迁移测试方案
    liuyl
    No ratings yet
  • HDS Health Check 1
    HDS Health Check 1
    Document4 pages
    HDS Health Check 1
    liuyl
    No ratings yet
  • Data SS0
    Data SS0
    Document4 pages
    Data SS0
    liuyl
    No ratings yet
  • Win Repair
    Win Repair
    Document3 pages
    Win Repair
    liuyl
    No ratings yet
  • Hp-Ux Q4 1
    Hp-Ux Q4 1
    Document10 pages
    Hp-Ux Q4 1
    liuyl
    No ratings yet
  • SW 2
    SW 2
    Document3 pages
    SW 2
    liuyl
    No ratings yet
  • SW 1
    SW 1
    Document1 page
    SW 1
    liuyl
    No ratings yet
  • 40F Backup 2
    40F Backup 2
    Document3 pages
    40F Backup 2
    liuyl
    No ratings yet
  • Win Repair
    Win Repair
    Document3 pages
    Win Repair
    liuyl
    No ratings yet
  • Adv FS1
    Adv FS1
    Document2 pages
    Adv FS1
    liuyl
    No ratings yet
  • 40F Backup 1
    40F Backup 1
    Document4 pages
    40F Backup 1
    liuyl
    No ratings yet
  • Aix Pha Faq
    Aix Pha Faq
    Document1 page
    Aix Pha Faq
    liuyl
    No ratings yet
  • SDUX2
    SDUX2
    Document3 pages
    SDUX2
    liuyl
    No ratings yet
  • SDUX1
    SDUX1
    Document4 pages
    SDUX1
    liuyl
    No ratings yet
  • Program Mmap
    Program Mmap
    Document5 pages
    Program Mmap
    liuyl
    No ratings yet
  • Program Make 7
    Program Make 7
    Document66 pages
    Program Make 7
    liuyl
    No ratings yet
  • Aix Mem Faq
    Aix Mem Faq
    Document3 pages
    Aix Mem Faq
    liuyl
    No ratings yet
  • Program Make 8
    Program Make 8
    Document10 pages
    Program Make 8
    liuyl
    No ratings yet
  • Program Make 9
    Program Make 9
    Document7 pages
    Program Make 9
    liuyl
    No ratings yet
  • Program Make 7
    Program Make 7
    Document66 pages
    Program Make 7
    liuyl
    No ratings yet
  • Program Make 5a
    Program Make 5a
    Document26 pages
    Program Make 5a
    liuyl
    No ratings yet