gcc参数-D 就是在编译时增加对-D后面的宏的定义
gcc如何设置入口函数,找到了这么一条指令 gcc -Os -e myentry -nostartfiles source.c -o source
-nostartfiles :目的是不让编译器把会引用main的代码编入我们的程序
-e:设置程序的入口函数
可以使用 -ffunction-sections 和 -fdata-sections 将每个函数或符号创建为一个sections,其中每个sections名与function或data名保持一致。而在链接阶段, -Wl,--gc-sections 指示链接器去掉不用的section(其中-wl, 表示后面的参数 -gc-sections 传递给链接器),这样就能减少最终的可执行程序的大小了。
我们常常使用下面的配置启用这个功能:
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections
strip -S:Remove all symbols not needed by relocations
strip的默认选项会去除.symbol节的内容以及.debug节的内容,因此尽量只对可执行文件执行strip而不要对静态库或动态库等目标文件strip
-fomit-frame-pointer 函数调用时不保存frame指针,即不保存栈调用地址,会导致无法追踪函数调用顺序,但可以减少代码量,提高程序运行速度。
gcc如何设置入口函数,找到了这么一条指令 gcc -Os -e myentry -nostartfiles source.c -o source
-nostartfiles :目的是不让编译器把会引用main的代码编入我们的程序
-e:设置程序的入口函数
可以使用 -ffunction-sections 和 -fdata-sections 将每个函数或符号创建为一个sections,其中每个sections名与function或data名保持一致。而在链接阶段, -Wl,--gc-sections 指示链接器去掉不用的section(其中-wl, 表示后面的参数 -gc-sections 传递给链接器),这样就能减少最终的可执行程序的大小了。
我们常常使用下面的配置启用这个功能:
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections
strip -S:Remove all symbols not needed by relocations
strip的默认选项会去除.symbol节的内容以及.debug节的内容,因此尽量只对可执行文件执行strip而不要对静态库或动态库等目标文件strip
strip删除的只是符号表的一部分,符号表由两个section组成,分别是dynsym和symtab,strip删除的只是后者,对于可重定位相关的部分,其并未删除。
-S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr
-fomit-frame-pointer 函数调用时不保存frame指针,即不保存栈调用地址,会导致无法追踪函数调用顺序,但可以减少代码量,提高程序运行速度。