使用objcopy删除未使用的部分

时间:2016-10-05 11:48:16

标签: gcc ld objcopy

假设我有以下源文件:

// file.c:
void f() {}
void g() {}

我使用gcc -ffunction-sections将其编译为目标文件:

$ gcc -c -ffunction-sections file.c -o file.o
# It now has at least two sections: `.text.f' (with `f'), `.text.g' (with `g').

然后我尝试从对象文件中删除部分.text.g(带g):

$ objcopy --remove-section .text.g file.o
objcopy: stQOLAU8: symbol `.text.g' required but not present
objcopy:stQOLAU8: No symbols

那么,是否可以从目标文件中删除特定于功能的部分(使用-ffunction-sections编译)?

额外信息:

  1. file.o中符号的完整列表是:

    $ objdump -t file.o 
    
    file.o:     file format elf64-x86-64
    
    SYMBOL TABLE:
    0000000000000000 l    df *ABS*  0000000000000000 file.c
    0000000000000000 l    d  .text  0000000000000000 .text
    0000000000000000 l    d  .data  0000000000000000 .data
    0000000000000000 l    d  .bss   0000000000000000 .bss
    0000000000000000 l    d  .text.f        0000000000000000 .text.f
    0000000000000000 l    d  .text.g        0000000000000000 .text.g
    0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
    0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
    0000000000000000 l    d  .comment       0000000000000000 .comment
    0000000000000000 g     F .text.f        0000000000000007 f
    0000000000000000 g     F .text.g        0000000000000007 g
    
  2. 我的目标是从对象文件中删除一些与ld --gc-sections类似的部分。

  3. 或者是否有一些理论上的理由说明为什么这样的任务完全超出objcopy的范围并且只能用ld -r执行?

0 个答案:

没有答案