标签和dd声明在NASM中如何工作?什么是C等效项? [英] How do labels and dd declarations work in NASM? What's the C equivalent?

查看:128
本文介绍了标签和dd声明在NASM中如何工作?什么是C等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解像这样的一些nasm惯用语的C等效词:

%define CONSTANT1 1
%define CONSTANT2 2

1) section name_section data align=N
    v1: dd 1.2345678
    v2: dd 0x12345678
    v3: dd 32767
    v4:
    v5: dd 1.0
    v6:
        dd 1.0, 2.0, 3.0, 4.0,
        dd 5.0, 6.0, 7.0, 8.0

2) section name_section bss align=N
    v7:
        resd 1

3) global _function_name@0
    section name_section code align=N
    _function_name@0:
        ...

4) global _g_structure1
    global _g_structure2
    section name_section data align=N
    _g_structure1:
        dw 01h
        dw 2
    _g_structure2:
        dd CONSTANT1
        dd CONSTANT2

5) section section_name code align=N
    function_name:
        ...

nasm文档此处解决方案

dd存储由参数指定的DWORDS序列.因此,dd 1会将4字节的值0x00000001存储在当前位置(由于它的目标是小端字节体系结构,因此最终将以字节0x01 0x00 0x00 0x00结束).

部分通常不会直接在C语言中公开-它更多是由编译器,链接器和运行时加载器处理的较低级别的问题.因此,通常您的工具链将处理代码和数据在部分中的正确分配.例如,编译器会将实际的汇编代码放入.text部分中,并将静态初始化的数据放入.data部分中,最后将未初始化或零初始化的静态分配的数据放入.bss部分中,依此类推. .这些细节并不是C本身的一部分,并且会因平台和可执行文件格式的不同而有所差异(例如,并非所有平台都具有相同类型的节).

另一方面,在使用汇编时,您需要对节有更多的了解.例如,如果您具有可变数据,则重要的是它结尾的代码段与代码不同,因为您不希望遇到只读的.text部分,也不希望自行修改代码的误报等.

节对齐是运行时加载程序的指令,告诉它节的最低要求对齐.您可以使用某些编译器或特定于平台的选项在C代码中对此进行影响-例如如果您请求静态分配的数组的对齐方式为32,则.data节可能会提升为至少32字节的对齐方式. C没有实际要求这种对齐的标准方法,但是您可以使用特定于平台的扩展,例如 posix_memalign ,gcc的 alignas 来执行此操作一种标准方式.

@N后缀是 stdcall name mangling 的结果.

您可以在nasm中的GLOBAL指令的帮助下声明全局标签.正如Peter所指出的那样,这仅会修改随后声明的标签的属性,而实际上并没有声明标签本身(仍然以通常的方式完成).该指令还具有其他格式特定的选项,例如, ,将导出的符号声明为函数.

I'm trying to understand what'd be the C equivalent of some nasm idioms like these ones:

%define CONSTANT1 1
%define CONSTANT2 2

1) section name_section data align=N
    v1: dd 1.2345678
    v2: dd 0x12345678
    v3: dd 32767
    v4:
    v5: dd 1.0
    v6:
        dd 1.0, 2.0, 3.0, 4.0,
        dd 5.0, 6.0, 7.0, 8.0

2) section name_section bss align=N
    v7:
        resd 1

3) global _function_name@0
    section name_section code align=N
    _function_name@0:
        ...

4) global _g_structure1
    global _g_structure2
    section name_section data align=N
    _g_structure1:
        dw 01h
        dw 2
    _g_structure2:
        dd CONSTANT1
        dd CONSTANT2

5) section section_name code align=N
    function_name:
        ...

The nasm documentation here and here didn't clarify too much. Guess my questions are:

  • How dd and similars are interpreted?
  • It seems you can declare N sections of type {code, bss, data} with X bytes alignment, what's the meaning of that in C?
  • There are functions with the @N suffix, what's the meaning of that?
  • global... you declare global labels? in what scope? nasm files?
  • v4: is empty, what does that mean?

解决方案

dd stores a sequence of DWORDS given by the arguments. So dd 1 will store the 4-byte value 0x00000001 at the current location (since it's targeting a little endian architecture, you'll end up with the bytes 0x01 0x00 0x00 0x00).

Sections aren't generally exposed directly in C - it's more of a lower level concern handled by compilers, linkers and runtime loaders. So in general your toolchain will handle the proper allocation of your code and data into sections. For example, the compiler will put the actual assembled code into .text sections, and will put statically initialized data into .data sections, and finally will put uninitialized or zero-initialized statically allocated data into .bss sections, and so on. The details aren't really part of C itself and will vary by platform and executable format (for example, not all platforms have the same types of sections).

When using assembly, on the other hand, you need to be a bit more aware of sections. For example, if you have mutable data it is important that it ends up a different section than your code, since you don't want to run into read-only .text sections, or self-modifying-code false positives, etc.

The section alignment is a directive to the runtime loader that tells it the minimum required alignment for the section. You can impact this in your C code using some compiler or platform specific options - e.g. if you request a statically allocated array to have an alignment of 32, then the .data section may be promoted to at least 32-byte alignment. C doesn't have a standard way to actually request such alignment, but you can use platform specific extensions such as posix_memalign, gcc's aligned attribute, or even #pragma pack. C++11 on the other hand has alignas to do this in a standard way.

The @N suffix is a result of stdcall name mangling.

You can declare global labels with the help of the GLOBAL directive in nasm. As Peter point out, this only modifies the attributes of a subsequently declared label, and doesn't actually declare the label itself (which is still done in the usual way). This directive has other format-specific options which let you, for example, declare your exported symbol as a function.

这篇关于标签和dd声明在NASM中如何工作?什么是C等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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