| 1 | title: A program with no run time |
| 2 | part: Standing alone |
| 3 | summary: What is in an executable, what isn't, and how to see it. |
| 4 | --- |
| 5 | 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. |
| 6 | |
| 7 | ## The smallest program |
| 8 | |
| 9 | This program imports nothing. It makes the `write` system call itself, with the built-in `SYSCALL`, and returns. |
| 10 | |
| 11 | ```raz Bare |
| 12 | module Bare; |
| 13 | import SYSTEM; |
| 14 | |
| 15 | const Msg = "hello from raw system calls\n"; |
| 16 | |
| 17 | proc main*; |
| 18 | begin |
| 19 | _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg)); |
| 20 | end; |
| 21 | |
| 22 | end 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 | |
| 33 | It 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 | |
| 43 | 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. |
| 44 | |
| 45 | ## Programs that use C |
| 46 | |
| 47 | 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`: |
| 48 | |
| 49 | ```raz Cprint |
| 50 | module Cprint; |
| 51 | |
| 52 | extern proc puts(s: ^U8): I32; |
| 53 | |
| 54 | proc main*; |
| 55 | begin |
| 56 | _ := puts("hello from C"); |
| 57 | end; |
| 58 | |
| 59 | end 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 | |
| 69 | 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. |
| 70 | |
| 71 | ## Only what is reached |
| 72 | |
| 73 | 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. |
| 74 | |
| 75 | ```raz Lean |
| 76 | module Lean; |
| 77 | import Out; |
| 78 | |
| 79 | proc main*; |
| 80 | begin |
| 81 | Out.str("same program\n"); |
| 82 | end; |
| 83 | |
| 84 | end Lean. |
| 85 | ``` |
| 86 | |
| 87 | ```raz Heavy |
| 88 | module Heavy; |
| 89 | import Out; |
| 90 | |
| 91 | var table: [100000]INT; |
| 92 | |
| 93 | (* exported, but never called, so never in the executable *) |
| 94 | proc unused*(n: INT): INT; |
| 95 | var i, s: INT; |
| 96 | begin |
| 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; |
| 100 | end; |
| 101 | |
| 102 | proc main*; |
| 103 | begin |
| 104 | Out.str("same program\n"); |
| 105 | end; |
| 106 | |
| 107 | end Heavy. |
| 108 | ``` |
| 109 | |
| 110 | ```sh |
| 111 | $ raz Lean.raz |
| 112 | $ raz Heavy.raz |
| 113 | $ ls -l Lean Heavy | awk '{print $9, $5}' |
| 114 | ``` |
| 115 | |
| 116 | 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. |
| 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. |