2017-01-30 4 views
1

ld --gc-sectionsコマンドラインフラグを無視しているようだ:私はGNUを使用してい

$ 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でld 2.24とGCC 4.8.4。

はどうすればOUTPUT_FORMAT(binary)とし、--gc-sectionsによって排除callmagicのコードで、この.cファイルのコンパイルとリンクを行うことができますか?

答えて

0

これは不可能で、GNU ldは--gc-sectionsOUTPUT_FORMAT(binary)と無視しています。

可能な回避策は、OUTPUT_FORMAT(elf32-i386)を使用し、出力ELFファイルを後処理することです。 objcopy -O binary a.out a.out.binである。