免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7668 | 回复: 6
打印 上一主题 下一主题

[C] 请问GCC有没有办法只连接用到的函数? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-27 13:18 |只看该作者 |倒序浏览
因为嵌入式编程中,比较在意可执行代码的大小。

我用 --ffunction-sections --fdata-sections选项给让gcc将每个函数都放到一个独立的sections里面,然后用

  1. /* the program code is stored in the .text section, which goes to Flash */

  2.     .text :

  3.     {

  4.             . = ALIGN(4);

  5.             

  6.         *(.text)                   /* remaining code */

  7.         *(.text.*)                   /* remaining code */

  8.         *(.rodata)                 /* read-only data (constants) */

  9.         *(.rodata*)

  10.         *(.glue_7)

  11.         *(.glue_7t)



  12.             . = ALIGN(4);

  13.             _etext = .;

  14.             /* This is used by the startup in order to initialize the .data secion */

  15.             _sidata = _etext;

  16.     } >FLASH

  17.    
复制代码

这样的连接脚本控制连接过程,但gcc还是将每一个函数都连接到了最后的结果中去了,有没有办法让gcc只连接需要的函数呢?谢谢。

论坛徽章:
0
2 [报告]
发表于 2009-09-27 13:24 |只看该作者
LDFLAGS += --gc-sections

论坛徽章:
0
3 [报告]
发表于 2009-09-27 13:28 |只看该作者

回复 #2 prc 的帖子

加了,好像没有效果。

  1. Building target: stm32test.elf
  2. Invoking: ARM Sourcery Linux GCC C Linker
  3. arm-none-eabi-gcc -T"/home/ifree/workspace/stm32test/src/usr/stm32f10x_flash_extsram.ld" -nostartfiles -nodefaultlibs -Wl,-Map,stm32test.map --gc-sections -mcpu=cortex-m3 -mthumb -o"stm32test.elf"  ./src/usr/main.o ./src/usr/stm32f10x_it.o  ./src/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.o ./src/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.o  ./src/Libraries/CMSIS/Core/CM3/core_cm3.o ./src/Libraries/CMSIS/Core/CM3/system_stm32f10x.o  ./src/Libraries/CMSIS/Core/CM3/startup/gcc/startup_stm32f10x_md.o   
  4. Finished building target: stm32test.elf
复制代码


这样连接后,我检查得到的lst文件,还是全部的函数都在里面。

[ 本帖最后由 2eye 于 2009-9-27 13:30 编辑 ]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
4 [报告]
发表于 2009-09-27 14:14 |只看该作者

回复 #3 2eye 的帖子

应该看stm32test.map这个文件吧
还有一个办法,就是直接nm最终的elf,然后grep,就可以知道不使用的函数有没有被连接进去。
这个肯定是ld的一个参数而不是gcc的参数,看一下ld的info吧。

论坛徽章:
0
5 [报告]
发表于 2009-09-27 14:14 |只看该作者
这个也许是你用的LD版本太老不支持

论坛徽章:
0
6 [报告]
发表于 2009-09-27 17:49 |只看该作者

回复 #4 koolcoy 的帖子

-Map,stm32test.map

这个是我在man中查到的-Map参数的含义:

  1. -M
  2.        --print-map
  3.            Print a link map to the standard output.  A link map provides information about the link, including the following:

  4.            *   Where object files are mapped into memory.

  5.            *   How common symbols are allocated.

  6.            *   All archive members included in the link, with a mention of the symbol which caused the archive member to be brought in.

  7.            *   The values assigned to symbols.

  8.                Note  -  symbols  whose values are computed by an expression which involves a reference to a previous value of the same symbol
  9.                may not have correct result displayed in the link map.  This is because the linker  discards  intermediate  results  and  only
  10.                retains the final value of an expression.  Under such circumstances the linker will display the final value enclosed by square
  11.                brackets.  Thus for example a linker script containing:

  12.                           foo = 1
  13.                           foo = foo * 4
  14.                           foo = foo + 8

  15.                will produce the following output in the link map if the -M option is used:

  16.                           0x00000001                foo = 0x1
  17.                           [0x0000000c]                foo = (foo * 0x4)
  18.                           [0x0000000c]                foo = (foo + 0x8)

  19.                See Expressions for more information about expressions in linker scripts.


  20.      -Map mapfile
  21.            Print a link map to the file mapfile.  See the description of the -M option, above.
复制代码

这样说来,map文件是连接器生成的文件,应该不是用于控制连接过程的。

论坛徽章:
0
7 [报告]
发表于 2009-09-27 20:51 |只看该作者
谢谢楼上诸位,问题已经解决了。
应该
LDFLAGS += -Wl,--gc-sections
否则参数没有传递给连接器。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP