raz / commit 26da1860

commit
26da18608f4523365727933cc9507aea121ef900
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f7fb2836
Rt's places are linked only where Rt declares them; test/own builds without -s

patch · log

8 files changed, +110 -31

doc/compiler.md (history) modified +20 -15

@@ -27,7 +27,7 @@
2727 | `src/Link.raz` | 408 | 316 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers |
2929 | `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 |
3131
3232 Every file opens with a comment that says what it is, what it depends
3333 on and what it leaves out. Read those first; they are the map.
@@ -2274,7 +2274,7 @@
22742274 `rt_sigaction` and `sigaltstack` would fault there (`EFER.SCE` is
22752275 clear) and, with no IDT yet, reset the machine. Nor is `Rt` given the
22762276 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
22782278 `Rt.exit` end in system calls, which the kernel catches or replaces. A
22792279 program that uses C is refused.
22802280
@@ -2317,29 +2317,34 @@
23172317 module, as always). The library is all of what is in `DIR`, and nothing
23182318 of raz's unless it is linked there. `test/own` does it: `check.sh`
23192319 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).
23212322
23222323 **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:
23242325
23252326 | Name | What | Needed |
23262327 |---|---|---|
23272328 | `proc exit*(status: INT)` | Ends the program; never returns, so the compiler puts `ud2` after its call. | always |
23282329 | `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` |
23332334 | variable `cExit: proc(status: I32)` | Given C's `exit`. | only in a program that uses C, which `-bare` refuses |
23342335
23352336 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.
23432348
23442349 **`fail`'s kinds.** The compiler's code calls `Rt.fail` through a stub,
23452350 one for each kind it uses, that makes the call from where the check

src/Raz.raz (history) modified +45 -11

@@ -171,6 +171,46 @@
171171 INC(numLinks);
172172 end;
173173
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+
174214 (* The library is given C's symbol sym, in its variable v *)
175215 proc addCLink(sym: []const U8; v: ^Sym.Obj);
176216 begin
@@ -348,18 +388,12 @@
348388 if rt = nil then Diag.fatal("can't find module Rt"); end;
349389 (* what Rt is given: the places, which the linker makes, for Rt to say
350390 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. *)
354395 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;
363397 (* what a program that uses C is given of C: Rt its exit, and
364398 Thread, if the program has it, the calls that make and join C's
365399 threads, so that the C library knows of them *)

test/check.sh (history) modified +6 -4

@@ -156,14 +156,16 @@
156156 # a program with its own Rt (doc/compiler.md): raz -lib of nothing but
157157 # test/own/lib/Rt.raz, whose exit and fail make their system calls
158158 # 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
160161 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
162163 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
164165 statusFail=$( { ( exec $out/own/OwnFail ) > $out/own/OwnFail.out 2>&1; echo $?; } 2>/dev/null )
165166 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))
167169 else echo "FAIL own Rt: status $status, $statusFail"; cat $out/own/Own.out $out/own/OwnFail.out; fail=$((fail + 1)); fi
168170
169171 # what src/ asks of lib/ is what doc/compiler.md lists (test/asks.sh)

test/err/RtPair.args (history) added +1 -0

@@ -0,0 +1 @@
1+-bare -lib test/err/lib

test/err/RtPair.err (history) added +1 -0

@@ -0,0 +1 @@
1+raz: Rt has calls but no teller

test/err/RtPair.raz (history) added +11 -0

@@ -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.

test/err/lib/Rt.raz (history) added +25 -0

@@ -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.

test/own/lib/Rt.raz (history) modified +1 -1

@@ -1,6 +1,6 @@
11 (* The smallest Rt a program can have: exit and fail, and nothing else,
22 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
44 has the same two procedures, with its own way of ending and of
55 saying a check failed. *)
66 module Rt;