Rt's places are linked only where Rt declares them; test/own builds without -s
| @@ -27,7 +27,7 @@ | ||
| 27 | 27 | | `src/Link.raz` | 408 | 316 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Host.raz` | 103 | 52 | The system running the compiler | |
| 30 | -| `src/Raz.raz` | 394 | 316 | The command line | | |
| 30 | +| `src/Raz.raz` | 439 | 345 | The command line | | |
| 31 | 31 | |
| 32 | 32 | Every file opens with a comment that says what it is, what it depends |
| 33 | 33 | on and what it leaves out. Read those first; they are the map. |
| @@ -2274,7 +2274,7 @@ | ||
| 2274 | 2274 | `rt_sigaction` and `sigaltstack` would fault there (`EFER.SCE` is |
| 2275 | 2275 | clear) and, with no IDT yet, reset the machine. Nor is `Rt` given the |
| 2276 | 2276 | profiler, which its body would start. It is still given the places, by |
| 2277 | -stores alone, so a failed check says where it was; but `Rt.fail` and | |
| 2277 | +stores alone, if its `Rt` declares them (below), so a failed check says where it was; but `Rt.fail` and | |
| 2278 | 2278 | `Rt.exit` end in system calls, which the kernel catches or replaces. A |
| 2279 | 2279 | program that uses C is refused. |
| 2280 | 2280 | |
| @@ -2317,29 +2317,34 @@ | ||
| 2317 | 2317 | module, as always). The library is all of what is in `DIR`, and nothing |
| 2318 | 2318 | of raz's unless it is linked there. `test/own` does it: `check.sh` |
| 2319 | 2319 | builds `test/own/Own.raz` and `OwnFail.raz` with `-lib test/own/lib`, |
| 2320 | -whose `Rt.raz` is the whole library, under `-bare -s`. | |
| 2320 | +whose `Rt.raz` is the whole library, under `-bare` (and `test/err/RtPair` | |
| 2321 | +gives an `Rt` with half a pair). | |
| 2321 | 2322 | |
| 2322 | 2323 | **What `Rt` must declare.** `Raz.raz` finds each name in `Rt`'s scope |
| 2323 | -(`objIn`) and stops with "Rt has no X" where one is missing: | |
| 2324 | +(`objIn`) and stops with "Rt has no X" where one of the first two, or `cExit`, is missing; the rest are optional: | |
| 2324 | 2325 | |
| 2325 | 2326 | | Name | What | Needed | |
| 2326 | 2327 | |---|---|---| |
| 2327 | 2328 | | `proc exit*(status: INT)` | Ends the program; never returns, so the compiler puts `ud2` after its call. | always | |
| 2328 | 2329 | | `proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT)` | A failed check; never returns. | always | |
| 2329 | -| variable `places` | The compiler stores the address of the places table in it, for `Rt` to say where a check failed. | unless `-s` | | |
| 2330 | -| `proc calls` and variable `teller` | `teller` is given the address of `calls`, which says the calls under way. | unless `-s` | | |
| 2331 | -| `proc place*` and variable `placer` | `placer` is given the address of `place`, which says where an address in the code comes from. | unless `-s` | | |
| 2332 | -| `proc startProfile*` and variable `profiler` | `profiler` is given `startProfile`, which `Rt`'s body calls when `RAZ_PROFILE` is set. | unless `-s`, `-noprofile` or `-bare` | | |
| 2330 | +| variable `places` | The compiler stores the address of the places table in it, for `Rt` to say where a check failed. | if declared | | |
| 2331 | +| `proc calls` and variable `teller` | `teller` is given the address of `calls`, which says the calls under way. | if both are declared, not under `-s` | | |
| 2332 | +| `proc place*` and variable `placer` | `placer` is given the address of `place`, which says where an address in the code comes from. | if both are declared, not under `-s` | | |
| 2333 | +| `proc startProfile*` and variable `profiler` | `profiler` is given `startProfile`, which `Rt`'s body calls when `RAZ_PROFILE` is set. | if both are declared, not under `-s`, `-noprofile` or `-bare` | | |
| 2333 | 2334 | | variable `cExit: proc(status: I32)` | Given C's `exit`. | only in a program that uses C, which `-bare` refuses | |
| 2334 | 2335 | |
| 2335 | 2336 | Each variable is 8 bytes, for an address stored with `lea` and a store |
| 2336 | -at the entry. `-s` leaves out the places and what needs them, so a | |
| 2337 | -failure is told by its address, and a program built with it needs | |
| 2338 | -`Rt` to declare only `exit` and `fail` (this is what `test/own` does). | |
| 2339 | -Without `-s` the compiler links all of the `unless -s` rows, even | |
| 2340 | -under `-bare`, and a missing name stops it ("Rt has no places"): a | |
| 2341 | -kernel that wants its symbols for a debugger and no places has no way | |
| 2342 | -yet. | |
| 2337 | +at the entry. The places are optional, each linked only when `Rt` | |
| 2338 | +declares it (`giveRtPlaces`): `places` alone; `calls` with `teller`; `place` | |
| 2339 | +with `placer`; `startProfile` with `profiler`. A pair is both or neither: | |
| 2340 | +one of them alone stops the compiler with "Rt has calls but no teller", | |
| 2341 | +naming the other. `-s` leaves all of them out, and the ELF's symbols and | |
| 2342 | +line numbers with them. Without `-s`, an `Rt` that declares none still | |
| 2343 | +has its symbols in the ELF (what a debugger reads), but no line table, which | |
| 2344 | +lives in the places table. A failed check in such a program is told by | |
| 2345 | +its address: `fail` is given `at`, an address inside the code, and it is | |
| 2346 | +for `Rt` to print it, or look it up in the symbols. A hosted program's | |
| 2347 | +`Rt` is raz's, which declares them all, so its output is as before. | |
| 2343 | 2348 | |
| 2344 | 2349 | **`fail`'s kinds.** The compiler's code calls `Rt.fail` through a stub, |
| 2345 | 2350 | one for each kind it uses, that makes the call from where the check |
| @@ -171,6 +171,46 @@ | ||
| 171 | 171 | INC(numLinks); |
| 172 | 172 | end; |
| 173 | 173 | |
| 174 | +(* Whether Rt declares a name n *) | |
| 175 | +proc declares(rt: ^Sym.Module; n: []const U8): BOOL; | |
| 176 | +begin | |
| 177 | + return Sym.find(rt^.scope, internStr(n)) # nil; | |
| 178 | +end; | |
| 179 | + | |
| 180 | +(* Rt is given the address of its procedure p in its variable v, if it | |
| 181 | + declares both; neither is no link, and one alone is an error that | |
| 182 | + names the other, since the address is useless without its variable | |
| 183 | + and the variable would be left empty *) | |
| 184 | +proc addPair(rt: ^Sym.Module; p, v: []const U8); | |
| 185 | +begin | |
| 186 | + if declares(rt, p) and not declares(rt, v) then | |
| 187 | + Diag.fatal(Diag.say("Rt has {} but no {}", [Fmt.str(p), Fmt.str(v)])); | |
| 188 | + elsif declares(rt, v) and not declares(rt, p) then | |
| 189 | + Diag.fatal(Diag.say("Rt has {} but no {}", [Fmt.str(v), Fmt.str(p)])); | |
| 190 | + elsif declares(rt, p) then | |
| 191 | + addLink(objIn(rt, p)^.chunk, objIn(rt, v)); | |
| 192 | + end; | |
| 193 | +end; | |
| 194 | + | |
| 195 | +(* What Rt is given for telling where a failed check was, as far as it | |
| 196 | + declares it. The places table (and the line table in it) is made only | |
| 197 | + if Rt has `places`, a variable for its address; Rt's calls, place and | |
| 198 | + startProfile are linked to the variables teller, placer and profiler | |
| 199 | + (profiled: unless -noprofile or -bare). An Rt that declares none of | |
| 200 | + these, a kernel's, gets a failed check with only the raw address of | |
| 201 | + the failing call, as with -s, and still has the ELF's symbols. *) | |
| 202 | +proc giveRtPlaces(rt: ^Sym.Module; profiled: BOOL); | |
| 203 | +begin | |
| 204 | + if declares(rt, "places") then | |
| 205 | + Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); | |
| 206 | + Chunk.setData(Link.placesChunk, nil, 0, 8); | |
| 207 | + addLink(Link.placesChunk, objIn(rt, "places")); | |
| 208 | + end; | |
| 209 | + addPair(rt, "calls", "teller"); | |
| 210 | + addPair(rt, "place", "placer"); | |
| 211 | + if profiled then addPair(rt, "startProfile", "profiler"); end; | |
| 212 | +end; | |
| 213 | + | |
| 174 | 214 | (* The library is given C's symbol sym, in its variable v *) |
| 175 | 215 | proc addCLink(sym: []const U8; v: ^Sym.Obj); |
| 176 | 216 | begin |
| @@ -348,18 +388,12 @@ | ||
| 348 | 388 | if rt = nil then Diag.fatal("can't find module Rt"); end; |
| 349 | 389 | (* what Rt is given: the places, which the linker makes, for Rt to say |
| 350 | 390 | where a failed check or a signal was (place), and the calls under |
| 351 | - way then (calls); and the profiler, which needs them too. raz -s | |
| 352 | - gives none, -noprofile no profiler, and -bare no profiler, since | |
| 353 | - Rt's body, which starts it, doesn't run. *) | |
| 391 | + way then (calls); and the profiler, which needs them too: each | |
| 392 | + only if Rt declares it (giveRtPlaces). raz -s gives none, | |
| 393 | + -noprofile no profiler, and -bare no profiler, since Rt's body, | |
| 394 | + which starts it, doesn't run. *) | |
| 354 | 395 | numLinks := 0; |
| 355 | - if not Elf.stripped then | |
| 356 | - Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); | |
| 357 | - Chunk.setData(Link.placesChunk, nil, 0, 8); | |
| 358 | - addLink(Link.placesChunk, objIn(rt, "places")); | |
| 359 | - addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller")); | |
| 360 | - addLink(objIn(rt, "place")^.chunk, objIn(rt, "placer")); | |
| 361 | - if not noProfile and not bare then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end; | |
| 362 | - end; | |
| 396 | + if not Elf.stripped then giveRtPlaces(rt, not noProfile and not bare); end; | |
| 363 | 397 | (* what a program that uses C is given of C: Rt its exit, and |
| 364 | 398 | Thread, if the program has it, the calls that make and join C's |
| 365 | 399 | threads, so that the C library knows of them *) |
| @@ -156,14 +156,16 @@ | ||
| 156 | 156 | # a program with its own Rt (doc/compiler.md): raz -lib of nothing but |
| 157 | 157 | # test/own/lib/Rt.raz, whose exit and fail make their system calls |
| 158 | 158 | # themselves; Own ends with main's 3 after saying hello, OwnFail with 2 |
| 159 | -# after Rt.fail's kind 0, an index | |
| 159 | +# after Rt.fail's kind 0, an index; built without -s, with Rt having no | |
| 160 | +# places, so the ELF has its symbols | |
| 160 | 161 | rm -rf $out/own; mkdir -p $out/own |
| 161 | -$raz -lib test/own/lib -Werror -bare -s -o $out/own/Own test/own/Own.raz | |
| 162 | +$raz -lib test/own/lib -Werror -bare -o $out/own/Own test/own/Own.raz | |
| 162 | 163 | status=$( { ( exec $out/own/Own ) > $out/own/Own.out 2>&1; echo $?; } 2>/dev/null ) |
| 163 | -$raz -lib test/own/lib -Werror -bare -s -o $out/own/OwnFail test/own/OwnFail.raz | |
| 164 | +$raz -lib test/own/lib -Werror -bare -o $out/own/OwnFail test/own/OwnFail.raz | |
| 164 | 165 | statusFail=$( { ( exec $out/own/OwnFail ) > $out/own/OwnFail.out 2>&1; echo $?; } 2>/dev/null ) |
| 165 | 166 | if [ $status = 3 ] && [ $statusFail = 2 ] && [ "$(cat $out/own/Own.out)" = hello ] \ |
| 166 | - && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ]; then pass=$((pass + 1)) | |
| 167 | + && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ] \ | |
| 168 | + && readelf -s $out/own/Own | grep -q ' Own.main$'; then pass=$((pass + 1)) | |
| 167 | 169 | else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi |
| 168 | 170 | |
| 169 | 171 | # what src/ asks of lib/ is what doc/compiler.md lists (test/asks.sh) |
| @@ -0,0 +1 @@ | ||
| 1 | +-bare -lib test/err/lib |
| @@ -0,0 +1 @@ | ||
| 1 | +raz: Rt has calls but no teller |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module RtPair; | |
| 2 | + | |
| 3 | +(* Rt declares calls and not teller: one of a pair alone is an error. *) | |
| 4 | +import Rt; | |
| 5 | + | |
| 6 | +proc main*; | |
| 7 | +begin | |
| 8 | + Rt.exit(0); | |
| 9 | +end; | |
| 10 | + | |
| 11 | +end RtPair. |
| @@ -0,0 +1,25 @@ | ||
| 1 | +(* An Rt for test/err/RtPair: exit and fail, and `calls` with no | |
| 2 | + `teller` to be given its address, which the compiler refuses. *) | |
| 3 | +module Rt; | |
| 4 | +import SYSTEM; | |
| 5 | + | |
| 6 | +const ExitGroup = 231; (* Linux's exit_group(status) *) | |
| 7 | + | |
| 8 | +(* Ends the program with status *) | |
| 9 | +proc exit*(status: INT); | |
| 10 | +begin | |
| 11 | + _ := SYSTEM.SYSCALL(ExitGroup, status); | |
| 12 | +end; | |
| 13 | + | |
| 14 | +(* A check failed; this Rt ends the program *) | |
| 15 | +proc fail*(_what, _a, _b, _len: INT; _at: UINT; _name: ^U8; _rdi, _rsi: UINT); | |
| 16 | +begin | |
| 17 | + exit(2); | |
| 18 | +end; | |
| 19 | + | |
| 20 | +(* Says the calls under way: the half of a pair that is alone *) | |
| 21 | +proc calls*; | |
| 22 | +begin | |
| 23 | +end; | |
| 24 | + | |
| 25 | +end Rt. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | (* The smallest Rt a program can have: exit and fail, and nothing else, |
| 2 | 2 | each making its system call itself. For test/own, which builds a |
| 3 | - program against it, with raz -bare -s, under Linux. A kernel's Rt | |
| 3 | + program against it, with raz -lib and -bare, under Linux. A kernel's Rt | |
| 4 | 4 | has the same two procedures, with its own way of ending and of |
| 5 | 5 | saying a check failed. *) |
| 6 | 6 | module Rt; |