raz / commit c16e8a93

commit
c16e8a93fb54f4762f8b4a90285cfcc3ca8bcb18
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
051844ee
raz -h, raz(1), -s and -noprofile; segments with no padding

By the owner's word. raz -h says the options; doc/raz.1 is the manual
page, which make install puts in $(PREFIX)/share/man/man1 and make
check has groff read.

The entry point now gives Rt a list of what the program has (TLink):
the places, calls (which tells the calls under way) and the profiler's
start, which Rt reaches only through variables. What it isn't given,
nothing reaches, so the linker leaves it out: -noprofile gives no
profiler, -s none of the three, and writes no symbols or line numbers;
a failure then says only its address.

The file has no padding between segments: each is loaded a page
further from its file offset than the one before, so they still share
no page.

hello, world: 35,616 bytes before; 35,120 now, 24,632 with -noprofile,
7,432 with -s. test/check.sh checks both flags.

patch · log

11 files changed, +378 -84

Makefile (history) modified +5 -3

@@ -85,16 +85,18 @@
8585 chromium --headless --no-pdf-header-footer --print-to-pdf=$@ build/raz.html 2>/dev/null
8686
8787 # raz and its library go together under $(PREFIX)/lib/raz; the link
88-# on PATH works since raz finds lib/ beside its real path.
88+# on PATH works since raz finds lib/ beside its real path. The manual
89+# page goes where man looks, beside bin.
8990 PREFIX = $(HOME)/.local
9091
9192 install: raz
9293 rm -rf $(PREFIX)/lib/raz
93- mkdir -p $(PREFIX)/lib/raz $(PREFIX)/bin
94+ mkdir -p $(PREFIX)/lib/raz $(PREFIX)/bin $(PREFIX)/share/man/man1
9495 cp -r raz lib $(PREFIX)/lib/raz
9596 ln -sf $(PREFIX)/lib/raz/raz $(PREFIX)/bin/raz
97+ cp doc/raz.1 $(PREFIX)/share/man/man1/raz.1
9698
9799 uninstall:
98- rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz
100+ rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(PREFIX)/share/man/man1/raz.1
99101
100102 .PHONY: units tests check gdb bench stress fmt loc pdf clean install uninstall

README.md (history) modified +11 -4

@@ -51,7 +51,8 @@
5151 and builds into the repository's root:
5252
5353 make # ./raz
54- make install # ~/.local/bin/raz, its library in ~/.local/lib/raz
54+ make install # ~/.local/bin/raz, its library in ~/.local/lib/raz,
55+ # and its manual page, raz(1)
5556 make uninstall # removes them again
5657
5758 Both take `PREFIX=/usr/local` (or another place) instead of `~/.local`.
@@ -70,10 +71,13 @@
7071
7172 ## Using it
7273
73- raz [-I dir]... [-o file] [-Werror] [-S] Main.raz
74- raz run [-I dir]... [-Werror] Main.raz [arg]...
74+ raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] Main.raz
75+ raz run [option]... Main.raz [arg]...
76+ raz -h
7577 raz --version
7678
79+`raz -h` says the options, and `man raz` says more.
80+
7781 `raz Main.raz` compiles module `Main` and every module it imports, and
7882 writes the executable `Main`. A module `Name` lives in `Name.raz`, found
7983 in the main module's directory, then in each `-I` directory, then in
@@ -88,7 +92,10 @@
8892 and of a variable assigned but never read, unless its name starts with
8993 `_`; `-Werror` makes warnings errors. `-S` prints the main module's
9094 code instead of writing it: each line of source, and under it the
91-instructions made for it.
95+instructions made for it. `-s` strips what is there for finding faults:
96+the symbols and line numbers, the places a failure is told by, and the
97+profiler, so hello, world is 7 KB instead of 35; `-noprofile` leaves
98+out only the profiler.
9299
93100 At run time, a failed check says where it is and what failed, as in
94101 `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends

doc/compiler.md (history) modified +17 -8

@@ -13,12 +13,12 @@
1313 | `src/sym.pas` | 644 | 527 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,128 | 936 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,768 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames |
16+| `src/gen.pas` | 1,775 | 1,294 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,551 | 1,278 | Items: types, constants, variables, operators, conversions, calls |
1818 | `src/parse.pas` | 2,975 | 2,612 | Declarations, statements, expressions, built-ins, recovery, warnings |
19-| `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
20-| `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
21-| `src/raz.pas` | 197 | 147 | The command line |
19+| `src/link.pas` | 259 | 195 | Tree shaking, layout, patching, the places |
20+| `src/elf.pas` | 438 | 355 | Static and dynamic ELF, symbol table, line numbers |
21+| `src/raz.pas` | 244 | 187 | The command line |
2222
2323 Every file opens with a comment that says what it is, what it depends
2424 on and what it leaves out. Read those first; they are the map.
@@ -766,11 +766,20 @@
766766 own code, and `exit` has `profile` say which procedures and places
767767 those fell in, by the same tables: each procedure's samples in its own
768768 code, and its total, counting once each sample it was on the chain of.
769+The entry point gives Rt what the program has (`TLink`): the places,
770+`calls`, which tells the calls under way, and the profiler's start.
771+What it isn't given, nothing reaches, so the linker leaves it out:
772+`raz -noprofile` gives no profiler, and `raz -s` none of the three,
773+whose failures say only their address (and `elf.pas` writes no symbols
774+or line numbers). Hello is 35 KB, 25 KB without the profiler, and 7 KB
775+stripped.
769776
770-The layout is chosen so the file can be mapped as it lies: text just
771-after the headers, rodata on the next page, then a writable page that
772-begins with the dynamic loader's table (in a program that uses C) and
773-goes on as bss.
777+The layout is chosen so the file can be mapped as it lies, with no
778+padding: text just after the headers, then rodata, then the dynamic
779+loader's table (in a program that uses C), each loaded a page further
780+from its place in the file than the one before, so that no two
781+segments, with their different permissions, share a page. bss follows
782+the loader's table, or begins a page of its own.
774783
775784 ## elf.pas: the executable
776785

doc/raz.1 (history) added +156 -0

@@ -0,0 +1,156 @@
1+.\" raz(1) - the raz compiler
2+.Dd October 12, 2026
3+.Dt RAZ 1
4+.Os
5+.Sh NAME
6+.Nm raz
7+.Nd compile a raz program to a Linux executable
8+.Sh SYNOPSIS
9+.Nm
10+.Op Ar option ...
11+.Ar Main.raz
12+.Nm
13+.Cm run
14+.Op Ar option ...
15+.Ar Main.raz
16+.Op Ar arg ...
17+.Sh DESCRIPTION
18+.Nm
19+compiles the module in
20+.Ar Main.raz ,
21+and every module it imports, to an executable for x86-64 Linux, with
22+no assembler, linker or C library in between.
23+A program that names a C library in a
24+.Ic link
25+line, or declares an
26+.Ic extern
27+procedure, is linked against C dynamically; any other is static.
28+.Pp
29+A module
30+.Ar Name
31+is found as
32+.Ar Name Ns .linux-x86_64.raz
33+or
34+.Ar Name Ns .raz
35+in the directory of
36+.Ar Main.raz ,
37+then in each
38+.Fl I
39+directory in turn, then in the library,
40+.Pa lib/
41+beside the
42+.Nm
43+executable.
44+A program has one module of each name: a module found in two of these
45+places is an error, unless one of the files is a symbolic link to the
46+other, which chooses it.
47+.Pp
48+.Nm
49+.Cm run
50+compiles the same way, then runs the program with the
51+.Ar arg Ns s ,
52+writing no file: its output and exit status are those of the command.
53+.Pp
54+Every array index and slice is checked, unless the module says
55+.Ic check off ;
56+a failed check, a nil pointer, a division by zero or a stack overflow
57+ends the program with status 2, and says on standard error where it
58+was, the calls under way, and their variables:
59+.Bd -literal -offset indent
60+Bad.raz:6:7: index 5 is out of range for a length of 3
61+ in Bad.main at Bad.raz:6:7: i = 5
62+.Ed
63+.Pp
64+The executable has a symbol table and line numbers for
65+.Xr gdb 1 ,
66+.Xr nm 1
67+and
68+.Xr perf 1 ,
69+unless built with
70+.Fl s .
71+.Sh OPTIONS
72+.Bl -tag -width "-noprofile"
73+.It Fl o Ar file
74+Write the executable to
75+.Ar file .
76+Left out, it is
77+.Ar Main.raz
78+without
79+.Pa .raz .
80+.Cm run
81+takes no
82+.Fl o .
83+.It Fl I Ar dir
84+Look for imported modules in
85+.Ar dir
86+too, after the main module's own directory and before the library.
87+It may be given up to 16 times.
88+.It Fl Werror
89+A warning counts as an error.
90+.It Fl S
91+List the main module's machine code, each instruction under the line
92+of source it was made for, on standard output, and write no
93+executable.
94+.It Fl s
95+Strip the program of what is there for finding faults: its symbol
96+table, its line numbers, the places a failure is told by, and the
97+profiler.
98+A failure then says only its address, and the program is much
99+smaller: hello, world is 7 KB, against 35 KB.
100+.It Fl noprofile
101+Leave out the profiler, which
102+.Ev RAZ_PROFILE
103+turns on, but keep the rest.
104+.It Fl h , Fl \-help
105+Say the options.
106+.It Fl \-version
107+Say the version of
108+.Nm ,
109+and the version of the report it follows.
110+.El
111+.Sh ENVIRONMENT
112+.Bl -tag -width "RAZ_PROFILE"
113+.It Ev RAZ_PROFILE
114+Set to anything, in the environment of a program built without
115+.Fl s
116+or
117+.Fl noprofile ,
118+it has the program sample itself every millisecond of its time, and
119+say at its end, on standard error, which procedures took the most of
120+it, each with its share in its own code and its share in all, with the
121+procedures it called, and which places in the source:
122+.Bd -literal -offset indent
123+profile: 521 samples of a millisecond; each procedure's own, then with its calls'
124+ 16% 27% Screen.put
125+ 9% 93% Parser.feed
126+by place:
127+ 8% src/Screen.raz:275:5
128+.Ed
129+.El
130+.Sh EXIT STATUS
131+.Nm
132+exits 0 if it wrote the program, and 1 if it found an error, which it
133+says on standard error, with the line of source and a mark under the
134+place.
135+.Nm
136+.Cm run
137+exits as the program does.
138+.Sh EXAMPLES
139+Compile and run hello, world:
140+.Bd -literal -offset indent
141+raz Hello.raz && ./Hello
142+raz run Hello.raz
143+.Ed
144+.Pp
145+Build a small program for shipping, then see where its time goes:
146+.Bd -literal -offset indent
147+raz -s -o hello Hello.raz
148+raz -o prog Prog.raz && RAZ_PROFILE=1 ./prog
149+.Ed
150+.Sh SEE ALSO
151+.Xr gdb 1 ;
152+the language,
153+.Pa doc/raz.md ,
154+and the guide to the compiler,
155+.Pa doc/compiler.md ,
156+in raz's sources.

lib/Rt.raz (history) modified +52 -32

@@ -2,14 +2,20 @@
22 calls, fail when a check fails; stop, for the library's own
33 failures; and the handlers that say where the program was when a
44 signal stops it.
5- The compiler finds fail, exit, cExit and places by these names.
5+ The compiler finds fail, exit, cExit, places, profiler,
6+ startProfile, teller and calls by these names.
67 After a failed check or a signal, and on demand with trace, Rt says
78 the calls under way and their variables, from the frames' chain of
89 rbp and the compiler's tables in places. A program run with
910 RAZ_PROFILE set is sampled every millisecond of its time, and at its
1011 end Rt says which procedures and places in the source took it: each
1112 procedure's samples in its own code, and in all, with those in the
12- procedures it was calling.
13+ procedures it was calling. A program built with raz -s or
14+ -noprofile has no profiler: the entry point leaves profiler nil,
15+ nothing reaches startProfile, and the linker leaves it out with all
16+ it uses. Built with -s it has no places either, and no teller: a
17+ failure is told by its address, and calls, which would need the
18+ places, is left out the same way.
1319 The handlers are Linux's on x86-64, set by system calls: what the
1420 kernel hands them is laid out as it is there. *)
1521 module Rt;
@@ -79,7 +85,10 @@
7985 Depth for each, a short one ended by 0; mapped when profiling *)
8086 chains: ^UINT;
8187 numSamples: INT;
82- profiling: BOOL;
88+ profiler: proc; (* startProfile, put here by the entry point, or nil *)
89+ (* calls, put here by the entry point, or nil: a failure in a program
90+ built with raz -s, which has no places, tells no calls *)
91+ teller: proc(ip, bp: UINT; vars: BOOL);
8392 procCount: [MaxProcs]INT;
8493 procTotal: [MaxProcs]INT; (* the samples each procedure was on the chain of *)
8594 placeCount: [MaxPlaces]INT;
@@ -109,7 +118,6 @@
109118 p := atExits[numAtExit];
110119 p();
111120 end;
112- if profiling then profile; end;
113121 if cExit # nil then cExit(I32(status)); end;
114122 Sys.exit(status);
115123 end;
@@ -173,13 +181,33 @@
173181 return ^U8(places + 5) + 16 * places[0] + 28 * places[3] + 12 * places[4];
174182 end;
175183
184+proc putHex(u: UINT);
185+var buf: [16]U8; i: INT;
186+begin
187+ i := 16;
188+ repeat
189+ DEC(i);
190+ buf[i] := Digits[INT(u & 15)];
191+ u := u shr 4;
192+ until u = 0;
193+ put("0x");
194+ put(buf[i..16]);
195+end;
196+
176197 (* Puts where the code at address a comes from, as file:line:col: and
177198 a space, if a is in the program's code: the place with the highest
178199 address up to a. *)
179200 proc where(a: UINT);
180201 var p, best: ^Place; i, n: INT;
181202 begin
182- if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end;
203+ if places = nil then
204+ (* built with raz -s, which keeps no places: the address *)
205+ put("at ");
206+ putHex(a);
207+ put(": ");
208+ return;
209+ end;
210+ if (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end;
183211 n := places[0];
184212 p := ^Place(places + 5);
185213 best := nil;
@@ -224,19 +252,6 @@
224252 return nil;
225253 end;
226254
227-proc putHex(u: UINT);
228-var buf: [16]U8; i: INT;
229-begin
230- i := 16;
231- repeat
232- DEC(i);
233- buf[i] := Digits[INT(u & 15)];
234- u := u shr 4;
235- until u = 0;
236- put("0x");
237- put(buf[i..16]);
238-end;
239-
240255 (* x with six figures after the point, or as m e n beyond them *)
241256 proc putReal(x: F64);
242257 var e, i: INT; f: U64;
@@ -423,7 +438,7 @@
423438 else put("a stack overflow");
424439 end;
425440 say;
426- calls(at, callerFrame(), true);
441+ if teller # nil then teller(at, callerFrame(), true); end;
427442 act := {}; (* the signal's default action *)
428443 _ := SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *)
429444 _ := SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *)
@@ -447,7 +462,7 @@
447462 putInt(len);
448463 end;
449464 say;
450- calls(at, callerFrame(), true);
465+ if teller # nil then teller(at, callerFrame(), true); end;
451466 exit(2);
452467 end;
453468
@@ -487,7 +502,7 @@
487502 put(why);
488503 say;
489504 (* the variables of a frame the stack overflowed in may be past it *)
490- calls(ip, bp, why # "a stack overflow");
505+ if teller # nil then teller(ip, bp, why # "a stack overflow"); end;
491506 end;
492507
493508 (* Whether a is in the program's own code, not C's *)
@@ -583,6 +598,21 @@
583598 end;
584599 end;
585600
601+(* The compiler's, as fail is: if RAZ_PROFILE is set, samples the
602+ program every millisecond of its time, and says at its end what took
603+ it (profile) *)
604+proc startProfile*;
605+begin
606+ if LEN(Sys.env("RAZ_PROFILE")) = 0 then return; end;
607+ chains := ^UINT(Sys.mmap(MaxSamples * Depth * SIZE(UINT)));
608+ if chains = nil then return; end;
609+ act := {handler: onProf, flags: ProfFlags, restorer: sigReturn};
610+ _ := SYSCALL(13, SigProf, ADR(act), 0, 8);
611+ timerv := [0, 1000, 0, 1000]; (* every millisecond, from one on *)
612+ _ := SYSCALL(38, 2, ADR(timerv), 0); (* setitimer(ITIMER_PROF) *)
613+ _ := atExit(profile);
614+end;
615+
586616 (* At the end of a profiled run: which procedures, and which places in
587617 the source, took the time, as shares of the samples *)
588618 proc profile;
@@ -590,7 +620,6 @@
590620 begin
591621 t := [0, 0, 0, 0];
592622 _ := SYSCALL(38, 2, ADR(t), 0); (* setitimer(ITIMER_PROF): no more *)
593- profiling := false;
594623 n := numSamples;
595624 if n > MaxSamples then n := MaxSamples; end;
596625 if (places = nil) or (n = 0) then return; end;
@@ -631,14 +660,5 @@
631660 act := {handler: onSignal, flags: Flags, restorer: sigReturn};
632661 _ := SYSCALL(13, SigSegv, ADR(act), 0, 8); (* rt_sigaction *)
633662 _ := SYSCALL(13, SigFpe, ADR(act), 0, 8);
634- if LEN(Sys.env("RAZ_PROFILE")) > 0 then
635- chains := ^UINT(Sys.mmap(MaxSamples * Depth * SIZE(UINT)));
636- end;
637- if chains # nil then
638- act := {handler: onProf, flags: ProfFlags, restorer: sigReturn};
639- _ := SYSCALL(13, SigProf, ADR(act), 0, 8);
640- timerv := [0, 1000, 0, 1000]; (* every millisecond, from one on *)
641- _ := SYSCALL(38, 2, ADR(timerv), 0); (* setitimer(ITIMER_PROF) *)
642- profiling := true;
643- end;
663+ if profiler # nil then profiler(); end;
644664 end Rt.

src/elf.pas modified +16 -13

@@ -30,6 +30,7 @@
3030 needed: array[0..31] of longint; { the libraries to load, names interned }
3131 numNeeded: longint; { none: a static program }
3232 mainPath: shortstring; { the main module's file, for gdb }
33+ stripped: boolean; { -s: no symbols and no line numbers; the sections are there, empty }
3334
3435 procedure WriteElf(const path: shortstring; entry: longint);
3536
@@ -259,9 +260,9 @@
259260 procedure PutTables;
260261 var c, k, i, at, name: longint;
261262 begin
262- len := extraAdr - Base;
263+ len := extraAdr - roAdr + roOff;
263264 PutStr(Interp);
264- PadTo(extraAdr - Base + symAt);
265+ PadTo(extraAdr - roAdr + roOff + symAt);
265266 PutN(0, 24);
266267 name := strLen;
267268 for c := numChunks - 1 downto 0 do { names go last in the string table }
@@ -290,7 +291,7 @@
290291 len := roOff + roSize;
291292
292293 { the dynamic table, at the start of the writable segment }
293- PadTo(bssAdr - Base);
294+ PadTo(bssOff);
294295 at := 1;
295296 for i := 0 to numNeeded - 1 do begin
296297 PutN(1, 8); PutN(at, 8); { DT_NEEDED }
@@ -349,13 +350,13 @@
349350 PutN(64, 2); PutN(nsh, 2); PutN(6, 2); { .shstrtab is section 6 }
350351
351352 { flags: 4 read, 5 read and run, 6 read and write }
352- if dynamic then Phdr(PtInterp, 4, extraAdr - Base, extraAdr, length(Interp) + 1, length(Interp) + 1, 1);
353+ if dynamic then Phdr(PtInterp, 4, extraAdr - roAdr + roOff, extraAdr, length(Interp) + 1, length(Interp) + 1, 1);
353354 { the first segment maps the headers too, so text can follow them }
354355 Phdr(PtLoad, 5, 0, Base, textOff + textSize, textOff + textSize, Page);
355- if roSize > 0 then Phdr(PtLoad, 4, roOff, Base + roOff, roSize, roSize, Page);
356+ if roSize > 0 then Phdr(PtLoad, 4, roOff, roAdr, roSize, roSize, Page);
356357 if dynamic then begin
357- Phdr(PtLoad, 6, bssAdr - Base, bssAdr, dynSize, bssSize, Page);
358- Phdr(PtDynamic, 6, bssAdr - Base, bssAdr, dynSize, dynSize, 8);
358+ Phdr(PtLoad, 6, bssOff, bssAdr, dynSize, bssSize, Page);
359+ Phdr(PtDynamic, 6, bssOff, bssAdr, dynSize, dynSize, 8);
359360 end else if bssSize > 0 then Phdr(PtLoad, 6, 0, bssAdr, 0, bssSize, Page);
360361 Phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); { a stack that can't run code }
361362
@@ -374,7 +375,7 @@
374375 PutN(0, 24);
375376 nsyms := 1;
376377 for c := 0 to numChunks - 1 do
377- if chunks^[c].live and (chunks^[c].name >= 0) and not chunks^[c].ext then begin
378+ if not stripped and chunks^[c].live and (chunks^[c].name >= 0) and not chunks^[c].ext then begin
378379 s := ChunkName(c);
379380 if strsLen + length(s) + 1 > MemSize(strs) then strs := ReAllocMem(strs, strsLen * 2 + 256);
380381 Move(s[1], strs^[strsLen], length(s));
@@ -398,7 +399,9 @@
398399 shstrOff := len;
399400 s := ShNames;
400401 PutBytes(@s[1], length(s));
401- PutDebug(dbgOff, dbgSize);
402+ if stripped then begin
403+ for c := 0 to 2 do begin dbgOff[c] := len; dbgSize[c] := 0 end;
404+ end else PutDebug(dbgOff, dbgSize);
402405
403406 PadTo((len + 7) div 8 * 8);
404407 shOff := len;
@@ -409,15 +412,15 @@
409412 here is the index the code above relies on. }
410413 Shdr('', 0, 0, 0, 0, 0, 0, 0, 0, 0);
411414 Shdr('.text', 1, 6, Base + textOff, textOff, textSize, 0, 0, 16, 0);
412- Shdr('.rodata', 1, 2, Base + roOff, roOff, roSize, 0, 0, 16, 0);
415+ Shdr('.rodata', 1, 2, roAdr, roOff, roSize, 0, 0, 16, 0);
413416 Shdr('.bss', 8, 3, bssAdr + dynSize, roOff + roSize, bssSize - dynSize, 0, 0, 16, 0);
414417 Shdr('.symtab', 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24);
415418 Shdr('.strtab', 3, 0, 0, strOff, strsLen, 0, 0, 1, 0);
416419 Shdr('.shstrtab', 3, 0, 0, shstrOff, length(ShNames), 0, 0, 1, 0);
417420 if dynamic then begin { the loader's tables, for readelf and objdump }
418- Shdr('.dynamic', 6, 3, bssAdr, bssAdr - Base, dynSize, 8, 0, 8, 16);
419- Shdr('.dynstr', 3, 2, extraAdr + strAt, extraAdr + strAt - Base, strLen, 0, 0, 1, 0);
420- Shdr('.dynsym', 11, 2, extraAdr + symAt, extraAdr + symAt - Base, nsym * 24, 8, 1, 8, 24);
421+ Shdr('.dynamic', 6, 3, bssAdr, bssOff, dynSize, 8, 0, 8, 16);
422+ Shdr('.dynstr', 3, 2, extraAdr + strAt, extraAdr + strAt - roAdr + roOff, strLen, 0, 0, 1, 0);
423+ Shdr('.dynsym', 11, 2, extraAdr + symAt, extraAdr + symAt - roAdr + roOff, nsym * 24, 8, 1, 8, 24);
421424 end;
422425 Shdr('.debug_line', 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0);
423426 Shdr('.debug_info', 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0);

src/gen.pas modified +14 -7

@@ -76,6 +76,10 @@
7676 { The code made so far, to go back to for a second try at a body }
7777 TCodeMark = record pc, fixups, lines, said: longint; end;
7878
79+ { For EntryPoint: the address of chunk, put in the variable whose
80+ chunk is into before the program starts }
81+ TLink = record chunk, into: longint; end;
82+
7983 TCall = record
8084 sig: PType;
8185 param: PObj; { the parameter the next argument is for }
@@ -177,7 +181,7 @@
177181 procedure DeferEnd(L, saved: longint);
178182 procedure RunDefer(at: longint);
179183
180-function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint;
184+function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, exitProc: longint; const links: array of TLink; numLinks: longint): longint;
181185 procedure CodeProc(c: longint; constExpr: TConstFn);
182186
183187 implementation
@@ -1679,10 +1683,11 @@
16791683
16801684 dynamic: the program uses C. first: the first module, whose chain
16811685 gives the order of the bodies. main: the program's main procedure, or
1682- nil. cExit, placesVar and exitProc: the chunks of Rt's cExit, places
1683- and exit. places: the chunk the linker fills with the places (the
1684- address of each line of code, for Rt to say where a check failed). }
1685-function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint;
1686+ nil. cExit and exitProc: the chunks of Rt's cExit and exit. The
1687+ first numLinks of links: the addresses Rt is given, of what the program has: the places, and
1688+ the profiler and calls; what it isn't given, nothing reaches, and the
1689+ linker leaves out. }
1690+function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, exitProc: longint; const links: array of TLink; numLinks: longint): longint;
16861691 var entry, start, i: longint; m: PModule;
16871692 begin
16881693 entry := NewChunk(secText, -1, Intern('_start', 6));
@@ -1718,8 +1723,10 @@
17181723 Lea(RAX, Mem(RSP, 8));
17191724 Store(MemRip(argvChunk, 0), RAX, 8);
17201725 end;
1721- Lea(RAX, MemRip(places, 0));
1722- Store(MemRip(placesVar, 0), RAX, 8);
1726+ for i := 0 to numLinks - 1 do begin
1727+ Lea(RAX, MemRip(links[i].chunk, 0));
1728+ Store(MemRip(links[i].into, 0), RAX, 8);
1729+ end;
17231730 m := first;
17241731 while m <> nil do begin
17251732 if m^.used and (m^.body >= 0) then CallChunk(m^.body);

src/link.pas modified +19 -7

@@ -28,10 +28,12 @@
2828 Page = 4096;
2929
3030 var
31- textOff, textSize: int64; { file offset and size of text }
31+ textOff, textSize: int64; { file offset and size of text, at Base + textOff }
3232 roOff, roSize: int64; { rodata, the room for the loader's tables included }
33+ roAdr: int64; { where rodata is loaded }
3334 extraAdr: int64; { that room }
3435 bssAdr, bssSize: int64; { the writable segment, the loader's table first }
36+ bssOff: int64; { the file offset of that table }
3537 textImg, roImg: PBytes;
3638 placesChunk: longint = -1; { the rodata chunk of the places, made here }
3739
@@ -198,7 +200,14 @@
198200 end;
199201
200202 { Places the live chunks, leaving roExtra bytes at the end of rodata
201- and dynSize at the start of the writable segment. }
203+ and dynSize at the start of the writable segment. The segments follow
204+ each other in the file with no padding, and each is loaded a page
205+ further from its file offset than the one before: rodata at Base +
206+ Page + roOff, the writable segment at Base + 2 * Page + bssOff. So an
207+ address and its offset agree within a page, as the loader needs, and
208+ no two segments, with their different permissions, share a page.
209+ The bss of a program without the loader's table has no bytes in the
210+ file, and starts on a page of its own. }
202211 procedure LinkAll(headerSize, roExtra, dynSize: int64);
203212 var c, f, at: longint; e, p, v: int64;
204213 begin
@@ -206,12 +215,15 @@
206215 e := Place(secText, Base + textOff);
207216 textSize := e - (Base + textOff);
208217 if placesChunk >= 0 then MakePlaces(Base + textOff, e);
209- roOff := (textOff + textSize + Page - 1) div Page * Page;
210- e := Place(secRodata, Base + roOff);
218+ roOff := (textOff + textSize + 31) div 32 * 32;
219+ roAdr := Base + Page + roOff;
220+ e := Place(secRodata, roAdr);
211221 extraAdr := (e + 7) div 8 * 8;
212222 if roExtra > 0 then e := extraAdr + roExtra;
213- roSize := e - (Base + roOff);
214- bssAdr := (Base + roOff + roSize + Page - 1) div Page * Page;
223+ roSize := e - roAdr;
224+ bssOff := (roOff + roSize + 31) div 32 * 32;
225+ if dynSize > 0 then bssAdr := Base + 2 * Page + bssOff
226+ else bssAdr := (roAdr + roSize + Page - 1) div Page * Page;
215227 e := Place(secBss, bssAdr + dynSize);
216228 bssSize := e - bssAdr;
217229
@@ -235,7 +247,7 @@
235247 end;
236248 end;
237249 secRodata: begin
238- p := chunks^[c].adr - (Base + roOff);
250+ p := chunks^[c].adr - roAdr;
239251 Move(rodata^[chunks^[c].start], roImg^[p], chunks^[c].size);
240252 for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do
241253 PInt64(@roImg^[p + dataFixups^[f].at])^ :=

src/raz.pas modified +57 -10

@@ -83,6 +83,24 @@
8383 UsesC := c <> nil;
8484 end;
8585
86+{ What raz -h says }
87+procedure Usage;
88+begin
89+ writeln('usage: raz [option]... Main.raz compiles Main.raz and the modules it imports');
90+ writeln(' raz run [option]... Main.raz [arg]... compiles it and runs it, with the args');
91+ writeln('options:');
92+ writeln(' -o file the program''s file, Main without .raz if left out');
93+ writeln(' -I dir looks for imported modules in dir too, after Main.raz''s own');
94+ writeln(' -Werror a warning counts as an error');
95+ writeln(' -S lists the main module''s code, under each line of source');
96+ writeln(' -s strips symbols, line numbers, the places a failure is told');
97+ writeln(' by and the profiler: a failure says only its address');
98+ writeln(' -noprofile leaves out the profiler, which RAZ_PROFILE turns on');
99+ writeln(' -h, --help says this');
100+ writeln(' --version says the version, and the report it follows');
101+ writeln('see raz(1), and the report, doc/raz.md');
102+end;
103+
86104 function RtObj(rt: PModule; const n: shortstring): PObj;
87105 var o: PObj;
88106 begin
@@ -96,7 +114,18 @@
96114 i, first, entry, fd: longint;
97115 m, rt: PModule;
98116 main: PObj;
99- run: boolean;
117+ run, noProfile: boolean;
118+ links: array[0..2] of TLink;
119+ numLinks: longint;
120+
121+
122+{ Rt is given the address of chunk, in its variable v }
123+procedure AddLink(chunk: longint; v: PObj);
124+begin
125+ links[numLinks].chunk := chunk;
126+ links[numLinks].into := v^.chunk;
127+ inc(numLinks);
128+end;
100129
101130 begin
102131 { constants fold as the machine computes: NaN and infinities, no traps }
@@ -104,6 +133,7 @@
104133 path := '';
105134 outPath := '';
106135 numInc := 0;
136+ noProfile := false;
107137 run := (ParamCount >= 1) and (ParamStr(1) = 'run');
108138 i := 1 + ord(run);
109139 while i <= ParamCount do begin
@@ -120,7 +150,14 @@
120150 werror := true
121151 else if s = '-S' then
122152 listing := true
123- else if s = '--version' then begin
153+ else if s = '-s' then
154+ stripped := true
155+ else if s = '-noprofile' then
156+ noProfile := true
157+ else if (s = '-h') or (s = '--help') then begin
158+ Usage;
159+ Halt(0);
160+ end else if s = '--version' then begin
124161 writeln('raz ', Version, ', report ', Report);
125162 Halt(0);
126163 end else if (s <> '') and (s[1] = '-') then
@@ -132,8 +169,10 @@
132169 end;
133170 inc(i);
134171 end;
135- if path = '' then
136- Fatal('usage: raz [-I dir]... [-o file] [-Werror] [-S] Main.raz, or raz run [options] Main.raz [arg]...');
172+ if path = '' then begin
173+ Usage;
174+ Halt(1);
175+ end;
137176 if run and (outPath <> '') then Fatal('raz run takes no -o');
138177 dir := ParamStr(0);
139178 i := length(dir);
@@ -164,12 +203,20 @@
164203 Finish;
165204
166205 if rt = nil then Fatal('can''t find module Rt');
167- { the places, which the linker makes, for Rt to say where a failed
168- check or a signal was }
169- placesChunk := NewChunk(secRodata, -1, -1);
170- SetData(placesChunk, nil, 0, 8);
171- entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk, placesChunk,
172- RtObj(rt, 'places')^.chunk, RtObj(rt, 'exit')^.chunk);
206+ { what Rt is given: the places, which the linker makes, for Rt to say
207+ where a failed check or a signal was, and the calls under way then
208+ (calls); and the profiler, which needs them too. raz -s gives none,
209+ -noprofile no profiler. }
210+ numLinks := 0;
211+ if not stripped then begin
212+ placesChunk := NewChunk(secRodata, -1, -1);
213+ SetData(placesChunk, nil, 0, 8);
214+ AddLink(placesChunk, RtObj(rt, 'places'));
215+ AddLink(RtObj(rt, 'calls')^.chunk, RtObj(rt, 'teller'));
216+ if not noProfile then AddLink(RtObj(rt, 'startProfile')^.chunk, RtObj(rt, 'profiler'));
217+ end;
218+ entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk,
219+ RtObj(rt, 'exit')^.chunk, links, numLinks);
173220
174221 MarkLive(entry);
175222 if not HasReals then

test/check.sh (history) modified +17 -0

@@ -98,6 +98,23 @@
9898 $raz -o $out/Busy test/list/Busy.raz
9999 if RAZ_PROFILE=1 $out/Busy 2>&1 >/dev/null | sed -n 2p | grep -q 'Busy.spin$'; then pass=$((pass + 1))
100100 else echo "FAIL RAZ_PROFILE: Busy.spin isn't first"; fail=$((fail + 1)); fi
101+# the manual page, which groff must read without a warning
102+if command -v groff > /dev/null; then
103+ groff -mdoc -ww -z doc/raz.1 > $out/man.out 2>&1
104+ if [ ! -s $out/man.out ]; then pass=$((pass + 1))
105+ else echo "FAIL doc/raz.1"; head -5 $out/man.out; fail=$((fail + 1)); fi
106+fi
107+# raz -noprofile leaves the profiler out, and raz -s the places too: a
108+# failed check is told by its address, and the program is smaller
109+$raz -noprofile -o $out/BusyNo test/list/Busy.raz
110+if [ -z "$(RAZ_PROFILE=1 $out/BusyNo 2>&1 >/dev/null)" ]; then pass=$((pass + 1))
111+else echo "FAIL raz -noprofile: a profile"; fail=$((fail + 1)); fi
112+$raz -o $out/Index test/strip/Index.raz
113+$raz -s -o $out/IndexS test/strip/Index.raz
114+$out/IndexS > $out/IndexS.out 2>&1
115+if [ $? = 2 ] && grep -q '^hello$' $out/IndexS.out && grep -q '^at 0x[0-9a-f]*: index 5 is out of range' $out/IndexS.out \
116+ && [ $(wc -c < $out/IndexS) -lt $(wc -c < $out/Index) ]; then pass=$((pass + 1))
117+else echo "FAIL raz -s:"; cat $out/IndexS.out; fail=$((fail + 1)); fi
101118
102119 # raz -S: test/list/X.raz lists as X.expect
103120 for f in test/list/*.raz; do

test/strip/Index.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+module Index;
2+
3+(* For raz -s: an index out of range, told by its address alone. *)
4+import Out;
5+
6+proc main*;
7+var a: [3]INT; i: INT;
8+begin
9+ Out.str("hello\n");
10+ i := 5;
11+ a[i] := 1;
12+end;
13+
14+end Index.