NASM elf file size difference with uppercase and lowercase letters in section

132 Views Asked by At

I wrote a simple "Hello world" in assembly under debian linux:

; 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

After assembling

nasm -f elf64 hello.asm -o hello.o
ld -o hello hello.o.

I got a 9048 byte binary.

Then I changed two lines in the code: from .data to .DATA and .text to .TEXT:

SECTION .DATA
SECTION .TEXT

and got a 4856 byte binary.
Changing them to

SECTION .dAtA
SECTION .TeXt

produced a 4856 byte binary too.

NASM is declared to be a case-insensitive compiler. What is the difference then?

1

There are 1 best solutions below

2
rici On

You're free to use whatever names you like for ELF sections, but if you don't use standard names, it becomes your responsibility to specify the section flags. (If you use standard names, you get to take advantage of default flag settings for those names.) Section names are case-sensitive, and .data and .text are known to NASM. .DATA, .dAta, etc. are not, and there is nothing which distinguishes these sections from each other, allowing ld to combine them into a single segment.

That automatically makes your executable smaller. With the standard flags for .text and .data, one of those is read-only and the other is read-write, which means that they cannot be placed into the same memory page. In your example program, both sections are quite small, so they could fit in a single memory page. Thus, using non-standard names makes your executable one page smaller, but one of the sections will have incorrect writability.