raz / site / public / advanced / static.html

159 lines · 10.6 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>A program with no run time - More Raz</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="A program with no run time - More Raz">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'>
22<li><a href="fmt.html">Output and formatting</a></li>
23<li><a href="bytes.html">Strings and bytes</a></li>
24<li><a href="collections.html">Lists, arrays, maps and sorting</a></li>
25<li><a href="files.html">Files and the command line</a></li>
26<li><a href="processes.html">Time, randomness and other programs</a></li>
27<li><a href="allocators.html">Writing an allocator</a></li>
28</ol>
29<h3>Standing alone</h3><ol start='7'>
30<li><a href="static.html" aria-current="page">A program with no run time</a></li>
31<li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li>
32<li><a href="lifecycle.html">Starting, ending, and signals</a></li>
33<li><a href="targets.html">One module, one file per target</a></li>
34</ol>
35<h3>The machine</h3><ol start='11'>
36<li><a href="asm.html">Procedures in assembly</a></li>
37<li><a href="syscalls.html">System calls</a></li>
38<li><a href="listing.html">What the compiler makes</a></li>
39</ol>
40<h3>Shared memory</h3><ol start='14'>
41<li><a href="volatile.html">Volatile</a></li>
42<li><a href="atomics.html">Atomics</a></li>
43<li><a href="ordering.html">Ordering and publication</a></li>
44<li><a href="lockfree.html">A lock-free stack</a></li>
45<li><a href="ring.html">A ring buffer between threads</a></li>
46</ol></aside><article><p class="kicker">Standing alone &middot; lesson 7 of 18</p><h1>A program with no run time</h1>
47<p>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.</p>
48<h2>The smallest program</h2>
49<p>This program imports nothing. It makes the <code>write</code> system call itself, with the built-in <code>SYSCALL</code>, and returns.</p>
50<div class="file"><span>Bare.raz</span></div><pre class="code"><code><span class="kw">module</span> Bare;
51<span class="kw">import</span> SYSTEM;
52
53<span class="kw">const</span> Msg = <span class="string">&quot;hello from raw system calls\n&quot;</span>;
54
55<span class="kw">proc</span> main*;
56<span class="kw">begin</span>
57 _ := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg));
58<span class="kw">end</span>;
59
60<span class="kw">end</span> Bare.</code></pre>
61<pre class="output term"><code><span class="cmd">$ raz Bare.raz</span>
62Bare.raz:8:42: error: Msg[0] is read through a []const slice, and can&#x27;t be changed
63 8 | _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
64 | ^^^^^^
65help: a []const slice&#x27;s elements are only read: write them through a []T
66raz: 1 error
67<span class="cmd">$ ./Bare</span>
68/bin/sh: line 1: ./Bare: No such file or directory
69<span class="cmd">$ file Bare</span>
70Bare: cannot open `Bare&#x27; (No such file or directory)
71<span class="cmd">$ ldd Bare</span>
72ldd: ./Bare: No such file or directory
73<span class="cmd">$ ls -l Bare | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
74ls: cannot access &#x27;Bare&#x27;: No such file or directory</code></pre>
75<p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p>
76<h2>What it does when it runs</h2>
77<p><code>strace</code> lists every system call a program makes. This is the whole of it:</p>
78<pre class="output term"><code><span class="cmd">$ strace ./Bare 2&gt;&amp;1 &gt;/dev/null | sed -e &#x27;s/0x[0-9a-f]\{5,\}/ADDR/g&#x27; -e &#x27;s/[0-9]* vars/N vars/&#x27;</span>
79strace: Cannot stat &#x27;./Bare&#x27;: No such file or directory</code></pre>
80<p>The start-up code does four things before <code>main</code> runs: it sets up a stack of its own for signal handlers (<code>sigaltstack</code>), and installs handlers for <code>SIGSEGV</code> and <code>SIGFPE</code>. That is what turns a <code>nil</code> pointer or a division by zero into a message that names the place, instead of a bare "Segmentation fault". Then <code>main</code> runs its single <code>write</code>, and the program ends with <code>exit_group</code>. Nothing is mapped, loaded, or looked up.</p>
81<h2>Programs that use C</h2>
82<p>A program that declares an <code>extern</code> 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 <code>printf</code>:</p>
83<div class="file"><span>Cprint.raz</span></div><pre class="code"><code><span class="kw">module</span> Cprint;
84
85<span class="kw">extern</span> <span class="kw">proc</span> puts(s: ^<span class="ty">U8</span>): <span class="ty">I32</span>;
86
87<span class="kw">proc</span> main*;
88<span class="kw">begin</span>
89 _ := puts(<span class="string">&quot;hello from C&quot;</span>);
90<span class="kw">end</span>;
91
92<span class="kw">end</span> Cprint.</code></pre>
93<pre class="output term"><code><span class="cmd">$ raz Cprint.raz</span>
94Cprint.raz:3:1: error: extern needs import SYSTEM
95 3 | extern proc puts(s: ^U8): I32;
96 | ^^^^^^
97help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
98Cprint.raz:7:15: error: a string as a ^U8 needs import SYSTEM
99 7 | _ := puts(&quot;hello from C&quot;);
100 | ^^^^^^^^^^^^^^
101help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
102raz: 2 errors
103<span class="cmd">$ ./Cprint</span>
104/bin/sh: line 1: ./Cprint: No such file or directory
105<span class="cmd">$ file Cprint</span>
106Cprint: cannot open `Cprint&#x27; (No such file or directory)
107<span class="cmd">$ ldd Cprint | sed &#x27;s/ (0x[0-9a-f]*)//&#x27;</span>
108ldd: ./Cprint: No such file or directory</code></pre>
109<p>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 <code>extern</code> or a <code>link</code> of a library.</p>
110<h2>Only what is reached</h2>
111<p>The compiler keeps only what can be reached from <code>main</code> 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.</p>
112<div class="file"><span>Lean.raz</span></div><pre class="code"><code><span class="kw">module</span> Lean;
113<span class="kw">import</span> Out;
114
115<span class="kw">proc</span> main*;
116<span class="kw">begin</span>
117 Out.str(<span class="string">&quot;same program\n&quot;</span>);
118<span class="kw">end</span>;
119
120<span class="kw">end</span> Lean.</code></pre>
121<div class="file"><span>Heavy.raz</span></div><pre class="code"><code><span class="kw">module</span> Heavy;
122<span class="kw">import</span> Out;
123
124<span class="kw">var</span> table: [<span class="number">100000</span>]<span class="ty">INT</span>;
125
126<span class="comment">(* exported, but never called, so never in the executable *)</span>
127<span class="kw">proc</span> unused*(n: <span class="ty">INT</span>): <span class="ty">INT</span>;
128<span class="kw">var</span> i, s: <span class="ty">INT</span>;
129<span class="kw">begin</span>
130 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">99999</span> <span class="kw">do</span> table[i] := n * i; s := s + table[i]; <span class="kw">end</span>;
131 Out.str(<span class="string">&quot;a long string that nobody will ever print, to give it some weight&quot;</span>);
132 <span class="kw">return</span> s;
133<span class="kw">end</span>;
134
135<span class="kw">proc</span> main*;
136<span class="kw">begin</span>
137 Out.str(<span class="string">&quot;same program\n&quot;</span>);
138<span class="kw">end</span>;
139
140<span class="kw">end</span> Heavy.</code></pre>
141<pre class="output term"><code><span class="cmd">$ raz Lean.raz</span>
142<span class="cmd">$ raz Heavy.raz</span>
143<span class="cmd">$ ls -l Lean Heavy | awk &#x27;{print $9, $5}&#x27;</span>
144Heavy 28608
145Lean 28600</code></pre>
146<p>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.</p>
147<h2>Stripping</h2>
148<p><code>-s</code> 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.</p>
149<pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span>
150<span class="cmd">$ ls -l Lean Lean-stripped | awk &#x27;{print $9, $5}&#x27;</span>
151Lean 28600
152Lean-stripped 6856
153<span class="cmd">$ ./Lean-stripped</span>
154same program</code></pre>
155<p><code>raz -S Lean.raz</code> 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.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="allocators.html">&larr; Writing an allocator</a><a href="index.html">Contents</a><a class="next" href="linkstatic.html">Threads without C, and keeping the two apart &rarr;</a></div></article></div>
156</main>
157<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
158</body>
159</html>