simple hello world exec is 10 kilobytes

3

I made a simple Hello World program in x86 nasm assembly and I don't understand why the final exec is almost 10Ko.

nasm version :

NASM version 2.13.03 compiled on Apr 1 2018

ld version :

GNU ld (GNU Binutils) 2.31.1

This is my source code :

    ; -------------------------------------------------------------
; Writes "Hello, World" to the console using only system calls.
; Runs on 64-bit Linux only.
; To assemble and run:
;
;     nasm -f elf64 hello.asm
;     ld hello.o -o hello
;     ./hello
;     or in one line
;     nasm -felf64 hello.asm && ld hello.o -o hello && ./hello
;
; -------------------------------------------------------------

%define newline 0xA
%define nullchar 0x0

%define SYS_WRITE 1 ; system callcode for write
%define SYS_EXIT 60 ; system callcode for exit

%define STD_OUT 1

section   .data
    message  db    "Hello, World!", newline, nullchar
    len_message  equ  $-message

section   .text
;we must export the entry point to the ELF linker or
;loader. They conventionally recognize _start as their
;entry point. Use ld -e foo to override the default.
global  _start

_start:
    call .print
    call .exit

.print:
    mov       rax, SYS_WRITE
    mov       rdi, STD_OUT            ; we write text in the shell
    mov       rsi, message            ; address of string to output
    mov       rdx, len_message        ; number of bytes
    syscall                           ; invoke kernel
    ret                               ; return

.exit:
    mov       rax, SYS_EXIT
    mov       rdi, 0                  ; exit code 0
    syscall                           ; invoke kernel

When I assemble it with nasm -f elf64 hello.asm, I get a hello.o file that have a size of 976 bytes.

When I link it with ld hello.o -o hello, I get a hello exec with a size of 8.9K (I get the size with ls -lh) .

Why the linking make the final exec so huge ? Even if I use the option to remove debug data (ld -s), I only loose 500 bytes.

this is the output from

readelf -a hello

:

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x401000
  Start of program headers:          64 (bytes into file)
  Start of section headers:          8656 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         4
  Size of section headers:           64 (bytes)
  Number of section headers:         7
  Section header string table index: 6

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .note.gnu.propert NOTE             0000000000400120  00000120
       0000000000000020  0000000000000000   A       0     0     8
  [ 2] .text             PROGBITS         0000000000401000  00001000
       0000000000000032  0000000000000000  AX       0     0     16
  [ 3] .data             PROGBITS         0000000000402000  00002000
       000000000000000f  0000000000000000  WA       0     0     4
  [ 4] .symtab           SYMTAB           0000000000000000  00002010
       0000000000000138  0000000000000018           5     9     8
  [ 5] .strtab           STRTAB           0000000000000000  00002148
       0000000000000048  0000000000000000           0     0     1
  [ 6] .shstrtab         STRTAB           0000000000000000  00002190
       000000000000003a  0000000000000000           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),
  l (large), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x0000000000000140 0x0000000000000140  R      0x1000
  LOAD           0x0000000000001000 0x0000000000401000 0x0000000000401000
                 0x0000000000000032 0x0000000000000032  R E    0x1000
  LOAD           0x0000000000002000 0x0000000000402000 0x0000000000402000
                 0x000000000000000f 0x000000000000000f  RW     0x1000
  NOTE           0x0000000000000120 0x0000000000400120 0x0000000000400120
                 0x0000000000000020 0x0000000000000020  R      0x8

 Section to Segment mapping:
  Segment Sections...
   00     .note.gnu.property 
   01     .text 
   02     .data 
   03     .note.gnu.property 

There is no dynamic section in this file.

There are no relocations in this file.

The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.

Symbol table '.symtab' contains 13 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000400120     0 SECTION LOCAL  DEFAULT    1 
     2: 0000000000401000     0 SECTION LOCAL  DEFAULT    2 
     3: 0000000000402000     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS hello.asm
     5: 0000000000402000     0 NOTYPE  LOCAL  DEFAULT    3 message
     6: 000000000000000f     0 NOTYPE  LOCAL  DEFAULT  ABS len_message
     7: 000000000040100a     0 NOTYPE  LOCAL  DEFAULT    2 _start.print
     8: 0000000000401026     0 NOTYPE  LOCAL  DEFAULT    2 _start.exit
     9: 0000000000401000     0 NOTYPE  GLOBAL DEFAULT    2 _start
    10: 000000000040200f     0 NOTYPE  GLOBAL DEFAULT    3 __bss_start
    11: 000000000040200f     0 NOTYPE  GLOBAL DEFAULT    3 _edata
    12: 0000000000402010     0 NOTYPE  GLOBAL DEFAULT    3 _end

No version information found in this file.

Displaying notes found in: .note.gnu.property
  Owner                 Data size   Description
  GNU                  0x00000010   NT_GNU_PROPERTY_TYPE_0
      Properties: x86 ISA needed: i486
assembly
x86
nasm
asked on Stack Overflow Oct 9, 2018 by MohamedLEGH • edited Oct 9, 2018 by MohamedLEGH

1 Answer

2

I think I found something.

In version 2.30 of ld a new flag was added and is activated by default

Changes in 2.30:

  • Add -z separate-code to generate separate code PT_LOAD segment.

source

If I link with ld -z noseparate-code hello.o -o hello

the size of the binary is 1032 bytes, which is more reasonable. I don't know what is PT_LOAD but it does not look essential to run a hello world program.

answered on Stack Overflow Oct 9, 2018 by MohamedLEGH • edited Oct 9, 2018 by MohamedLEGH

User contributions licensed under CC BY-SA 3.0