如何设置.data节的对齐方式? [英] How to set the alignment for the .data section?

查看:159
本文介绍了如何设置.data节的对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NASM的 .data 部分中定义了以下变量:

I defined the following variables in the .data section in NASM:

section .data
    var1       DD   12345    ; int (4 bytes)

    var2       DB   'A'      ; char (1 byte)

    padding1   DB   123      ; 1 byte padding
    padding2   DB   123      ; 1 byte padding
    padding3   DB   123      ; 1 byte padding       

    var3       DQ   174.13   ; double (8 bytes)

为了使这些变量正确对齐, .data 部分必须对齐为8个字节.

In order for these variables to be correctly aligned, the .data section must be aligned to 8 bytes.

我相信 .data 部分的对齐方式是由链接器指定的.我正在使用Visual C ++ 2010链接器,如何使用此链接器设置 .data 部分的对齐方式?

I believe that the alignment for the .data section is specified by the linker. I am using the Visual C++ 2010 linker, how can I set the alignment for the .data section using this linker?

推荐答案

align指令适用于数据和代码.

The align directive works for data as well as code.

在汇编器的输出文件(MSVC链接器可以理解的格式的目标文件)中,它使用元数据表示所需的每个节对齐.

In the assembler's output file (an object file in the format the MSVC's linker can understand), it signals the required alignment of each section using metadata.

例如,如果您使用

section .data
align 1024*1024*2
foo: dd 1234
align 8       ; will assemble to 4 bytes of padding to reach the next multiple of 8
bar: dd 4567

对于该部分,目标文件的必需对齐将设置为2MiB.对于Win32目标文件,NASM甚至具有用于节对齐的特殊语法:
section .data data align=4

The object file will have its required-alignment for that section set to 2MiB. For win32 object files, NASM even has special syntax for section alignment:
section .data data align=4

ELF目标文件(Linux)的工作方式相同,每个部分都有必填项.

ELF object files (Linux) work the same way, with each section having a required-alignment.

您的目标文件(希望)最终没有填充到最多2MiB的填充,如果在链接后的某个段中有几个字节的其他段之后,则该段链接可能会在链接后出现.data在可执行文件中.

Your object file (hopefully) doesn't end up filled with up-to-2MiB of padding, bu it might after linking if it links after something else that has a few bytes in a section that goes into the same segment as .data in the executable.

但是,即使知道(或设置)节开头的最小对齐方式,汇编器仍可以在任何节中间的任意点支持2的幂次幂的align指令 . align指令不必位于节的开头.

But still, knowing (or setting) the minimum alignment of the start of a section, the assembler can support align directives of any power of 2 at any point in the middle of any section. The align directive doesn't have to be at the start of a section.

这篇关于如何设置.data节的对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆