-ffunction-sections, -fdata-sections会使compiler为每个function和data item分配独立的section。 --gc-sections会使ld删除没有被使用的section。
链接操作以section作为最小的处理单元,只要一个section中有某个符号被引用,该section就会被放入output中。
这些选项一起使用会从最终的输出文件中删除所有未被使用的function和data, 只包含用到的unction和data。
示例:
示例:
C code:
- struct person {
- int age;
- int no;
- };
- int plus_one(int no)
- {
- return no + 1;
- }
- int minus_one(int no)
- {
- return no - 1;
- }
Run 'gcc -S -ffunction-sections -fdata-sections' produce:
- .file "sec.c"
- .section .text.plus_one,"ax",@progbits
- .globl plus_one
- .type plus_one, @function
- plus_one:
- pushl %ebp
- movl %esp, %ebp
- movl 8(%ebp), %eax
- addl $1, %eax
- popl %ebp
- ret
- .size plus_one, .-plus_one
- .section .text.minus_one,"ax",@progbits
- .globl minus_one
- .type minus_one, @function
- minus_one:
- pushl %ebp
- movl %esp, %ebp
- movl 8(%ebp), %eax
- subl $1, %eax
- popl %ebp
- ret
- .size minus_one, .-minus_one
- .ident "GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3"
- .section .note.GNU-stack,"",@progbits