NASM elf 文件大小差异(部分大小写字母)

Ego*_*kov 2 linux compiler-construction assembly nasm binary-size

我在debian linux下用汇编写了一个简单的“Hello world”:

; Define variables in the data section
SECTION .data
    hello:     db 'Hello world!',10
    helloLen:  equ $-hello

; Code goes in the text section
SECTION .text
GLOBAL _start 

_start:
    mov eax,4            ; 'write' system call = 4
    mov ebx,1            ; file descriptor 1 = STDOUT
    mov ecx,hello        ; string to write
    mov edx,helloLen     ; length of string to write
    int 80h              ; call the kernel

    ; Terminate program
    mov eax,1            ; 'exit' system call
    mov ebx,0            ; exit with error code 0
    int 80h              ; call the kernel
Run Code Online (Sandbox Code Playgroud)

组装后

nasm -f elf64 hello.asm -o hello.o
ld -o hello hello.o.
Run Code Online (Sandbox Code Playgroud)

我得到了一个9048字节的二进制文件。

然后我更改了代码中的两行: from .datato.DATA.textto .TEXT

SECTION .DATA
SECTION .TEXT
Run Code Online (Sandbox Code Playgroud)

并得到一个4856字节的二进制文件。
将它们更改为

SECTION .dAtA
SECTION .TeXt
Run Code Online (Sandbox Code Playgroud)

也生成了4856字节的二进制文件。

NASM 被声明为不区分大小写的编译器。那有什么区别呢?

ric*_*ici 5

您可以自由地为 ELF 节使用任何您喜欢的名称,但如果您不使用标准名称,则您有责任指定节标志。(如果您使用标准名称,则可以利用这些名称的默认标志设置。)节名称区分大小写,并且.dataNASM.text知道 和 。.DATA.dAta等不是,并且没有任何东西可以将这些部分彼此区分开,从而允许ld将它们组合成单个段。

这会自动使您的可执行文件变小。对于.text和 的标准标志.data,其中一个是只读的,另一个是读写的,这意味着它们不能放置在同一内存页中。在您的示例程序中,这两个部分都非常小,因此它们可以放入单个内存页面中。因此,使用非标准名称会使可执行文件缩小一页,但其中一个部分的可写性将不正确。