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

  

AVR/GCC设置不链接未调用的函数

http://blog.csdn.net/shevsten/article/details/7049688

在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-sections

gcc的-ffunction-sections和-fdata-sections选项与ld的--gc-sections选项

http://songzhangzhang.blog.163.com/blog/static/69401981201141321641323/

How to remove unused C/C++ symbols with GCC and ld?

g++: error: unrecognized option ‘--gc-sections’

注意:若不添加这些选项的话,则默认是不链接未调用的函数的

testlib.cpp:

#include "testlib.h"

void MyFile::TestLibA()
{
▸ cout<<"In MyFile::TestLibA()"<<endl;
} int my_add(int x,int y)
{
▸ return x+y;
}

testlib.h

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

#include "testlib.h"

int main()
{
▸ MyFile::TestLib(); ▸ return 0;
}

libtestlib.a的编译:

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

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$

注意:

1、对于代码中未使用到的全局变量,该选项生成的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

-ffunction-sections -Wl,--gc-sections的更多相关文章

  1. 史上最简洁的UITableView Sections 展示包含NSDicionary 的NSArray

    这个最典型的就是电话本,然后根据A-Z分组, 当然很多例子,不过现在发现一个很简洁易懂的: 1. 准备数据,定义一个dictionary来显示所有的内容,这个dictionary对应的value全是数 ...

  2. Linker scripts之SECTIONS

    1 Purpose The linker script describes how the sections in the input files should be mapped into the ...

  3. 链接脚本(Linker Script)用法解析(一) 关键字SECTIONS与MEMORY

    1.MEMORY关键字用于描述一个MCU ROM和RAM的内存地址分布(Memory Map),MEMORY中所做的内存描述主要用于SECTIONS中LMA和VMA的定义. 2.SECTIONS关键字 ...

  4. 【小丸类库系列】Word操作类

    using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Dr ...

  5. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探

    更新 1.如果看不懂本文,或者比较困难,先别着急问问题,我单写了一个关于依赖注入的小Demo,可以下载看看,多思考思考注入的原理: https://github.com/anjoy8/BlogArti ...

  6. uboot makefile构建分析-续

    前言 这篇博文是 uboot makefile构建分析的续篇,继续分析uboot构建u-boot.bin的过程 构建u-boot.bin过程分析 makefile一开始,就是确定链接脚本.在构建ubo ...

  7. MySqlDBHelper数据库连接

    这里是本人在工作中用到,希望给大家帮助 public class MySqlDBHelper { //获取一个记录器 private static readonly log4net.ILog log ...

  8. Linux平台Makefile文件的编写基础篇

    目的:        基本掌握了 make 的用法,能在Linux系统上编程. 环境:        Linux系统,或者有一台Linux服务器,通过终端连接.一句话:有Linux编译环境. 准备: ...

  9. make的使用和Makefile规则和编程及其基本命令(简单)

      转自:http://blog.chinaunix.net/uid-23929712-id-2650328.html   概述: make从Makefile中文件中获取模块间的依赖关系,判断哪些文件 ...

  10. C# 合并及拆分Word文档

    本文简要分析一下如何如何使用C#简单实现合并和拆分word文档.平时我们在处理多个word文档时,可能会想要将两个文档合并为一个,或者是将某个文档的一部分添加到另一个文档中,有的时候也会想要将文档拆分 ...

随机推荐

  1. GNU make 总结 (五)

    一.使用make更新静态库 静态库文件是一些.o文件的集合,在Linux中使用ar工具对它进行维护管理.一个静态库通常由多个.o文件组成,这些.o文件可独立的被作为一个规则的目标,库成员作为目标时需要 ...

  2. 为什么Linux的fdisk分区时第一块磁盘分区的First Sector是2048?

    这个问题曾经困扰我很久,在了解了MBR之后,我曾认为第一块分区之前为一个block.但是用fdisk查看是2048,一直不了解其中的缘由,今天查了一下资料,大概了解了,其中的细节留着慢慢去了解. 最直 ...

  3. Java团队项目总结

    Java团队项目总结 1.项目实现情况 项目概述: 我们团队项目准备实现一个有关于大富翁有的游戏程序. 大富翁游戏,以经营权为主要的游戏方式,通过购买经营权与架构经营的星级服务来获得最大的利益,当其他 ...

  4. 15、android 用toast实现简单的进度显示

    if(mtoast!=null) { mtoast.setText(progress); } else { mtoast=Toast.makeText(getApplicationContext(), ...

  5. merry Christmas

    圣诞节的来历 圣诞节这个名称是基督弥撒的缩写. 弥撒是教会的一种礼拜仪式. 1.耶诞节是一个宗节,我们把它当作耶苏的诞辰来庆祝,因而又名耶诞节.这一天,世界所有的基督教会都举行特别的礼拜仪式.但是有很 ...

  6. vi中正则表达式的使用

    在当前行中删除从aa到zz的所有字符 :s/aa.*zz//在整个文件用and代替所有的&字符:1,$s/&/and/g在每一行的首行插入字符串new:1,$s/^/new/g在第二行 ...

  7. hdu 1028 Ignatius and the Princess III

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题目大意:3=1+1+1=1+2=3 :4=4=1+1+1+1=1+2+1=1+3:所以3有3种 ...

  8. Hibernate exercise 54

    针对马士兵的Hibernate讲解第54讲的练习: 1) 学生.课程.分数的设计,并用Hibernate操作 在实际中,一般是先手动写SQL(可以优化)去创建表和关系,再设置Hibernate配置为u ...

  9. Linux VPS 免费管理面板推荐

    现在各种国内外VPS,云主机横行,越来越多的站长接受在VPS上建站,很多VPS主机售价便宜,性能优秀,但都是基于linux系统的,如openvz的主机,linux服务器系统主要是通过shell命令行来 ...

  10. switch语句的使用,非常好

    这是谭浩强课本上枚举类型的例子,但是我贴这个例子的代码不是因为枚举类型,是因为这个代码使用switch语句用得非常好,值得一贴. 题目是这样的:有红.黄.蓝.白.黑5中颜色的球若干,依次取出3个球,求 ...