【问题标题】:How to use ld --gc-sections with OUTPUT_FORMAT(binary)如何将 ld --gc-sections 与 OUTPUT_FORMAT(binary) 一起使用
【发布时间】:2017-01-30 07:33:22
【问题描述】:

如果我的 GNU ld 链接器脚本有 OUTPUT_FORMAT(binary) ld --gc-sections 命令行标志似乎被忽略:

$ cat gcs.c extern void magic(void); void callmagic(void) { magic(); } int uanswer = 42; int main(void) { return 0; } $ cat ofbin.scr OUTPUT_FORMAT(binary) OUTPUT_ARCH(i386) ENTRY(main) SECTIONS { . = 0x10000; .text : { *(.text*) } .data : { *(.data*) *(.rodata*) } .bss : { *(.bss*); } } $ cat ofelf.scr OUTPUT_FORMAT(elf32-i386) OUTPUT_ARCH(i386) ENTRY(main) SECTIONS { . = 0x10000; .text : { *(.text*) } .data : { *(.data*) *(.rodata*) } .bss : { *(.bss*); } } $ gcc -m32 -nostdlib -nostartfiles -nodefaultlibs gcs.c -ffunction-sections -fdata-sections -Wl,--gc-sections,--print-gc-sections -Wl,-T,ofelf.scr /usr/bin/ld: Removing unused section '.text.callmagic' in file '/tmp/ccSFoPYg.o' /usr/bin/ld: Removing unused section '.data.uanswer' in file '/tmp/ccSFoPYg.o' /usr/bin/ld: Removing unused section '.eh_frame' in file '/tmp/ccSFoPYg.o' $ gcc -m32 -nostdlib -nostartfiles -nodefaultlibs gcs.c -ffunction-sections -fdata-sections -Wl,--gc-sections,--print-gc-sections -Wl,-T,ofbin.scr /tmp/cceXCw2b.o: In function `callmagic': gcs.c:(.text.callmagic+0x7): undefined reference to `magic' collect2: error: ld returned 1 exit status

我在 Ubuntu 14.04 上使用 GNU ld 2.24 和 GCC 4.8.4。

我怎样才能使这个.c 文件编译并与OUTPUT_FORMAT(binary)--gc-sections 消除的callmagic 代码链接?

【问题讨论】:

    标签: linker ld


    【解决方案1】:

    看起来这是不可能的,GNU ld 会忽略 --gc-sectionsOUTPUT_FORMAT(binary)

    一种可能的解决方法是使用OUTPUT_FORMAT(elf32-i386) 并对输出 ELF 文件进行后处理,例如objcopy -O binary a.out a.out.bin

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2015-10-09
      • 2020-08-27
      • 2015-10-06
      • 1970-01-01
      相关资源
      最近更新 更多