What prevents you from running an object file on its own?

Viewed 52

So I have been learning assembly and I understand the process of source.s -> assembler -> source.o -> linker -> executable. But I have been wondering, if there are no other object files I need to execute my program.
Why can't I just run the object file?

For example: something as simple as

global _start

section .text
_start:
    mov rax, 1
    mov rbx, 0
    int 0x80

still requires linking even though the object file should in theory contain all the code needed to run.

1 Answers

The difference between executable and linkable format is described in Executable and Linkable Format (ELF).

ELF file begins with a file header and its member Elf32_Ehdr.e_type informs the OS loader whether the file is executable (e_type=2) or linkable object (e_type=1).

Executables also require structure Elf32_Phdr called program header which tells the OS loader where in virtual address space should each section be linked and relocated. Ordinary assemblers do not produce program header.

Related