简单的hello world exec是10 KB

时间:2018-10-09 14:53:55

标签: assembly x86 nasm

我在x86 nasm汇编中制作了一个简单的Hello World程序,但我不明白为什么最终的执行者差不多是10Ko。

nasm版本:

  

NASM版本2.13.03(于2018年4月1日编译)

ld版本:

  

GNU ld(GNU Binutils)2.31.1

这是我的源代码:

    ; -------------------------------------------------------------
; 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

当我使用nasm -f elf64 hello.asm进行组装时, 我得到一个hello.o文件,大小为976个字节。

当我将其与ld hello.o -o hello链接时, 我得到了一个8.9K的hello exec(我得到的是ls -lh)。

为什么链接使最终执行者如此庞大?即使我使用该选项删除调试数据(ld -s),也只能释放500个字节。

这是

的输出
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

1 个答案:

答案 0 :(得分:1)

我想我发现了一些东西。

在ld的2.30版本中,添加了一个新标志,并默认将其激活

  

2.30中的更改:

     
      
  • 添加-z分离代码以生成单独的代码PT_LOAD段。
  •   

source

如果我链接到ld -z noseparate-code hello.o -o hello

二进制文件的大小是1032字节,这是更合理的。 我不知道什么是 PT_LOAD ,但是运行hello world程序看起来并不重要。