2  From Source to Executables: Towards Processes

Winter Semester 2026/2027

Author
Affiliation

Stavros Kousidis

ICCT

Lecture 1 showed that an RV64 target can execute a small program without a guest operating system. Lecture 2 asks how that execution comes into existence and why, for an operating system, simply starting a program is not enough.

The path is:

source → objects → linked ELF → memory → startup code → main

and then:

executing program → OS-managed execution → process

The build tools are therefore not the final topic. We unfold them once because they make the transition from source code to a running machine state visible.

2.1 From source code to target-specific object code

We first compile main.c without linking:

mkdir -p build/manual

riscv64-unknown-elf-gcc -march=rv64imac -mabi=lp64 \
  -mcmodel=medany -msmall-data-limit=0 \
  -c main.c \
  -o build/manual/main.o

The important option is -c: stop before linking. The remaining options are course-provided target/build settings.

Inspecting the result with

file build/manual/main.o

shows that main.o is already a RISC-V ELF object.

ELF (Executable and Linkable Format) is the binary file format we use here for both object files and the final executable.

This distinction matters: the compiler has already translated C into code for our target ISA. The object file is therefore not architecture-neutral. But it is also not yet a complete program. Separate pieces can still refer to one another, and their final addresses have not yet been established.

The startup assembly is translated in the same way:

riscv64-unknown-elf-gcc -march=rv64imac -mabi=lp64 \
  -mcmodel=medany -c start.S -o build/manual/start.o

2.2 Why object files still need a linker

A symbol is a name used to refer to a program entity or location, such as a function or a linker-defined address.

Using nm (“name list”) lets us inspect the symbols in the two objects:

riscv64-unknown-elf-nm build/manual/start.o
riscv64-unknown-elf-nm build/manual/main.o

For the symbols relevant here, we can read the result as:

start.o defines _start
start.o needs   main
start.o needs   _stack_top

main.o  defines main

The important concept is not the letters printed by nm, but the relationship between separately translated pieces. One object can provide a symbol while another refers to it. The linker connects those pieces.

The unresolved _stack_top also reveals a second responsibility of the linker: it must establish where the program lives in target memory.

2.3 From the stored-program idea to a concrete memory layout

Lecture 1 used the abstract model:

instructions + data
        ↓
      memory
        ↕
       CPU

Our real program now contains different kinds of bytes that need concrete places.

The stack is runtime memory used during function calls, for example for local state and saved call information. The register sp is the stack pointer; _stack_top names the initial top address of this stack.

RV64 memory from 0x80000000

+----------------------------------------------------------+
| .text    _start, main, ...      | instructions          |
| .rodata  "Hello BVS1\n"         | constant data         |
| .data                           | initialized data      |
| .bss                            | zero-initialized data |
| C stack  (top: _stack_top)      | runtime data          |
+----------------------------------------------------------+

These roles are not properties that memory itself knows. They arise from the executable layout and from how the program and CPU use the addresses.

The course-provided linker.ld makes that layout concrete:

ENTRY(_start)

MEMORY {
    RAM (rwx) : ORIGIN = 0x80000000, LENGTH = 128M
}

. = ALIGN(16);
. += 16K;
_stack_top = .;

Here . is the linker’s current output address. After 16 KiB have been reserved for the initial stack, that address is named _stack_top.

Students do not need to write linker scripts in BVS1. The important idea is that the linker does not merely join files; it constructs one final arrangement of addresses in target memory in which the pieces can refer to one another.

2.4 Linking produces the program QEMU can load

We link the two objects using the course layout:

riscv64-unknown-elf-gcc -nostdlib -nostartfiles -no-pie \
  -T linker.ld build/manual/start.o build/manual/main.o \
  -o build/manual/hello-bvs1.elf

Here -T linker.ld selects our course-provided memory layout; the remaining options are link settings for our target without a guest operating system.

At this point the linker has:

  • combined the objects;
  • resolved the symbol references;
  • established the final memory layout.

The resulting ELF is what QEMU receives. QEMU does not compile or link the program; it provides the emulated RV64 machine and loads the already-built image.

2.5 Why execution starts before main

The linker script declares:

ENTRY(_start)

and _start is defined in start.S:

_start:
    la   sp, _stack_top
    call main
1:
    j    1b

This small fragment exposes an important boundary between machine startup and ordinary C code.

Before main can run, startup code establishes at least the execution environment that C expects. Here that means setting sp, the stack pointer, to the address denoted by _stack_top.

Then call main transfers control to main and remembers where execution should return. We deliberately do not open the full function-call mechanism yet. The fact that a return point must somehow be preserved becomes important in Lecture 3.

The local label 1: and 1b only express the final fallback loop: if main returns, execution remains there.

2.6 The whole path, and what Make hides

Running the ELF manually:

qemu-system-riscv64 -machine virt -m 128M \
  -smp 1 -nographic -bios none \
  -kernel build/manual/hello-bvs1.elf

reconstructs the complete path:

main.c    → compile  → main.o  ──┐
                                 │
start.S   → assemble → start.o ──┼─→ link → hello-bvs1.elf
                                 │                ↓
linker.ld ───────────────────────┘               QEMU
                                                  ↓
                                                 RV64
                                                  ↓
                                              Hello BVS1

Compilation, assembly, linking, and QEMU itself run in the Ubuntu development VM. The final RV64 instructions execute in the emulated target.

Make automates this workflow. It decides when a target is out of date and runs the corresponding recipe. The GCC driver performs the translation steps and invokes the linker. QEMU provides the machine on which the result executes.

Doing the path manually once is useful because the automation then has a visible meaning rather than being a black box.

2.7 Change, rebuild, observe

If only the string in main.c changes, we can predict the consequences before rebuilding:

  • main.o must change;
  • start.o does not;
  • the final ELF must be linked again;
  • linker.ld does not change;
  • QEMU itself does not change.

These are the logical dependencies. A concrete Makefile may encode them at a coarser granularity and therefore rebuild more than the minimum set of intermediate files.

This small exercise is about maintaining a causal model of the system:

predict → change → build → run → observe → explain

That pattern will be reused when the changes are no longer only strings, but operating-system mechanisms.

2.8 Why an executing program is not yet a process

Our current target has one execution:

_start
  ↓
main
  ↓
print
  ↓
for (;;) {}

Once it starts, nothing inside our target can currently stop it, regain control, and later continue it.

That is the new requirement. An operating system must be able to manage an execution rather than merely start it.

For our first model:

A process is an execution that the operating system can manage.

For Checkpoint 1, “manage” means only:

create → run → yield → resume → exit

This is intentionally much smaller than a complete Unix process model. We do not yet need address spaces, files, process IDs, or scheduling policies.

The minimum lifecycle is:

create ──→ READY ──run / resume──→ RUNNING ──exit──→ FINISHED
             ↑                       │
             └──────── yield ────────┘

A yielded process is still runnable. A finished process is not.

2.9 Yield, exit, resume, and restart

Yield and exit both transfer control back to the operating system, but they have different meanings.

  • yield: the execution is not finished and must be able to continue later;
  • exit: the execution is finished and must never continue.

The distinction becomes visible in this intended interface:

uart_puts("A\n");
process_yield();
uart_puts("B\n");
process_exit();

process_yield() and process_exit() are not library functions we already have. They are functions our minimal operating system still has to implement.

After process_yield(), the operating system has control. When the process runs again, it must continue with uart_puts("B\n"). Printing A again would mean that we restarted the execution instead of resuming it.

This is the conceptual step that makes the next lecture necessary.

2.10 The question that leads to execution context

To resume correctly, something about the paused execution must survive:

process runs
     ↓
yield
     ↓
OS regains control
     ↓
resume
     ↓
continue exactly where it stopped

What must be preserved so this can work?

At this point it is enough to suspect that some machine state must survive the pause. Lecture 3 determines exactly what that state is.

The final path of Lecture 2 is therefore:

source → objects → linked ELF → memory → _start → main

followed by:

executing program → OS-managed execution → process

The remaining question is:

What machine state makes resume possible?

2.11 Key ideas

Lecture 2 connects the build path to the first process abstraction:

  • Compilation and assembly produce target-specific object files.
  • ELF is the binary format we use for both object files and the final executable.
  • Symbols name program entities or locations; the linker resolves references between separately translated pieces.
  • The linker also establishes the final memory layout of the program.
  • The stack is runtime memory used during function calls, and sp is the stack pointer.
  • Execution begins in startup code at _start, which establishes the initial environment before entering C.
  • QEMU loads the already-built ELF into the emulated RV64 target; it does not compile or link the program.
  • Make automates the build workflow, while the compiler, assembler, and linker perform the translation steps.
  • A process is an execution that the operating system can manage through create → run → yield → resume → exit.
  • yield and exit both return control to the operating system, but only a yielded execution may continue later.
  • Resuming means continuing where execution stopped, not restarting from the beginning.

A useful test of understanding is to explain both chains:

source → object files → linked ELF → memory → _start → main

and

executing program → OS-managed execution → process

and then explain why resuming a process requires some machine state to survive the pause.

References