From c16e8a93fb54f4762f8b4a90285cfcc3ca8bcb18 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:33:13 +0100 Subject: [PATCH] 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. --- diff --git a/Makefile b/Makefile index 21fd5e7..858303e 100644 --- a/Makefile +++ b/Makefile @@ -85,16 +85,18 @@ chromium --headless --no-pdf-header-footer --print-to-pdf=$@ build/raz.html 2>/dev/null # raz and its library go together under $(PREFIX)/lib/raz; the link -# on PATH works since raz finds lib/ beside its real path. +# on PATH works since raz finds lib/ beside its real path. The manual +# page goes where man looks, beside bin. PREFIX = $(HOME)/.local install: raz rm -rf $(PREFIX)/lib/raz - mkdir -p $(PREFIX)/lib/raz $(PREFIX)/bin + mkdir -p $(PREFIX)/lib/raz $(PREFIX)/bin $(PREFIX)/share/man/man1 cp -r raz lib $(PREFIX)/lib/raz ln -sf $(PREFIX)/lib/raz/raz $(PREFIX)/bin/raz + cp doc/raz.1 $(PREFIX)/share/man/man1/raz.1 uninstall: - rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz + rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(PREFIX)/share/man/man1/raz.1 .PHONY: units tests check gdb bench stress fmt loc pdf clean install uninstall diff --git a/README.md b/README.md index e9181ba..7e0d9e1 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,8 @@ and builds into the repository's root: make # ./raz - make install # ~/.local/bin/raz, its library in ~/.local/lib/raz + make install # ~/.local/bin/raz, its library in ~/.local/lib/raz, + # and its manual page, raz(1) make uninstall # removes them again Both take `PREFIX=/usr/local` (or another place) instead of `~/.local`. @@ -70,10 +71,13 @@ ## Using it - raz [-I dir]... [-o file] [-Werror] [-S] Main.raz - raz run [-I dir]... [-Werror] Main.raz [arg]... + raz [-I dir]... [-o file] [-Werror] [-S] [-s] [-noprofile] Main.raz + raz run [option]... Main.raz [arg]... + raz -h raz --version +`raz -h` says the options, and `man raz` says more. + `raz Main.raz` compiles module `Main` and every module it imports, and writes the executable `Main`. A module `Name` lives in `Name.raz`, found in the main module's directory, then in each `-I` directory, then in @@ -88,7 +92,10 @@ and of a variable assigned but never read, unless its name starts with `_`; `-Werror` makes warnings errors. `-S` prints the main module's code instead of writing it: each line of source, and under it the -instructions made for it. +instructions made for it. `-s` strips what is there for finding faults: +the symbols and line numbers, the places a failure is told by, and the +profiler, so hello, world is 7 KB instead of 35; `-noprofile` leaves +out only the profiler. At run time, a failed check says where it is and what failed, as in `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends diff --git a/doc/compiler.md b/doc/compiler.md index 58621b2..235fbbe 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -13,12 +13,12 @@ | `src/sym.pas` | 644 | 527 | Types, objects, scopes, layout | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,128 | 936 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,768 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/gen.pas` | 1,775 | 1,294 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,551 | 1,278 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 2,975 | 2,612 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | -| `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 197 | 147 | The command line | +| `src/link.pas` | 259 | 195 | Tree shaking, layout, patching, the places | +| `src/elf.pas` | 438 | 355 | Static and dynamic ELF, symbol table, line numbers | +| `src/raz.pas` | 244 | 187 | 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. @@ -766,11 +766,20 @@ own code, and `exit` has `profile` say which procedures and places those fell in, by the same tables: each procedure's samples in its own code, and its total, counting once each sample it was on the chain of. +The entry point gives Rt what the program has (`TLink`): the places, +`calls`, which tells the calls under way, and the profiler's start. +What it isn't given, nothing reaches, so the linker leaves it out: +`raz -noprofile` gives no profiler, and `raz -s` none of the three, +whose failures say only their address (and `elf.pas` writes no symbols +or line numbers). Hello is 35 KB, 25 KB without the profiler, and 7 KB +stripped. -The layout is chosen so the file can be mapped as it lies: text just -after the headers, rodata on the next page, then a writable page that -begins with the dynamic loader's table (in a program that uses C) and -goes on as bss. +The layout is chosen so the file can be mapped as it lies, with no +padding: text just after the headers, then rodata, then the dynamic +loader's table (in a program that uses C), each loaded a page further +from its place in the file than the one before, so that no two +segments, with their different permissions, share a page. bss follows +the loader's table, or begins a page of its own. ## elf.pas: the executable diff --git a/doc/raz.1 b/doc/raz.1 new file mode 100644 index 0000000..e1f3913 --- /dev/null +++ b/doc/raz.1 @@ -0,0 +1,156 @@ +.\" raz(1) - the raz compiler +.Dd October 12, 2026 +.Dt RAZ 1 +.Os +.Sh NAME +.Nm raz +.Nd compile a raz program to a Linux executable +.Sh SYNOPSIS +.Nm +.Op Ar option ... +.Ar Main.raz +.Nm +.Cm run +.Op Ar option ... +.Ar Main.raz +.Op Ar arg ... +.Sh DESCRIPTION +.Nm +compiles the module in +.Ar Main.raz , +and every module it imports, to an executable for x86-64 Linux, with +no assembler, linker or C library in between. +A program that names a C library in a +.Ic link +line, or declares an +.Ic extern +procedure, is linked against C dynamically; any other is static. +.Pp +A module +.Ar Name +is found as +.Ar Name Ns .linux-x86_64.raz +or +.Ar Name Ns .raz +in the directory of +.Ar Main.raz , +then in each +.Fl I +directory in turn, then in the library, +.Pa lib/ +beside the +.Nm +executable. +A program has one module of each name: a module found in two of these +places is an error, unless one of the files is a symbolic link to the +other, which chooses it. +.Pp +.Nm +.Cm run +compiles the same way, then runs the program with the +.Ar arg Ns s , +writing no file: its output and exit status are those of the command. +.Pp +Every array index and slice is checked, unless the module says +.Ic check off ; +a failed check, a nil pointer, a division by zero or a stack overflow +ends the program with status 2, and says on standard error where it +was, the calls under way, and their variables: +.Bd -literal -offset indent +Bad.raz:6:7: index 5 is out of range for a length of 3 + in Bad.main at Bad.raz:6:7: i = 5 +.Ed +.Pp +The executable has a symbol table and line numbers for +.Xr gdb 1 , +.Xr nm 1 +and +.Xr perf 1 , +unless built with +.Fl s . +.Sh OPTIONS +.Bl -tag -width "-noprofile" +.It Fl o Ar file +Write the executable to +.Ar file . +Left out, it is +.Ar Main.raz +without +.Pa .raz . +.Cm run +takes no +.Fl o . +.It Fl I Ar dir +Look for imported modules in +.Ar dir +too, after the main module's own directory and before the library. +It may be given up to 16 times. +.It Fl Werror +A warning counts as an error. +.It Fl S +List the main module's machine code, each instruction under the line +of source it was made for, on standard output, and write no +executable. +.It Fl s +Strip the program of what is there for finding faults: its symbol +table, its line numbers, the places a failure is told by, and the +profiler. +A failure then says only its address, and the program is much +smaller: hello, world is 7 KB, against 35 KB. +.It Fl noprofile +Leave out the profiler, which +.Ev RAZ_PROFILE +turns on, but keep the rest. +.It Fl h , Fl \-help +Say the options. +.It Fl \-version +Say the version of +.Nm , +and the version of the report it follows. +.El +.Sh ENVIRONMENT +.Bl -tag -width "RAZ_PROFILE" +.It Ev RAZ_PROFILE +Set to anything, in the environment of a program built without +.Fl s +or +.Fl noprofile , +it has the program sample itself every millisecond of its time, and +say at its end, on standard error, which procedures took the most of +it, each with its share in its own code and its share in all, with the +procedures it called, and which places in the source: +.Bd -literal -offset indent +profile: 521 samples of a millisecond; each procedure's own, then with its calls' + 16% 27% Screen.put + 9% 93% Parser.feed +by place: + 8% src/Screen.raz:275:5 +.Ed +.El +.Sh EXIT STATUS +.Nm +exits 0 if it wrote the program, and 1 if it found an error, which it +says on standard error, with the line of source and a mark under the +place. +.Nm +.Cm run +exits as the program does. +.Sh EXAMPLES +Compile and run hello, world: +.Bd -literal -offset indent +raz Hello.raz && ./Hello +raz run Hello.raz +.Ed +.Pp +Build a small program for shipping, then see where its time goes: +.Bd -literal -offset indent +raz -s -o hello Hello.raz +raz -o prog Prog.raz && RAZ_PROFILE=1 ./prog +.Ed +.Sh SEE ALSO +.Xr gdb 1 ; +the language, +.Pa doc/raz.md , +and the guide to the compiler, +.Pa doc/compiler.md , +in raz's sources. diff --git a/lib/Rt.raz b/lib/Rt.raz index 7c65ee0..4f7d684 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -2,14 +2,20 @@ calls, fail when a check fails; stop, for the library's own failures; and the handlers that say where the program was when a signal stops it. - The compiler finds fail, exit, cExit and places by these names. + The compiler finds fail, exit, cExit, places, profiler, + startProfile, teller and calls by these names. After a failed check or a signal, and on demand with trace, Rt says the calls under way and their variables, from the frames' chain of rbp and the compiler's tables in places. A program run with RAZ_PROFILE set is sampled every millisecond of its time, and at its end Rt says which procedures and places in the source took it: each procedure's samples in its own code, and in all, with those in the - procedures it was calling. + procedures it was calling. A program built with raz -s or + -noprofile has no profiler: the entry point leaves profiler nil, + nothing reaches startProfile, and the linker leaves it out with all + it uses. Built with -s it has no places either, and no teller: a + failure is told by its address, and calls, which would need the + places, is left out the same way. The handlers are Linux's on x86-64, set by system calls: what the kernel hands them is laid out as it is there. *) module Rt; @@ -79,7 +85,10 @@ Depth for each, a short one ended by 0; mapped when profiling *) chains: ^UINT; numSamples: INT; - profiling: BOOL; + profiler: proc; (* startProfile, put here by the entry point, or nil *) + (* calls, put here by the entry point, or nil: a failure in a program + built with raz -s, which has no places, tells no calls *) + teller: proc(ip, bp: UINT; vars: BOOL); procCount: [MaxProcs]INT; procTotal: [MaxProcs]INT; (* the samples each procedure was on the chain of *) placeCount: [MaxPlaces]INT; @@ -109,7 +118,6 @@ p := atExits[numAtExit]; p(); end; - if profiling then profile; end; if cExit # nil then cExit(I32(status)); end; Sys.exit(status); end; @@ -173,13 +181,33 @@ return ^U8(places + 5) + 16 * places[0] + 28 * places[3] + 12 * places[4]; end; +proc putHex(u: UINT); +var buf: [16]U8; i: INT; +begin + i := 16; + repeat + DEC(i); + buf[i] := Digits[INT(u & 15)]; + u := u shr 4; + until u = 0; + put("0x"); + put(buf[i..16]); +end; + (* Puts where the code at address a comes from, as file:line:col: and a space, if a is in the program's code: the place with the highest address up to a. *) proc where(a: UINT); var p, best: ^Place; i, n: INT; begin - if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end; + if places = nil then + (* built with raz -s, which keeps no places: the address *) + put("at "); + putHex(a); + put(": "); + return; + end; + if (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end; n := places[0]; p := ^Place(places + 5); best := nil; @@ -224,19 +252,6 @@ return nil; end; -proc putHex(u: UINT); -var buf: [16]U8; i: INT; -begin - i := 16; - repeat - DEC(i); - buf[i] := Digits[INT(u & 15)]; - u := u shr 4; - until u = 0; - put("0x"); - put(buf[i..16]); -end; - (* x with six figures after the point, or as m e n beyond them *) proc putReal(x: F64); var e, i: INT; f: U64; @@ -423,7 +438,7 @@ else put("a stack overflow"); end; say; - calls(at, callerFrame(), true); + if teller # nil then teller(at, callerFrame(), true); end; act := {}; (* the signal's default action *) _ := SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) _ := SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *) @@ -447,7 +462,7 @@ putInt(len); end; say; - calls(at, callerFrame(), true); + if teller # nil then teller(at, callerFrame(), true); end; exit(2); end; @@ -487,7 +502,7 @@ put(why); say; (* the variables of a frame the stack overflowed in may be past it *) - calls(ip, bp, why # "a stack overflow"); + if teller # nil then teller(ip, bp, why # "a stack overflow"); end; end; (* Whether a is in the program's own code, not C's *) @@ -583,6 +598,21 @@ end; end; +(* The compiler's, as fail is: if RAZ_PROFILE is set, samples the + program every millisecond of its time, and says at its end what took + it (profile) *) +proc startProfile*; +begin + if LEN(Sys.env("RAZ_PROFILE")) = 0 then return; end; + chains := ^UINT(Sys.mmap(MaxSamples * Depth * SIZE(UINT))); + if chains = nil then return; end; + act := {handler: onProf, flags: ProfFlags, restorer: sigReturn}; + _ := SYSCALL(13, SigProf, ADR(act), 0, 8); + timerv := [0, 1000, 0, 1000]; (* every millisecond, from one on *) + _ := SYSCALL(38, 2, ADR(timerv), 0); (* setitimer(ITIMER_PROF) *) + _ := atExit(profile); +end; + (* At the end of a profiled run: which procedures, and which places in the source, took the time, as shares of the samples *) proc profile; @@ -590,7 +620,6 @@ begin t := [0, 0, 0, 0]; _ := SYSCALL(38, 2, ADR(t), 0); (* setitimer(ITIMER_PROF): no more *) - profiling := false; n := numSamples; if n > MaxSamples then n := MaxSamples; end; if (places = nil) or (n = 0) then return; end; @@ -631,14 +660,5 @@ act := {handler: onSignal, flags: Flags, restorer: sigReturn}; _ := SYSCALL(13, SigSegv, ADR(act), 0, 8); (* rt_sigaction *) _ := SYSCALL(13, SigFpe, ADR(act), 0, 8); - if LEN(Sys.env("RAZ_PROFILE")) > 0 then - chains := ^UINT(Sys.mmap(MaxSamples * Depth * SIZE(UINT))); - end; - if chains # nil then - act := {handler: onProf, flags: ProfFlags, restorer: sigReturn}; - _ := SYSCALL(13, SigProf, ADR(act), 0, 8); - timerv := [0, 1000, 0, 1000]; (* every millisecond, from one on *) - _ := SYSCALL(38, 2, ADR(timerv), 0); (* setitimer(ITIMER_PROF) *) - profiling := true; - end; + if profiler # nil then profiler(); end; end Rt. diff --git a/src/elf.pas b/src/elf.pas index 776fc3b..09277a7 100644 --- a/src/elf.pas +++ b/src/elf.pas @@ -30,6 +30,7 @@ needed: array[0..31] of longint; { the libraries to load, names interned } numNeeded: longint; { none: a static program } mainPath: shortstring; { the main module's file, for gdb } + stripped: boolean; { -s: no symbols and no line numbers; the sections are there, empty } procedure WriteElf(const path: shortstring; entry: longint); @@ -259,9 +260,9 @@ procedure PutTables; var c, k, i, at, name: longint; begin - len := extraAdr - Base; + len := extraAdr - roAdr + roOff; PutStr(Interp); - PadTo(extraAdr - Base + symAt); + PadTo(extraAdr - roAdr + roOff + symAt); PutN(0, 24); name := strLen; for c := numChunks - 1 downto 0 do { names go last in the string table } @@ -290,7 +291,7 @@ len := roOff + roSize; { the dynamic table, at the start of the writable segment } - PadTo(bssAdr - Base); + PadTo(bssOff); at := 1; for i := 0 to numNeeded - 1 do begin PutN(1, 8); PutN(at, 8); { DT_NEEDED } @@ -349,13 +350,13 @@ PutN(64, 2); PutN(nsh, 2); PutN(6, 2); { .shstrtab is section 6 } { flags: 4 read, 5 read and run, 6 read and write } - if dynamic then Phdr(PtInterp, 4, extraAdr - Base, extraAdr, length(Interp) + 1, length(Interp) + 1, 1); + if dynamic then Phdr(PtInterp, 4, extraAdr - roAdr + roOff, extraAdr, length(Interp) + 1, length(Interp) + 1, 1); { the first segment maps the headers too, so text can follow them } Phdr(PtLoad, 5, 0, Base, textOff + textSize, textOff + textSize, Page); - if roSize > 0 then Phdr(PtLoad, 4, roOff, Base + roOff, roSize, roSize, Page); + if roSize > 0 then Phdr(PtLoad, 4, roOff, roAdr, roSize, roSize, Page); if dynamic then begin - Phdr(PtLoad, 6, bssAdr - Base, bssAdr, dynSize, bssSize, Page); - Phdr(PtDynamic, 6, bssAdr - Base, bssAdr, dynSize, dynSize, 8); + Phdr(PtLoad, 6, bssOff, bssAdr, dynSize, bssSize, Page); + Phdr(PtDynamic, 6, bssOff, bssAdr, dynSize, dynSize, 8); end else if bssSize > 0 then Phdr(PtLoad, 6, 0, bssAdr, 0, bssSize, Page); Phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); { a stack that can't run code } @@ -374,7 +375,7 @@ PutN(0, 24); nsyms := 1; for c := 0 to numChunks - 1 do - if chunks^[c].live and (chunks^[c].name >= 0) and not chunks^[c].ext then begin + if not stripped and chunks^[c].live and (chunks^[c].name >= 0) and not chunks^[c].ext then begin s := ChunkName(c); if strsLen + length(s) + 1 > MemSize(strs) then strs := ReAllocMem(strs, strsLen * 2 + 256); Move(s[1], strs^[strsLen], length(s)); @@ -398,7 +399,9 @@ shstrOff := len; s := ShNames; PutBytes(@s[1], length(s)); - PutDebug(dbgOff, dbgSize); + if stripped then begin + for c := 0 to 2 do begin dbgOff[c] := len; dbgSize[c] := 0 end; + end else PutDebug(dbgOff, dbgSize); PadTo((len + 7) div 8 * 8); shOff := len; @@ -409,15 +412,15 @@ here is the index the code above relies on. } Shdr('', 0, 0, 0, 0, 0, 0, 0, 0, 0); Shdr('.text', 1, 6, Base + textOff, textOff, textSize, 0, 0, 16, 0); - Shdr('.rodata', 1, 2, Base + roOff, roOff, roSize, 0, 0, 16, 0); + Shdr('.rodata', 1, 2, roAdr, roOff, roSize, 0, 0, 16, 0); Shdr('.bss', 8, 3, bssAdr + dynSize, roOff + roSize, bssSize - dynSize, 0, 0, 16, 0); Shdr('.symtab', 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); Shdr('.strtab', 3, 0, 0, strOff, strsLen, 0, 0, 1, 0); Shdr('.shstrtab', 3, 0, 0, shstrOff, length(ShNames), 0, 0, 1, 0); if dynamic then begin { the loader's tables, for readelf and objdump } - Shdr('.dynamic', 6, 3, bssAdr, bssAdr - Base, dynSize, 8, 0, 8, 16); - Shdr('.dynstr', 3, 2, extraAdr + strAt, extraAdr + strAt - Base, strLen, 0, 0, 1, 0); - Shdr('.dynsym', 11, 2, extraAdr + symAt, extraAdr + symAt - Base, nsym * 24, 8, 1, 8, 24); + Shdr('.dynamic', 6, 3, bssAdr, bssOff, dynSize, 8, 0, 8, 16); + Shdr('.dynstr', 3, 2, extraAdr + strAt, extraAdr + strAt - roAdr + roOff, strLen, 0, 0, 1, 0); + Shdr('.dynsym', 11, 2, extraAdr + symAt, extraAdr + symAt - roAdr + roOff, nsym * 24, 8, 1, 8, 24); end; Shdr('.debug_line', 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0); Shdr('.debug_info', 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0); diff --git a/src/gen.pas b/src/gen.pas index a5d93a3..e5323ac 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -76,6 +76,10 @@ { The code made so far, to go back to for a second try at a body } TCodeMark = record pc, fixups, lines, said: longint; end; + { For EntryPoint: the address of chunk, put in the variable whose + chunk is into before the program starts } + TLink = record chunk, into: longint; end; + TCall = record sig: PType; param: PObj; { the parameter the next argument is for } @@ -177,7 +181,7 @@ procedure DeferEnd(L, saved: longint); procedure RunDefer(at: longint); -function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; +function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, exitProc: longint; const links: array of TLink; numLinks: longint): longint; procedure CodeProc(c: longint; constExpr: TConstFn); implementation @@ -1679,10 +1683,11 @@ dynamic: the program uses C. first: the first module, whose chain gives the order of the bodies. main: the program's main procedure, or - nil. cExit, placesVar and exitProc: the chunks of Rt's cExit, places - and exit. places: the chunk the linker fills with the places (the - address of each line of code, for Rt to say where a check failed). } -function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; + nil. cExit and exitProc: the chunks of Rt's cExit and exit. The + first numLinks of links: the addresses Rt is given, of what the program has: the places, and + the profiler and calls; what it isn't given, nothing reaches, and the + linker leaves out. } +function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, exitProc: longint; const links: array of TLink; numLinks: longint): longint; var entry, start, i: longint; m: PModule; begin entry := NewChunk(secText, -1, Intern('_start', 6)); @@ -1718,8 +1723,10 @@ Lea(RAX, Mem(RSP, 8)); Store(MemRip(argvChunk, 0), RAX, 8); end; - Lea(RAX, MemRip(places, 0)); - Store(MemRip(placesVar, 0), RAX, 8); + for i := 0 to numLinks - 1 do begin + Lea(RAX, MemRip(links[i].chunk, 0)); + Store(MemRip(links[i].into, 0), RAX, 8); + end; m := first; while m <> nil do begin if m^.used and (m^.body >= 0) then CallChunk(m^.body); diff --git a/src/link.pas b/src/link.pas index dabec8e..952f201 100644 --- a/src/link.pas +++ b/src/link.pas @@ -28,10 +28,12 @@ Page = 4096; var - textOff, textSize: int64; { file offset and size of text } + textOff, textSize: int64; { file offset and size of text, at Base + textOff } roOff, roSize: int64; { rodata, the room for the loader's tables included } + roAdr: int64; { where rodata is loaded } extraAdr: int64; { that room } bssAdr, bssSize: int64; { the writable segment, the loader's table first } + bssOff: int64; { the file offset of that table } textImg, roImg: PBytes; placesChunk: longint = -1; { the rodata chunk of the places, made here } @@ -198,7 +200,14 @@ end; { Places the live chunks, leaving roExtra bytes at the end of rodata - and dynSize at the start of the writable segment. } + and dynSize at the start of the writable segment. The segments follow + each other in the file with no padding, and each is loaded a page + further from its file offset than the one before: rodata at Base + + Page + roOff, the writable segment at Base + 2 * Page + bssOff. So an + address and its offset agree within a page, as the loader needs, and + no two segments, with their different permissions, share a page. + The bss of a program without the loader's table has no bytes in the + file, and starts on a page of its own. } procedure LinkAll(headerSize, roExtra, dynSize: int64); var c, f, at: longint; e, p, v: int64; begin @@ -206,12 +215,15 @@ e := Place(secText, Base + textOff); textSize := e - (Base + textOff); if placesChunk >= 0 then MakePlaces(Base + textOff, e); - roOff := (textOff + textSize + Page - 1) div Page * Page; - e := Place(secRodata, Base + roOff); + roOff := (textOff + textSize + 31) div 32 * 32; + roAdr := Base + Page + roOff; + e := Place(secRodata, roAdr); extraAdr := (e + 7) div 8 * 8; if roExtra > 0 then e := extraAdr + roExtra; - roSize := e - (Base + roOff); - bssAdr := (Base + roOff + roSize + Page - 1) div Page * Page; + roSize := e - roAdr; + bssOff := (roOff + roSize + 31) div 32 * 32; + if dynSize > 0 then bssAdr := Base + 2 * Page + bssOff + else bssAdr := (roAdr + roSize + Page - 1) div Page * Page; e := Place(secBss, bssAdr + dynSize); bssSize := e - bssAdr; @@ -235,7 +247,7 @@ end; end; secRodata: begin - p := chunks^[c].adr - (Base + roOff); + p := chunks^[c].adr - roAdr; Move(rodata^[chunks^[c].start], roImg^[p], chunks^[c].size); for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do PInt64(@roImg^[p + dataFixups^[f].at])^ := diff --git a/src/raz.pas b/src/raz.pas index 7cbcb4b..3959c29 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -83,6 +83,24 @@ UsesC := c <> nil; end; +{ What raz -h says } +procedure Usage; +begin + writeln('usage: raz [option]... Main.raz compiles Main.raz and the modules it imports'); + writeln(' raz run [option]... Main.raz [arg]... compiles it and runs it, with the args'); + writeln('options:'); + writeln(' -o file the program''s file, Main without .raz if left out'); + writeln(' -I dir looks for imported modules in dir too, after Main.raz''s own'); + writeln(' -Werror a warning counts as an error'); + writeln(' -S lists the main module''s code, under each line of source'); + writeln(' -s strips symbols, line numbers, the places a failure is told'); + writeln(' by and the profiler: a failure says only its address'); + writeln(' -noprofile leaves out the profiler, which RAZ_PROFILE turns on'); + writeln(' -h, --help says this'); + writeln(' --version says the version, and the report it follows'); + writeln('see raz(1), and the report, doc/raz.md'); +end; + function RtObj(rt: PModule; const n: shortstring): PObj; var o: PObj; begin @@ -96,7 +114,18 @@ i, first, entry, fd: longint; m, rt: PModule; main: PObj; - run: boolean; + run, noProfile: boolean; + links: array[0..2] of TLink; + numLinks: longint; + + +{ Rt is given the address of chunk, in its variable v } +procedure AddLink(chunk: longint; v: PObj); +begin + links[numLinks].chunk := chunk; + links[numLinks].into := v^.chunk; + inc(numLinks); +end; begin { constants fold as the machine computes: NaN and infinities, no traps } @@ -104,6 +133,7 @@ path := ''; outPath := ''; numInc := 0; + noProfile := false; run := (ParamCount >= 1) and (ParamStr(1) = 'run'); i := 1 + ord(run); while i <= ParamCount do begin @@ -120,7 +150,14 @@ werror := true else if s = '-S' then listing := true - else if s = '--version' then begin + else if s = '-s' then + stripped := true + else if s = '-noprofile' then + noProfile := true + else if (s = '-h') or (s = '--help') then begin + Usage; + Halt(0); + end else if s = '--version' then begin writeln('raz ', Version, ', report ', Report); Halt(0); end else if (s <> '') and (s[1] = '-') then @@ -132,8 +169,10 @@ end; inc(i); end; - if path = '' then - Fatal('usage: raz [-I dir]... [-o file] [-Werror] [-S] Main.raz, or raz run [options] Main.raz [arg]...'); + if path = '' then begin + Usage; + Halt(1); + end; if run and (outPath <> '') then Fatal('raz run takes no -o'); dir := ParamStr(0); i := length(dir); @@ -164,12 +203,20 @@ Finish; if rt = nil then Fatal('can''t find module Rt'); - { the places, which the linker makes, for Rt to say where a failed - check or a signal was } - placesChunk := NewChunk(secRodata, -1, -1); - SetData(placesChunk, nil, 0, 8); - entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk, placesChunk, - RtObj(rt, 'places')^.chunk, RtObj(rt, 'exit')^.chunk); + { what Rt is given: the places, which the linker makes, for Rt to say + where a failed check or a signal was, and the calls under way then + (calls); and the profiler, which needs them too. raz -s gives none, + -noprofile no profiler. } + numLinks := 0; + if not stripped then begin + placesChunk := NewChunk(secRodata, -1, -1); + SetData(placesChunk, nil, 0, 8); + AddLink(placesChunk, RtObj(rt, 'places')); + AddLink(RtObj(rt, 'calls')^.chunk, RtObj(rt, 'teller')); + if not noProfile then AddLink(RtObj(rt, 'startProfile')^.chunk, RtObj(rt, 'profiler')); + end; + entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk, + RtObj(rt, 'exit')^.chunk, links, numLinks); MarkLive(entry); if not HasReals then diff --git a/test/check.sh b/test/check.sh index b9e3dc8..193a018 100755 --- a/test/check.sh +++ b/test/check.sh @@ -98,6 +98,23 @@ $raz -o $out/Busy test/list/Busy.raz if RAZ_PROFILE=1 $out/Busy 2>&1 >/dev/null | sed -n 2p | grep -q 'Busy.spin$'; then pass=$((pass + 1)) else echo "FAIL RAZ_PROFILE: Busy.spin isn't first"; fail=$((fail + 1)); fi +# the manual page, which groff must read without a warning +if command -v groff > /dev/null; then + groff -mdoc -ww -z doc/raz.1 > $out/man.out 2>&1 + if [ ! -s $out/man.out ]; then pass=$((pass + 1)) + else echo "FAIL doc/raz.1"; head -5 $out/man.out; fail=$((fail + 1)); fi +fi +# raz -noprofile leaves the profiler out, and raz -s the places too: a +# failed check is told by its address, and the program is smaller +$raz -noprofile -o $out/BusyNo test/list/Busy.raz +if [ -z "$(RAZ_PROFILE=1 $out/BusyNo 2>&1 >/dev/null)" ]; then pass=$((pass + 1)) +else echo "FAIL raz -noprofile: a profile"; fail=$((fail + 1)); fi +$raz -o $out/Index test/strip/Index.raz +$raz -s -o $out/IndexS test/strip/Index.raz +$out/IndexS > $out/IndexS.out 2>&1 +if [ $? = 2 ] && grep -q '^hello$' $out/IndexS.out && grep -q '^at 0x[0-9a-f]*: index 5 is out of range' $out/IndexS.out \ + && [ $(wc -c < $out/IndexS) -lt $(wc -c < $out/Index) ]; then pass=$((pass + 1)) +else echo "FAIL raz -s:"; cat $out/IndexS.out; fail=$((fail + 1)); fi # raz -S: test/list/X.raz lists as X.expect for f in test/list/*.raz; do diff --git a/test/strip/Index.raz b/test/strip/Index.raz new file mode 100644 index 0000000..61ba164 --- /dev/null +++ b/test/strip/Index.raz @@ -0,0 +1,14 @@ +module Index; + +(* For raz -s: an index out of range, told by its address alone. *) +import Out; + +proc main*; +var a: [3]INT; i: INT; +begin + Out.str("hello\n"); + i := 5; + a[i] := 1; +end; + +end Index.