From 26da18608f4523365727933cc9507aea121ef900 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:23:32 +0000 Subject: [PATCH] Rt's places are linked only where Rt declares them; test/own builds without -s --- diff --git a/doc/compiler.md b/doc/compiler.md index 13c3cd2..e27bf09 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -27,7 +27,7 @@ | `src/Link.raz` | 408 | 316 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers | | `src/Host.raz` | 103 | 52 | The system running the compiler | -| `src/Raz.raz` | 394 | 316 | The command line | +| `src/Raz.raz` | 439 | 345 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -2274,7 +2274,7 @@ `rt_sigaction` and `sigaltstack` would fault there (`EFER.SCE` is clear) and, with no IDT yet, reset the machine. Nor is `Rt` given the profiler, which its body would start. It is still given the places, by -stores alone, so a failed check says where it was; but `Rt.fail` and +stores alone, if its `Rt` declares them (below), so a failed check says where it was; but `Rt.fail` and `Rt.exit` end in system calls, which the kernel catches or replaces. A program that uses C is refused. @@ -2317,29 +2317,34 @@ module, as always). The library is all of what is in `DIR`, and nothing of raz's unless it is linked there. `test/own` does it: `check.sh` builds `test/own/Own.raz` and `OwnFail.raz` with `-lib test/own/lib`, -whose `Rt.raz` is the whole library, under `-bare -s`. +whose `Rt.raz` is the whole library, under `-bare` (and `test/err/RtPair` +gives an `Rt` with half a pair). **What `Rt` must declare.** `Raz.raz` finds each name in `Rt`'s scope -(`objIn`) and stops with "Rt has no X" where one is missing: +(`objIn`) and stops with "Rt has no X" where one of the first two, or `cExit`, is missing; the rest are optional: | Name | What | Needed | |---|---|---| | `proc exit*(status: INT)` | Ends the program; never returns, so the compiler puts `ud2` after its call. | always | | `proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT)` | A failed check; never returns. | always | -| variable `places` | The compiler stores the address of the places table in it, for `Rt` to say where a check failed. | unless `-s` | -| `proc calls` and variable `teller` | `teller` is given the address of `calls`, which says the calls under way. | unless `-s` | -| `proc place*` and variable `placer` | `placer` is given the address of `place`, which says where an address in the code comes from. | unless `-s` | -| `proc startProfile*` and variable `profiler` | `profiler` is given `startProfile`, which `Rt`'s body calls when `RAZ_PROFILE` is set. | unless `-s`, `-noprofile` or `-bare` | +| variable `places` | The compiler stores the address of the places table in it, for `Rt` to say where a check failed. | if declared | +| `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` | +| `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` | +| `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` | | variable `cExit: proc(status: I32)` | Given C's `exit`. | only in a program that uses C, which `-bare` refuses | Each variable is 8 bytes, for an address stored with `lea` and a store -at the entry. `-s` leaves out the places and what needs them, so a -failure is told by its address, and a program built with it needs -`Rt` to declare only `exit` and `fail` (this is what `test/own` does). -Without `-s` the compiler links all of the `unless -s` rows, even -under `-bare`, and a missing name stops it ("Rt has no places"): a -kernel that wants its symbols for a debugger and no places has no way -yet. +at the entry. The places are optional, each linked only when `Rt` +declares it (`giveRtPlaces`): `places` alone; `calls` with `teller`; `place` +with `placer`; `startProfile` with `profiler`. A pair is both or neither: +one of them alone stops the compiler with "Rt has calls but no teller", +naming the other. `-s` leaves all of them out, and the ELF's symbols and +line numbers with them. Without `-s`, an `Rt` that declares none still +has its symbols in the ELF (what a debugger reads), but no line table, which +lives in the places table. A failed check in such a program is told by +its address: `fail` is given `at`, an address inside the code, and it is +for `Rt` to print it, or look it up in the symbols. A hosted program's +`Rt` is raz's, which declares them all, so its output is as before. **`fail`'s kinds.** The compiler's code calls `Rt.fail` through a stub, one for each kind it uses, that makes the call from where the check diff --git a/src/Raz.raz b/src/Raz.raz index a90ea15..5353653 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -171,6 +171,46 @@ INC(numLinks); end; +(* Whether Rt declares a name n *) +proc declares(rt: ^Sym.Module; n: []const U8): BOOL; +begin + return Sym.find(rt^.scope, internStr(n)) # nil; +end; + +(* Rt is given the address of its procedure p in its variable v, if it + declares both; neither is no link, and one alone is an error that + names the other, since the address is useless without its variable + and the variable would be left empty *) +proc addPair(rt: ^Sym.Module; p, v: []const U8); +begin + if declares(rt, p) and not declares(rt, v) then + Diag.fatal(Diag.say("Rt has {} but no {}", [Fmt.str(p), Fmt.str(v)])); + elsif declares(rt, v) and not declares(rt, p) then + Diag.fatal(Diag.say("Rt has {} but no {}", [Fmt.str(v), Fmt.str(p)])); + elsif declares(rt, p) then + addLink(objIn(rt, p)^.chunk, objIn(rt, v)); + end; +end; + +(* What Rt is given for telling where a failed check was, as far as it + declares it. The places table (and the line table in it) is made only + if Rt has `places`, a variable for its address; Rt's calls, place and + startProfile are linked to the variables teller, placer and profiler + (profiled: unless -noprofile or -bare). An Rt that declares none of + these, a kernel's, gets a failed check with only the raw address of + the failing call, as with -s, and still has the ELF's symbols. *) +proc giveRtPlaces(rt: ^Sym.Module; profiled: BOOL); +begin + if declares(rt, "places") then + Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); + Chunk.setData(Link.placesChunk, nil, 0, 8); + addLink(Link.placesChunk, objIn(rt, "places")); + end; + addPair(rt, "calls", "teller"); + addPair(rt, "place", "placer"); + if profiled then addPair(rt, "startProfile", "profiler"); end; +end; + (* The library is given C's symbol sym, in its variable v *) proc addCLink(sym: []const U8; v: ^Sym.Obj); begin @@ -348,18 +388,12 @@ if rt = nil then Diag.fatal("can't find module Rt"); end; (* what Rt is given: the places, which the linker makes, for Rt to say where a failed check or a signal was (place), and the calls under - way then (calls); and the profiler, which needs them too. raz -s - gives none, -noprofile no profiler, and -bare no profiler, since - Rt's body, which starts it, doesn't run. *) + way then (calls); and the profiler, which needs them too: each + only if Rt declares it (giveRtPlaces). raz -s gives none, + -noprofile no profiler, and -bare no profiler, since Rt's body, + which starts it, doesn't run. *) numLinks := 0; - if not Elf.stripped then - Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); - Chunk.setData(Link.placesChunk, nil, 0, 8); - addLink(Link.placesChunk, objIn(rt, "places")); - addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller")); - addLink(objIn(rt, "place")^.chunk, objIn(rt, "placer")); - if not noProfile and not bare then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end; - end; + if not Elf.stripped then giveRtPlaces(rt, not noProfile and not bare); end; (* what a program that uses C is given of C: Rt its exit, and Thread, if the program has it, the calls that make and join C's threads, so that the C library knows of them *) diff --git a/test/check.sh b/test/check.sh index 89ac759..d418989 100755 --- a/test/check.sh +++ b/test/check.sh @@ -156,14 +156,16 @@ # a program with its own Rt (doc/compiler.md): raz -lib of nothing but # test/own/lib/Rt.raz, whose exit and fail make their system calls # themselves; Own ends with main's 3 after saying hello, OwnFail with 2 -# after Rt.fail's kind 0, an index +# after Rt.fail's kind 0, an index; built without -s, with Rt having no +# places, so the ELF has its symbols rm -rf $out/own; mkdir -p $out/own -$raz -lib test/own/lib -Werror -bare -s -o $out/own/Own test/own/Own.raz +$raz -lib test/own/lib -Werror -bare -o $out/own/Own test/own/Own.raz status=$( { ( exec $out/own/Own ) > $out/own/Own.out 2>&1; echo $?; } 2>/dev/null ) -$raz -lib test/own/lib -Werror -bare -s -o $out/own/OwnFail test/own/OwnFail.raz +$raz -lib test/own/lib -Werror -bare -o $out/own/OwnFail test/own/OwnFail.raz statusFail=$( { ( exec $out/own/OwnFail ) > $out/own/OwnFail.out 2>&1; echo $?; } 2>/dev/null ) if [ $status = 3 ] && [ $statusFail = 2 ] && [ "$(cat $out/own/Own.out)" = hello ] \ - && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ]; then pass=$((pass + 1)) + && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ] \ + && readelf -s $out/own/Own | grep -q ' Own.main$'; then pass=$((pass + 1)) else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi # what src/ asks of lib/ is what doc/compiler.md lists (test/asks.sh) diff --git a/test/err/RtPair.args b/test/err/RtPair.args new file mode 100644 index 0000000..d52d71c --- /dev/null +++ b/test/err/RtPair.args @@ -0,0 +1 @@ +-bare -lib test/err/lib diff --git a/test/err/RtPair.err b/test/err/RtPair.err new file mode 100644 index 0000000..c99ff9e --- /dev/null +++ b/test/err/RtPair.err @@ -0,0 +1 @@ +raz: Rt has calls but no teller diff --git a/test/err/RtPair.raz b/test/err/RtPair.raz new file mode 100644 index 0000000..8f0263b --- /dev/null +++ b/test/err/RtPair.raz @@ -0,0 +1,11 @@ +module RtPair; + +(* Rt declares calls and not teller: one of a pair alone is an error. *) +import Rt; + +proc main*; +begin + Rt.exit(0); +end; + +end RtPair. diff --git a/test/err/lib/Rt.raz b/test/err/lib/Rt.raz new file mode 100644 index 0000000..4918203 --- /dev/null +++ b/test/err/lib/Rt.raz @@ -0,0 +1,25 @@ +(* An Rt for test/err/RtPair: exit and fail, and `calls` with no + `teller` to be given its address, which the compiler refuses. *) +module Rt; +import SYSTEM; + +const ExitGroup = 231; (* Linux's exit_group(status) *) + +(* Ends the program with status *) +proc exit*(status: INT); +begin + _ := SYSTEM.SYSCALL(ExitGroup, status); +end; + +(* A check failed; this Rt ends the program *) +proc fail*(_what, _a, _b, _len: INT; _at: UINT; _name: ^U8; _rdi, _rsi: UINT); +begin + exit(2); +end; + +(* Says the calls under way: the half of a pair that is alone *) +proc calls*; +begin +end; + +end Rt. diff --git a/test/own/lib/Rt.raz b/test/own/lib/Rt.raz index 5ddd367..d5784ac 100644 --- a/test/own/lib/Rt.raz +++ b/test/own/lib/Rt.raz @@ -1,6 +1,6 @@ (* The smallest Rt a program can have: exit and fail, and nothing else, each making its system call itself. For test/own, which builds a - program against it, with raz -bare -s, under Linux. A kernel's Rt + program against it, with raz -lib and -bare, under Linux. A kernel's Rt has the same two procedures, with its own way of ending and of saying a check failed. *) module Rt;