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

来源:互联网 发布:中途岛级航空母舰数据 编辑:程序博客网 时间:2024/05/16 15:22
-ffunction-sections, -fdata-sections会使compiler为每个function和data item分配独立的section。 --gc-sections会使ld删除没有被使用的section。

链接操作以section作为最小的处理单元,只要一个section中有某个符号被引用,该section就会被放入output中。

这些选项一起使用会从最终的输出文件中删除所有未被使用的function和data, 只包含用到的unction和data。

示例:

C code:

 

C代码  收藏代码
  1. struct person {  
  2.     int age;  
  3.     int no;  
  4. };  
  5.   
  6. int plus_one(int no)  
  7. {  
  8.     return no + 1;  
  9. }  
  10.   
  11. int minus_one(int no)  
  12. {  
  13.     return no - 1;  
  14. }  

 

Run 'gcc -S -ffunction-sections -fdata-sections' produce:

 

Assembly代码  收藏代码
  1.     .file   "sec.c"  
  2.     .section    .text.plus_one,"ax",@progbits  
  3. .globl plus_one  
  4.     .type   plus_one, @function  
  5. plus_one:  
  6.     pushl   %ebp  
  7.     movl    %esp, %ebp  
  8.     movl    8(%ebp), %eax  
  9.     addl    $1, %eax  
  10.     popl    %ebp  
  11.     ret  
  12.     .size   plus_one, .-plus_one  
  13.     .section    .text.minus_one,"ax",@progbits  
  14. .globl minus_one  
  15.     .type   minus_one, @function  
  16. minus_one:  
  17.     pushl   %ebp  
  18.     movl    %esp, %ebp  
  19.     movl    8(%ebp), %eax  
  20.     subl    $1, %eax  
  21.     popl    %ebp  
  22.     ret  
  23.     .size   minus_one, .-minus_one  
  24.     .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3"  
  25.     .section    .note.GNU-stack,"",@progbits 

0 0
原创粉丝点击