1

I just started learning assembly and want to make a really tiny hello world program. After compiling with NASM and then linking the executable size is 4.5 kb. This is odd as I though the assembler basically just substitutes the instructions with binary equivalants. If somebody could tell my how to decrease the size or just a bit on how assemblers work it would be appreciated.

Code

; hello.asm

section .text:

global _start
_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, 12
    int 0x80

    mov eax, 1
    mov ebx, 0
    int 0x80

section .data:
    msg: db "hello world"

Compilation

$ nasm -f elf32 hello.asm -o hello.o
$ ld -m elf_i386 hello.o -o hello

readelf -e output

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x8049000
  Start of program headers:          52 (bytes into file)
  Start of section headers:          4332 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         2
  Size of section headers:           40 (bytes)
  Number of section headers:         5
  Section header string table index: 4

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08049000 001000 000021 00  AX  0   0 16
  [ 2] .symtab           SYMTAB          00000000 001024 000080 10      3   4  4
  [ 3] .strtab           STRTAB          00000000 0010a4 000027 00      0   0  1
  [ 4] .shstrtab         STRTAB          00000000 0010cb 000021 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  p (processor specific)

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x08048000 0x08048000 0x00074 0x00074 R   0x1000
  LOAD           0x001000 0x08049000 0x08049000 0x00021 0x00021 R E 0x1000

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .text 
  • 1
    Most of the file is padding because `.data` section starts at a 4096 byte aligned address. Don't worry about it and use the `size` utility` to find the true size of the program. – fuz Nov 06 '21 at 09:59
  • Is there any way to remove this padding – ZeroSkill Gaming Nov 06 '21 at 10:00
  • Yes, but then you don't have the benefit of memory protection. The easiest way is to just stick everything into one section and make it read/write/executable. – fuz Nov 06 '21 at 10:02
  • I tried doing removing the section but the padding still remains. Is there not any method of removing it with assembler/linker options. – ZeroSkill Gaming Nov 06 '21 at 10:18
  • I don't know, could you perhaps post the output of `readelf -e` on the offending binary? Just [edit] your question and add it. – fuz Nov 06 '21 at 10:22
  • Hm that's weird. It loads the ELF headers themselves into memory. It doesn't do that on my system. Not sure what the problem is. But anyway, don't worry about this too much. – fuz Nov 06 '21 at 10:30
  • 1
    https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html – ecm Nov 06 '21 at 10:36
  • In this case your data doesn't need to be writeable, so you can just put it in `.text`, with no `.data` section at all. See also [Minimal executable size now 10x larger after linking than 2 years ago, for tiny programs?](https://stackoverflow.com/q/65037919) for more about `ld` defaults that affect this. – Peter Cordes Nov 06 '21 at 16:16

0 Answers0