上一篇簡單回顧操作系統(tǒng)相關(guān)的知識,這篇正式開始進(jìn)入編譯鏈接部分。
開發(fā)工具IDE一般都把編譯和鏈接都集成進(jìn)去了,這個過程叫做構(gòu)建(build),就算是一個簡單的gcc hello.c命令就包含非常復(fù)雜的過程。下面介紹下將一步一步介紹這一切是怎么實(shí)現(xiàn)的。
編譯過程
一個簡單Hello World程序
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
當(dāng)使用gcc來編譯的時候
gcc hello.c
然后運(yùn)行該程序
./ a.out
上面總共經(jīng)歷了四個步驟,分別是預(yù)處理、編譯、匯編和鏈接。如下圖所示:

gcc可以通過參數(shù)來指定具體的編譯步驟,參數(shù)如下:
-o:指定生成的輸出文件;
-E:僅執(zhí)行編譯預(yù)處理;
-S:將C代碼轉(zhuǎn)換為匯編代碼;
-wall:顯示警告信息;
-c:僅執(zhí)行編譯操作,不進(jìn)行連接操作。
預(yù)編譯(輸出.i文件)
c文件預(yù)處理為.i文件,cpp預(yù)處理為.ii文件。可以通過參數(shù)控制gcc當(dāng)前操作的步驟。前面講過用-E僅僅執(zhí)行預(yù)編譯
gcc -E hello.c -o hello.i
由于預(yù)編譯后的文件內(nèi)容很長就不貼在這里了,放在了本文你末尾,有興趣的可以看一看。預(yù)編譯是處理源代碼中的#開始的預(yù)編譯指令,比如#include #define。
規(guī)則如下(可以參看預(yù)編譯后的內(nèi)容對比):
- 將所有
#define刪除,并且展開所有的宏定義。 - 處理所有的條件預(yù)編譯指令,如
#if #ifdef #eif #else #endif - 處理
#include預(yù)編譯指令,將被包含的文件Haru到預(yù)編譯指令的位置。這個過程可能是一個遞歸的過程,有可能文件中還包含其他文件。 - 刪除所有的注釋
-
添加行號及文件標(biāo)識符。如
# 2 "hello.c" 2,用于編譯器編譯時產(chǎn)生調(diào)試用的行號信息。——格式?jīng)]有弄懂? - 保留所有的#pragma編譯指令,編譯器需要
如果當(dāng)無法判斷宏定義是否正確或者文件包含是否正確時,可以查看編譯后的文件來查找問題。
根據(jù)預(yù)編譯后的
.i文件,沒弄明白的是為什么預(yù)編譯之后很多文件會重復(fù)引入?
編譯(輸出匯編代碼)
編譯是程序構(gòu)建的核心部分,也最復(fù)雜。需要在這個過程中完成詞法分析、語法分析、語義分析及優(yōu)化產(chǎn)生的匯編代碼。
gcc -S hello.i -o hello.s
現(xiàn)在一般GCC把預(yù)編譯和編譯都合在一起了。但是也可以分開實(shí)現(xiàn)!GCC的命令其實(shí)是對一些編譯程序的包裝而已,它根據(jù)不同的參數(shù)去調(diào)用預(yù)編譯程序(cc1)、匯編器(as)、鏈接器(ld)
后面會詳細(xì)介紹
匯編(輸出.o文件)
匯編器將上面生成的匯編代碼轉(zhuǎn)為機(jī)器可以執(zhí)行的指令。每一條匯編語句對應(yīng)一條機(jī)器指令。所以匯編過程比編譯過程簡單,沒有復(fù)雜的語法、語義,也不需要優(yōu)化指令。最終輸出的是目標(biāo)文件.o。
as -c hello.s -o hello.o
匯編之后的.o文件是二進(jìn)制格式的。
鏈接
由于作者實(shí)驗(yàn)的系統(tǒng)是Linux,鏈接的部分需要用到Linux中的庫及.o文件。所以實(shí)驗(yàn)在本地做不了
鏈接總的命令:

如果把路徑都省略了

其中很多不知道什么意思,crt1.o、crti.o文件是什么(Mac中能找到),參數(shù)lgcc、lgcc_en是什么意思。后面在講
如果直接執(zhí)行ld -static helloworld.o,則會報(bào)找不到符號的錯誤。
ld: warning: -macosx_version_min not specified, assuming 10.11
Undefined symbols for architecture x86_64:
"_printf", referenced from:
_main in helloworld.o
"start", referenced from:
-u command line option
ld: symbol(s) not found for inferred architecture x86_64
編譯器(核心)
編譯器就是將高級語言翻譯為機(jī)器語言的一個工具。如果直接使用機(jī)器指令或匯編來寫,一方面效率低,一方面只能為一種CPU編寫,而不能移植。
編譯過程一般分為6步(掃描、語法分析、詞法分析、語義分析、源代碼優(yōu)化、代碼生成和目標(biāo)代碼優(yōu)化):

下面用這段代碼為例:

詞法分析(記號)
源代碼首先被載入掃描器(語法分析器)。掃描器主要作用就是進(jìn)行詞法分析。運(yùn)用類似有限狀態(tài)機(jī)的算法將源代碼字符序列分隔為一系列的記號。其主要作用就是分隔為一系列的記號。
如上面代碼詞法分析結(jié)果:


把這些記號歸類可以有:關(guān)鍵字、標(biāo)識符、字面量(包含數(shù)字、字符串)、特殊符號(加、等號)。然后將這些記號分別存入到對應(yīng)的表中,以便后續(xù)使用。比如標(biāo)識符放到符號表,數(shù)字、字符串放到文字表。
語法分析(語法樹)
語法分析器讀取上面詞法分析的字符流、從中識別出語素、最后生成不同類型的標(biāo)記。其間一旦發(fā)現(xiàn)無效標(biāo)記,便會報(bào)錯。
對掃描器產(chǎn)生的記號進(jìn)行語法分析會生成語法樹,語法樹就是以表達(dá)式為結(jié)點(diǎn)的樹。一個語句可能由多個表達(dá)式組成,如上面的例子。最終產(chǎn)生的語法樹如下:

解讀一下:整個語句看做是一個賦值表達(dá)式,左邊是要給數(shù)組表達(dá)式,右邊是一個乘法表達(dá)式,數(shù)組表達(dá)式由連個符號表達(dá)式組成。符號和數(shù)字是最小的表達(dá)式,所以作為整個語法書的結(jié)點(diǎn)。同時優(yōu)先級和含義也會在這個過程中確定下來,比如乘法優(yōu)先級比加法搞。
如果出現(xiàn)表達(dá)式不合法,比如括號不匹配,缺少什么操作符等,編譯器就會報(bào)語法分析錯誤。
語義分析(標(biāo)識類型)
語法分析主要是完成對表達(dá)式的語法層面的分析,但是不了解這個語句是否真的有含義。比如c語言兩個指針做乘法是沒有意義的,但語句是合法的。編譯能分析的只能是靜態(tài)語義,也即是在編譯階段可以確定的語義。動態(tài)語義只能在運(yùn)行期才能確定。
常見的靜態(tài)語義包括聲明和類型匹配、類型轉(zhuǎn)換。比如浮點(diǎn)表達(dá)式賦值給整型表達(dá)式,隱含著浮點(diǎn)轉(zhuǎn)整型的過程;浮點(diǎn)賦值給指針的時候,語義分析這個類型不匹配,編譯器也會報(bào)錯。動態(tài)最長見得就是將0作為除數(shù)就是運(yùn)行期語義錯誤。
經(jīng)過語義分析極端,整個語法樹表達(dá)式都被標(biāo)識了類型。

中間語言生成(跨平臺)
編譯器有很多層次優(yōu)化,并且在源代碼就會有優(yōu)化。這里提到的是源碼級別的而優(yōu)化器。
比如源碼級別的優(yōu)化針對上面的(2+6)表達(dá)式就會直接優(yōu)化為8,因?yàn)檫@個值在編譯期就可以確定了。最終生成如下語法樹

直接在語法樹上做優(yōu)化比較困難,所以源代碼優(yōu)化器往往把語法樹轉(zhuǎn)換為中間代碼,他是語法樹的順序表示。非常接近目標(biāo)代碼,但是不保護(hù)數(shù)據(jù)尺寸,變量地址,寄存器名字等。
中間有很多種,比如三地址碼和P-代碼。
三地址碼:x = y op z
表示把變量y和z操作以后賦值給x。因?yàn)槿齻€地址碼語句有三個變量地址因此得名。
上面的語法樹轉(zhuǎn)為三地址碼如下:

這里利用幾個臨時變量,優(yōu)化程序會將2+6計(jì)算出來,得到t1=6然后將t1帶入后面的代碼。其實(shí)還可以省略t3。因?yàn)閠2可以復(fù)用。最終簡化如下:


中間代碼使得編譯器分為前端和后端,前端負(fù)責(zé)產(chǎn)生平臺無關(guān)的中間代碼,后端將中間代碼轉(zhuǎn)為平臺相關(guān)的代碼。如果是跨平臺的編譯器,可以針對不同的平臺使用同一個前端,不同的平臺用多個后端。
生成目標(biāo)代碼
中間代碼之后就術(shù)語編譯器后端,主要包含代碼生成及目標(biāo)代優(yōu)化。
代碼生成將中間代碼轉(zhuǎn)換為目標(biāo)的機(jī)器代碼,這個過程依賴于機(jī)器,不同的機(jī)器有不同的字長,寄存器,整數(shù)數(shù)據(jù)類型及浮點(diǎn)數(shù)類型。上面生成的中間代碼會可能會生成如下代碼序列(X86下的匯編語言 )

高級編程語言本身非常復(fù)雜,如C++的定義極為復(fù)雜,目前沒有任何一個編譯器完整支持C++所有特性。其次CPU異常復(fù)雜,本身采用了流水線、多發(fā)射、超標(biāo)量等特性,為了支持這些特性,編譯器也異常復(fù)雜,尤其是表一起支持多硬件平臺。
經(jīng)過了掃描、語法分析、語義分析,源代碼優(yōu)化、代碼生成及目標(biāo)代碼優(yōu)化,最終編譯為了目標(biāo)代碼。但是目標(biāo)代碼中的變量如index、array地址還沒確定。如果index、array在同一編譯單元里面,那么編譯器可以為index和array分配內(nèi)存空間,確定地址,如果是定義在其他程序模塊,則還需要鏈接其他模塊
鏈接器
符號(Symbol)這個概念隨著匯編語言的普及而迅速被使用,用來表示一個地址??梢允挂欢巫映绦颍ê瘮?shù)的其實(shí)地址),也可以是一個變量的地址。
重用,劃分更小的粒度,最開始程序時寫在一個文件中,后來拆分為多個模塊。
模塊之間如何組合可以歸結(jié)為模塊之前如何通信,最常見的就是C/C++這種靜態(tài)語言兩章方式:一種模塊間函數(shù)調(diào)用,一種是模塊間變量訪問。函數(shù)調(diào)用必須知道目標(biāo)函數(shù)的其實(shí)地址,變量訪問也需要知道變量地址。
最終歸結(jié)為一種方式,那么就是模塊間符號引用,模塊間通過符號來通信類似于拼圖版,定義符號的模塊多出一塊區(qū)域,引用該符號的模塊搞好少了那個區(qū)域——拼接過程就是鏈接

模塊拼裝
鏈接的主要內(nèi)容就是將各個模塊之間相互引用的部分都處理好,是模塊之間能夠正確的銜接。本質(zhì)就是將依稀額之靈對其他符號地址的引用加以修正。
鏈接過程包含地址空間分配、符號決議和重定位

每個模塊的源代碼進(jìn)過編譯器便以為目標(biāo)文件,目標(biāo)文件和庫一起鏈接為最終的可執(zhí)行文件。最常見的就是運(yùn)行時庫(Runtime Library),它是支持程序運(yùn)行的基本函數(shù)集合。庫本身是一組目標(biāo)文件的包,包含一些常用代碼編譯為目標(biāo)文件打包之后的集合。
重定位
在鏈接過程中,對其他定義在目標(biāo)文件中的函數(shù)、變量調(diào)用的指令都需要重新調(diào)整,結(jié)合CPU指令來理解。
假設(shè)有個全局變量var目標(biāo)文件在目標(biāo)文件A里面定義,現(xiàn)在在目標(biāo)文件B要訪問這個全局變量,比如執(zhí)行var = 42這段代碼。對應(yīng)的匯編

現(xiàn)在編譯目標(biāo)文件得到這個機(jī)器碼:

由于目標(biāo)文件并不知道var的變量地址,所以編譯器沒有辦法確認(rèn),這里默認(rèn)將目標(biāo)地址置位0,等待編譯器在鏈接目標(biāo)文件A、B的時候在將其修正。
加入A、B鏈接之后,變量var的地址為0x1000,那么連接器就會把這個目標(biāo)地址修正為0x1000。
上面的這個過程就是重定位,修正的地方叫做重定位入口。
目標(biāo)文件與可執(zhí)行文件對比
這里用的工具是hooper,在iOS逆向的時候經(jīng)常用到
目標(biāo)文件hello.o的內(nèi)容

可執(zhí)行文件a.out的內(nèi)容

以上對比就能看到,目標(biāo)文件與可執(zhí)行文件的差別很大??蓤?zhí)行文件中包含了其他.o文件的符號。
實(shí)驗(yàn)數(shù)據(jù)
原程序
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
預(yù)編譯后的內(nèi)容
# 1 "hello.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 331 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "hello.c" 2
# 1 "/usr/include/stdio.h" 1 3 4
# 64 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 587 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/sys/_symbol_aliasing.h" 1 3 4
# 588 "/usr/include/sys/cdefs.h" 2 3 4
# 653 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/sys/_posix_availability.h" 1 3 4
# 654 "/usr/include/sys/cdefs.h" 2 3 4
# 65 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/Availability.h" 1 3 4
# 190 "/usr/include/Availability.h" 3 4
# 1 "/usr/include/AvailabilityInternal.h" 1 3 4
# 191 "/usr/include/Availability.h" 2 3 4
# 66 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/_types.h" 1 3 4
# 27 "/usr/include/_types.h" 3 4
# 1 "/usr/include/sys/_types.h" 1 3 4
# 33 "/usr/include/sys/_types.h" 3 4
# 1 "/usr/include/machine/_types.h" 1 3 4
# 32 "/usr/include/machine/_types.h" 3 4
# 1 "/usr/include/i386/_types.h" 1 3 4
# 37 "/usr/include/i386/_types.h" 3 4
typedef signed char __int8_t;
typedef unsigned char __uint8_t;
typedef short __int16_t;
typedef unsigned short __uint16_t;
typedef int __int32_t;
typedef unsigned int __uint32_t;
typedef long long __int64_t;
typedef unsigned long long __uint64_t;
typedef long __darwin_intptr_t;
typedef unsigned int __darwin_natural_t;
# 70 "/usr/include/i386/_types.h" 3 4
typedef int __darwin_ct_rune_t;
typedef union {
char __mbstate8[128];
long long _mbstateL;
} __mbstate_t;
typedef __mbstate_t __darwin_mbstate_t;
typedef long int __darwin_ptrdiff_t;
typedef long unsigned int __darwin_size_t;
typedef __builtin_va_list __darwin_va_list;
typedef int __darwin_wchar_t;
typedef __darwin_wchar_t __darwin_rune_t;
typedef int __darwin_wint_t;
typedef unsigned long __darwin_clock_t;
typedef __uint32_t __darwin_socklen_t;
typedef long __darwin_ssize_t;
typedef long __darwin_time_t;
# 33 "/usr/include/machine/_types.h" 2 3 4
# 34 "/usr/include/sys/_types.h" 2 3 4
# 55 "/usr/include/sys/_types.h" 3 4
typedef __int64_t __darwin_blkcnt_t;
typedef __int32_t __darwin_blksize_t;
typedef __int32_t __darwin_dev_t;
typedef unsigned int __darwin_fsblkcnt_t;
typedef unsigned int __darwin_fsfilcnt_t;
typedef __uint32_t __darwin_gid_t;
typedef __uint32_t __darwin_id_t;
typedef __uint64_t __darwin_ino64_t;
typedef __darwin_ino64_t __darwin_ino_t;
typedef __darwin_natural_t __darwin_mach_port_name_t;
typedef __darwin_mach_port_name_t __darwin_mach_port_t;
typedef __uint16_t __darwin_mode_t;
typedef __int64_t __darwin_off_t;
typedef __int32_t __darwin_pid_t;
typedef __uint32_t __darwin_sigset_t;
typedef __int32_t __darwin_suseconds_t;
typedef __uint32_t __darwin_uid_t;
typedef __uint32_t __darwin_useconds_t;
typedef unsigned char __darwin_uuid_t[16];
typedef char __darwin_uuid_string_t[37];
# 1 "/usr/include/sys/_pthread/_pthread_types.h" 1 3 4
# 57 "/usr/include/sys/_pthread/_pthread_types.h" 3 4
struct __darwin_pthread_handler_rec {
void (*__routine)(void *);
void *__arg;
struct __darwin_pthread_handler_rec *__next;
};
struct _opaque_pthread_attr_t {
long __sig;
char __opaque[56];
};
struct _opaque_pthread_cond_t {
long __sig;
char __opaque[40];
};
struct _opaque_pthread_condattr_t {
long __sig;
char __opaque[8];
};
struct _opaque_pthread_mutex_t {
long __sig;
char __opaque[56];
};
struct _opaque_pthread_mutexattr_t {
long __sig;
char __opaque[8];
};
struct _opaque_pthread_once_t {
long __sig;
char __opaque[8];
};
struct _opaque_pthread_rwlock_t {
long __sig;
char __opaque[192];
};
struct _opaque_pthread_rwlockattr_t {
long __sig;
char __opaque[16];
};
struct _opaque_pthread_t {
long __sig;
struct __darwin_pthread_handler_rec *__cleanup_stack;
char __opaque[8176];
};
typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
typedef struct _opaque_pthread_cond_t __darwin_pthread_cond_t;
typedef struct _opaque_pthread_condattr_t __darwin_pthread_condattr_t;
typedef unsigned long __darwin_pthread_key_t;
typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t;
typedef struct _opaque_pthread_mutexattr_t __darwin_pthread_mutexattr_t;
typedef struct _opaque_pthread_once_t __darwin_pthread_once_t;
typedef struct _opaque_pthread_rwlock_t __darwin_pthread_rwlock_t;
typedef struct _opaque_pthread_rwlockattr_t __darwin_pthread_rwlockattr_t;
typedef struct _opaque_pthread_t *__darwin_pthread_t;
# 81 "/usr/include/sys/_types.h" 2 3 4
# 28 "/usr/include/_types.h" 2 3 4
# 39 "/usr/include/_types.h" 3 4
typedef int __darwin_nl_item;
typedef int __darwin_wctrans_t;
typedef __uint32_t __darwin_wctype_t;
# 68 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/sys/_types/_va_list.h" 1 3 4
# 31 "/usr/include/sys/_types/_va_list.h" 3 4
typedef __darwin_va_list va_list;
# 72 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/sys/_types/_size_t.h" 1 3 4
# 30 "/usr/include/sys/_types/_size_t.h" 3 4
typedef __darwin_size_t size_t;
# 73 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/sys/_types/_null.h" 1 3 4
# 74 "/usr/include/stdio.h" 2 3 4
# 1 "/usr/include/sys/stdio.h" 1 3 4
# 39 "/usr/include/sys/stdio.h" 3 4
int renameat(int, const char *, int, const char *) __attribute__((availability(macosx,introduced=10.10)));
int renamex_np(const char *, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)));
int renameatx_np(int, const char *, int, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)));
# 76 "/usr/include/stdio.h" 2 3 4
typedef __darwin_off_t fpos_t;
# 88 "/usr/include/stdio.h" 3 4
struct __sbuf {
unsigned char *_base;
int _size;
};
struct __sFILEX;
# 122 "/usr/include/stdio.h" 3 4
typedef struct __sFILE {
unsigned char *_p;
int _r;
int _w;
short _flags;
short _file;
struct __sbuf _bf;
int _lbfsize;
void *_cookie;
int (* _Nullable _close)(void *);
int (* _Nullable _read) (void *, char *, int);
fpos_t (* _Nullable _seek) (void *, fpos_t, int);
int (* _Nullable _write)(void *, const char *, int);
struct __sbuf _ub;
struct __sFILEX *_extra;
int _ur;
unsigned char _ubuf[3];
unsigned char _nbuf[1];
struct __sbuf _lb;
int _blksize;
fpos_t _offset;
} FILE;
extern FILE *__stdinp;
extern FILE *__stdoutp;
extern FILE *__stderrp;
# 231 "/usr/include/stdio.h" 3 4
void clearerr(FILE *);
int fclose(FILE *);
int feof(FILE *);
int ferror(FILE *);
int fflush(FILE *);
int fgetc(FILE *);
int fgetpos(FILE * restrict, fpos_t *);
char *fgets(char * restrict, int, FILE *);
FILE *fopen(const char * restrict __filename, const char * restrict __mode) __asm("_" "fopen" );
int fprintf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
int fputc(int, FILE *);
int fputs(const char * restrict, FILE * restrict) __asm("_" "fputs" );
size_t fread(void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream);
FILE *freopen(const char * restrict, const char * restrict,
FILE * restrict) __asm("_" "freopen" );
int fscanf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3)));
int fseek(FILE *, long, int);
int fsetpos(FILE *, const fpos_t *);
long ftell(FILE *);
size_t fwrite(const void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream) __asm("_" "fwrite" );
int getc(FILE *);
int getchar(void);
char *gets(char *);
void perror(const char *);
int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
int putc(int, FILE *);
int putchar(int);
int puts(const char *);
int remove(const char *);
int rename (const char *__old, const char *__new);
void rewind(FILE *);
int scanf(const char * restrict, ...) __attribute__((__format__ (__scanf__, 1, 2)));
void setbuf(FILE * restrict, char * restrict);
int setvbuf(FILE * restrict, char * restrict, int, size_t);
int sprintf(char * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((__availability__(swift, unavailable, message="Use snprintf instead.")));
int sscanf(const char * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3)));
FILE *tmpfile(void);
__attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead.")))
__attribute__((deprecated("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead.")))
char *tmpnam(char *);
int ungetc(int, FILE *);
int vfprintf(FILE * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0)));
int vprintf(const char * restrict, va_list) __attribute__((__format__ (__printf__, 1, 0)));
int vsprintf(char * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((__availability__(swift, unavailable, message="Use vsnprintf instead.")));
# 297 "/usr/include/stdio.h" 3 4
char *ctermid(char *);
FILE *fdopen(int, const char *) __asm("_" "fdopen" );
int fileno(FILE *);
# 321 "/usr/include/stdio.h" 3 4
int pclose(FILE *) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead.")));
FILE *popen(const char *, const char *) __asm("_" "popen" ) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead.")));
# 342 "/usr/include/stdio.h" 3 4
int __srget(FILE *);
int __svfscanf(FILE *, const char *, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
int __swbuf(int, FILE *);
# 353 "/usr/include/stdio.h" 3 4
inline __attribute__ ((__always_inline__)) int __sputc(int _c, FILE *_p) {
if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
return (*_p->_p++ = _c);
else
return (__swbuf(_c, _p));
}
# 379 "/usr/include/stdio.h" 3 4
void flockfile(FILE *);
int ftrylockfile(FILE *);
void funlockfile(FILE *);
int getc_unlocked(FILE *);
int getchar_unlocked(void);
int putc_unlocked(int, FILE *);
int putchar_unlocked(int);
int getw(FILE *);
int putw(int, FILE *);
__attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead.")))
__attribute__((deprecated("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead.")))
char *tempnam(const char *__dir, const char *__prefix) __asm("_" "tempnam" );
# 417 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/sys/_types/_off_t.h" 1 3 4
# 30 "/usr/include/sys/_types/_off_t.h" 3 4
typedef __darwin_off_t off_t;
# 418 "/usr/include/stdio.h" 2 3 4
int fseeko(FILE * __stream, off_t __offset, int __whence);
off_t ftello(FILE * __stream);
int snprintf(char * restrict __str, size_t __size, const char * restrict __format, ...) __attribute__((__format__ (__printf__, 3, 4)));
int vfscanf(FILE * restrict __stream, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
int vscanf(const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 1, 0)));
int vsnprintf(char * restrict __str, size_t __size, const char * restrict __format, va_list) __attribute__((__format__ (__printf__, 3, 0)));
int vsscanf(const char * restrict __str, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
# 442 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/sys/_types/_ssize_t.h" 1 3 4
# 30 "/usr/include/sys/_types/_ssize_t.h" 3 4
typedef __darwin_ssize_t ssize_t;
# 443 "/usr/include/stdio.h" 2 3 4
int dprintf(int, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((availability(macosx,introduced=10.7)));
int vdprintf(int, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((availability(macosx,introduced=10.7)));
ssize_t getdelim(char ** restrict __linep, size_t * restrict __linecapp, int __delimiter, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7)));
ssize_t getline(char ** restrict __linep, size_t * restrict __linecapp, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7)));
# 458 "/usr/include/stdio.h" 3 4
extern const int sys_nerr;
extern const char *const sys_errlist[];
int asprintf(char ** restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
char *ctermid_r(char *);
char *fgetln(FILE *, size_t *);
const char *fmtcheck(const char *, const char *);
int fpurge(FILE *);
void setbuffer(FILE *, char *, int);
int setlinebuf(FILE *);
int vasprintf(char ** restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0)));
FILE *zopen(const char *, const char *, int);
FILE *funopen(const void *,
int (* _Nullable)(void *, char *, int),
int (* _Nullable)(void *, const char *, int),
fpos_t (* _Nullable)(void *, fpos_t, int),
int (* _Nullable)(void *));
# 498 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/secure/_stdio.h" 1 3 4
# 31 "/usr/include/secure/_stdio.h" 3 4
# 1 "/usr/include/secure/_common.h" 1 3 4
# 32 "/usr/include/secure/_stdio.h" 2 3 4
# 42 "/usr/include/secure/_stdio.h" 3 4
extern int __sprintf_chk (char * restrict, int, size_t,
const char * restrict, ...);
# 52 "/usr/include/secure/_stdio.h" 3 4
extern int __snprintf_chk (char * restrict, size_t, int, size_t,
const char * restrict, ...);
extern int __vsprintf_chk (char * restrict, int, size_t,
const char * restrict, va_list);
extern int __vsnprintf_chk (char * restrict, size_t, int, size_t,
const char * restrict, va_list);
# 499 "/usr/include/stdio.h" 2 3 4
# 2 "hello.c" 2
int main()
{
printf("Hello World");
return 0;
}
編譯之后的內(nèi)容
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 12
.globl _main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
pushq %rbp
Lcfi0:
.cfi_def_cfa_offset 16
Lcfi1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Lcfi2:
.cfi_def_cfa_register %rbp
subq $16, %rsp
leaq L_.str(%rip), %rdi
movl $0, -4(%rbp)
movb $0, %al
callq _printf
xorl %ecx, %ecx
movl %eax, -8(%rbp) ## 4-byte Spill
movl %ecx, %eax
addq $16, %rsp
popq %rbp
retq
.cfi_endproc
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "Hello World"
.subsections_via_symbols