如何将 ld --gc-sections 与 OUTPUT_FORMAT(二进制)一起使用
How to use ld --gc-sections with OUTPUT_FORMAT(binary)
如果我的 GNU ld linker 脚本有 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。
如何使用 OUTPUT_FORMAT(binary)
和 callmagic 的代码编译 .c
文件并 link 由 [=16] 消除=]?
看起来这是不可能的,GNU ld 忽略 --gc-sections
和 OUTPUT_FORMAT(binary)
。
一种可能的解决方法是使用 OUTPUT_FORMAT(elf32-i386)
和 post 处理输出 ELF 文件,例如objcopy -O binary a.out a.out.bin
.
如果我的 GNU ld linker 脚本有 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。
如何使用 OUTPUT_FORMAT(binary)
和 callmagic 的代码编译 .c
文件并 link 由 [=16] 消除=]?
看起来这是不可能的,GNU ld 忽略 --gc-sections
和 OUTPUT_FORMAT(binary)
。
一种可能的解决方法是使用 OUTPUT_FORMAT(elf32-i386)
和 post 处理输出 ELF 文件,例如objcopy -O binary a.out a.out.bin
.