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.
module Bare;
const Msg = "hello from raw system calls\n";
proc main*;
begin
_ := SYSCALL(1, 1, ADR(Msg[0]), LEN(Msg));
end;
end Bare.
$ raz Bare.raz
$ ./Bare
hello from raw system calls
$ file Bare
Bare: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
$ ldd Bare
not a dynamic executable
$ ls -l Bare | awk '{print $5, "bytes"}'
26096 bytes
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/'
execve("./Bare", ["./Bare"], ADDR /* N vars */) = 0
sigaltstack({ss_sp=ADDR, ss_flags=0, ss_size=16384}, NULL) = 0
rt_sigaction(SIGSEGV, {sa_handler=ADDR, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESETHAND|SA_SIGINFO, sa_restorer=ADDR}, NULL, 8) = 0
rt_sigaction(SIGFPE, {sa_handler=ADDR, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESETHAND|SA_SIGINFO, sa_restorer=ADDR}, NULL, 8) = 0
write(1, "hello from raw system calls\n", 28) = 28
exit_group(0) = ?
+++ exited with 0 +++
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:
module Cprint;
extern proc puts(s: ^U8): I32;
proc main*;
begin
_ := puts("hello from C");
end;
end Cprint.
$ raz Cprint.raz
$ ./Cprint
hello from C
$ file Cprint
Cprint: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, with debug_info, not stripped
$ ldd Cprint | sed 's/ (0x[0-9a-f]*)//'
linux-vdso.so.1
libc.so.6 => /usr/lib/libc.so.6
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2
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.
module Lean;
import Out;
proc main*;
begin
Out.str("same program\n");
end;
end Lean.
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 28304
Lean 28296
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 28296
Lean-stripped 6720
$ ./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.