There are 37 section headers, starting at offset 0x27f2868: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .reset PROGBITS 00000000 010000 0010c0 00 AX 0 0 16 [ 2] .bootimage_func PROGBITS 000010c0 5d0000 000000 00 W 0 0 1 [ 3] .power_sleep_func PROGBITS 000010c0 5d0000 000000 00 W 0 0 1 [ 4] .power_suspend_fu PROGBITS 000010c0 5d0000 000000 00 W 0 0 1 [ 5] .init PROGBITS 000010c0 0110c0 000018 00 AX 0 0 4 [ 6] .text PROGBITS 000010d8 0110d8 4644ec 00 AX 0 0 4 [ 7] .init.text PROGBITS 004655c4 4755c4 0000c0 00 AX 0 0 4 [ 8] .fini PROGBITS 00465684 475684 000018 00 AX 0 0 4 [ 9] .rodata PROGBITS 004656a0 4756a0 0d9ed0 00 A 0 0 8 [10] .ARM.extab PROGBITS 0053f570 54f570 0097e8 00 A 0 0 4 [11] .ARM.exidx ARM_EXIDX 00548d58 558d58 0197f8 00 AL 6 0 4 [12] .eh_frame PROGBITS 00562550 572550 000048 00 A 0 0 4 [13] .init_array INIT_ARRAY 00562598 572598 000020 00 WA 0 0 4 [14] .fini_array FINI_ARRAY 005625b8 5725b8 000004 00 WA 0 0 4 [15] .jcr PROGBITS 005625bc 5725bc 000004 00 WA 0 0 4 [16] .data PROGBITS 005625c0 5725c0 0596e8 00 WA 0 0 8 [17] .tlb PROGBITS 005bbca8 5cbca8 004358 00 WA 0 0 4 [18] .bss NOBITS 005c0000 5d0000 d772e0 00 WA 0 0 32 [19] .mmap PROGBITS 013372e0 5d0000 000000 00 W 0 0 1 [20] .stack NOBITS 013372e0 5d0000 004400 00 WA 0 0 1 [21] .reserved NOBITS 0133b6e0 5d0000 040040 00 WA 0 0 1 [22] .heap PROGBITS 0137b720 5d0000 000000 00 W 0 0 1 [23] .comment PROGBITS 00000000 5d0000 000058 01 MS 0 0 1 [24] .debug_aranges PROGBITS 00000000 5d0058 036708 00 0 0 8 [25] .debug_info PROGBITS 00000000 606760 f1979f 00 0 0 1
http://blog.csdn.net/shevsten/article/details/7049688ios
在AVR Studio4/5的AVR/GCC默認設置下,未調用的函數也會被link進最終的鏡像,從而增大image的大小,這會浪費flash資源.
如下是如何在AVR Studio4/5設置,不把未調用的函數link進image.
方法是在complier命令中增長-ffunction-sections,linker命令中增長-Wl,--gc-sections.
-ffunction-sections:不用此參數時,.o裏代碼部分只有.text段;使用此參數,則會使每一個函數單獨成爲一段,好比函數func1()成爲.text.func1段,但對連接後代碼大小沒影響。
--gc-sections:這是avr-ld的參數,經過-Wl,<option>由gcc把option裏的參數傳遞給avr-ld。它使得連接器ld連接時刪除不用的段。
這樣,由於每一個函數自成一段(便可以看做函數=段),若是有某個函數未被任何函數/段調用,則ld不會連接它。
AVR Studio 4:
Edit Configuration Options – Custom Options – [All Files] – add -ffunction-sections
– [Linker Options] – add -Wl,--gc-sectionsvim
http://songzhangzhang.blog.163.com/blog/static/69401981201141321641323/bash
g++: error: unrecognized option ‘--gc-sections’函數
注意:若不添加這些選項的話,則默認是不連接未調用的函數的ui
testlib.cpp:spa
#include "testlib.h" void MyFile::TestLibA() { ▸ cout<<"In MyFile::TestLibA()"<<endl; } int my_add(int x,int y) { ▸ return x+y; }
testlib.h.net
#include <stdlib.h> #include <iostream> using namespace std; class MyFile { ▸ public: ▸ ▸ static void TestLib() ▸ ▸ { ▸ ▸ ▸ cout<<"In MyFile::TestLib()"<<endl; ▸ ▸ } ▸ ▸ void TestLibA(); }; int my_add(int x,int y);
main.cppdebug
#include "testlib.h" int main() { ▸ MyFile::TestLib(); ▸ return 0; }
libtestlib.a的編譯:blog
g++ -c -ffunction-sections -fdata-sections testlib.cpp
yingc@yingc:~/gcyin/test/tmp/csdn$ !ar
ar crv libtestlib.a testlib.o
a - testlib.o
g++ -c -ffunction-sections -fdata-sections main.cppip
yingc@yingc:~/gcyin/test/tmp/csdn$ g++ -Wl,--gc-sections main.o -L. -ltestlib -static -o main yingc@yingc:~/gcyin/test/tmp/csdn$ !nm nm libtestlib.a | grep my_add 00000000 T _Z6my_addii yingc@yingc:~/gcyin/test/tmp/csdn$ nm main | grep my_add yingc@yingc:~/gcyin/test/tmp/csdn$
編譯爲動態庫的方法:
yingc@yingc:~/gcyin/test/tmp/csdn$ g++ -shared -fPIC -o libtestlib.so testlib.o yingc@yingc:~/gcyin/test/tmp/csdn$ ls libtestlib.so main.cpp testlib.cpp testlib.h testlib.o yingc@yingc:~/gcyin/test/tmp/csdn$ vim main.cpp [1]+ Stopped vim main.cpp yingc@yingc:~/gcyin/test/tmp/csdn$ yingc@yingc:~/gcyin/test/tmp/csdn$ yingc@yingc:~/gcyin/test/tmp/csdn$ ls libtestlib.so main.cpp testlib.cpp testlib.h testlib.o yingc@yingc:~/gcyin/test/tmp/csdn$ gcc -c main.cpp yingc@yingc:~/gcyin/test/tmp/csdn$ ll total 48 drwxrwxr-x 2 yingc yingc 4096 5月 28 10:49 ./ drwxrwxr-x 17 yingc yingc 4096 5月 28 10:32 ../ -rwxrwxr-x 1 yingc yingc 6990 5月 28 10:48 libtestlib.so* -rw-rw-r-- 1 yingc yingc 520 5月 28 10:49 main.cpp -rw-r--r-- 1 yingc yingc 12288 5月 28 10:49 .main.cpp.swp -rw-rw-r-- 1 yingc yingc 2176 5月 28 10:49 main.o -rw-rw-r-- 1 yingc yingc 546 5月 28 10:48 testlib.cpp -rw-rw-r-- 1 yingc yingc 635 5月 28 10:47 testlib.h -rw-rw-r-- 1 yingc yingc 1448 5月 28 10:38 testlib.o yingc@yingc:~/gcyin/test/tmp/csdn$ g++ -o main main.o -L. -ltestlib yingc@yingc:~/gcyin/test/tmp/csdn$ file main main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x02a9f9e7e9c76d2c9b277aff913bf5387a8d7f8d, not stripped yingc@yingc:~/gcyin/test/tmp/csdn$ ls libtestlib.so main main.cpp main.o testlib.cpp testlib.h testlib.o yingc@yingc:~/gcyin/test/tmp/csdn$ ./main In MyFile::TestLib() yingc@yingc:~/gcyin/test/tmp/csdn$
注意:
一、對於代碼中未使用到的全局變量,該選項生成的elf文件中,bss段仍是會佔用內存空間。實例(注意觀察bss段的size大小):
main.c中加入char g_ttt[5*1024*1024];全局變量以前:
There are 37 section headers, starting at offset 0x27f2820: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .reset PROGBITS 00000000 010000 0010c0 00 AX 0 0 16 [ 2] .bootimage_func PROGBITS 000010c0 5d0000 000000 00 W 0 0 1 [ 3] .power_sleep_func PROGBITS 000010c0 5d0000 000000 00 W 0 0 1 [ 4] .power_suspend_fu PROGBITS 000010c0 5d0000 000000 00 W 0 0 1 [ 5] .init PROGBITS 000010c0 0110c0 000018 00 AX 0 0 4 [ 6] .text PROGBITS 000010d8 0110d8 4644ec 00 AX 0 0 4 [ 7] .init.text PROGBITS 004655c4 4755c4 0000c0 00 AX 0 0 4 [ 8] .fini PROGBITS 00465684 475684 000018 00 AX 0 0 4 [ 9] .rodata PROGBITS 004656a0 4756a0 0d9ed0 00 A 0 0 8 [10] .ARM.extab PROGBITS 0053f570 54f570 0097e8 00 A 0 0 4 [11] .ARM.exidx ARM_EXIDX 00548d58 558d58 0197f8 00 AL 6 0 4 [12] .eh_frame PROGBITS 00562550 572550 000048 00 A 0 0 4 [13] .init_array INIT_ARRAY 00562598 572598 000020 00 WA 0 0 4 [14] .fini_array FINI_ARRAY 005625b8 5725b8 000004 00 WA 0 0 4 [15] .jcr PROGBITS 005625bc 5725bc 000004 00 WA 0 0 4 [16] .data PROGBITS 005625c0 5725c0 0596e8 00 WA 0 0 8 [17] .tlb PROGBITS 005bbca8 5cbca8 004358 00 WA 0 0 4 [18] .bss NOBITS 005c0000 5d0000 8772e0 00 WA 0 0 32 [19] .mmap PROGBITS 00e372e0 5d0000 000000 00 W 0 0 1 [20] .stack NOBITS 00e372e0 5d0000 004400 00 WA 0 0 1
加入以後:
aa