Standing alone · lesson 7 of 18

A program with no run time

Most languages arrive with a run time: a collector, a loader of libraries, start-up code you didn't write. A raz program that doesn't use C has none of that. The executable holds the code the program reaches, a few lines of start-up code, and nothing else. This part of the tour looks at what that means in practice, and what a program can do when nothing stands between it and the kernel.

The smallest program

This program imports nothing. It makes the write system call itself, with the built-in SYSCALL, and returns.

Bare.raz
module Bare;
import SYSTEM;

const Msg = "hello from raw system calls\n";

proc main*;
begin
    _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
end;

end Bare.
$ raz Bare.raz
Bare.raz:8:42: error: Msg[0] is read through a []const slice, and can't be changed
    8 |     _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
      |                                          ^^^^^^
help: a []const slice's elements are only read: write them through a []T
raz: 1 error
$ ./Bare
/bin/sh: line 1: ./Bare: No such file or directory
$ file Bare
Bare: cannot open `Bare' (No such file or directory)
$ ldd Bare
ldd: ./Bare: No such file or directory
$ ls -l Bare | awk '{print $5, "bytes"}'
ls: cannot access 'Bare': No such file or directory

It is statically linked: no interpreter, no shared libraries, no libc. ldd says so.

What it does when it runs

strace lists every system call a program makes. This is the whole of it:

$ strace ./Bare 2>&1 >/dev/null | sed -e 's/0x[0-9a-f]\{5,\}/ADDR/g' -e 's/[0-9]* vars/N vars/'
strace: Cannot stat './Bare': No such file or directory

The start-up code does four things before main runs: it sets up a stack of its own for signal handlers (sigaltstack), and installs handlers for SIGSEGV and SIGFPE. That is what turns a nil pointer or a division by zero into a message that names the place, instead of a bare "Segmentation fault". Then main runs its single write, and the program ends with exit_group. Nothing is mapped, loaded, or looked up.

Programs that use C

A program that declares an extern procedure, or links a library, is a different animal. It is linked dynamically and starts through the C library, so that C's own set-up and clean-up run. Compare the same kind of hello, with C's printf:

Cprint.raz
module Cprint;

extern proc puts(s: ^U8): I32;

proc main*;
begin
    _ := puts("hello from C");
end;

end Cprint.
$ raz Cprint.raz
Cprint.raz:3:1: error: extern needs import SYSTEM
    3 | extern proc puts(s: ^U8): I32;
      | ^^^^^^
help: it reaches memory other than through a variable: say import SYSTEM after the module's imports
Cprint.raz:7:15: error: a string as a ^U8 needs import SYSTEM
    7 |     _ := puts("hello from C");
      |               ^^^^^^^^^^^^^^
help: it reaches memory other than through a variable: say import SYSTEM after the module's imports
raz: 2 errors
$ ./Cprint
/bin/sh: line 1: ./Cprint: No such file or directory
$ file Cprint
Cprint: cannot open `Cprint' (No such file or directory)
$ ldd Cprint | sed 's/ (0x[0-9a-f]*)//'
ldd: ./Cprint: No such file or directory

Both are fine. The point is that you choose, and the language says which you chose: a program uses C exactly when a module of it has an extern or a link of a library.

Only what is reached

The compiler keeps only what can be reached from main and the module bodies. Unused procedures, variables and strings are left out. Here are two programs that differ by one large procedure that nothing calls.

Lean.raz
module Lean;
import Out;

proc main*;
begin
    Out.str("same program\n");
end;

end Lean.
Heavy.raz
module Heavy;
import Out;

var table: [100000]INT;

(* exported, but never called, so never in the executable *)
proc unused*(n: INT): INT;
var i, s: INT;
begin
    for i := 0 to 99999 do table[i] := n * i; s := s + table[i]; end;
    Out.str("a long string that nobody will ever print, to give it some weight");
    return s;
end;

proc main*;
begin
    Out.str("same program\n");
end;

end Heavy.
$ raz Lean.raz
$ raz Heavy.raz
$ ls -l Lean Heavy | awk '{print $9, $5}'
Heavy 28608
Lean 28600

The sizes differ by a few bytes, not by the hundreds of kilobytes that the table and the procedure would take: they are not in the executable at all. You can link in a library module without worrying what else it offers.

Stripping

-s leaves out what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the profiler. It is for the final build of a size-conscious program; the default keeps them so that gdb and failure messages work.

$ raz -s -o Lean-stripped Lean.raz
$ ls -l Lean Lean-stripped | awk '{print $9, $5}'
Lean 28600
Lean-stripped 6856
$ ./Lean-stripped
same program

raz -S Lean.raz is the other direction: it prints the machine code the compiler made, one source line at a time. The next two parts of the tour use it.

Save a program under its module’s name and run it with raz run Name.raz.