简体   繁体   English

每使用一个 label 地址,就会向 .o 文件的总大小增加 24 个字节(加上 8 个字节(总共 32 个字节))

[英]Each label address used, adds 24 bytes more (plus 8 bytes (totalling 32 bytes)) to the .o file's total size

In my FASM project (Object), I'm trying to create a jump-table and I use dq for each jump address but there is a problem!在我的 FASM 项目(对象)中,我正在尝试创建一个跳转表并且我对每个跳转地址使用dq但有一个问题!
For each dq.jmp1 (jump address definition), 24 bytes more (plus 8 bytes for .jmp1 address (totalling 32 bytes)) will be added to my final.o file's total size!对于每个dq.jmp1 (跳转地址定义),我的 final.o 文件的总大小将增加 24 个字节(加上.jmp1地址的 8 个字节(总共 32 个字节))!
What is that extra 24 bytes?那额外的 24 个字节是什么? Is there any way to avoid it?有什么办法可以避免吗? This happens only in object file, not in executable!这只发生在 object 文件中,而不是可执行文件中!

Instead of 8 bytes for each jump address, it defines 32 bytes?它不是为每个跳转地址定义 8 个字节,而是定义 32 个字节? What is the problem?问题是什么?

format ELF64

section '.text'

func:
        lea     rax, [8*rax+.jmp_table]

 .jmp1:

 .jmp_table:
        dq .jmp1 ; 8 bytes + 24 bytes !!! (to .o total size)
        dq .jmp1 ; 8 bytes + 24 bytes !!! (to .o total size)   

But when I create an executable, each dq takes only 8 bytes (what I expect)...但是当我创建一个可执行文件时,每个dq只需要 8 个字节(我所期望的)......

format ELF64 EXECUTABLE

segment readable executable

func:
        lea     rax, [8*rax+.jmp_table]

 .jmp1:

 .jmp_table:
        dq .jmp1 ; 8-BYTE, no extra 24 bytes to .o total size
        dq .jmp1 ; 8-BYTE, no extra 24 bytes to .o total size

Remember that a .o file is not just a flat image of the code and data you assembled;请记住, .o文件不仅仅是您组装的代码和数据的平面图像; it also contains metadata, such as relocations .它还包含元数据,例如relocations

Since dq.jmp1 references a symbol whose absolute address will not be known until after linking, it requires a relocation entry.由于dq.jmp1引用了一个符号,其绝对地址只有在链接之后才能知道,因此它需要一个重定位条目。 The link above shows that ELF on x86-64 uses Elf64_Rela relocation entries, which are 24 bytes.上面的链接显示 x86-64 上的 ELF 使用Elf64_Rela重定位条目,这是 24 个字节。 So the 8 bytes of actual data, plus 24 bytes of metadata, accounts exactly for the 32-byte increase in file size.因此,实际数据的 8 个字节加上 24 个字节的元数据恰好说明了文件大小增加 32 个字节。 (It could be more or less in other instances, eg perhaps due to padding for alignment requirements.) (在其他情况下可能或多或少,例如可能是由于 alignment 要求的填充。)

Once linking is complete, the relocation metadata is not included in the executable, so the executable size increased only by the size of the actual data.一旦链接完成,重定位元数据就不会包含在可执行文件中,因此可执行文件的大小只会增加实际数据的大小。

So what you're seeing is completely normal and there is nothing to avoid.所以你所看到的是完全正常的,没有什么可以避免的。 The jump table entries will in fact occupy 8 bytes in program memory, and the arithmetic of your lea rax, [8*rax+.jmp_table] remains correct.跳转表条目实际上将在程序 memory 中占用 8 个字节,并且您的lea rax, [8*rax+.jmp_table]的算法仍然正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM