raz / site / content / advanced / 07-static.txt

128 lines · 3.8 KiB · raw · history · last change a5145f64

1title: A program with no run time
2part: Standing alone
3summary: What is in an executable, what isn't, and how to see it.
4---
5Most 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.
6
7## The smallest program
8
9This program imports nothing. It makes the `write` system call itself, with the built-in `SYSCALL`, and returns.
10
11```raz Bare
12module Bare;
13import SYSTEM;
14
15const Msg = "hello from raw system calls\n";
16
17proc main*;
18begin
19 _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
20end;
21
22end Bare.
23```
24
25```sh
26$ raz Bare.raz
27$ ./Bare
28$ file Bare
29$ ldd Bare
30$ ls -l Bare | awk '{print $5, "bytes"}'
31```
32
33It is statically linked: no interpreter, no shared libraries, no `libc`. `ldd` says so.
34
35## What it does when it runs
36
37`strace` lists every system call a program makes. This is the whole of it:
38
39```sh
40$ strace ./Bare 2>&1 >/dev/null | sed -e 's/0x[0-9a-f]\{5,\}/ADDR/g' -e 's/[0-9]* vars/N vars/'
41```
42
43The 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.
44
45## Programs that use C
46
47A 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`:
48
49```raz Cprint
50module Cprint;
51
52extern proc puts(s: ^U8): I32;
53
54proc main*;
55begin
56 _ := puts("hello from C");
57end;
58
59end Cprint.
60```
61
62```sh
63$ raz Cprint.raz
64$ ./Cprint
65$ file Cprint
66$ ldd Cprint | sed 's/ (0x[0-9a-f]*)//'
67```
68
69Both 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.
70
71## Only what is reached
72
73The 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.
74
75```raz Lean
76module Lean;
77import Out;
78
79proc main*;
80begin
81 Out.str("same program\n");
82end;
83
84end Lean.
85```
86
87```raz Heavy
88module Heavy;
89import Out;
90
91var table: [100000]INT;
92
93(* exported, but never called, so never in the executable *)
94proc unused*(n: INT): INT;
95var i, s: INT;
96begin
97 for i := 0 to 99999 do table[i] := n * i; s := s + table[i]; end;
98 Out.str("a long string that nobody will ever print, to give it some weight");
99 return s;
100end;
101
102proc main*;
103begin
104 Out.str("same program\n");
105end;
106
107end Heavy.
108```
109
110```sh
111$ raz Lean.raz
112$ raz Heavy.raz
113$ ls -l Lean Heavy | awk '{print $9, $5}'
114```
115
116The 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.
117
118## Stripping
119
120`-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.
121
122```sh
123$ raz -s -o Lean-stripped Lean.raz
124$ ls -l Lean Lean-stripped | awk '{print $9, $5}'
125$ ./Lean-stripped
126```
127
128`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.