Date created: 04/07/19 20:59:35. Last modified: 04/07/19 22:38:49

Assembling, Linking, Loading (yasm)

References:
x86-64 Assembly Language Programming with Ubuntu By Ed Jorgensen

"The human readable source file is converted into an object file by the assembler. In the most basic form, the object file is converted into an executable file by the linker. The loader will load the executable file into memory."

 

$ cat basic.asm
;
; Constants
;

section .data

EXIT_SUCCESS equ 0  ; return/exit code
SYS_exit equ 60     ; syscall code for terminate

bVar1 db 5
bVar2 db 3


;
; Global vars
;

section .bss

bRet db 1


;
; Code
;

section .text
global _start
_start:

mov al, byte [bVar1]
add al, byte [bVar2]
mov byte [bRet], al

mov rax, SYS_exit
mov rdi, EXIT_SUCCESS
syscall

 

Compile the above assembly to an ELF64 format machine code object file and optionally (using -l) create a list file. The list file is purely for debugging and not needed for further compilation:

$ yasm -Worphan-labels -g dwarf2 -f elf64 basic.asm -l basic.lst
$ file basic.o
basic.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
$ cat basic.lst 1 %line 1+1 basic.asm 2 3 4 5 6 7 8 9 10 11 [section .data] 12 13 EXIT_SUCCESS equ 0 14 SYS_exit equ 60 15 16 00000000 05 bVar1 db 5 17 00000001 03 bVar2 db 3 18 19 20 21 22 23 24 [section .bss] 25 26 00000000 01 bRet db 1 27 28 29 30 31 32 33 [section .text] 34 [global _start] 35 _start: 36 37 00000000 8A0425[00000000] mov al, byte [bVar1] 38 00000007 020425[00000000] add al, byte [bVar2] 39 0000000E 880425[00000000] mov byte [bRet], al 40 41 00000015 48C7C03C000000 mov rax, SYS_exit 42 0000001C 48C7C700000000 mov rdi, EXIT_SUCCESS 43 00000023 0F05 syscall

 

Link the object file(s) and any system references/libraries into an single executable:

$ ld -g -o basic basic.o
$ file ./basic
basic: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
$ ./basic
$ echo $?
0