一锯岖、main運(yùn)行前可運(yùn)行哪些代碼
? (1)全局對象的構(gòu)造函數(shù)會在main 函數(shù)之前執(zhí)行。
? (2)一些全局變量痹兜、對象和靜態(tài)變量、對象的空間分配和賦初值就是在執(zhí)行main函數(shù)之前颤诀,而main函數(shù)執(zhí)行完后字旭,還要去執(zhí)行一些諸如釋放空間、釋放資源使用權(quán)等操作
? (3)進(jìn)程啟動后着绊,要執(zhí)行一些初始化代碼(如設(shè)置環(huán)境變量等)谐算,然后跳轉(zhuǎn)到main執(zhí)行。全局對象的構(gòu)造也在main之前归露。
? (4)通過關(guān)鍵字__attribute__洲脂,讓一個函數(shù)在主函數(shù)之前運(yùn)行,進(jìn)行一些數(shù)據(jù)初始化、模塊加載驗證等恐锦。
main函數(shù)運(yùn)行之前需要運(yùn)行的邏輯(為了保持原味往果,我就不翻譯了)
Some of the stuff that has to happen before main():
set up initial stack pointer?
initialize static and global data?
zero out uninitialized data?
run global constructors
Some of this comes with the runtime library's crt0.o file or its __start() function. Some of it you need to do yourself.
Crt0 is a synonym for the C runtime library.
Depending on the system you're using the follwing may be incomplete, but it should give you an idea. Using newlib-1.9.0/libgloss/m68k/crt0.S as an outline, the steps are:
1. Set stack pointer to value of __STACK if set?
2. Set the initial value of the frame pointer?
3. Clear .bss (where all the values that start at zero go)?
4. Call indirect of hardware_init_hook if set to initialize hardware?
5. Call indirect of software_init_hook if set to initialize software?
6. Add __do_global_dtors and __FINI_SECTION__ to the atexit function so destructors and other cleanup functions are called when the program exits by either returning from main, or calling exit
7. setup the paramters for argc, argv, argp and call main?
8. call exit if main returns
誰調(diào)用了我的main函數(shù)
我們都聽說過一句話:“main是C語言的入口”。我至今不明白為什么這么說一铅。就好像如果有人說:“掙錢是泡妞”陕贮,肯定無數(shù)磚頭拍過來。這句話應(yīng)該是“掙錢是泡妞的一個條件潘飘,只不過這個條件特別重要”肮之。那么上面那句話應(yīng)該是 “main是C語言中一個符號,只不過這個符號比較特別卜录「昵埽”
我們看下面的例子:
/* file name battle.c */
#include<stdio.h>
int main(int argc, char* argv[])
{
????????????????printf("老鐵,感謝有緣一起學(xué)習(xí)C語言艰毒,除了跟著這套課程走筐高,為了更好地解決大家實操中的問題,可以加QQ群676593534丑瞧,及時交流柑土。群里還有很多精致資料哦,我這這里等你绊汹!");
return 0;
}
編譯鏈接它:
cc battle.c -o test.exe
會生成 test.exe
但是我們加上這個選項: -nostdlib (不鏈接標(biāo)準(zhǔn)庫)
cc battle.c-nostdlib -o test.exe
鏈接器會報錯:
undefined symbol: __start
也就是說:
1. 編譯器缺省是找 __start 符號稽屏,而不是 main
2. __start 這個符號是程序的起始點
3. main 是被標(biāo)準(zhǔn)庫調(diào)用的一個符號
繼續(xù)探索
我們寫程序,比如一個模塊灸促,通常要有 initialize 和 de-initialize诫欠,但是我們寫 C 程序的時候為什么有些模塊沒有這兩個過程么呢?比如我們程序從 main 開始就可以 malloc浴栽,free荒叼,但是我們在 main 里面卻沒有初始化堆。再比如在 main 里面可以直接 printf典鸡,可是我們并沒有打開標(biāo)準(zhǔn)輸出文件啊被廓。(不知道什么是 stdin,stdout萝玷,stderr 以及 printf 和 stdout 關(guān)系的群眾請先看看 C 語言中文件的概念)嫁乘。
有人說,這些東西不需要初始化球碉。如果你真得這么想蜓斧,有點率性而為了。
聰明的人民群眾會想睁冬,一定是在 main 之前干了些什么挎春。使這些函數(shù)可以直接調(diào)用而不用初始化。通常,我們會在編譯器的環(huán)境中找到一個名字類似于 crt0.o 的文件直奋,這個文件中包含了我們剛才所說的 __start 符號能庆。(crt 大概是 C Runtime 的縮寫)
那么真正的 crt0.s 是什么樣子呢?下面我們給出部分偽代碼:
section .text:
__start:
:
init stack;
init heap;
open stdin;
open stdout;
open stderr;
:
push argv;
push argc;
call _main; (調(diào)用 main)
:
destory heap;
close stdin;
close stdout;
close stderr;
:
call __exit;
實際上可能還有很多初始化工作脚线,因為都是和操作系統(tǒng)相關(guān)的搁胆,這里就不一一列出了。
注意:
1. 不同的編譯器邮绿,不一定缺省得符號都是 __start渠旁。
2. 匯編里面的 _main 就是 C 語言里面的 main,是因為匯編器和C編譯器對符號的命名有差異(通常是差一個下劃線'_')船逮。
Demo演示
#include <stdio.h>
#if 0
The constructor attribute causes the function to be called automatically before?
execution enters main (). Similarly, the destructor attribute causes the function?
to be called automatically after main () completes or exit () is called.?
Functions with these attributes are useful for initializing data that is used?
implicitly during the execution of the program.
more infoformation: https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes
#endif?
__attribute__((constructor)) void before_main_to_run()?
{?
? ? printf("Hi~,i am called before the main function!\n");
? ? printf("%s\n",__FUNCTION__);?
}?
__attribute((constructor)) void before_main_to_run_two()?
{?
? ? printf("Hi~,i am called before the main function!\n");
? ? printf("%s\n",__FUNCTION__);?
}?
__attribute__((destructor)) void after_main_to_run()?
{?
? ? printf("%s\n",__FUNCTION__);?
? ? printf("Hi~,i am called after the main function!\n");
}?
__attribute((destructor)) void after_main_to_run_two()?
{?
? ? printf("%s\n",__FUNCTION__);?
? ? printf("Hi~,i am called after the main function!\n");
}?
int main( int argc, char ** argv )?
{?
? ? printf("i am main function, and i can get my name(%s) by this way.\n",__FUNCTION__);?
? ? return 0;?
}?
總結(jié)
main函數(shù)執(zhí)行之前一死,主要就是初始化系統(tǒng)相關(guān)資源:
1.設(shè)置棧指針
2.初始化static靜態(tài)和global全局變量,即data段的內(nèi)容
3.將未初始化部分的賦初值:數(shù)值型short傻唾,int,long等為0承耿,bool為FALSE冠骄,指針為NULL,等等加袋,即.bss段的內(nèi)容
4.將main函數(shù)的參數(shù)凛辣,argc,argv等傳遞給main函數(shù)职烧,然后才真正運(yùn)行main函數(shù)