Binutils LD创建巨大的文件 [英] Binutils LD creates huge files
问题描述
我正在尝试创建尽可能小的ELF.我创建了这样的测试文件(NASM语法):
I'm trying to create as small ELF as possible. I created a test file like this (NASM syntax):
SECTION .text
dd 0xdeadbeef
使用此链接描述文件:
SECTIONS {
.text : {
*(.text)
}
}
然后我检查了平面二进制文件和ELF的大小,构建了两种方法:
Then I checked sizes of flat binary and ELFs built two ways:
nasm -f bin -o test test.asm
它是平面二进制,所以是4个字节.
It's flat binary, so 4 bytes.
nasm -f elf -o test.o test.asm
i686-elf-ld -Tlinker.ld test.o -o test
我希望最大可以达到500字节左右,但是生成的文件长4396字节!但是,有一个名为--strip-all的选项可以使该文件更小.
I'd expect something like 500 bytes max, but the resulting file is 4396 bytes long! There is an option however, named --strip-all, that could make this file smaller.
i686-elf-ld -Tlinker.ld test.o -o test --strip-all
4244字节.仍然很大.
4244 bytes. Still huge.
为什么LD生成这么大的文件?有没有办法缩小它?
Why is LD generating so big files? Is there a way to make it smaller?
推荐答案
链接器会将您的文本部分与最近的页面边界进行页面对齐,以便需求分页.
The linker is page aligning your text section to the nearest page boundary so that demand paging can be used.
$ objdump --headers -f test
test: file format elf32-i386
architecture: i386, flags 0x00000102:
EXEC_P, D_PAGED
start address 0x00000000
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000004 00000000 00000000 00001000 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
请注意,文本部分的对齐"列设置为4KB.由于将对齐方式设置为4Kb,并且正在使用按需分页(D_PAGED),因此.text节位于文件的4Kb中.您的文字部分只有4个字节长.
Notice the "Align" column of the text section is set to 4KB. Because of the alignment is set to 4Kb and demand paging is in use (D_PAGED), the .text section is located 4Kb into the file. Your text section is only 4 bytes long.
与 -n
链接以禁用按需分页:
Link with -n
to disable demand paging:
$ ld -Tlinker.ld test.o -o test --strip-all -n
$ objdump --headers -f test
test: file format elf32-i386
architecture: i386, flags 0x00000002:
EXEC_P
start address 0x00000000
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000004 00000000 00000000 00000060 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
$ ls -l test
-rwxrwxr-x 1 mikel mikel 240 Apr 15 12:31 test
这篇关于Binutils LD创建巨大的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!